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