+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
+ */
+}
+
+/*
+ * _byteblock (mf)
+ */
+
+static void mf_byteblock(adns_query qu, void *datap) {
+ adns_rr_byteblock *rrp= datap;
+ void *bytes= rrp->data;
+ adns__makefinal_block(qu,&bytes,rrp->len);
+ rrp->data= bytes;
+}
+
+/*
+ * _opaque (pa,cs)
+ */
+
+static adns_status pa_opaque(const parseinfo *pai, int cbyte,
+ int max, void *datap) {
+ adns_rr_byteblock *rrp= datap;
+
+ rrp->len= max - cbyte;
+ rrp->data= adns__alloc_interim(pai->qu, rrp->len);
+ if (!rrp->data) R_NOMEM;
+ memcpy(rrp->data, pai->dgram + cbyte, rrp->len);
+ return adns_s_ok;
+}
+
+static adns_status cs_opaque(vbuf *vb, const void *datap) {
+ const adns_rr_byteblock *rrp= datap;
+ char buf[10];
+ int l;
+ unsigned char *p;
+
+ sprintf(buf,"\\# %d",rrp->len);
+ CSP_ADDSTR(buf);
+
+ for (l= rrp->len, p= rrp->data;
+ l>=4;
+ l -= 4, p += 4) {
+ sprintf(buf," %02x%02x%02x%02x",p[0],p[1],p[2],p[3]);
+ CSP_ADDSTR(buf);
+ }
+ for (;
+ l>0;
+ l--, p++) {
+ sprintf(buf," %02x",*p);
+ CSP_ADDSTR(buf);
+ }
+ return adns_s_ok;
+}
+