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 | |
146 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
147 | struct timeval maxto) { |
148 | struct timeval *rbuf; |
149 | |
150 | if (!tv_io) return; |
151 | rbuf= *tv_io; |
94436798 |
152 | if (!rbuf) { |
153 | *tvbuf= maxto; *tv_io= tvbuf; |
154 | } else { |
155 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
156 | } |
ffbda80c |
157 | /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", |
158 | maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/ |
4353a5c4 |
159 | } |
160 | |
161 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
162 | struct timeval now, struct timeval maxtime) { |
f7f83b4a |
163 | /* tv_io may be 0 */ |
4353a5c4 |
164 | ldiv_t dr; |
165 | |
ffbda80c |
166 | /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", |
167 | now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/ |
4353a5c4 |
168 | if (!tv_io) return; |
94436798 |
169 | maxtime.tv_sec -= (now.tv_sec+2); |
170 | maxtime.tv_usec -= (now.tv_usec-2000000); |
171 | dr= ldiv(maxtime.tv_usec,1000000); |
4353a5c4 |
172 | maxtime.tv_sec += dr.quot; |
94436798 |
173 | maxtime.tv_usec -= dr.quot*1000000; |
174 | if (maxtime.tv_sec<0) timerclear(&maxtime); |
4353a5c4 |
175 | inter_maxto(tv_io,tvbuf,maxtime); |
176 | } |
177 | |
f7f83b4a |
178 | static void timeouts_queue(adns_state ads, int act, |
179 | struct timeval **tv_io, struct timeval *tvbuf, |
180 | struct timeval now, struct query_queue *queue) { |
4353a5c4 |
181 | adns_query qu, nqu; |
f7f83b4a |
182 | |
183 | for (qu= queue->head; qu; qu= nqu) { |
4353a5c4 |
184 | nqu= qu->next; |
d855b532 |
185 | if (!timercmp(&now,&qu->timeout,>)) { |
620c146d |
186 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
187 | } else { |
3905bfc2 |
188 | if (!act) { |
6a9f9437 |
189 | tvbuf->tv_sec= 0; |
190 | tvbuf->tv_usec= 0; |
191 | *tv_io= tvbuf; |
3905bfc2 |
192 | return; |
193 | } |
f7f83b4a |
194 | LIST_UNLINK(*queue,qu); |
d8c062fa |
195 | if (qu->state != query_tosend) { |
3955725c |
196 | adns__query_fail(qu,adns_s_timeout); |
4353a5c4 |
197 | } else { |
d8c062fa |
198 | adns__query_send(qu,now); |
4353a5c4 |
199 | } |
f7f83b4a |
200 | nqu= queue->head; |
4353a5c4 |
201 | } |
202 | } |
f7f83b4a |
203 | } |
204 | |
205 | static void tcp_events(adns_state ads, int act, |
206 | struct timeval **tv_io, struct timeval *tvbuf, |
207 | struct timeval now) { |
208 | adns_query qu, nqu; |
209 | |
210 | for (;;) { |
211 | switch (ads->tcpstate) { |
212 | case server_broken: |
213 | for (qu= ads->tcpw.head; qu; qu= nqu) { |
214 | nqu= qu->next; |
215 | assert(qu->state == query_tcpw); |
216 | if (qu->retries > ads->nservers) { |
217 | LIST_UNLINK(ads->tcpw,qu); |
218 | adns__query_fail(qu,adns_s_allservfail); |
219 | } |
220 | } |
221 | ads->tcpstate= server_disconnected; |
222 | case server_disconnected: /* fall through */ |
223 | if (!ads->tcpw.head) return; |
224 | adns__tcp_tryconnect(ads,now); |
225 | break; |
226 | case server_ok: |
227 | if (ads->tcpw.head) return; |
228 | if (!ads->tcptimeout.tv_sec) { |
229 | assert(!ads->tcptimeout.tv_usec); |
230 | ads->tcptimeout= now; |
231 | timevaladd(&ads->tcptimeout,TCPIDLEMS); |
232 | } |
233 | case server_connecting: /* fall through */ |
234 | if (!timercmp(&now,&ads->tcptimeout,>)) { |
235 | inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout); |
236 | return; |
237 | } { |
238 | /* TCP timeout has happened */ |
239 | switch (ads->tcpstate) { |
240 | case server_connecting: /* failed to connect */ |
241 | adns__tcp_broken(ads,"unable to make connection","timed out"); |
242 | break; |
243 | case server_ok: /* idle timeout */ |
244 | tcp_close(ads); |
245 | ads->tcpstate= server_disconnected; |
246 | return; |
247 | default: |
248 | abort(); |
249 | } |
250 | } |
251 | break; |
252 | default: |
253 | abort(); |
254 | } |
255 | } |
256 | } |
257 | |
258 | void adns__timeouts(adns_state ads, int act, |
259 | struct timeval **tv_io, struct timeval *tvbuf, |
260 | struct timeval now) { |
261 | timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw); |
262 | timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw); |
263 | tcp_events(ads,act,tv_io,tvbuf,now); |
264 | } |
94436798 |
265 | |
620c146d |
266 | void adns_firsttimeout(adns_state ads, |
267 | struct timeval **tv_io, struct timeval *tvbuf, |
268 | struct timeval now) { |
28de6442 |
269 | adns__consistency(ads,0,cc_entex); |
620c146d |
270 | adns__timeouts(ads, 0, tv_io,tvbuf, now); |
28de6442 |
271 | adns__consistency(ads,0,cc_entex); |
620c146d |
272 | } |
273 | |
274 | void adns_processtimeouts(adns_state ads, const struct timeval *now) { |
275 | struct timeval tv_buf; |
276 | |
28de6442 |
277 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
278 | adns__must_gettimeofday(ads,&now,&tv_buf); |
279 | if (now) adns__timeouts(ads, 1, 0,0, *now); |
28de6442 |
280 | adns__consistency(ads,0,cc_entex); |
a8768e80 |
281 | } |
282 | |
620c146d |
283 | /* fd handling functions. These are the top-level of the real work of |
284 | * reception and often transmission. |
285 | */ |
286 | |
287 | int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) { |
288 | /* Returns the number of entries filled in. Always zeroes revents. */ |
289 | |
290 | assert(MAX_POLLFDS==2); |
a8768e80 |
291 | |
620c146d |
292 | pollfds_buf[0].fd= ads->udpsocket; |
293 | pollfds_buf[0].events= POLLIN; |
294 | pollfds_buf[0].revents= 0; |
4353a5c4 |
295 | |
296 | switch (ads->tcpstate) { |
297 | case server_disconnected: |
620c146d |
298 | return 1; |
4353a5c4 |
299 | case server_connecting: |
620c146d |
300 | pollfds_buf[1].events= POLLOUT; |
4353a5c4 |
301 | break; |
302 | case server_ok: |
620c146d |
303 | pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI; |
e062dcae |
304 | break; |
4353a5c4 |
305 | default: |
306 | abort(); |
307 | } |
620c146d |
308 | pollfds_buf[1].fd= ads->tcpsocket; |
620c146d |
309 | return 2; |
4353a5c4 |
310 | } |
311 | |
620c146d |
312 | int adns_processreadable(adns_state ads, int fd, const struct timeval *now) { |
70ad7a2a |
313 | int want, dgramlen, r, udpaddrlen, serv, old_skip; |
98a3f706 |
314 | byte udpbuf[DNS_MAXUDP]; |
37e28fde |
315 | struct sockaddr_in udpaddr; |
620c146d |
316 | |
28de6442 |
317 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
318 | |
ddfda861 |
319 | switch (ads->tcpstate) { |
320 | case server_disconnected: |
ddfda861 |
321 | case server_connecting: |
ddfda861 |
322 | break; |
323 | case server_ok: |
620c146d |
324 | if (fd != ads->tcpsocket) break; |
70ad7a2a |
325 | assert(!ads->tcprecv_skip); |
620c146d |
326 | for (;;) { |
70ad7a2a |
327 | if (ads->tcprecv.used >= ads->tcprecv_skip+2) { |
328 | dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) | |
329 | ads->tcprecv.buf[ads->tcprecv_skip+1]); |
330 | if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) { |
331 | old_skip= ads->tcprecv_skip; |
332 | ads->tcprecv_skip += 2+dgramlen; |
333 | adns__procdgram(ads, ads->tcprecv.buf+old_skip+2, |
334 | dgramlen, ads->tcpserver, 1,*now); |
335 | continue; |
656b2da9 |
336 | } else { |
70ad7a2a |
337 | want= 2+dgramlen; |
656b2da9 |
338 | } |
70ad7a2a |
339 | } else { |
340 | want= 2; |
656b2da9 |
341 | } |
70ad7a2a |
342 | ads->tcprecv.used -= ads->tcprecv_skip; |
343 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+ads->tcprecv_skip,ads->tcprecv.used); |
344 | ads->tcprecv_skip= 0; |
3e2e5fab |
345 | if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; } |
620c146d |
346 | assert(ads->tcprecv.used <= ads->tcprecv.avail); |
347 | if (ads->tcprecv.used == ads->tcprecv.avail) continue; |
348 | r= read(ads->tcpsocket, |
349 | ads->tcprecv.buf+ads->tcprecv.used, |
350 | ads->tcprecv.avail-ads->tcprecv.used); |
351 | if (r>0) { |
352 | ads->tcprecv.used+= r; |
353 | } else { |
354 | if (r) { |
3e2e5fab |
355 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
620c146d |
356 | if (errno==EINTR) continue; |
3e2e5fab |
357 | if (errno_resources(errno)) { r= errno; goto xit; } |
656b2da9 |
358 | } |
620c146d |
359 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
3e2e5fab |
360 | r= 0; goto xit; |
656b2da9 |
361 | } |
620c146d |
362 | } /* never reached */ |
ddfda861 |
363 | default: |
364 | abort(); |
656b2da9 |
365 | } |
620c146d |
366 | if (fd == ads->udpsocket) { |
37e28fde |
367 | for (;;) { |
368 | udpaddrlen= sizeof(udpaddr); |
eec1d9c8 |
369 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0, |
370 | (struct sockaddr*)&udpaddr,&udpaddrlen); |
37e28fde |
371 | if (r<0) { |
3e2e5fab |
372 | if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; } |
620c146d |
373 | if (errno == EINTR) continue; |
3e2e5fab |
374 | if (errno_resources(errno)) { r= errno; goto xit; } |
620c146d |
375 | adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno)); |
3e2e5fab |
376 | r= 0; goto xit; |
37e28fde |
377 | } |
378 | if (udpaddrlen != sizeof(udpaddr)) { |
68442019 |
379 | adns__diag(ads,-1,0,"datagram received with wrong address length %d" |
380 | " (expected %d)", udpaddrlen,sizeof(udpaddr)); |
37e28fde |
381 | continue; |
382 | } |
383 | if (udpaddr.sin_family != AF_INET) { |
68442019 |
384 | adns__diag(ads,-1,0,"datagram received with wrong protocol family" |
4353a5c4 |
385 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
37e28fde |
386 | continue; |
387 | } |
98a3f706 |
388 | if (ntohs(udpaddr.sin_port) != DNS_PORT) { |
68442019 |
389 | adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)", |
98a3f706 |
390 | ntohs(udpaddr.sin_port),DNS_PORT); |
37e28fde |
391 | continue; |
392 | } |
393 | for (serv= 0; |
394 | serv < ads->nservers && |
395 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
396 | serv++); |
397 | if (serv >= ads->nservers) { |
68442019 |
398 | adns__warn(ads,-1,0,"datagram received from unknown nameserver %s", |
4353a5c4 |
399 | inet_ntoa(udpaddr.sin_addr)); |
37e28fde |
400 | continue; |
401 | } |
ebf4877a |
402 | adns__procdgram(ads,udpbuf,r,serv,0,*now); |
656b2da9 |
403 | } |
37e28fde |
404 | } |
3e2e5fab |
405 | r= 0; |
406 | xit: |
28de6442 |
407 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
408 | return r; |
656b2da9 |
409 | } |
656b2da9 |
410 | |
620c146d |
411 | int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) { |
656b2da9 |
412 | int r; |
620c146d |
413 | |
28de6442 |
414 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
415 | |
620c146d |
416 | switch (ads->tcpstate) { |
417 | case server_disconnected: |
418 | break; |
419 | case server_connecting: |
420 | if (fd != ads->tcpsocket) break; |
421 | assert(ads->tcprecv.used==0); |
70ad7a2a |
422 | assert(ads->tcprecv_skip==0); |
620c146d |
423 | for (;;) { |
3e2e5fab |
424 | if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; } |
620c146d |
425 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
426 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
427 | tcp_connected(ads,*now); |
3e2e5fab |
428 | r= 0; goto xit; |
620c146d |
429 | } |
430 | if (r>0) { |
431 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
3e2e5fab |
432 | r= 0; goto xit; |
620c146d |
433 | } |
434 | if (errno==EINTR) continue; |
3e2e5fab |
435 | if (errno_resources(errno)) { r= errno; goto xit; } |
620c146d |
436 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
3e2e5fab |
437 | r= 0; goto xit; |
620c146d |
438 | } /* not reached */ |
439 | case server_ok: |
440 | if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break; |
441 | for (;;) { |
442 | adns__sigpipe_protect(ads); |
443 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
444 | adns__sigpipe_unprotect(ads); |
445 | if (r<0) { |
446 | if (errno==EINTR) continue; |
3e2e5fab |
447 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
448 | if (errno_resources(errno)) { r= errno; goto xit; } |
620c146d |
449 | adns__tcp_broken(ads,"write",strerror(errno)); |
3e2e5fab |
450 | r= 0; goto xit; |
620c146d |
451 | } else if (r>0) { |
452 | ads->tcpsend.used -= r; |
453 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
454 | } |
455 | } /* not reached */ |
456 | default: |
457 | abort(); |
458 | } |
3e2e5fab |
459 | r= 0; |
460 | xit: |
28de6442 |
461 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
462 | return r; |
620c146d |
463 | } |
464 | |
465 | int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) { |
28de6442 |
466 | adns__consistency(ads,0,cc_entex); |
620c146d |
467 | switch (ads->tcpstate) { |
468 | case server_disconnected: |
469 | break; |
470 | case server_connecting: |
471 | case server_ok: |
472 | if (fd != ads->tcpsocket) break; |
473 | adns__tcp_broken(ads,"poll/select","exceptional condition detected"); |
3e2e5fab |
474 | break; |
620c146d |
475 | default: |
476 | abort(); |
477 | } |
28de6442 |
478 | adns__consistency(ads,0,cc_entex); |
620c146d |
479 | return 0; |
480 | } |
37e28fde |
481 | |
620c146d |
482 | static void fd_event(adns_state ads, int fd, |
483 | int revent, int pollflag, |
484 | int maxfd, const fd_set *fds, |
485 | int (*func)(adns_state, int fd, const struct timeval *now), |
486 | struct timeval now, int *r_r) { |
656b2da9 |
487 | int r; |
620c146d |
488 | |
489 | if (!(revent & pollflag)) return; |
490 | if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return; |
491 | r= func(ads,fd,&now); |
492 | if (r) { |
493 | if (r_r) { |
494 | *r_r= r; |
495 | } else { |
496 | adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno)); |
497 | adns_globalsystemfailure(ads); |
498 | } |
499 | } |
500 | } |
501 | |
502 | void adns__fdevents(adns_state ads, |
503 | const struct pollfd *pollfds, int npollfds, |
504 | int maxfd, const fd_set *readfds, |
505 | const fd_set *writefds, const fd_set *exceptfds, |
506 | struct timeval now, int *r_r) { |
507 | int i, fd, revents; |
508 | |
509 | for (i=0; i<npollfds; i++) { |
510 | fd= pollfds[i].fd; |
511 | if (fd >= maxfd) maxfd= fd+1; |
512 | revents= pollfds[i].revents; |
513 | fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r); |
514 | fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r); |
515 | fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r); |
516 | } |
517 | } |
37e28fde |
518 | |
620c146d |
519 | /* Wrappers for select(2). */ |
520 | |
521 | void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io, |
522 | fd_set *writefds_io, fd_set *exceptfds_io, |
523 | struct timeval **tv_mod, struct timeval *tv_tobuf, |
524 | const struct timeval *now) { |
525 | struct timeval tv_nowbuf; |
526 | struct pollfd pollfds[MAX_POLLFDS]; |
527 | int i, fd, maxfd, npollfds; |
528 | |
28de6442 |
529 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
530 | |
620c146d |
531 | if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) { |
532 | /* The caller is planning to sleep. */ |
533 | adns__must_gettimeofday(ads,&now,&tv_nowbuf); |
6cfbe3cb |
534 | if (!now) { inter_immed(tv_mod,tv_iobuf); goto xit; } |
620c146d |
535 | adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now); |
536 | } |
537 | |
538 | npollfds= adns__pollfds(ads,pollfds); |
539 | maxfd= *maxfd_io; |
540 | for (i=0; i<npollfds; i++) { |
541 | fd= pollfds[i].fd; |
542 | if (fd >= maxfd) maxfd= fd+1; |
543 | if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io); |
544 | if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io); |
545 | if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io); |
546 | } |
547 | *maxfd_io= maxfd; |
3e2e5fab |
548 | |
549 | xit: |
28de6442 |
550 | adns__consistency(ads,0,cc_entex); |
620c146d |
551 | } |
552 | |
553 | void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds, |
554 | const fd_set *writefds, const fd_set *exceptfds, |
555 | const struct timeval *now) { |
fc6a52ae |
556 | struct timeval tv_buf; |
620c146d |
557 | struct pollfd pollfds[MAX_POLLFDS]; |
fc6a52ae |
558 | int npollfds, i; |
620c146d |
559 | |
28de6442 |
560 | adns__consistency(ads,0,cc_entex); |
fc6a52ae |
561 | adns__must_gettimeofday(ads,&now,&tv_buf); |
3e2e5fab |
562 | if (!now) goto xit; |
620c146d |
563 | adns_processtimeouts(ads,now); |
564 | |
565 | npollfds= adns__pollfds(ads,pollfds); |
fc6a52ae |
566 | for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI; |
620c146d |
567 | adns__fdevents(ads, |
568 | pollfds,npollfds, |
569 | maxfd,readfds,writefds,exceptfds, |
570 | *now, 0); |
3e2e5fab |
571 | xit: |
28de6442 |
572 | adns__consistency(ads,0,cc_entex); |
620c146d |
573 | } |
574 | |
575 | /* General helpful functions. */ |
576 | |
577 | void adns_globalsystemfailure(adns_state ads) { |
28de6442 |
578 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
579 | |
f7f83b4a |
580 | while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail); |
581 | while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail); |
620c146d |
582 | |
583 | switch (ads->tcpstate) { |
584 | case server_connecting: |
585 | case server_ok: |
f7f83b4a |
586 | adns__tcp_broken(ads,0,0); |
620c146d |
587 | break; |
588 | case server_disconnected: |
589 | break; |
590 | default: |
591 | abort(); |
592 | } |
28de6442 |
593 | adns__consistency(ads,0,cc_entex); |
656b2da9 |
594 | } |
595 | |
620c146d |
596 | int adns_processany(adns_state ads) { |
aedf10f2 |
597 | int r, i; |
620c146d |
598 | struct timeval now; |
599 | struct pollfd pollfds[MAX_POLLFDS]; |
600 | int npollfds; |
601 | |
28de6442 |
602 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
603 | |
620c146d |
604 | r= gettimeofday(&now,0); |
605 | if (!r) adns_processtimeouts(ads,&now); |
606 | |
25913ee2 |
607 | /* We just use adns__fdevents to loop over the fd's trying them. |
608 | * This seems more sensible than calling select, since we're most |
609 | * likely just to want to do a read on one or two fds anyway. |
610 | */ |
620c146d |
611 | npollfds= adns__pollfds(ads,pollfds); |
aedf10f2 |
612 | for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events; |
620c146d |
613 | adns__fdevents(ads, |
614 | pollfds,npollfds, |
615 | 0,0,0,0, |
616 | now,&r); |
3e2e5fab |
617 | |
28de6442 |
618 | adns__consistency(ads,0,cc_entex); |
c118a725 |
619 | return 0; |
620c146d |
620 | } |
ddfda861 |
621 | |
4353a5c4 |
622 | void adns__autosys(adns_state ads, struct timeval now) { |
ddfda861 |
623 | if (ads->iflags & adns_if_noautosys) return; |
620c146d |
624 | adns_processany(ads); |
ddfda861 |
625 | } |
626 | |
940356bd |
627 | int adns__internal_check(adns_state ads, |
628 | adns_query *query_io, |
629 | adns_answer **answer, |
630 | void **context_r) { |
656b2da9 |
631 | adns_query qu; |
632 | |
633 | qu= *query_io; |
634 | if (!qu) { |
4ae90a83 |
635 | if (ads->output.head) { |
636 | qu= ads->output.head; |
f7f83b4a |
637 | } else if (ads->udpw.head || ads->tcpw.head) { |
4ae90a83 |
638 | return EAGAIN; |
639 | } else { |
640 | return ESRCH; |
641 | } |
656b2da9 |
642 | } else { |
226c5eef |
643 | if (qu->id>=0) return EAGAIN; |
656b2da9 |
644 | } |
645 | LIST_UNLINK(ads->output,qu); |
8e5b0abb |
646 | *answer= qu->answer; |
a6536d8b |
647 | if (context_r) *context_r= qu->ctx.ext; |
8ce38e76 |
648 | *query_io= qu; |
656b2da9 |
649 | free(qu); |
650 | return 0; |
651 | } |
652 | |
653 | int adns_wait(adns_state ads, |
654 | adns_query *query_io, |
655 | adns_answer **answer_r, |
656 | void **context_r) { |
620c146d |
657 | int r, maxfd, rsel; |
656b2da9 |
658 | fd_set readfds, writefds, exceptfds; |
6cfbe3cb |
659 | struct timeval *now, nowbuf, tvbuf, *tvp; |
656b2da9 |
660 | |
28de6442 |
661 | adns__consistency(ads,*query_io,cc_entex); |
656b2da9 |
662 | for (;;) { |
940356bd |
663 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
226c5eef |
664 | if (r != EAGAIN) break; |
656b2da9 |
665 | maxfd= 0; tvp= 0; |
37e28fde |
666 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
620c146d |
667 | adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0); |
656b2da9 |
668 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
88372443 |
669 | if (rsel==-1) { |
620c146d |
670 | if (errno == EINTR) { |
3e2e5fab |
671 | if (ads->iflags & adns_if_eintr) { r= EINTR; break; } |
620c146d |
672 | } else { |
673 | adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno)); |
674 | adns_globalsystemfailure(ads); |
675 | } |
676 | } else { |
677 | assert(rsel >= 0); |
678 | adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0); |
88372443 |
679 | } |
656b2da9 |
680 | } |
28de6442 |
681 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
682 | return r; |
656b2da9 |
683 | } |
684 | |
685 | int adns_check(adns_state ads, |
686 | adns_query *query_io, |
687 | adns_answer **answer_r, |
688 | void **context_r) { |
4353a5c4 |
689 | struct timeval now; |
690 | int r; |
691 | |
28de6442 |
692 | adns__consistency(ads,*query_io,cc_entex); |
620c146d |
693 | r= gettimeofday(&now,0); |
694 | if (!r) adns__autosys(ads,now); |
3e2e5fab |
695 | |
940356bd |
696 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
28de6442 |
697 | adns__consistency(ads,0,cc_entex); |
3e2e5fab |
698 | return r; |
656b2da9 |
699 | } |