e576be50 |
1 | /* |
2 | * event.c |
3 | * - event loop core |
4 | * - TCP connection management |
5 | * - user-visible check/wait and event-loop-related functions |
6 | */ |
7 | /* |
a719a4be |
8 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
e576be50 |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation; either version 2, or (at your option) |
13 | * any later version. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with this program; if not, write to the Free Software Foundation, |
22 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 | */ |
656b2da9 |
24 | |
4353a5c4 |
25 | #include <string.h> |
26 | #include <errno.h> |
27 | #include <stdlib.h> |
0f091044 |
28 | #include <unistd.h> |
4353a5c4 |
29 | |
0f091044 |
30 | #include <sys/types.h> |
31 | #include <sys/time.h> |
4353a5c4 |
32 | #include <netdb.h> |
eec1d9c8 |
33 | #include <sys/socket.h> |
34 | #include <netinet/in.h> |
4353a5c4 |
35 | #include <arpa/inet.h> |
36 | |
37 | #include "internal.h" |
37e28fde |
38 | |
620c146d |
39 | /* TCP connection management. */ |
40 | |
41 | void adns__tcp_closenext(adns_state ads) { |
42 | int serv; |
43 | |
44 | serv= ads->tcpserver; |
45 | close(ads->tcpsocket); |
46 | ads->tcpstate= server_disconnected; |
47 | ads->tcprecv.used= ads->tcpsend.used= 0; |
48 | ads->tcpserver= (serv+1)%ads->nservers; |
49 | } |
656b2da9 |
50 | |
8402e34c |
51 | void adns__tcp_broken(adns_state ads, const char *what, const char *why) { |
52 | int serv; |
4353a5c4 |
53 | adns_query qu, nqu; |
8402e34c |
54 | |
55 | assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok); |
ddfda861 |
56 | serv= ads->tcpserver; |
68442019 |
57 | adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why); |
620c146d |
58 | adns__tcp_closenext(ads); |
37e28fde |
59 | |
4353a5c4 |
60 | for (qu= ads->timew.head; qu; qu= nqu) { |
8402e34c |
61 | nqu= qu->next; |
ddfda861 |
62 | if (qu->state == query_udp) continue; |
63 | assert(qu->state == query_tcpwait || qu->state == query_tcpsent); |
64 | qu->state= query_tcpwait; |
65 | qu->tcpfailed |= (1<<serv); |
66 | if (qu->tcpfailed == (1<<ads->nservers)-1) { |
4353a5c4 |
67 | LIST_UNLINK(ads->timew,qu); |
3955725c |
68 | adns__query_fail(qu,adns_s_allservfail); |
ddfda861 |
69 | } |
8402e34c |
70 | } |
8402e34c |
71 | } |
72 | |
ddfda861 |
73 | static void tcp_connected(adns_state ads, struct timeval now) { |
4353a5c4 |
74 | adns_query qu, nqu; |
75 | |
68442019 |
76 | adns__debug(ads,ads->tcpserver,0,"TCP connected"); |
4353a5c4 |
77 | ads->tcpstate= server_ok; |
ddfda861 |
78 | for (qu= ads->timew.head; qu; qu= nqu) { |
79 | nqu= qu->next; |
80 | if (qu->state == query_udp) continue; |
81 | assert (qu->state == query_tcpwait); |
3955725c |
82 | adns__query_tcp(qu,now); |
ddfda861 |
83 | } |
84 | } |
85 | |
86 | void adns__tcp_tryconnect(adns_state ads, struct timeval now) { |
8402e34c |
87 | int r, fd, tries; |
4353a5c4 |
88 | struct sockaddr_in addr; |
89 | struct protoent *proto; |
8402e34c |
90 | |
91 | for (tries=0; tries<ads->nservers; tries++) { |
92 | if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return; |
93 | assert(ads->tcpstate == server_disconnected); |
4353a5c4 |
94 | assert(!ads->tcpsend.used); |
95 | assert(!ads->tcprecv.used); |
8402e34c |
96 | |
97 | proto= getprotobyname("tcp"); |
68442019 |
98 | if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; } |
8402e34c |
99 | fd= socket(AF_INET,SOCK_STREAM,proto->p_proto); |
4353a5c4 |
100 | if (fd<0) { |
68442019 |
101 | adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno)); |
4353a5c4 |
102 | return; |
103 | } |
104 | r= adns__setnonblock(ads,fd); |
105 | if (r) { |
68442019 |
106 | adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r)); |
4353a5c4 |
107 | close(fd); |
108 | return; |
109 | } |
8402e34c |
110 | memset(&addr,0,sizeof(addr)); |
111 | addr.sin_family= AF_INET; |
98a3f706 |
112 | addr.sin_port= htons(DNS_PORT); |
8402e34c |
113 | addr.sin_addr= ads->servers[ads->tcpserver].addr; |
eec1d9c8 |
114 | r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr)); |
8402e34c |
115 | ads->tcpsocket= fd; |
116 | ads->tcpstate= server_connecting; |
4353a5c4 |
117 | if (r==0) { tcp_connected(ads,now); continue; } |
8402e34c |
118 | if (errno == EWOULDBLOCK || errno == EINPROGRESS) return; |
ddfda861 |
119 | adns__tcp_broken(ads,"connect",strerror(errno)); |
8402e34c |
120 | } |
121 | } |
122 | |
620c146d |
123 | /* Timeout handling functions. */ |
124 | |
125 | void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, |
126 | struct timeval *tv_buf) { |
127 | const struct timeval *now; |
128 | int r; |
129 | |
130 | now= *now_io; |
131 | if (now) return; |
132 | r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; } |
133 | adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno)); |
134 | adns_globalsystemfailure(ads); |
135 | return; |
136 | } |
4353a5c4 |
137 | |
138 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
139 | struct timeval maxto) { |
140 | struct timeval *rbuf; |
141 | |
142 | if (!tv_io) return; |
143 | rbuf= *tv_io; |
94436798 |
144 | if (!rbuf) { |
145 | *tvbuf= maxto; *tv_io= tvbuf; |
146 | } else { |
147 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
148 | } |
ffbda80c |
149 | /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", |
150 | maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/ |
4353a5c4 |
151 | } |
152 | |
153 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
154 | struct timeval now, struct timeval maxtime) { |
155 | ldiv_t dr; |
156 | |
ffbda80c |
157 | /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", |
158 | now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/ |
4353a5c4 |
159 | if (!tv_io) return; |
94436798 |
160 | maxtime.tv_sec -= (now.tv_sec+2); |
161 | maxtime.tv_usec -= (now.tv_usec-2000000); |
162 | dr= ldiv(maxtime.tv_usec,1000000); |
4353a5c4 |
163 | maxtime.tv_sec += dr.quot; |
94436798 |
164 | maxtime.tv_usec -= dr.quot*1000000; |
165 | if (maxtime.tv_sec<0) timerclear(&maxtime); |
4353a5c4 |
166 | inter_maxto(tv_io,tvbuf,maxtime); |
167 | } |
168 | |
620c146d |
169 | void adns__timeouts(adns_state ads, int act, |
170 | struct timeval **tv_io, struct timeval *tvbuf, |
171 | struct timeval now) { |
4353a5c4 |
172 | adns_query qu, nqu; |
620c146d |
173 | |
4353a5c4 |
174 | for (qu= ads->timew.head; qu; qu= nqu) { |
175 | nqu= qu->next; |
620c146d |
176 | if (timercmp(&now,&qu->timeout,<=)) { |
177 | if (!tv_io) continue; |
178 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
179 | } else { |
180 | if (!act) continue; |
4353a5c4 |
181 | LIST_UNLINK(ads->timew,qu); |
182 | if (qu->state != query_udp) { |
3955725c |
183 | adns__query_fail(qu,adns_s_timeout); |
4353a5c4 |
184 | } else { |
3955725c |
185 | adns__query_udp(qu,now); |
4353a5c4 |
186 | } |
620c146d |
187 | nqu= ads->timew.head; |
4353a5c4 |
188 | } |
189 | } |
190 | } |
94436798 |
191 | |
620c146d |
192 | void adns_firsttimeout(adns_state ads, |
193 | struct timeval **tv_io, struct timeval *tvbuf, |
194 | struct timeval now) { |
195 | adns__timeouts(ads, 0, tv_io,tvbuf, now); |
196 | } |
197 | |
198 | void adns_processtimeouts(adns_state ads, const struct timeval *now) { |
199 | struct timeval tv_buf; |
200 | |
201 | adns__must_gettimeofday(ads,&now,&tv_buf); if (!now) return; |
202 | adns__timeouts(ads, 1, 0,0, *now); |
a8768e80 |
203 | } |
204 | |
620c146d |
205 | /* fd handling functions. These are the top-level of the real work of |
206 | * reception and often transmission. |
207 | */ |
208 | |
209 | int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) { |
210 | /* Returns the number of entries filled in. Always zeroes revents. */ |
211 | |
212 | assert(MAX_POLLFDS==2); |
a8768e80 |
213 | |
620c146d |
214 | pollfds_buf[0].fd= ads->udpsocket; |
215 | pollfds_buf[0].events= POLLIN; |
216 | pollfds_buf[0].revents= 0; |
4353a5c4 |
217 | |
218 | switch (ads->tcpstate) { |
219 | case server_disconnected: |
620c146d |
220 | return 1; |
4353a5c4 |
221 | case server_connecting: |
620c146d |
222 | pollfds_buf[1].events= POLLOUT; |
4353a5c4 |
223 | break; |
224 | case server_ok: |
620c146d |
225 | pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI; |
e062dcae |
226 | break; |
4353a5c4 |
227 | default: |
228 | abort(); |
229 | } |
620c146d |
230 | pollfds_buf[1].fd= ads->tcpsocket; |
231 | pollfds_buf[1].revents= 0; |
232 | return 2; |
4353a5c4 |
233 | } |
234 | |
620c146d |
235 | int adns_processreadable(adns_state ads, int fd, const struct timeval *now) { |
236 | int skip, want, dgramlen, r, udpaddrlen, serv; |
98a3f706 |
237 | byte udpbuf[DNS_MAXUDP]; |
37e28fde |
238 | struct sockaddr_in udpaddr; |
620c146d |
239 | |
ddfda861 |
240 | switch (ads->tcpstate) { |
241 | case server_disconnected: |
ddfda861 |
242 | case server_connecting: |
ddfda861 |
243 | break; |
244 | case server_ok: |
620c146d |
245 | if (fd != ads->tcpsocket) break; |
246 | skip= 0; |
247 | for (;;) { |
248 | if (ads->tcprecv.used<skip+2) { |
249 | want= 2; |
250 | } else { |
251 | dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1]; |
252 | if (ads->tcprecv.used<skip+2+dgramlen) { |
253 | want= 2+dgramlen; |
656b2da9 |
254 | } else { |
620c146d |
255 | adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,*now); |
256 | skip+= 2+dgramlen; continue; |
656b2da9 |
257 | } |
258 | } |
620c146d |
259 | ads->tcprecv.used -= skip; |
260 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used); |
261 | skip= 0; |
262 | if (!adns__vbuf_ensure(&ads->tcprecv,want)) return ENOMEM; |
263 | assert(ads->tcprecv.used <= ads->tcprecv.avail); |
264 | if (ads->tcprecv.used == ads->tcprecv.avail) continue; |
265 | r= read(ads->tcpsocket, |
266 | ads->tcprecv.buf+ads->tcprecv.used, |
267 | ads->tcprecv.avail-ads->tcprecv.used); |
268 | if (r>0) { |
269 | ads->tcprecv.used+= r; |
270 | } else { |
271 | if (r) { |
272 | if (errno==EAGAIN || errno==EWOULDBLOCK) return 0; |
273 | if (errno==EINTR) continue; |
274 | if (errno_resources(errno)) return errno; |
656b2da9 |
275 | } |
620c146d |
276 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
277 | return 0; |
656b2da9 |
278 | } |
620c146d |
279 | } /* never reached */ |
ddfda861 |
280 | default: |
281 | abort(); |
656b2da9 |
282 | } |
620c146d |
283 | if (fd == ads->udpsocket) { |
37e28fde |
284 | for (;;) { |
285 | udpaddrlen= sizeof(udpaddr); |
eec1d9c8 |
286 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0, |
287 | (struct sockaddr*)&udpaddr,&udpaddrlen); |
37e28fde |
288 | if (r<0) { |
620c146d |
289 | if (errno == EAGAIN || errno == EWOULDBLOCK) return 0; |
290 | if (errno == EINTR) continue; |
291 | if (errno_resources(errno)) return errno; |
292 | adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno)); |
293 | return 0; |
37e28fde |
294 | } |
295 | if (udpaddrlen != sizeof(udpaddr)) { |
68442019 |
296 | adns__diag(ads,-1,0,"datagram received with wrong address length %d" |
297 | " (expected %d)", udpaddrlen,sizeof(udpaddr)); |
37e28fde |
298 | continue; |
299 | } |
300 | if (udpaddr.sin_family != AF_INET) { |
68442019 |
301 | adns__diag(ads,-1,0,"datagram received with wrong protocol family" |
4353a5c4 |
302 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
37e28fde |
303 | continue; |
304 | } |
98a3f706 |
305 | if (ntohs(udpaddr.sin_port) != DNS_PORT) { |
68442019 |
306 | adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)", |
98a3f706 |
307 | ntohs(udpaddr.sin_port),DNS_PORT); |
37e28fde |
308 | continue; |
309 | } |
310 | for (serv= 0; |
311 | serv < ads->nservers && |
312 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
313 | serv++); |
314 | if (serv >= ads->nservers) { |
68442019 |
315 | adns__warn(ads,-1,0,"datagram received from unknown nameserver %s", |
4353a5c4 |
316 | inet_ntoa(udpaddr.sin_addr)); |
37e28fde |
317 | continue; |
318 | } |
620c146d |
319 | adns__procdgram(ads,udpbuf,r,serv,*now); |
656b2da9 |
320 | } |
37e28fde |
321 | } |
620c146d |
322 | return 0; |
656b2da9 |
323 | } |
656b2da9 |
324 | |
620c146d |
325 | int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) { |
656b2da9 |
326 | int r; |
620c146d |
327 | |
328 | switch (ads->tcpstate) { |
329 | case server_disconnected: |
330 | break; |
331 | case server_connecting: |
332 | if (fd != ads->tcpsocket) break; |
333 | assert(ads->tcprecv.used==0); |
334 | for (;;) { |
335 | if (!adns__vbuf_ensure(&ads->tcprecv,1)) return ENOMEM; |
336 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
337 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
338 | tcp_connected(ads,*now); |
339 | return 0; |
340 | } |
341 | if (r>0) { |
342 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
343 | return 0; |
344 | } |
345 | if (errno==EINTR) continue; |
346 | if (errno_resources(errno)) return errno; |
347 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
348 | return 0; |
349 | } /* not reached */ |
350 | case server_ok: |
351 | if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break; |
352 | for (;;) { |
353 | adns__sigpipe_protect(ads); |
354 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
355 | adns__sigpipe_unprotect(ads); |
356 | if (r<0) { |
357 | if (errno==EINTR) continue; |
358 | if (errno==EAGAIN || errno==EWOULDBLOCK) return 0; |
359 | if (errno_resources(errno)) return errno; |
360 | adns__tcp_broken(ads,"write",strerror(errno)); |
361 | return 0; |
362 | } else if (r>0) { |
363 | ads->tcpsend.used -= r; |
364 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
365 | } |
366 | } /* not reached */ |
367 | default: |
368 | abort(); |
369 | } |
370 | return 0; |
371 | } |
372 | |
373 | int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) { |
374 | switch (ads->tcpstate) { |
375 | case server_disconnected: |
376 | break; |
377 | case server_connecting: |
378 | case server_ok: |
379 | if (fd != ads->tcpsocket) break; |
380 | adns__tcp_broken(ads,"poll/select","exceptional condition detected"); |
381 | return 0; |
382 | default: |
383 | abort(); |
384 | } |
385 | return 0; |
386 | } |
37e28fde |
387 | |
620c146d |
388 | static void fd_event(adns_state ads, int fd, |
389 | int revent, int pollflag, |
390 | int maxfd, const fd_set *fds, |
391 | int (*func)(adns_state, int fd, const struct timeval *now), |
392 | struct timeval now, int *r_r) { |
656b2da9 |
393 | int r; |
620c146d |
394 | |
395 | if (!(revent & pollflag)) return; |
396 | if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return; |
397 | r= func(ads,fd,&now); |
398 | if (r) { |
399 | if (r_r) { |
400 | *r_r= r; |
401 | } else { |
402 | adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno)); |
403 | adns_globalsystemfailure(ads); |
404 | } |
405 | } |
406 | } |
407 | |
408 | void adns__fdevents(adns_state ads, |
409 | const struct pollfd *pollfds, int npollfds, |
410 | int maxfd, const fd_set *readfds, |
411 | const fd_set *writefds, const fd_set *exceptfds, |
412 | struct timeval now, int *r_r) { |
413 | int i, fd, revents; |
414 | |
415 | for (i=0; i<npollfds; i++) { |
416 | fd= pollfds[i].fd; |
417 | if (fd >= maxfd) maxfd= fd+1; |
418 | revents= pollfds[i].revents; |
419 | fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r); |
420 | fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r); |
421 | fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r); |
422 | } |
423 | } |
37e28fde |
424 | |
620c146d |
425 | /* Wrappers for select(2). */ |
426 | |
427 | void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io, |
428 | fd_set *writefds_io, fd_set *exceptfds_io, |
429 | struct timeval **tv_mod, struct timeval *tv_tobuf, |
430 | const struct timeval *now) { |
431 | struct timeval tv_nowbuf; |
432 | struct pollfd pollfds[MAX_POLLFDS]; |
433 | int i, fd, maxfd, npollfds; |
434 | |
435 | if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) { |
436 | /* The caller is planning to sleep. */ |
437 | adns__must_gettimeofday(ads,&now,&tv_nowbuf); |
438 | if (!now) return; |
439 | adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now); |
440 | } |
441 | |
442 | npollfds= adns__pollfds(ads,pollfds); |
443 | maxfd= *maxfd_io; |
444 | for (i=0; i<npollfds; i++) { |
445 | fd= pollfds[i].fd; |
446 | if (fd >= maxfd) maxfd= fd+1; |
447 | if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io); |
448 | if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io); |
449 | if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io); |
450 | } |
451 | *maxfd_io= maxfd; |
452 | } |
453 | |
454 | void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds, |
455 | const fd_set *writefds, const fd_set *exceptfds, |
456 | const struct timeval *now) { |
457 | struct pollfd pollfds[MAX_POLLFDS]; |
458 | int npollfds; |
459 | |
460 | adns_processtimeouts(ads,now); |
461 | |
462 | npollfds= adns__pollfds(ads,pollfds); |
463 | adns__fdevents(ads, |
464 | pollfds,npollfds, |
465 | maxfd,readfds,writefds,exceptfds, |
466 | *now, 0); |
467 | } |
468 | |
469 | /* General helpful functions. */ |
470 | |
471 | void adns_globalsystemfailure(adns_state ads) { |
472 | while (ads->timew.head) { |
473 | adns__query_fail(ads->timew.head, adns_s_systemfail); |
474 | } |
475 | |
476 | switch (ads->tcpstate) { |
477 | case server_connecting: |
478 | case server_ok: |
479 | adns__tcp_closenext(ads); |
480 | break; |
481 | case server_disconnected: |
482 | break; |
483 | default: |
484 | abort(); |
485 | } |
656b2da9 |
486 | } |
487 | |
620c146d |
488 | int adns_processany(adns_state ads) { |
489 | int r; |
490 | struct timeval now; |
491 | struct pollfd pollfds[MAX_POLLFDS]; |
492 | int npollfds; |
493 | |
494 | r= gettimeofday(&now,0); |
495 | if (!r) adns_processtimeouts(ads,&now); |
496 | |
497 | npollfds= adns__pollfds(ads,pollfds); |
498 | adns__fdevents(ads, |
499 | pollfds,npollfds, |
500 | 0,0,0,0, |
501 | now,&r); |
502 | return r; |
503 | } |
ddfda861 |
504 | |
4353a5c4 |
505 | void adns__autosys(adns_state ads, struct timeval now) { |
ddfda861 |
506 | if (ads->iflags & adns_if_noautosys) return; |
620c146d |
507 | adns_processany(ads); |
ddfda861 |
508 | } |
509 | |
656b2da9 |
510 | static int internal_check(adns_state ads, |
511 | adns_query *query_io, |
512 | adns_answer **answer, |
513 | void **context_r) { |
514 | adns_query qu; |
515 | |
516 | qu= *query_io; |
517 | if (!qu) { |
518 | if (!ads->output.head) return EWOULDBLOCK; |
519 | qu= ads->output.head; |
520 | } else { |
521 | if (qu->id>=0) return EWOULDBLOCK; |
522 | } |
523 | LIST_UNLINK(ads->output,qu); |
8e5b0abb |
524 | *answer= qu->answer; |
a6536d8b |
525 | if (context_r) *context_r= qu->ctx.ext; |
656b2da9 |
526 | free(qu); |
527 | return 0; |
528 | } |
529 | |
530 | int adns_wait(adns_state ads, |
531 | adns_query *query_io, |
532 | adns_answer **answer_r, |
533 | void **context_r) { |
620c146d |
534 | int r, maxfd, rsel; |
656b2da9 |
535 | fd_set readfds, writefds, exceptfds; |
536 | struct timeval tvbuf, *tvp; |
537 | |
538 | for (;;) { |
539 | r= internal_check(ads,query_io,answer_r,context_r); |
94436798 |
540 | if (r != EWOULDBLOCK) return r; |
656b2da9 |
541 | maxfd= 0; tvp= 0; |
37e28fde |
542 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
620c146d |
543 | adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0); |
656b2da9 |
544 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
88372443 |
545 | if (rsel==-1) { |
620c146d |
546 | if (errno == EINTR) { |
547 | if (ads->iflags & adns_if_eintr) return EINTR; |
548 | } else { |
549 | adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno)); |
550 | adns_globalsystemfailure(ads); |
551 | } |
552 | } else { |
553 | assert(rsel >= 0); |
554 | adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0); |
88372443 |
555 | } |
656b2da9 |
556 | } |
557 | } |
558 | |
559 | int adns_check(adns_state ads, |
560 | adns_query *query_io, |
561 | adns_answer **answer_r, |
562 | void **context_r) { |
4353a5c4 |
563 | struct timeval now; |
564 | int r; |
565 | |
620c146d |
566 | r= gettimeofday(&now,0); |
567 | if (!r) adns__autosys(ads,now); |
568 | |
656b2da9 |
569 | return internal_check(ads,query_io,answer_r,context_r); |
570 | } |