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
) {
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
) {
213 for (qu
= queue
->head
; qu
; qu
= nqu
) {
215 if (!timercmp(&now
,&qu
->timeout
,>)) {
216 inter_maxtoabs(tv_io
,tvbuf
,now
,qu
->timeout
);
218 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
219 LIST_UNLINK(*queue
,qu
);
220 if (qu
->state
!= query_tosend
) {
221 adns__query_fail(qu
,adns_s_timeout
);
223 adns__query_send(qu
,now
);
230 static void tcp_events(adns_state ads
, int act
,
231 struct timeval
**tv_io
, struct timeval
*tvbuf
,
232 struct timeval now
) {
234 switch (ads
->tcpstate
) {
236 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
237 tcp_broken_events(ads
);
238 case server_disconnected
: /* fall through */
239 if (!ads
->tcpw
.head
) return;
240 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
241 adns__tcp_tryconnect(ads
,now
);
244 if (ads
->tcpw
.head
) return;
245 if (!ads
->tcptimeout
.tv_sec
) {
246 assert(!ads
->tcptimeout
.tv_usec
);
247 ads
->tcptimeout
= now
;
248 timevaladd(&ads
->tcptimeout
,TCPIDLEMS
);
250 case server_connecting
: /* fall through */
251 if (!act
|| !timercmp(&now
,&ads
->tcptimeout
,>)) {
252 inter_maxtoabs(tv_io
,tvbuf
,now
,ads
->tcptimeout
);
255 /* TCP timeout has happened */
256 switch (ads
->tcpstate
) {
257 case server_connecting
: /* failed to connect */
258 adns__tcp_broken(ads
,"unable to make connection","timed out");
260 case server_ok
: /* idle timeout */
262 ads
->tcpstate
= server_disconnected
;
276 void adns__timeouts(adns_state ads
, int act
,
277 struct timeval
**tv_io
, struct timeval
*tvbuf
,
278 struct timeval now
) {
279 timeouts_queue(ads
,act
,tv_io
,tvbuf
,now
, &ads
->udpw
);
280 timeouts_queue(ads
,act
,tv_io
,tvbuf
,now
, &ads
->tcpw
);
281 tcp_events(ads
,act
,tv_io
,tvbuf
,now
);
284 void adns_firsttimeout(adns_state ads
,
285 struct timeval
**tv_io
, struct timeval
*tvbuf
,
286 struct timeval now
) {
287 adns__consistency(ads
,0,cc_entex
);
288 adns__timeouts(ads
, 0, tv_io
,tvbuf
, now
);
289 adns__consistency(ads
,0,cc_entex
);
292 void adns_processtimeouts(adns_state ads
, const struct timeval
*now
) {
293 struct timeval tv_buf
;
295 adns__consistency(ads
,0,cc_entex
);
296 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
297 if (now
) adns__timeouts(ads
, 1, 0,0, *now
);
298 adns__consistency(ads
,0,cc_entex
);
301 /* fd handling functions. These are the top-level of the real work of
302 * reception and often transmission.
305 int adns__pollfds(adns_state ads
, struct pollfd pollfds_buf
[MAX_POLLFDS
]) {
306 /* Returns the number of entries filled in. Always zeroes revents. */
308 assert(MAX_POLLFDS
==2);
310 pollfds_buf
[0].fd
= ads
->udpsocket
;
311 pollfds_buf
[0].events
= POLLIN
;
312 pollfds_buf
[0].revents
= 0;
314 switch (ads
->tcpstate
) {
315 case server_disconnected
:
318 case server_connecting
:
319 pollfds_buf
[1].events
= POLLOUT
;
322 pollfds_buf
[1].events
=
323 ads
->tcpsend
.used ? POLLIN
|POLLOUT
|POLLPRI
: POLLIN
|POLLPRI
;
328 pollfds_buf
[1].fd
= ads
->tcpsocket
;
332 int adns_processreadable(adns_state ads
, int fd
, const struct timeval
*now
) {
333 int want
, dgramlen
, r
, udpaddrlen
, serv
, old_skip
;
334 byte udpbuf
[DNS_MAXUDP
];
335 struct sockaddr_in udpaddr
;
337 adns__consistency(ads
,0,cc_entex
);
339 switch (ads
->tcpstate
) {
340 case server_disconnected
:
342 case server_connecting
:
345 if (fd
!= ads
->tcpsocket
) break;
346 assert(!ads
->tcprecv_skip
);
348 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2) {
349 dgramlen
= ((ads
->tcprecv
.buf
[ads
->tcprecv_skip
]<<8) |
350 ads
->tcprecv
.buf
[ads
->tcprecv_skip
+1]);
351 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2+dgramlen
) {
352 old_skip
= ads
->tcprecv_skip
;
353 ads
->tcprecv_skip
+= 2+dgramlen
;
354 adns__procdgram(ads
, ads
->tcprecv
.buf
+old_skip
+2,
355 dgramlen
, ads
->tcpserver
, 1,*now
);
363 ads
->tcprecv
.used
-= ads
->tcprecv_skip
;
364 memmove(ads
->tcprecv
.buf
, ads
->tcprecv
.buf
+ads
->tcprecv_skip
,
366 ads
->tcprecv_skip
= 0;
367 if (!adns__vbuf_ensure(&ads
->tcprecv
,want
)) { r
= ENOMEM
; goto xit
; }
368 assert(ads
->tcprecv
.used
<= ads
->tcprecv
.avail
);
369 if (ads
->tcprecv
.used
== ads
->tcprecv
.avail
) continue;
370 r
= read(ads
->tcpsocket
,
371 ads
->tcprecv
.buf
+ads
->tcprecv
.used
,
372 ads
->tcprecv
.avail
-ads
->tcprecv
.used
);
374 ads
->tcprecv
.used
+= r
;
377 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
378 if (errno
==EINTR
) continue;
379 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
381 adns__tcp_broken(ads
,"read",r?
strerror(errno
):"closed");
383 } while (ads
->tcpstate
== server_ok
);
388 if (fd
== ads
->udpsocket
) {
390 udpaddrlen
= sizeof(udpaddr
);
391 r
= recvfrom(ads
->udpsocket
,udpbuf
,sizeof(udpbuf
),0,
392 (struct sockaddr
*)&udpaddr
,&udpaddrlen
);
394 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) { r
= 0; goto xit
; }
395 if (errno
== EINTR
) continue;
396 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
397 adns__warn(ads
,-1,0,"datagram receive error: %s",strerror(errno
));
400 if (udpaddrlen
!= sizeof(udpaddr
)) {
401 adns__diag(ads
,-1,0,"datagram received with wrong address length %d"
402 " (expected %lu)", udpaddrlen
,
403 (unsigned long)sizeof(udpaddr
));
406 if (udpaddr
.sin_family
!= AF_INET
) {
407 adns__diag(ads
,-1,0,"datagram received with wrong protocol family"
408 " %u (expected %u)",udpaddr
.sin_family
,AF_INET
);
411 if (ntohs(udpaddr
.sin_port
) != DNS_PORT
) {
412 adns__diag(ads
,-1,0,"datagram received from wrong port"
413 " %u (expected %u)", ntohs(udpaddr
.sin_port
),DNS_PORT
);
417 serv
< ads
->nservers
&&
418 (assert(ads
->servers
[serv
].addr
.sa
.sa_family
==AF_INET
),
419 ads
->servers
[serv
].addr
.inet
.sin_addr
.s_addr
!=
420 udpaddr
.sin_addr
.s_addr
);
422 if (serv
>= ads
->nservers
) {
423 adns__warn(ads
,-1,0,"datagram received from unknown nameserver %s",
424 inet_ntoa(udpaddr
.sin_addr
));
427 adns__procdgram(ads
,udpbuf
,r
,serv
,0,*now
);
432 adns__consistency(ads
,0,cc_entex
);
436 int adns_processwriteable(adns_state ads
, int fd
, const struct timeval
*now
) {
439 adns__consistency(ads
,0,cc_entex
);
441 switch (ads
->tcpstate
) {
442 case server_disconnected
:
445 case server_connecting
:
446 if (fd
!= ads
->tcpsocket
) break;
447 assert(ads
->tcprecv
.used
==0);
448 assert(ads
->tcprecv_skip
==0);
450 if (!adns__vbuf_ensure(&ads
->tcprecv
,1)) { r
= ENOMEM
; goto xit
; }
451 r
= read(ads
->tcpsocket
,&ads
->tcprecv
.buf
,1);
452 if (r
==0 || (r
<0 && (errno
==EAGAIN
|| errno
==EWOULDBLOCK
))) {
453 tcp_connected(ads
,*now
);
457 adns__tcp_broken(ads
,"connect/read","sent data before first request");
460 if (errno
==EINTR
) continue;
461 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
462 adns__tcp_broken(ads
,"connect/read",strerror(errno
));
466 if (fd
!= ads
->tcpsocket
) break;
467 while (ads
->tcpsend
.used
) {
468 adns__sigpipe_protect(ads
);
469 r
= write(ads
->tcpsocket
,ads
->tcpsend
.buf
,ads
->tcpsend
.used
);
470 adns__sigpipe_unprotect(ads
);
472 if (errno
==EINTR
) continue;
473 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
474 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
475 adns__tcp_broken(ads
,"write",strerror(errno
));
478 ads
->tcpsend
.used
-= r
;
479 memmove(ads
->tcpsend
.buf
,ads
->tcpsend
.buf
+r
,ads
->tcpsend
.used
);
489 adns__consistency(ads
,0,cc_entex
);
493 int adns_processexceptional(adns_state ads
, int fd
,
494 const struct timeval
*now
) {
495 adns__consistency(ads
,0,cc_entex
);
496 switch (ads
->tcpstate
) {
497 case server_disconnected
:
500 case server_connecting
:
502 if (fd
!= ads
->tcpsocket
) break;
503 adns__tcp_broken(ads
,"poll/select","exceptional condition detected");
508 adns__consistency(ads
,0,cc_entex
);
512 static void fd_event(adns_state ads
, int fd
,
513 int revent
, int pollflag
,
514 int maxfd
, const fd_set
*fds
,
515 int (*func
)(adns_state
, int fd
,
516 const struct timeval
*now
),
517 struct timeval now
, int *r_r
) {
520 if (!(revent
& pollflag
)) return;
521 if (fds
&& !(fd
<maxfd
&& FD_ISSET(fd
,fds
))) return;
522 r
= func(ads
,fd
,&now
);
527 adns__diag(ads
,-1,0,"process fd failed after select:"
528 " %s",strerror(errno
));
529 adns_globalsystemfailure(ads
);
534 void adns__fdevents(adns_state ads
,
535 const struct pollfd
*pollfds
, int npollfds
,
536 int maxfd
, const fd_set
*readfds
,
537 const fd_set
*writefds
, const fd_set
*exceptfds
,
538 struct timeval now
, int *r_r
) {
541 for (i
=0; i
<npollfds
; i
++) {
543 if (fd
>= maxfd
) maxfd
= fd
+1;
544 revents
= pollfds
[i
].revents
;
545 #define EV(pollfl,fds,how) \
546 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
547 EV( POLLIN
, readfds
, readable
);
548 EV( POLLOUT
, writefds
, writeable
);
549 EV( POLLPRI
, exceptfds
, exceptional
);
554 /* Wrappers for select(2). */
556 void adns_beforeselect(adns_state ads
, int *maxfd_io
, fd_set
*readfds_io
,
557 fd_set
*writefds_io
, fd_set
*exceptfds_io
,
558 struct timeval
**tv_mod
, struct timeval
*tv_tobuf
,
559 const struct timeval
*now
) {
560 struct timeval tv_nowbuf
;
561 struct pollfd pollfds
[MAX_POLLFDS
];
562 int i
, fd
, maxfd
, npollfds
;
564 adns__consistency(ads
,0,cc_entex
);
566 if (tv_mod
&& (!*tv_mod
|| (*tv_mod
)->tv_sec
|| (*tv_mod
)->tv_usec
)) {
567 /* The caller is planning to sleep. */
568 adns__must_gettimeofday(ads
,&now
,&tv_nowbuf
);
569 if (!now
) { inter_immed(tv_mod
,tv_tobuf
); goto xit
; }
570 adns__timeouts(ads
, 0, tv_mod
,tv_tobuf
, *now
);
573 npollfds
= adns__pollfds(ads
,pollfds
);
575 for (i
=0; i
<npollfds
; i
++) {
577 if (fd
>= maxfd
) maxfd
= fd
+1;
578 if (pollfds
[i
].events
& POLLIN
) FD_SET(fd
,readfds_io
);
579 if (pollfds
[i
].events
& POLLOUT
) FD_SET(fd
,writefds_io
);
580 if (pollfds
[i
].events
& POLLPRI
) FD_SET(fd
,exceptfds_io
);
585 adns__consistency(ads
,0,cc_entex
);
588 void adns_afterselect(adns_state ads
, int maxfd
, const fd_set
*readfds
,
589 const fd_set
*writefds
, const fd_set
*exceptfds
,
590 const struct timeval
*now
) {
591 struct timeval tv_buf
;
592 struct pollfd pollfds
[MAX_POLLFDS
];
595 adns__consistency(ads
,0,cc_entex
);
596 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
598 adns_processtimeouts(ads
,now
);
600 npollfds
= adns__pollfds(ads
,pollfds
);
601 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= POLLIN
|POLLOUT
|POLLPRI
;
604 maxfd
,readfds
,writefds
,exceptfds
,
607 adns__consistency(ads
,0,cc_entex
);
610 /* General helpful functions. */
612 void adns_globalsystemfailure(adns_state ads
) {
613 adns__consistency(ads
,0,cc_entex
);
615 while (ads
->udpw
.head
) adns__query_fail(ads
->udpw
.head
, adns_s_systemfail
);
616 while (ads
->tcpw
.head
) adns__query_fail(ads
->tcpw
.head
, adns_s_systemfail
);
618 switch (ads
->tcpstate
) {
619 case server_connecting
:
621 adns__tcp_broken(ads
,0,0);
623 case server_disconnected
:
629 adns__consistency(ads
,0,cc_entex
);
632 int adns_processany(adns_state ads
) {
635 struct pollfd pollfds
[MAX_POLLFDS
];
638 adns__consistency(ads
,0,cc_entex
);
640 r
= gettimeofday(&now
,0);
641 if (!r
) adns_processtimeouts(ads
,&now
);
643 /* We just use adns__fdevents to loop over the fd's trying them.
644 * This seems more sensible than calling select, since we're most
645 * likely just to want to do a read on one or two fds anyway.
647 npollfds
= adns__pollfds(ads
,pollfds
);
648 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= pollfds
[i
].events
& ~POLLPRI
;
654 adns__consistency(ads
,0,cc_entex
);
658 void adns__autosys(adns_state ads
, struct timeval now
) {
659 if (ads
->iflags
& adns_if_noautosys
) return;
660 adns_processany(ads
);
663 int adns__internal_check(adns_state ads
,
664 adns_query
*query_io
,
665 adns_answer
**answer
,
671 if (ads
->output
.head
) {
672 qu
= ads
->output
.head
;
673 } else if (ads
->udpw
.head
|| ads
->tcpw
.head
) {
679 if (qu
->id
>=0) return EAGAIN
;
681 LIST_UNLINK(ads
->output
,qu
);
683 if (context_r
) *context_r
= qu
->ctx
.ext
;
689 int adns_wait(adns_state ads
,
690 adns_query
*query_io
,
691 adns_answer
**answer_r
,
694 fd_set readfds
, writefds
, exceptfds
;
695 struct timeval tvbuf
, *tvp
;
697 adns__consistency(ads
,*query_io
,cc_entex
);
699 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
700 if (r
!= EAGAIN
) break;
702 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
703 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,&tvp
,&tvbuf
,0);
705 rsel
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tvp
);
707 if (errno
== EINTR
) {
708 if (ads
->iflags
& adns_if_eintr
) { r
= EINTR
; break; }
710 adns__diag(ads
,-1,0,"select failed in wait: %s",strerror(errno
));
711 adns_globalsystemfailure(ads
);
715 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,0);
718 adns__consistency(ads
,0,cc_entex
);
722 int adns_check(adns_state ads
,
723 adns_query
*query_io
,
724 adns_answer
**answer_r
,
729 adns__consistency(ads
,*query_io
,cc_entex
);
730 r
= gettimeofday(&now
,0);
731 if (!r
) adns__autosys(ads
,now
);
733 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
734 adns__consistency(ads
,0,cc_entex
);