-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsocket_utils.c
61 lines (50 loc) · 1.5 KB
/
socket_utils.c
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
#include "socket_utils.h"
#include "c_utils.c"
void *get_in_addr(struct sockaddr *sa){
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
/*
* Set up a UDP connection (not really connected) so we can send()
* and recv().
* param: struct addrinfo name_server. info about where to connect to.
* return: a file descriptor that can be sent and received from.
*/
int set_up_socket(struct addrinfo *server){
int sockfd;
struct addrinfo hints, *p;
int x;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((x = getaddrinfo(NAME_SERVER, DNS_PORT, &hints, &server)) != 0) {
perror("getaddrinfo:\n");
return 1;
}
// loop through all the results and make a socket
for(p = server; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
perror("talker: socket");
continue;
}
server = p;
break;
}
if (p == NULL) {
printf("bind failed\n");
return 1;
}
return sockfd;
}
/*
* Send data in the DNS_REQUEST struct to the file descriptor sockfd.
*/
int send_request(struct DNS_REQUEST* data, unsigned char *answer){
int nb;
int sockfd = client_get_socket(DNS_PORT,NAME_SERVER);
send(sockfd,data->query,data->size,0);
nb = recv(sockfd,answer,512,0);
return 0;
}