4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
9 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
11 * It is part of adns, which is
12 * Copyright (C) 1997-1999 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>
43 /* TCP connection management. */
45 void adns__tcp_closenext(adns_state ads
) {
49 close(ads
->tcpsocket
);
50 ads
->tcpstate
= server_disconnected
;
51 ads
->tcprecv
.used
= ads
->tcpsend
.used
= 0;
52 ads
->tcpserver
= (serv
+1)%ads
->nservers
;
55 void adns__tcp_broken(adns_state ads
, const char *what
, const char *why
) {
59 assert(ads
->tcpstate
== server_connecting
|| ads
->tcpstate
== server_ok
);
61 adns__warn(ads
,serv
,0,"TCP connection lost: %s: %s",what
,why
);
62 adns__tcp_closenext(ads
);
64 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
66 if (qu
->state
== query_tosend
) continue;
67 assert(qu
->state
== query_tcpwait
|| qu
->state
== query_tcpsent
);
68 qu
->state
= query_tcpwait
;
69 qu
->tcpfailed
|= (1<<serv
);
70 if (qu
->tcpfailed
== (1<<ads
->nservers
)-1) {
71 LIST_UNLINK(ads
->timew
,qu
);
72 adns__query_fail(qu
,adns_s_allservfail
);
77 static void tcp_connected(adns_state ads
, struct timeval now
) {
80 adns__debug(ads
,ads
->tcpserver
,0,"TCP connected");
81 ads
->tcpstate
= server_ok
;
82 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
84 if (qu
->state
== query_tosend
) continue;
85 assert (qu
->state
== query_tcpwait
);
86 adns__query_tcp(qu
,now
);
90 void adns__tcp_tryconnect(adns_state ads
, struct timeval now
) {
92 struct sockaddr_in addr
;
93 struct protoent
*proto
;
95 for (tries
=0; tries
<ads
->nservers
; tries
++) {
96 if (ads
->tcpstate
== server_connecting
|| ads
->tcpstate
== server_ok
) return;
97 assert(ads
->tcpstate
== server_disconnected
);
98 assert(!ads
->tcpsend
.used
);
99 assert(!ads
->tcprecv
.used
);
101 proto
= getprotobyname("tcp");
102 if (!proto
) { adns__diag(ads
,-1,0,"unable to find protocol no. for TCP !"); return; }
103 fd
= socket(AF_INET
,SOCK_STREAM
,proto
->p_proto
);
105 adns__diag(ads
,-1,0,"cannot create TCP socket: %s",strerror(errno
));
108 r
= adns__setnonblock(ads
,fd
);
110 adns__diag(ads
,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r
));
114 memset(&addr
,0,sizeof(addr
));
115 addr
.sin_family
= AF_INET
;
116 addr
.sin_port
= htons(DNS_PORT
);
117 addr
.sin_addr
= ads
->servers
[ads
->tcpserver
].addr
;
118 r
= connect(fd
,(const struct sockaddr
*)&addr
,sizeof(addr
));
120 ads
->tcpstate
= server_connecting
;
121 if (r
==0) { tcp_connected(ads
,now
); continue; }
122 if (errno
== EWOULDBLOCK
|| errno
== EINPROGRESS
) return;
123 adns__tcp_broken(ads
,"connect",strerror(errno
));
127 /* Timeout handling functions. */
129 void adns__must_gettimeofday(adns_state ads
, const struct timeval
**now_io
,
130 struct timeval
*tv_buf
) {
131 const struct timeval
*now
;
136 r
= gettimeofday(tv_buf
,0); if (!r
) { *now_io
= tv_buf
; return; }
137 adns__diag(ads
,-1,0,"gettimeofday failed: %s",strerror(errno
));
138 adns_globalsystemfailure(ads
);
142 static void inter_maxto(struct timeval
**tv_io
, struct timeval
*tvbuf
,
143 struct timeval maxto
) {
144 struct timeval
*rbuf
;
149 *tvbuf
= maxto
; *tv_io
= tvbuf
;
151 if (timercmp(rbuf
,&maxto
,>)) *rbuf
= maxto
;
153 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
154 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
157 static void inter_maxtoabs(struct timeval
**tv_io
, struct timeval
*tvbuf
,
158 struct timeval now
, struct timeval maxtime
) {
161 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
162 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
164 maxtime
.tv_sec
-= (now
.tv_sec
+2);
165 maxtime
.tv_usec
-= (now
.tv_usec
-2000000);
166 dr
= ldiv(maxtime
.tv_usec
,1000000);
167 maxtime
.tv_sec
+= dr
.quot
;
168 maxtime
.tv_usec
-= dr
.quot
*1000000;
169 if (maxtime
.tv_sec
<0) timerclear(&maxtime
);
170 inter_maxto(tv_io
,tvbuf
,maxtime
);
173 void adns__timeouts(adns_state ads
, int act
,
174 struct timeval
**tv_io
, struct timeval
*tvbuf
,
175 struct timeval now
) {
178 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
180 if (!timercmp(&now
,&qu
->timeout
,>)) {
181 if (!tv_io
) continue;
182 inter_maxtoabs(tv_io
,tvbuf
,now
,qu
->timeout
);
185 LIST_UNLINK(ads
->timew
,qu
);
186 if (qu
->state
!= query_tosend
) {
187 adns__query_fail(qu
,adns_s_timeout
);
189 adns__query_send(qu
,now
);
191 nqu
= ads
->timew
.head
;
196 void adns_firsttimeout(adns_state ads
,
197 struct timeval
**tv_io
, struct timeval
*tvbuf
,
198 struct timeval now
) {
199 adns__consistency(ads
,0,cc_entex
);
200 adns__timeouts(ads
, 0, tv_io
,tvbuf
, now
);
201 adns__consistency(ads
,0,cc_entex
);
204 void adns_processtimeouts(adns_state ads
, const struct timeval
*now
) {
205 struct timeval tv_buf
;
207 adns__consistency(ads
,0,cc_entex
);
208 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
209 if (now
) adns__timeouts(ads
, 1, 0,0, *now
);
210 adns__consistency(ads
,0,cc_entex
);
213 /* fd handling functions. These are the top-level of the real work of
214 * reception and often transmission.
217 int adns__pollfds(adns_state ads
, struct pollfd pollfds_buf
[MAX_POLLFDS
]) {
218 /* Returns the number of entries filled in. Always zeroes revents. */
220 assert(MAX_POLLFDS
==2);
222 pollfds_buf
[0].fd
= ads
->udpsocket
;
223 pollfds_buf
[0].events
= POLLIN
;
224 pollfds_buf
[0].revents
= 0;
226 switch (ads
->tcpstate
) {
227 case server_disconnected
:
229 case server_connecting
:
230 pollfds_buf
[1].events
= POLLOUT
;
233 pollfds_buf
[1].events
= ads
->tcpsend
.used ? POLLIN
|POLLOUT
|POLLPRI
: POLLIN
|POLLPRI
;
238 pollfds_buf
[1].fd
= ads
->tcpsocket
;
242 int adns_processreadable(adns_state ads
, int fd
, const struct timeval
*now
) {
243 int skip
, want
, dgramlen
, r
, udpaddrlen
, serv
;
244 byte udpbuf
[DNS_MAXUDP
];
245 struct sockaddr_in udpaddr
;
247 adns__consistency(ads
,0,cc_entex
);
249 switch (ads
->tcpstate
) {
250 case server_disconnected
:
251 case server_connecting
:
254 if (fd
!= ads
->tcpsocket
) break;
257 if (ads
->tcprecv
.used
<skip
+2) {
260 dgramlen
= (ads
->tcprecv
.buf
[skip
]<<8) | ads
->tcprecv
.buf
[skip
+1];
261 if (ads
->tcprecv
.used
<skip
+2+dgramlen
) {
264 adns__procdgram(ads
,ads
->tcprecv
.buf
+skip
+2,dgramlen
,ads
->tcpserver
,1,*now
);
265 skip
+= 2+dgramlen
; continue;
268 ads
->tcprecv
.used
-= skip
;
269 memmove(ads
->tcprecv
.buf
,ads
->tcprecv
.buf
+skip
,ads
->tcprecv
.used
);
271 if (!adns__vbuf_ensure(&ads
->tcprecv
,want
)) { r
= ENOMEM
; goto xit
; }
272 assert(ads
->tcprecv
.used
<= ads
->tcprecv
.avail
);
273 if (ads
->tcprecv
.used
== ads
->tcprecv
.avail
) continue;
274 r
= read(ads
->tcpsocket
,
275 ads
->tcprecv
.buf
+ads
->tcprecv
.used
,
276 ads
->tcprecv
.avail
-ads
->tcprecv
.used
);
278 ads
->tcprecv
.used
+= r
;
281 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
282 if (errno
==EINTR
) continue;
283 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
285 adns__tcp_broken(ads
,"read",r?
strerror(errno
):"closed");
288 } /* never reached */
292 if (fd
== ads
->udpsocket
) {
294 udpaddrlen
= sizeof(udpaddr
);
295 r
= recvfrom(ads
->udpsocket
,udpbuf
,sizeof(udpbuf
),0,
296 (struct sockaddr
*)&udpaddr
,&udpaddrlen
);
298 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) { r
= 0; goto xit
; }
299 if (errno
== EINTR
) continue;
300 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
301 adns__warn(ads
,-1,0,"datagram receive error: %s",strerror(errno
));
304 if (udpaddrlen
!= sizeof(udpaddr
)) {
305 adns__diag(ads
,-1,0,"datagram received with wrong address length %d"
306 " (expected %d)", udpaddrlen
,sizeof(udpaddr
));
309 if (udpaddr
.sin_family
!= AF_INET
) {
310 adns__diag(ads
,-1,0,"datagram received with wrong protocol family"
311 " %u (expected %u)",udpaddr
.sin_family
,AF_INET
);
314 if (ntohs(udpaddr
.sin_port
) != DNS_PORT
) {
315 adns__diag(ads
,-1,0,"datagram received from wrong port %u (expected %u)",
316 ntohs(udpaddr
.sin_port
),DNS_PORT
);
320 serv
< ads
->nservers
&&
321 ads
->servers
[serv
].addr
.s_addr
!= udpaddr
.sin_addr
.s_addr
;
323 if (serv
>= ads
->nservers
) {
324 adns__warn(ads
,-1,0,"datagram received from unknown nameserver %s",
325 inet_ntoa(udpaddr
.sin_addr
));
328 adns__procdgram(ads
,udpbuf
,r
,serv
,0,*now
);
333 adns__consistency(ads
,0,cc_entex
);
337 int adns_processwriteable(adns_state ads
, int fd
, const struct timeval
*now
) {
340 adns__consistency(ads
,0,cc_entex
);
342 switch (ads
->tcpstate
) {
343 case server_disconnected
:
345 case server_connecting
:
346 if (fd
!= ads
->tcpsocket
) break;
347 assert(ads
->tcprecv
.used
==0);
349 if (!adns__vbuf_ensure(&ads
->tcprecv
,1)) { r
= ENOMEM
; goto xit
; }
350 r
= read(ads
->tcpsocket
,&ads
->tcprecv
.buf
,1);
351 if (r
==0 || (r
<0 && (errno
==EAGAIN
|| errno
==EWOULDBLOCK
))) {
352 tcp_connected(ads
,*now
);
356 adns__tcp_broken(ads
,"connect/read","sent data before first request");
359 if (errno
==EINTR
) continue;
360 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
361 adns__tcp_broken(ads
,"connect/read",strerror(errno
));
365 if (!(ads
->tcpsend
.used
&& fd
== ads
->tcpsocket
)) break;
367 adns__sigpipe_protect(ads
);
368 r
= write(ads
->tcpsocket
,ads
->tcpsend
.buf
,ads
->tcpsend
.used
);
369 adns__sigpipe_unprotect(ads
);
371 if (errno
==EINTR
) continue;
372 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
373 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
374 adns__tcp_broken(ads
,"write",strerror(errno
));
377 ads
->tcpsend
.used
-= r
;
378 memmove(ads
->tcpsend
.buf
,ads
->tcpsend
.buf
+r
,ads
->tcpsend
.used
);
386 adns__consistency(ads
,0,cc_entex
);
390 int adns_processexceptional(adns_state ads
, int fd
, const struct timeval
*now
) {
391 adns__consistency(ads
,0,cc_entex
);
392 switch (ads
->tcpstate
) {
393 case server_disconnected
:
395 case server_connecting
:
397 if (fd
!= ads
->tcpsocket
) break;
398 adns__tcp_broken(ads
,"poll/select","exceptional condition detected");
403 adns__consistency(ads
,0,cc_entex
);
407 static void fd_event(adns_state ads
, int fd
,
408 int revent
, int pollflag
,
409 int maxfd
, const fd_set
*fds
,
410 int (*func
)(adns_state
, int fd
, const struct timeval
*now
),
411 struct timeval now
, int *r_r
) {
414 if (!(revent
& pollflag
)) return;
415 if (fds
&& !(fd
<maxfd
&& FD_ISSET(fd
,fds
))) return;
416 r
= func(ads
,fd
,&now
);
421 adns__diag(ads
,-1,0,"process fd failed after select: %s",strerror(errno
));
422 adns_globalsystemfailure(ads
);
427 void adns__fdevents(adns_state ads
,
428 const struct pollfd
*pollfds
, int npollfds
,
429 int maxfd
, const fd_set
*readfds
,
430 const fd_set
*writefds
, const fd_set
*exceptfds
,
431 struct timeval now
, int *r_r
) {
434 for (i
=0; i
<npollfds
; i
++) {
436 if (fd
>= maxfd
) maxfd
= fd
+1;
437 revents
= pollfds
[i
].revents
;
438 fd_event(ads
,fd
, revents
,POLLIN
, maxfd
,readfds
, adns_processreadable
,now
,r_r
);
439 fd_event(ads
,fd
, revents
,POLLOUT
, maxfd
,writefds
, adns_processwriteable
,now
,r_r
);
440 fd_event(ads
,fd
, revents
,POLLPRI
, maxfd
,exceptfds
, adns_processexceptional
,now
,r_r
);
444 /* Wrappers for select(2). */
446 void adns_beforeselect(adns_state ads
, int *maxfd_io
, fd_set
*readfds_io
,
447 fd_set
*writefds_io
, fd_set
*exceptfds_io
,
448 struct timeval
**tv_mod
, struct timeval
*tv_tobuf
,
449 const struct timeval
*now
) {
450 struct timeval tv_nowbuf
;
451 struct pollfd pollfds
[MAX_POLLFDS
];
452 int i
, fd
, maxfd
, npollfds
;
454 adns__consistency(ads
,0,cc_entex
);
456 if (tv_mod
&& (!*tv_mod
|| (*tv_mod
)->tv_sec
|| (*tv_mod
)->tv_usec
)) {
457 /* The caller is planning to sleep. */
458 adns__must_gettimeofday(ads
,&now
,&tv_nowbuf
);
460 adns__timeouts(ads
, 1, tv_mod
,tv_tobuf
, *now
);
463 npollfds
= adns__pollfds(ads
,pollfds
);
465 for (i
=0; i
<npollfds
; i
++) {
467 if (fd
>= maxfd
) maxfd
= fd
+1;
468 if (pollfds
[i
].events
& POLLIN
) FD_SET(fd
,readfds_io
);
469 if (pollfds
[i
].events
& POLLOUT
) FD_SET(fd
,writefds_io
);
470 if (pollfds
[i
].events
& POLLPRI
) FD_SET(fd
,exceptfds_io
);
475 adns__consistency(ads
,0,cc_entex
);
478 void adns_afterselect(adns_state ads
, int maxfd
, const fd_set
*readfds
,
479 const fd_set
*writefds
, const fd_set
*exceptfds
,
480 const struct timeval
*now
) {
481 struct timeval tv_buf
;
482 struct pollfd pollfds
[MAX_POLLFDS
];
485 adns__consistency(ads
,0,cc_entex
);
486 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
488 adns_processtimeouts(ads
,now
);
490 npollfds
= adns__pollfds(ads
,pollfds
);
491 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= POLLIN
|POLLOUT
|POLLPRI
;
494 maxfd
,readfds
,writefds
,exceptfds
,
497 adns__consistency(ads
,0,cc_entex
);
500 /* General helpful functions. */
502 void adns_globalsystemfailure(adns_state ads
) {
503 adns__consistency(ads
,0,cc_entex
);
505 while (ads
->timew
.head
) {
506 adns__query_fail(ads
->timew
.head
, adns_s_systemfail
);
509 switch (ads
->tcpstate
) {
510 case server_connecting
:
512 adns__tcp_closenext(ads
);
514 case server_disconnected
:
519 adns__consistency(ads
,0,cc_entex
);
522 int adns_processany(adns_state ads
) {
525 struct pollfd pollfds
[MAX_POLLFDS
];
528 adns__consistency(ads
,0,cc_entex
);
530 r
= gettimeofday(&now
,0);
531 if (!r
) adns_processtimeouts(ads
,&now
);
533 npollfds
= adns__pollfds(ads
,pollfds
);
539 adns__consistency(ads
,0,cc_entex
);
543 void adns__autosys(adns_state ads
, struct timeval now
) {
544 if (ads
->iflags
& adns_if_noautosys
) return;
545 adns_processany(ads
);
548 int adns__internal_check(adns_state ads
,
549 adns_query
*query_io
,
550 adns_answer
**answer
,
556 if (!ads
->output
.head
) return EWOULDBLOCK
;
557 qu
= ads
->output
.head
;
559 if (qu
->id
>=0) return EWOULDBLOCK
;
561 LIST_UNLINK(ads
->output
,qu
);
563 if (context_r
) *context_r
= qu
->ctx
.ext
;
569 int adns_wait(adns_state ads
,
570 adns_query
*query_io
,
571 adns_answer
**answer_r
,
574 fd_set readfds
, writefds
, exceptfds
;
575 struct timeval tvbuf
, *tvp
;
577 adns__consistency(ads
,*query_io
,cc_entex
);
579 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
580 if (r
!= EWOULDBLOCK
) break;
582 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
583 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,&tvp
,&tvbuf
,0);
584 rsel
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tvp
);
586 if (errno
== EINTR
) {
587 if (ads
->iflags
& adns_if_eintr
) { r
= EINTR
; break; }
589 adns__diag(ads
,-1,0,"select failed in wait: %s",strerror(errno
));
590 adns_globalsystemfailure(ads
);
594 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,0);
597 adns__consistency(ads
,0,cc_entex
);
601 int adns_check(adns_state ads
,
602 adns_query
*query_io
,
603 adns_answer
**answer_r
,
608 adns__consistency(ads
,*query_io
,cc_entex
);
609 r
= gettimeofday(&now
,0);
610 if (!r
) adns__autosys(ads
,now
);
612 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
613 adns__consistency(ads
,0,cc_entex
);