+static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *bufp) {
+ const char *p, *q;
+ char tbuf[200], *slash, *ep;
+ struct in_addr base, mask;
+ int l;
+ unsigned long initial, baselocal;
+
+ ads->nsortlist= 0;
+ if (!bufp) return;
+
+ for (;;) {
+ while (ctype_whitespace(*bufp)) bufp++;
+ if (!*bufp) return;
+
+ q= bufp;
+ while (*q && !ctype_whitespace(*q)) q++;
+
+ p= bufp;
+ l= q-p;
+ bufp= q;
+
+ if (ads->nsortlist >= MAXSORTLIST) {
+ adns__diag(ads,-1,0,"too many sortlist entries, ignoring %.*s onwards",l,p);
+ return;
+ }
+
+ if (l >= sizeof(tbuf)) {
+ configparseerr(ads,fn,lno,"sortlist entry `%.*s' too long",l,p);
+ continue;
+ }
+
+ memcpy(tbuf,p,l);
+ slash= strchr(tbuf,'/');
+ if (slash) *slash++= 0;
+
+ if (!inet_aton(tbuf,&base)) {
+ configparseerr(ads,fn,lno,"invalid address `%s' in sortlist",tbuf);
+ continue;
+ }
+
+ if (slash) {
+ if (strchr(slash,'.')) {
+ if (!inet_aton(slash,&mask)) {
+ configparseerr(ads,fn,lno,"invalid mask `%s' in sortlist",slash);
+ continue;
+ }
+ if (base.s_addr & ~mask.s_addr) {
+ configparseerr(ads,fn,lno,
+ "mask `%s' in sortlist overlaps address `%s'",slash,tbuf);
+ continue;
+ }
+ } else {
+ initial= strtoul(slash,&ep,10);
+ if (*ep || initial>32) {
+ configparseerr(ads,fn,lno,"mask length `%s' invalid",slash);
+ continue;
+ }
+ mask.s_addr= htonl((0x0ffffffffUL) << (32-initial));
+ }
+ } else {
+ baselocal= ntohl(base.s_addr);
+ if (!baselocal & 0x080000000UL) /* class A */
+ mask.s_addr= htonl(0x0ff000000UL);
+ else if ((baselocal & 0x0c0000000UL) == 0x080000000UL)
+ mask.s_addr= htonl(0x0ffff0000UL); /* class B */
+ else if ((baselocal & 0x0f0000000UL) == 0x0e0000000UL)
+ mask.s_addr= htonl(0x0ff000000UL); /* class C */
+ else {
+ configparseerr(ads,fn,lno,
+ "network address `%s' in sortlist is not in classed ranges,"
+ " must specify mask explicitly", tbuf);
+ continue;
+ }
+ }
+
+ ads->sortlist[ads->nsortlist].base= base;
+ ads->sortlist[ads->nsortlist].mask= mask;
+ ads->nsortlist++;
+ }