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