X-Git-Url: https://git.distorted.org.uk/~mdw/adns/blobdiff_plain/3955725ceceb330041f8e7a27e6629a2e8a9b5ba..a8768e80b11709ee11b70a769f0ea86964988e3b:/src/event.c diff --git a/src/event.c b/src/event.c index 8329ff4..983e266 100644 --- a/src/event.c +++ b/src/event.c @@ -5,7 +5,7 @@ * - user-visible check/wait and event-loop-related functions */ /* - * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson + * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -76,7 +76,6 @@ void adns__tcp_tryconnect(adns_state ads, struct timeval now) { int r, fd, tries; struct sockaddr_in addr; struct protoent *proto; - /* fixme: single TCP timeout, not once per server */ for (tries=0; triesnservers; tries++) { if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return; @@ -125,16 +124,16 @@ static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, } else { if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; } -fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", - maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec); +/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n", + maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/ } static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, struct timeval now, struct timeval maxtime) { ldiv_t dr; -fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", - now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec); +/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n", + now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/ if (!tv_io) return; maxtime.tv_sec -= (now.tv_sec+2); maxtime.tv_usec -= (now.tv_usec-2000000); @@ -168,7 +167,79 @@ static void checktimeouts(adns_state ads, struct timeval now, inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); } } -} +} + +static void checkfds(adns_state ads, int *maxfd, + fd_set *readfds, fd_set *writefds, fd_set *exceptfds, + int avail, struct pollfd *buf, int *count_r) { + int count; + + count= 0; + inter_addfd(ads->udpsocket,maxfd,readfds); + poll_addfd(ads->udpsocket,avail,buf,&count,POLLIN); + + switch (ads->tcpstate) { + case server_disconnected: + break; + case server_connecting: + inter_addfd(ads->tcpsocket,maxfd,writefds); + poll_addfd(ads->tcpsocket,avail,buf,&count,POLLOUT); + break; + case server_ok: + inter_addfd(ads->tcpsocket,maxfd,readfds); + inter_addfd(ads->tcpsocket,maxfd,exceptfds); + if (ads->tcpsend.used) inter_addfd(ads->tcpsocket,maxfd,writefds); + poll_addfd(ads->tcpsocket,avail,buf,&count, + ads->tcpsend.used ? POLLIN|POLLOUT : POLLIN); + break; + default: + abort(); + } +} + +struct pollfd *adns_pollfds(adns_state ads, struct pollfd *buf, + int *len_io, int *timeout_io) { + struct timeval now, tvbuf, *tvp; + int timeout; + + r= gettimeofday(&now,0); + if (r) return 0; + + timeout= *timeout_io; + if (timeout < 0) { + tvtop= 0; + } else { + tvbuf.tv_sec= now.tv_sec + (timeout/1000); + tvbuf.tv_usec= now.tv_usec + (timeout%1000)*1000; + if (tvbuf.tv_sec >= 1000000) { + tvbuf.tv_sec += 1; + tvbuf.tv_usec -= 1000000; + } + tvp= &tvbuf; + } + checktimouts(ads,now,&tvp,&tvbuf); + + if (tvp) { + assert(tvbuf.tv_sectv_sec - now.tv_sec)*1000 + (tvp->tv_usec - now.tv_usec)/1000; + } else { + *timeout_io= -1; + } + + avail= *len_io; + if (avail == -1) { + buf= ads->pollfdsbuf; + avail= 2; + if (!buf) { + buf= ads->pollfdsbuf= malloc(sizeof(struct pollfd)*avail); + if (!buf) return 0; + } + } + checkfds(ads, 0,0,0,0, avail,buf,len_io); + if (*len_io > avail) { errno= ENOSPC; return 0; } + + return buf; +} void adns_interest(adns_state ads, int *maxfd, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, @@ -177,9 +248,9 @@ void adns_interest(adns_state ads, int *maxfd, struct timeval tvto_lr; int r; -fprintf(stderr,"adns_interest\n"); +/*fprintf(stderr,"adns_interest\n");*/ -r= gettimeofday(&now,0); + r= gettimeofday(&now,0); if (r) { adns__warn(ads,-1,0,"gettimeofday failed - will sleep for a bit: %s", strerror(errno)); @@ -188,22 +259,8 @@ r= gettimeofday(&now,0); } else { checktimeouts(ads,now,tv_io,tvbuf); } - - inter_addfd(maxfd,readfds,ads->udpsocket); - switch (ads->tcpstate) { - case server_disconnected: - break; - case server_connecting: - inter_addfd(maxfd,writefds,ads->tcpsocket); - break; - case server_ok: - inter_addfd(maxfd,readfds,ads->tcpsocket); - inter_addfd(maxfd,exceptfds,ads->tcpsocket); - if (ads->tcpsend.used) inter_addfd(maxfd,writefds,ads->tcpsocket); - default: - abort(); - } + checkfds(ads, maxfd,readfds,writefds,exceptfds, 0,0,0); } /* Callback procedures - these do the real work of reception and timeout, etc. */ @@ -230,7 +287,7 @@ static int internal_callback(adns_state ads, int maxfd, if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) { count++; assert(ads->tcprecv.used==0); - adns__vbuf_ensure(&ads->tcprecv,1); + if (!adns__vbuf_ensure(&ads->tcprecv,1)) return -1; if (ads->tcprecv.buf) { r= read(ads->tcpsocket,&ads->tcprecv.buf,1); if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { @@ -263,8 +320,10 @@ static int internal_callback(adns_state ads, int maxfd, } ads->tcprecv.used -= skip; memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used); - adns__vbuf_ensure(&ads->tcprecv,want); - if (ads->tcprecv.used >= ads->tcprecv.avail) break; + skip= 0; + if (!adns__vbuf_ensure(&ads->tcprecv,want)) return -1; + assert(ads->tcprecv.used <= ads->tcprecv.avail); + if (ads->tcprecv.used == ads->tcprecv.avail) continue; r= read(ads->tcpsocket, ads->tcprecv.buf+ads->tcprecv.used, ads->tcprecv.avail-ads->tcprecv.used); @@ -292,6 +351,7 @@ static int internal_callback(adns_state ads, int maxfd, memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); } } + break; default: abort(); } @@ -370,7 +430,7 @@ static int internal_check(adns_state ads, } LIST_UNLINK(ads->output,qu); *answer= qu->answer; - if (context_r) *context_r= qu->context.ext; + if (context_r) *context_r= qu->ctx.ext; free(qu); return 0; } @@ -390,7 +450,10 @@ int adns_wait(adns_state ads, FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); - if (rsel==-1) return r; + if (rsel==-1) { + if (errno == EINTR && !(ads->iflags & adns_if_eintr)) continue; + return errno; + } rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); assert(rcb==rsel); }