4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
8 * This file is part of adns, which is Copyright Ian Jackson
9 * and contributors (see the file INSTALL for full details).
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation.
29 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
40 /* TCP connection management. */
42 static void tcp_close(adns_state ads) {
43 close(ads->tcpsocket);
45 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
48 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
52 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
54 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
56 if (ads->tcpstate == server_connecting) {
57 /* Counts as a retry for all the queries waiting for TCP. */
58 for (qu= ads->tcpw.head; qu; qu= qu->next)
63 ads->tcpstate= server_broken;
64 ads->tcpserver= (serv+1)%ads->nservers;
67 static void tcp_connected(adns_state ads, struct timeval now) {
70 adns__debug(ads,ads->tcpserver,0,"TCP connected");
71 ads->tcpstate= server_ok;
72 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
74 assert(qu->state == query_tcpw);
75 adns__querysend_tcp(qu,now);
79 static void tcp_broken_events(adns_state ads) {
82 assert(ads->tcpstate == server_broken);
83 for (qu= ads->tcpw.head; qu; qu= nqu) {
85 assert(qu->state == query_tcpw);
86 if (qu->retries > ads->nservers) {
87 LIST_UNLINK(ads->tcpw,qu);
88 adns__query_fail(qu,adns_s_allservfail);
91 ads->tcpstate= server_disconnected;
94 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
97 struct protoent *proto;
99 for (tries=0; tries<ads->nservers; tries++) {
100 switch (ads->tcpstate) {
101 case server_connecting:
105 case server_disconnected:
111 assert(!ads->tcpsend.used);
112 assert(!ads->tcprecv.used);
113 assert(!ads->tcprecv_skip);
115 proto= getprotobyname("tcp");
117 adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
120 addr = &ads->servers[ads->tcpserver];
121 fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
123 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
126 r= adns__setnonblock(ads,fd);
128 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
133 r= connect(fd,&addr->addr.sa,addr->len);
135 ads->tcpstate= server_connecting;
136 if (r==0) { tcp_connected(ads,now); return; }
137 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
138 ads->tcptimeout= now;
139 timevaladd(&ads->tcptimeout,TCPCONNMS);
142 adns__tcp_broken(ads,"connect",strerror(errno));
143 tcp_broken_events(ads);
147 /* Timeout handling functions. */
149 int adns__gettimeofday(adns_state ads, struct timeval *tv) {
150 if (!(ads->iflags & adns_if_monotonic))
151 return gettimeofday(tv,0);
154 int r = clock_gettime(CLOCK_MONOTONIC,&ts);
157 tv->tv_sec = ts.tv_sec;
158 tv->tv_usec = ts.tv_nsec / 1000;
162 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
163 struct timeval *tv_buf) {
164 const struct timeval *now;
169 r= adns__gettimeofday(ads,tv_buf); if (!r) { *now_io= tv_buf; return; }
170 adns__diag(ads,-1,0,"gettimeofday/clock_gettime failed: %s",
172 adns_globalsystemfailure(ads);
176 static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
177 struct timeval *rbuf;
182 if (!rbuf) { *tv_io= rbuf= tvbuf; }
187 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
188 struct timeval maxto) {
189 struct timeval *rbuf;
194 *tvbuf= maxto; *tv_io= tvbuf;
196 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
198 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
199 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
202 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
203 struct timeval now, struct timeval maxtime) {
207 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
208 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
210 maxtime.tv_sec -= (now.tv_sec+2);
211 maxtime.tv_usec -= (now.tv_usec-2000000);
212 dr= ldiv(maxtime.tv_usec,1000000);
213 maxtime.tv_sec += dr.quot;
214 maxtime.tv_usec -= dr.quot*1000000;
215 if (maxtime.tv_sec<0) timerclear(&maxtime);
216 inter_maxto(tv_io,tvbuf,maxtime);
219 static void timeouts_queue(adns_state ads, int act,
220 struct timeval **tv_io, struct timeval *tvbuf,
221 struct timeval now, struct query_queue *queue) {
223 struct timeval expires;
225 for (qu= queue->head; qu; qu= nqu) {
227 if (timercmp(&now,&qu->timeout_started,<)) /* clock rewound */
228 qu->timeout_started= now;
229 expires= qu->timeout_started;
230 timevaladd(&expires, qu->timeout_ms);
231 if (!timercmp(&now,&expires,>)) {
232 inter_maxtoabs(tv_io,tvbuf,now,expires);
234 if (!act) { inter_immed(tv_io,tvbuf); return; }
235 LIST_UNLINK(*queue,qu);
236 if (qu->state != query_tosend) {
237 adns__query_fail(qu,adns_s_timeout);
239 adns__query_send(qu,now);
246 static void tcp_events(adns_state ads, int act,
247 struct timeval **tv_io, struct timeval *tvbuf,
248 struct timeval now) {
250 switch (ads->tcpstate) {
252 if (!act) { inter_immed(tv_io,tvbuf); return; }
253 tcp_broken_events(ads);
254 case server_disconnected: /* fall through */
255 if (!ads->tcpw.head) return;
256 if (!act) { inter_immed(tv_io,tvbuf); return; }
257 adns__tcp_tryconnect(ads,now);
260 if (ads->tcpw.head) return;
261 if (!ads->tcptimeout.tv_sec) {
262 assert(!ads->tcptimeout.tv_usec);
263 ads->tcptimeout= now;
264 timevaladd(&ads->tcptimeout,TCPIDLEMS);
266 case server_connecting: /* fall through */
267 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
268 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
271 /* TCP timeout has happened */
272 switch (ads->tcpstate) {
273 case server_connecting: /* failed to connect */
274 adns__tcp_broken(ads,"unable to make connection","timed out");
276 case server_ok: /* idle timeout */
278 ads->tcpstate= server_disconnected;
292 void adns__timeouts(adns_state ads, int act,
293 struct timeval **tv_io, struct timeval *tvbuf,
294 struct timeval now) {
295 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
296 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
297 tcp_events(ads,act,tv_io,tvbuf,now);
300 void adns_firsttimeout(adns_state ads,
301 struct timeval **tv_io, struct timeval *tvbuf,
302 struct timeval now) {
303 adns__consistency(ads,0,cc_enter);
304 adns__timeouts(ads, 0, tv_io,tvbuf, now);
305 adns__returning(ads,0);
308 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
309 struct timeval tv_buf;
311 adns__consistency(ads,0,cc_enter);
312 adns__must_gettimeofday(ads,&now,&tv_buf);
313 if (now) adns__timeouts(ads, 1, 0,0, *now);
314 adns__returning(ads,0);
317 /* fd handling functions. These are the top-level of the real work of
318 * reception and often transmission.
321 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
322 /* Returns the number of entries filled in. Always zeroes revents. */
324 #define ADD_POLLFD(wantfd, wantevents) do{ \
325 pollfds_buf[nwanted].fd= (wantfd); \
326 pollfds_buf[nwanted].events= (wantevents); \
327 pollfds_buf[nwanted].revents= 0; \
333 assert(MAX_POLLFDS == MAXUDP + 1);
335 for (i=0; i<ads->nudpsockets; i++)
336 ADD_POLLFD(ads->udpsockets[i].fd, POLLIN);
338 switch (ads->tcpstate) {
339 case server_disconnected:
342 case server_connecting:
343 ADD_POLLFD(ads->tcpsocket, POLLOUT);
346 ADD_POLLFD(ads->tcpsocket,
347 ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI);
352 assert(nwanted<=MAX_POLLFDS);
357 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
358 int want, dgramlen, r, i, serv, old_skip;
359 socklen_t udpaddrlen;
360 byte udpbuf[DNS_MAXUDP];
361 char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
362 struct udpsocket *udp;
363 adns_sockaddr udpaddr;
365 adns__consistency(ads,0,cc_enter);
367 switch (ads->tcpstate) {
368 case server_disconnected:
370 case server_connecting:
373 if (fd != ads->tcpsocket) break;
374 assert(!ads->tcprecv_skip);
376 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
377 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
378 ads->tcprecv.buf[ads->tcprecv_skip+1]);
379 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
380 old_skip= ads->tcprecv_skip;
381 ads->tcprecv_skip += 2+dgramlen;
382 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
383 dgramlen, ads->tcpserver, 1,*now);
391 ads->tcprecv.used -= ads->tcprecv_skip;
392 memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
394 ads->tcprecv_skip= 0;
395 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
396 assert(ads->tcprecv.used <= ads->tcprecv.avail);
397 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
398 r= read(ads->tcpsocket,
399 ads->tcprecv.buf+ads->tcprecv.used,
400 ads->tcprecv.avail-ads->tcprecv.used);
402 ads->tcprecv.used+= r;
405 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
406 if (errno==EINTR) continue;
407 if (errno_resources(errno)) { r= errno; goto xit; }
409 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
411 } while (ads->tcpstate == server_ok);
416 for (i=0; i<ads->nudpsockets; i++) {
417 udp= &ads->udpsockets[i];
418 if (fd != udp->fd) continue;
420 udpaddrlen= sizeof(udpaddr);
421 r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
423 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
424 if (errno == EINTR) continue;
425 if (errno_resources(errno)) { r= errno; goto xit; }
426 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
430 serv < ads->nservers &&
431 !adns__sockaddrs_equal(&udpaddr.sa,
432 &ads->servers[serv].addr.sa);
434 if (serv >= ads->nservers) {
435 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
436 adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
439 adns__procdgram(ads,udpbuf,r,serv,0,*now);
445 adns__returning(ads,0);
449 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
452 adns__consistency(ads,0,cc_enter);
454 switch (ads->tcpstate) {
455 case server_disconnected:
458 case server_connecting:
459 if (fd != ads->tcpsocket) break;
460 assert(ads->tcprecv.used==0);
461 assert(ads->tcprecv_skip==0);
463 /* This function can be called even if the fd wasn't actually
464 * flagged as writeable. For asynch tcp connect we have to
465 * actually use the writeability to tell us the connect has
466 * completed (or failed), so we need to double check. */
468 struct timeval timeout = { 0,0 };
470 FD_SET(ads->tcpsocket,&writeable);
471 r= select(ads->tcpsocket+1,0,&writeable,0,&timeout);
474 if (errno==EINTR) continue;
475 adns__tcp_broken(ads,"select","failed connecting writeability check");
478 assert(FD_ISSET(ads->tcpsocket,&writeable));
479 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
480 r= read(ads->tcpsocket,ads->tcprecv.buf,1);
481 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
482 tcp_connected(ads,*now);
486 adns__tcp_broken(ads,"connect/read","sent data before first request");
489 if (errno==EINTR) continue;
490 if (errno_resources(errno)) { r= errno; goto xit; }
491 adns__tcp_broken(ads,"connect/read",strerror(errno));
495 if (fd != ads->tcpsocket) break;
496 while (ads->tcpsend.used) {
497 adns__sigpipe_protect(ads);
498 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
499 adns__sigpipe_unprotect(ads);
501 if (errno==EINTR) continue;
502 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
503 if (errno_resources(errno)) { r= errno; goto xit; }
504 adns__tcp_broken(ads,"write",strerror(errno));
507 assert(r <= ads->tcpsend.used);
508 ads->tcpsend.used -= r;
509 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
519 adns__returning(ads,0);
523 int adns_processexceptional(adns_state ads, int fd,
524 const struct timeval *now) {
525 adns__consistency(ads,0,cc_enter);
526 switch (ads->tcpstate) {
527 case server_disconnected:
530 case server_connecting:
532 if (fd != ads->tcpsocket) break;
533 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
538 adns__returning(ads,0);
542 static void fd_event(adns_state ads, int fd,
543 int revent, int pollflag,
544 int maxfd, const fd_set *fds,
545 int (*func)(adns_state, int fd,
546 const struct timeval *now),
547 struct timeval now, int *r_r) {
550 if (!(revent & pollflag)) return;
551 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
552 r= func(ads,fd,&now);
557 adns__diag(ads,-1,0,"process fd failed after select:"
558 " %s",strerror(errno));
559 adns_globalsystemfailure(ads);
564 void adns__fdevents(adns_state ads,
565 const struct pollfd *pollfds, int npollfds,
566 int maxfd, const fd_set *readfds,
567 const fd_set *writefds, const fd_set *exceptfds,
568 struct timeval now, int *r_r) {
571 for (i=0; i<npollfds; i++) {
573 if (fd >= maxfd) maxfd= fd+1;
574 revents= pollfds[i].revents;
575 #define EV(pollfl,fds,how) \
576 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
577 EV( POLLIN, readfds, readable );
578 EV( POLLOUT, writefds, writeable );
579 EV( POLLPRI, exceptfds, exceptional );
584 /* Wrappers for select(2). */
586 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
587 fd_set *writefds_io, fd_set *exceptfds_io,
588 struct timeval **tv_mod, struct timeval *tv_tobuf,
589 const struct timeval *now) {
590 struct timeval tv_nowbuf;
591 struct pollfd pollfds[MAX_POLLFDS];
592 int i, fd, maxfd, npollfds;
594 adns__consistency(ads,0,cc_enter);
596 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
597 /* The caller is planning to sleep. */
598 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
599 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
600 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
603 npollfds= adns__pollfds(ads,pollfds);
605 for (i=0; i<npollfds; i++) {
607 if (fd >= maxfd) maxfd= fd+1;
608 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
609 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
610 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
615 adns__returning(ads,0);
618 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
619 const fd_set *writefds, const fd_set *exceptfds,
620 const struct timeval *now) {
621 struct timeval tv_buf;
622 struct pollfd pollfds[MAX_POLLFDS];
625 adns__consistency(ads,0,cc_enter);
626 adns__must_gettimeofday(ads,&now,&tv_buf);
628 adns_processtimeouts(ads,now);
630 npollfds= adns__pollfds(ads,pollfds);
631 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
634 maxfd,readfds,writefds,exceptfds,
637 adns__returning(ads,0);
640 /* General helpful functions. */
642 void adns_globalsystemfailure(adns_state ads) {
643 /* Must not be called by adns during actual processing of a
644 * particular query, since it reenters adns. Only safe to call in
645 * situations where it would be safe to call adns_returning. */
646 adns__consistency(ads,0,cc_enter);
651 if ((qu= ads->QQ.head)) { \
652 LIST_UNLINK(ads->QQ,qu); \
653 adns__query_fail(qu, adns_s_systemfail); \
662 switch (ads->tcpstate) {
663 case server_connecting:
665 adns__tcp_broken(ads,0,0);
667 case server_disconnected:
673 adns__returning(ads,0);
676 int adns_processany(adns_state ads) {
679 struct pollfd pollfds[MAX_POLLFDS];
682 adns__consistency(ads,0,cc_enter);
684 r= adns__gettimeofday(ads,&now);
685 if (!r) adns_processtimeouts(ads,&now);
687 /* We just use adns__fdevents to loop over the fd's trying them.
688 * This seems more sensible than calling select, since we're most
689 * likely just to want to do a read on one or two fds anyway.
691 npollfds= adns__pollfds(ads,pollfds);
692 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
698 adns__returning(ads,0);
702 void adns__autosys(adns_state ads, struct timeval now) {
703 if (ads->iflags & adns_if_noautosys) return;
704 adns_processany(ads);
707 int adns__internal_check(adns_state ads,
708 adns_query *query_io,
709 adns_answer **answer,
715 if (ads->output.head) {
716 qu= ads->output.head;
717 } else if (ads->udpw.head || ads->tcpw.head) {
723 if (qu->id>=0) return EAGAIN;
725 LIST_UNLINK(ads->output,qu);
727 if (context_r) *context_r= qu->ctx.ext;
733 int adns_wait(adns_state ads,
734 adns_query *query_io,
735 adns_answer **answer_r,
738 fd_set readfds, writefds, exceptfds;
739 struct timeval tvbuf, *tvp;
741 adns__consistency(ads,*query_io,cc_enter);
743 r= adns__internal_check(ads,query_io,answer_r,context_r);
744 if (r != EAGAIN) break;
746 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
747 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
749 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
751 if (errno == EINTR) {
752 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
754 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
755 adns_globalsystemfailure(ads);
759 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
762 adns__returning(ads,0);
766 int adns_check(adns_state ads,
767 adns_query *query_io,
768 adns_answer **answer_r,
773 adns__consistency(ads,*query_io,cc_enter);
774 r= adns__gettimeofday(ads,&now);
775 if (!r) adns__autosys(ads,now);
777 r= adns__internal_check(ads,query_io,answer_r,context_r);
778 adns__returning(ads,0);