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);
51 ads->tcpstate= server_disconnected;
52 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
53 ads->tcpserver= (serv+1)%ads->nservers;
56 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
60 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
62 adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
63 adns__tcp_closenext(ads);
65 for (qu= ads->timew.head; qu; qu= nqu) {
67 if (qu->state == query_tosend) continue;
68 assert(qu->state == query_tcpwait || qu->state == query_tcpsent);
69 qu->state= query_tcpwait;
70 qu->tcpfailed |= (1<<serv);
71 if (qu->tcpfailed == (1<<ads->nservers)-1) {
72 LIST_UNLINK(ads->timew,qu);
73 adns__query_fail(qu,adns_s_allservfail);
78 static void tcp_connected(adns_state ads, struct timeval now) {
81 adns__debug(ads,ads->tcpserver,0,"TCP connected");
82 ads->tcpstate= server_ok;
83 for (qu= ads->timew.head; qu; qu= nqu) {
85 if (qu->state == query_tosend) continue;
86 assert (qu->state == query_tcpwait);
87 adns__query_tcp(qu,now);
91 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
93 struct sockaddr_in addr;
94 struct protoent *proto;
96 for (tries=0; tries<ads->nservers; tries++) {
97 if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return;
98 assert(ads->tcpstate == server_disconnected);
99 assert(!ads->tcpsend.used);
100 assert(!ads->tcprecv.used);
101 assert(!ads->tcprecv_skip);
103 proto= getprotobyname("tcp");
104 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
105 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
107 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
110 r= adns__setnonblock(ads,fd);
112 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
116 memset(&addr,0,sizeof(addr));
117 addr.sin_family= AF_INET;
118 addr.sin_port= htons(DNS_PORT);
119 addr.sin_addr= ads->servers[ads->tcpserver].addr;
120 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
122 ads->tcpstate= server_connecting;
123 if (r==0) { tcp_connected(ads,now); continue; }
124 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
125 adns__tcp_broken(ads,"connect",strerror(errno));
129 /* Timeout handling functions. */
131 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
132 struct timeval *tv_buf) {
133 const struct timeval *now;
138 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
139 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
140 adns_globalsystemfailure(ads);
144 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
145 struct timeval maxto) {
146 struct timeval *rbuf;
151 *tvbuf= maxto; *tv_io= tvbuf;
153 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
155 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
156 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
159 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
160 struct timeval now, struct timeval maxtime) {
163 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
164 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
166 maxtime.tv_sec -= (now.tv_sec+2);
167 maxtime.tv_usec -= (now.tv_usec-2000000);
168 dr= ldiv(maxtime.tv_usec,1000000);
169 maxtime.tv_sec += dr.quot;
170 maxtime.tv_usec -= dr.quot*1000000;
171 if (maxtime.tv_sec<0) timerclear(&maxtime);
172 inter_maxto(tv_io,tvbuf,maxtime);
175 void adns__timeouts(adns_state ads, int act,
176 struct timeval **tv_io, struct timeval *tvbuf,
177 struct timeval now) {
180 for (qu= ads->timew.head; qu; qu= nqu) {
182 if (!timercmp(&now,&qu->timeout,>)) {
183 if (!tv_io) continue;
184 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
187 LIST_UNLINK(ads->timew,qu);
188 if (qu->state != query_tosend) {
189 adns__query_fail(qu,adns_s_timeout);
191 adns__query_send(qu,now);
193 nqu= ads->timew.head;
198 void adns_firsttimeout(adns_state ads,
199 struct timeval **tv_io, struct timeval *tvbuf,
200 struct timeval now) {
201 adns__consistency(ads,0,cc_entex);
202 adns__timeouts(ads, 0, tv_io,tvbuf, now);
203 adns__consistency(ads,0,cc_entex);
206 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
207 struct timeval tv_buf;
209 adns__consistency(ads,0,cc_entex);
210 adns__must_gettimeofday(ads,&now,&tv_buf);
211 if (now) adns__timeouts(ads, 1, 0,0, *now);
212 adns__consistency(ads,0,cc_entex);
215 /* fd handling functions. These are the top-level of the real work of
216 * reception and often transmission.
219 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
220 /* Returns the number of entries filled in. Always zeroes revents. */
222 assert(MAX_POLLFDS==2);
224 pollfds_buf[0].fd= ads->udpsocket;
225 pollfds_buf[0].events= POLLIN;
226 pollfds_buf[0].revents= 0;
228 switch (ads->tcpstate) {
229 case server_disconnected:
231 case server_connecting:
232 pollfds_buf[1].events= POLLOUT;
235 pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
240 pollfds_buf[1].fd= ads->tcpsocket;
244 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
245 int want, dgramlen, r, udpaddrlen, serv, old_skip;
246 byte udpbuf[DNS_MAXUDP];
247 struct sockaddr_in udpaddr;
249 adns__consistency(ads,0,cc_entex);
251 switch (ads->tcpstate) {
252 case server_disconnected:
253 case server_connecting:
256 if (fd != ads->tcpsocket) break;
257 assert(!ads->tcprecv_skip);
259 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
260 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
261 ads->tcprecv.buf[ads->tcprecv_skip+1]);
262 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
263 old_skip= ads->tcprecv_skip;
264 ads->tcprecv_skip += 2+dgramlen;
265 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
266 dgramlen, ads->tcpserver, 1,*now);
274 ads->tcprecv.used -= ads->tcprecv_skip;
275 memmove(ads->tcprecv.buf,ads->tcprecv.buf+ads->tcprecv_skip,ads->tcprecv.used);
276 ads->tcprecv_skip= 0;
277 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
278 assert(ads->tcprecv.used <= ads->tcprecv.avail);
279 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
280 r= read(ads->tcpsocket,
281 ads->tcprecv.buf+ads->tcprecv.used,
282 ads->tcprecv.avail-ads->tcprecv.used);
284 ads->tcprecv.used+= r;
287 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
288 if (errno==EINTR) continue;
289 if (errno_resources(errno)) { r= errno; goto xit; }
291 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
294 } /* never reached */
298 if (fd == ads->udpsocket) {
300 udpaddrlen= sizeof(udpaddr);
301 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
302 (struct sockaddr*)&udpaddr,&udpaddrlen);
304 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
305 if (errno == EINTR) continue;
306 if (errno_resources(errno)) { r= errno; goto xit; }
307 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
310 if (udpaddrlen != sizeof(udpaddr)) {
311 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
312 " (expected %d)", udpaddrlen,sizeof(udpaddr));
315 if (udpaddr.sin_family != AF_INET) {
316 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
317 " %u (expected %u)",udpaddr.sin_family,AF_INET);
320 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
321 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
322 ntohs(udpaddr.sin_port),DNS_PORT);
326 serv < ads->nservers &&
327 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
329 if (serv >= ads->nservers) {
330 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
331 inet_ntoa(udpaddr.sin_addr));
334 adns__procdgram(ads,udpbuf,r,serv,0,*now);
339 adns__consistency(ads,0,cc_entex);
343 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
346 adns__consistency(ads,0,cc_entex);
348 switch (ads->tcpstate) {
349 case server_disconnected:
351 case server_connecting:
352 if (fd != ads->tcpsocket) break;
353 assert(ads->tcprecv.used==0);
354 assert(ads->tcprecv_skip==0);
356 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
357 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
358 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
359 tcp_connected(ads,*now);
363 adns__tcp_broken(ads,"connect/read","sent data before first request");
366 if (errno==EINTR) continue;
367 if (errno_resources(errno)) { r= errno; goto xit; }
368 adns__tcp_broken(ads,"connect/read",strerror(errno));
372 if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break;
374 adns__sigpipe_protect(ads);
375 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
376 adns__sigpipe_unprotect(ads);
378 if (errno==EINTR) continue;
379 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
380 if (errno_resources(errno)) { r= errno; goto xit; }
381 adns__tcp_broken(ads,"write",strerror(errno));
384 ads->tcpsend.used -= r;
385 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
393 adns__consistency(ads,0,cc_entex);
397 int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
398 adns__consistency(ads,0,cc_entex);
399 switch (ads->tcpstate) {
400 case server_disconnected:
402 case server_connecting:
404 if (fd != ads->tcpsocket) break;
405 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
410 adns__consistency(ads,0,cc_entex);
414 static void fd_event(adns_state ads, int fd,
415 int revent, int pollflag,
416 int maxfd, const fd_set *fds,
417 int (*func)(adns_state, int fd, const struct timeval *now),
418 struct timeval now, int *r_r) {
421 if (!(revent & pollflag)) return;
422 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
423 r= func(ads,fd,&now);
428 adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno));
429 adns_globalsystemfailure(ads);
434 void adns__fdevents(adns_state ads,
435 const struct pollfd *pollfds, int npollfds,
436 int maxfd, const fd_set *readfds,
437 const fd_set *writefds, const fd_set *exceptfds,
438 struct timeval now, int *r_r) {
441 for (i=0; i<npollfds; i++) {
443 if (fd >= maxfd) maxfd= fd+1;
444 revents= pollfds[i].revents;
445 fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r);
446 fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r);
447 fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r);
451 /* Wrappers for select(2). */
453 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
454 fd_set *writefds_io, fd_set *exceptfds_io,
455 struct timeval **tv_mod, struct timeval *tv_tobuf,
456 const struct timeval *now) {
457 struct timeval tv_nowbuf;
458 struct pollfd pollfds[MAX_POLLFDS];
459 int i, fd, maxfd, npollfds;
461 adns__consistency(ads,0,cc_entex);
463 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
464 /* The caller is planning to sleep. */
465 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
467 adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now);
470 npollfds= adns__pollfds(ads,pollfds);
472 for (i=0; i<npollfds; i++) {
474 if (fd >= maxfd) maxfd= fd+1;
475 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
476 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
477 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
482 adns__consistency(ads,0,cc_entex);
485 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
486 const fd_set *writefds, const fd_set *exceptfds,
487 const struct timeval *now) {
488 struct timeval tv_buf;
489 struct pollfd pollfds[MAX_POLLFDS];
492 adns__consistency(ads,0,cc_entex);
493 adns__must_gettimeofday(ads,&now,&tv_buf);
495 adns_processtimeouts(ads,now);
497 npollfds= adns__pollfds(ads,pollfds);
498 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
501 maxfd,readfds,writefds,exceptfds,
504 adns__consistency(ads,0,cc_entex);
507 /* General helpful functions. */
509 void adns_globalsystemfailure(adns_state ads) {
510 adns__consistency(ads,0,cc_entex);
512 while (ads->timew.head) {
513 adns__query_fail(ads->timew.head, adns_s_systemfail);
516 switch (ads->tcpstate) {
517 case server_connecting:
519 adns__tcp_closenext(ads);
521 case server_disconnected:
526 adns__consistency(ads,0,cc_entex);
529 int adns_processany(adns_state ads) {
532 struct pollfd pollfds[MAX_POLLFDS];
535 adns__consistency(ads,0,cc_entex);
537 r= gettimeofday(&now,0);
538 if (!r) adns_processtimeouts(ads,&now);
540 /* We just use adns__fdevents to loop over the fd's trying them.
541 * This seems more sensible than calling select, since we're most
542 * likely just to want to do a read on one or two fds anyway.
544 npollfds= adns__pollfds(ads,pollfds);
545 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events;
551 adns__consistency(ads,0,cc_entex);
555 void adns__autosys(adns_state ads, struct timeval now) {
556 if (ads->iflags & adns_if_noautosys) return;
557 adns_processany(ads);
560 int adns__internal_check(adns_state ads,
561 adns_query *query_io,
562 adns_answer **answer,
568 if (ads->output.head) {
569 qu= ads->output.head;
570 } else if (ads->timew.head) {
576 if (qu->id>=0) return EAGAIN;
578 LIST_UNLINK(ads->output,qu);
580 if (context_r) *context_r= qu->ctx.ext;
586 int adns_wait(adns_state ads,
587 adns_query *query_io,
588 adns_answer **answer_r,
591 fd_set readfds, writefds, exceptfds;
592 struct timeval tvbuf, *tvp;
594 adns__consistency(ads,*query_io,cc_entex);
596 r= adns__internal_check(ads,query_io,answer_r,context_r);
597 if (r != EAGAIN) break;
599 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
600 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
601 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
603 if (errno == EINTR) {
604 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
606 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
607 adns_globalsystemfailure(ads);
611 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
614 adns__consistency(ads,0,cc_entex);
618 int adns_check(adns_state ads,
619 adns_query *query_io,
620 adns_answer **answer_r,
625 adns__consistency(ads,*query_io,cc_entex);
626 r= gettimeofday(&now,0);
627 if (!r) adns__autosys(ads,now);
629 r= adns__internal_check(ads,query_io,answer_r,context_r);
630 adns__consistency(ads,0,cc_entex);