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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
** Copyright 1998 - 2011 Double Precision, Inc.
** See COPYING for distribution information.
*/
#include "config.h"
#include "rfc1035.h"
#include <stdio.h>
#include <string.h>
#include "soxwrap/soxwrap.h"
#include <sys/types.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
int rfc1035_open_udp(int *af)
{
return (rfc1035_mksocket(SOCK_DGRAM, 0, af));
}
int rfc1035_send_udp(int fd, const struct sockaddr *sin, int sin_len,
const char *query, unsigned query_len)
{
if (sox_sendto(fd, (const char *)query, query_len, 0,
sin, sin_len) == query_len)
return (0);
return (-1);
}
static struct rfc1035_udp_query_responses *
rfc1035_udp_query_response_alloc_common(int n_queries)
{
struct rfc1035_udp_query_response *buf=
calloc(n_queries, sizeof(struct rfc1035_udp_query_response));
struct rfc1035_udp_query_responses *resps;
if (!buf)
return 0;
resps=malloc(sizeof(struct rfc1035_udp_query_responses));
if (!resps)
{
free(buf);
return 0;
}
resps->n_queries=n_queries;
resps->queries=buf;
return resps;
}
struct rfc1035_udp_query_responses *
rfc1035_udp_query_response_alloc(const char **queries,
const unsigned *querylens,
int n_queries)
{
struct rfc1035_udp_query_responses *resps =
rfc1035_udp_query_response_alloc_common(n_queries);
if (!resps)
return 0;
for (int n=0; n<n_queries; ++n)
{
resps->queries[n].query=queries[n];
resps->queries[n].querylen=querylens[n];
}
return resps;
}
struct rfc1035_udp_query_responses *
rfc1035_udp_query_response_alloc_bis(struct querybuf *queries,
int n_queries)
{
struct rfc1035_udp_query_responses *resps =
rfc1035_udp_query_response_alloc_common(n_queries);
if (!resps)
return 0;
for (int n=0; n<n_queries; ++n)
{
resps->queries[n].query=queries[n].qbuf;
resps->queries[n].querylen=queries[n].qbuflen;
}
return resps;
}
void rfc1035_udp_query_response_free(struct rfc1035_udp_query_responses *resps)
{
int n;
for (n=0; n<resps->n_queries; ++n)
{
if (resps->queries[n].response)
free(resps->queries[n].response);
}
free(resps->queries);
free(resps);
}
static int dorecv(int fd, char *bufptr, unsigned buflen, int flags,
struct sockaddr *addr, socklen_t *addrlen)
{
socklen_t len;
do
{
len=sox_recvfrom(fd, bufptr, buflen, flags, addr, addrlen);
} while (len < 0 && errno == EINTR);
return (len);
}
static int rfc1035_recv_one_udp_response(int fd,
const struct sockaddr *addrshouldfrom, int addrshouldfrom_len,
struct rfc1035_udp_query_responses *queries)
{
int len;
#if RFC1035_IPV6
struct sockaddr_storage addrfrom;
#else
struct sockaddr_in addrfrom;
#endif
socklen_t addrfromlen;
char rfc1035_buf[512];
char *bufptr=rfc1035_buf;
char *mallocedbuf=0;
int i;
unsigned buflen=sizeof(rfc1035_buf);
while ((len=dorecv(fd, bufptr, buflen, MSG_PEEK, 0, 0)) >= buflen )
{
if (len == buflen) len += 511;
++len;
if (mallocedbuf) free(mallocedbuf);
mallocedbuf=(char *)malloc(len);
if (!mallocedbuf) return (0);
bufptr= mallocedbuf;
buflen=len;
}
addrfromlen=sizeof(addrfrom);
if (len < 0 || (len=dorecv(fd, bufptr, buflen, 0,
(struct sockaddr *)&addrfrom, &addrfromlen)) < 0)
{
if (mallocedbuf)
free(mallocedbuf);
errno=EIO;
return (0);
}
buflen=len;
if ( !rfc1035_same_ip( &addrfrom, addrfromlen,
addrshouldfrom, addrshouldfrom_len))
{
if (mallocedbuf)
free(mallocedbuf);
errno=EAGAIN;
return (0);
}
if ( buflen < 2)
{
if (mallocedbuf)
free(mallocedbuf);
errno=EAGAIN;
return (0);
}
for (i=0; i<queries->n_queries; ++i)
{
if (queries->queries[i].response)
continue; /* Already received this one */
if ( bufptr[0] != queries->queries[i].query[0] ||
bufptr[1] != queries->queries[i].query[1]
|| (unsigned char)(bufptr[2] & 0x80) == 0 )
continue;
if (!mallocedbuf)
{
if ((mallocedbuf=malloc( buflen )) == 0)
return (0);
memcpy(mallocedbuf, bufptr, buflen);
bufptr=mallocedbuf;
}
queries->queries[i].response=mallocedbuf;
queries->queries[i].resplen=buflen;
return 1;
}
if (mallocedbuf)
free(mallocedbuf);
errno=EAGAIN;
return (0);
}
char *rfc1035_query_udp(struct rfc1035_res *res,
int fd, const struct sockaddr *sin, int sin_len,
const char *query, unsigned query_len, int *buflen, unsigned w)
{
struct rfc1035_udp_query_responses *resps=
rfc1035_udp_query_response_alloc(&query, &query_len, 1);
char *bufptr=0;
if (!resps)
return 0;
if (rfc1035_udp_query_multi(res, fd, sin, sin_len, resps, w))
{
bufptr=resps->queries[0].response;
*buflen=resps->queries[0].resplen;
resps->queries[0].response=0;
}
rfc1035_udp_query_response_free(resps);
return bufptr;
}
int rfc1035_udp_query_multi(struct rfc1035_res *res,
int fd, const struct sockaddr *sin, int sin_len,
struct rfc1035_udp_query_responses *qr,
unsigned w)
{
time_t current_time, final_time;
int i;
time(¤t_time);
for (i=0; i<qr->n_queries; ++i)
{
if (qr->queries[i].response)
continue; /* Already sent it */
if (rfc1035_send_udp(fd, sin, sin_len,
qr->queries[i].query,
qr->queries[i].querylen))
return (0);
}
final_time=current_time+w;
while (1)
{
for (i=0; i<qr->n_queries; ++i)
if (!qr->queries[i].response)
break;
if (i == qr->n_queries)
return 1; /* Everything received */
if (current_time >= final_time)
break;
if (rfc1035_wait_reply(fd, final_time-current_time))
break;
if (!rfc1035_recv_one_udp_response(fd,
sin,
sin_len,
qr))
{
if (errno != EAGAIN)
return 0;
}
time(¤t_time);
}
errno=EAGAIN;
return (0);
}
|