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