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