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