Copyrights now acknowledge fanf for adns. Individual file copyrights too.
[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
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>
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 */
29
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <netdb.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40
41 #include "internal.h"
42
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 }
54
55 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
56 int serv;
57 adns_query qu, nqu;
58
59 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
60 serv= ads->tcpserver;
61 adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
62 adns__tcp_closenext(ads);
63
64 for (qu= ads->timew.head; qu; qu= nqu) {
65 nqu= qu->next;
66 if (qu->state == query_tosend) continue;
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) {
71 LIST_UNLINK(ads->timew,qu);
72 adns__query_fail(qu,adns_s_allservfail);
73 }
74 }
75 }
76
77 static void tcp_connected(adns_state ads, struct timeval now) {
78 adns_query qu, nqu;
79
80 adns__debug(ads,ads->tcpserver,0,"TCP connected");
81 ads->tcpstate= server_ok;
82 for (qu= ads->timew.head; qu; qu= nqu) {
83 nqu= qu->next;
84 if (qu->state == query_tosend) continue;
85 assert (qu->state == query_tcpwait);
86 adns__query_tcp(qu,now);
87 }
88 }
89
90 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
91 int r, fd, tries;
92 struct sockaddr_in addr;
93 struct protoent *proto;
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);
98 assert(!ads->tcpsend.used);
99 assert(!ads->tcprecv.used);
100
101 proto= getprotobyname("tcp");
102 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
103 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
104 if (fd<0) {
105 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
106 return;
107 }
108 r= adns__setnonblock(ads,fd);
109 if (r) {
110 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
111 close(fd);
112 return;
113 }
114 memset(&addr,0,sizeof(addr));
115 addr.sin_family= AF_INET;
116 addr.sin_port= htons(DNS_PORT);
117 addr.sin_addr= ads->servers[ads->tcpserver].addr;
118 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
119 ads->tcpsocket= fd;
120 ads->tcpstate= server_connecting;
121 if (r==0) { tcp_connected(ads,now); continue; }
122 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
123 adns__tcp_broken(ads,"connect",strerror(errno));
124 }
125 }
126
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 }
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;
148 if (!rbuf) {
149 *tvbuf= maxto; *tv_io= tvbuf;
150 } else {
151 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
152 }
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);*/
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
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);*/
163 if (!tv_io) return;
164 maxtime.tv_sec -= (now.tv_sec+2);
165 maxtime.tv_usec -= (now.tv_usec-2000000);
166 dr= ldiv(maxtime.tv_usec,1000000);
167 maxtime.tv_sec += dr.quot;
168 maxtime.tv_usec -= dr.quot*1000000;
169 if (maxtime.tv_sec<0) timerclear(&maxtime);
170 inter_maxto(tv_io,tvbuf,maxtime);
171 }
172
173 void adns__timeouts(adns_state ads, int act,
174 struct timeval **tv_io, struct timeval *tvbuf,
175 struct timeval now) {
176 adns_query qu, nqu;
177
178 for (qu= ads->timew.head; qu; qu= nqu) {
179 nqu= qu->next;
180 if (!timercmp(&now,&qu->timeout,>)) {
181 if (!tv_io) continue;
182 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
183 } else {
184 if (!act) continue;
185 LIST_UNLINK(ads->timew,qu);
186 if (qu->state != query_tosend) {
187 adns__query_fail(qu,adns_s_timeout);
188 } else {
189 adns__query_send(qu,now);
190 }
191 nqu= ads->timew.head;
192 }
193 }
194 }
195
196 void adns_firsttimeout(adns_state ads,
197 struct timeval **tv_io, struct timeval *tvbuf,
198 struct timeval now) {
199 adns__consistency(ads,0,cc_entex);
200 adns__timeouts(ads, 0, tv_io,tvbuf, now);
201 adns__consistency(ads,0,cc_entex);
202 }
203
204 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
205 struct timeval tv_buf;
206
207 adns__consistency(ads,0,cc_entex);
208 adns__must_gettimeofday(ads,&now,&tv_buf);
209 if (now) adns__timeouts(ads, 1, 0,0, *now);
210 adns__consistency(ads,0,cc_entex);
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;
225
226 switch (ads->tcpstate) {
227 case server_disconnected:
228 return 1;
229 case server_connecting:
230 pollfds_buf[1].events= POLLOUT;
231 break;
232 case server_ok:
233 pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
234 break;
235 default:
236 abort();
237 }
238 pollfds_buf[1].fd= ads->tcpsocket;
239 return 2;
240 }
241
242 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
243 int skip, want, dgramlen, r, udpaddrlen, serv;
244 byte udpbuf[DNS_MAXUDP];
245 struct sockaddr_in udpaddr;
246
247 adns__consistency(ads,0,cc_entex);
248
249 switch (ads->tcpstate) {
250 case server_disconnected:
251 case server_connecting:
252 break;
253 case server_ok:
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;
263 } else {
264 adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,1,*now);
265 skip+= 2+dgramlen; continue;
266 }
267 }
268 ads->tcprecv.used -= skip;
269 memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
270 skip= 0;
271 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
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) {
281 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
282 if (errno==EINTR) continue;
283 if (errno_resources(errno)) { r= errno; goto xit; }
284 }
285 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
286 r= 0; goto xit;
287 }
288 } /* never reached */
289 default:
290 abort();
291 }
292 if (fd == ads->udpsocket) {
293 for (;;) {
294 udpaddrlen= sizeof(udpaddr);
295 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
296 (struct sockaddr*)&udpaddr,&udpaddrlen);
297 if (r<0) {
298 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
299 if (errno == EINTR) continue;
300 if (errno_resources(errno)) { r= errno; goto xit; }
301 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
302 r= 0; goto xit;
303 }
304 if (udpaddrlen != sizeof(udpaddr)) {
305 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
306 " (expected %d)", udpaddrlen,sizeof(udpaddr));
307 continue;
308 }
309 if (udpaddr.sin_family != AF_INET) {
310 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
311 " %u (expected %u)",udpaddr.sin_family,AF_INET);
312 continue;
313 }
314 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
315 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
316 ntohs(udpaddr.sin_port),DNS_PORT);
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) {
324 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
325 inet_ntoa(udpaddr.sin_addr));
326 continue;
327 }
328 adns__procdgram(ads,udpbuf,r,serv,0,*now);
329 }
330 }
331 r= 0;
332 xit:
333 adns__consistency(ads,0,cc_entex);
334 return r;
335 }
336
337 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
338 int r;
339
340 adns__consistency(ads,0,cc_entex);
341
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 (;;) {
349 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
350 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
351 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
352 tcp_connected(ads,*now);
353 r= 0; goto xit;
354 }
355 if (r>0) {
356 adns__tcp_broken(ads,"connect/read","sent data before first request");
357 r= 0; goto xit;
358 }
359 if (errno==EINTR) continue;
360 if (errno_resources(errno)) { r= errno; goto xit; }
361 adns__tcp_broken(ads,"connect/read",strerror(errno));
362 r= 0; goto xit;
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;
372 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
373 if (errno_resources(errno)) { r= errno; goto xit; }
374 adns__tcp_broken(ads,"write",strerror(errno));
375 r= 0; goto xit;
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 }
384 r= 0;
385 xit:
386 adns__consistency(ads,0,cc_entex);
387 return r;
388 }
389
390 int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
391 adns__consistency(ads,0,cc_entex);
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");
399 break;
400 default:
401 abort();
402 }
403 adns__consistency(ads,0,cc_entex);
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) {
412 int r;
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 }
443
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
454 adns__consistency(ads,0,cc_entex);
455
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);
459 if (!now) goto xit;
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;
473
474 xit:
475 adns__consistency(ads,0,cc_entex);
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) {
481 struct timeval tv_buf;
482 struct pollfd pollfds[MAX_POLLFDS];
483 int npollfds, i;
484
485 adns__consistency(ads,0,cc_entex);
486 adns__must_gettimeofday(ads,&now,&tv_buf);
487 if (!now) goto xit;
488 adns_processtimeouts(ads,now);
489
490 npollfds= adns__pollfds(ads,pollfds);
491 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
492 adns__fdevents(ads,
493 pollfds,npollfds,
494 maxfd,readfds,writefds,exceptfds,
495 *now, 0);
496 xit:
497 adns__consistency(ads,0,cc_entex);
498 }
499
500 /* General helpful functions. */
501
502 void adns_globalsystemfailure(adns_state ads) {
503 adns__consistency(ads,0,cc_entex);
504
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 }
519 adns__consistency(ads,0,cc_entex);
520 }
521
522 int adns_processany(adns_state ads) {
523 int r;
524 struct timeval now;
525 struct pollfd pollfds[MAX_POLLFDS];
526 int npollfds;
527
528 adns__consistency(ads,0,cc_entex);
529
530 r= gettimeofday(&now,0);
531 if (!r) adns_processtimeouts(ads,&now);
532
533 npollfds= adns__pollfds(ads,pollfds);
534 adns__fdevents(ads,
535 pollfds,npollfds,
536 0,0,0,0,
537 now,&r);
538
539 adns__consistency(ads,0,cc_entex);
540 return r;
541 }
542
543 void adns__autosys(adns_state ads, struct timeval now) {
544 if (ads->iflags & adns_if_noautosys) return;
545 adns_processany(ads);
546 }
547
548 int adns__internal_check(adns_state ads,
549 adns_query *query_io,
550 adns_answer **answer,
551 void **context_r) {
552 adns_query qu;
553
554 qu= *query_io;
555 if (!qu) {
556 if (!ads->output.head) return EWOULDBLOCK;
557 qu= ads->output.head;
558 } else {
559 if (qu->id>=0) return EWOULDBLOCK;
560 }
561 LIST_UNLINK(ads->output,qu);
562 *answer= qu->answer;
563 if (context_r) *context_r= qu->ctx.ext;
564 *query_io= qu;
565 free(qu);
566 return 0;
567 }
568
569 int adns_wait(adns_state ads,
570 adns_query *query_io,
571 adns_answer **answer_r,
572 void **context_r) {
573 int r, maxfd, rsel;
574 fd_set readfds, writefds, exceptfds;
575 struct timeval tvbuf, *tvp;
576
577 adns__consistency(ads,*query_io,cc_entex);
578 for (;;) {
579 r= adns__internal_check(ads,query_io,answer_r,context_r);
580 if (r != EWOULDBLOCK) break;
581 maxfd= 0; tvp= 0;
582 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
583 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
584 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
585 if (rsel==-1) {
586 if (errno == EINTR) {
587 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
588 } else {
589 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
590 adns_globalsystemfailure(ads);
591 }
592 } else {
593 assert(rsel >= 0);
594 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
595 }
596 }
597 adns__consistency(ads,0,cc_entex);
598 return r;
599 }
600
601 int adns_check(adns_state ads,
602 adns_query *query_io,
603 adns_answer **answer_r,
604 void **context_r) {
605 struct timeval now;
606 int r;
607
608 adns__consistency(ads,*query_io,cc_entex);
609 r= gettimeofday(&now,0);
610 if (!r) adns__autosys(ads,now);
611
612 r= adns__internal_check(ads,query_io,answer_r,context_r);
613 adns__consistency(ads,0,cc_entex);
614 return r;
615 }