98db6da3 |
1 | /* |
2 | * event.c |
3 | * - event loop core |
4 | * - TCP connection management |
5 | * - user-visible check/wait and event-loop-related functions |
6 | */ |
7 | /* |
a79ac5ba |
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> |
98db6da3 |
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 | */ |
6f17710a |
29 | |
d05cc330 |
30 | #include <errno.h> |
31 | #include <stdlib.h> |
b6b3ac61 |
32 | #include <unistd.h> |
d05cc330 |
33 | |
b6b3ac61 |
34 | #include <sys/types.h> |
35 | #include <sys/time.h> |
d05cc330 |
36 | #include <netdb.h> |
29cf9c44 |
37 | #include <sys/socket.h> |
38 | #include <netinet/in.h> |
d05cc330 |
39 | #include <arpa/inet.h> |
40 | |
41 | #include "internal.h" |
dfdbb32c |
42 | |
74c94831 |
43 | /* TCP connection management. */ |
44 | |
45 | void adns__tcp_closenext(adns_state ads) { |
46 | int serv; |
47 | |
48 | serv= ads->tcpserver; |
49 | close(ads->tcpsocket); |
50 | ads->tcpstate= server_disconnected; |
51 | ads->tcprecv.used= ads->tcpsend.used= 0; |
52 | ads->tcpserver= (serv+1)%ads->nservers; |
53 | } |
6f17710a |
54 | |
71324651 |
55 | void adns__tcp_broken(adns_state ads, const char *what, const char *why) { |
56 | int serv; |
d05cc330 |
57 | adns_query qu, nqu; |
71324651 |
58 | |
59 | assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok); |
96e79df5 |
60 | serv= ads->tcpserver; |
ae41e040 |
61 | adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why); |
74c94831 |
62 | adns__tcp_closenext(ads); |
dfdbb32c |
63 | |
d05cc330 |
64 | for (qu= ads->timew.head; qu; qu= nqu) { |
71324651 |
65 | nqu= qu->next; |
24d52b13 |
66 | if (qu->state == query_tosend) continue; |
96e79df5 |
67 | assert(qu->state == query_tcpwait || qu->state == query_tcpsent); |
68 | qu->state= query_tcpwait; |
69 | qu->tcpfailed |= (1<<serv); |
70 | if (qu->tcpfailed == (1<<ads->nservers)-1) { |
d05cc330 |
71 | LIST_UNLINK(ads->timew,qu); |
11c8bf9b |
72 | adns__query_fail(qu,adns_s_allservfail); |
96e79df5 |
73 | } |
71324651 |
74 | } |
71324651 |
75 | } |
76 | |
96e79df5 |
77 | static void tcp_connected(adns_state ads, struct timeval now) { |
d05cc330 |
78 | adns_query qu, nqu; |
79 | |
ae41e040 |
80 | adns__debug(ads,ads->tcpserver,0,"TCP connected"); |
d05cc330 |
81 | ads->tcpstate= server_ok; |
96e79df5 |
82 | for (qu= ads->timew.head; qu; qu= nqu) { |
83 | nqu= qu->next; |
24d52b13 |
84 | if (qu->state == query_tosend) continue; |
96e79df5 |
85 | assert (qu->state == query_tcpwait); |
11c8bf9b |
86 | adns__query_tcp(qu,now); |
96e79df5 |
87 | } |
88 | } |
89 | |
90 | void adns__tcp_tryconnect(adns_state ads, struct timeval now) { |
71324651 |
91 | int r, fd, tries; |
d05cc330 |
92 | struct sockaddr_in addr; |
93 | struct protoent *proto; |
71324651 |
94 | |
95 | for (tries=0; tries<ads->nservers; tries++) { |
96 | if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return; |
97 | assert(ads->tcpstate == server_disconnected); |
d05cc330 |
98 | assert(!ads->tcpsend.used); |
99 | assert(!ads->tcprecv.used); |
71324651 |
100 | |
101 | proto= getprotobyname("tcp"); |
ae41e040 |
102 | if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; } |
71324651 |
103 | fd= socket(AF_INET,SOCK_STREAM,proto->p_proto); |
d05cc330 |
104 | if (fd<0) { |
ae41e040 |
105 | adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno)); |
d05cc330 |
106 | return; |
107 | } |
108 | r= adns__setnonblock(ads,fd); |
109 | if (r) { |
ae41e040 |
110 | adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r)); |
d05cc330 |
111 | close(fd); |
112 | return; |
113 | } |
71324651 |
114 | memset(&addr,0,sizeof(addr)); |
115 | addr.sin_family= AF_INET; |
5c596e4d |
116 | addr.sin_port= htons(DNS_PORT); |
71324651 |
117 | addr.sin_addr= ads->servers[ads->tcpserver].addr; |
29cf9c44 |
118 | r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr)); |
71324651 |
119 | ads->tcpsocket= fd; |
120 | ads->tcpstate= server_connecting; |
d05cc330 |
121 | if (r==0) { tcp_connected(ads,now); continue; } |
71324651 |
122 | if (errno == EWOULDBLOCK || errno == EINPROGRESS) return; |
96e79df5 |
123 | adns__tcp_broken(ads,"connect",strerror(errno)); |
71324651 |
124 | } |
125 | } |
126 | |
74c94831 |
127 | /* Timeout handling functions. */ |
128 | |
129 | void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, |
130 | struct timeval *tv_buf) { |
131 | const struct timeval *now; |
132 | int r; |
133 | |
134 | now= *now_io; |
135 | if (now) return; |
136 | r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; } |
137 | adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno)); |
138 | adns_globalsystemfailure(ads); |
139 | return; |
140 | } |
d05cc330 |
141 | |
142 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
143 | struct timeval maxto) { |
144 | struct timeval *rbuf; |
145 | |
146 | if (!tv_io) return; |
147 | rbuf= *tv_io; |
de8b18da |
148 | if (!rbuf) { |
149 | *tvbuf= maxto; *tv_io= tvbuf; |
150 | } else { |
151 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
152 | } |
cfdca685 |
153 | /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", |
154 | maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/ |
d05cc330 |
155 | } |
156 | |
157 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
158 | struct timeval now, struct timeval maxtime) { |
159 | ldiv_t dr; |
160 | |
cfdca685 |
161 | /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", |
162 | now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/ |
d05cc330 |
163 | if (!tv_io) return; |
de8b18da |
164 | maxtime.tv_sec -= (now.tv_sec+2); |
165 | maxtime.tv_usec -= (now.tv_usec-2000000); |
166 | dr= ldiv(maxtime.tv_usec,1000000); |
d05cc330 |
167 | maxtime.tv_sec += dr.quot; |
de8b18da |
168 | maxtime.tv_usec -= dr.quot*1000000; |
169 | if (maxtime.tv_sec<0) timerclear(&maxtime); |
d05cc330 |
170 | inter_maxto(tv_io,tvbuf,maxtime); |
171 | } |
172 | |
74c94831 |
173 | void adns__timeouts(adns_state ads, int act, |
174 | struct timeval **tv_io, struct timeval *tvbuf, |
175 | struct timeval now) { |
d05cc330 |
176 | adns_query qu, nqu; |
74c94831 |
177 | |
d05cc330 |
178 | for (qu= ads->timew.head; qu; qu= nqu) { |
179 | nqu= qu->next; |
7def4935 |
180 | if (!timercmp(&now,&qu->timeout,>)) { |
74c94831 |
181 | if (!tv_io) continue; |
182 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
183 | } else { |
184 | if (!act) continue; |
d05cc330 |
185 | LIST_UNLINK(ads->timew,qu); |
24d52b13 |
186 | if (qu->state != query_tosend) { |
11c8bf9b |
187 | adns__query_fail(qu,adns_s_timeout); |
d05cc330 |
188 | } else { |
24d52b13 |
189 | adns__query_send(qu,now); |
d05cc330 |
190 | } |
74c94831 |
191 | nqu= ads->timew.head; |
d05cc330 |
192 | } |
193 | } |
194 | } |
de8b18da |
195 | |
74c94831 |
196 | void adns_firsttimeout(adns_state ads, |
197 | struct timeval **tv_io, struct timeval *tvbuf, |
198 | struct timeval now) { |
2ac463bf |
199 | adns__consistency(ads,0,cc_entex); |
74c94831 |
200 | adns__timeouts(ads, 0, tv_io,tvbuf, now); |
2ac463bf |
201 | adns__consistency(ads,0,cc_entex); |
74c94831 |
202 | } |
203 | |
204 | void adns_processtimeouts(adns_state ads, const struct timeval *now) { |
205 | struct timeval tv_buf; |
206 | |
2ac463bf |
207 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
208 | adns__must_gettimeofday(ads,&now,&tv_buf); |
209 | if (now) adns__timeouts(ads, 1, 0,0, *now); |
2ac463bf |
210 | adns__consistency(ads,0,cc_entex); |
74c94831 |
211 | } |
212 | |
213 | /* fd handling functions. These are the top-level of the real work of |
214 | * reception and often transmission. |
215 | */ |
216 | |
217 | int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) { |
218 | /* Returns the number of entries filled in. Always zeroes revents. */ |
219 | |
220 | assert(MAX_POLLFDS==2); |
221 | |
222 | pollfds_buf[0].fd= ads->udpsocket; |
223 | pollfds_buf[0].events= POLLIN; |
224 | pollfds_buf[0].revents= 0; |
d05cc330 |
225 | |
226 | switch (ads->tcpstate) { |
227 | case server_disconnected: |
74c94831 |
228 | return 1; |
d05cc330 |
229 | case server_connecting: |
74c94831 |
230 | pollfds_buf[1].events= POLLOUT; |
d05cc330 |
231 | break; |
232 | case server_ok: |
74c94831 |
233 | pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI; |
f2ad23ee |
234 | break; |
d05cc330 |
235 | default: |
236 | abort(); |
237 | } |
74c94831 |
238 | pollfds_buf[1].fd= ads->tcpsocket; |
74c94831 |
239 | return 2; |
d05cc330 |
240 | } |
241 | |
74c94831 |
242 | int adns_processreadable(adns_state ads, int fd, const struct timeval *now) { |
243 | int skip, want, dgramlen, r, udpaddrlen, serv; |
5c596e4d |
244 | byte udpbuf[DNS_MAXUDP]; |
dfdbb32c |
245 | struct sockaddr_in udpaddr; |
74c94831 |
246 | |
2ac463bf |
247 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
248 | |
96e79df5 |
249 | switch (ads->tcpstate) { |
250 | case server_disconnected: |
96e79df5 |
251 | case server_connecting: |
96e79df5 |
252 | break; |
253 | case server_ok: |
74c94831 |
254 | if (fd != ads->tcpsocket) break; |
255 | skip= 0; |
256 | for (;;) { |
257 | if (ads->tcprecv.used<skip+2) { |
258 | want= 2; |
259 | } else { |
260 | dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1]; |
261 | if (ads->tcprecv.used<skip+2+dgramlen) { |
262 | want= 2+dgramlen; |
6f17710a |
263 | } else { |
c84b7355 |
264 | adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,1,*now); |
74c94831 |
265 | skip+= 2+dgramlen; continue; |
6f17710a |
266 | } |
267 | } |
74c94831 |
268 | ads->tcprecv.used -= skip; |
269 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used); |
270 | skip= 0; |
1389dc72 |
271 | if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; } |
74c94831 |
272 | assert(ads->tcprecv.used <= ads->tcprecv.avail); |
273 | if (ads->tcprecv.used == ads->tcprecv.avail) continue; |
274 | r= read(ads->tcpsocket, |
275 | ads->tcprecv.buf+ads->tcprecv.used, |
276 | ads->tcprecv.avail-ads->tcprecv.used); |
277 | if (r>0) { |
278 | ads->tcprecv.used+= r; |
279 | } else { |
280 | if (r) { |
1389dc72 |
281 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
74c94831 |
282 | if (errno==EINTR) continue; |
1389dc72 |
283 | if (errno_resources(errno)) { r= errno; goto xit; } |
6f17710a |
284 | } |
74c94831 |
285 | adns__tcp_broken(ads,"read",r?strerror(errno):"closed"); |
1389dc72 |
286 | r= 0; goto xit; |
6f17710a |
287 | } |
74c94831 |
288 | } /* never reached */ |
96e79df5 |
289 | default: |
290 | abort(); |
6f17710a |
291 | } |
74c94831 |
292 | if (fd == ads->udpsocket) { |
dfdbb32c |
293 | for (;;) { |
294 | udpaddrlen= sizeof(udpaddr); |
29cf9c44 |
295 | r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0, |
296 | (struct sockaddr*)&udpaddr,&udpaddrlen); |
dfdbb32c |
297 | if (r<0) { |
1389dc72 |
298 | if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; } |
74c94831 |
299 | if (errno == EINTR) continue; |
1389dc72 |
300 | if (errno_resources(errno)) { r= errno; goto xit; } |
74c94831 |
301 | adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno)); |
1389dc72 |
302 | r= 0; goto xit; |
dfdbb32c |
303 | } |
304 | if (udpaddrlen != sizeof(udpaddr)) { |
ae41e040 |
305 | adns__diag(ads,-1,0,"datagram received with wrong address length %d" |
306 | " (expected %d)", udpaddrlen,sizeof(udpaddr)); |
dfdbb32c |
307 | continue; |
308 | } |
309 | if (udpaddr.sin_family != AF_INET) { |
ae41e040 |
310 | adns__diag(ads,-1,0,"datagram received with wrong protocol family" |
d05cc330 |
311 | " %u (expected %u)",udpaddr.sin_family,AF_INET); |
dfdbb32c |
312 | continue; |
313 | } |
5c596e4d |
314 | if (ntohs(udpaddr.sin_port) != DNS_PORT) { |
ae41e040 |
315 | adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)", |
5c596e4d |
316 | ntohs(udpaddr.sin_port),DNS_PORT); |
dfdbb32c |
317 | continue; |
318 | } |
319 | for (serv= 0; |
320 | serv < ads->nservers && |
321 | ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr; |
322 | serv++); |
323 | if (serv >= ads->nservers) { |
ae41e040 |
324 | adns__warn(ads,-1,0,"datagram received from unknown nameserver %s", |
d05cc330 |
325 | inet_ntoa(udpaddr.sin_addr)); |
dfdbb32c |
326 | continue; |
327 | } |
c84b7355 |
328 | adns__procdgram(ads,udpbuf,r,serv,0,*now); |
6f17710a |
329 | } |
dfdbb32c |
330 | } |
1389dc72 |
331 | r= 0; |
332 | xit: |
2ac463bf |
333 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
334 | return r; |
6f17710a |
335 | } |
6f17710a |
336 | |
74c94831 |
337 | int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) { |
338 | int r; |
339 | |
2ac463bf |
340 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
341 | |
74c94831 |
342 | switch (ads->tcpstate) { |
343 | case server_disconnected: |
344 | break; |
345 | case server_connecting: |
346 | if (fd != ads->tcpsocket) break; |
347 | assert(ads->tcprecv.used==0); |
348 | for (;;) { |
1389dc72 |
349 | if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; } |
74c94831 |
350 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
351 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
352 | tcp_connected(ads,*now); |
1389dc72 |
353 | r= 0; goto xit; |
74c94831 |
354 | } |
355 | if (r>0) { |
356 | adns__tcp_broken(ads,"connect/read","sent data before first request"); |
1389dc72 |
357 | r= 0; goto xit; |
74c94831 |
358 | } |
359 | if (errno==EINTR) continue; |
1389dc72 |
360 | if (errno_resources(errno)) { r= errno; goto xit; } |
74c94831 |
361 | adns__tcp_broken(ads,"connect/read",strerror(errno)); |
1389dc72 |
362 | r= 0; goto xit; |
74c94831 |
363 | } /* not reached */ |
364 | case server_ok: |
365 | if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break; |
366 | for (;;) { |
367 | adns__sigpipe_protect(ads); |
368 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
369 | adns__sigpipe_unprotect(ads); |
370 | if (r<0) { |
371 | if (errno==EINTR) continue; |
1389dc72 |
372 | if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; } |
373 | if (errno_resources(errno)) { r= errno; goto xit; } |
74c94831 |
374 | adns__tcp_broken(ads,"write",strerror(errno)); |
1389dc72 |
375 | r= 0; goto xit; |
74c94831 |
376 | } else if (r>0) { |
377 | ads->tcpsend.used -= r; |
378 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
379 | } |
380 | } /* not reached */ |
381 | default: |
382 | abort(); |
383 | } |
1389dc72 |
384 | r= 0; |
385 | xit: |
2ac463bf |
386 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
387 | return r; |
74c94831 |
388 | } |
389 | |
390 | int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) { |
2ac463bf |
391 | adns__consistency(ads,0,cc_entex); |
74c94831 |
392 | switch (ads->tcpstate) { |
393 | case server_disconnected: |
394 | break; |
395 | case server_connecting: |
396 | case server_ok: |
397 | if (fd != ads->tcpsocket) break; |
398 | adns__tcp_broken(ads,"poll/select","exceptional condition detected"); |
1389dc72 |
399 | break; |
74c94831 |
400 | default: |
401 | abort(); |
402 | } |
2ac463bf |
403 | adns__consistency(ads,0,cc_entex); |
74c94831 |
404 | return 0; |
405 | } |
406 | |
407 | static void fd_event(adns_state ads, int fd, |
408 | int revent, int pollflag, |
409 | int maxfd, const fd_set *fds, |
410 | int (*func)(adns_state, int fd, const struct timeval *now), |
411 | struct timeval now, int *r_r) { |
6f17710a |
412 | int r; |
74c94831 |
413 | |
414 | if (!(revent & pollflag)) return; |
415 | if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return; |
416 | r= func(ads,fd,&now); |
417 | if (r) { |
418 | if (r_r) { |
419 | *r_r= r; |
420 | } else { |
421 | adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno)); |
422 | adns_globalsystemfailure(ads); |
423 | } |
424 | } |
425 | } |
426 | |
427 | void adns__fdevents(adns_state ads, |
428 | const struct pollfd *pollfds, int npollfds, |
429 | int maxfd, const fd_set *readfds, |
430 | const fd_set *writefds, const fd_set *exceptfds, |
431 | struct timeval now, int *r_r) { |
432 | int i, fd, revents; |
433 | |
434 | for (i=0; i<npollfds; i++) { |
435 | fd= pollfds[i].fd; |
436 | if (fd >= maxfd) maxfd= fd+1; |
437 | revents= pollfds[i].revents; |
438 | fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r); |
439 | fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r); |
440 | fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r); |
441 | } |
442 | } |
dfdbb32c |
443 | |
74c94831 |
444 | /* Wrappers for select(2). */ |
445 | |
446 | void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io, |
447 | fd_set *writefds_io, fd_set *exceptfds_io, |
448 | struct timeval **tv_mod, struct timeval *tv_tobuf, |
449 | const struct timeval *now) { |
450 | struct timeval tv_nowbuf; |
451 | struct pollfd pollfds[MAX_POLLFDS]; |
452 | int i, fd, maxfd, npollfds; |
453 | |
2ac463bf |
454 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
455 | |
74c94831 |
456 | if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) { |
457 | /* The caller is planning to sleep. */ |
458 | adns__must_gettimeofday(ads,&now,&tv_nowbuf); |
1389dc72 |
459 | if (!now) goto xit; |
74c94831 |
460 | adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now); |
461 | } |
462 | |
463 | npollfds= adns__pollfds(ads,pollfds); |
464 | maxfd= *maxfd_io; |
465 | for (i=0; i<npollfds; i++) { |
466 | fd= pollfds[i].fd; |
467 | if (fd >= maxfd) maxfd= fd+1; |
468 | if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io); |
469 | if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io); |
470 | if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io); |
471 | } |
472 | *maxfd_io= maxfd; |
1389dc72 |
473 | |
474 | xit: |
2ac463bf |
475 | adns__consistency(ads,0,cc_entex); |
74c94831 |
476 | } |
477 | |
478 | void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds, |
479 | const fd_set *writefds, const fd_set *exceptfds, |
480 | const struct timeval *now) { |
4f973eb5 |
481 | struct timeval tv_buf; |
74c94831 |
482 | struct pollfd pollfds[MAX_POLLFDS]; |
4f973eb5 |
483 | int npollfds, i; |
74c94831 |
484 | |
2ac463bf |
485 | adns__consistency(ads,0,cc_entex); |
4f973eb5 |
486 | adns__must_gettimeofday(ads,&now,&tv_buf); |
1389dc72 |
487 | if (!now) goto xit; |
74c94831 |
488 | adns_processtimeouts(ads,now); |
489 | |
490 | npollfds= adns__pollfds(ads,pollfds); |
4f973eb5 |
491 | for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI; |
74c94831 |
492 | adns__fdevents(ads, |
493 | pollfds,npollfds, |
494 | maxfd,readfds,writefds,exceptfds, |
495 | *now, 0); |
1389dc72 |
496 | xit: |
2ac463bf |
497 | adns__consistency(ads,0,cc_entex); |
74c94831 |
498 | } |
499 | |
500 | /* General helpful functions. */ |
501 | |
502 | void adns_globalsystemfailure(adns_state ads) { |
2ac463bf |
503 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
504 | |
74c94831 |
505 | while (ads->timew.head) { |
506 | adns__query_fail(ads->timew.head, adns_s_systemfail); |
507 | } |
508 | |
509 | switch (ads->tcpstate) { |
510 | case server_connecting: |
511 | case server_ok: |
512 | adns__tcp_closenext(ads); |
513 | break; |
514 | case server_disconnected: |
515 | break; |
516 | default: |
517 | abort(); |
518 | } |
2ac463bf |
519 | adns__consistency(ads,0,cc_entex); |
6f17710a |
520 | } |
521 | |
74c94831 |
522 | int adns_processany(adns_state ads) { |
523 | int r; |
524 | struct timeval now; |
525 | struct pollfd pollfds[MAX_POLLFDS]; |
526 | int npollfds; |
527 | |
2ac463bf |
528 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
529 | |
74c94831 |
530 | r= gettimeofday(&now,0); |
531 | if (!r) adns_processtimeouts(ads,&now); |
532 | |
82ab0d30 |
533 | /* We just use adns__fdevents to loop over the fd's trying them. |
534 | * This seems more sensible than calling select, since we're most |
535 | * likely just to want to do a read on one or two fds anyway. |
536 | */ |
74c94831 |
537 | npollfds= adns__pollfds(ads,pollfds); |
538 | adns__fdevents(ads, |
539 | pollfds,npollfds, |
540 | 0,0,0,0, |
541 | now,&r); |
1389dc72 |
542 | |
2ac463bf |
543 | adns__consistency(ads,0,cc_entex); |
d16697d9 |
544 | return 0; |
74c94831 |
545 | } |
96e79df5 |
546 | |
d05cc330 |
547 | void adns__autosys(adns_state ads, struct timeval now) { |
96e79df5 |
548 | if (ads->iflags & adns_if_noautosys) return; |
74c94831 |
549 | adns_processany(ads); |
96e79df5 |
550 | } |
551 | |
ef20fccf |
552 | int adns__internal_check(adns_state ads, |
553 | adns_query *query_io, |
554 | adns_answer **answer, |
555 | void **context_r) { |
6f17710a |
556 | adns_query qu; |
557 | |
558 | qu= *query_io; |
559 | if (!qu) { |
560 | if (!ads->output.head) return EWOULDBLOCK; |
561 | qu= ads->output.head; |
562 | } else { |
563 | if (qu->id>=0) return EWOULDBLOCK; |
564 | } |
565 | LIST_UNLINK(ads->output,qu); |
965c9782 |
566 | *answer= qu->answer; |
cd1bde2f |
567 | if (context_r) *context_r= qu->ctx.ext; |
8f2aa812 |
568 | *query_io= qu; |
6f17710a |
569 | free(qu); |
570 | return 0; |
571 | } |
572 | |
573 | int adns_wait(adns_state ads, |
574 | adns_query *query_io, |
575 | adns_answer **answer_r, |
576 | void **context_r) { |
74c94831 |
577 | int r, maxfd, rsel; |
6f17710a |
578 | fd_set readfds, writefds, exceptfds; |
579 | struct timeval tvbuf, *tvp; |
580 | |
2ac463bf |
581 | adns__consistency(ads,*query_io,cc_entex); |
6f17710a |
582 | for (;;) { |
ef20fccf |
583 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
1389dc72 |
584 | if (r != EWOULDBLOCK) break; |
6f17710a |
585 | maxfd= 0; tvp= 0; |
dfdbb32c |
586 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
74c94831 |
587 | adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0); |
6f17710a |
588 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
f318f883 |
589 | if (rsel==-1) { |
74c94831 |
590 | if (errno == EINTR) { |
1389dc72 |
591 | if (ads->iflags & adns_if_eintr) { r= EINTR; break; } |
74c94831 |
592 | } else { |
593 | adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno)); |
594 | adns_globalsystemfailure(ads); |
595 | } |
596 | } else { |
597 | assert(rsel >= 0); |
598 | adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0); |
f318f883 |
599 | } |
6f17710a |
600 | } |
2ac463bf |
601 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
602 | return r; |
6f17710a |
603 | } |
604 | |
605 | int adns_check(adns_state ads, |
606 | adns_query *query_io, |
607 | adns_answer **answer_r, |
608 | void **context_r) { |
d05cc330 |
609 | struct timeval now; |
610 | int r; |
611 | |
2ac463bf |
612 | adns__consistency(ads,*query_io,cc_entex); |
74c94831 |
613 | r= gettimeofday(&now,0); |
614 | if (!r) adns__autosys(ads,now); |
1389dc72 |
615 | |
ef20fccf |
616 | r= adns__internal_check(ads,query_io,answer_r,context_r); |
2ac463bf |
617 | adns__consistency(ads,0,cc_entex); |
1389dc72 |
618 | return r; |
6f17710a |
619 | } |