14ce1b03f6150a8811a22b2be30340323860b4ab
12 /* TCP connection management */
14 void adns__tcp_broken(adns_state ads
, const char *what
, const char *why
) {
18 assert(ads
->tcpstate
== server_connecting
|| ads
->tcpstate
== server_ok
);
20 adns__warn(ads
,serv
,"TCP connection lost: %s: %s",what
,why
);
21 close(ads
->tcpsocket
);
22 ads
->tcpstate
= server_disconnected
;
24 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
26 if (qu
->state
== query_udp
) continue;
27 assert(qu
->state
== query_tcpwait
|| qu
->state
== query_tcpsent
);
28 qu
->state
= query_tcpwait
;
29 qu
->tcpfailed
|= (1<<serv
);
30 if (qu
->tcpfailed
== (1<<ads
->nservers
)-1) {
31 LIST_UNLINK(ads
->timew
,qu
);
32 adns__query_fail(ads
,qu
,adns_s_allservfail
);
36 ads
->tcprecv
.used
= ads
->tcpsend
.used
= 0;
37 ads
->tcpserver
= (serv
+1)%ads
->nservers
;
40 static void tcp_connected(adns_state ads
, struct timeval now
) {
43 adns__debug(ads
,ads
->tcpserver
,"TCP connected");
44 ads
->tcpstate
= server_ok
;
45 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
47 if (qu
->state
== query_udp
) continue;
48 assert (qu
->state
== query_tcpwait
);
49 adns__query_tcp(ads
,qu
,now
);
53 void adns__tcp_tryconnect(adns_state ads
, struct timeval now
) {
55 struct sockaddr_in addr
;
56 struct protoent
*proto
;
57 /* fixme: single TCP timeout, not once per server */
59 for (tries
=0; tries
<ads
->nservers
; tries
++) {
60 if (ads
->tcpstate
== server_connecting
|| ads
->tcpstate
== server_ok
) return;
61 assert(ads
->tcpstate
== server_disconnected
);
62 assert(!ads
->tcpsend
.used
);
63 assert(!ads
->tcprecv
.used
);
65 proto
= getprotobyname("tcp");
66 if (!proto
) { adns__diag(ads
,-1,"unable to find protocol no. for TCP !"); return; }
67 fd
= socket(AF_INET
,SOCK_STREAM
,proto
->p_proto
);
69 adns__diag(ads
,-1,"cannot create TCP socket: %s",strerror(errno
));
72 r
= adns__setnonblock(ads
,fd
);
74 adns__diag(ads
,-1,"cannot make TCP socket nonblocking: %s",strerror(r
));
78 memset(&addr
,0,sizeof(addr
));
79 addr
.sin_family
= AF_INET
;
80 addr
.sin_port
= htons(NSPORT
);
81 addr
.sin_addr
= ads
->servers
[ads
->tcpserver
].addr
;
82 r
= connect(fd
,&addr
,sizeof(addr
));
84 ads
->tcpstate
= server_connecting
;
85 if (r
==0) { tcp_connected(ads
,now
); continue; }
86 if (errno
== EWOULDBLOCK
|| errno
== EINPROGRESS
) return;
87 adns__tcp_broken(ads
,"connect",strerror(errno
));
91 /* `Interest' functions - find out which fd's we might be interested in,
92 * and when we want to be called back for a timeout.
95 static void inter_maxto(struct timeval
**tv_io
, struct timeval
*tvbuf
,
96 struct timeval maxto
) {
101 if (!rbuf
) { *tvbuf
= maxto
; *tv_io
= tvbuf
; return; }
102 if (timercmp(rbuf
,&maxto
,>)) *rbuf
= maxto
;
105 static void inter_maxtoabs(struct timeval
**tv_io
, struct timeval
*tvbuf
,
106 struct timeval now
, struct timeval maxtime
) {
110 maxtime
.tv_sec
-= (now
.tv_sec
-1);
111 maxtime
.tv_usec
+= (1000-now
.tv_usec
);
112 dr
= ldiv(maxtime
.tv_usec
,1000);
113 maxtime
.tv_sec
+= dr
.quot
;
114 maxtime
.tv_usec
-= dr
.rem
;
115 inter_maxto(tv_io
,tvbuf
,maxtime
);
118 static void inter_addfd(int *maxfd
, fd_set
*fds
, int fd
) {
119 if (!maxfd
|| !fds
) return;
120 if (fd
>=*maxfd
) *maxfd
= fd
+1;
124 static void checktimeouts(adns_state ads
, struct timeval now
,
125 struct timeval
**tv_io
, struct timeval
*tvbuf
) {
128 for (qu
= ads
->timew
.head
; qu
; qu
= nqu
) {
130 if (timercmp(&now
,&qu
->timeout
,>)) {
131 LIST_UNLINK(ads
->timew
,qu
);
132 if (qu
->state
!= query_udp
) {
133 adns__query_fail(ads
,qu
,adns_s_timeout
);
135 adns__query_udp(ads
,qu
,now
);
138 inter_maxtoabs(tv_io
,tvbuf
,now
,qu
->timeout
);
143 void adns_interest(adns_state ads
, int *maxfd
,
144 fd_set
*readfds
, fd_set
*writefds
, fd_set
*exceptfds
,
145 struct timeval
**tv_io
, struct timeval
*tvbuf
) {
147 struct timeval tvto_lr
;
150 r
= gettimeofday(&now
,0);
152 adns__warn(ads
,-1,"gettimeofday failed - will sleep for a bit: %s",strerror(errno
));
153 timerclear(&tvto_lr
); timevaladd(&tvto_lr
,LOCALRESOURCEMS
);
154 inter_maxto(tv_io
, tvbuf
, tvto_lr
);
156 checktimeouts(ads
,now
,tv_io
,tvbuf
);
159 inter_addfd(maxfd
,readfds
,ads
->udpsocket
);
161 switch (ads
->tcpstate
) {
162 case server_disconnected
:
164 case server_connecting
:
165 inter_addfd(maxfd
,writefds
,ads
->tcpsocket
);
168 inter_addfd(maxfd
,readfds
,ads
->tcpsocket
);
169 inter_addfd(maxfd
,exceptfds
,ads
->tcpsocket
);
170 if (ads
->tcpsend
.used
) inter_addfd(maxfd
,writefds
,ads
->tcpsocket
);
176 /* Callback procedures - these do the real work of reception and timeout, etc. */
178 static int callb_checkfd(int maxfd
, const fd_set
*fds
, int fd
) {
179 return maxfd
<0 || !fds ?
1 :
180 fd
<maxfd
&& FD_ISSET(fd
,fds
);
183 static int internal_callback(adns_state ads
, int maxfd
,
184 const fd_set
*readfds
, const fd_set
*writefds
,
185 const fd_set
*exceptfds
,
186 struct timeval now
) {
187 int skip
, want
, dgramlen
, count
, udpaddrlen
, r
, serv
;
188 byte udpbuf
[MAXUDPDGRAM
];
189 struct sockaddr_in udpaddr
;
193 switch (ads
->tcpstate
) {
194 case server_disconnected
:
196 case server_connecting
:
197 if (callb_checkfd(maxfd
,writefds
,ads
->tcpsocket
)) {
199 assert(ads
->tcprecv
.used
==0);
200 adns__vbuf_ensure(&ads
->tcprecv
,1);
201 if (ads
->tcprecv
.buf
) {
202 r
= read(ads
->tcpsocket
,&ads
->tcprecv
.buf
,1);
203 if (r
==0 || (r
<0 && (errno
==EAGAIN
|| errno
==EWOULDBLOCK
))) {
204 tcp_connected(ads
,now
);
206 adns__tcp_broken(ads
,"connect/read","sent data before first request");
207 } else if (errno
!=EINTR
) {
208 adns__tcp_broken(ads
,"connect/read",strerror(errno
));
214 count
+= callb_checkfd(maxfd
,readfds
,ads
->tcpsocket
) +
215 callb_checkfd(maxfd
,exceptfds
,ads
->tcpsocket
) +
216 (ads
->tcpsend
.used
&& callb_checkfd(maxfd
,writefds
,ads
->tcpsocket
));
217 if (callb_checkfd(maxfd
,readfds
,ads
->tcpsocket
)) {
220 if (ads
->tcprecv
.used
<skip
+2) {
223 dgramlen
= (ads
->tcprecv
.buf
[skip
]<<8) | ads
->tcprecv
.buf
[skip
+1];
224 if (ads
->tcprecv
.used
<skip
+2+dgramlen
) {
227 adns__procdgram(ads
,ads
->tcprecv
.buf
+skip
+2,dgramlen
,ads
->tcpserver
);
228 skip
+= 2+dgramlen
; continue;
231 ads
->tcprecv
.used
-= skip
;
232 memmove(ads
->tcprecv
.buf
,ads
->tcprecv
.buf
+skip
,ads
->tcprecv
.used
);
233 adns__vbuf_ensure(&ads
->tcprecv
,want
);
234 if (ads
->tcprecv
.used
>= ads
->tcprecv
.avail
) break;
235 r
= read(ads
->tcpsocket
,
236 ads
->tcprecv
.buf
+ads
->tcprecv
.used
,
237 ads
->tcprecv
.avail
-ads
->tcprecv
.used
);
239 ads
->tcprecv
.used
+= r
;
242 if (errno
==EAGAIN
|| errno
==EWOULDBLOCK
|| errno
==ENOMEM
) break;
243 if (errno
==EINTR
) continue;
245 adns__tcp_broken(ads
,"read",r?
strerror(errno
):"closed");
249 } else if (callb_checkfd(maxfd
,exceptfds
,ads
->tcpsocket
)) {
250 adns__tcp_broken(ads
,"select","exceptional condition detected");
251 } else if (ads
->tcpsend
.used
&& callb_checkfd(maxfd
,writefds
,ads
->tcpsocket
)) {
252 r
= write(ads
->tcpsocket
,ads
->tcpsend
.buf
,ads
->tcpsend
.used
);
254 if (errno
!=EAGAIN
&& errno
!=EWOULDBLOCK
&& errno
!=ENOMEM
&& errno
!=EINTR
) {
255 adns__tcp_broken(ads
,"write",strerror(errno
));
258 ads
->tcpsend
.used
-= r
;
259 memmove(ads
->tcpsend
.buf
,ads
->tcpsend
.buf
+r
,ads
->tcpsend
.used
);
266 if (callb_checkfd(maxfd
,readfds
,ads
->udpsocket
)) {
269 udpaddrlen
= sizeof(udpaddr
);
270 r
= recvfrom(ads
->udpsocket
,udpbuf
,sizeof(udpbuf
),0,&udpaddr
,&udpaddrlen
);
272 if (!(errno
== EAGAIN
|| errno
== EWOULDBLOCK
||
273 errno
== EINTR
|| errno
== ENOMEM
|| errno
== ENOBUFS
))
274 adns__warn(ads
,-1,"datagram receive error: %s",strerror(errno
));
277 if (udpaddrlen
!= sizeof(udpaddr
)) {
278 adns__diag(ads
,-1,"datagram received with wrong address length %d (expected %d)",
279 udpaddrlen
,sizeof(udpaddr
));
282 if (udpaddr
.sin_family
!= AF_INET
) {
283 adns__diag(ads
,-1,"datagram received with wrong protocol family"
284 " %u (expected %u)",udpaddr
.sin_family
,AF_INET
);
287 if (ntohs(udpaddr
.sin_port
) != NSPORT
) {
288 adns__diag(ads
,-1,"datagram received from wrong port %u (expected %u)",
289 ntohs(udpaddr
.sin_port
),NSPORT
);
293 serv
< ads
->nservers
&&
294 ads
->servers
[serv
].addr
.s_addr
!= udpaddr
.sin_addr
.s_addr
;
296 if (serv
>= ads
->nservers
) {
297 adns__warn(ads
,-1,"datagram received from unknown nameserver %s",
298 inet_ntoa(udpaddr
.sin_addr
));
301 adns__procdgram(ads
,udpbuf
,r
,serv
);
307 int adns_callback(adns_state ads
, int maxfd
,
308 const fd_set
*readfds
, const fd_set
*writefds
,
309 const fd_set
*exceptfds
) {
313 r
= gettimeofday(&now
,0); if (r
) return -1;
314 checktimeouts(ads
,now
,0,0);
315 return internal_callback(ads
,maxfd
,readfds
,writefds
,exceptfds
,now
);
318 /* User-visible functions and their implementation. */
320 void adns__autosys(adns_state ads
, struct timeval now
) {
321 if (ads
->iflags
& adns_if_noautosys
) return;
322 adns_callback(ads
,-1,0,0,0);
325 static int internal_check(adns_state ads
,
326 adns_query
*query_io
,
327 adns_answer
**answer
,
333 if (!ads
->output
.head
) return EWOULDBLOCK
;
334 qu
= ads
->output
.head
;
336 if (qu
->id
>=0) return EWOULDBLOCK
;
338 LIST_UNLINK(ads
->output
,qu
);
339 *answer
= (adns_answer
*)qu
->answer
.buf
;
340 if (context_r
) *context_r
= qu
->context
.ext
;
345 int adns_wait(adns_state ads
,
346 adns_query
*query_io
,
347 adns_answer
**answer_r
,
349 int r
, maxfd
, rsel
, rcb
;
350 fd_set readfds
, writefds
, exceptfds
;
351 struct timeval tvbuf
, *tvp
;
354 r
= internal_check(ads
,query_io
,answer_r
,context_r
);
355 if (r
&& r
!= EWOULDBLOCK
) return r
;
357 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
358 adns_interest(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,&tvp
,&tvbuf
);
359 rsel
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tvp
);
360 if (rsel
==-1) return r
;
361 rcb
= adns_callback(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
);
366 int adns_check(adns_state ads
,
367 adns_query
*query_io
,
368 adns_answer
**answer_r
,
373 r
= gettimeofday(&now
,0); if (r
) return errno
;
374 adns__autosys(ads
,now
);
375 return internal_check(ads
,query_io
,answer_r
,context_r
);