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