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