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