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