adns__checkqueues fix for large queues
[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/*
a719a4be 8 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
e576be50 9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
656b2da9 24
4353a5c4 25#include <string.h>
26#include <errno.h>
27#include <stdlib.h>
0f091044 28#include <unistd.h>
4353a5c4 29
0f091044 30#include <sys/types.h>
31#include <sys/time.h>
4353a5c4 32#include <netdb.h>
eec1d9c8 33#include <sys/socket.h>
34#include <netinet/in.h>
4353a5c4 35#include <arpa/inet.h>
36
37#include "internal.h"
37e28fde 38
620c146d 39/* TCP connection management. */
40
41void adns__tcp_closenext(adns_state ads) {
42 int serv;
43
44 serv= ads->tcpserver;
45 close(ads->tcpsocket);
46 ads->tcpstate= server_disconnected;
47 ads->tcprecv.used= ads->tcpsend.used= 0;
48 ads->tcpserver= (serv+1)%ads->nservers;
49}
656b2da9 50
8402e34c 51void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
52 int serv;
4353a5c4 53 adns_query qu, nqu;
8402e34c 54
55 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
ddfda861 56 serv= ads->tcpserver;
68442019 57 adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
620c146d 58 adns__tcp_closenext(ads);
37e28fde 59
4353a5c4 60 for (qu= ads->timew.head; qu; qu= nqu) {
8402e34c 61 nqu= qu->next;
ddfda861 62 if (qu->state == query_udp) continue;
63 assert(qu->state == query_tcpwait || qu->state == query_tcpsent);
64 qu->state= query_tcpwait;
65 qu->tcpfailed |= (1<<serv);
66 if (qu->tcpfailed == (1<<ads->nservers)-1) {
4353a5c4 67 LIST_UNLINK(ads->timew,qu);
3955725c 68 adns__query_fail(qu,adns_s_allservfail);
ddfda861 69 }
8402e34c 70 }
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;
ddfda861 78 for (qu= ads->timew.head; qu; qu= nqu) {
79 nqu= qu->next;
80 if (qu->state == query_udp) continue;
81 assert (qu->state == query_tcpwait);
3955725c 82 adns__query_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++) {
92 if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return;
93 assert(ads->tcpstate == server_disconnected);
4353a5c4 94 assert(!ads->tcpsend.used);
95 assert(!ads->tcprecv.used);
8402e34c 96
97 proto= getprotobyname("tcp");
68442019 98 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
8402e34c 99 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
4353a5c4 100 if (fd<0) {
68442019 101 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 102 return;
103 }
104 r= adns__setnonblock(ads,fd);
105 if (r) {
68442019 106 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
4353a5c4 107 close(fd);
108 return;
109 }
8402e34c 110 memset(&addr,0,sizeof(addr));
111 addr.sin_family= AF_INET;
98a3f706 112 addr.sin_port= htons(DNS_PORT);
8402e34c 113 addr.sin_addr= ads->servers[ads->tcpserver].addr;
eec1d9c8 114 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
8402e34c 115 ads->tcpsocket= fd;
116 ads->tcpstate= server_connecting;
4353a5c4 117 if (r==0) { tcp_connected(ads,now); continue; }
8402e34c 118 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
ddfda861 119 adns__tcp_broken(ads,"connect",strerror(errno));
8402e34c 120 }
121}
122
620c146d 123/* Timeout handling functions. */
124
125void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
126 struct timeval *tv_buf) {
127 const struct timeval *now;
128 int r;
129
130 now= *now_io;
131 if (now) return;
132 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
133 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
134 adns_globalsystemfailure(ads);
135 return;
136}
4353a5c4 137
138static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
139 struct timeval maxto) {
140 struct timeval *rbuf;
141
142 if (!tv_io) return;
143 rbuf= *tv_io;
94436798 144 if (!rbuf) {
145 *tvbuf= maxto; *tv_io= tvbuf;
146 } else {
147 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
148 }
ffbda80c 149/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
150 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
4353a5c4 151}
152
153static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
154 struct timeval now, struct timeval maxtime) {
155 ldiv_t dr;
156
ffbda80c 157/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
158 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
4353a5c4 159 if (!tv_io) return;
94436798 160 maxtime.tv_sec -= (now.tv_sec+2);
161 maxtime.tv_usec -= (now.tv_usec-2000000);
162 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 163 maxtime.tv_sec += dr.quot;
94436798 164 maxtime.tv_usec -= dr.quot*1000000;
165 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 166 inter_maxto(tv_io,tvbuf,maxtime);
167}
168
620c146d 169void adns__timeouts(adns_state ads, int act,
170 struct timeval **tv_io, struct timeval *tvbuf,
171 struct timeval now) {
4353a5c4 172 adns_query qu, nqu;
620c146d 173
4353a5c4 174 for (qu= ads->timew.head; qu; qu= nqu) {
175 nqu= qu->next;
d855b532 176 if (!timercmp(&now,&qu->timeout,>)) {
620c146d 177 if (!tv_io) continue;
178 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
179 } else {
180 if (!act) continue;
4353a5c4 181 LIST_UNLINK(ads->timew,qu);
182 if (qu->state != query_udp) {
3955725c 183 adns__query_fail(qu,adns_s_timeout);
4353a5c4 184 } else {
3955725c 185 adns__query_udp(qu,now);
4353a5c4 186 }
620c146d 187 nqu= ads->timew.head;
4353a5c4 188 }
189 }
190}
94436798 191
620c146d 192void adns_firsttimeout(adns_state ads,
193 struct timeval **tv_io, struct timeval *tvbuf,
194 struct timeval now) {
195 adns__timeouts(ads, 0, tv_io,tvbuf, now);
196}
197
198void adns_processtimeouts(adns_state ads, const struct timeval *now) {
199 struct timeval tv_buf;
200
201 adns__must_gettimeofday(ads,&now,&tv_buf); if (!now) return;
202 adns__timeouts(ads, 1, 0,0, *now);
a8768e80 203}
204
620c146d 205/* fd handling functions. These are the top-level of the real work of
206 * reception and often transmission.
207 */
208
209int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
210 /* Returns the number of entries filled in. Always zeroes revents. */
211
212 assert(MAX_POLLFDS==2);
a8768e80 213
620c146d 214 pollfds_buf[0].fd= ads->udpsocket;
215 pollfds_buf[0].events= POLLIN;
216 pollfds_buf[0].revents= 0;
4353a5c4 217
218 switch (ads->tcpstate) {
219 case server_disconnected:
620c146d 220 return 1;
4353a5c4 221 case server_connecting:
620c146d 222 pollfds_buf[1].events= POLLOUT;
4353a5c4 223 break;
224 case server_ok:
620c146d 225 pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
e062dcae 226 break;
4353a5c4 227 default:
228 abort();
229 }
620c146d 230 pollfds_buf[1].fd= ads->tcpsocket;
620c146d 231 return 2;
4353a5c4 232}
233
620c146d 234int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
235 int skip, want, dgramlen, r, udpaddrlen, serv;
98a3f706 236 byte udpbuf[DNS_MAXUDP];
37e28fde 237 struct sockaddr_in udpaddr;
620c146d 238
ddfda861 239 switch (ads->tcpstate) {
240 case server_disconnected:
ddfda861 241 case server_connecting:
ddfda861 242 break;
243 case server_ok:
620c146d 244 if (fd != ads->tcpsocket) break;
245 skip= 0;
246 for (;;) {
247 if (ads->tcprecv.used<skip+2) {
248 want= 2;
249 } else {
250 dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1];
251 if (ads->tcprecv.used<skip+2+dgramlen) {
252 want= 2+dgramlen;
656b2da9 253 } else {
620c146d 254 adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,*now);
255 skip+= 2+dgramlen; continue;
656b2da9 256 }
257 }
620c146d 258 ads->tcprecv.used -= skip;
259 memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
260 skip= 0;
261 if (!adns__vbuf_ensure(&ads->tcprecv,want)) return ENOMEM;
262 assert(ads->tcprecv.used <= ads->tcprecv.avail);
263 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
264 r= read(ads->tcpsocket,
265 ads->tcprecv.buf+ads->tcprecv.used,
266 ads->tcprecv.avail-ads->tcprecv.used);
267 if (r>0) {
268 ads->tcprecv.used+= r;
269 } else {
270 if (r) {
271 if (errno==EAGAIN || errno==EWOULDBLOCK) return 0;
272 if (errno==EINTR) continue;
273 if (errno_resources(errno)) return errno;
656b2da9 274 }
620c146d 275 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
276 return 0;
656b2da9 277 }
620c146d 278 } /* never reached */
ddfda861 279 default:
280 abort();
656b2da9 281 }
620c146d 282 if (fd == ads->udpsocket) {
37e28fde 283 for (;;) {
284 udpaddrlen= sizeof(udpaddr);
eec1d9c8 285 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
286 (struct sockaddr*)&udpaddr,&udpaddrlen);
37e28fde 287 if (r<0) {
620c146d 288 if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
289 if (errno == EINTR) continue;
290 if (errno_resources(errno)) return errno;
291 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
292 return 0;
37e28fde 293 }
294 if (udpaddrlen != sizeof(udpaddr)) {
68442019 295 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
296 " (expected %d)", udpaddrlen,sizeof(udpaddr));
37e28fde 297 continue;
298 }
299 if (udpaddr.sin_family != AF_INET) {
68442019 300 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
4353a5c4 301 " %u (expected %u)",udpaddr.sin_family,AF_INET);
37e28fde 302 continue;
303 }
98a3f706 304 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
68442019 305 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
98a3f706 306 ntohs(udpaddr.sin_port),DNS_PORT);
37e28fde 307 continue;
308 }
309 for (serv= 0;
310 serv < ads->nservers &&
311 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
312 serv++);
313 if (serv >= ads->nservers) {
68442019 314 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
4353a5c4 315 inet_ntoa(udpaddr.sin_addr));
37e28fde 316 continue;
317 }
620c146d 318 adns__procdgram(ads,udpbuf,r,serv,*now);
656b2da9 319 }
37e28fde 320 }
620c146d 321 return 0;
656b2da9 322}
656b2da9 323
620c146d 324int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
656b2da9 325 int r;
620c146d 326
327 switch (ads->tcpstate) {
328 case server_disconnected:
329 break;
330 case server_connecting:
331 if (fd != ads->tcpsocket) break;
332 assert(ads->tcprecv.used==0);
333 for (;;) {
334 if (!adns__vbuf_ensure(&ads->tcprecv,1)) return ENOMEM;
335 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
336 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
337 tcp_connected(ads,*now);
338 return 0;
339 }
340 if (r>0) {
341 adns__tcp_broken(ads,"connect/read","sent data before first request");
342 return 0;
343 }
344 if (errno==EINTR) continue;
345 if (errno_resources(errno)) return errno;
346 adns__tcp_broken(ads,"connect/read",strerror(errno));
347 return 0;
348 } /* not reached */
349 case server_ok:
350 if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break;
351 for (;;) {
352 adns__sigpipe_protect(ads);
353 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
354 adns__sigpipe_unprotect(ads);
355 if (r<0) {
356 if (errno==EINTR) continue;
357 if (errno==EAGAIN || errno==EWOULDBLOCK) return 0;
358 if (errno_resources(errno)) return errno;
359 adns__tcp_broken(ads,"write",strerror(errno));
360 return 0;
361 } else if (r>0) {
362 ads->tcpsend.used -= r;
363 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
364 }
365 } /* not reached */
366 default:
367 abort();
368 }
369 return 0;
370}
371
372int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
373 switch (ads->tcpstate) {
374 case server_disconnected:
375 break;
376 case server_connecting:
377 case server_ok:
378 if (fd != ads->tcpsocket) break;
379 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
380 return 0;
381 default:
382 abort();
383 }
384 return 0;
385}
37e28fde 386
620c146d 387static void fd_event(adns_state ads, int fd,
388 int revent, int pollflag,
389 int maxfd, const fd_set *fds,
390 int (*func)(adns_state, int fd, const struct timeval *now),
391 struct timeval now, int *r_r) {
656b2da9 392 int r;
620c146d 393
394 if (!(revent & pollflag)) return;
395 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
396 r= func(ads,fd,&now);
397 if (r) {
398 if (r_r) {
399 *r_r= r;
400 } else {
401 adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno));
402 adns_globalsystemfailure(ads);
403 }
404 }
405}
406
407void adns__fdevents(adns_state ads,
408 const struct pollfd *pollfds, int npollfds,
409 int maxfd, const fd_set *readfds,
410 const fd_set *writefds, const fd_set *exceptfds,
411 struct timeval now, int *r_r) {
412 int i, fd, revents;
413
414 for (i=0; i<npollfds; i++) {
415 fd= pollfds[i].fd;
416 if (fd >= maxfd) maxfd= fd+1;
417 revents= pollfds[i].revents;
418 fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r);
419 fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r);
420 fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r);
421 }
422}
37e28fde 423
620c146d 424/* Wrappers for select(2). */
425
426void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
427 fd_set *writefds_io, fd_set *exceptfds_io,
428 struct timeval **tv_mod, struct timeval *tv_tobuf,
429 const struct timeval *now) {
430 struct timeval tv_nowbuf;
431 struct pollfd pollfds[MAX_POLLFDS];
432 int i, fd, maxfd, npollfds;
433
434 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
435 /* The caller is planning to sleep. */
436 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
437 if (!now) return;
438 adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now);
439 }
440
441 npollfds= adns__pollfds(ads,pollfds);
442 maxfd= *maxfd_io;
443 for (i=0; i<npollfds; i++) {
444 fd= pollfds[i].fd;
445 if (fd >= maxfd) maxfd= fd+1;
446 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
447 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
448 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
449 }
450 *maxfd_io= maxfd;
451}
452
453void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
454 const fd_set *writefds, const fd_set *exceptfds,
455 const struct timeval *now) {
fc6a52ae 456 struct timeval tv_buf;
620c146d 457 struct pollfd pollfds[MAX_POLLFDS];
fc6a52ae 458 int npollfds, i;
620c146d 459
fc6a52ae 460 adns__must_gettimeofday(ads,&now,&tv_buf);
461 if (!now) return;
620c146d 462 adns_processtimeouts(ads,now);
463
464 npollfds= adns__pollfds(ads,pollfds);
fc6a52ae 465 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
620c146d 466 adns__fdevents(ads,
467 pollfds,npollfds,
468 maxfd,readfds,writefds,exceptfds,
469 *now, 0);
470}
471
472/* General helpful functions. */
473
474void adns_globalsystemfailure(adns_state ads) {
475 while (ads->timew.head) {
476 adns__query_fail(ads->timew.head, adns_s_systemfail);
477 }
478
479 switch (ads->tcpstate) {
480 case server_connecting:
481 case server_ok:
482 adns__tcp_closenext(ads);
483 break;
484 case server_disconnected:
485 break;
486 default:
487 abort();
488 }
656b2da9 489}
490
620c146d 491int adns_processany(adns_state ads) {
492 int r;
493 struct timeval now;
494 struct pollfd pollfds[MAX_POLLFDS];
495 int npollfds;
496
497 r= gettimeofday(&now,0);
498 if (!r) adns_processtimeouts(ads,&now);
499
500 npollfds= adns__pollfds(ads,pollfds);
501 adns__fdevents(ads,
502 pollfds,npollfds,
503 0,0,0,0,
504 now,&r);
505 return r;
506}
ddfda861 507
4353a5c4 508void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 509 if (ads->iflags & adns_if_noautosys) return;
620c146d 510 adns_processany(ads);
ddfda861 511}
512
656b2da9 513static int internal_check(adns_state ads,
514 adns_query *query_io,
515 adns_answer **answer,
516 void **context_r) {
517 adns_query qu;
518
519 qu= *query_io;
520 if (!qu) {
521 if (!ads->output.head) return EWOULDBLOCK;
522 qu= ads->output.head;
523 } else {
524 if (qu->id>=0) return EWOULDBLOCK;
525 }
526 LIST_UNLINK(ads->output,qu);
8e5b0abb 527 *answer= qu->answer;
a6536d8b 528 if (context_r) *context_r= qu->ctx.ext;
8ce38e76 529 *query_io= qu;
656b2da9 530 free(qu);
531 return 0;
532}
533
534int adns_wait(adns_state ads,
535 adns_query *query_io,
536 adns_answer **answer_r,
537 void **context_r) {
620c146d 538 int r, maxfd, rsel;
656b2da9 539 fd_set readfds, writefds, exceptfds;
540 struct timeval tvbuf, *tvp;
541
542 for (;;) {
543 r= internal_check(ads,query_io,answer_r,context_r);
94436798 544 if (r != EWOULDBLOCK) return r;
656b2da9 545 maxfd= 0; tvp= 0;
37e28fde 546 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
620c146d 547 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
656b2da9 548 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
88372443 549 if (rsel==-1) {
620c146d 550 if (errno == EINTR) {
551 if (ads->iflags & adns_if_eintr) return EINTR;
552 } else {
553 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
554 adns_globalsystemfailure(ads);
555 }
556 } else {
557 assert(rsel >= 0);
558 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
88372443 559 }
656b2da9 560 }
561}
562
563int adns_check(adns_state ads,
564 adns_query *query_io,
565 adns_answer **answer_r,
566 void **context_r) {
4353a5c4 567 struct timeval now;
568 int r;
569
620c146d 570 r= gettimeofday(&now,0);
571 if (!r) adns__autosys(ads,now);
572
656b2da9 573 return internal_check(ads,query_io,answer_r,context_r);
574}