4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
8 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
39 /* TCP connection management. */
41 void adns__tcp_closenext(adns_state ads) {
45 close(ads->tcpsocket);
46 ads->tcpstate= server_disconnected;
47 ads->tcprecv.used= ads->tcpsend.used= 0;
48 ads->tcpserver= (serv+1)%ads->nservers;
51 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
55 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
57 adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
58 adns__tcp_closenext(ads);
60 for (qu= ads->timew.head; qu; qu= nqu) {
62 if (qu->state == query_udp) continue;
63 assert(qu->state == query_tcpwait || qu->state == query_tcpsent);
64 qu->state= query_tcpwait;
65 qu->tcpfailed |= (1<<serv);
66 if (qu->tcpfailed == (1<<ads->nservers)-1) {
67 LIST_UNLINK(ads->timew,qu);
68 adns__query_fail(qu,adns_s_allservfail);
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->timew.head; qu; qu= nqu) {
80 if (qu->state == query_udp) continue;
81 assert (qu->state == query_tcpwait);
82 adns__query_tcp(qu,now);
86 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
88 struct sockaddr_in addr;
89 struct protoent *proto;
91 for (tries=0; tries<ads->nservers; tries++) {
92 if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return;
93 assert(ads->tcpstate == server_disconnected);
94 assert(!ads->tcpsend.used);
95 assert(!ads->tcprecv.used);
97 proto= getprotobyname("tcp");
98 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
99 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
101 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
104 r= adns__setnonblock(ads,fd);
106 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
110 memset(&addr,0,sizeof(addr));
111 addr.sin_family= AF_INET;
112 addr.sin_port= htons(DNS_PORT);
113 addr.sin_addr= ads->servers[ads->tcpserver].addr;
114 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
116 ads->tcpstate= server_connecting;
117 if (r==0) { tcp_connected(ads,now); continue; }
118 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
119 adns__tcp_broken(ads,"connect",strerror(errno));
123 /* Timeout handling functions. */
125 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
126 struct timeval *tv_buf) {
127 const struct timeval *now;
132 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
133 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
134 adns_globalsystemfailure(ads);
138 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
139 struct timeval maxto) {
140 struct timeval *rbuf;
145 *tvbuf= maxto; *tv_io= tvbuf;
147 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
149 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
150 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
153 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
154 struct timeval now, struct timeval maxtime) {
157 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
158 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
160 maxtime.tv_sec -= (now.tv_sec+2);
161 maxtime.tv_usec -= (now.tv_usec-2000000);
162 dr= ldiv(maxtime.tv_usec,1000000);
163 maxtime.tv_sec += dr.quot;
164 maxtime.tv_usec -= dr.quot*1000000;
165 if (maxtime.tv_sec<0) timerclear(&maxtime);
166 inter_maxto(tv_io,tvbuf,maxtime);
169 void adns__timeouts(adns_state ads, int act,
170 struct timeval **tv_io, struct timeval *tvbuf,
171 struct timeval now) {
174 for (qu= ads->timew.head; qu; qu= nqu) {
176 if (timercmp(&now,&qu->timeout,<=)) {
177 if (!tv_io) continue;
178 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
181 LIST_UNLINK(ads->timew,qu);
182 if (qu->state != query_udp) {
183 adns__query_fail(qu,adns_s_timeout);
185 adns__query_udp(qu,now);
187 nqu= ads->timew.head;
192 void adns_firsttimeout(adns_state ads,
193 struct timeval **tv_io, struct timeval *tvbuf,
194 struct timeval now) {
195 adns__timeouts(ads, 0, tv_io,tvbuf, now);
198 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
199 struct timeval tv_buf;
201 adns__must_gettimeofday(ads,&now,&tv_buf); if (!now) return;
202 adns__timeouts(ads, 1, 0,0, *now);
205 /* fd handling functions. These are the top-level of the real work of
206 * reception and often transmission.
209 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
210 /* Returns the number of entries filled in. Always zeroes revents. */
212 assert(MAX_POLLFDS==2);
214 pollfds_buf[0].fd= ads->udpsocket;
215 pollfds_buf[0].events= POLLIN;
216 pollfds_buf[0].revents= 0;
218 switch (ads->tcpstate) {
219 case server_disconnected:
221 case server_connecting:
222 pollfds_buf[1].events= POLLOUT;
225 pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
230 pollfds_buf[1].fd= ads->tcpsocket;
234 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
235 int skip, want, dgramlen, r, udpaddrlen, serv;
236 byte udpbuf[DNS_MAXUDP];
237 struct sockaddr_in udpaddr;
239 switch (ads->tcpstate) {
240 case server_disconnected:
241 case server_connecting:
244 if (fd != ads->tcpsocket) break;
247 if (ads->tcprecv.used<skip+2) {
250 dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1];
251 if (ads->tcprecv.used<skip+2+dgramlen) {
254 adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,*now);
255 skip+= 2+dgramlen; continue;
258 ads->tcprecv.used -= skip;
259 memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
261 if (!adns__vbuf_ensure(&ads->tcprecv,want)) return ENOMEM;
262 assert(ads->tcprecv.used <= ads->tcprecv.avail);
263 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
264 r= read(ads->tcpsocket,
265 ads->tcprecv.buf+ads->tcprecv.used,
266 ads->tcprecv.avail-ads->tcprecv.used);
268 ads->tcprecv.used+= r;
271 if (errno==EAGAIN || errno==EWOULDBLOCK) return 0;
272 if (errno==EINTR) continue;
273 if (errno_resources(errno)) return errno;
275 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
278 } /* never reached */
282 if (fd == ads->udpsocket) {
284 udpaddrlen= sizeof(udpaddr);
285 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
286 (struct sockaddr*)&udpaddr,&udpaddrlen);
288 if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
289 if (errno == EINTR) continue;
290 if (errno_resources(errno)) return errno;
291 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
294 if (udpaddrlen != sizeof(udpaddr)) {
295 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
296 " (expected %d)", udpaddrlen,sizeof(udpaddr));
299 if (udpaddr.sin_family != AF_INET) {
300 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
301 " %u (expected %u)",udpaddr.sin_family,AF_INET);
304 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
305 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
306 ntohs(udpaddr.sin_port),DNS_PORT);
310 serv < ads->nservers &&
311 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
313 if (serv >= ads->nservers) {
314 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
315 inet_ntoa(udpaddr.sin_addr));
318 adns__procdgram(ads,udpbuf,r,serv,*now);
324 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
327 switch (ads->tcpstate) {
328 case server_disconnected:
330 case server_connecting:
331 if (fd != ads->tcpsocket) break;
332 assert(ads->tcprecv.used==0);
334 if (!adns__vbuf_ensure(&ads->tcprecv,1)) return ENOMEM;
335 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
336 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
337 tcp_connected(ads,*now);
341 adns__tcp_broken(ads,"connect/read","sent data before first request");
344 if (errno==EINTR) continue;
345 if (errno_resources(errno)) return errno;
346 adns__tcp_broken(ads,"connect/read",strerror(errno));
350 if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break;
352 adns__sigpipe_protect(ads);
353 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
354 adns__sigpipe_unprotect(ads);
356 if (errno==EINTR) continue;
357 if (errno==EAGAIN || errno==EWOULDBLOCK) return 0;
358 if (errno_resources(errno)) return errno;
359 adns__tcp_broken(ads,"write",strerror(errno));
362 ads->tcpsend.used -= r;
363 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
372 int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
373 switch (ads->tcpstate) {
374 case server_disconnected:
376 case server_connecting:
378 if (fd != ads->tcpsocket) break;
379 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
387 static void fd_event(adns_state ads, int fd,
388 int revent, int pollflag,
389 int maxfd, const fd_set *fds,
390 int (*func)(adns_state, int fd, const struct timeval *now),
391 struct timeval now, int *r_r) {
394 if (!(revent & pollflag)) return;
395 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
396 r= func(ads,fd,&now);
401 adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno));
402 adns_globalsystemfailure(ads);
407 void adns__fdevents(adns_state ads,
408 const struct pollfd *pollfds, int npollfds,
409 int maxfd, const fd_set *readfds,
410 const fd_set *writefds, const fd_set *exceptfds,
411 struct timeval now, int *r_r) {
414 for (i=0; i<npollfds; i++) {
416 if (fd >= maxfd) maxfd= fd+1;
417 revents= pollfds[i].revents;
418 fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r);
419 fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r);
420 fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r);
424 /* Wrappers for select(2). */
426 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
427 fd_set *writefds_io, fd_set *exceptfds_io,
428 struct timeval **tv_mod, struct timeval *tv_tobuf,
429 const struct timeval *now) {
430 struct timeval tv_nowbuf;
431 struct pollfd pollfds[MAX_POLLFDS];
432 int i, fd, maxfd, npollfds;
434 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
435 /* The caller is planning to sleep. */
436 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
438 adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now);
441 npollfds= adns__pollfds(ads,pollfds);
443 for (i=0; i<npollfds; i++) {
445 if (fd >= maxfd) maxfd= fd+1;
446 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
447 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
448 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
453 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
454 const fd_set *writefds, const fd_set *exceptfds,
455 const struct timeval *now) {
456 struct timeval tv_buf;
457 struct pollfd pollfds[MAX_POLLFDS];
460 adns__must_gettimeofday(ads,&now,&tv_buf);
462 adns_processtimeouts(ads,now);
464 npollfds= adns__pollfds(ads,pollfds);
465 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
468 maxfd,readfds,writefds,exceptfds,
472 /* General helpful functions. */
474 void adns_globalsystemfailure(adns_state ads) {
475 while (ads->timew.head) {
476 adns__query_fail(ads->timew.head, adns_s_systemfail);
479 switch (ads->tcpstate) {
480 case server_connecting:
482 adns__tcp_closenext(ads);
484 case server_disconnected:
491 int adns_processany(adns_state ads) {
494 struct pollfd pollfds[MAX_POLLFDS];
497 r= gettimeofday(&now,0);
498 if (!r) adns_processtimeouts(ads,&now);
500 npollfds= adns__pollfds(ads,pollfds);
508 void adns__autosys(adns_state ads, struct timeval now) {
509 if (ads->iflags & adns_if_noautosys) return;
510 adns_processany(ads);
513 static int internal_check(adns_state ads,
514 adns_query *query_io,
515 adns_answer **answer,
521 if (!ads->output.head) return EWOULDBLOCK;
522 qu= ads->output.head;
524 if (qu->id>=0) return EWOULDBLOCK;
526 LIST_UNLINK(ads->output,qu);
528 if (context_r) *context_r= qu->ctx.ext;
533 int adns_wait(adns_state ads,
534 adns_query *query_io,
535 adns_answer **answer_r,
538 fd_set readfds, writefds, exceptfds;
539 struct timeval tvbuf, *tvp;
542 r= internal_check(ads,query_io,answer_r,context_r);
543 if (r != EWOULDBLOCK) return r;
545 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
546 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
547 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
549 if (errno == EINTR) {
550 if (ads->iflags & adns_if_eintr) return EINTR;
552 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
553 adns_globalsystemfailure(ads);
557 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
562 int adns_check(adns_state ads,
563 adns_query *query_io,
564 adns_answer **answer_r,
569 r= gettimeofday(&now,0);
570 if (!r) adns__autosys(ads,now);
572 return internal_check(ads,query_io,answer_r,context_r);