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
->tcprecv_skip
= 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
);
100 assert(!ads
->tcprecv_skip
);
102 proto
= getprotobyname("tcp");
103 if (!proto
) { adns__diag(ads
,-1,0,"unable to find protocol no. for TCP !"); return; }
104 fd
= socket(AF_INET
,SOCK_STREAM
,proto
->p_proto
);
106 adns__diag(ads
,-1,0,"cannot create TCP socket: %s",strerror(errno
));
109 r
= adns__setnonblock(ads
,fd
);
111 adns__diag(ads
,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r
));
115 memset(&addr
,0,sizeof(addr
));
116 addr
.sin_family
= AF_INET
;
117 addr
.sin_port
= htons(DNS_PORT
);
118 addr
.sin_addr
= ads
->servers
[ads
->tcpserver
].addr
;
119 r
= connect(fd
,(const struct sockaddr
*)&addr
,sizeof(addr
));
121 ads
->tcpstate
= server_connecting
;
122 if (r
==0) { tcp_connected(ads
,now
); continue; }
123 if (errno
== EWOULDBLOCK
|| errno
== EINPROGRESS
) return;
124 adns__tcp_broken(ads
,"connect",strerror(errno
));
128 /* Timeout handling functions. */
130 void adns__must_gettimeofday(adns_state ads
, const struct timeval
**now_io
,
131 struct timeval
*tv_buf
) {
132 const struct timeval
*now
;
137 r
= gettimeofday(tv_buf
,0); if (!r
) { *now_io
= tv_buf
; return; }
138 adns__diag(ads
,-1,0,"gettimeofday failed: %s",strerror(errno
));
139 adns_globalsystemfailure(ads
);
143 static void inter_maxto(struct timeval
**tv_io
, struct timeval
*tvbuf
,
144 struct timeval maxto
) {
145 struct timeval
*rbuf
;
150 *tvbuf
= maxto
; *tv_io
= tvbuf
;
152 if (timercmp(rbuf
,&maxto
,>)) *rbuf
= maxto
;
154 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
155 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
158 static void inter_maxtoabs(struct timeval
**tv_io
, struct timeval
*tvbuf
,
159 struct timeval now
, struct timeval maxtime
) {
162 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
163 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
165 maxtime
.tv_sec
-= (now
.tv_sec
+2);
166 maxtime
.tv_usec
-= (now
.tv_usec
-2000000);
167 dr
= ldiv(maxtime
.tv_usec
,1000000);
168 maxtime
.tv_sec
+= dr
.quot
;
169 maxtime
.tv_usec
-= dr
.quot
*1000000;
170 if (maxtime
.tv_sec
<0) timerclear(&maxtime
);
171 inter_maxto(tv_io
,tvbuf
,maxtime
);
174 void adns__timeouts(adns_state ads
, int act
,
175 struct timeval
**tv_io
, struct timeval
*tvbuf
,
176 struct timeval now
) {
179 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
181 if (!timercmp(&now
,&qu
->timeout
,>)) {
182 if (!tv_io
) continue;
183 inter_maxtoabs(tv_io
,tvbuf
,now
,qu
->timeout
);
186 LIST_UNLINK(ads
->timew
,qu
);
187 if (qu
->state
!= query_tosend
) {
188 adns__query_fail(qu
,adns_s_timeout
);
190 adns__query_send(qu
,now
);
192 nqu
= ads
->timew
.head
;
197 void adns_firsttimeout(adns_state ads
,
198 struct timeval
**tv_io
, struct timeval
*tvbuf
,
199 struct timeval now
) {
200 adns__consistency(ads
,0,cc_entex
);
201 adns__timeouts(ads
, 0, tv_io
,tvbuf
, now
);
202 adns__consistency(ads
,0,cc_entex
);
205 void adns_processtimeouts(adns_state ads
, const struct timeval
*now
) {
206 struct timeval tv_buf
;
208 adns__consistency(ads
,0,cc_entex
);
209 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
210 if (now
) adns__timeouts(ads
, 1, 0,0, *now
);
211 adns__consistency(ads
,0,cc_entex
);
214 /* fd handling functions. These are the top-level of the real work of
215 * reception and often transmission.
218 int adns__pollfds(adns_state ads
, struct pollfd pollfds_buf
[MAX_POLLFDS
]) {
219 /* Returns the number of entries filled in. Always zeroes revents. */
221 assert(MAX_POLLFDS
==2);
223 pollfds_buf
[0].fd
= ads
->udpsocket
;
224 pollfds_buf
[0].events
= POLLIN
;
225 pollfds_buf
[0].revents
= 0;
227 switch (ads
->tcpstate
) {
228 case server_disconnected
:
230 case server_connecting
:
231 pollfds_buf
[1].events
= POLLOUT
;
234 pollfds_buf
[1].events
= ads
->tcpsend
.used ? POLLIN
|POLLOUT
|POLLPRI
: POLLIN
|POLLPRI
;
239 pollfds_buf
[1].fd
= ads
->tcpsocket
;
243 int adns_processreadable(adns_state ads
, int fd
, const struct timeval
*now
) {
244 int want
, dgramlen
, r
, udpaddrlen
, serv
, old_skip
;
245 byte udpbuf
[DNS_MAXUDP
];
246 struct sockaddr_in udpaddr
;
248 adns__consistency(ads
,0,cc_entex
);
250 switch (ads
->tcpstate
) {
251 case server_disconnected
:
252 case server_connecting
:
255 if (fd
!= ads
->tcpsocket
) break;
256 assert(!ads
->tcprecv_skip
);
258 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2) {
259 dgramlen
= ((ads
->tcprecv
.buf
[ads
->tcprecv_skip
]<<8) |
260 ads
->tcprecv
.buf
[ads
->tcprecv_skip
+1]);
261 if (ads
->tcprecv
.used
>= ads
->tcprecv_skip
+2+dgramlen
) {
262 old_skip
= ads
->tcprecv_skip
;
263 ads
->tcprecv_skip
+= 2+dgramlen
;
264 adns__procdgram(ads
, ads
->tcprecv
.buf
+old_skip
+2,
265 dgramlen
, ads
->tcpserver
, 1,*now
);
273 ads
->tcprecv
.used
-= ads
->tcprecv_skip
;
274 memmove(ads
->tcprecv
.buf
,ads
->tcprecv
.buf
+ads
->tcprecv_skip
,ads
->tcprecv
.used
);
275 ads
->tcprecv_skip
= 0;
276 if (!adns__vbuf_ensure(&ads
->tcprecv
,want
)) { r
= ENOMEM
; goto xit
; }
277 assert(ads
->tcprecv
.used
<= ads
->tcprecv
.avail
);
278 if (ads
->tcprecv
.used
== ads
->tcprecv
.avail
) continue;
279 r
= read(ads
->tcpsocket
,
280 ads
->tcprecv
.buf
+ads
->tcprecv
.used
,
281 ads
->tcprecv
.avail
-ads
->tcprecv
.used
);
283 ads
->tcprecv
.used
+= r
;
286 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
287 if (errno
==EINTR
) continue;
288 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
290 adns__tcp_broken(ads
,"read",r?
strerror(errno
):"closed");
293 } /* never reached */
297 if (fd
== ads
->udpsocket
) {
299 udpaddrlen
= sizeof(udpaddr
);
300 r
= recvfrom(ads
->udpsocket
,udpbuf
,sizeof(udpbuf
),0,
301 (struct sockaddr
*)&udpaddr
,&udpaddrlen
);
303 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) { r
= 0; goto xit
; }
304 if (errno
== EINTR
) continue;
305 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
306 adns__warn(ads
,-1,0,"datagram receive error: %s",strerror(errno
));
309 if (udpaddrlen
!= sizeof(udpaddr
)) {
310 adns__diag(ads
,-1,0,"datagram received with wrong address length %d"
311 " (expected %d)", udpaddrlen
,sizeof(udpaddr
));
314 if (udpaddr
.sin_family
!= AF_INET
) {
315 adns__diag(ads
,-1,0,"datagram received with wrong protocol family"
316 " %u (expected %u)",udpaddr
.sin_family
,AF_INET
);
319 if (ntohs(udpaddr
.sin_port
) != DNS_PORT
) {
320 adns__diag(ads
,-1,0,"datagram received from wrong port %u (expected %u)",
321 ntohs(udpaddr
.sin_port
),DNS_PORT
);
325 serv
< ads
->nservers
&&
326 ads
->servers
[serv
].addr
.s_addr
!= udpaddr
.sin_addr
.s_addr
;
328 if (serv
>= ads
->nservers
) {
329 adns__warn(ads
,-1,0,"datagram received from unknown nameserver %s",
330 inet_ntoa(udpaddr
.sin_addr
));
333 adns__procdgram(ads
,udpbuf
,r
,serv
,0,*now
);
338 adns__consistency(ads
,0,cc_entex
);
342 int adns_processwriteable(adns_state ads
, int fd
, const struct timeval
*now
) {
345 adns__consistency(ads
,0,cc_entex
);
347 switch (ads
->tcpstate
) {
348 case server_disconnected
:
350 case server_connecting
:
351 if (fd
!= ads
->tcpsocket
) break;
352 assert(ads
->tcprecv
.used
==0);
353 assert(ads
->tcprecv_skip
==0);
355 if (!adns__vbuf_ensure(&ads
->tcprecv
,1)) { r
= ENOMEM
; goto xit
; }
356 r
= read(ads
->tcpsocket
,&ads
->tcprecv
.buf
,1);
357 if (r
==0 || (r
<0 && (errno
==EAGAIN
|| errno
==EWOULDBLOCK
))) {
358 tcp_connected(ads
,*now
);
362 adns__tcp_broken(ads
,"connect/read","sent data before first request");
365 if (errno
==EINTR
) continue;
366 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
367 adns__tcp_broken(ads
,"connect/read",strerror(errno
));
371 if (!(ads
->tcpsend
.used
&& fd
== ads
->tcpsocket
)) break;
373 adns__sigpipe_protect(ads
);
374 r
= write(ads
->tcpsocket
,ads
->tcpsend
.buf
,ads
->tcpsend
.used
);
375 adns__sigpipe_unprotect(ads
);
377 if (errno
==EINTR
) continue;
378 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
) { r
= 0; goto xit
; }
379 if (errno_resources(errno
)) { r
= errno
; goto xit
; }
380 adns__tcp_broken(ads
,"write",strerror(errno
));
383 ads
->tcpsend
.used
-= r
;
384 memmove(ads
->tcpsend
.buf
,ads
->tcpsend
.buf
+r
,ads
->tcpsend
.used
);
392 adns__consistency(ads
,0,cc_entex
);
396 int adns_processexceptional(adns_state ads
, int fd
, const struct timeval
*now
) {
397 adns__consistency(ads
,0,cc_entex
);
398 switch (ads
->tcpstate
) {
399 case server_disconnected
:
401 case server_connecting
:
403 if (fd
!= ads
->tcpsocket
) break;
404 adns__tcp_broken(ads
,"poll/select","exceptional condition detected");
409 adns__consistency(ads
,0,cc_entex
);
413 static void fd_event(adns_state ads
, int fd
,
414 int revent
, int pollflag
,
415 int maxfd
, const fd_set
*fds
,
416 int (*func
)(adns_state
, int fd
, const struct timeval
*now
),
417 struct timeval now
, int *r_r
) {
420 if (!(revent
& pollflag
)) return;
421 if (fds
&& !(fd
<maxfd
&& FD_ISSET(fd
,fds
))) return;
422 r
= func(ads
,fd
,&now
);
427 adns__diag(ads
,-1,0,"process fd failed after select: %s",strerror(errno
));
428 adns_globalsystemfailure(ads
);
433 void adns__fdevents(adns_state ads
,
434 const struct pollfd
*pollfds
, int npollfds
,
435 int maxfd
, const fd_set
*readfds
,
436 const fd_set
*writefds
, const fd_set
*exceptfds
,
437 struct timeval now
, int *r_r
) {
440 for (i
=0; i
<npollfds
; i
++) {
442 if (fd
>= maxfd
) maxfd
= fd
+1;
443 revents
= pollfds
[i
].revents
;
444 fd_event(ads
,fd
, revents
,POLLIN
, maxfd
,readfds
, adns_processreadable
,now
,r_r
);
445 fd_event(ads
,fd
, revents
,POLLOUT
, maxfd
,writefds
, adns_processwriteable
,now
,r_r
);
446 fd_event(ads
,fd
, revents
,POLLPRI
, maxfd
,exceptfds
, adns_processexceptional
,now
,r_r
);
450 /* Wrappers for select(2). */
452 void adns_beforeselect(adns_state ads
, int *maxfd_io
, fd_set
*readfds_io
,
453 fd_set
*writefds_io
, fd_set
*exceptfds_io
,
454 struct timeval
**tv_mod
, struct timeval
*tv_tobuf
,
455 const struct timeval
*now
) {
456 struct timeval tv_nowbuf
;
457 struct pollfd pollfds
[MAX_POLLFDS
];
458 int i
, fd
, maxfd
, npollfds
;
460 adns__consistency(ads
,0,cc_entex
);
462 if (tv_mod
&& (!*tv_mod
|| (*tv_mod
)->tv_sec
|| (*tv_mod
)->tv_usec
)) {
463 /* The caller is planning to sleep. */
464 adns__must_gettimeofday(ads
,&now
,&tv_nowbuf
);
466 adns__timeouts(ads
, 1, tv_mod
,tv_tobuf
, *now
);
469 npollfds
= adns__pollfds(ads
,pollfds
);
471 for (i
=0; i
<npollfds
; i
++) {
473 if (fd
>= maxfd
) maxfd
= fd
+1;
474 if (pollfds
[i
].events
& POLLIN
) FD_SET(fd
,readfds_io
);
475 if (pollfds
[i
].events
& POLLOUT
) FD_SET(fd
,writefds_io
);
476 if (pollfds
[i
].events
& POLLPRI
) FD_SET(fd
,exceptfds_io
);
481 adns__consistency(ads
,0,cc_entex
);
484 void adns_afterselect(adns_state ads
, int maxfd
, const fd_set
*readfds
,
485 const fd_set
*writefds
, const fd_set
*exceptfds
,
486 const struct timeval
*now
) {
487 struct timeval tv_buf
;
488 struct pollfd pollfds
[MAX_POLLFDS
];
491 adns__consistency(ads
,0,cc_entex
);
492 adns__must_gettimeofday(ads
,&now
,&tv_buf
);
494 adns_processtimeouts(ads
,now
);
496 npollfds
= adns__pollfds(ads
,pollfds
);
497 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= POLLIN
|POLLOUT
|POLLPRI
;
500 maxfd
,readfds
,writefds
,exceptfds
,
503 adns__consistency(ads
,0,cc_entex
);
506 /* General helpful functions. */
508 void adns_globalsystemfailure(adns_state ads
) {
509 adns__consistency(ads
,0,cc_entex
);
511 while (ads
->timew
.head
) {
512 adns__query_fail(ads
->timew
.head
, adns_s_systemfail
);
515 switch (ads
->tcpstate
) {
516 case server_connecting
:
518 adns__tcp_closenext(ads
);
520 case server_disconnected
:
525 adns__consistency(ads
,0,cc_entex
);
528 int adns_processany(adns_state ads
) {
531 struct pollfd pollfds
[MAX_POLLFDS
];
534 adns__consistency(ads
,0,cc_entex
);
536 r
= gettimeofday(&now
,0);
537 if (!r
) adns_processtimeouts(ads
,&now
);
539 /* We just use adns__fdevents to loop over the fd's trying them.
540 * This seems more sensible than calling select, since we're most
541 * likely just to want to do a read on one or two fds anyway.
543 npollfds
= adns__pollfds(ads
,pollfds
);
544 for (i
=0; i
<npollfds
; i
++) pollfds
[i
].revents
= pollfds
[i
].events
;
550 adns__consistency(ads
,0,cc_entex
);
554 void adns__autosys(adns_state ads
, struct timeval now
) {
555 if (ads
->iflags
& adns_if_noautosys
) return;
556 adns_processany(ads
);
559 int adns__internal_check(adns_state ads
,
560 adns_query
*query_io
,
561 adns_answer
**answer
,
567 if (!ads
->output
.head
) return EWOULDBLOCK
;
568 qu
= ads
->output
.head
;
570 if (qu
->id
>=0) return EWOULDBLOCK
;
572 LIST_UNLINK(ads
->output
,qu
);
574 if (context_r
) *context_r
= qu
->ctx
.ext
;
580 int adns_wait(adns_state ads
,
581 adns_query
*query_io
,
582 adns_answer
**answer_r
,
585 fd_set readfds
, writefds
, exceptfds
;
586 struct timeval tvbuf
, *tvp
;
588 adns__consistency(ads
,*query_io
,cc_entex
);
590 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
591 if (r
!= EWOULDBLOCK
) break;
593 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
594 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,&tvp
,&tvbuf
,0);
595 rsel
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tvp
);
597 if (errno
== EINTR
) {
598 if (ads
->iflags
& adns_if_eintr
) { r
= EINTR
; break; }
600 adns__diag(ads
,-1,0,"select failed in wait: %s",strerror(errno
));
601 adns_globalsystemfailure(ads
);
605 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,0);
608 adns__consistency(ads
,0,cc_entex
);
612 int adns_check(adns_state ads
,
613 adns_query
*query_io
,
614 adns_answer
**answer_r
,
619 adns__consistency(ads
,*query_io
,cc_entex
);
620 r
= gettimeofday(&now
,0);
621 if (!r
) adns__autosys(ads
,now
);
623 r
= adns__internal_check(ads
,query_io
,answer_r
,context_r
);
624 adns__consistency(ads
,0,cc_entex
);