Rearrangement more or less sorted, back to trying to get it to compile.
[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/*
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 */
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);
ddfda861 54 adns__query_fail(ads,qu,adns_s_allservfail);
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);
71 adns__query_tcp(ads,qu,now);
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;
ddfda861 79 /* fixme: single TCP timeout, not once per server */
8402e34c 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);
4353a5c4 84 assert(!ads->tcpsend.used);
85 assert(!ads->tcprecv.used);
8402e34c 86
87 proto= getprotobyname("tcp");
68442019 88 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
8402e34c 89 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
4353a5c4 90 if (fd<0) {
68442019 91 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 92 return;
93 }
94 r= adns__setnonblock(ads,fd);
95 if (r) {
68442019 96 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
4353a5c4 97 close(fd);
98 return;
99 }
8402e34c 100 memset(&addr,0,sizeof(addr));
101 addr.sin_family= AF_INET;
98a3f706 102 addr.sin_port= htons(DNS_PORT);
8402e34c 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;
4353a5c4 107 if (r==0) { tcp_connected(ads,now); continue; }
8402e34c 108 if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
ddfda861 109 adns__tcp_broken(ads,"connect",strerror(errno));
8402e34c 110 }
111}
112
4353a5c4 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;
94436798 123 if (!rbuf) {
124 *tvbuf= maxto; *tv_io= tvbuf;
125 } else {
126 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
127 }
128fprintf(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);
4353a5c4 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
94436798 136fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
137 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);
4353a5c4 138 if (!tv_io) return;
94436798 139 maxtime.tv_sec -= (now.tv_sec+2);
140 maxtime.tv_usec -= (now.tv_usec-2000000);
141 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 142 maxtime.tv_sec += dr.quot;
94436798 143 maxtime.tv_usec -= dr.quot*1000000;
144 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 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) {
163 adns__query_fail(ads,qu,adns_s_timeout);
164 } else {
165 adns__query_udp(ads,qu,now);
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
94436798 180fprintf(stderr,"adns_interest\n");
181
182r= gettimeofday(&now,0);
4353a5c4 183 if (r) {
68442019 184 adns__warn(ads,-1,0,"gettimeofday failed - will sleep for a bit: %s",
185 strerror(errno));
4353a5c4 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);
204 default:
205 abort();
206 }
207}
208
ddfda861 209/* Callback procedures - these do the real work of reception and timeout, etc. */
210
211static int callb_checkfd(int maxfd, const fd_set *fds, int fd) {
212 return maxfd<0 || !fds ? 1 :
213 fd<maxfd && FD_ISSET(fd,fds);
214}
215
216static int internal_callback(adns_state ads, int maxfd,
217 const fd_set *readfds, const fd_set *writefds,
4353a5c4 218 const fd_set *exceptfds,
219 struct timeval now) {
220 int skip, want, dgramlen, count, udpaddrlen, r, serv;
98a3f706 221 byte udpbuf[DNS_MAXUDP];
37e28fde 222 struct sockaddr_in udpaddr;
656b2da9 223
224 count= 0;
ddfda861 225
226 switch (ads->tcpstate) {
227 case server_disconnected:
228 break;
229 case server_connecting:
656b2da9 230 if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
231 count++;
232 assert(ads->tcprecv.used==0);
4353a5c4 233 adns__vbuf_ensure(&ads->tcprecv,1);
656b2da9 234 if (ads->tcprecv.buf) {
235 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
236 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
4353a5c4 237 tcp_connected(ads,now);
656b2da9 238 } else if (r>0) {
4353a5c4 239 adns__tcp_broken(ads,"connect/read","sent data before first request");
656b2da9 240 } else if (errno!=EINTR) {
4353a5c4 241 adns__tcp_broken(ads,"connect/read",strerror(errno));
656b2da9 242 }
243 }
244 }
ddfda861 245 break;
246 case server_ok:
247 count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) +
248 callb_checkfd(maxfd,exceptfds,ads->tcpsocket) +
249 (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket));
250 if (callb_checkfd(maxfd,readfds,ads->tcpsocket)) {
656b2da9 251 skip= 0;
252 for (;;) {
253 if (ads->tcprecv.used<skip+2) {
254 want= 2;
255 } else {
256 dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1];
257 if (ads->tcprecv.used<skip+2+dgramlen) {
258 want= 2+dgramlen;
259 } else {
98a3f706 260 adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,now);
656b2da9 261 skip+= 2+dgramlen; continue;
262 }
263 }
37e28fde 264 ads->tcprecv.used -= skip;
656b2da9 265 memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
4353a5c4 266 adns__vbuf_ensure(&ads->tcprecv,want);
656b2da9 267 if (ads->tcprecv.used >= ads->tcprecv.avail) break;
268 r= read(ads->tcpsocket,
269 ads->tcprecv.buf+ads->tcprecv.used,
270 ads->tcprecv.avail-ads->tcprecv.used);
271 if (r>0) {
272 ads->tcprecv.used+= r;
273 } else {
274 if (r<0) {
275 if (errno==EAGAIN || errno==EWOULDBLOCK || errno==ENOMEM) break;
276 if (errno==EINTR) continue;
277 }
4353a5c4 278 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
656b2da9 279 break;
280 }
281 }
282 } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) {
4353a5c4 283 adns__tcp_broken(ads,"select","exceptional condition detected");
656b2da9 284 } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
285 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
286 if (r<0) {
287 if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) {
4353a5c4 288 adns__tcp_broken(ads,"write",strerror(errno));
656b2da9 289 }
290 } else if (r>0) {
291 ads->tcpsend.used -= r;
292 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
293 }
294 }
ddfda861 295 default:
296 abort();
656b2da9 297 }
298
37e28fde 299 if (callb_checkfd(maxfd,readfds,ads->udpsocket)) {
300 count++;
301 for (;;) {
302 udpaddrlen= sizeof(udpaddr);
303 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,&udpaddr,&udpaddrlen);
304 if (r<0) {
305 if (!(errno == EAGAIN || errno == EWOULDBLOCK ||
306 errno == EINTR || errno == ENOMEM || errno == ENOBUFS))
68442019 307 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
37e28fde 308 break;
309 }
310 if (udpaddrlen != sizeof(udpaddr)) {
68442019 311 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
312 " (expected %d)", udpaddrlen,sizeof(udpaddr));
37e28fde 313 continue;
314 }
315 if (udpaddr.sin_family != AF_INET) {
68442019 316 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
4353a5c4 317 " %u (expected %u)",udpaddr.sin_family,AF_INET);
37e28fde 318 continue;
319 }
98a3f706 320 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
68442019 321 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
98a3f706 322 ntohs(udpaddr.sin_port),DNS_PORT);
37e28fde 323 continue;
324 }
325 for (serv= 0;
326 serv < ads->nservers &&
327 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
328 serv++);
329 if (serv >= ads->nservers) {
68442019 330 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
4353a5c4 331 inet_ntoa(udpaddr.sin_addr));
37e28fde 332 continue;
333 }
98a3f706 334 adns__procdgram(ads,udpbuf,r,serv,now);
656b2da9 335 }
37e28fde 336 }
4353a5c4 337 return count;
656b2da9 338}
656b2da9 339
ddfda861 340int adns_callback(adns_state ads, int maxfd,
341 const fd_set *readfds, const fd_set *writefds,
342 const fd_set *exceptfds) {
343 struct timeval now;
656b2da9 344 int r;
37e28fde 345
4353a5c4 346 r= gettimeofday(&now,0); if (r) return -1;
347 checktimeouts(ads,now,0,0);
348 return internal_callback(ads,maxfd,readfds,writefds,exceptfds,now);
656b2da9 349}
350
ddfda861 351/* User-visible functions and their implementation. */
352
4353a5c4 353void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 354 if (ads->iflags & adns_if_noautosys) return;
355 adns_callback(ads,-1,0,0,0);
356}
357
656b2da9 358static int internal_check(adns_state ads,
359 adns_query *query_io,
360 adns_answer **answer,
361 void **context_r) {
362 adns_query qu;
363
364 qu= *query_io;
365 if (!qu) {
366 if (!ads->output.head) return EWOULDBLOCK;
367 qu= ads->output.head;
368 } else {
369 if (qu->id>=0) return EWOULDBLOCK;
370 }
371 LIST_UNLINK(ads->output,qu);
8e5b0abb 372 *answer= qu->answer;
4353a5c4 373 if (context_r) *context_r= qu->context.ext;
656b2da9 374 free(qu);
375 return 0;
376}
377
378int adns_wait(adns_state ads,
379 adns_query *query_io,
380 adns_answer **answer_r,
381 void **context_r) {
382 int r, maxfd, rsel, rcb;
383 fd_set readfds, writefds, exceptfds;
384 struct timeval tvbuf, *tvp;
385
386 for (;;) {
387 r= internal_check(ads,query_io,answer_r,context_r);
94436798 388 if (r != EWOULDBLOCK) return r;
656b2da9 389 maxfd= 0; tvp= 0;
37e28fde 390 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
656b2da9 391 adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf);
392 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
393 if (rsel==-1) return r;
4353a5c4 394 rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds);
656b2da9 395 assert(rcb==rsel);
396 }
397}
398
399int adns_check(adns_state ads,
400 adns_query *query_io,
401 adns_answer **answer_r,
402 void **context_r) {
4353a5c4 403 struct timeval now;
404 int r;
405
406 r= gettimeofday(&now,0); if (r) return errno;
407 adns__autosys(ads,now);
656b2da9 408 return internal_check(ads,query_io,answer_r,context_r);
409}