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