chiark / gitweb /
Improving parsing code, and changing memory management. On its way,
[adns.git] / src / event.c
index 14ce1b03f6150a8811a22b2be30340323860b4ab..453567d4534ef9cbada895eca6e6e1ffe9a3d29f 100644 (file)
@@ -77,7 +77,7 @@ void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
     }
     memset(&addr,0,sizeof(addr));
     addr.sin_family= AF_INET;
-    addr.sin_port= htons(NSPORT);
+    addr.sin_port= htons(DNS_PORT);
     addr.sin_addr= ads->servers[ads->tcpserver].addr;
     r= connect(fd,&addr,sizeof(addr));
     ads->tcpsocket= fd;
@@ -98,20 +98,28 @@ static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
 
   if (!tv_io) return;
   rbuf= *tv_io;
-  if (!rbuf) { *tvbuf= maxto; *tv_io= tvbuf; return; }
-  if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
+  if (!rbuf) {
+    *tvbuf= maxto; *tv_io= 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);
 }
 
 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);
   if (!tv_io) return;
-  maxtime.tv_sec -= (now.tv_sec-1);
-  maxtime.tv_usec += (1000-now.tv_usec);
-  dr= ldiv(maxtime.tv_usec,1000);
+  maxtime.tv_sec -= (now.tv_sec+2);
+  maxtime.tv_usec -= (now.tv_usec-2000000);
+  dr= ldiv(maxtime.tv_usec,1000000);
   maxtime.tv_sec += dr.quot;
-  maxtime.tv_usec -= dr.rem;
+  maxtime.tv_usec -= dr.quot*1000000;
+  if (maxtime.tv_sec<0) timerclear(&maxtime);
   inter_maxto(tv_io,tvbuf,maxtime);
 }
 
@@ -147,7 +155,9 @@ void adns_interest(adns_state ads, int *maxfd,
   struct timeval tvto_lr;
   int r;
   
-  r= gettimeofday(&now,0);
+fprintf(stderr,"adns_interest\n");
+
+r= gettimeofday(&now,0);
   if (r) {
     adns__warn(ads,-1,"gettimeofday failed - will sleep for a bit: %s",strerror(errno));
     timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS);
@@ -185,7 +195,7 @@ static int internal_callback(adns_state ads, int maxfd,
                             const fd_set *exceptfds,
                             struct timeval now) {
   int skip, want, dgramlen, count, udpaddrlen, r, serv;
-  byte udpbuf[MAXUDPDGRAM];
+  byte udpbuf[DNS_MAXUDP];
   struct sockaddr_in udpaddr;
 
   count= 0;
@@ -224,7 +234,7 @@ static int internal_callback(adns_state ads, int maxfd,
          if (ads->tcprecv.used<skip+2+dgramlen) {
            want= 2+dgramlen;
          } else {
-           adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver);
+           adns__procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver,now);
            skip+= 2+dgramlen; continue;
          }
        }
@@ -284,9 +294,9 @@ static int internal_callback(adns_state ads, int maxfd,
                   " %u (expected %u)",udpaddr.sin_family,AF_INET);
        continue;
       }
-      if (ntohs(udpaddr.sin_port) != NSPORT) {
+      if (ntohs(udpaddr.sin_port) != DNS_PORT) {
        adns__diag(ads,-1,"datagram received from wrong port %u (expected %u)",
-                  ntohs(udpaddr.sin_port),NSPORT);
+                  ntohs(udpaddr.sin_port),DNS_PORT);
        continue;
       }
       for (serv= 0;
@@ -298,7 +308,7 @@ static int internal_callback(adns_state ads, int maxfd,
                   inet_ntoa(udpaddr.sin_addr));
        continue;
       }
-      adns__procdgram(ads,udpbuf,r,serv);
+      adns__procdgram(ads,udpbuf,r,serv,now);
     }
   }
   return count;
@@ -336,7 +346,7 @@ static int internal_check(adns_state ads,
     if (qu->id>=0) return EWOULDBLOCK;
   }
   LIST_UNLINK(ads->output,qu);
-  *answer= (adns_answer*)qu->answer.buf;
+#error copy answer    *answer= (adns_answer*)qu->ans.buf;
   if (context_r) *context_r= qu->context.ext;
   free(qu);
   return 0;
@@ -352,7 +362,7 @@ int adns_wait(adns_state ads,
   
   for (;;) {
     r= internal_check(ads,query_io,answer_r,context_r);
-    if (r && r != EWOULDBLOCK) return r;
+    if (r != EWOULDBLOCK) return r;
     maxfd= 0; tvp= 0;
     FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
     adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf);