4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
9 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
11 * It is part of adns, which is
12 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
13 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
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 2, 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,
27 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
44 /* TCP connection management. */
46 static void tcp_close(adns_state ads
) {
50 close(ads
->tcpsocket
);
52 ads
->tcprecv
.used
= ads
->tcprecv_skip
= ads
->tcpsend
.used
= 0;
55 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
);
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 void adns__tcp_tryconnect(adns_state ads
, struct timeval now
) {
81 struct sockaddr_in addr
;
82 struct protoent
*proto
;
84 for (tries
=0; tries
<ads
->nservers
; tries
++) {
85 switch (ads
->tcpstate
) {
86 case server_connecting
:
90 case server_disconnected
:
96 assert(!ads
->tcpsend
.used
);
97 assert(!ads
->tcprecv
.used
);
98 assert(!ads
->tcprecv_skip
);
100 proto
= getprotobyname("tcp");
101 if (!proto
) { adns__diag(ads
,-1,0,"unable to find protocol no. for TCP !"); return; }
102 fd
= socket(AF_INET
,SOCK_STREAM
,proto
->p_proto
);
104 adns__diag(ads
,-1,0,"cannot create TCP socket: %s",strerror(errno
));
107 r
= adns__setnonblock(ads
,fd
);
109 adns__diag(ads
,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r
));
113 memset(&addr
,0,sizeof(addr
));
114 addr
.sin_family
= AF_INET
;
115 addr
.sin_port
= htons(DNS_PORT
);
116 addr
.sin_addr
= ads
->servers
[ads
->tcpserver
].addr
;
117 r
= connect(fd
,(const struct sockaddr
*)&addr
,sizeof(addr
));
119 ads
->tcpstate
= server_connecting
;
120 if (r
==0) { tcp_connected(ads
,now
); return; }
121 if (errno
== EWOULDBLOCK
|| errno
== EINPROGRESS
) {
122 ads
->tcptimeout
= now
;
123 timevaladd(&ads
->tcptimeout
,TCPCONNMS
);
126 adns__tcp_broken(ads
,"connect",strerror(errno
));
127 ads
->tcpstate
= server_disconnected
;
131 /* Timeout handling functions. */
133 void adns__must_gettimeofday(adns_state ads
, const struct timeval
**now_io
,
134 struct timeval
*tv_buf
) {
135 const struct timeval
*now
;
140 r
= gettimeofday(tv_buf
,0); if (!r
) { *now_io
= tv_buf
; return; }
141 adns__diag(ads
,-1,0,"gettimeofday failed: %s",strerror(errno
));
142 adns_globalsystemfailure(ads
);
146 static void inter_immed(struct timeval
**tv_io
, struct timeval
*tvbuf
) {
147 struct timeval
*rbuf
;
152 if (!rbuf
) { *tv_io
= rbuf
= tvbuf
; }
157 static void inter_maxto(struct timeval
**tv_io
, struct timeval
*tvbuf
,
158 struct timeval maxto
) {
159 struct timeval
*rbuf
;
164 *tvbuf
= maxto
; *tv_io
= tvbuf
;
166 if (timercmp(rbuf
,&maxto
,>)) *rbuf
= maxto
;
168 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
169 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
172 static void inter_maxtoabs(struct timeval
**tv_io
, struct timeval
*tvbuf
,
173 struct timeval now
, struct timeval maxtime
) {
177 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
178 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
180 maxtime
.tv_sec
-= (now
.tv_sec
+2);
181 maxtime
.tv_usec
-= (now
.tv_usec
-2000000);
182 dr
= ldiv(maxtime
.tv_usec
,1000000);
183 maxtime
.tv_sec
+= dr
.quot
;
184 maxtime
.tv_usec
-= dr
.quot
*1000000;
185 if (maxtime
.tv_sec
<0) timerclear(&maxtime
);
186 inter_maxto(tv_io
,tvbuf
,maxtime
);
189 static void timeouts_queue(adns_state ads
, int act
,
190 struct timeval
**tv_io
, struct timeval
*tvbuf
,
191 struct timeval now
, struct query_queue
*queue
) {
194 for (qu
= queue
->head
; qu
; qu
= nqu
) {
196 if (!timercmp(&now
,&qu
->timeout
,>)) {
197 inter_maxtoabs(tv_io
,tvbuf
,now
,qu
->timeout
);
199 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
200 LIST_UNLINK(*queue
,qu
);
201 if (qu
->state
!= query_tosend
) {
202 adns__query_fail(qu
,adns_s_timeout
);
204 adns__query_send(qu
,now
);
211 static void tcp_events(adns_state ads
, int act
,
212 struct timeval
**tv_io
, struct timeval
*tvbuf
,
213 struct timeval now
) {
217 switch (ads
->tcpstate
) {
219 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
220 for (qu
= ads
->tcpw
.head
; qu
; qu
= nqu
) {
222 assert(qu
->state
== query_tcpw
);
223 if (qu
->retries
> ads
->nservers
) {
224 LIST_UNLINK(ads
->tcpw
,qu
);
225 adns__query_fail(qu
,adns_s_allservfail
);
228 ads
->tcpstate
= server_disconnected
;
229 case server_disconnected
: /* fall through */
230 if (!ads
->tcpw
.head
) return;
231 if (!act
) { inter_immed(tv_io
,tvbuf
); return; }
232 adns__tcp_tryconnect(ads
,now
);
235 if (ads
->tcpw
.head
) return;
236 if (!ads
->tcptimeout
.tv_sec
) {
237 assert(!ads
->tcptimeout
.tv_usec
);
238 ads
->tcptimeout
= now
;
239 timevaladd(&ads
->tcptimeout
,TCPIDLEMS
);
241 case server_connecting
: /* fall through */
242 if (!act
|| !timercmp(&now
,&ads
->tcptimeout
,>)) {
243 inter_maxtoabs(tv_io
,tvbuf
,now
,ads
->tcptimeout
);
246 /* TCP timeout has happened */
247 switch (ads
->tcpstate
) {
248 case server_connecting
: /* failed to connect */
249 adns__tcp_broken(ads
,"unable to make connection","timed out");
251 case server_ok
: /* idle timeout */
253 ads
->tcpstate
= server_disconnected
;
267 void adns__timeouts(adns_state ads
, int act
,
268 struct timeval
**tv_io
, struct timeval
*tvbuf
,
269 struct timeval now
) {
270 timeouts_queue(ads
,act
,tv_io
,tvbuf
,now
, &ads
->udpw
);
271 timeouts_queue(ads
,act
,tv_io
,tvbuf
,now
, &ads
->tcpw
);
272 tcp_events(ads
,act
,tv_io
,tvbuf
,now
);
275 void adns_firsttimeout(adns_state ads
,
276 struct timeval
**tv_io
, struct timeval
*tvbuf
,
277 struct timeval now
) {
278 adns__consistency(ads
,0,cc_entex
);
279 adns__timeouts(ads
, 0, tv_io
,tvbuf
, now
);
280 adns__consistency(ads
,0,cc_entex
);
283 void adns_processtimeouts(adns_state ads
, const struct timeval
*now
) {
284 struct timeval tv_buf
;
286 adns__consistency(ads
,0,cc_entex
);
287 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
288 if (now
) adns__timeouts(ads
, 1, 0,0, *now
);
289 adns__consistency(ads
,0,cc_entex
);
292 /* fd handling functions. These are the top-level of the real work of
293 * reception and often transmission.
296 int adns__pollfds(adns_state ads
, struct pollfd pollfds_buf
[MAX_POLLFDS
]) {
297 /* Returns the number of entries filled in. Always zeroes revents. */
299 assert(MAX_POLLFDS
==2);
301 pollfds_buf
[0].fd
= ads
->udpsocket
;
302 pollfds_buf
[0].events
= POLLIN
;
303 pollfds_buf
[0].revents
= 0;
305 switch (ads
->tcpstate
) {
306 case server_disconnected
:
309 case server_connecting
:
310 pollfds_buf
[1].events
= POLLOUT
;
313 pollfds_buf
[1].events
= ads
->tcpsend
.used ? POLLIN
|POLLOUT
|POLLPRI
: POLLIN
|POLLPRI
;
318 pollfds_buf
[1].fd
= ads
->tcpsocket
;
322 int adns_processreadable(adns_state ads
, int fd
, const struct timeval
*now
) {
323 int want
, dgramlen
, r
, udpaddrlen
, serv
, old_skip
;
324 byte udpbuf
[DNS_MAXUDP
];
325 struct sockaddr_in udpaddr
;
327 adns__consistency(ads
,0,cc_entex
);
329 switch (ads
->tcpstate
) {
330 case server_disconnected
:
331 case server_connecting
:
334 if (fd
!= ads
->tcpsocket
) break;
335 assert(!ads
->tcprecv_skip
);
337 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2) {
338 dgramlen
= ((ads
->tcprecv
.buf
[ads
->tcprecv_skip
]<<8) |
339 ads
->tcprecv
.buf
[ads
->tcprecv_skip
+1]);
340 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2+dgramlen
) {
341 old_skip
= ads
->tcprecv_skip
;
342 ads
->tcprecv_skip
+= 2+dgramlen
;
343 adns__procdgram(ads
, ads
->tcprecv
.buf
+old_skip
+2,
344 dgramlen
, ads
->tcpserver
, 1,*now
);
352 ads
->tcprecv
.used
-= ads
->tcprecv_skip
;
353 memmove(ads
->tcprecv
.buf
,ads
->tcprecv
.buf
+ads
->tcprecv_skip
,ads
->tcprecv
.used
);
354 ads
->tcprecv_skip
= 0;
355 if (!adns__vbuf_ensure(&ads
->tcprecv
,want
)) { r
= ENOMEM
; goto xit
; }
356 assert(ads
->tcprecv
.used
<= ads
->tcprecv
.avail
);
357 if (ads
->tcprecv
.used
== ads
->tcprecv
.avail
) continue;
358 r
= read(ads
->tcpsocket
,
359 ads
->tcprecv
.buf
+ads
->tcprecv
.used
,
360 ads
->tcprecv
.avail
-ads
->tcprecv
.used
);
362 ads
->tcprecv
.used
+= r
;
365 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
366 if (errno
==EINTR
) continue;
367 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
369 adns__tcp_broken(ads
,"read",r?
strerror(errno
):"closed");
371 } while (ads
->tcpstate
== server_ok
);
376 if (fd
== ads
->udpsocket
) {
378 udpaddrlen
= sizeof(udpaddr
);
379 r
= recvfrom(ads
->udpsocket
,udpbuf
,sizeof(udpbuf
),0,
380 (struct sockaddr
*)&udpaddr
,&udpaddrlen
);
382 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) { r
= 0; goto xit
; }
383 if (errno
== EINTR
) continue;
384 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
385 adns__warn(ads
,-1,0,"datagram receive error: %s",strerror(errno
));
388 if (udpaddrlen
!= sizeof(udpaddr
)) {
389 adns__diag(ads
,-1,0,"datagram received with wrong address length %d"
390 " (expected %d)", udpaddrlen
,sizeof(udpaddr
));
393 if (udpaddr
.sin_family
!= AF_INET
) {
394 adns__diag(ads
,-1,0,"datagram received with wrong protocol family"
395 " %u (expected %u)",udpaddr
.sin_family
,AF_INET
);
398 if (ntohs(udpaddr
.sin_port
) != DNS_PORT
) {
399 adns__diag(ads
,-1,0,"datagram received from wrong port %u (expected %u)",
400 ntohs(udpaddr
.sin_port
),DNS_PORT
);
404 serv
< ads
->nservers
&&
405 ads
->servers
[serv
].addr
.s_addr
!= udpaddr
.sin_addr
.s_addr
;
407 if (serv
>= ads
->nservers
) {
408 adns__warn(ads
,-1,0,"datagram received from unknown nameserver %s",
409 inet_ntoa(udpaddr
.sin_addr
));
412 adns__procdgram(ads
,udpbuf
,r
,serv
,0,*now
);
417 adns__consistency(ads
,0,cc_entex
);
421 int adns_processwriteable(adns_state ads
, int fd
, const struct timeval
*now
) {
424 adns__consistency(ads
,0,cc_entex
);
426 switch (ads
->tcpstate
) {
427 case server_disconnected
:
429 case server_connecting
:
430 if (fd
!= ads
->tcpsocket
) break;
431 assert(ads
->tcprecv
.used
==0);
432 assert(ads
->tcprecv_skip
==0);
434 if (!adns__vbuf_ensure(&ads
->tcprecv
,1)) { r
= ENOMEM
; goto xit
; }
435 r
= read(ads
->tcpsocket
,&ads
->tcprecv
.buf
,1);
436 if (r
==0 || (r
<0 && (errno
==EAGAIN
|| errno
==EWOULDBLOCK
))) {
437 tcp_connected(ads
,*now
);
441 adns__tcp_broken(ads
,"connect/read","sent data before first request");
444 if (errno
==EINTR
) continue;
445 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
446 adns__tcp_broken(ads
,"connect/read",strerror(errno
));
450 if (!(ads
->tcpsend
.used
&& fd
== ads
->tcpsocket
)) break;
452 adns__sigpipe_protect(ads
);
453 r
= write(ads
->tcpsocket
,ads
->tcpsend
.buf
,ads
->tcpsend
.used
);
454 adns__sigpipe_unprotect(ads
);
456 if (errno
==EINTR
) continue;
457 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
458 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
459 adns__tcp_broken(ads
,"write",strerror(errno
));
462 ads
->tcpsend
.used
-= r
;
463 memmove(ads
->tcpsend
.buf
,ads
->tcpsend
.buf
+r
,ads
->tcpsend
.used
);
471 adns__consistency(ads
,0,cc_entex
);
475 int adns_processexceptional(adns_state ads
, int fd
, const struct timeval
*now
) {
476 adns__consistency(ads
,0,cc_entex
);
477 switch (ads
->tcpstate
) {
478 case server_disconnected
:
480 case server_connecting
:
482 if (fd
!= ads
->tcpsocket
) break;
483 adns__tcp_broken(ads
,"poll/select","exceptional condition detected");
488 adns__consistency(ads
,0,cc_entex
);
492 static void fd_event(adns_state ads
, int fd
,
493 int revent
, int pollflag
,
494 int maxfd
, const fd_set
*fds
,
495 int (*func
)(adns_state
, int fd
, const struct timeval
*now
),
496 struct timeval now
, int *r_r
) {
499 if (!(revent
& pollflag
)) return;
500 if (fds
&& !(fd
<maxfd
&& FD_ISSET(fd
,fds
))) return;
501 r
= func(ads
,fd
,&now
);
506 adns__diag(ads
,-1,0,"process fd failed after select: %s",strerror(errno
));
507 adns_globalsystemfailure(ads
);
512 void adns__fdevents(adns_state ads
,
513 const struct pollfd
*pollfds
, int npollfds
,
514 int maxfd
, const fd_set
*readfds
,
515 const fd_set
*writefds
, const fd_set
*exceptfds
,
516 struct timeval now
, int *r_r
) {
519 for (i
=0; i
<npollfds
; i
++) {
521 if (fd
>= maxfd
) maxfd
= fd
+1;
522 revents
= pollfds
[i
].revents
;
523 fd_event(ads
,fd
, revents
,POLLIN
, maxfd
,readfds
, adns_processreadable
,now
,r_r
);
524 fd_event(ads
,fd
, revents
,POLLOUT
, maxfd
,writefds
, adns_processwriteable
,now
,r_r
);
525 fd_event(ads
,fd
, revents
,POLLPRI
, maxfd
,exceptfds
, adns_processexceptional
,now
,r_r
);
529 /* Wrappers for select(2). */
531 void adns_beforeselect(adns_state ads
, int *maxfd_io
, fd_set
*readfds_io
,
532 fd_set
*writefds_io
, fd_set
*exceptfds_io
,
533 struct timeval
**tv_mod
, struct timeval
*tv_tobuf
,
534 const struct timeval
*now
) {
535 struct timeval tv_nowbuf
;
536 struct pollfd pollfds
[MAX_POLLFDS
];
537 int i
, fd
, maxfd
, npollfds
;
539 adns__consistency(ads
,0,cc_entex
);
541 if (tv_mod
&& (!*tv_mod
|| (*tv_mod
)->tv_sec
|| (*tv_mod
)->tv_usec
)) {
542 /* The caller is planning to sleep. */
543 adns__must_gettimeofday(ads
,&now
,&tv_nowbuf
);
544 if (!now
) { inter_immed(tv_mod
,tv_tobuf
); goto xit
; }
545 adns__timeouts(ads
, 0, tv_mod
,tv_tobuf
, *now
);
548 npollfds
= adns__pollfds(ads
,pollfds
);
550 for (i
=0; i
<npollfds
; i
++) {
552 if (fd
>= maxfd
) maxfd
= fd
+1;
553 if (pollfds
[i
].events
& POLLIN
) FD_SET(fd
,readfds_io
);
554 if (pollfds
[i
].events
& POLLOUT
) FD_SET(fd
,writefds_io
);
555 if (pollfds
[i
].events
& POLLPRI
) FD_SET(fd
,exceptfds_io
);
560 adns__consistency(ads
,0,cc_entex
);
563 void adns_afterselect(adns_state ads
, int maxfd
, const fd_set
*readfds
,
564 const fd_set
*writefds
, const fd_set
*exceptfds
,
565 const struct timeval
*now
) {
566 struct timeval tv_buf
;
567 struct pollfd pollfds
[MAX_POLLFDS
];
570 adns__consistency(ads
,0,cc_entex
);
571 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
573 adns_processtimeouts(ads
,now
);
575 npollfds
= adns__pollfds(ads
,pollfds
);
576 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= POLLIN
|POLLOUT
|POLLPRI
;
579 maxfd
,readfds
,writefds
,exceptfds
,
582 adns__consistency(ads
,0,cc_entex
);
585 /* General helpful functions. */
587 void adns_globalsystemfailure(adns_state ads
) {
588 adns__consistency(ads
,0,cc_entex
);
590 while (ads
->udpw
.head
) adns__query_fail(ads
->udpw
.head
, adns_s_systemfail
);
591 while (ads
->tcpw
.head
) adns__query_fail(ads
->tcpw
.head
, adns_s_systemfail
);
593 switch (ads
->tcpstate
) {
594 case server_connecting
:
596 adns__tcp_broken(ads
,0,0);
598 case server_disconnected
:
603 adns__consistency(ads
,0,cc_entex
);
606 int adns_processany(adns_state ads
) {
609 struct pollfd pollfds
[MAX_POLLFDS
];
612 adns__consistency(ads
,0,cc_entex
);
614 r
= gettimeofday(&now
,0);
615 if (!r
) adns_processtimeouts(ads
,&now
);
617 /* We just use adns__fdevents to loop over the fd's trying them.
618 * This seems more sensible than calling select, since we're most
619 * likely just to want to do a read on one or two fds anyway.
621 npollfds
= adns__pollfds(ads
,pollfds
);
622 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= pollfds
[i
].events
;
628 adns__consistency(ads
,0,cc_entex
);
632 void adns__autosys(adns_state ads
, struct timeval now
) {
633 if (ads
->iflags
& adns_if_noautosys
) return;
634 adns_processany(ads
);
637 int adns__internal_check(adns_state ads
,
638 adns_query
*query_io
,
639 adns_answer
**answer
,
645 if (ads
->output
.head
) {
646 qu
= ads
->output
.head
;
647 } else if (ads
->udpw
.head
|| ads
->tcpw
.head
) {
653 if (qu
->id
>=0) return EAGAIN
;
655 LIST_UNLINK(ads
->output
,qu
);
657 if (context_r
) *context_r
= qu
->ctx
.ext
;
663 int adns_wait(adns_state ads
,
664 adns_query
*query_io
,
665 adns_answer
**answer_r
,
668 fd_set readfds
, writefds
, exceptfds
;
669 struct timeval tvbuf
, *tvp
;
671 adns__consistency(ads
,*query_io
,cc_entex
);
673 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
674 if (r
!= EAGAIN
) break;
676 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
677 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,&tvp
,&tvbuf
,0);
679 rsel
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tvp
);
681 if (errno
== EINTR
) {
682 if (ads
->iflags
& adns_if_eintr
) { r
= EINTR
; break; }
684 adns__diag(ads
,-1,0,"select failed in wait: %s",strerror(errno
));
685 adns_globalsystemfailure(ads
);
689 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,0);
692 adns__consistency(ads
,0,cc_entex
);
696 int adns_check(adns_state ads
,
697 adns_query
*query_io
,
698 adns_answer
**answer_r
,
703 adns__consistency(ads
,*query_io
,cc_entex
);
704 r
= gettimeofday(&now
,0);
705 if (!r
) adns__autosys(ads
,now
);
707 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
708 adns__consistency(ads
,0,cc_entex
);