4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
8 * This file is part of adns, which is
9 * Copyright (C) 1997-2000,2003,2006,2014-2016,2020 Ian Jackson
10 * Copyright (C) 2014 Mark Wooding
11 * Copyright (C) 1999-2000,2003,2006 Tony Finch
12 * Copyright (C) 1991 Massachusetts Institute of Technology
13 * (See the file INSTALL for full details.)
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 3, or (at your option)
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.
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.
33 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
44 /* TCP connection management. */
46 static void tcp_close(adns_state ads) {
47 close(ads->tcpsocket);
49 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
52 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
56 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
58 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
60 if (ads->tcpstate == server_connecting) {
61 /* Counts as a retry for all the queries waiting for TCP. */
62 for (qu= ads->tcpw.head; qu; qu= qu->next)
67 ads->tcpstate= server_broken;
68 ads->tcpserver= (serv+1)%ads->nservers;
71 static void tcp_connected(adns_state ads, struct timeval now) {
74 adns__debug(ads,ads->tcpserver,0,"TCP connected");
75 ads->tcpstate= server_ok;
76 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
78 assert(qu->state == query_tcpw);
79 adns__querysend_tcp(qu,now);
83 static void tcp_broken_events(adns_state ads) {
86 assert(ads->tcpstate == server_broken);
87 for (qu= ads->tcpw.head; qu; qu= nqu) {
89 assert(qu->state == query_tcpw);
90 if (qu->retries > ads->nservers) {
91 LIST_UNLINK(ads->tcpw,qu);
92 adns__query_fail(qu,adns_s_allservfail);
95 ads->tcpstate= server_disconnected;
98 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
101 struct protoent *proto;
103 for (tries=0; tries<ads->nservers; tries++) {
104 switch (ads->tcpstate) {
105 case server_connecting:
109 case server_disconnected:
115 assert(!ads->tcpsend.used);
116 assert(!ads->tcprecv.used);
117 assert(!ads->tcprecv_skip);
119 proto= getprotobyname("tcp");
121 adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
124 addr = &ads->servers[ads->tcpserver];
125 fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
127 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
130 r= adns__setnonblock(ads,fd);
132 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
137 r= connect(fd,&addr->addr.sa,addr->len);
139 ads->tcpstate= server_connecting;
140 if (r==0) { tcp_connected(ads,now); return; }
141 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
142 ads->tcptimeout= now;
143 timevaladd(&ads->tcptimeout,TCPCONNMS);
146 adns__tcp_broken(ads,"connect",strerror(errno));
147 tcp_broken_events(ads);
151 /* Timeout handling functions. */
153 int adns__gettimeofday(adns_state ads, struct timeval *tv) {
154 if (!(ads->iflags & adns_if_monotonic))
155 return gettimeofday(tv,0);
158 int r = clock_gettime(CLOCK_MONOTONIC,&ts);
161 tv->tv_sec = ts.tv_sec;
162 tv->tv_usec = ts.tv_nsec / 1000;
166 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
167 struct timeval *tv_buf) {
168 const struct timeval *now;
173 r= adns__gettimeofday(ads,tv_buf); if (!r) { *now_io= tv_buf; return; }
174 adns__diag(ads,-1,0,"gettimeofday/clock_gettime failed: %s",
176 adns_globalsystemfailure(ads);
180 static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
181 struct timeval *rbuf;
186 if (!rbuf) { *tv_io= rbuf= tvbuf; }
191 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
192 struct timeval maxto) {
193 struct timeval *rbuf;
198 *tvbuf= maxto; *tv_io= tvbuf;
200 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
202 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
203 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
206 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
207 struct timeval now, struct timeval maxtime) {
211 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
212 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
214 maxtime.tv_sec -= (now.tv_sec+2);
215 maxtime.tv_usec -= (now.tv_usec-2000000);
216 dr= ldiv(maxtime.tv_usec,1000000);
217 maxtime.tv_sec += dr.quot;
218 maxtime.tv_usec -= dr.quot*1000000;
219 if (maxtime.tv_sec<0) timerclear(&maxtime);
220 inter_maxto(tv_io,tvbuf,maxtime);
223 static void timeouts_queue(adns_state ads, int act,
224 struct timeval **tv_io, struct timeval *tvbuf,
225 struct timeval now, struct query_queue *queue) {
227 struct timeval expires;
229 for (qu= queue->head; qu; qu= nqu) {
231 if (timercmp(&now,&qu->timeout_started,<)) /* clock rewound */
232 qu->timeout_started= now;
233 expires= qu->timeout_started;
234 timevaladd(&expires, qu->timeout_ms);
235 if (!timercmp(&now,&expires,>)) {
236 inter_maxtoabs(tv_io,tvbuf,now,expires);
238 if (!act) { inter_immed(tv_io,tvbuf); return; }
239 LIST_UNLINK(*queue,qu);
240 if (qu->state != query_tosend) {
241 adns__query_fail(qu,adns_s_timeout);
243 adns__query_send(qu,now);
250 static void tcp_events(adns_state ads, int act,
251 struct timeval **tv_io, struct timeval *tvbuf,
252 struct timeval now) {
254 switch (ads->tcpstate) {
256 if (!act) { inter_immed(tv_io,tvbuf); return; }
257 tcp_broken_events(ads);
258 case server_disconnected: /* fall through */
259 if (!ads->tcpw.head) return;
260 if (!act) { inter_immed(tv_io,tvbuf); return; }
261 adns__tcp_tryconnect(ads,now);
264 if (ads->tcpw.head) return;
265 if (!ads->tcptimeout.tv_sec) {
266 assert(!ads->tcptimeout.tv_usec);
267 ads->tcptimeout= now;
268 timevaladd(&ads->tcptimeout,TCPIDLEMS);
270 case server_connecting: /* fall through */
271 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
272 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
275 /* TCP timeout has happened */
276 switch (ads->tcpstate) {
277 case server_connecting: /* failed to connect */
278 adns__tcp_broken(ads,"unable to make connection","timed out");
280 case server_ok: /* idle timeout */
282 ads->tcpstate= server_disconnected;
296 void adns__timeouts(adns_state ads, int act,
297 struct timeval **tv_io, struct timeval *tvbuf,
298 struct timeval now) {
299 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
300 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
301 tcp_events(ads,act,tv_io,tvbuf,now);
304 void adns_firsttimeout(adns_state ads,
305 struct timeval **tv_io, struct timeval *tvbuf,
306 struct timeval now) {
307 adns__consistency(ads,0,cc_enter);
308 adns__timeouts(ads, 0, tv_io,tvbuf, now);
309 adns__returning(ads,0);
312 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
313 struct timeval tv_buf;
315 adns__consistency(ads,0,cc_enter);
316 adns__must_gettimeofday(ads,&now,&tv_buf);
317 if (now) adns__timeouts(ads, 1, 0,0, *now);
318 adns__returning(ads,0);
321 /* fd handling functions. These are the top-level of the real work of
322 * reception and often transmission.
325 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
326 /* Returns the number of entries filled in. Always zeroes revents. */
328 #define ADD_POLLFD(wantfd, wantevents) do{ \
329 pollfds_buf[nwanted].fd= (wantfd); \
330 pollfds_buf[nwanted].events= (wantevents); \
331 pollfds_buf[nwanted].revents= 0; \
337 assert(MAX_POLLFDS == MAXUDP + 1);
339 for (i=0; i<ads->nudpsockets; i++)
340 ADD_POLLFD(ads->udpsockets[i].fd, POLLIN);
342 switch (ads->tcpstate) {
343 case server_disconnected:
346 case server_connecting:
347 ADD_POLLFD(ads->tcpsocket, POLLOUT);
350 ADD_POLLFD(ads->tcpsocket,
351 ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI);
356 assert(nwanted<=MAX_POLLFDS);
361 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
362 int want, dgramlen, r, i, serv, old_skip;
363 socklen_t udpaddrlen;
364 byte udpbuf[DNS_MAXUDP];
365 char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
366 struct udpsocket *udp;
367 adns_sockaddr udpaddr;
369 adns__consistency(ads,0,cc_enter);
371 switch (ads->tcpstate) {
372 case server_disconnected:
374 case server_connecting:
377 if (fd != ads->tcpsocket) break;
378 assert(!ads->tcprecv_skip);
380 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
381 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
382 ads->tcprecv.buf[ads->tcprecv_skip+1]);
383 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
384 old_skip= ads->tcprecv_skip;
385 ads->tcprecv_skip += 2+dgramlen;
386 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
387 dgramlen, ads->tcpserver, 1,*now);
395 ads->tcprecv.used -= ads->tcprecv_skip;
396 memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
398 ads->tcprecv_skip= 0;
399 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
400 assert(ads->tcprecv.used <= ads->tcprecv.avail);
401 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
402 r= read(ads->tcpsocket,
403 ads->tcprecv.buf+ads->tcprecv.used,
404 ads->tcprecv.avail-ads->tcprecv.used);
406 ads->tcprecv.used+= r;
409 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
410 if (errno==EINTR) continue;
411 if (errno_resources(errno)) { r= errno; goto xit; }
413 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
415 } while (ads->tcpstate == server_ok);
420 for (i=0; i<ads->nudpsockets; i++) {
421 udp= &ads->udpsockets[i];
422 if (fd != udp->fd) continue;
424 udpaddrlen= sizeof(udpaddr);
425 r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
427 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
428 if (errno == EINTR) continue;
429 if (errno_resources(errno)) { r= errno; goto xit; }
430 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
434 serv < ads->nservers &&
435 !adns__sockaddrs_equal(&udpaddr.sa,
436 &ads->servers[serv].addr.sa);
438 if (serv >= ads->nservers) {
439 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
440 adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
443 adns__procdgram(ads,udpbuf,r,serv,0,*now);
449 adns__returning(ads,0);
453 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
456 adns__consistency(ads,0,cc_enter);
458 switch (ads->tcpstate) {
459 case server_disconnected:
462 case server_connecting:
463 if (fd != ads->tcpsocket) break;
464 assert(ads->tcprecv.used==0);
465 assert(ads->tcprecv_skip==0);
467 /* This function can be called even if the fd wasn't actually
468 * flagged as writeable. For asynch tcp connect we have to
469 * actually use the writeability to tell us the connect has
470 * completed (or failed), so we need to double check. */
472 struct timeval timeout = { 0,0 };
474 FD_SET(ads->tcpsocket,&writeable);
475 r= select(ads->tcpsocket+1,0,&writeable,0,&timeout);
478 if (errno==EINTR) continue;
479 adns__tcp_broken(ads,"select","failed connecting writeability check");
482 assert(FD_ISSET(ads->tcpsocket,&writeable));
483 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
484 r= read(ads->tcpsocket,ads->tcprecv.buf,1);
485 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
486 tcp_connected(ads,*now);
490 adns__tcp_broken(ads,"connect/read","sent data before first request");
493 if (errno==EINTR) continue;
494 if (errno_resources(errno)) { r= errno; goto xit; }
495 adns__tcp_broken(ads,"connect/read",strerror(errno));
499 if (fd != ads->tcpsocket) break;
500 while (ads->tcpsend.used) {
501 adns__sigpipe_protect(ads);
502 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
503 adns__sigpipe_unprotect(ads);
505 if (errno==EINTR) continue;
506 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
507 if (errno_resources(errno)) { r= errno; goto xit; }
508 adns__tcp_broken(ads,"write",strerror(errno));
511 assert(r <= ads->tcpsend.used);
512 ads->tcpsend.used -= r;
513 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
523 adns__returning(ads,0);
527 int adns_processexceptional(adns_state ads, int fd,
528 const struct timeval *now) {
529 adns__consistency(ads,0,cc_enter);
530 switch (ads->tcpstate) {
531 case server_disconnected:
534 case server_connecting:
536 if (fd != ads->tcpsocket) break;
537 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
542 adns__returning(ads,0);
546 static void fd_event(adns_state ads, int fd,
547 int revent, int pollflag,
548 int maxfd, const fd_set *fds,
549 int (*func)(adns_state, int fd,
550 const struct timeval *now),
551 struct timeval now, int *r_r) {
554 if (!(revent & pollflag)) return;
555 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
556 r= func(ads,fd,&now);
561 adns__diag(ads,-1,0,"process fd failed after select:"
562 " %s",strerror(errno));
563 adns_globalsystemfailure(ads);
568 void adns__fdevents(adns_state ads,
569 const struct pollfd *pollfds, int npollfds,
570 int maxfd, const fd_set *readfds,
571 const fd_set *writefds, const fd_set *exceptfds,
572 struct timeval now, int *r_r) {
575 for (i=0; i<npollfds; i++) {
577 if (fd >= maxfd) maxfd= fd+1;
578 revents= pollfds[i].revents;
579 #define EV(pollfl,fds,how) \
580 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
581 EV( POLLIN, readfds, readable );
582 EV( POLLOUT, writefds, writeable );
583 EV( POLLPRI, exceptfds, exceptional );
588 /* Wrappers for select(2). */
590 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
591 fd_set *writefds_io, fd_set *exceptfds_io,
592 struct timeval **tv_mod, struct timeval *tv_tobuf,
593 const struct timeval *now) {
594 struct timeval tv_nowbuf;
595 struct pollfd pollfds[MAX_POLLFDS];
596 int i, fd, maxfd, npollfds;
598 adns__consistency(ads,0,cc_enter);
600 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
601 /* The caller is planning to sleep. */
602 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
603 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
604 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
607 npollfds= adns__pollfds(ads,pollfds);
609 for (i=0; i<npollfds; i++) {
611 if (fd >= maxfd) maxfd= fd+1;
612 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
613 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
614 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
619 adns__returning(ads,0);
622 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
623 const fd_set *writefds, const fd_set *exceptfds,
624 const struct timeval *now) {
625 struct timeval tv_buf;
626 struct pollfd pollfds[MAX_POLLFDS];
629 adns__consistency(ads,0,cc_enter);
630 adns__must_gettimeofday(ads,&now,&tv_buf);
632 adns_processtimeouts(ads,now);
634 npollfds= adns__pollfds(ads,pollfds);
635 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
638 maxfd,readfds,writefds,exceptfds,
641 adns__returning(ads,0);
644 /* General helpful functions. */
646 void adns_globalsystemfailure(adns_state ads) {
647 /* Must not be called by adns during actual processing of a
648 * particular query, since it reenters adns. Only safe to call in
649 * situations where it would be safe to call adns_returning. */
650 adns__consistency(ads,0,cc_enter);
655 if ((qu= ads->QQ.head)) { \
656 LIST_UNLINK(ads->QQ,qu); \
657 adns__query_fail(qu, adns_s_systemfail); \
666 switch (ads->tcpstate) {
667 case server_connecting:
669 adns__tcp_broken(ads,0,0);
671 case server_disconnected:
677 adns__returning(ads,0);
680 int adns_processany(adns_state ads) {
683 struct pollfd pollfds[MAX_POLLFDS];
686 adns__consistency(ads,0,cc_enter);
688 r= adns__gettimeofday(ads,&now);
689 if (!r) adns_processtimeouts(ads,&now);
691 /* We just use adns__fdevents to loop over the fd's trying them.
692 * This seems more sensible than calling select, since we're most
693 * likely just to want to do a read on one or two fds anyway.
695 npollfds= adns__pollfds(ads,pollfds);
696 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
702 adns__returning(ads,0);
706 void adns__autosys(adns_state ads, struct timeval now) {
707 if (ads->iflags & adns_if_noautosys) return;
708 adns_processany(ads);
711 int adns__internal_check(adns_state ads,
712 adns_query *query_io,
713 adns_answer **answer,
719 if (ads->output.head) {
720 qu= ads->output.head;
721 } else if (ads->udpw.head || ads->tcpw.head) {
727 if (qu->id>=0) return EAGAIN;
729 LIST_UNLINK(ads->output,qu);
731 if (context_r) *context_r= qu->ctx.ext;
737 int adns_wait(adns_state ads,
738 adns_query *query_io,
739 adns_answer **answer_r,
742 fd_set readfds, writefds, exceptfds;
743 struct timeval tvbuf, *tvp;
745 adns__consistency(ads,*query_io,cc_enter);
747 r= adns__internal_check(ads,query_io,answer_r,context_r);
748 if (r != EAGAIN) break;
750 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
751 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
753 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
755 if (errno == EINTR) {
756 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
758 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
759 adns_globalsystemfailure(ads);
763 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
766 adns__returning(ads,0);
770 int adns_check(adns_state ads,
771 adns_query *query_io,
772 adns_answer **answer_r,
777 adns__consistency(ads,*query_io,cc_enter);
778 r= adns__gettimeofday(ads,&now);
779 if (!r) adns__autosys(ads,now);
781 r= adns__internal_check(ads,query_io,answer_r,context_r);
782 adns__returning(ads,0);