http.c
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "http.h"
#include "rssi_list.h"
void error(char* err, int sock){
printf("%s %d", err, sock);
close(sock);
}
int main(int argc , char *argv[]){
//udp_listening(char *msg,char *host);
pthread_t pcap;
pthread_t udp;//our thread for listen to UDP
char *mess_udp = "UDP";
//struct thread_params tp;
int * my_ret = 0;
//We create the thread
pthread_create(&pcap, NULL, &pcap_function, NULL);//(void *) &tp );
//and launch the threads
pthread_join(pcap, NULL); //void(**) &my_ret);
printf("pcap function launched\n");
printf("yes2");
int sock, sockansw;
struct sockaddr_in my_addr; // my address
struct sockaddr_in their_addr; // connected address
int addr_len, numbytes;
char buf[MAXBUFLEN];
char *message="";
Element *rssi_list = malloc(sizeof(Element));
/* We create the socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1){
error("ERROR creating socket", sock);
exit(1);
}
struct hostent *hostinfo = NULL;
struct sockaddr_in server = { 0 }; /* initialise the structure */
/*
const char *hostname = host;
hostinfo = gethostbyname(hostname); /* get host informations */
//if (hostinfo == NULL){ /* host doesn't exist */
/* fprintf (stderr, "ERROR,Unknown host %s.\n", hostname);
exit(1);
}*/
/* fill in the structure */
memcpy(&server.sin_addr, hostinfo->h_addr, hostinfo->h_length);
server.sin_port = htons(PORT);
server.sin_family = AF_INET;
//Bind
if( bind(sock,(struct sockaddr *)&server , sizeof(server)) < 0){
fprintf (stderr, "ERROR,Bind fail %s.\n");
exit(1);
}
printf("bind done\n");
sleep(1); //We wait a little bit
printf("pcap function launched\n");
addr_len = sizeof(struct sockaddr);
while (1) { //listening
printf("Waiting... \n");
if ((numbytes=recvfrom(sock, buf, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { //wait for reception
perror("recvfrom");
exit(1);
}
printf("Packet received\n");
buf[numbytes] = '\0';
if ((sockansw = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { //We create the answer socket
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(THEIRPORT); // short, network byte order
bzero(&(their_addr.sin_zero), 8);
//We send the answer
char * answer;
char * buffer="";
answer = build_buffer_full(rssi_list, buffer, message);
if ((numbytes=sendto(sock, answer, strlen(answer), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
close(sockansw); //We close the answer socket
}
/* close the socket */
close(sock);
pthread_exit(0); //We exit the thread
printf("Complete");
//printf("%d",&my_ret);
return 0;
}