-
Notifications
You must be signed in to change notification settings - Fork 3
/
query.c
129 lines (108 loc) · 3.42 KB
/
query.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include "query.h"
#include "dns.h"
#define RESPONSE_BUFFER_SIZE 1000
// numer of query attempts
unsigned int attempts = 0;
void *query_dns_server(void *request_buffer, int *packet_size,
const char *server, int port, int timeout, int retries,
char *error_message) {
int sd, bytes_sent, bytes_received;
char *response_buffer;
struct sockaddr_in server_addr;
struct sigaction handler; // for alarm handling
/* Open a socket for sending UDP datagrams. */
sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sd < 0) {
strncpy(error_message, "could not open socket", ERROR_BUFFER);
return 0;
}
/* Setup the destination address for the packet. */
memset(&server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(server);
server_addr.sin_port = htons(port);
/* Setup signal handler for alarm/timeout. */
handler.sa_handler = handle_alarm;
handler.sa_flags = 0;
if (sigfillset(&handler.sa_mask) < 0) {
strncpy(error_message, "sigfillset() failed", ERROR_BUFFER);
close(sd);
return 0;
}
if (sigaction(SIGALRM, &handler, 0) < 0) {
strncpy(error_message, "sigaction() failed for SIGALRM", ERROR_BUFFER);
close(sd);
return 0;
}
fcntl(sd,F_SETOWN, getpid());
/* Allocate space to receive the response packet. */
response_buffer = malloc(RESPONSE_BUFFER_SIZE);
if (response_buffer == 0) {
strncpy(error_message, "could not allocate memory for response", ERROR_BUFFER);
close(sd);
return 0;
}
/* Send the packet and verify that all of it was sent. */
bytes_sent = sendto(sd, request_buffer, *packet_size, 0,
(struct sockaddr *)&server_addr,
sizeof(struct sockaddr_in));
if (bytes_sent != *packet_size) {
strncpy(error_message, "full request packet was not sent", ERROR_BUFFER);
close(sd);
free(response_buffer);
return 0;
}
/* Start the timer and wait for the response from the server. */
alarm(timeout);
while ((bytes_received = recvfrom(sd, response_buffer,
RESPONSE_BUFFER_SIZE - 1,
0, 0, 0)) < 0) {
/* Check if the timeout signal went off. */
if (errno == EINTR) {
if (attempts < retries) {
/* If we still have more retries left, re-send the request
packet and reset the timeout alarm. */
bytes_sent = sendto(sd, request_buffer, *packet_size, 0,
(struct sockaddr *)&server_addr,
sizeof(struct sockaddr_in));
if (bytes_sent != *packet_size) {
strncpy(error_message, "full request packet on retry "
"was not sent", ERROR_BUFFER);
close(sd);
free(response_buffer);
return 0;
}
alarm(timeout);
} else {
/* Too many retry attempts, return early. */
strncpy(error_message, "no response from server", ERROR_BUFFER);
close(sd);
free(response_buffer);
return 0;
}
}
}
/* Received the response packet so we can disable the timeout. */
alarm(0);
/* Reset the packet size add a null terminator to the buffer. */
*packet_size = bytes_received;
response_buffer[bytes_received] = 0;
close(sd);
return response_buffer;
}
/* Interrupt handler for network timeouts. */
void handle_alarm(int time)
{
attempts += 1;
}
void free_response_buffer(void *buffer) {
free(buffer);
}