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
) {
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 addr
= &ads
->servers
[ads
->tcpserver
];
127 fd
= socket(addr
->addr
.sa
.sa_family
, SOCK_STREAM
, proto
->p_proto
);
129 adns__diag(ads
,-1,0,"cannot create TCP socket: %s",strerror(errno
));
132 r
= adns__setnonblock(ads
,fd
);
134 adns__diag(ads
,-1,0,"cannot make TCP socket nonblocking:"
139 r
= connect(fd
,&addr
->addr
.sa
,addr
->len
);
141 ads
->tcpstate
= server_connecting
;
142 if (r
==0) { tcp_connected(ads
,now
); return; }
143 if (errno
== EWOULDBLOCK
|| errno
== EINPROGRESS
) {
144 ads
->tcptimeout
= now
;
145 timevaladd(&ads
->tcptimeout
,TCPCONNMS
);
148 adns__tcp_broken(ads
,"connect",strerror(errno
));
149 tcp_broken_events(ads
);
153 /* Timeout handling functions. */
155 void adns__must_gettimeofday(adns_state ads
, const struct timeval
**now_io
,
156 struct timeval
*tv_buf
) {
157 const struct timeval
*now
;
162 r
= gettimeofday(tv_buf
,0); if (!r
) { *now_io
= tv_buf
; return; }
163 adns__diag(ads
,-1,0,"gettimeofday failed: %s",strerror(errno
));
164 adns_globalsystemfailure(ads
);
168 static void inter_immed(struct timeval
**tv_io
, struct timeval
*tvbuf
) {
169 struct timeval
*rbuf
;
174 if (!rbuf
) { *tv_io
= rbuf
= tvbuf
; }
179 static void inter_maxto(struct timeval
**tv_io
, struct timeval
*tvbuf
,
180 struct timeval maxto
) {
181 struct timeval
*rbuf
;
186 *tvbuf
= maxto
; *tv_io
= tvbuf
;
188 if (timercmp(rbuf
,&maxto
,>)) *rbuf
= maxto
;
190 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
191 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
194 static void inter_maxtoabs(struct timeval
**tv_io
, struct timeval
*tvbuf
,
195 struct timeval now
, struct timeval maxtime
) {
199 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
200 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
202 maxtime
.tv_sec
-= (now
.tv_sec
+2);
203 maxtime
.tv_usec
-= (now
.tv_usec
-2000000);
204 dr
= ldiv(maxtime
.tv_usec
,1000000);
205 maxtime
.tv_sec
+= dr
.quot
;
206 maxtime
.tv_usec
-= dr
.quot
*1000000;
207 if (maxtime
.tv_sec
<0) timerclear(&maxtime
);
208 inter_maxto(tv_io
,tvbuf
,maxtime
);
211 static void timeouts_queue(adns_state ads
, int act
,
212 struct timeval
**tv_io
, struct timeval
*tvbuf
,
213 struct timeval now
, struct query_queue
*queue
) {
216 for (qu
= queue
->head
; qu
; qu
= nqu
) {
218 if (!timercmp(&now
,&qu
->timeout
,>)) {
219 inter_maxtoabs(tv_io
,tvbuf
,now
,qu
->timeout
);
221 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
222 LIST_UNLINK(*queue
,qu
);
223 if (qu
->state
!= query_tosend
) {
224 adns__query_fail(qu
,adns_s_timeout
);
226 adns__query_send(qu
,now
);
233 static void tcp_events(adns_state ads
, int act
,
234 struct timeval
**tv_io
, struct timeval
*tvbuf
,
235 struct timeval now
) {
237 switch (ads
->tcpstate
) {
239 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
240 tcp_broken_events(ads
);
241 case server_disconnected
: /* fall through */
242 if (!ads
->tcpw
.head
) return;
243 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
244 adns__tcp_tryconnect(ads
,now
);
247 if (ads
->tcpw
.head
) return;
248 if (!ads
->tcptimeout
.tv_sec
) {
249 assert(!ads
->tcptimeout
.tv_usec
);
250 ads
->tcptimeout
= now
;
251 timevaladd(&ads
->tcptimeout
,TCPIDLEMS
);
253 case server_connecting
: /* fall through */
254 if (!act
|| !timercmp(&now
,&ads
->tcptimeout
,>)) {
255 inter_maxtoabs(tv_io
,tvbuf
,now
,ads
->tcptimeout
);
258 /* TCP timeout has happened */
259 switch (ads
->tcpstate
) {
260 case server_connecting
: /* failed to connect */
261 adns__tcp_broken(ads
,"unable to make connection","timed out");
263 case server_ok
: /* idle timeout */
265 ads
->tcpstate
= server_disconnected
;
279 void adns__timeouts(adns_state ads
, int act
,
280 struct timeval
**tv_io
, struct timeval
*tvbuf
,
281 struct timeval now
) {
282 timeouts_queue(ads
,act
,tv_io
,tvbuf
,now
, &ads
->udpw
);
283 timeouts_queue(ads
,act
,tv_io
,tvbuf
,now
, &ads
->tcpw
);
284 tcp_events(ads
,act
,tv_io
,tvbuf
,now
);
287 void adns_firsttimeout(adns_state ads
,
288 struct timeval
**tv_io
, struct timeval
*tvbuf
,
289 struct timeval now
) {
290 adns__consistency(ads
,0,cc_entex
);
291 adns__timeouts(ads
, 0, tv_io
,tvbuf
, now
);
292 adns__consistency(ads
,0,cc_entex
);
295 void adns_processtimeouts(adns_state ads
, const struct timeval
*now
) {
296 struct timeval tv_buf
;
298 adns__consistency(ads
,0,cc_entex
);
299 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
300 if (now
) adns__timeouts(ads
, 1, 0,0, *now
);
301 adns__consistency(ads
,0,cc_entex
);
304 /* fd handling functions. These are the top-level of the real work of
305 * reception and often transmission.
308 int adns__pollfds(adns_state ads
, struct pollfd pollfds_buf
[MAX_POLLFDS
]) {
309 /* Returns the number of entries filled in. Always zeroes revents. */
313 assert(MAX_POLLFDS
== MAXUDP
+ 1);
315 for (i
= 0; i
< ads
->nudp
; i
++) {
316 pollfds_buf
[i
].fd
= ads
->udpsocket
[i
].fd
;
317 pollfds_buf
[i
].events
= POLLIN
;
318 pollfds_buf
[i
].revents
= 0;
321 switch (ads
->tcpstate
) {
322 case server_disconnected
:
325 case server_connecting
:
326 pollfds_buf
[i
].events
= POLLOUT
;
329 pollfds_buf
[i
].events
=
330 ads
->tcpsend
.used ? POLLIN
|POLLOUT
|POLLPRI
: POLLIN
|POLLPRI
;
335 pollfds_buf
[i
++].fd
= ads
->tcpsocket
;
339 int adns_processreadable(adns_state ads
, int fd
, const struct timeval
*now
) {
340 int want
, dgramlen
, r
, i
, udpaddrlen
, serv
, old_skip
;
341 byte udpbuf
[DNS_MAXUDP
];
342 char addrbuf
[MAX_ADDRSTRLEN
];
343 struct udpsocket
*udp
;
344 adns_sockaddr udpaddr
;
346 adns__consistency(ads
,0,cc_entex
);
348 switch (ads
->tcpstate
) {
349 case server_disconnected
:
351 case server_connecting
:
354 if (fd
!= ads
->tcpsocket
) break;
355 assert(!ads
->tcprecv_skip
);
357 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2) {
358 dgramlen
= ((ads
->tcprecv
.buf
[ads
->tcprecv_skip
]<<8) |
359 ads
->tcprecv
.buf
[ads
->tcprecv_skip
+1]);
360 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2+dgramlen
) {
361 old_skip
= ads
->tcprecv_skip
;
362 ads
->tcprecv_skip
+= 2+dgramlen
;
363 adns__procdgram(ads
, ads
->tcprecv
.buf
+old_skip
+2,
364 dgramlen
, ads
->tcpserver
, 1,*now
);
372 ads
->tcprecv
.used
-= ads
->tcprecv_skip
;
373 memmove(ads
->tcprecv
.buf
, ads
->tcprecv
.buf
+ads
->tcprecv_skip
,
375 ads
->tcprecv_skip
= 0;
376 if (!adns__vbuf_ensure(&ads
->tcprecv
,want
)) { r
= ENOMEM
; goto xit
; }
377 assert(ads
->tcprecv
.used
<= ads
->tcprecv
.avail
);
378 if (ads
->tcprecv
.used
== ads
->tcprecv
.avail
) continue;
379 r
= read(ads
->tcpsocket
,
380 ads
->tcprecv
.buf
+ads
->tcprecv
.used
,
381 ads
->tcprecv
.avail
-ads
->tcprecv
.used
);
383 ads
->tcprecv
.used
+= r
;
386 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
387 if (errno
==EINTR
) continue;
388 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
390 adns__tcp_broken(ads
,"read",r?
strerror(errno
):"closed");
392 } while (ads
->tcpstate
== server_ok
);
397 for (i
= 0; i
< ads
->nudp
; i
++) {
398 udp
= &ads
->udpsocket
[i
];
401 udpaddrlen
= sizeof(udpaddr
);
402 r
= recvfrom(fd
,udpbuf
,sizeof(udpbuf
),0, &udpaddr
.sa
,&udpaddrlen
);
404 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) { r
= 0; goto xit
; }
405 if (errno
== EINTR
) continue;
406 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
407 adns__warn(ads
,-1,0,"datagram receive error: %s",strerror(errno
));
411 serv
< ads
->nservers
&&
412 !adns__sockaddr_equal_p(&udpaddr
.sa
,
413 &ads
->servers
[serv
].addr
.sa
);
415 if (serv
>= ads
->nservers
) {
416 adns__warn(ads
,-1,0,"datagram received from unknown nameserver %s",
417 adns__sockaddr_ntoa(&udpaddr
.sa
, udpaddrlen
, addrbuf
));
420 adns__procdgram(ads
,udpbuf
,r
,serv
,0,*now
);
427 adns__consistency(ads
,0,cc_entex
);
431 int adns_processwriteable(adns_state ads
, int fd
, const struct timeval
*now
) {
434 adns__consistency(ads
,0,cc_entex
);
436 switch (ads
->tcpstate
) {
437 case server_disconnected
:
440 case server_connecting
:
441 if (fd
!= ads
->tcpsocket
) break;
442 assert(ads
->tcprecv
.used
==0);
443 assert(ads
->tcprecv_skip
==0);
445 if (!adns__vbuf_ensure(&ads
->tcprecv
,1)) { r
= ENOMEM
; goto xit
; }
446 r
= read(ads
->tcpsocket
,&ads
->tcprecv
.buf
,1);
447 if (r
==0 || (r
<0 && (errno
==EAGAIN
|| errno
==EWOULDBLOCK
))) {
448 tcp_connected(ads
,*now
);
452 adns__tcp_broken(ads
,"connect/read","sent data before first request");
455 if (errno
==EINTR
) continue;
456 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
457 adns__tcp_broken(ads
,"connect/read",strerror(errno
));
461 if (fd
!= ads
->tcpsocket
) break;
462 while (ads
->tcpsend
.used
) {
463 adns__sigpipe_protect(ads
);
464 r
= write(ads
->tcpsocket
,ads
->tcpsend
.buf
,ads
->tcpsend
.used
);
465 adns__sigpipe_unprotect(ads
);
467 if (errno
==EINTR
) continue;
468 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
469 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
470 adns__tcp_broken(ads
,"write",strerror(errno
));
473 ads
->tcpsend
.used
-= r
;
474 memmove(ads
->tcpsend
.buf
,ads
->tcpsend
.buf
+r
,ads
->tcpsend
.used
);
484 adns__consistency(ads
,0,cc_entex
);
488 int adns_processexceptional(adns_state ads
, int fd
,
489 const struct timeval
*now
) {
490 adns__consistency(ads
,0,cc_entex
);
491 switch (ads
->tcpstate
) {
492 case server_disconnected
:
495 case server_connecting
:
497 if (fd
!= ads
->tcpsocket
) break;
498 adns__tcp_broken(ads
,"poll/select","exceptional condition detected");
503 adns__consistency(ads
,0,cc_entex
);
507 static void fd_event(adns_state ads
, int fd
,
508 int revent
, int pollflag
,
509 int maxfd
, const fd_set
*fds
,
510 int (*func
)(adns_state
, int fd
,
511 const struct timeval
*now
),
512 struct timeval now
, int *r_r
) {
515 if (!(revent
& pollflag
)) return;
516 if (fds
&& !(fd
<maxfd
&& FD_ISSET(fd
,fds
))) return;
517 r
= func(ads
,fd
,&now
);
522 adns__diag(ads
,-1,0,"process fd failed after select:"
523 " %s",strerror(errno
));
524 adns_globalsystemfailure(ads
);
529 void adns__fdevents(adns_state ads
,
530 const struct pollfd
*pollfds
, int npollfds
,
531 int maxfd
, const fd_set
*readfds
,
532 const fd_set
*writefds
, const fd_set
*exceptfds
,
533 struct timeval now
, int *r_r
) {
536 for (i
=0; i
<npollfds
; i
++) {
538 if (fd
>= maxfd
) maxfd
= fd
+1;
539 revents
= pollfds
[i
].revents
;
540 #define EV(pollfl,fds,how) \
541 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
542 EV( POLLIN
, readfds
, readable
);
543 EV( POLLOUT
, writefds
, writeable
);
544 EV( POLLPRI
, exceptfds
, exceptional
);
549 /* Wrappers for select(2). */
551 void adns_beforeselect(adns_state ads
, int *maxfd_io
, fd_set
*readfds_io
,
552 fd_set
*writefds_io
, fd_set
*exceptfds_io
,
553 struct timeval
**tv_mod
, struct timeval
*tv_tobuf
,
554 const struct timeval
*now
) {
555 struct timeval tv_nowbuf
;
556 struct pollfd pollfds
[MAX_POLLFDS
];
557 int i
, fd
, maxfd
, npollfds
;
559 adns__consistency(ads
,0,cc_entex
);
561 if (tv_mod
&& (!*tv_mod
|| (*tv_mod
)->tv_sec
|| (*tv_mod
)->tv_usec
)) {
562 /* The caller is planning to sleep. */
563 adns__must_gettimeofday(ads
,&now
,&tv_nowbuf
);
564 if (!now
) { inter_immed(tv_mod
,tv_tobuf
); goto xit
; }
565 adns__timeouts(ads
, 0, tv_mod
,tv_tobuf
, *now
);
568 npollfds
= adns__pollfds(ads
,pollfds
);
570 for (i
=0; i
<npollfds
; i
++) {
572 if (fd
>= maxfd
) maxfd
= fd
+1;
573 if (pollfds
[i
].events
& POLLIN
) FD_SET(fd
,readfds_io
);
574 if (pollfds
[i
].events
& POLLOUT
) FD_SET(fd
,writefds_io
);
575 if (pollfds
[i
].events
& POLLPRI
) FD_SET(fd
,exceptfds_io
);
580 adns__consistency(ads
,0,cc_entex
);
583 void adns_afterselect(adns_state ads
, int maxfd
, const fd_set
*readfds
,
584 const fd_set
*writefds
, const fd_set
*exceptfds
,
585 const struct timeval
*now
) {
586 struct timeval tv_buf
;
587 struct pollfd pollfds
[MAX_POLLFDS
];
590 adns__consistency(ads
,0,cc_entex
);
591 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
593 adns_processtimeouts(ads
,now
);
595 npollfds
= adns__pollfds(ads
,pollfds
);
596 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= POLLIN
|POLLOUT
|POLLPRI
;
599 maxfd
,readfds
,writefds
,exceptfds
,
602 adns__consistency(ads
,0,cc_entex
);
605 /* General helpful functions. */
607 void adns_globalsystemfailure(adns_state ads
) {
608 adns__consistency(ads
,0,cc_entex
);
610 while (ads
->udpw
.head
) adns__query_fail(ads
->udpw
.head
, adns_s_systemfail
);
611 while (ads
->tcpw
.head
) adns__query_fail(ads
->tcpw
.head
, adns_s_systemfail
);
613 switch (ads
->tcpstate
) {
614 case server_connecting
:
616 adns__tcp_broken(ads
,0,0);
618 case server_disconnected
:
624 adns__consistency(ads
,0,cc_entex
);
627 int adns_processany(adns_state ads
) {
630 struct pollfd pollfds
[MAX_POLLFDS
];
633 adns__consistency(ads
,0,cc_entex
);
635 r
= gettimeofday(&now
,0);
636 if (!r
) adns_processtimeouts(ads
,&now
);
638 /* We just use adns__fdevents to loop over the fd's trying them.
639 * This seems more sensible than calling select, since we're most
640 * likely just to want to do a read on one or two fds anyway.
642 npollfds
= adns__pollfds(ads
,pollfds
);
643 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= pollfds
[i
].events
& ~POLLPRI
;
649 adns__consistency(ads
,0,cc_entex
);
653 void adns__autosys(adns_state ads
, struct timeval now
) {
654 if (ads
->iflags
& adns_if_noautosys
) return;
655 adns_processany(ads
);
658 int adns__internal_check(adns_state ads
,
659 adns_query
*query_io
,
660 adns_answer
**answer
,
666 if (ads
->output
.head
) {
667 qu
= ads
->output
.head
;
668 } else if (ads
->udpw
.head
|| ads
->tcpw
.head
) {
674 if (qu
->id
>=0) return EAGAIN
;
676 LIST_UNLINK(ads
->output
,qu
);
678 if (context_r
) *context_r
= qu
->ctx
.ext
;
684 int adns_wait(adns_state ads
,
685 adns_query
*query_io
,
686 adns_answer
**answer_r
,
689 fd_set readfds
, writefds
, exceptfds
;
690 struct timeval tvbuf
, *tvp
;
692 adns__consistency(ads
,*query_io
,cc_entex
);
694 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
695 if (r
!= EAGAIN
) break;
697 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
698 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,&tvp
,&tvbuf
,0);
700 rsel
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tvp
);
702 if (errno
== EINTR
) {
703 if (ads
->iflags
& adns_if_eintr
) { r
= EINTR
; break; }
705 adns__diag(ads
,-1,0,"select failed in wait: %s",strerror(errno
));
706 adns_globalsystemfailure(ads
);
710 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,0);
713 adns__consistency(ads
,0,cc_entex
);
717 int adns_check(adns_state ads
,
718 adns_query
*query_io
,
719 adns_answer
**answer_r
,
724 adns__consistency(ads
,*query_io
,cc_entex
);
725 r
= gettimeofday(&now
,0);
726 if (!r
) adns__autosys(ads
,now
);
728 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
729 adns__consistency(ads
,0,cc_entex
);