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