656b2da9 |
1 | /**/ |
2 | |
4353a5c4 |
3 | #include <string.h> |
4 | #include <errno.h> |
5 | #include <stdlib.h> |
6 | |
7 | #include <netdb.h> |
8 | #include <arpa/inet.h> |
9 | |
10 | #include "internal.h" |
37e28fde |
11 | |
ddfda861 |
12 | /* TCP connection management */ |
656b2da9 |
13 | |
8402e34c |
14 | void adns__tcp_broken(adns_state ads, const char *what, const char *why) { |
15 | int serv; |
4353a5c4 |
16 | adns_query qu, nqu; |
8402e34c |
17 | |
18 | assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok); |
ddfda861 |
19 | serv= ads->tcpserver; |
4353a5c4 |
20 | adns__warn(ads,serv,"TCP connection lost: %s: %s",what,why); |
37e28fde |
21 | close(ads->tcpsocket); |
22 | ads->tcpstate= server_disconnected; |
23 | |
4353a5c4 |
24 | for (qu= ads->timew.head; qu; qu= nqu) { |
8402e34c |
25 | nqu= qu->next; |
ddfda861 |
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) { |
4353a5c4 |
31 | LIST_UNLINK(ads->timew,qu); |
ddfda861 |
32 | adns__query_fail(ads,qu,adns_s_allservfail); |
33 | } |
8402e34c |
34 | } |
35 | |
4353a5c4 |
36 | ads->tcprecv.used= ads->tcpsend.used= 0; |
8402e34c |
37 | ads->tcpserver= (serv+1)%ads->nservers; |
38 | } |
39 | |
ddfda861 |
40 | static void tcp_connected(adns_state ads, struct timeval now) { |
4353a5c4 |
41 | adns_query qu, nqu; |
42 | |
43 | adns__debug(ads,ads->tcpserver,"TCP connected"); |
44 | ads->tcpstate= server_ok; |
ddfda861 |
45 | for (qu= ads->timew.head; qu; qu= nqu) { |
46 | nqu= qu->next; |
47 | if (qu->state == query_udp) continue; |
48 | assert (qu->state == query_tcpwait); |
49 | adns__query_tcp(ads,qu,now); |
50 | } |
51 | } |
52 | |
53 | void adns__tcp_tryconnect(adns_state ads, struct timeval now) { |
8402e34c |
54 | int r, fd, tries; |
4353a5c4 |
55 | struct sockaddr_in addr; |
56 | struct protoent *proto; |
ddfda861 |
57 | /* fixme: single TCP timeout, not once per server */ |
8402e34c |
58 | |
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); |
4353a5c4 |
62 | assert(!ads->tcpsend.used); |
63 | assert(!ads->tcprecv.used); |
8402e34c |
64 | |
65 | proto= getprotobyname("tcp"); |
4353a5c4 |
66 | if (!proto) { adns__diag(ads,-1,"unable to find protocol no. for TCP !"); return; } |
8402e34c |
67 | fd= socket(AF_INET,SOCK_STREAM,proto->p_proto); |
4353a5c4 |
68 | if (fd<0) { |
69 | adns__diag(ads,-1,"cannot create TCP socket: %s",strerror(errno)); |
70 | return; |
71 | } |
72 | r= adns__setnonblock(ads,fd); |
73 | if (r) { |
74 | adns__diag(ads,-1,"cannot make TCP socket nonblocking: %s",strerror(r)); |
75 | close(fd); |
76 | return; |
77 | } |
8402e34c |
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)); |
83 | ads->tcpsocket= fd; |
84 | ads->tcpstate= server_connecting; |
4353a5c4 |
85 | if (r==0) { tcp_connected(ads,now); continue; } |
8402e34c |
86 | if (errno == EWOULDBLOCK || errno == EINPROGRESS) return; |
ddfda861 |
87 | adns__tcp_broken(ads,"connect",strerror(errno)); |
8402e34c |
88 | } |
89 | } |
90 | |
4353a5c4 |
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. |
93 | */ |
94 | |
95 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
96 | struct timeval maxto) { |
97 | struct timeval *rbuf; |
98 | |
99 | if (!tv_io) return; |
100 | rbuf= *tv_io; |
101 | if (!rbuf) { *tvbuf= maxto; *tv_io= tvbuf; return; } |
102 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
103 | } |
104 | |
105 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
106 | struct timeval now, struct timeval maxtime) { |
107 | ldiv_t dr; |
108 | |
109 | if (!tv_io) return; |
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); |
116 | } |
117 | |
118 | static void inter_addfd(int *maxfd, fd_set *fds, int fd) { |
119 | if (!maxfd || !fds) return; |
120 | if (fd>=*maxfd) *maxfd= fd+1; |
121 | FD_SET(fd,fds); |
122 | } |
123 | |
124 | static void checktimeouts(adns_state ads, struct timeval now, |
125 | struct timeval **tv_io, struct timeval *tvbuf) { |
126 | adns_query qu, nqu; |
127 | |
128 | for (qu= ads->timew.head; qu; qu= nqu) { |
129 | nqu= qu->next; |
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); |
134 | } else { |
135 | adns__query_udp(ads,qu,now); |
136 | } |
137 | } else { |
138 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
139 | } |
140 | } |
141 | } |
142 | |
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) { |
146 | struct timeval now; |
147 | struct timeval tvto_lr; |
148 | int r; |
149 | |
150 | r= gettimeofday(&now,0); |
151 | if (r) { |
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); |
155 | } else { |
156 | checktimeouts(ads,now,tv_io,tvbuf); |
157 | } |
158 | |
159 | inter_addfd(maxfd,readfds,ads->udpsocket); |
160 | |
161 | switch (ads->tcpstate) { |
162 | case server_disconnected: |
163 | break; |
164 | case server_connecting: |
165 | inter_addfd(maxfd,writefds,ads->tcpsocket); |
166 | break; |
167 | case server_ok: |
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); |
171 | default: |
172 | abort(); |
173 | } |
174 | } |
175 | |
ddfda861 |
176 | /* Callback procedures - these do the real work of reception and timeout, etc. */ |
177 | |
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); |
181 | } |
182 | |
183 | static int internal_callback(adns_state ads, int maxfd, |
184 | const fd_set *readfds, const fd_set *writefds, |
4353a5c4 |
185 | const fd_set *exceptfds, |
186 | struct timeval now) { |
187 | int skip, want, dgramlen, count, udpaddrlen, r, serv; |
188 | byte udpbuf[MAXUDPDGRAM]; |
37e28fde |
189 | struct sockaddr_in udpaddr; |
656b2da9 |
190 | |
191 | count= 0; |
ddfda861 |
192 | |
193 | switch (ads->tcpstate) { |
194 | case server_disconnected: |
195 | break; |
196 | case server_connecting: |
656b2da9 |
197 | if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) { |
198 | count++; |
199 | assert(ads->tcprecv.used==0); |
4353a5c4 |
200 | adns__vbuf_ensure(&ads->tcprecv,1); |
656b2da9 |
201 | if (ads->tcprecv.buf) { |
202 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
203 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
4353a5c4 |
204 | tcp_connected(ads,now); |
656b2da9 |
205 | } else if (r>0) { |
4353a5c4 |
206 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
656b2da9 |
207 | } else if (errno!=EINTR) { |
4353a5c4 |
208 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
656b2da9 |
209 | } |
210 | } |
211 | } |
ddfda861 |
212 | break; |
213 | case server_ok: |
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)) { |
656b2da9 |
218 | skip= 0; |
219 | for (;;) { |
220 | if (ads->tcprecv.used<skip+2) { |
221 | want= 2; |
222 | } else { |
223 | dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1]; |
224 | if (ads->tcprecv.used<skip+2+dgramlen) { |
225 | want= 2+dgramlen; |
226 | } else { |
4353a5c4 |
227 | adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver); |
656b2da9 |
228 | skip+= 2+dgramlen; continue; |
229 | } |
230 | } |
37e28fde |
231 | ads->tcprecv.used -= skip; |
656b2da9 |
232 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used); |
4353a5c4 |
233 | adns__vbuf_ensure(&ads->tcprecv,want); |
656b2da9 |
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); |
238 | if (r>0) { |
239 | ads->tcprecv.used+= r; |
240 | } else { |
241 | if (r<0) { |
242 | if (errno==EAGAIN || errno==EWOULDBLOCK || errno==ENOMEM) break; |
243 | if (errno==EINTR) continue; |
244 | } |
4353a5c4 |
245 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
656b2da9 |
246 | break; |
247 | } |
248 | } |
249 | } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) { |
4353a5c4 |
250 | adns__tcp_broken(ads,"select","exceptional condition detected"); |
656b2da9 |
251 | } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) { |
252 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
253 | if (r<0) { |
254 | if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) { |
4353a5c4 |
255 | adns__tcp_broken(ads,"write",strerror(errno)); |
656b2da9 |
256 | } |
257 | } else if (r>0) { |
258 | ads->tcpsend.used -= r; |
259 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
260 | } |
261 | } |
ddfda861 |
262 | default: |
263 | abort(); |
656b2da9 |
264 | } |
265 | |
37e28fde |
266 | if (callb_checkfd(maxfd,readfds,ads->udpsocket)) { |
267 | count++; |
268 | for (;;) { |
269 | udpaddrlen= sizeof(udpaddr); |
270 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,&udpaddr,&udpaddrlen); |
271 | if (r<0) { |
272 | if (!(errno == EAGAIN || errno == EWOULDBLOCK || |
273 | errno == EINTR || errno == ENOMEM || errno == ENOBUFS)) |
4353a5c4 |
274 | adns__warn(ads,-1,"datagram receive error: %s",strerror(errno)); |
37e28fde |
275 | break; |
276 | } |
277 | if (udpaddrlen != sizeof(udpaddr)) { |
4353a5c4 |
278 | adns__diag(ads,-1,"datagram received with wrong address length %d (expected %d)", |
279 | udpaddrlen,sizeof(udpaddr)); |
37e28fde |
280 | continue; |
281 | } |
282 | if (udpaddr.sin_family != AF_INET) { |
4353a5c4 |
283 | adns__diag(ads,-1,"datagram received with wrong protocol family" |
284 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
37e28fde |
285 | continue; |
286 | } |
287 | if (ntohs(udpaddr.sin_port) != NSPORT) { |
4353a5c4 |
288 | adns__diag(ads,-1,"datagram received from wrong port %u (expected %u)", |
289 | ntohs(udpaddr.sin_port),NSPORT); |
37e28fde |
290 | continue; |
291 | } |
292 | for (serv= 0; |
293 | serv < ads->nservers && |
294 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
295 | serv++); |
296 | if (serv >= ads->nservers) { |
4353a5c4 |
297 | adns__warn(ads,-1,"datagram received from unknown nameserver %s", |
298 | inet_ntoa(udpaddr.sin_addr)); |
37e28fde |
299 | continue; |
300 | } |
4353a5c4 |
301 | adns__procdgram(ads,udpbuf,r,serv); |
656b2da9 |
302 | } |
37e28fde |
303 | } |
4353a5c4 |
304 | return count; |
656b2da9 |
305 | } |
656b2da9 |
306 | |
ddfda861 |
307 | int adns_callback(adns_state ads, int maxfd, |
308 | const fd_set *readfds, const fd_set *writefds, |
309 | const fd_set *exceptfds) { |
310 | struct timeval now; |
656b2da9 |
311 | int r; |
37e28fde |
312 | |
4353a5c4 |
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); |
656b2da9 |
316 | } |
317 | |
ddfda861 |
318 | /* User-visible functions and their implementation. */ |
319 | |
4353a5c4 |
320 | void adns__autosys(adns_state ads, struct timeval now) { |
ddfda861 |
321 | if (ads->iflags & adns_if_noautosys) return; |
322 | adns_callback(ads,-1,0,0,0); |
323 | } |
324 | |
656b2da9 |
325 | static int internal_check(adns_state ads, |
326 | adns_query *query_io, |
327 | adns_answer **answer, |
328 | void **context_r) { |
329 | adns_query qu; |
330 | |
331 | qu= *query_io; |
332 | if (!qu) { |
333 | if (!ads->output.head) return EWOULDBLOCK; |
334 | qu= ads->output.head; |
335 | } else { |
336 | if (qu->id>=0) return EWOULDBLOCK; |
337 | } |
338 | LIST_UNLINK(ads->output,qu); |
4353a5c4 |
339 | *answer= (adns_answer*)qu->answer.buf; |
340 | if (context_r) *context_r= qu->context.ext; |
656b2da9 |
341 | free(qu); |
342 | return 0; |
343 | } |
344 | |
345 | int adns_wait(adns_state ads, |
346 | adns_query *query_io, |
347 | adns_answer **answer_r, |
348 | void **context_r) { |
349 | int r, maxfd, rsel, rcb; |
350 | fd_set readfds, writefds, exceptfds; |
351 | struct timeval tvbuf, *tvp; |
352 | |
353 | for (;;) { |
354 | r= internal_check(ads,query_io,answer_r,context_r); |
355 | if (r && r != EWOULDBLOCK) return r; |
656b2da9 |
356 | maxfd= 0; tvp= 0; |
37e28fde |
357 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
656b2da9 |
358 | adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); |
359 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
360 | if (rsel==-1) return r; |
4353a5c4 |
361 | rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); |
656b2da9 |
362 | assert(rcb==rsel); |
363 | } |
364 | } |
365 | |
366 | int adns_check(adns_state ads, |
367 | adns_query *query_io, |
368 | adns_answer **answer_r, |
369 | void **context_r) { |
4353a5c4 |
370 | struct timeval now; |
371 | int r; |
372 | |
373 | r= gettimeofday(&now,0); if (r) return errno; |
374 | adns__autosys(ads,now); |
656b2da9 |
375 | return internal_check(ads,query_io,answer_r,context_r); |
376 | } |