e576be50 |
1 | /* |
2 | * event.c |
3 | * - event loop core |
4 | * - TCP connection management |
5 | * - user-visible check/wait and event-loop-related functions |
6 | */ |
7 | /* |
ae8cc977 |
8 | * This file is part of adns, which is |
9 | * Copyright (C) 1997-2000,2003,2006 Ian Jackson |
10 | * Copyright (C) 1999-2000,2003,2006 Tony Finch |
11 | * Copyright (C) 1991 Massachusetts Institute of Technology |
12 | * (See the file INSTALL for full details.) |
e576be50 |
13 | * |
14 | * This program is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2, or (at your option) |
17 | * any later version. |
18 | * |
19 | * This program is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with this program; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
656b2da9 |
28 | |
4353a5c4 |
29 | #include <errno.h> |
30 | #include <stdlib.h> |
0f091044 |
31 | #include <unistd.h> |
4353a5c4 |
32 | |
0f091044 |
33 | #include <sys/types.h> |
34 | #include <sys/time.h> |
4353a5c4 |
35 | #include <netdb.h> |
eec1d9c8 |
36 | #include <sys/socket.h> |
37 | #include <netinet/in.h> |
4353a5c4 |
38 | #include <arpa/inet.h> |
39 | |
40 | #include "internal.h" |
2953d358 |
41 | #include "tvarith.h" |
37e28fde |
42 | |
620c146d |
43 | /* TCP connection management. */ |
44 | |
f7f83b4a |
45 | static void tcp_close(adns_state ads) { |
620c146d |
46 | close(ads->tcpsocket); |
6a578b2c |
47 | ads->tcpsocket= -1; |
70ad7a2a |
48 | ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0; |
620c146d |
49 | } |
656b2da9 |
50 | |
8402e34c |
51 | void adns__tcp_broken(adns_state ads, const char *what, const char *why) { |
52 | int serv; |
fb901bf5 |
53 | adns_query qu; |
8402e34c |
54 | |
55 | assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok); |
ddfda861 |
56 | serv= ads->tcpserver; |
f7f83b4a |
57 | if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why); |
58 | |
fb901bf5 |
59 | if (ads->tcpstate == server_connecting) { |
60 | /* Counts as a retry for all the queries waiting for TCP. */ |
61 | for (qu= ads->tcpw.head; qu; qu= qu->next) |
62 | qu->retries++; |
63 | } |
64 | |
f7f83b4a |
65 | tcp_close(ads); |
66 | ads->tcpstate= server_broken; |
67 | ads->tcpserver= (serv+1)%ads->nservers; |
8402e34c |
68 | } |
69 | |
ddfda861 |
70 | static void tcp_connected(adns_state ads, struct timeval now) { |
4353a5c4 |
71 | adns_query qu, nqu; |
72 | |
68442019 |
73 | adns__debug(ads,ads->tcpserver,0,"TCP connected"); |
4353a5c4 |
74 | ads->tcpstate= server_ok; |
f7f83b4a |
75 | for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) { |
ddfda861 |
76 | nqu= qu->next; |
f7f83b4a |
77 | assert(qu->state == query_tcpw); |
78 | adns__querysend_tcp(qu,now); |
ddfda861 |
79 | } |
80 | } |
81 | |
237ce710 |
82 | static void tcp_broken_events(adns_state ads) { |
83 | adns_query qu, nqu; |
84 | |
85 | assert(ads->tcpstate == server_broken); |
86 | for (qu= ads->tcpw.head; qu; qu= nqu) { |
87 | nqu= qu->next; |
88 | assert(qu->state == query_tcpw); |
89 | if (qu->retries > ads->nservers) { |
90 | LIST_UNLINK(ads->tcpw,qu); |
91 | adns__query_fail(qu,adns_s_allservfail); |
92 | } |
93 | } |
94 | ads->tcpstate= server_disconnected; |
95 | } |
96 | |
ddfda861 |
97 | void adns__tcp_tryconnect(adns_state ads, struct timeval now) { |
8402e34c |
98 | int r, fd, tries; |
4353a5c4 |
99 | struct sockaddr_in addr; |
100 | struct protoent *proto; |
8402e34c |
101 | |
102 | for (tries=0; tries<ads->nservers; tries++) { |
f7f83b4a |
103 | switch (ads->tcpstate) { |
104 | case server_connecting: |
105 | case server_ok: |
106 | case server_broken: |
107 | return; |
108 | case server_disconnected: |
109 | break; |
110 | default: |
111 | abort(); |
112 | } |
113 | |
4353a5c4 |
114 | assert(!ads->tcpsend.used); |
115 | assert(!ads->tcprecv.used); |
70ad7a2a |
116 | assert(!ads->tcprecv_skip); |
8402e34c |
117 | |
118 | proto= getprotobyname("tcp"); |
609133ee |
119 | if (!proto) { |
120 | adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); |
121 | return; |
122 | } |
8402e34c |
123 | fd= socket(AF_INET,SOCK_STREAM,proto->p_proto); |
4353a5c4 |
124 | if (fd<0) { |
68442019 |
125 | adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno)); |
4353a5c4 |
126 | return; |
127 | } |
128 | r= adns__setnonblock(ads,fd); |
129 | if (r) { |
609133ee |
130 | adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:" |
131 | " %s",strerror(r)); |
4353a5c4 |
132 | close(fd); |
133 | return; |
134 | } |
8402e34c |
135 | memset(&addr,0,sizeof(addr)); |
136 | addr.sin_family= AF_INET; |
98a3f706 |
137 | addr.sin_port= htons(DNS_PORT); |
8402e34c |
138 | addr.sin_addr= ads->servers[ads->tcpserver].addr; |
eec1d9c8 |
139 | r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr)); |
8402e34c |
140 | ads->tcpsocket= fd; |
141 | ads->tcpstate= server_connecting; |
f7f83b4a |
142 | if (r==0) { tcp_connected(ads,now); return; } |
143 | if (errno == EWOULDBLOCK || errno == EINPROGRESS) { |
144 | ads->tcptimeout= now; |
145 | timevaladd(&ads->tcptimeout,TCPCONNMS); |
146 | return; |
147 | } |
ddfda861 |
148 | adns__tcp_broken(ads,"connect",strerror(errno)); |
237ce710 |
149 | tcp_broken_events(ads); |
8402e34c |
150 | } |
151 | } |
152 | |
620c146d |
153 | /* Timeout handling functions. */ |
154 | |
155 | void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, |
156 | struct timeval *tv_buf) { |
157 | const struct timeval *now; |
158 | int r; |
159 | |
160 | now= *now_io; |
161 | if (now) return; |
162 | r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; } |
163 | adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno)); |
164 | adns_globalsystemfailure(ads); |
165 | return; |
166 | } |
4353a5c4 |
167 | |
4fad263d |
168 | static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) { |
169 | struct timeval *rbuf; |
170 | |
171 | if (!tv_io) return; |
172 | |
173 | rbuf= *tv_io; |
174 | if (!rbuf) { *tv_io= rbuf= tvbuf; } |
175 | |
176 | timerclear(rbuf); |
177 | } |
178 | |
4353a5c4 |
179 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
180 | struct timeval maxto) { |
181 | struct timeval *rbuf; |
182 | |
183 | if (!tv_io) return; |
184 | rbuf= *tv_io; |
94436798 |
185 | if (!rbuf) { |
186 | *tvbuf= maxto; *tv_io= tvbuf; |
187 | } else { |
188 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
189 | } |
ffbda80c |
190 | /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", |
191 | maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/ |
4353a5c4 |
192 | } |
193 | |
194 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
195 | struct timeval now, struct timeval maxtime) { |
f7f83b4a |
196 | /* tv_io may be 0 */ |
4353a5c4 |
197 | ldiv_t dr; |
198 | |
ffbda80c |
199 | /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", |
200 | now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/ |
4353a5c4 |
201 | if (!tv_io) return; |
94436798 |
202 | maxtime.tv_sec -= (now.tv_sec+2); |
203 | maxtime.tv_usec -= (now.tv_usec-2000000); |
204 | dr= ldiv(maxtime.tv_usec,1000000); |
4353a5c4 |
205 | maxtime.tv_sec += dr.quot; |
94436798 |
206 | maxtime.tv_usec -= dr.quot*1000000; |
207 | if (maxtime.tv_sec<0) timerclear(&maxtime); |
4353a5c4 |
208 | inter_maxto(tv_io,tvbuf,maxtime); |
209 | } |
210 | |
f7f83b4a |
211 | static void timeouts_queue(adns_state ads, int act, |
212 | struct timeval **tv_io, struct timeval *tvbuf, |
213 | struct timeval now, struct query_queue *queue) { |
4353a5c4 |
214 | adns_query qu, nqu; |
f7f83b4a |
215 | |
216 | for (qu= queue->head; qu; qu= nqu) { |
4353a5c4 |
217 | nqu= qu->next; |
d855b532 |
218 | if (!timercmp(&now,&qu->timeout,>)) { |
620c146d |
219 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
220 | } else { |
4fad263d |
221 | if (!act) { inter_immed(tv_io,tvbuf); return; } |
f7f83b4a |
222 | LIST_UNLINK(*queue,qu); |
d8c062fa |
223 | if (qu->state != query_tosend) { |
3955725c |
224 | adns__query_fail(qu,adns_s_timeout); |
4353a5c4 |
225 | } else { |
d8c062fa |
226 | adns__query_send(qu,now); |
4353a5c4 |
227 | } |
f7f83b4a |
228 | nqu= queue->head; |
4353a5c4 |
229 | } |
230 | } |
f7f83b4a |
231 | } |
232 | |
233 | static void tcp_events(adns_state ads, int act, |
234 | struct timeval **tv_io, struct timeval *tvbuf, |
235 | struct timeval now) { |
f7f83b4a |
236 | for (;;) { |
237 | switch (ads->tcpstate) { |
238 | case server_broken: |
4fad263d |
239 | if (!act) { inter_immed(tv_io,tvbuf); return; } |
237ce710 |
240 | tcp_broken_events(ads); |
f7f83b4a |
241 | case server_disconnected: /* fall through */ |
242 | if (!ads->tcpw.head) return; |
4fad263d |
243 | if (!act) { inter_immed(tv_io,tvbuf); return; } |
f7f83b4a |
244 | adns__tcp_tryconnect(ads,now); |
245 | break; |
246 | case server_ok: |
247 | if (ads->tcpw.head) return; |
248 | if (!ads->tcptimeout.tv_sec) { |
249 | assert(!ads->tcptimeout.tv_usec); |
250 | ads->tcptimeout= now; |
251 | timevaladd(&ads->tcptimeout,TCPIDLEMS); |
252 | } |
253 | case server_connecting: /* fall through */ |
4fad263d |
254 | if (!act || !timercmp(&now,&ads->tcptimeout,>)) { |
f7f83b4a |
255 | inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout); |
256 | return; |
257 | } { |
258 | /* TCP timeout has happened */ |
259 | switch (ads->tcpstate) { |
260 | case server_connecting: /* failed to connect */ |
261 | adns__tcp_broken(ads,"unable to make connection","timed out"); |
262 | break; |
263 | case server_ok: /* idle timeout */ |
264 | tcp_close(ads); |
265 | ads->tcpstate= server_disconnected; |
266 | return; |
267 | default: |
268 | abort(); |
269 | } |
270 | } |
271 | break; |
272 | default: |
273 | abort(); |
274 | } |
275 | } |
4fad263d |
276 | return; |
f7f83b4a |
277 | } |
278 | |
279 | void adns__timeouts(adns_state ads, int act, |
280 | struct timeval **tv_io, struct timeval *tvbuf, |
281 | struct timeval now) { |
282 | timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw); |
283 | timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw); |
284 | tcp_events(ads,act,tv_io,tvbuf,now); |
285 | } |
94436798 |
286 | |
620c146d |
287 | void adns_firsttimeout(adns_state ads, |
288 | struct timeval **tv_io, struct timeval *tvbuf, |
289 | struct timeval now) { |
28de6442 |
290 | adns__consistency(ads,0,cc_entex); |
620c146d |
291 | adns__timeouts(ads, 0, tv_io,tvbuf, now); |
28de6442 |
292 | adns__consistency(ads,0,cc_entex); |
620c146d |
293 | } |
294 | |
295 | void adns_processtimeouts(adns_state ads, const struct timeval *now) { |
296 | struct timeval tv_buf; |
297 | |
28de6442 |
298 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
299 | adns__must_gettimeofday(ads,&now,&tv_buf); |
300 | if (now) adns__timeouts(ads, 1, 0,0, *now); |
28de6442 |
301 | adns__consistency(ads,0,cc_entex); |
a8768e80 |
302 | } |
303 | |
620c146d |
304 | /* fd handling functions. These are the top-level of the real work of |
305 | * reception and often transmission. |
306 | */ |
307 | |
308 | int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) { |
309 | /* Returns the number of entries filled in. Always zeroes revents. */ |
310 | |
311 | assert(MAX_POLLFDS==2); |
a8768e80 |
312 | |
620c146d |
313 | pollfds_buf[0].fd= ads->udpsocket; |
314 | pollfds_buf[0].events= POLLIN; |
315 | pollfds_buf[0].revents= 0; |
4353a5c4 |
316 | |
317 | switch (ads->tcpstate) { |
318 | case server_disconnected: |
6c68a593 |
319 | case server_broken: |
620c146d |
320 | return 1; |
4353a5c4 |
321 | case server_connecting: |
620c146d |
322 | pollfds_buf[1].events= POLLOUT; |
4353a5c4 |
323 | break; |
324 | case server_ok: |
609133ee |
325 | pollfds_buf[1].events= |
326 | ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI; |
e062dcae |
327 | break; |
4353a5c4 |
328 | default: |
329 | abort(); |
330 | } |
620c146d |
331 | pollfds_buf[1].fd= ads->tcpsocket; |
620c146d |
332 | return 2; |
4353a5c4 |
333 | } |
334 | |
620c146d |
335 | int adns_processreadable(adns_state ads, int fd, const struct timeval *now) { |
70ad7a2a |
336 | int want, dgramlen, r, udpaddrlen, serv, old_skip; |
98a3f706 |
337 | byte udpbuf[DNS_MAXUDP]; |
37e28fde |
338 | struct sockaddr_in udpaddr; |
620c146d |
339 | |
28de6442 |
340 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
341 | |
ddfda861 |
342 | switch (ads->tcpstate) { |
343 | case server_disconnected: |
fb901bf5 |
344 | case server_broken: |
ddfda861 |
345 | case server_connecting: |
ddfda861 |
346 | break; |
347 | case server_ok: |
620c146d |
348 | if (fd != ads->tcpsocket) break; |
70ad7a2a |
349 | assert(!ads->tcprecv_skip); |
428bd682 |
350 | do { |
70ad7a2a |
351 | if (ads->tcprecv.used >= ads->tcprecv_skip+2) { |
352 | dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) | |
353 | ads->tcprecv.buf[ads->tcprecv_skip+1]); |
354 | if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) { |
355 | old_skip= ads->tcprecv_skip; |
356 | ads->tcprecv_skip += 2+dgramlen; |
357 | adns__procdgram(ads, ads->tcprecv.buf+old_skip+2, |
358 | dgramlen, ads->tcpserver, 1,*now); |
359 | continue; |
656b2da9 |
360 | } else { |
70ad7a2a |
361 | want= 2+dgramlen; |
656b2da9 |
362 | } |
70ad7a2a |
363 | } else { |
364 | want= 2; |
656b2da9 |
365 | } |
70ad7a2a |
366 | ads->tcprecv.used -= ads->tcprecv_skip; |
609133ee |
367 | memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip, |
368 | ads->tcprecv.used); |
70ad7a2a |
369 | ads->tcprecv_skip= 0; |
3e2e5fab |
370 | if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; } |
620c146d |
371 | assert(ads->tcprecv.used <= ads->tcprecv.avail); |
372 | if (ads->tcprecv.used == ads->tcprecv.avail) continue; |
373 | r= read(ads->tcpsocket, |
374 | ads->tcprecv.buf+ads->tcprecv.used, |
375 | ads->tcprecv.avail-ads->tcprecv.used); |
376 | if (r>0) { |
377 | ads->tcprecv.used+= r; |
378 | } else { |
379 | if (r) { |
3e2e5fab |
380 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
620c146d |
381 | if (errno==EINTR) continue; |
3e2e5fab |
382 | if (errno_resources(errno)) { r= errno; goto xit; } |
656b2da9 |
383 | } |
620c146d |
384 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
656b2da9 |
385 | } |
428bd682 |
386 | } while (ads->tcpstate == server_ok); |
387 | r= 0; goto xit; |
ddfda861 |
388 | default: |
389 | abort(); |
656b2da9 |
390 | } |
620c146d |
391 | if (fd == ads->udpsocket) { |
37e28fde |
392 | for (;;) { |
393 | udpaddrlen= sizeof(udpaddr); |
eec1d9c8 |
394 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0, |
395 | (struct sockaddr*)&udpaddr,&udpaddrlen); |
37e28fde |
396 | if (r<0) { |
3e2e5fab |
397 | if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; } |
620c146d |
398 | if (errno == EINTR) continue; |
3e2e5fab |
399 | if (errno_resources(errno)) { r= errno; goto xit; } |
620c146d |
400 | adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno)); |
3e2e5fab |
401 | r= 0; goto xit; |
37e28fde |
402 | } |
403 | if (udpaddrlen != sizeof(udpaddr)) { |
68442019 |
404 | adns__diag(ads,-1,0,"datagram received with wrong address length %d" |
5a0be244 |
405 | " (expected %lu)", udpaddrlen, |
406 | (unsigned long)sizeof(udpaddr)); |
37e28fde |
407 | continue; |
408 | } |
409 | if (udpaddr.sin_family != AF_INET) { |
68442019 |
410 | adns__diag(ads,-1,0,"datagram received with wrong protocol family" |
4353a5c4 |
411 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
37e28fde |
412 | continue; |
413 | } |
98a3f706 |
414 | if (ntohs(udpaddr.sin_port) != DNS_PORT) { |
609133ee |
415 | adns__diag(ads,-1,0,"datagram received from wrong port" |
416 | " %u (expected %u)", ntohs(udpaddr.sin_port),DNS_PORT); |
37e28fde |
417 | continue; |
418 | } |
419 | for (serv= 0; |
420 | serv < ads->nservers && |
421 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
422 | serv++); |
423 | if (serv >= ads->nservers) { |
68442019 |
424 | adns__warn(ads,-1,0,"datagram received from unknown nameserver %s", |
4353a5c4 |
425 | inet_ntoa(udpaddr.sin_addr)); |
37e28fde |
426 | continue; |
427 | } |
ebf4877a |
428 | adns__procdgram(ads,udpbuf,r,serv,0,*now); |
656b2da9 |
429 | } |
37e28fde |
430 | } |
3e2e5fab |
431 | r= 0; |
432 | xit: |
28de6442 |
433 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
434 | return r; |
656b2da9 |
435 | } |
656b2da9 |
436 | |
620c146d |
437 | int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) { |
656b2da9 |
438 | int r; |
620c146d |
439 | |
28de6442 |
440 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
441 | |
620c146d |
442 | switch (ads->tcpstate) { |
443 | case server_disconnected: |
fb901bf5 |
444 | case server_broken: |
620c146d |
445 | break; |
446 | case server_connecting: |
447 | if (fd != ads->tcpsocket) break; |
448 | assert(ads->tcprecv.used==0); |
70ad7a2a |
449 | assert(ads->tcprecv_skip==0); |
620c146d |
450 | for (;;) { |
3e2e5fab |
451 | if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; } |
620c146d |
452 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
453 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
454 | tcp_connected(ads,*now); |
3e2e5fab |
455 | r= 0; goto xit; |
620c146d |
456 | } |
457 | if (r>0) { |
458 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
3e2e5fab |
459 | r= 0; goto xit; |
620c146d |
460 | } |
461 | if (errno==EINTR) continue; |
3e2e5fab |
462 | if (errno_resources(errno)) { r= errno; goto xit; } |
620c146d |
463 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
3e2e5fab |
464 | r= 0; goto xit; |
620c146d |
465 | } /* not reached */ |
466 | case server_ok: |
fb901bf5 |
467 | if (fd != ads->tcpsocket) break; |
468 | while (ads->tcpsend.used) { |
620c146d |
469 | adns__sigpipe_protect(ads); |
470 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
471 | adns__sigpipe_unprotect(ads); |
472 | if (r<0) { |
473 | if (errno==EINTR) continue; |
3e2e5fab |
474 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
475 | if (errno_resources(errno)) { r= errno; goto xit; } |
620c146d |
476 | adns__tcp_broken(ads,"write",strerror(errno)); |
3e2e5fab |
477 | r= 0; goto xit; |
620c146d |
478 | } else if (r>0) { |
479 | ads->tcpsend.used -= r; |
480 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
481 | } |
fb901bf5 |
482 | } |
483 | r= 0; |
484 | goto xit; |
620c146d |
485 | default: |
486 | abort(); |
487 | } |
3e2e5fab |
488 | r= 0; |
489 | xit: |
28de6442 |
490 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
491 | return r; |
620c146d |
492 | } |
493 | |
609133ee |
494 | int adns_processexceptional(adns_state ads, int fd, |
495 | const struct timeval *now) { |
28de6442 |
496 | adns__consistency(ads,0,cc_entex); |
620c146d |
497 | switch (ads->tcpstate) { |
498 | case server_disconnected: |
fb901bf5 |
499 | case server_broken: |
620c146d |
500 | break; |
501 | case server_connecting: |
502 | case server_ok: |
503 | if (fd != ads->tcpsocket) break; |
504 | adns__tcp_broken(ads,"poll/select","exceptional condition detected"); |
3e2e5fab |
505 | break; |
620c146d |
506 | default: |
507 | abort(); |
508 | } |
28de6442 |
509 | adns__consistency(ads,0,cc_entex); |
620c146d |
510 | return 0; |
511 | } |
37e28fde |
512 | |
620c146d |
513 | static void fd_event(adns_state ads, int fd, |
514 | int revent, int pollflag, |
515 | int maxfd, const fd_set *fds, |
609133ee |
516 | int (*func)(adns_state, int fd, |
517 | const struct timeval *now), |
620c146d |
518 | struct timeval now, int *r_r) { |
656b2da9 |
519 | int r; |
620c146d |
520 | |
521 | if (!(revent & pollflag)) return; |
522 | if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return; |
523 | r= func(ads,fd,&now); |
524 | if (r) { |
525 | if (r_r) { |
526 | *r_r= r; |
527 | } else { |
609133ee |
528 | adns__diag(ads,-1,0,"process fd failed after select:" |
529 | " %s",strerror(errno)); |
620c146d |
530 | adns_globalsystemfailure(ads); |
531 | } |
532 | } |
533 | } |
534 | |
535 | void adns__fdevents(adns_state ads, |
536 | const struct pollfd *pollfds, int npollfds, |
537 | int maxfd, const fd_set *readfds, |
538 | const fd_set *writefds, const fd_set *exceptfds, |
539 | struct timeval now, int *r_r) { |
540 | int i, fd, revents; |
541 | |
542 | for (i=0; i<npollfds; i++) { |
543 | fd= pollfds[i].fd; |
544 | if (fd >= maxfd) maxfd= fd+1; |
545 | revents= pollfds[i].revents; |
609133ee |
546 | #define EV(pollfl,fds,how) \ |
547 | fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r) |
548 | EV( POLLIN, readfds, readable ); |
549 | EV( POLLOUT, writefds, writeable ); |
550 | EV( POLLPRI, exceptfds, exceptional ); |
551 | #undef EV |
620c146d |
552 | } |
553 | } |
37e28fde |
554 | |
620c146d |
555 | /* Wrappers for select(2). */ |
556 | |
557 | void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io, |
558 | fd_set *writefds_io, fd_set *exceptfds_io, |
559 | struct timeval **tv_mod, struct timeval *tv_tobuf, |
560 | const struct timeval *now) { |
561 | struct timeval tv_nowbuf; |
562 | struct pollfd pollfds[MAX_POLLFDS]; |
563 | int i, fd, maxfd, npollfds; |
564 | |
28de6442 |
565 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
566 | |
620c146d |
567 | if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) { |
568 | /* The caller is planning to sleep. */ |
569 | adns__must_gettimeofday(ads,&now,&tv_nowbuf); |
26cc07a3 |
570 | if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; } |
6c68a593 |
571 | adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now); |
620c146d |
572 | } |
573 | |
574 | npollfds= adns__pollfds(ads,pollfds); |
575 | maxfd= *maxfd_io; |
576 | for (i=0; i<npollfds; i++) { |
577 | fd= pollfds[i].fd; |
578 | if (fd >= maxfd) maxfd= fd+1; |
579 | if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io); |
580 | if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io); |
581 | if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io); |
582 | } |
583 | *maxfd_io= maxfd; |
3e2e5fab |
584 | |
585 | xit: |
28de6442 |
586 | adns__consistency(ads,0,cc_entex); |
620c146d |
587 | } |
588 | |
589 | void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds, |
590 | const fd_set *writefds, const fd_set *exceptfds, |
591 | const struct timeval *now) { |
fc6a52ae |
592 | struct timeval tv_buf; |
620c146d |
593 | struct pollfd pollfds[MAX_POLLFDS]; |
fc6a52ae |
594 | int npollfds, i; |
620c146d |
595 | |
28de6442 |
596 | adns__consistency(ads,0,cc_entex); |
fc6a52ae |
597 | adns__must_gettimeofday(ads,&now,&tv_buf); |
3e2e5fab |
598 | if (!now) goto xit; |
620c146d |
599 | adns_processtimeouts(ads,now); |
600 | |
601 | npollfds= adns__pollfds(ads,pollfds); |
fc6a52ae |
602 | for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI; |
620c146d |
603 | adns__fdevents(ads, |
604 | pollfds,npollfds, |
605 | maxfd,readfds,writefds,exceptfds, |
606 | *now, 0); |
3e2e5fab |
607 | xit: |
28de6442 |
608 | adns__consistency(ads,0,cc_entex); |
620c146d |
609 | } |
610 | |
611 | /* General helpful functions. */ |
612 | |
613 | void adns_globalsystemfailure(adns_state ads) { |
28de6442 |
614 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
615 | |
f7f83b4a |
616 | while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail); |
617 | while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail); |
620c146d |
618 | |
619 | switch (ads->tcpstate) { |
620 | case server_connecting: |
621 | case server_ok: |
f7f83b4a |
622 | adns__tcp_broken(ads,0,0); |
620c146d |
623 | break; |
624 | case server_disconnected: |
fb901bf5 |
625 | case server_broken: |
620c146d |
626 | break; |
627 | default: |
628 | abort(); |
629 | } |
28de6442 |
630 | adns__consistency(ads,0,cc_entex); |
656b2da9 |
631 | } |
632 | |
620c146d |
633 | int adns_processany(adns_state ads) { |
aedf10f2 |
634 | int r, i; |
620c146d |
635 | struct timeval now; |
636 | struct pollfd pollfds[MAX_POLLFDS]; |
637 | int npollfds; |
638 | |
28de6442 |
639 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
640 | |
620c146d |
641 | r= gettimeofday(&now,0); |
642 | if (!r) adns_processtimeouts(ads,&now); |
643 | |
25913ee2 |
644 | /* We just use adns__fdevents to loop over the fd's trying them. |
645 | * This seems more sensible than calling select, since we're most |
646 | * likely just to want to do a read on one or two fds anyway. |
647 | */ |
620c146d |
648 | npollfds= adns__pollfds(ads,pollfds); |
1ed3dba4 |
649 | for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI; |
620c146d |
650 | adns__fdevents(ads, |
651 | pollfds,npollfds, |
652 | 0,0,0,0, |
653 | now,&r); |
3e2e5fab |
654 | |
28de6442 |
655 | adns__consistency(ads,0,cc_entex); |
c118a725 |
656 | return 0; |
620c146d |
657 | } |
ddfda861 |
658 | |
4353a5c4 |
659 | void adns__autosys(adns_state ads, struct timeval now) { |
ddfda861 |
660 | if (ads->iflags & adns_if_noautosys) return; |
620c146d |
661 | adns_processany(ads); |
ddfda861 |
662 | } |
663 | |
940356bd |
664 | int adns__internal_check(adns_state ads, |
665 | adns_query *query_io, |
666 | adns_answer **answer, |
667 | void **context_r) { |
656b2da9 |
668 | adns_query qu; |
669 | |
670 | qu= *query_io; |
671 | if (!qu) { |
4ae90a83 |
672 | if (ads->output.head) { |
673 | qu= ads->output.head; |
f7f83b4a |
674 | } else if (ads->udpw.head || ads->tcpw.head) { |
4ae90a83 |
675 | return EAGAIN; |
676 | } else { |
677 | return ESRCH; |
678 | } |
656b2da9 |
679 | } else { |
226c5eef |
680 | if (qu->id>=0) return EAGAIN; |
656b2da9 |
681 | } |
682 | LIST_UNLINK(ads->output,qu); |
8e5b0abb |
683 | *answer= qu->answer; |
a6536d8b |
684 | if (context_r) *context_r= qu->ctx.ext; |
8ce38e76 |
685 | *query_io= qu; |
656b2da9 |
686 | free(qu); |
687 | return 0; |
688 | } |
689 | |
690 | int adns_wait(adns_state ads, |
691 | adns_query *query_io, |
692 | adns_answer **answer_r, |
693 | void **context_r) { |
620c146d |
694 | int r, maxfd, rsel; |
656b2da9 |
695 | fd_set readfds, writefds, exceptfds; |
26cc07a3 |
696 | struct timeval tvbuf, *tvp; |
656b2da9 |
697 | |
28de6442 |
698 | adns__consistency(ads,*query_io,cc_entex); |
656b2da9 |
699 | for (;;) { |
940356bd |
700 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
226c5eef |
701 | if (r != EAGAIN) break; |
656b2da9 |
702 | maxfd= 0; tvp= 0; |
37e28fde |
703 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
620c146d |
704 | adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0); |
4fad263d |
705 | assert(tvp); |
656b2da9 |
706 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
88372443 |
707 | if (rsel==-1) { |
620c146d |
708 | if (errno == EINTR) { |
3e2e5fab |
709 | if (ads->iflags & adns_if_eintr) { r= EINTR; break; } |
620c146d |
710 | } else { |
711 | adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno)); |
712 | adns_globalsystemfailure(ads); |
713 | } |
714 | } else { |
715 | assert(rsel >= 0); |
716 | adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0); |
88372443 |
717 | } |
656b2da9 |
718 | } |
28de6442 |
719 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
720 | return r; |
656b2da9 |
721 | } |
722 | |
723 | int adns_check(adns_state ads, |
724 | adns_query *query_io, |
725 | adns_answer **answer_r, |
726 | void **context_r) { |
4353a5c4 |
727 | struct timeval now; |
728 | int r; |
729 | |
28de6442 |
730 | adns__consistency(ads,*query_io,cc_entex); |
620c146d |
731 | r= gettimeofday(&now,0); |
732 | if (!r) adns__autosys(ads,now); |
3e2e5fab |
733 | |
940356bd |
734 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
28de6442 |
735 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
736 | return r; |
656b2da9 |
737 | } |