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