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