656b2da9 |
1 | /**/ |
2 | |
4353a5c4 |
3 | #include <string.h> |
4 | #include <errno.h> |
5 | #include <stdlib.h> |
6 | |
7 | #include <netdb.h> |
8 | #include <arpa/inet.h> |
9 | |
10 | #include "internal.h" |
37e28fde |
11 | |
ddfda861 |
12 | /* TCP connection management */ |
656b2da9 |
13 | |
8402e34c |
14 | void adns__tcp_broken(adns_state ads, const char *what, const char *why) { |
15 | int serv; |
4353a5c4 |
16 | adns_query qu, nqu; |
8402e34c |
17 | |
18 | assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok); |
ddfda861 |
19 | serv= ads->tcpserver; |
68442019 |
20 | adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why); |
37e28fde |
21 | close(ads->tcpsocket); |
22 | ads->tcpstate= server_disconnected; |
23 | |
4353a5c4 |
24 | for (qu= ads->timew.head; qu; qu= nqu) { |
8402e34c |
25 | nqu= qu->next; |
ddfda861 |
26 | if (qu->state == query_udp) continue; |
27 | assert(qu->state == query_tcpwait || qu->state == query_tcpsent); |
28 | qu->state= query_tcpwait; |
29 | qu->tcpfailed |= (1<<serv); |
30 | if (qu->tcpfailed == (1<<ads->nservers)-1) { |
4353a5c4 |
31 | LIST_UNLINK(ads->timew,qu); |
ddfda861 |
32 | adns__query_fail(ads,qu,adns_s_allservfail); |
33 | } |
8402e34c |
34 | } |
35 | |
4353a5c4 |
36 | ads->tcprecv.used= ads->tcpsend.used= 0; |
8402e34c |
37 | ads->tcpserver= (serv+1)%ads->nservers; |
38 | } |
39 | |
ddfda861 |
40 | static void tcp_connected(adns_state ads, struct timeval now) { |
4353a5c4 |
41 | adns_query qu, nqu; |
42 | |
68442019 |
43 | adns__debug(ads,ads->tcpserver,0,"TCP connected"); |
4353a5c4 |
44 | ads->tcpstate= server_ok; |
ddfda861 |
45 | for (qu= ads->timew.head; qu; qu= nqu) { |
46 | nqu= qu->next; |
47 | if (qu->state == query_udp) continue; |
48 | assert (qu->state == query_tcpwait); |
49 | adns__query_tcp(ads,qu,now); |
50 | } |
51 | } |
52 | |
53 | void adns__tcp_tryconnect(adns_state ads, struct timeval now) { |
8402e34c |
54 | int r, fd, tries; |
4353a5c4 |
55 | struct sockaddr_in addr; |
56 | struct protoent *proto; |
ddfda861 |
57 | /* fixme: single TCP timeout, not once per server */ |
8402e34c |
58 | |
59 | for (tries=0; tries<ads->nservers; tries++) { |
60 | if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return; |
61 | assert(ads->tcpstate == server_disconnected); |
4353a5c4 |
62 | assert(!ads->tcpsend.used); |
63 | assert(!ads->tcprecv.used); |
8402e34c |
64 | |
65 | proto= getprotobyname("tcp"); |
68442019 |
66 | if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; } |
8402e34c |
67 | fd= socket(AF_INET,SOCK_STREAM,proto->p_proto); |
4353a5c4 |
68 | if (fd<0) { |
68442019 |
69 | adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno)); |
4353a5c4 |
70 | return; |
71 | } |
72 | r= adns__setnonblock(ads,fd); |
73 | if (r) { |
68442019 |
74 | adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r)); |
4353a5c4 |
75 | close(fd); |
76 | return; |
77 | } |
8402e34c |
78 | memset(&addr,0,sizeof(addr)); |
79 | addr.sin_family= AF_INET; |
98a3f706 |
80 | addr.sin_port= htons(DNS_PORT); |
8402e34c |
81 | addr.sin_addr= ads->servers[ads->tcpserver].addr; |
82 | r= connect(fd,&addr,sizeof(addr)); |
83 | ads->tcpsocket= fd; |
84 | ads->tcpstate= server_connecting; |
4353a5c4 |
85 | if (r==0) { tcp_connected(ads,now); continue; } |
8402e34c |
86 | if (errno == EWOULDBLOCK || errno == EINPROGRESS) return; |
ddfda861 |
87 | adns__tcp_broken(ads,"connect",strerror(errno)); |
8402e34c |
88 | } |
89 | } |
90 | |
4353a5c4 |
91 | /* `Interest' functions - find out which fd's we might be interested in, |
92 | * and when we want to be called back for a timeout. |
93 | */ |
94 | |
95 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
96 | struct timeval maxto) { |
97 | struct timeval *rbuf; |
98 | |
99 | if (!tv_io) return; |
100 | rbuf= *tv_io; |
94436798 |
101 | if (!rbuf) { |
102 | *tvbuf= maxto; *tv_io= tvbuf; |
103 | } else { |
104 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
105 | } |
106 | fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", |
107 | maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec); |
4353a5c4 |
108 | } |
109 | |
110 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
111 | struct timeval now, struct timeval maxtime) { |
112 | ldiv_t dr; |
113 | |
94436798 |
114 | fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", |
115 | now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec); |
4353a5c4 |
116 | if (!tv_io) return; |
94436798 |
117 | maxtime.tv_sec -= (now.tv_sec+2); |
118 | maxtime.tv_usec -= (now.tv_usec-2000000); |
119 | dr= ldiv(maxtime.tv_usec,1000000); |
4353a5c4 |
120 | maxtime.tv_sec += dr.quot; |
94436798 |
121 | maxtime.tv_usec -= dr.quot*1000000; |
122 | if (maxtime.tv_sec<0) timerclear(&maxtime); |
4353a5c4 |
123 | inter_maxto(tv_io,tvbuf,maxtime); |
124 | } |
125 | |
126 | static void inter_addfd(int *maxfd, fd_set *fds, int fd) { |
127 | if (!maxfd || !fds) return; |
128 | if (fd>=*maxfd) *maxfd= fd+1; |
129 | FD_SET(fd,fds); |
130 | } |
131 | |
132 | static void checktimeouts(adns_state ads, struct timeval now, |
133 | struct timeval **tv_io, struct timeval *tvbuf) { |
134 | adns_query qu, nqu; |
135 | |
136 | for (qu= ads->timew.head; qu; qu= nqu) { |
137 | nqu= qu->next; |
138 | if (timercmp(&now,&qu->timeout,>)) { |
139 | LIST_UNLINK(ads->timew,qu); |
140 | if (qu->state != query_udp) { |
141 | adns__query_fail(ads,qu,adns_s_timeout); |
142 | } else { |
143 | adns__query_udp(ads,qu,now); |
144 | } |
145 | } else { |
146 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
147 | } |
148 | } |
149 | } |
150 | |
151 | void adns_interest(adns_state ads, int *maxfd, |
152 | fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
153 | struct timeval **tv_io, struct timeval *tvbuf) { |
154 | struct timeval now; |
155 | struct timeval tvto_lr; |
156 | int r; |
157 | |
94436798 |
158 | fprintf(stderr,"adns_interest\n"); |
159 | |
160 | r= gettimeofday(&now,0); |
4353a5c4 |
161 | if (r) { |
68442019 |
162 | adns__warn(ads,-1,0,"gettimeofday failed - will sleep for a bit: %s", |
163 | strerror(errno)); |
4353a5c4 |
164 | timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS); |
165 | inter_maxto(tv_io, tvbuf, tvto_lr); |
166 | } else { |
167 | checktimeouts(ads,now,tv_io,tvbuf); |
168 | } |
169 | |
170 | inter_addfd(maxfd,readfds,ads->udpsocket); |
171 | |
172 | switch (ads->tcpstate) { |
173 | case server_disconnected: |
174 | break; |
175 | case server_connecting: |
176 | inter_addfd(maxfd,writefds,ads->tcpsocket); |
177 | break; |
178 | case server_ok: |
179 | inter_addfd(maxfd,readfds,ads->tcpsocket); |
180 | inter_addfd(maxfd,exceptfds,ads->tcpsocket); |
181 | if (ads->tcpsend.used) inter_addfd(maxfd,writefds,ads->tcpsocket); |
182 | default: |
183 | abort(); |
184 | } |
185 | } |
186 | |
ddfda861 |
187 | /* Callback procedures - these do the real work of reception and timeout, etc. */ |
188 | |
189 | static int callb_checkfd(int maxfd, const fd_set *fds, int fd) { |
190 | return maxfd<0 || !fds ? 1 : |
191 | fd<maxfd && FD_ISSET(fd,fds); |
192 | } |
193 | |
194 | static int internal_callback(adns_state ads, int maxfd, |
195 | const fd_set *readfds, const fd_set *writefds, |
4353a5c4 |
196 | const fd_set *exceptfds, |
197 | struct timeval now) { |
198 | int skip, want, dgramlen, count, udpaddrlen, r, serv; |
98a3f706 |
199 | byte udpbuf[DNS_MAXUDP]; |
37e28fde |
200 | struct sockaddr_in udpaddr; |
656b2da9 |
201 | |
202 | count= 0; |
ddfda861 |
203 | |
204 | switch (ads->tcpstate) { |
205 | case server_disconnected: |
206 | break; |
207 | case server_connecting: |
656b2da9 |
208 | if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) { |
209 | count++; |
210 | assert(ads->tcprecv.used==0); |
4353a5c4 |
211 | adns__vbuf_ensure(&ads->tcprecv,1); |
656b2da9 |
212 | if (ads->tcprecv.buf) { |
213 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
214 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
4353a5c4 |
215 | tcp_connected(ads,now); |
656b2da9 |
216 | } else if (r>0) { |
4353a5c4 |
217 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
656b2da9 |
218 | } else if (errno!=EINTR) { |
4353a5c4 |
219 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
656b2da9 |
220 | } |
221 | } |
222 | } |
ddfda861 |
223 | break; |
224 | case server_ok: |
225 | count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) + |
226 | callb_checkfd(maxfd,exceptfds,ads->tcpsocket) + |
227 | (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)); |
228 | if (callb_checkfd(maxfd,readfds,ads->tcpsocket)) { |
656b2da9 |
229 | skip= 0; |
230 | for (;;) { |
231 | if (ads->tcprecv.used<skip+2) { |
232 | want= 2; |
233 | } else { |
234 | dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1]; |
235 | if (ads->tcprecv.used<skip+2+dgramlen) { |
236 | want= 2+dgramlen; |
237 | } else { |
98a3f706 |
238 | adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,now); |
656b2da9 |
239 | skip+= 2+dgramlen; continue; |
240 | } |
241 | } |
37e28fde |
242 | ads->tcprecv.used -= skip; |
656b2da9 |
243 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used); |
4353a5c4 |
244 | adns__vbuf_ensure(&ads->tcprecv,want); |
656b2da9 |
245 | if (ads->tcprecv.used >= ads->tcprecv.avail) break; |
246 | r= read(ads->tcpsocket, |
247 | ads->tcprecv.buf+ads->tcprecv.used, |
248 | ads->tcprecv.avail-ads->tcprecv.used); |
249 | if (r>0) { |
250 | ads->tcprecv.used+= r; |
251 | } else { |
252 | if (r<0) { |
253 | if (errno==EAGAIN || errno==EWOULDBLOCK || errno==ENOMEM) break; |
254 | if (errno==EINTR) continue; |
255 | } |
4353a5c4 |
256 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
656b2da9 |
257 | break; |
258 | } |
259 | } |
260 | } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) { |
4353a5c4 |
261 | adns__tcp_broken(ads,"select","exceptional condition detected"); |
656b2da9 |
262 | } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) { |
263 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
264 | if (r<0) { |
265 | if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) { |
4353a5c4 |
266 | adns__tcp_broken(ads,"write",strerror(errno)); |
656b2da9 |
267 | } |
268 | } else if (r>0) { |
269 | ads->tcpsend.used -= r; |
270 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
271 | } |
272 | } |
ddfda861 |
273 | default: |
274 | abort(); |
656b2da9 |
275 | } |
276 | |
37e28fde |
277 | if (callb_checkfd(maxfd,readfds,ads->udpsocket)) { |
278 | count++; |
279 | for (;;) { |
280 | udpaddrlen= sizeof(udpaddr); |
281 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,&udpaddr,&udpaddrlen); |
282 | if (r<0) { |
283 | if (!(errno == EAGAIN || errno == EWOULDBLOCK || |
284 | errno == EINTR || errno == ENOMEM || errno == ENOBUFS)) |
68442019 |
285 | adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno)); |
37e28fde |
286 | break; |
287 | } |
288 | if (udpaddrlen != sizeof(udpaddr)) { |
68442019 |
289 | adns__diag(ads,-1,0,"datagram received with wrong address length %d" |
290 | " (expected %d)", udpaddrlen,sizeof(udpaddr)); |
37e28fde |
291 | continue; |
292 | } |
293 | if (udpaddr.sin_family != AF_INET) { |
68442019 |
294 | adns__diag(ads,-1,0,"datagram received with wrong protocol family" |
4353a5c4 |
295 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
37e28fde |
296 | continue; |
297 | } |
98a3f706 |
298 | if (ntohs(udpaddr.sin_port) != DNS_PORT) { |
68442019 |
299 | adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)", |
98a3f706 |
300 | ntohs(udpaddr.sin_port),DNS_PORT); |
37e28fde |
301 | continue; |
302 | } |
303 | for (serv= 0; |
304 | serv < ads->nservers && |
305 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
306 | serv++); |
307 | if (serv >= ads->nservers) { |
68442019 |
308 | adns__warn(ads,-1,0,"datagram received from unknown nameserver %s", |
4353a5c4 |
309 | inet_ntoa(udpaddr.sin_addr)); |
37e28fde |
310 | continue; |
311 | } |
98a3f706 |
312 | adns__procdgram(ads,udpbuf,r,serv,now); |
656b2da9 |
313 | } |
37e28fde |
314 | } |
4353a5c4 |
315 | return count; |
656b2da9 |
316 | } |
656b2da9 |
317 | |
ddfda861 |
318 | int adns_callback(adns_state ads, int maxfd, |
319 | const fd_set *readfds, const fd_set *writefds, |
320 | const fd_set *exceptfds) { |
321 | struct timeval now; |
656b2da9 |
322 | int r; |
37e28fde |
323 | |
4353a5c4 |
324 | r= gettimeofday(&now,0); if (r) return -1; |
325 | checktimeouts(ads,now,0,0); |
326 | return internal_callback(ads,maxfd,readfds,writefds,exceptfds,now); |
656b2da9 |
327 | } |
328 | |
ddfda861 |
329 | /* User-visible functions and their implementation. */ |
330 | |
4353a5c4 |
331 | void adns__autosys(adns_state ads, struct timeval now) { |
ddfda861 |
332 | if (ads->iflags & adns_if_noautosys) return; |
333 | adns_callback(ads,-1,0,0,0); |
334 | } |
335 | |
656b2da9 |
336 | static int internal_check(adns_state ads, |
337 | adns_query *query_io, |
338 | adns_answer **answer, |
339 | void **context_r) { |
340 | adns_query qu; |
341 | |
342 | qu= *query_io; |
343 | if (!qu) { |
344 | if (!ads->output.head) return EWOULDBLOCK; |
345 | qu= ads->output.head; |
346 | } else { |
347 | if (qu->id>=0) return EWOULDBLOCK; |
348 | } |
349 | LIST_UNLINK(ads->output,qu); |
8e5b0abb |
350 | *answer= qu->answer; |
4353a5c4 |
351 | if (context_r) *context_r= qu->context.ext; |
656b2da9 |
352 | free(qu); |
353 | return 0; |
354 | } |
355 | |
356 | int adns_wait(adns_state ads, |
357 | adns_query *query_io, |
358 | adns_answer **answer_r, |
359 | void **context_r) { |
360 | int r, maxfd, rsel, rcb; |
361 | fd_set readfds, writefds, exceptfds; |
362 | struct timeval tvbuf, *tvp; |
363 | |
364 | for (;;) { |
365 | r= internal_check(ads,query_io,answer_r,context_r); |
94436798 |
366 | if (r != EWOULDBLOCK) return r; |
656b2da9 |
367 | maxfd= 0; tvp= 0; |
37e28fde |
368 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
656b2da9 |
369 | adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); |
370 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
371 | if (rsel==-1) return r; |
4353a5c4 |
372 | rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); |
656b2da9 |
373 | assert(rcb==rsel); |
374 | } |
375 | } |
376 | |
377 | int adns_check(adns_state ads, |
378 | adns_query *query_io, |
379 | adns_answer **answer_r, |
380 | void **context_r) { |
4353a5c4 |
381 | struct timeval now; |
382 | int r; |
383 | |
384 | r= gettimeofday(&now,0); if (r) return errno; |
385 | adns__autosys(ads,now); |
656b2da9 |
386 | return internal_check(ads,query_io,answer_r,context_r); |
387 | } |