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 Ian Jackson
10 * Copyright (C) 1999-2000,2003,2006 Tony Finch
11 * Copyright (C) 1991 Massachusetts Institute of Technology
12 * (See the file INSTALL for full details.)
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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) {
49 close(ads->tcpsocket);
51 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
54 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
58 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
60 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
62 if (ads->tcpstate == server_connecting) {
63 /* Counts as a retry for all the queries waiting for TCP. */
64 for (qu= ads->tcpw.head; qu; qu= qu->next)
69 ads->tcpstate= server_broken;
70 ads->tcpserver= (serv+1)%ads->nservers;
73 static void tcp_connected(adns_state ads, struct timeval now) {
76 adns__debug(ads,ads->tcpserver,0,"TCP connected");
77 ads->tcpstate= server_ok;
78 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
80 assert(qu->state == query_tcpw);
81 adns__querysend_tcp(qu,now);
85 static void tcp_broken_events(adns_state ads) {
88 assert(ads->tcpstate == server_broken);
89 for (qu= ads->tcpw.head; qu; qu= nqu) {
91 assert(qu->state == query_tcpw);
92 if (qu->retries > ads->nservers) {
93 LIST_UNLINK(ads->tcpw,qu);
94 adns__query_fail(qu,adns_s_allservfail);
97 ads->tcpstate= server_disconnected;
100 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
102 struct sockaddr_in addr;
103 struct protoent *proto;
105 for (tries=0; tries<ads->nservers; tries++) {
106 switch (ads->tcpstate) {
107 case server_connecting:
111 case server_disconnected:
117 assert(!ads->tcpsend.used);
118 assert(!ads->tcprecv.used);
119 assert(!ads->tcprecv_skip);
121 proto= getprotobyname("tcp");
123 adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
126 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
128 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
131 r= adns__setnonblock(ads,fd);
133 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
138 memset(&addr,0,sizeof(addr));
139 addr.sin_family= AF_INET;
140 addr.sin_port= htons(DNS_PORT);
141 addr.sin_addr= ads->servers[ads->tcpserver].addr;
142 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
144 ads->tcpstate= server_connecting;
145 if (r==0) { tcp_connected(ads,now); return; }
146 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
147 ads->tcptimeout= now;
148 timevaladd(&ads->tcptimeout,TCPCONNMS);
151 adns__tcp_broken(ads,"connect",strerror(errno));
152 tcp_broken_events(ads);
156 /* Timeout handling functions. */
158 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
159 struct timeval *tv_buf) {
160 const struct timeval *now;
165 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
166 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
167 adns_globalsystemfailure(ads);
171 static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
172 struct timeval *rbuf;
177 if (!rbuf) { *tv_io= rbuf= tvbuf; }
182 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
183 struct timeval maxto) {
184 struct timeval *rbuf;
189 *tvbuf= maxto; *tv_io= tvbuf;
191 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
193 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
194 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
197 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
198 struct timeval now, struct timeval maxtime) {
202 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
203 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
205 maxtime.tv_sec -= (now.tv_sec+2);
206 maxtime.tv_usec -= (now.tv_usec-2000000);
207 dr= ldiv(maxtime.tv_usec,1000000);
208 maxtime.tv_sec += dr.quot;
209 maxtime.tv_usec -= dr.quot*1000000;
210 if (maxtime.tv_sec<0) timerclear(&maxtime);
211 inter_maxto(tv_io,tvbuf,maxtime);
214 static void timeouts_queue(adns_state ads, int act,
215 struct timeval **tv_io, struct timeval *tvbuf,
216 struct timeval now, struct query_queue *queue) {
219 for (qu= queue->head; qu; qu= nqu) {
221 if (!timercmp(&now,&qu->timeout,>)) {
222 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
224 if (!act) { inter_immed(tv_io,tvbuf); return; }
225 LIST_UNLINK(*queue,qu);
226 if (qu->state != query_tosend) {
227 adns__query_fail(qu,adns_s_timeout);
229 adns__query_send(qu,now);
236 static void tcp_events(adns_state ads, int act,
237 struct timeval **tv_io, struct timeval *tvbuf,
238 struct timeval now) {
240 switch (ads->tcpstate) {
242 if (!act) { inter_immed(tv_io,tvbuf); return; }
243 tcp_broken_events(ads);
244 case server_disconnected: /* fall through */
245 if (!ads->tcpw.head) return;
246 if (!act) { inter_immed(tv_io,tvbuf); return; }
247 adns__tcp_tryconnect(ads,now);
250 if (ads->tcpw.head) return;
251 if (!ads->tcptimeout.tv_sec) {
252 assert(!ads->tcptimeout.tv_usec);
253 ads->tcptimeout= now;
254 timevaladd(&ads->tcptimeout,TCPIDLEMS);
256 case server_connecting: /* fall through */
257 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
258 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
261 /* TCP timeout has happened */
262 switch (ads->tcpstate) {
263 case server_connecting: /* failed to connect */
264 adns__tcp_broken(ads,"unable to make connection","timed out");
266 case server_ok: /* idle timeout */
268 ads->tcpstate= server_disconnected;
282 void adns__timeouts(adns_state ads, int act,
283 struct timeval **tv_io, struct timeval *tvbuf,
284 struct timeval now) {
285 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
286 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
287 tcp_events(ads,act,tv_io,tvbuf,now);
290 void adns_firsttimeout(adns_state ads,
291 struct timeval **tv_io, struct timeval *tvbuf,
292 struct timeval now) {
293 adns__consistency(ads,0,cc_entex);
294 adns__timeouts(ads, 0, tv_io,tvbuf, now);
295 adns__consistency(ads,0,cc_entex);
298 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
299 struct timeval tv_buf;
301 adns__consistency(ads,0,cc_entex);
302 adns__must_gettimeofday(ads,&now,&tv_buf);
303 if (now) adns__timeouts(ads, 1, 0,0, *now);
304 adns__consistency(ads,0,cc_entex);
307 /* fd handling functions. These are the top-level of the real work of
308 * reception and often transmission.
311 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
312 /* Returns the number of entries filled in. Always zeroes revents. */
314 assert(MAX_POLLFDS==2);
316 pollfds_buf[0].fd= ads->udpsocket;
317 pollfds_buf[0].events= POLLIN;
318 pollfds_buf[0].revents= 0;
320 switch (ads->tcpstate) {
321 case server_disconnected:
324 case server_connecting:
325 pollfds_buf[1].events= POLLOUT;
328 pollfds_buf[1].events=
329 ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
334 pollfds_buf[1].fd= ads->tcpsocket;
338 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
339 int want, dgramlen, r, udpaddrlen, serv, old_skip;
340 byte udpbuf[DNS_MAXUDP];
341 struct sockaddr_in udpaddr;
343 adns__consistency(ads,0,cc_entex);
345 switch (ads->tcpstate) {
346 case server_disconnected:
348 case server_connecting:
351 if (fd != ads->tcpsocket) break;
352 assert(!ads->tcprecv_skip);
354 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
355 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
356 ads->tcprecv.buf[ads->tcprecv_skip+1]);
357 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
358 old_skip= ads->tcprecv_skip;
359 ads->tcprecv_skip += 2+dgramlen;
360 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
361 dgramlen, ads->tcpserver, 1,*now);
369 ads->tcprecv.used -= ads->tcprecv_skip;
370 memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
372 ads->tcprecv_skip= 0;
373 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
374 assert(ads->tcprecv.used <= ads->tcprecv.avail);
375 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
376 r= read(ads->tcpsocket,
377 ads->tcprecv.buf+ads->tcprecv.used,
378 ads->tcprecv.avail-ads->tcprecv.used);
380 ads->tcprecv.used+= r;
383 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
384 if (errno==EINTR) continue;
385 if (errno_resources(errno)) { r= errno; goto xit; }
387 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
389 } while (ads->tcpstate == server_ok);
394 if (fd == ads->udpsocket) {
396 udpaddrlen= sizeof(udpaddr);
397 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
398 (struct sockaddr*)&udpaddr,&udpaddrlen);
400 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
401 if (errno == EINTR) continue;
402 if (errno_resources(errno)) { r= errno; goto xit; }
403 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
406 if (udpaddrlen != sizeof(udpaddr)) {
407 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
408 " (expected %lu)", udpaddrlen,
409 (unsigned long)sizeof(udpaddr));
412 if (udpaddr.sin_family != AF_INET) {
413 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
414 " %u (expected %u)",udpaddr.sin_family,AF_INET);
417 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
418 adns__diag(ads,-1,0,"datagram received from wrong port"
419 " %u (expected %u)", ntohs(udpaddr.sin_port),DNS_PORT);
423 serv < ads->nservers &&
424 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
426 if (serv >= ads->nservers) {
427 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
428 inet_ntoa(udpaddr.sin_addr));
431 adns__procdgram(ads,udpbuf,r,serv,0,*now);
436 adns__consistency(ads,0,cc_entex);
440 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
443 adns__consistency(ads,0,cc_entex);
445 switch (ads->tcpstate) {
446 case server_disconnected:
449 case server_connecting:
450 if (fd != ads->tcpsocket) break;
451 assert(ads->tcprecv.used==0);
452 assert(ads->tcprecv_skip==0);
454 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
455 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
456 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
457 tcp_connected(ads,*now);
461 adns__tcp_broken(ads,"connect/read","sent data before first request");
464 if (errno==EINTR) continue;
465 if (errno_resources(errno)) { r= errno; goto xit; }
466 adns__tcp_broken(ads,"connect/read",strerror(errno));
470 if (fd != ads->tcpsocket) break;
471 while (ads->tcpsend.used) {
472 adns__sigpipe_protect(ads);
473 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
474 adns__sigpipe_unprotect(ads);
476 if (errno==EINTR) continue;
477 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
478 if (errno_resources(errno)) { r= errno; goto xit; }
479 adns__tcp_broken(ads,"write",strerror(errno));
482 ads->tcpsend.used -= r;
483 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
493 adns__consistency(ads,0,cc_entex);
497 int adns_processexceptional(adns_state ads, int fd,
498 const struct timeval *now) {
499 adns__consistency(ads,0,cc_entex);
500 switch (ads->tcpstate) {
501 case server_disconnected:
504 case server_connecting:
506 if (fd != ads->tcpsocket) break;
507 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
512 adns__consistency(ads,0,cc_entex);
516 static void fd_event(adns_state ads, int fd,
517 int revent, int pollflag,
518 int maxfd, const fd_set *fds,
519 int (*func)(adns_state, int fd,
520 const struct timeval *now),
521 struct timeval now, int *r_r) {
524 if (!(revent & pollflag)) return;
525 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
526 r= func(ads,fd,&now);
531 adns__diag(ads,-1,0,"process fd failed after select:"
532 " %s",strerror(errno));
533 adns_globalsystemfailure(ads);
538 void adns__fdevents(adns_state ads,
539 const struct pollfd *pollfds, int npollfds,
540 int maxfd, const fd_set *readfds,
541 const fd_set *writefds, const fd_set *exceptfds,
542 struct timeval now, int *r_r) {
545 for (i=0; i<npollfds; i++) {
547 if (fd >= maxfd) maxfd= fd+1;
548 revents= pollfds[i].revents;
549 #define EV(pollfl,fds,how) \
550 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
551 EV( POLLIN, readfds, readable );
552 EV( POLLOUT, writefds, writeable );
553 EV( POLLPRI, exceptfds, exceptional );
558 /* Wrappers for select(2). */
560 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
561 fd_set *writefds_io, fd_set *exceptfds_io,
562 struct timeval **tv_mod, struct timeval *tv_tobuf,
563 const struct timeval *now) {
564 struct timeval tv_nowbuf;
565 struct pollfd pollfds[MAX_POLLFDS];
566 int i, fd, maxfd, npollfds;
568 adns__consistency(ads,0,cc_entex);
570 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
571 /* The caller is planning to sleep. */
572 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
573 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
574 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
577 npollfds= adns__pollfds(ads,pollfds);
579 for (i=0; i<npollfds; i++) {
581 if (fd >= maxfd) maxfd= fd+1;
582 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
583 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
584 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
589 adns__consistency(ads,0,cc_entex);
592 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
593 const fd_set *writefds, const fd_set *exceptfds,
594 const struct timeval *now) {
595 struct timeval tv_buf;
596 struct pollfd pollfds[MAX_POLLFDS];
599 adns__consistency(ads,0,cc_entex);
600 adns__must_gettimeofday(ads,&now,&tv_buf);
602 adns_processtimeouts(ads,now);
604 npollfds= adns__pollfds(ads,pollfds);
605 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
608 maxfd,readfds,writefds,exceptfds,
611 adns__consistency(ads,0,cc_entex);
614 /* General helpful functions. */
616 void adns_globalsystemfailure(adns_state ads) {
617 adns__consistency(ads,0,cc_entex);
619 while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
620 while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
622 switch (ads->tcpstate) {
623 case server_connecting:
625 adns__tcp_broken(ads,0,0);
627 case server_disconnected:
633 adns__consistency(ads,0,cc_entex);
636 int adns_processany(adns_state ads) {
639 struct pollfd pollfds[MAX_POLLFDS];
642 adns__consistency(ads,0,cc_entex);
644 r= gettimeofday(&now,0);
645 if (!r) adns_processtimeouts(ads,&now);
647 /* We just use adns__fdevents to loop over the fd's trying them.
648 * This seems more sensible than calling select, since we're most
649 * likely just to want to do a read on one or two fds anyway.
651 npollfds= adns__pollfds(ads,pollfds);
652 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
658 adns__consistency(ads,0,cc_entex);
662 void adns__autosys(adns_state ads, struct timeval now) {
663 if (ads->iflags & adns_if_noautosys) return;
664 adns_processany(ads);
667 int adns__internal_check(adns_state ads,
668 adns_query *query_io,
669 adns_answer **answer,
675 if (ads->output.head) {
676 qu= ads->output.head;
677 } else if (ads->udpw.head || ads->tcpw.head) {
683 if (qu->id>=0) return EAGAIN;
685 LIST_UNLINK(ads->output,qu);
687 if (context_r) *context_r= qu->ctx.ext;
693 int adns_wait(adns_state ads,
694 adns_query *query_io,
695 adns_answer **answer_r,
698 fd_set readfds, writefds, exceptfds;
699 struct timeval tvbuf, *tvp;
701 adns__consistency(ads,*query_io,cc_entex);
703 r= adns__internal_check(ads,query_io,answer_r,context_r);
704 if (r != EAGAIN) break;
706 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
707 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
709 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
711 if (errno == EINTR) {
712 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
714 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
715 adns_globalsystemfailure(ads);
719 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
722 adns__consistency(ads,0,cc_entex);
726 int adns_check(adns_state ads,
727 adns_query *query_io,
728 adns_answer **answer_r,
733 adns__consistency(ads,*query_io,cc_entex);
734 r= gettimeofday(&now,0);
735 if (!r) adns__autosys(ads,now);
737 r= adns__internal_check(ads,query_io,answer_r,context_r);
738 adns__consistency(ads,0,cc_entex);