Licensing: Update copyright dates for Ian Jackson
[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/*
ae8cc977 8 * This file is part of adns, which is
26e1c3d6 9 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
ae8cc977 10 * Copyright (C) 1999-2000,2003,2006 Tony Finch
11 * Copyright (C) 1991 Massachusetts Institute of Technology
12 * (See the file INSTALL for full details.)
e576be50 13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
7f8bbe29 16 * the Free Software Foundation; either version 3, or (at your option)
e576be50 17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
8c09a4c6 25 * along with this program; if not, write to the Free Software Foundation.
e576be50 26 */
656b2da9 27
4353a5c4 28#include <errno.h>
29#include <stdlib.h>
0f091044 30#include <unistd.h>
4353a5c4 31
0f091044 32#include <sys/types.h>
33#include <sys/time.h>
4353a5c4 34#include <netdb.h>
eec1d9c8 35#include <sys/socket.h>
36#include <netinet/in.h>
4353a5c4 37#include <arpa/inet.h>
38
39#include "internal.h"
2953d358 40#include "tvarith.h"
37e28fde 41
620c146d 42/* TCP connection management. */
43
f7f83b4a 44static void tcp_close(adns_state ads) {
620c146d 45 close(ads->tcpsocket);
6a578b2c 46 ads->tcpsocket= -1;
70ad7a2a 47 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
620c146d 48}
656b2da9 49
8402e34c 50void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
51 int serv;
fb901bf5 52 adns_query qu;
8402e34c 53
54 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
ddfda861 55 serv= ads->tcpserver;
f7f83b4a 56 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
57
fb901bf5 58 if (ads->tcpstate == server_connecting) {
59 /* Counts as a retry for all the queries waiting for TCP. */
60 for (qu= ads->tcpw.head; qu; qu= qu->next)
61 qu->retries++;
62 }
63
f7f83b4a 64 tcp_close(ads);
65 ads->tcpstate= server_broken;
66 ads->tcpserver= (serv+1)%ads->nservers;
8402e34c 67}
68
ddfda861 69static void tcp_connected(adns_state ads, struct timeval now) {
4353a5c4 70 adns_query qu, nqu;
71
68442019 72 adns__debug(ads,ads->tcpserver,0,"TCP connected");
4353a5c4 73 ads->tcpstate= server_ok;
f7f83b4a 74 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
ddfda861 75 nqu= qu->next;
f7f83b4a 76 assert(qu->state == query_tcpw);
77 adns__querysend_tcp(qu,now);
ddfda861 78 }
79}
80
237ce710 81static void tcp_broken_events(adns_state ads) {
82 adns_query qu, nqu;
83
84 assert(ads->tcpstate == server_broken);
85 for (qu= ads->tcpw.head; qu; qu= nqu) {
86 nqu= qu->next;
87 assert(qu->state == query_tcpw);
88 if (qu->retries > ads->nservers) {
89 LIST_UNLINK(ads->tcpw,qu);
90 adns__query_fail(qu,adns_s_allservfail);
91 }
92 }
93 ads->tcpstate= server_disconnected;
94}
95
ddfda861 96void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
8402e34c 97 int r, fd, tries;
f930c455 98 adns_rr_addr *addr;
4353a5c4 99 struct protoent *proto;
8402e34c 100
101 for (tries=0; tries<ads->nservers; tries++) {
f7f83b4a 102 switch (ads->tcpstate) {
103 case server_connecting:
104 case server_ok:
105 case server_broken:
106 return;
107 case server_disconnected:
108 break;
109 default:
110 abort();
111 }
112
4353a5c4 113 assert(!ads->tcpsend.used);
114 assert(!ads->tcprecv.used);
70ad7a2a 115 assert(!ads->tcprecv_skip);
8402e34c 116
117 proto= getprotobyname("tcp");
609133ee 118 if (!proto) {
119 adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
120 return;
121 }
f930c455
MW
122 addr = &ads->servers[ads->tcpserver];
123 fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
4353a5c4 124 if (fd<0) {
68442019 125 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 126 return;
127 }
128 r= adns__setnonblock(ads,fd);
129 if (r) {
609133ee 130 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
131 " %s",strerror(r));
4353a5c4 132 close(fd);
133 return;
134 }
f930c455 135 r= connect(fd,&addr->addr.sa,addr->len);
8402e34c 136 ads->tcpsocket= fd;
137 ads->tcpstate= server_connecting;
f7f83b4a 138 if (r==0) { tcp_connected(ads,now); return; }
139 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
140 ads->tcptimeout= now;
141 timevaladd(&ads->tcptimeout,TCPCONNMS);
142 return;
143 }
ddfda861 144 adns__tcp_broken(ads,"connect",strerror(errno));
237ce710 145 tcp_broken_events(ads);
8402e34c 146 }
147}
148
620c146d 149/* Timeout handling functions. */
150
151void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
152 struct timeval *tv_buf) {
153 const struct timeval *now;
154 int r;
155
156 now= *now_io;
157 if (now) return;
158 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
159 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
160 adns_globalsystemfailure(ads);
161 return;
162}
4353a5c4 163
4fad263d 164static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
165 struct timeval *rbuf;
166
167 if (!tv_io) return;
168
169 rbuf= *tv_io;
170 if (!rbuf) { *tv_io= rbuf= tvbuf; }
171
172 timerclear(rbuf);
173}
174
4353a5c4 175static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
176 struct timeval maxto) {
177 struct timeval *rbuf;
178
179 if (!tv_io) return;
180 rbuf= *tv_io;
94436798 181 if (!rbuf) {
182 *tvbuf= maxto; *tv_io= tvbuf;
183 } else {
184 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
185 }
ffbda80c 186/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
187 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
4353a5c4 188}
189
190static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
191 struct timeval now, struct timeval maxtime) {
f7f83b4a 192 /* tv_io may be 0 */
4353a5c4 193 ldiv_t dr;
194
ffbda80c 195/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
196 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
4353a5c4 197 if (!tv_io) return;
94436798 198 maxtime.tv_sec -= (now.tv_sec+2);
199 maxtime.tv_usec -= (now.tv_usec-2000000);
200 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 201 maxtime.tv_sec += dr.quot;
94436798 202 maxtime.tv_usec -= dr.quot*1000000;
203 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 204 inter_maxto(tv_io,tvbuf,maxtime);
205}
206
f7f83b4a 207static void timeouts_queue(adns_state ads, int act,
208 struct timeval **tv_io, struct timeval *tvbuf,
209 struct timeval now, struct query_queue *queue) {
4353a5c4 210 adns_query qu, nqu;
f7f83b4a 211
212 for (qu= queue->head; qu; qu= nqu) {
4353a5c4 213 nqu= qu->next;
d855b532 214 if (!timercmp(&now,&qu->timeout,>)) {
620c146d 215 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
216 } else {
4fad263d 217 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 218 LIST_UNLINK(*queue,qu);
d8c062fa 219 if (qu->state != query_tosend) {
3955725c 220 adns__query_fail(qu,adns_s_timeout);
4353a5c4 221 } else {
d8c062fa 222 adns__query_send(qu,now);
4353a5c4 223 }
f7f83b4a 224 nqu= queue->head;
4353a5c4 225 }
226 }
f7f83b4a 227}
228
229static void tcp_events(adns_state ads, int act,
230 struct timeval **tv_io, struct timeval *tvbuf,
231 struct timeval now) {
f7f83b4a 232 for (;;) {
233 switch (ads->tcpstate) {
234 case server_broken:
4fad263d 235 if (!act) { inter_immed(tv_io,tvbuf); return; }
237ce710 236 tcp_broken_events(ads);
f7f83b4a 237 case server_disconnected: /* fall through */
238 if (!ads->tcpw.head) return;
4fad263d 239 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 240 adns__tcp_tryconnect(ads,now);
241 break;
242 case server_ok:
243 if (ads->tcpw.head) return;
244 if (!ads->tcptimeout.tv_sec) {
245 assert(!ads->tcptimeout.tv_usec);
246 ads->tcptimeout= now;
247 timevaladd(&ads->tcptimeout,TCPIDLEMS);
248 }
249 case server_connecting: /* fall through */
4fad263d 250 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
f7f83b4a 251 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
252 return;
253 } {
254 /* TCP timeout has happened */
255 switch (ads->tcpstate) {
256 case server_connecting: /* failed to connect */
257 adns__tcp_broken(ads,"unable to make connection","timed out");
258 break;
259 case server_ok: /* idle timeout */
260 tcp_close(ads);
261 ads->tcpstate= server_disconnected;
262 return;
263 default:
264 abort();
265 }
266 }
267 break;
268 default:
269 abort();
270 }
271 }
4fad263d 272 return;
f7f83b4a 273}
274
275void adns__timeouts(adns_state ads, int act,
276 struct timeval **tv_io, struct timeval *tvbuf,
277 struct timeval now) {
278 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
279 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
280 tcp_events(ads,act,tv_io,tvbuf,now);
281}
94436798 282
620c146d 283void adns_firsttimeout(adns_state ads,
284 struct timeval **tv_io, struct timeval *tvbuf,
285 struct timeval now) {
28de6442 286 adns__consistency(ads,0,cc_entex);
620c146d 287 adns__timeouts(ads, 0, tv_io,tvbuf, now);
eb2a930e 288 adns__returning(ads,0);
620c146d 289}
290
291void adns_processtimeouts(adns_state ads, const struct timeval *now) {
292 struct timeval tv_buf;
293
28de6442 294 adns__consistency(ads,0,cc_entex);
3e2e5fab 295 adns__must_gettimeofday(ads,&now,&tv_buf);
296 if (now) adns__timeouts(ads, 1, 0,0, *now);
eb2a930e 297 adns__returning(ads,0);
a8768e80 298}
299
620c146d 300/* fd handling functions. These are the top-level of the real work of
301 * reception and often transmission.
302 */
303
304int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
305 /* Returns the number of entries filled in. Always zeroes revents. */
13d889da
IJ
306 int nwanted=0;
307#define ADD_POLLFD(wantfd, wantevents) do{ \
308 pollfds_buf[nwanted].fd= (wantfd); \
309 pollfds_buf[nwanted].events= (wantevents); \
310 pollfds_buf[nwanted].revents= 0; \
311 nwanted++; \
312 }while(0)
620c146d 313
3eb20edd 314 int i;
a8768e80 315
3eb20edd
MW
316 assert(MAX_POLLFDS == MAXUDP + 1);
317
97a937c8
IJ
318 for (i=0; i<ads->nudpsockets; i++)
319 ADD_POLLFD(ads->udpsockets[i].fd, POLLIN);
4353a5c4 320
321 switch (ads->tcpstate) {
322 case server_disconnected:
6c68a593 323 case server_broken:
13d889da 324 break;
4353a5c4 325 case server_connecting:
13d889da 326 ADD_POLLFD(ads->tcpsocket, POLLOUT);
4353a5c4 327 break;
328 case server_ok:
13d889da
IJ
329 ADD_POLLFD(ads->tcpsocket,
330 ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI);
e062dcae 331 break;
4353a5c4 332 default:
333 abort();
334 }
13d889da
IJ
335 assert(nwanted<=MAX_POLLFDS);
336#undef ADD_POLLFD
337 return nwanted;
4353a5c4 338}
339
620c146d 340int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
3eb20edd 341 int want, dgramlen, r, i, udpaddrlen, serv, old_skip;
98a3f706 342 byte udpbuf[DNS_MAXUDP];
3eb20edd
MW
343 char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
344 struct udpsocket *udp;
345 adns_sockaddr udpaddr;
620c146d 346
28de6442 347 adns__consistency(ads,0,cc_entex);
3e2e5fab 348
ddfda861 349 switch (ads->tcpstate) {
350 case server_disconnected:
fb901bf5 351 case server_broken:
ddfda861 352 case server_connecting:
ddfda861 353 break;
354 case server_ok:
620c146d 355 if (fd != ads->tcpsocket) break;
70ad7a2a 356 assert(!ads->tcprecv_skip);
428bd682 357 do {
70ad7a2a 358 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
359 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
360 ads->tcprecv.buf[ads->tcprecv_skip+1]);
361 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
362 old_skip= ads->tcprecv_skip;
363 ads->tcprecv_skip += 2+dgramlen;
364 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
365 dgramlen, ads->tcpserver, 1,*now);
366 continue;
656b2da9 367 } else {
70ad7a2a 368 want= 2+dgramlen;
656b2da9 369 }
70ad7a2a 370 } else {
371 want= 2;
656b2da9 372 }
70ad7a2a 373 ads->tcprecv.used -= ads->tcprecv_skip;
609133ee 374 memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
375 ads->tcprecv.used);
70ad7a2a 376 ads->tcprecv_skip= 0;
3e2e5fab 377 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
620c146d 378 assert(ads->tcprecv.used <= ads->tcprecv.avail);
379 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
380 r= read(ads->tcpsocket,
381 ads->tcprecv.buf+ads->tcprecv.used,
382 ads->tcprecv.avail-ads->tcprecv.used);
383 if (r>0) {
384 ads->tcprecv.used+= r;
385 } else {
386 if (r) {
3e2e5fab 387 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
620c146d 388 if (errno==EINTR) continue;
3e2e5fab 389 if (errno_resources(errno)) { r= errno; goto xit; }
656b2da9 390 }
620c146d 391 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
656b2da9 392 }
428bd682 393 } while (ads->tcpstate == server_ok);
394 r= 0; goto xit;
ddfda861 395 default:
396 abort();
656b2da9 397 }
97a937c8
IJ
398 for (i=0; i<ads->nudpsockets; i++) {
399 udp= &ads->udpsockets[i];
41536342
IJ
400 if (fd != udp->fd) continue;
401 for (;;) {
402 udpaddrlen= sizeof(udpaddr);
403 r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
404 if (r<0) {
405 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
406 if (errno == EINTR) continue;
407 if (errno_resources(errno)) { r= errno; goto xit; }
408 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
409 r= 0; goto xit;
37e28fde 410 }
41536342
IJ
411 for (serv= 0;
412 serv < ads->nservers &&
413 !adns__sockaddrs_equal(&udpaddr.sa,
414 &ads->servers[serv].addr.sa);
415 serv++);
416 if (serv >= ads->nservers) {
417 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
418 adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
419 continue;
420 }
421 adns__procdgram(ads,udpbuf,r,serv,0,*now);
656b2da9 422 }
41536342 423 break;
37e28fde 424 }
3e2e5fab 425 r= 0;
426xit:
eb2a930e 427 adns__returning(ads,0);
3e2e5fab 428 return r;
656b2da9 429}
656b2da9 430
620c146d 431int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
656b2da9 432 int r;
620c146d 433
28de6442 434 adns__consistency(ads,0,cc_entex);
3e2e5fab 435
620c146d 436 switch (ads->tcpstate) {
437 case server_disconnected:
fb901bf5 438 case server_broken:
620c146d 439 break;
440 case server_connecting:
441 if (fd != ads->tcpsocket) break;
442 assert(ads->tcprecv.used==0);
70ad7a2a 443 assert(ads->tcprecv_skip==0);
620c146d 444 for (;;) {
3e2e5fab 445 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
620c146d 446 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
447 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
448 tcp_connected(ads,*now);
3e2e5fab 449 r= 0; goto xit;
620c146d 450 }
451 if (r>0) {
452 adns__tcp_broken(ads,"connect/read","sent data before first request");
3e2e5fab 453 r= 0; goto xit;
620c146d 454 }
455 if (errno==EINTR) continue;
3e2e5fab 456 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 457 adns__tcp_broken(ads,"connect/read",strerror(errno));
3e2e5fab 458 r= 0; goto xit;
620c146d 459 } /* not reached */
460 case server_ok:
fb901bf5 461 if (fd != ads->tcpsocket) break;
462 while (ads->tcpsend.used) {
620c146d 463 adns__sigpipe_protect(ads);
464 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
465 adns__sigpipe_unprotect(ads);
466 if (r<0) {
467 if (errno==EINTR) continue;
3e2e5fab 468 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
469 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 470 adns__tcp_broken(ads,"write",strerror(errno));
3e2e5fab 471 r= 0; goto xit;
620c146d 472 } else if (r>0) {
473 ads->tcpsend.used -= r;
474 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
475 }
fb901bf5 476 }
477 r= 0;
478 goto xit;
620c146d 479 default:
480 abort();
481 }
3e2e5fab 482 r= 0;
483xit:
eb2a930e 484 adns__returning(ads,0);
3e2e5fab 485 return r;
620c146d 486}
487
609133ee 488int adns_processexceptional(adns_state ads, int fd,
489 const struct timeval *now) {
28de6442 490 adns__consistency(ads,0,cc_entex);
620c146d 491 switch (ads->tcpstate) {
492 case server_disconnected:
fb901bf5 493 case server_broken:
620c146d 494 break;
495 case server_connecting:
496 case server_ok:
497 if (fd != ads->tcpsocket) break;
498 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
3e2e5fab 499 break;
620c146d 500 default:
501 abort();
502 }
eb2a930e 503 adns__returning(ads,0);
620c146d 504 return 0;
505}
37e28fde 506
620c146d 507static void fd_event(adns_state ads, int fd,
508 int revent, int pollflag,
509 int maxfd, const fd_set *fds,
609133ee 510 int (*func)(adns_state, int fd,
511 const struct timeval *now),
620c146d 512 struct timeval now, int *r_r) {
656b2da9 513 int r;
620c146d 514
515 if (!(revent & pollflag)) return;
516 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
517 r= func(ads,fd,&now);
518 if (r) {
519 if (r_r) {
520 *r_r= r;
521 } else {
609133ee 522 adns__diag(ads,-1,0,"process fd failed after select:"
523 " %s",strerror(errno));
620c146d 524 adns_globalsystemfailure(ads);
525 }
526 }
527}
528
529void adns__fdevents(adns_state ads,
530 const struct pollfd *pollfds, int npollfds,
531 int maxfd, const fd_set *readfds,
532 const fd_set *writefds, const fd_set *exceptfds,
533 struct timeval now, int *r_r) {
534 int i, fd, revents;
535
536 for (i=0; i<npollfds; i++) {
537 fd= pollfds[i].fd;
538 if (fd >= maxfd) maxfd= fd+1;
539 revents= pollfds[i].revents;
609133ee 540#define EV(pollfl,fds,how) \
541 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
542 EV( POLLIN, readfds, readable );
543 EV( POLLOUT, writefds, writeable );
544 EV( POLLPRI, exceptfds, exceptional );
545#undef EV
620c146d 546 }
547}
37e28fde 548
620c146d 549/* Wrappers for select(2). */
550
551void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
552 fd_set *writefds_io, fd_set *exceptfds_io,
553 struct timeval **tv_mod, struct timeval *tv_tobuf,
554 const struct timeval *now) {
555 struct timeval tv_nowbuf;
556 struct pollfd pollfds[MAX_POLLFDS];
557 int i, fd, maxfd, npollfds;
558
28de6442 559 adns__consistency(ads,0,cc_entex);
3e2e5fab 560
620c146d 561 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
562 /* The caller is planning to sleep. */
563 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
26cc07a3 564 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
6c68a593 565 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
620c146d 566 }
567
568 npollfds= adns__pollfds(ads,pollfds);
569 maxfd= *maxfd_io;
570 for (i=0; i<npollfds; i++) {
571 fd= pollfds[i].fd;
572 if (fd >= maxfd) maxfd= fd+1;
573 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
574 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
575 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
576 }
577 *maxfd_io= maxfd;
3e2e5fab 578
579xit:
eb2a930e 580 adns__returning(ads,0);
620c146d 581}
582
583void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
584 const fd_set *writefds, const fd_set *exceptfds,
585 const struct timeval *now) {
fc6a52ae 586 struct timeval tv_buf;
620c146d 587 struct pollfd pollfds[MAX_POLLFDS];
fc6a52ae 588 int npollfds, i;
620c146d 589
28de6442 590 adns__consistency(ads,0,cc_entex);
fc6a52ae 591 adns__must_gettimeofday(ads,&now,&tv_buf);
3e2e5fab 592 if (!now) goto xit;
620c146d 593 adns_processtimeouts(ads,now);
594
595 npollfds= adns__pollfds(ads,pollfds);
fc6a52ae 596 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
620c146d 597 adns__fdevents(ads,
598 pollfds,npollfds,
599 maxfd,readfds,writefds,exceptfds,
600 *now, 0);
3e2e5fab 601xit:
eb2a930e 602 adns__returning(ads,0);
620c146d 603}
604
605/* General helpful functions. */
606
607void adns_globalsystemfailure(adns_state ads) {
28de6442 608 adns__consistency(ads,0,cc_entex);
3e2e5fab 609
f7f83b4a 610 while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
611 while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
620c146d 612
613 switch (ads->tcpstate) {
614 case server_connecting:
615 case server_ok:
f7f83b4a 616 adns__tcp_broken(ads,0,0);
620c146d 617 break;
618 case server_disconnected:
fb901bf5 619 case server_broken:
620c146d 620 break;
621 default:
622 abort();
623 }
eb2a930e 624 adns__returning(ads,0);
656b2da9 625}
626
620c146d 627int adns_processany(adns_state ads) {
aedf10f2 628 int r, i;
620c146d 629 struct timeval now;
630 struct pollfd pollfds[MAX_POLLFDS];
631 int npollfds;
632
28de6442 633 adns__consistency(ads,0,cc_entex);
3e2e5fab 634
620c146d 635 r= gettimeofday(&now,0);
636 if (!r) adns_processtimeouts(ads,&now);
637
25913ee2 638 /* We just use adns__fdevents to loop over the fd's trying them.
639 * This seems more sensible than calling select, since we're most
640 * likely just to want to do a read on one or two fds anyway.
641 */
620c146d 642 npollfds= adns__pollfds(ads,pollfds);
1ed3dba4 643 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
620c146d 644 adns__fdevents(ads,
645 pollfds,npollfds,
646 0,0,0,0,
647 now,&r);
3e2e5fab 648
eb2a930e 649 adns__returning(ads,0);
c118a725 650 return 0;
620c146d 651}
ddfda861 652
4353a5c4 653void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 654 if (ads->iflags & adns_if_noautosys) return;
620c146d 655 adns_processany(ads);
ddfda861 656}
657
940356bd 658int adns__internal_check(adns_state ads,
659 adns_query *query_io,
660 adns_answer **answer,
661 void **context_r) {
656b2da9 662 adns_query qu;
663
664 qu= *query_io;
665 if (!qu) {
4ae90a83 666 if (ads->output.head) {
667 qu= ads->output.head;
f7f83b4a 668 } else if (ads->udpw.head || ads->tcpw.head) {
4ae90a83 669 return EAGAIN;
670 } else {
671 return ESRCH;
672 }
656b2da9 673 } else {
226c5eef 674 if (qu->id>=0) return EAGAIN;
656b2da9 675 }
676 LIST_UNLINK(ads->output,qu);
8e5b0abb 677 *answer= qu->answer;
a6536d8b 678 if (context_r) *context_r= qu->ctx.ext;
8ce38e76 679 *query_io= qu;
656b2da9 680 free(qu);
681 return 0;
682}
683
684int adns_wait(adns_state ads,
685 adns_query *query_io,
686 adns_answer **answer_r,
687 void **context_r) {
620c146d 688 int r, maxfd, rsel;
656b2da9 689 fd_set readfds, writefds, exceptfds;
26cc07a3 690 struct timeval tvbuf, *tvp;
656b2da9 691
28de6442 692 adns__consistency(ads,*query_io,cc_entex);
656b2da9 693 for (;;) {
940356bd 694 r= adns__internal_check(ads,query_io,answer_r,context_r);
226c5eef 695 if (r != EAGAIN) break;
656b2da9 696 maxfd= 0; tvp= 0;
37e28fde 697 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
620c146d 698 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
4fad263d 699 assert(tvp);
656b2da9 700 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
88372443 701 if (rsel==-1) {
620c146d 702 if (errno == EINTR) {
3e2e5fab 703 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
620c146d 704 } else {
705 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
706 adns_globalsystemfailure(ads);
707 }
708 } else {
709 assert(rsel >= 0);
710 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
88372443 711 }
656b2da9 712 }
eb2a930e 713 adns__returning(ads,0);
3e2e5fab 714 return r;
656b2da9 715}
716
717int adns_check(adns_state ads,
718 adns_query *query_io,
719 adns_answer **answer_r,
720 void **context_r) {
4353a5c4 721 struct timeval now;
722 int r;
723
28de6442 724 adns__consistency(ads,*query_io,cc_entex);
620c146d 725 r= gettimeofday(&now,0);
726 if (!r) adns__autosys(ads,now);
3e2e5fab 727
940356bd 728 r= adns__internal_check(ads,query_io,answer_r,context_r);
eb2a930e 729 adns__returning(ads,0);
3e2e5fab 730 return r;
656b2da9 731}