adns_pollfds and struct pollfd support - on branch, because it's difficult
[adns] / src / event.c
CommitLineData
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/*
a719a4be 8 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
e576be50 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 */
656b2da9 24
4353a5c4 25#include <string.h>
26#include <errno.h>
27#include <stdlib.h>
28
29#include <netdb.h>
30#include <arpa/inet.h>
31
32#include "internal.h"
37e28fde 33
ddfda861 34/* TCP connection management */
656b2da9 35
8402e34c 36void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
37 int serv;
4353a5c4 38 adns_query qu, nqu;
8402e34c 39
40 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
ddfda861 41 serv= ads->tcpserver;
68442019 42 adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
37e28fde 43 close(ads->tcpsocket);
44 ads->tcpstate= server_disconnected;
45
4353a5c4 46 for (qu= ads->timew.head; qu; qu= nqu) {
8402e34c 47 nqu= qu->next;
ddfda861 48 if (qu->state == query_udp) continue;
49 assert(qu->state == query_tcpwait || qu->state == query_tcpsent);
50 qu->state= query_tcpwait;
51 qu->tcpfailed |= (1<<serv);
52 if (qu->tcpfailed == (1<<ads->nservers)-1) {
4353a5c4 53 LIST_UNLINK(ads->timew,qu);
3955725c 54 adns__query_fail(qu,adns_s_allservfail);
ddfda861 55 }
8402e34c 56 }
57
4353a5c4 58 ads->tcprecv.used= ads->tcpsend.used= 0;
8402e34c 59 ads->tcpserver= (serv+1)%ads->nservers;
60}
61
ddfda861 62static void tcp_connected(adns_state ads, struct timeval now) {
4353a5c4 63 adns_query qu, nqu;
64
68442019 65 adns__debug(ads,ads->tcpserver,0,"TCP connected");
4353a5c4 66 ads->tcpstate= server_ok;
ddfda861 67 for (qu= ads->timew.head; qu; qu= nqu) {
68 nqu= qu->next;
69 if (qu->state == query_udp) continue;
70 assert (qu->state == query_tcpwait);
3955725c 71 adns__query_tcp(qu,now);
ddfda861 72 }
73}
74
75void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
8402e34c 76 int r, fd, tries;
4353a5c4 77 struct sockaddr_in addr;
78 struct protoent *proto;
8402e34c 79
80 for (tries=0; tries<ads->nservers; tries++) {
81 if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return;
82 assert(ads->tcpstate == server_disconnected);
4353a5c4 83 assert(!ads->tcpsend.used);
84 assert(!ads->tcprecv.used);
8402e34c 85
86 proto= getprotobyname("tcp");
68442019 87 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
8402e34c 88 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
4353a5c4 89 if (fd<0) {
68442019 90 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 91 return;
92 }
93 r= adns__setnonblock(ads,fd);
94 if (r) {
68442019 95 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
4353a5c4 96 close(fd);
97 return;
98 }
8402e34c 99 memset(&addr,0,sizeof(addr));
100 addr.sin_family= AF_INET;
98a3f706 101 addr.sin_port= htons(DNS_PORT);
8402e34c 102 addr.sin_addr= ads->servers[ads->tcpserver].addr;
103 r= connect(fd,&addr,sizeof(addr));
104 ads->tcpsocket= fd;
105 ads->tcpstate= server_connecting;
4353a5c4 106 if (r==0) { tcp_connected(ads,now); continue; }
8402e34c 107 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
ddfda861 108 adns__tcp_broken(ads,"connect",strerror(errno));
8402e34c 109 }
110}
111
4353a5c4 112/* `Interest' functions - find out which fd's we might be interested in,
113 * and when we want to be called back for a timeout.
114 */
115
116static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
117 struct timeval maxto) {
118 struct timeval *rbuf;
119
120 if (!tv_io) return;
121 rbuf= *tv_io;
94436798 122 if (!rbuf) {
123 *tvbuf= maxto; *tv_io= tvbuf;
124 } else {
125 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
126 }
ffbda80c 127/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
128 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
4353a5c4 129}
130
131static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
132 struct timeval now, struct timeval maxtime) {
133 ldiv_t dr;
134
ffbda80c 135/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
136 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
4353a5c4 137 if (!tv_io) return;
94436798 138 maxtime.tv_sec -= (now.tv_sec+2);
139 maxtime.tv_usec -= (now.tv_usec-2000000);
140 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 141 maxtime.tv_sec += dr.quot;
94436798 142 maxtime.tv_usec -= dr.quot*1000000;
143 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 144 inter_maxto(tv_io,tvbuf,maxtime);
145}
146
147static void inter_addfd(int *maxfd, fd_set *fds, int fd) {
148 if (!maxfd || !fds) return;
149 if (fd>=*maxfd) *maxfd= fd+1;
150 FD_SET(fd,fds);
151}
152
153static void checktimeouts(adns_state ads, struct timeval now,
154 struct timeval **tv_io, struct timeval *tvbuf) {
155 adns_query qu, nqu;
156
157 for (qu= ads->timew.head; qu; qu= nqu) {
158 nqu= qu->next;
159 if (timercmp(&now,&qu->timeout,>)) {
160 LIST_UNLINK(ads->timew,qu);
161 if (qu->state != query_udp) {
3955725c 162 adns__query_fail(qu,adns_s_timeout);
4353a5c4 163 } else {
3955725c 164 adns__query_udp(qu,now);
4353a5c4 165 }
166 } else {
167 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
168 }
169 }
a8768e80 170}
171
172static void checkfds(adns_state ads, int *maxfd,
173 fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
174 int avail, struct pollfd *buf, int *count_r) {
175 int count;
176
177 count= 0;
178 inter_addfd(ads->udpsocket,maxfd,readfds);
179 poll_addfd(ads->udpsocket,avail,buf,&count,POLLIN);
180
181 switch (ads->tcpstate) {
182 case server_disconnected:
183 break;
184 case server_connecting:
185 inter_addfd(ads->tcpsocket,maxfd,writefds);
186 poll_addfd(ads->tcpsocket,avail,buf,&count,POLLOUT);
187 break;
188 case server_ok:
189 inter_addfd(ads->tcpsocket,maxfd,readfds);
190 inter_addfd(ads->tcpsocket,maxfd,exceptfds);
191 if (ads->tcpsend.used) inter_addfd(ads->tcpsocket,maxfd,writefds);
192 poll_addfd(ads->tcpsocket,avail,buf,&count,
193 ads->tcpsend.used ? POLLIN|POLLOUT : POLLIN);
194 break;
195 default:
196 abort();
197 }
198}
199
200struct pollfd *adns_pollfds(adns_state ads, struct pollfd *buf,
201 int *len_io, int *timeout_io) {
202 struct timeval now, tvbuf, *tvp;
203 int timeout;
204
205 r= gettimeofday(&now,0);
206 if (r) return 0;
207
208 timeout= *timeout_io;
209 if (timeout < 0) {
210 tvtop= 0;
211 } else {
212 tvbuf.tv_sec= now.tv_sec + (timeout/1000);
213 tvbuf.tv_usec= now.tv_usec + (timeout%1000)*1000;
214 if (tvbuf.tv_sec >= 1000000) {
215 tvbuf.tv_sec += 1;
216 tvbuf.tv_usec -= 1000000;
217 }
218 tvp= &tvbuf;
219 }
220 checktimouts(ads,now,&tvp,&tvbuf);
221
222 if (tvp) {
223 assert(tvbuf.tv_sec<INT_MAX/1000);
224 *timeout_io= (tvp->tv_sec - now.tv_sec)*1000 + (tvp->tv_usec - now.tv_usec)/1000;
225 } else {
226 *timeout_io= -1;
227 }
228
229 avail= *len_io;
230 if (avail == -1) {
231 buf= ads->pollfdsbuf;
232 avail= 2;
233 if (!buf) {
234 buf= ads->pollfdsbuf= malloc(sizeof(struct pollfd)*avail);
235 if (!buf) return 0;
236 }
237 }
238 checkfds(ads, 0,0,0,0, avail,buf,len_io);
239 if (*len_io > avail) { errno= ENOSPC; return 0; }
240
241 return buf;
242}
4353a5c4 243
244void adns_interest(adns_state ads, int *maxfd,
245 fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
246 struct timeval **tv_io, struct timeval *tvbuf) {
247 struct timeval now;
248 struct timeval tvto_lr;
249 int r;
250
ffbda80c 251/*fprintf(stderr,"adns_interest\n");*/
94436798 252
ffbda80c 253 r= gettimeofday(&now,0);
4353a5c4 254 if (r) {
68442019 255 adns__warn(ads,-1,0,"gettimeofday failed - will sleep for a bit: %s",
256 strerror(errno));
4353a5c4 257 timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS);
258 inter_maxto(tv_io, tvbuf, tvto_lr);
259 } else {
260 checktimeouts(ads,now,tv_io,tvbuf);
261 }
4353a5c4 262
a8768e80 263 checkfds(ads, maxfd,readfds,writefds,exceptfds, 0,0,0);
4353a5c4 264}
265
ddfda861 266/* Callback procedures - these do the real work of reception and timeout, etc. */
267
268static int callb_checkfd(int maxfd, const fd_set *fds, int fd) {
269 return maxfd<0 || !fds ? 1 :
270 fd<maxfd && FD_ISSET(fd,fds);
271}
272
273static int internal_callback(adns_state ads, int maxfd,
274 const fd_set *readfds, const fd_set *writefds,
4353a5c4 275 const fd_set *exceptfds,
276 struct timeval now) {
277 int skip, want, dgramlen, count, udpaddrlen, r, serv;
98a3f706 278 byte udpbuf[DNS_MAXUDP];
37e28fde 279 struct sockaddr_in udpaddr;
656b2da9 280
281 count= 0;
ddfda861 282
283 switch (ads->tcpstate) {
284 case server_disconnected:
285 break;
286 case server_connecting:
656b2da9 287 if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
288 count++;
289 assert(ads->tcprecv.used==0);
e062dcae 290 if (!adns__vbuf_ensure(&ads->tcprecv,1)) return -1;
656b2da9 291 if (ads->tcprecv.buf) {
292 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
293 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
4353a5c4 294 tcp_connected(ads,now);
656b2da9 295 } else if (r>0) {
4353a5c4 296 adns__tcp_broken(ads,"connect/read","sent data before first request");
656b2da9 297 } else if (errno!=EINTR) {
4353a5c4 298 adns__tcp_broken(ads,"connect/read",strerror(errno));
656b2da9 299 }
300 }
301 }
ddfda861 302 break;
303 case server_ok:
304 count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) +
305 callb_checkfd(maxfd,exceptfds,ads->tcpsocket) +
306 (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket));
307 if (callb_checkfd(maxfd,readfds,ads->tcpsocket)) {
656b2da9 308 skip= 0;
309 for (;;) {
310 if (ads->tcprecv.used<skip+2) {
311 want= 2;
312 } else {
313 dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1];
314 if (ads->tcprecv.used<skip+2+dgramlen) {
315 want= 2+dgramlen;
316 } else {
98a3f706 317 adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,now);
656b2da9 318 skip+= 2+dgramlen; continue;
319 }
320 }
37e28fde 321 ads->tcprecv.used -= skip;
656b2da9 322 memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
e062dcae 323 skip= 0;
324 if (!adns__vbuf_ensure(&ads->tcprecv,want)) return -1;
325 assert(ads->tcprecv.used <= ads->tcprecv.avail);
326 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
656b2da9 327 r= read(ads->tcpsocket,
328 ads->tcprecv.buf+ads->tcprecv.used,
329 ads->tcprecv.avail-ads->tcprecv.used);
330 if (r>0) {
331 ads->tcprecv.used+= r;
332 } else {
333 if (r<0) {
334 if (errno==EAGAIN || errno==EWOULDBLOCK || errno==ENOMEM) break;
335 if (errno==EINTR) continue;
336 }
4353a5c4 337 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
656b2da9 338 break;
339 }
340 }
341 } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) {
4353a5c4 342 adns__tcp_broken(ads,"select","exceptional condition detected");
656b2da9 343 } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
344 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
345 if (r<0) {
346 if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) {
4353a5c4 347 adns__tcp_broken(ads,"write",strerror(errno));
656b2da9 348 }
349 } else if (r>0) {
350 ads->tcpsend.used -= r;
351 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
352 }
353 }
e062dcae 354 break;
ddfda861 355 default:
356 abort();
656b2da9 357 }
358
37e28fde 359 if (callb_checkfd(maxfd,readfds,ads->udpsocket)) {
360 count++;
361 for (;;) {
362 udpaddrlen= sizeof(udpaddr);
363 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,&udpaddr,&udpaddrlen);
364 if (r<0) {
365 if (!(errno == EAGAIN || errno == EWOULDBLOCK ||
366 errno == EINTR || errno == ENOMEM || errno == ENOBUFS))
68442019 367 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
37e28fde 368 break;
369 }
370 if (udpaddrlen != sizeof(udpaddr)) {
68442019 371 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
372 " (expected %d)", udpaddrlen,sizeof(udpaddr));
37e28fde 373 continue;
374 }
375 if (udpaddr.sin_family != AF_INET) {
68442019 376 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
4353a5c4 377 " %u (expected %u)",udpaddr.sin_family,AF_INET);
37e28fde 378 continue;
379 }
98a3f706 380 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
68442019 381 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
98a3f706 382 ntohs(udpaddr.sin_port),DNS_PORT);
37e28fde 383 continue;
384 }
385 for (serv= 0;
386 serv < ads->nservers &&
387 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
388 serv++);
389 if (serv >= ads->nservers) {
68442019 390 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
4353a5c4 391 inet_ntoa(udpaddr.sin_addr));
37e28fde 392 continue;
393 }
98a3f706 394 adns__procdgram(ads,udpbuf,r,serv,now);
656b2da9 395 }
37e28fde 396 }
4353a5c4 397 return count;
656b2da9 398}
656b2da9 399
ddfda861 400int adns_callback(adns_state ads, int maxfd,
401 const fd_set *readfds, const fd_set *writefds,
402 const fd_set *exceptfds) {
403 struct timeval now;
656b2da9 404 int r;
37e28fde 405
4353a5c4 406 r= gettimeofday(&now,0); if (r) return -1;
407 checktimeouts(ads,now,0,0);
408 return internal_callback(ads,maxfd,readfds,writefds,exceptfds,now);
656b2da9 409}
410
ddfda861 411/* User-visible functions and their implementation. */
412
4353a5c4 413void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 414 if (ads->iflags & adns_if_noautosys) return;
415 adns_callback(ads,-1,0,0,0);
416}
417
656b2da9 418static int internal_check(adns_state ads,
419 adns_query *query_io,
420 adns_answer **answer,
421 void **context_r) {
422 adns_query qu;
423
424 qu= *query_io;
425 if (!qu) {
426 if (!ads->output.head) return EWOULDBLOCK;
427 qu= ads->output.head;
428 } else {
429 if (qu->id>=0) return EWOULDBLOCK;
430 }
431 LIST_UNLINK(ads->output,qu);
8e5b0abb 432 *answer= qu->answer;
a6536d8b 433 if (context_r) *context_r= qu->ctx.ext;
656b2da9 434 free(qu);
435 return 0;
436}
437
438int adns_wait(adns_state ads,
439 adns_query *query_io,
440 adns_answer **answer_r,
441 void **context_r) {
442 int r, maxfd, rsel, rcb;
443 fd_set readfds, writefds, exceptfds;
444 struct timeval tvbuf, *tvp;
445
446 for (;;) {
447 r= internal_check(ads,query_io,answer_r,context_r);
94436798 448 if (r != EWOULDBLOCK) return r;
656b2da9 449 maxfd= 0; tvp= 0;
37e28fde 450 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
656b2da9 451 adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf);
452 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
88372443 453 if (rsel==-1) {
454 if (errno == EINTR && !(ads->iflags & adns_if_eintr)) continue;
455 return errno;
456 }
4353a5c4 457 rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds);
656b2da9 458 assert(rcb==rsel);
459 }
460}
461
462int adns_check(adns_state ads,
463 adns_query *query_io,
464 adns_answer **answer_r,
465 void **context_r) {
4353a5c4 466 struct timeval now;
467 int r;
468
469 r= gettimeofday(&now,0); if (r) return errno;
470 adns__autosys(ads,now);
656b2da9 471 return internal_check(ads,query_io,answer_r,context_r);
472}