X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=adns.git;a=blobdiff_plain;f=src%2Ftransmit.c;h=08e49d01577f760cf78b4c4d58f08bcab8805de5;hp=11d774bb05d56e5c7c58312e19678eb81e757fbf;hb=67887df61675bf52ff1a82e52d6c7359f64c9b9a;hpb=660d7d3b4af029c3e86fdfe1ac078815c41ed79e diff --git a/src/transmit.c b/src/transmit.c index 11d774b..08e49d0 100644 --- a/src/transmit.c +++ b/src/transmit.c @@ -4,7 +4,12 @@ * - send queries */ /* - * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson + * This file is + * Copyright (C) 1997-2000 Ian Jackson + * + * It is part of adns, which is + * Copyright (C) 1997-2000 Ian Jackson + * Copyright (C) 1999 Tony Finch * * 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 @@ -22,11 +27,12 @@ */ #include -#include +#include #include #include "internal.h" +#include "tvarith.h" #define MKQUERY_START(vb) (rqp= (vb)->buf+(vb)->used) #define MKQUERY_ADDB(b) *rqp++= (b) @@ -71,19 +77,18 @@ static adns_status mkquery_footer(vbuf *vb, adns_rrtype type) { adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r, const char *owner, int ol, const typeinfo *typei, adns_queryflags flags) { - int ll, c, nlabs; + int ll, c, nbytes; byte label[255], *rqp; const char *p, *pe; adns_status st; - st= mkquery_header(ads,vb,id_r,strlen(owner)+2); if (st) return st; + st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st; MKQUERY_START(vb); p= owner; pe= owner+ol; - nlabs= 0; - if (!*p) return adns_s_querydomaininvalid; - do { + nbytes= 0; + while (p!=pe) { ll= 0; while (p!=pe && (c= *p++)!='.') { if (c=='\\') { @@ -110,10 +115,12 @@ adns_status adns__mkquery(adns_state ads, vbuf *vb, int *id_r, label[ll++]= c; } if (!ll) return adns_s_querydomaininvalid; - if (nlabs++ > 63) return adns_s_querydomaintoolong; + if (ll > DNS_MAXLABEL) return adns_s_querydomaintoolong; + nbytes+= ll+1; + if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong; MKQUERY_ADDB(ll); memcpy(rqp,label,ll); rqp+= ll; - } while (p!=pe); + } MKQUERY_ADDB(0); MKQUERY_STOP(vb); @@ -153,7 +160,7 @@ adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r, return adns_s_ok; } -void adns__query_tcp(adns_query qu, struct timeval now) { +void adns__querysend_tcp(adns_query qu, struct timeval now) { byte length[2]; struct iovec iov[2]; int wr, r; @@ -161,15 +168,18 @@ void adns__query_tcp(adns_query qu, struct timeval now) { if (qu->ads->tcpstate != server_ok) return; + assert(qu->state == query_tcpw); + length[0]= (qu->query_dglen&0x0ff00U) >>8; length[1]= (qu->query_dglen&0x0ff); ads= qu->ads; if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return; - timevaladd(&now,TCPMS); - qu->timeout= now; - qu->state= query_tcpsent; + qu->retries++; + + /* Reset idle timeout. */ + ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0; if (ads->tcpsend.used) { wr= 0; @@ -203,26 +213,26 @@ void adns__query_tcp(adns_query qu, struct timeval now) { } static void query_usetcp(adns_query qu, struct timeval now) { - timevaladd(&now,TCPMS); + qu->state= query_tcpw; qu->timeout= now; - qu->state= query_tcpwait; - LIST_LINK_TAIL(qu->ads->timew,qu); - adns__query_tcp(qu,now); + timevaladd(&qu->timeout,TCPWAITMS); + LIST_LINK_TAIL(qu->ads->tcpw,qu); + adns__querysend_tcp(qu,now); adns__tcp_tryconnect(qu->ads,now); } -void adns__query_udp(adns_query qu, struct timeval now) { +void adns__query_send(adns_query qu, struct timeval now) { struct sockaddr_in servaddr; int serv, r; adns_state ads; - assert(qu->state == query_udp); + assert(qu->state == query_tosend); if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) { query_usetcp(qu,now); return; } - if (qu->udpretries >= UDPMAXRETRIES) { + if (qu->retries >= UDPMAXRETRIES) { adns__query_fail(qu,adns_s_timeout); return; } @@ -235,14 +245,15 @@ void adns__query_udp(adns_query qu, struct timeval now) { servaddr.sin_addr= ads->servers[serv].addr; servaddr.sin_port= htons(DNS_PORT); - r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0,&servaddr,sizeof(servaddr)); - if (r<0 && errno == EMSGSIZE) { query_usetcp(qu,now); return; } - if (r<0) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); + r= sendto(ads->udpsocket,qu->query_dgram,qu->query_dglen,0, + (const struct sockaddr*)&servaddr,sizeof(servaddr)); + if (r<0 && errno == EMSGSIZE) { qu->retries= 0; query_usetcp(qu,now); return; } + if (r<0 && errno != EAGAIN) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); - timevaladd(&now,UDPRETRYMS); qu->timeout= now; + timevaladd(&qu->timeout,UDPRETRYMS); qu->udpsent |= (1<udpnextserver= (serv+1)%ads->nservers; - qu->udpretries++; - LIST_LINK_TAIL(ads->timew,qu); + qu->retries++; + LIST_LINK_TAIL(ads->udpw,qu); }