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_broken(adns_state ads, const char *what, const char *why) {
45 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
47 adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
48 close(ads->tcpsocket);
49 ads->tcpstate= server_disconnected;
51 for (qu= ads->timew.head; qu; qu= nqu) {
53 if (qu->state == query_udp) continue;
54 assert(qu->state == query_tcpwait || qu->state == query_tcpsent);
55 qu->state= query_tcpwait;
56 qu->tcpfailed |= (1<<serv);
57 if (qu->tcpfailed == (1<<ads->nservers)-1) {
58 LIST_UNLINK(ads->timew,qu);
59 adns__query_fail(qu,adns_s_allservfail);
63 ads->tcprecv.used= ads->tcpsend.used= 0;
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->timew.head; qu; qu= nqu) {
74 if (qu->state == query_udp) continue;
75 assert (qu->state == query_tcpwait);
76 adns__query_tcp(qu,now);
80 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
82 struct sockaddr_in addr;
83 struct protoent *proto;
85 for (tries=0; tries<ads->nservers; tries++) {
86 if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return;
87 assert(ads->tcpstate == server_disconnected);
88 assert(!ads->tcpsend.used);
89 assert(!ads->tcprecv.used);
91 proto= getprotobyname("tcp");
92 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
93 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
95 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
98 r= adns__setnonblock(ads,fd);
100 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
104 memset(&addr,0,sizeof(addr));
105 addr.sin_family= AF_INET;
106 addr.sin_port= htons(DNS_PORT);
107 addr.sin_addr= ads->servers[ads->tcpserver].addr;
108 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
110 ads->tcpstate= server_connecting;
111 if (r==0) { tcp_connected(ads,now); continue; }
112 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
113 adns__tcp_broken(ads,"connect",strerror(errno));
117 /* `Interest' functions - find out which fd's we might be interested in,
118 * and when we want to be called back for a timeout.
121 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
122 struct timeval maxto) {
123 struct timeval *rbuf;
128 *tvbuf= maxto; *tv_io= tvbuf;
130 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
132 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
133 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
136 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
137 struct timeval now, struct timeval maxtime) {
140 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
141 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
143 maxtime.tv_sec -= (now.tv_sec+2);
144 maxtime.tv_usec -= (now.tv_usec-2000000);
145 dr= ldiv(maxtime.tv_usec,1000000);
146 maxtime.tv_sec += dr.quot;
147 maxtime.tv_usec -= dr.quot*1000000;
148 if (maxtime.tv_sec<0) timerclear(&maxtime);
149 inter_maxto(tv_io,tvbuf,maxtime);
152 static void inter_addfd(int *maxfd, fd_set *fds, int fd) {
153 if (!maxfd || !fds) return;
154 if (fd>=*maxfd) *maxfd= fd+1;
158 static void checktimeouts(adns_state ads, struct timeval now,
159 struct timeval **tv_io, struct timeval *tvbuf) {
162 for (qu= ads->timew.head; qu; qu= nqu) {
164 if (timercmp(&now,&qu->timeout,>)) {
165 LIST_UNLINK(ads->timew,qu);
166 if (qu->state != query_udp) {
167 adns__query_fail(qu,adns_s_timeout);
169 adns__query_udp(qu,now);
172 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
177 void adns_interest(adns_state ads, int *maxfd,
178 fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
179 struct timeval **tv_io, struct timeval *tvbuf) {
181 struct timeval tvto_lr;
184 /*fprintf(stderr,"adns_interest\n");*/
186 r= gettimeofday(&now,0);
188 adns__warn(ads,-1,0,"gettimeofday failed - will sleep for a bit: %s",
190 timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS);
191 inter_maxto(tv_io, tvbuf, tvto_lr);
193 checktimeouts(ads,now,tv_io,tvbuf);
196 inter_addfd(maxfd,readfds,ads->udpsocket);
198 switch (ads->tcpstate) {
199 case server_disconnected:
201 case server_connecting:
202 inter_addfd(maxfd,writefds,ads->tcpsocket);
205 inter_addfd(maxfd,readfds,ads->tcpsocket);
206 inter_addfd(maxfd,exceptfds,ads->tcpsocket);
207 if (ads->tcpsend.used) inter_addfd(maxfd,writefds,ads->tcpsocket);
214 /* Callback procedures - these do the real work of reception and timeout, etc. */
216 static int callb_checkfd(int maxfd, const fd_set *fds, int fd) {
217 return maxfd<0 || !fds ? 1 :
218 fd<maxfd && FD_ISSET(fd,fds);
221 static int internal_callback(adns_state ads, int maxfd,
222 const fd_set *readfds, const fd_set *writefds,
223 const fd_set *exceptfds,
224 struct timeval now) {
225 int skip, want, dgramlen, count, udpaddrlen, r, serv;
226 byte udpbuf[DNS_MAXUDP];
227 struct sockaddr_in udpaddr;
231 switch (ads->tcpstate) {
232 case server_disconnected:
234 case server_connecting:
235 if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
237 assert(ads->tcprecv.used==0);
238 if (!adns__vbuf_ensure(&ads->tcprecv,1)) return -1;
239 if (ads->tcprecv.buf) {
240 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
241 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
242 tcp_connected(ads,now);
244 adns__tcp_broken(ads,"connect/read","sent data before first request");
245 } else if (errno!=EINTR) {
246 adns__tcp_broken(ads,"connect/read",strerror(errno));
252 count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) +
253 callb_checkfd(maxfd,exceptfds,ads->tcpsocket) +
254 (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket));
255 if (callb_checkfd(maxfd,readfds,ads->tcpsocket)) {
258 if (ads->tcprecv.used<skip+2) {
261 dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1];
262 if (ads->tcprecv.used<skip+2+dgramlen) {
265 adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,now);
266 skip+= 2+dgramlen; continue;
269 ads->tcprecv.used -= skip;
270 memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
272 if (!adns__vbuf_ensure(&ads->tcprecv,want)) return -1;
273 assert(ads->tcprecv.used <= ads->tcprecv.avail);
274 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
275 r= read(ads->tcpsocket,
276 ads->tcprecv.buf+ads->tcprecv.used,
277 ads->tcprecv.avail-ads->tcprecv.used);
279 ads->tcprecv.used+= r;
282 if (errno==EAGAIN || errno==EWOULDBLOCK || errno==ENOMEM) break;
283 if (errno==EINTR) continue;
285 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
289 } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) {
290 adns__tcp_broken(ads,"select","exceptional condition detected");
291 } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
292 adns__sigpipe_protect(ads);
293 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
294 adns__sigpipe_unprotect(ads);
296 if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) {
297 adns__tcp_broken(ads,"write",strerror(errno));
300 ads->tcpsend.used -= r;
301 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
309 if (callb_checkfd(maxfd,readfds,ads->udpsocket)) {
312 udpaddrlen= sizeof(udpaddr);
313 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
314 (struct sockaddr*)&udpaddr,&udpaddrlen);
316 if (!(errno == EAGAIN || errno == EWOULDBLOCK ||
317 errno == EINTR || errno == ENOMEM || errno == ENOBUFS))
318 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
321 if (udpaddrlen != sizeof(udpaddr)) {
322 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
323 " (expected %d)", udpaddrlen,sizeof(udpaddr));
326 if (udpaddr.sin_family != AF_INET) {
327 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
328 " %u (expected %u)",udpaddr.sin_family,AF_INET);
331 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
332 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
333 ntohs(udpaddr.sin_port),DNS_PORT);
337 serv < ads->nservers &&
338 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
340 if (serv >= ads->nservers) {
341 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
342 inet_ntoa(udpaddr.sin_addr));
345 adns__procdgram(ads,udpbuf,r,serv,now);
351 int adns_callback(adns_state ads, int maxfd,
352 const fd_set *readfds, const fd_set *writefds,
353 const fd_set *exceptfds) {
357 r= gettimeofday(&now,0); if (r) return -1;
358 checktimeouts(ads,now,0,0);
359 return internal_callback(ads,maxfd,readfds,writefds,exceptfds,now);
362 /* User-visible functions and their implementation. */
364 void adns__autosys(adns_state ads, struct timeval now) {
365 if (ads->iflags & adns_if_noautosys) return;
366 adns_callback(ads,-1,0,0,0);
369 static int internal_check(adns_state ads,
370 adns_query *query_io,
371 adns_answer **answer,
377 if (!ads->output.head) return EWOULDBLOCK;
378 qu= ads->output.head;
380 if (qu->id>=0) return EWOULDBLOCK;
382 LIST_UNLINK(ads->output,qu);
384 if (context_r) *context_r= qu->ctx.ext;
389 int adns_wait(adns_state ads,
390 adns_query *query_io,
391 adns_answer **answer_r,
393 int r, maxfd, rsel, rcb;
394 fd_set readfds, writefds, exceptfds;
395 struct timeval tvbuf, *tvp;
398 r= internal_check(ads,query_io,answer_r,context_r);
399 if (r != EWOULDBLOCK) return r;
401 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
402 adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf);
403 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
405 if (errno == EINTR && !(ads->iflags & adns_if_eintr)) continue;
408 rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds);
413 int adns_check(adns_state ads,
414 adns_query *query_io,
415 adns_answer **answer_r,
420 r= gettimeofday(&now,0); if (r) return errno;
421 adns__autosys(ads,now);
422 return internal_check(ads,query_io,answer_r,context_r);