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