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>
43 /* TCP connection management. */
45 static void tcp_close(adns_state ads) {
46 close(ads->tcpsocket);
48 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
51 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
55 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
57 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
59 if (ads->tcpstate == server_connecting) {
60 /* Counts as a retry for all the queries waiting for TCP. */
61 for (qu= ads->tcpw.head; qu; qu= qu->next)
66 ads->tcpstate= server_broken;
67 ads->tcpserver= (serv+1)%ads->nservers;
70 static void tcp_connected(adns_state ads, struct timeval now) {
73 adns__debug(ads,ads->tcpserver,0,"TCP connected");
74 ads->tcpstate= server_ok;
75 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
77 assert(qu->state == query_tcpw);
78 adns__querysend_tcp(qu,now);
82 static void tcp_broken_events(adns_state ads) {
85 assert(ads->tcpstate == server_broken);
86 for (qu= ads->tcpw.head; qu; qu= nqu) {
88 assert(qu->state == query_tcpw);
89 if (qu->retries > ads->nservers) {
90 LIST_UNLINK(ads->tcpw,qu);
91 adns__query_fail(qu,adns_s_allservfail);
94 ads->tcpstate= server_disconnected;
97 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
100 struct protoent *proto;
102 for (tries=0; tries<ads->nservers; tries++) {
103 switch (ads->tcpstate) {
104 case server_connecting:
108 case server_disconnected:
114 assert(!ads->tcpsend.used);
115 assert(!ads->tcprecv.used);
116 assert(!ads->tcprecv_skip);
118 proto= getprotobyname("tcp");
120 adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
123 addr = &ads->servers[ads->tcpserver];
124 fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
126 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
129 r= adns__setnonblock(ads,fd);
131 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
136 r= connect(fd,&addr->addr.sa,addr->len);
138 ads->tcpstate= server_connecting;
139 if (r==0) { tcp_connected(ads,now); return; }
140 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
141 ads->tcptimeout= now;
142 timevaladd(&ads->tcptimeout,TCPCONNMS);
145 adns__tcp_broken(ads,"connect",strerror(errno));
146 tcp_broken_events(ads);
150 /* Timeout handling functions. */
152 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
153 struct timeval *tv_buf) {
154 const struct timeval *now;
159 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
160 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
161 adns_globalsystemfailure(ads);
165 static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
166 struct timeval *rbuf;
171 if (!rbuf) { *tv_io= rbuf= tvbuf; }
176 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
177 struct timeval maxto) {
178 struct timeval *rbuf;
183 *tvbuf= maxto; *tv_io= tvbuf;
185 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
187 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
188 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
191 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
192 struct timeval now, struct timeval maxtime) {
196 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
197 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
199 maxtime.tv_sec -= (now.tv_sec+2);
200 maxtime.tv_usec -= (now.tv_usec-2000000);
201 dr= ldiv(maxtime.tv_usec,1000000);
202 maxtime.tv_sec += dr.quot;
203 maxtime.tv_usec -= dr.quot*1000000;
204 if (maxtime.tv_sec<0) timerclear(&maxtime);
205 inter_maxto(tv_io,tvbuf,maxtime);
208 static void timeouts_queue(adns_state ads, int act,
209 struct timeval **tv_io, struct timeval *tvbuf,
210 struct timeval now, struct query_queue *queue) {
212 struct timeval expires;
214 for (qu= queue->head; qu; qu= nqu) {
216 if (timercmp(&now,&qu->timeout_started,<)) /* clock rewound */
217 qu->timeout_started= now;
218 expires= qu->timeout_started;
219 timevaladd(&expires, qu->timeout_ms);
220 if (!timercmp(&now,&expires,>)) {
221 inter_maxtoabs(tv_io,tvbuf,now,expires);
223 if (!act) { inter_immed(tv_io,tvbuf); return; }
224 LIST_UNLINK(*queue,qu);
225 if (qu->state != query_tosend) {
226 adns__query_fail(qu,adns_s_timeout);
228 adns__query_send(qu,now);
235 static void tcp_events(adns_state ads, int act,
236 struct timeval **tv_io, struct timeval *tvbuf,
237 struct timeval now) {
239 switch (ads->tcpstate) {
241 if (!act) { inter_immed(tv_io,tvbuf); return; }
242 tcp_broken_events(ads);
243 case server_disconnected: /* fall through */
244 if (!ads->tcpw.head) return;
245 if (!act) { inter_immed(tv_io,tvbuf); return; }
246 adns__tcp_tryconnect(ads,now);
249 if (ads->tcpw.head) return;
250 if (!ads->tcptimeout.tv_sec) {
251 assert(!ads->tcptimeout.tv_usec);
252 ads->tcptimeout= now;
253 timevaladd(&ads->tcptimeout,TCPIDLEMS);
255 case server_connecting: /* fall through */
256 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
257 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
260 /* TCP timeout has happened */
261 switch (ads->tcpstate) {
262 case server_connecting: /* failed to connect */
263 adns__tcp_broken(ads,"unable to make connection","timed out");
265 case server_ok: /* idle timeout */
267 ads->tcpstate= server_disconnected;
281 void adns__timeouts(adns_state ads, int act,
282 struct timeval **tv_io, struct timeval *tvbuf,
283 struct timeval now) {
284 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
285 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
286 tcp_events(ads,act,tv_io,tvbuf,now);
289 void adns_firsttimeout(adns_state ads,
290 struct timeval **tv_io, struct timeval *tvbuf,
291 struct timeval now) {
292 adns__consistency(ads,0,cc_enter);
293 adns__timeouts(ads, 0, tv_io,tvbuf, now);
294 adns__returning(ads,0);
297 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
298 struct timeval tv_buf;
300 adns__consistency(ads,0,cc_enter);
301 adns__must_gettimeofday(ads,&now,&tv_buf);
302 if (now) adns__timeouts(ads, 1, 0,0, *now);
303 adns__returning(ads,0);
306 /* fd handling functions. These are the top-level of the real work of
307 * reception and often transmission.
310 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
311 /* Returns the number of entries filled in. Always zeroes revents. */
313 #define ADD_POLLFD(wantfd, wantevents) do{ \
314 pollfds_buf[nwanted].fd= (wantfd); \
315 pollfds_buf[nwanted].events= (wantevents); \
316 pollfds_buf[nwanted].revents= 0; \
322 assert(MAX_POLLFDS == MAXUDP + 1);
324 for (i=0; i<ads->nudpsockets; i++)
325 ADD_POLLFD(ads->udpsockets[i].fd, POLLIN);
327 switch (ads->tcpstate) {
328 case server_disconnected:
331 case server_connecting:
332 ADD_POLLFD(ads->tcpsocket, POLLOUT);
335 ADD_POLLFD(ads->tcpsocket,
336 ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI);
341 assert(nwanted<=MAX_POLLFDS);
346 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
347 int want, dgramlen, r, i, serv, old_skip;
348 socklen_t udpaddrlen;
349 byte udpbuf[DNS_MAXUDP];
350 char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
351 struct udpsocket *udp;
352 adns_sockaddr udpaddr;
354 adns__consistency(ads,0,cc_enter);
356 switch (ads->tcpstate) {
357 case server_disconnected:
359 case server_connecting:
362 if (fd != ads->tcpsocket) break;
363 assert(!ads->tcprecv_skip);
365 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
366 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
367 ads->tcprecv.buf[ads->tcprecv_skip+1]);
368 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
369 old_skip= ads->tcprecv_skip;
370 ads->tcprecv_skip += 2+dgramlen;
371 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
372 dgramlen, ads->tcpserver, 1,*now);
380 ads->tcprecv.used -= ads->tcprecv_skip;
381 memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
383 ads->tcprecv_skip= 0;
384 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
385 assert(ads->tcprecv.used <= ads->tcprecv.avail);
386 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
387 r= read(ads->tcpsocket,
388 ads->tcprecv.buf+ads->tcprecv.used,
389 ads->tcprecv.avail-ads->tcprecv.used);
391 ads->tcprecv.used+= r;
394 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
395 if (errno==EINTR) continue;
396 if (errno_resources(errno)) { r= errno; goto xit; }
398 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
400 } while (ads->tcpstate == server_ok);
405 for (i=0; i<ads->nudpsockets; i++) {
406 udp= &ads->udpsockets[i];
407 if (fd != udp->fd) continue;
409 udpaddrlen= sizeof(udpaddr);
410 r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
412 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
413 if (errno == EINTR) continue;
414 if (errno_resources(errno)) { r= errno; goto xit; }
415 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
419 serv < ads->nservers &&
420 !adns__sockaddrs_equal(&udpaddr.sa,
421 &ads->servers[serv].addr.sa);
423 if (serv >= ads->nservers) {
424 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
425 adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
428 adns__procdgram(ads,udpbuf,r,serv,0,*now);
434 adns__returning(ads,0);
438 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
441 adns__consistency(ads,0,cc_enter);
443 switch (ads->tcpstate) {
444 case server_disconnected:
447 case server_connecting:
448 if (fd != ads->tcpsocket) break;
449 assert(ads->tcprecv.used==0);
450 assert(ads->tcprecv_skip==0);
452 /* This function can be called even if the fd wasn't actually
453 * flagged as writeable. For asynch tcp connect we have to
454 * actually use the writeability to tell us the connect has
455 * completed (or failed), so we need to double check. */
457 struct timeval timeout = { 0,0 };
459 FD_SET(ads->tcpsocket,&writeable);
460 r= select(ads->tcpsocket+1,0,&writeable,0,&timeout);
463 if (errno==EINTR) continue;
464 adns__tcp_broken(ads,"select","failed connecting writeability check");
467 assert(FD_ISSET(ads->tcpsocket,&writeable));
468 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
469 r= read(ads->tcpsocket,ads->tcprecv.buf,1);
470 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
471 tcp_connected(ads,*now);
475 adns__tcp_broken(ads,"connect/read","sent data before first request");
478 if (errno==EINTR) continue;
479 if (errno_resources(errno)) { r= errno; goto xit; }
480 adns__tcp_broken(ads,"connect/read",strerror(errno));
484 if (fd != ads->tcpsocket) break;
485 while (ads->tcpsend.used) {
486 adns__sigpipe_protect(ads);
487 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
488 adns__sigpipe_unprotect(ads);
490 if (errno==EINTR) continue;
491 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
492 if (errno_resources(errno)) { r= errno; goto xit; }
493 adns__tcp_broken(ads,"write",strerror(errno));
496 assert(r <= ads->tcpsend.used);
497 ads->tcpsend.used -= r;
498 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
508 adns__returning(ads,0);
512 int adns_processexceptional(adns_state ads, int fd,
513 const struct timeval *now) {
514 adns__consistency(ads,0,cc_enter);
515 switch (ads->tcpstate) {
516 case server_disconnected:
519 case server_connecting:
521 if (fd != ads->tcpsocket) break;
522 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
527 adns__returning(ads,0);
531 static void fd_event(adns_state ads, int fd,
532 int revent, int pollflag,
533 int maxfd, const fd_set *fds,
534 int (*func)(adns_state, int fd,
535 const struct timeval *now),
536 struct timeval now, int *r_r) {
539 if (!(revent & pollflag)) return;
540 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
541 r= func(ads,fd,&now);
546 adns__diag(ads,-1,0,"process fd failed after select:"
547 " %s",strerror(errno));
548 adns_globalsystemfailure(ads);
553 void adns__fdevents(adns_state ads,
554 const struct pollfd *pollfds, int npollfds,
555 int maxfd, const fd_set *readfds,
556 const fd_set *writefds, const fd_set *exceptfds,
557 struct timeval now, int *r_r) {
560 for (i=0; i<npollfds; i++) {
562 if (fd >= maxfd) maxfd= fd+1;
563 revents= pollfds[i].revents;
564 #define EV(pollfl,fds,how) \
565 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
566 EV( POLLIN, readfds, readable );
567 EV( POLLOUT, writefds, writeable );
568 EV( POLLPRI, exceptfds, exceptional );
573 /* Wrappers for select(2). */
575 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
576 fd_set *writefds_io, fd_set *exceptfds_io,
577 struct timeval **tv_mod, struct timeval *tv_tobuf,
578 const struct timeval *now) {
579 struct timeval tv_nowbuf;
580 struct pollfd pollfds[MAX_POLLFDS];
581 int i, fd, maxfd, npollfds;
583 adns__consistency(ads,0,cc_enter);
585 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
586 /* The caller is planning to sleep. */
587 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
588 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
589 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
592 npollfds= adns__pollfds(ads,pollfds);
594 for (i=0; i<npollfds; i++) {
596 if (fd >= maxfd) maxfd= fd+1;
597 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
598 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
599 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
604 adns__returning(ads,0);
607 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
608 const fd_set *writefds, const fd_set *exceptfds,
609 const struct timeval *now) {
610 struct timeval tv_buf;
611 struct pollfd pollfds[MAX_POLLFDS];
614 adns__consistency(ads,0,cc_enter);
615 adns__must_gettimeofday(ads,&now,&tv_buf);
617 adns_processtimeouts(ads,now);
619 npollfds= adns__pollfds(ads,pollfds);
620 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
623 maxfd,readfds,writefds,exceptfds,
626 adns__returning(ads,0);
629 /* General helpful functions. */
631 void adns_globalsystemfailure(adns_state ads) {
632 /* Must not be called by adns during actual processing of a
633 * particular query, since it reenters adns. Only safe to call in
634 * situations where it would be safe to call adns_returning. */
635 adns__consistency(ads,0,cc_enter);
640 if ((qu= ads->QQ.head)) { \
641 LIST_UNLINK(ads->QQ,qu); \
642 adns__query_fail(qu, adns_s_systemfail); \
651 switch (ads->tcpstate) {
652 case server_connecting:
654 adns__tcp_broken(ads,0,0);
656 case server_disconnected:
662 adns__returning(ads,0);
665 int adns_processany(adns_state ads) {
668 struct pollfd pollfds[MAX_POLLFDS];
671 adns__consistency(ads,0,cc_enter);
673 r= gettimeofday(&now,0);
674 if (!r) adns_processtimeouts(ads,&now);
676 /* We just use adns__fdevents to loop over the fd's trying them.
677 * This seems more sensible than calling select, since we're most
678 * likely just to want to do a read on one or two fds anyway.
680 npollfds= adns__pollfds(ads,pollfds);
681 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
687 adns__returning(ads,0);
691 void adns__autosys(adns_state ads, struct timeval now) {
692 if (ads->iflags & adns_if_noautosys) return;
693 adns_processany(ads);
696 int adns__internal_check(adns_state ads,
697 adns_query *query_io,
698 adns_answer **answer,
704 if (ads->output.head) {
705 qu= ads->output.head;
706 } else if (ads->udpw.head || ads->tcpw.head) {
712 if (qu->id>=0) return EAGAIN;
714 LIST_UNLINK(ads->output,qu);
716 if (context_r) *context_r= qu->ctx.ext;
722 int adns_wait(adns_state ads,
723 adns_query *query_io,
724 adns_answer **answer_r,
727 fd_set readfds, writefds, exceptfds;
728 struct timeval tvbuf, *tvp;
730 adns__consistency(ads,*query_io,cc_enter);
732 r= adns__internal_check(ads,query_io,answer_r,context_r);
733 if (r != EAGAIN) break;
735 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
736 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
738 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
740 if (errno == EINTR) {
741 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
743 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
744 adns_globalsystemfailure(ads);
748 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
751 adns__returning(ads,0);
755 int adns_check(adns_state ads,
756 adns_query *query_io,
757 adns_answer **answer_r,
762 adns__consistency(ads,*query_io,cc_enter);
763 r= gettimeofday(&now,0);
764 if (!r) adns__autosys(ads,now);
766 r= adns__internal_check(ads,query_io,answer_r,context_r);
767 adns__returning(ads,0);