X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Ftypes.c;h=14afc900b3fff77cb646b6056d2477c7afad90ee;hb=2bd4b9f4167fa254b424385ee430d1402ef087b7;hp=cfe785075ed92b9cf3be56a562808a8ca3873ca6;hpb=a889f4bef2235d4358c39113a745551773f2450b;p=adns.git diff --git a/src/types.c b/src/types.c index cfe7850..14afc90 100644 --- a/src/types.c +++ b/src/types.c @@ -63,7 +63,7 @@ * _mailbox (pap +pap_mailbox822) * _rp (pa) * _soa (pa,mf,cs) - * _srv* (qdpl,(pap),pa,mf,di,(csp),cs) + * _srv* (qdpl,(pap),pa,mf,di,(csp),cs,postsort) * _flat (mf) * * within each section: @@ -76,6 +76,7 @@ * mf_* * csp_* * cs_* + * postsort_* */ /* @@ -1003,7 +1004,7 @@ static adns_status cs_soa(vbuf *vb, const void *datap) { } /* - * _srv* (pa*2,di,cs*2,qdpl) + * _srv* (pa*2,di,cs*2,qdpl,postsort) */ static adns_status qdpl_srv(adns_state ads, @@ -1036,8 +1037,8 @@ static adns_status qdpl_srv(adns_state ads, } static adns_status pap_srv_begin(const parseinfo *pai, int *cbyte_io, int max, - adns_rr_srvraw *rrp - /* might be adns_rr_srvha* */) { + adns_rr_srvha *rrp + /* might be adns_rr_srvraw* */) { const byte *dgram= pai->dgram; int ti, cbyte; @@ -1096,8 +1097,8 @@ static int di_srv(adns_state ads, const void *datap_a, const void *datap_b) { return 0; } -static adns_status csp_srv_begin(vbuf *vb, const adns_rr_srvraw *rrp - /* might be adns_rr_srvha* */) { +static adns_status csp_srv_begin(vbuf *vb, const adns_rr_srvha *rrp + /* might be adns_rr_srvraw* */) { char buf[30]; sprintf(buf,"%u %u %u ", rrp->priority, rrp->weight, rrp->port); CSP_ADDSTR(buf); @@ -1108,7 +1109,7 @@ static adns_status cs_srvraw(vbuf *vb, const void *datap) { const adns_rr_srvraw *rrp= datap; adns_status st; - st= csp_srv_begin(vb,rrp); if (st) return st; + st= csp_srv_begin(vb,(const void*)rrp); if (st) return st; return csp_domain(vb,rrp->host); } @@ -1116,10 +1117,71 @@ static adns_status cs_srvha(vbuf *vb, const void *datap) { const adns_rr_srvha *rrp= datap; adns_status st; - st= csp_srv_begin(vb,datap); if (st) return st; + st= csp_srv_begin(vb,(const void*)datap); if (st) return st; return csp_hostaddr(vb,&rrp->ha); } +static void postsort_srv(adns_state ads, void *array, int nrrs, + const struct typeinfo *typei) { + /* we treat everything in the array as if it were an adns_rr_srvha + * even though the array might be of adns_rr_srvraw. That's OK + * because they have the same prefix, which is all we access. + * We use typei->rrsz, too, rather than naive array indexing, of course. + */ + char *workbegin, *workend, *search, *arrayend; + const adns_rr_srvha *rr; + union { adns_rr_srvha ha; adns_rr_srvraw raw; } rrtmp; + int cpriority, totalweight, runtotal; + long randval; + + for (workbegin= array, arrayend= workbegin + typei->rrsz * nrrs; + workbegin < arrayend; + workbegin= workend) { + cpriority= (rr=(void*)workbegin)->priority; + + for (workend= workbegin, totalweight= 0; + workend < arrayend && (rr=(void*)workend)->priority == cpriority; + workend += typei->rrsz) { + totalweight += rr->weight; + } + + /* Now workbegin..(workend-1) incl. are exactly all of the RRs of + * cpriority. From now on, workbegin points to the `remaining' + * records: we select one record at a time (RFC2782 `Usage rules' + * and `Format of the SRV RR' subsection `Weight') to place at + * workbegin (swapping with the one that was there, and then + * advance workbegin. */ + for (; + workbegin + typei->rrsz < workend; /* don't bother if just one */ + workbegin += typei->rrsz) { + + randval= nrand48(ads->rand48xsubi); + randval %= (totalweight + 1); + /* makes it into 0..totalweight inclusive; with 2^10 RRs, + * totalweight must be <= 2^26 so probability nonuniformity is + * no worse than 1 in 2^(31-26) ie 1 in 2^5, ie + * abs(log(P_intended(RR_i) / P_actual(RR_i)) <= log(2^-5). + */ + + for (search=workbegin, runtotal=0; + (runtotal += (rr=(void*)search)->weight) < randval; + search += typei->rrsz); + assert(search < arrayend); + totalweight -= rr->weight; + if (search != workbegin) { + memcpy(&rrtmp, workbegin, typei->rrsz); + memcpy(workbegin, search, typei->rrsz); + memcpy(search, &rrtmp, typei->rrsz); + } + } + } + /* tests: + * dig -t srv _srv._tcp.test.iwj.relativity.greenend.org.uk. + * ./adnshost_s -t srv- _sip._udp.voip.net.cam.ac.uk. + * ./adnshost_s -t srv- _jabber._tcp.jabber.org + */ +} + /* * _flat (mf) */ @@ -1137,13 +1199,13 @@ static void mf_flat(adns_query qu, void *data) { } #define DEEP_TYPE(code,rrt,fmt,memb,parser,comparer,printer) \ { adns_r_##code, rrt,fmt,TYPESZ_M(memb), mf_##memb, \ - printer,parser,comparer, adns__qdpl_normal } + printer,parser,comparer, adns__qdpl_normal,0 } #define FLAT_TYPE(code,rrt,fmt,memb,parser,comparer,printer) \ { adns_r_##code, rrt,fmt,TYPESZ_M(memb), mf_flat, \ - printer,parser,comparer, adns__qdpl_normal } -#define XTRA_TYPE(code,rrt,fmt,memb,parser,comparer,printer,qdpl) \ - { adns_r_##code, rrt,fmt,TYPESZ_M(memb), mf_##memb, \ - printer,parser,comparer,qdpl } + printer,parser,comparer, adns__qdpl_normal,0 } +#define XTRA_TYPE(code,rrt,fmt,memb,parser,comparer,printer,qdpl,postsort) \ + { adns_r_##code, rrt,fmt,TYPESZ_M(memb), mf_##memb, \ + printer,parser,comparer,qdpl,postsort } static const typeinfo typeinfos[] = { /* Must be in ascending order of rrtype ! */ @@ -1158,13 +1220,15 @@ DEEP_TYPE(hinfo, "HINFO", 0, intstrpair,pa_hinfo, 0, cs_hinfo ), DEEP_TYPE(mx_raw, "MX", "raw",intstr, pa_mx_raw, di_mx_raw,cs_inthost ), DEEP_TYPE(txt, "TXT", 0, manyistr,pa_txt, 0, cs_txt ), DEEP_TYPE(rp_raw, "RP", "raw",strpair, pa_rp, 0, cs_rp ), -XTRA_TYPE(srv_raw,"SRV", "raw",srvraw , pa_srvraw, di_srv,cs_srvraw,qdpl_srv), +XTRA_TYPE(srv_raw,"SRV", "raw",srvraw , pa_srvraw, di_srv, cs_srvraw, + qdpl_srv, postsort_srv), FLAT_TYPE(addr, "A", "addr", addr, pa_addr, di_addr, cs_addr ), DEEP_TYPE(ns, "NS", "+addr",hostaddr,pa_hostaddr,di_hostaddr,cs_hostaddr ), DEEP_TYPE(ptr, "PTR","checked",str, pa_ptr, 0, cs_domain ), DEEP_TYPE(mx, "MX", "+addr",inthostaddr,pa_mx, di_mx, cs_inthostaddr), -XTRA_TYPE(srv, "SRV","+addr",srvha, pa_srvha, di_srv,cs_srvha, qdpl_srv), +XTRA_TYPE(srv, "SRV","+addr",srvha, pa_srvha, di_srv, cs_srvha, + qdpl_srv, postsort_srv), DEEP_TYPE(soa, "SOA","822", soa, pa_soa, 0, cs_soa ), DEEP_TYPE(rp, "RP", "822", strpair, pa_rp, 0, cs_rp ),