Changed memory semantics of internal queries to fix bugs.
[adns] / src / types.c
1 /*
2 * types.c
3 * - RR-type-specific code, and the machinery to call it
4 */
5 /*
6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30
31 #include "internal.h"
32
33 #define R_NOMEM return adns_s_nomemory
34 #define CSP_ADDSTR(s) do { if (!adns__vbuf_appendstr(vb,(s))) R_NOMEM; } while (0)
35
36 /*
37 * order of sections:
38 *
39 * _string (pap)
40 * _textdata, _qstring (csp)
41 * _str (mf,cs)
42 * _intstr (mf,csp,cs)
43 * _manyistr (mf,cs)
44 * _txt (pa)
45 * _inaddr (pa,dip,di)
46 * _addr (pa,di,csp,cs)
47 * _domain (pap)
48 * _host_raw (pa)
49 * _hostaddr (pap,pa,dip,di,mfp,mf,csp,cs +pap_findaddrs)
50 * _mx_raw (pa,di)
51 * _mx (pa,di)
52 * _inthostaddr (mf,cs)
53 * _ptr (pa)
54 * _strpair (mf,cs)
55 * _intstrpair (mf,cs)
56 * _hinfo (pa)
57 * _mailbox (pap +pap_mailbox822)
58 * _rp (pa)
59 * _soa (pa,mf,cs)
60 * _flat (mf)
61 *
62 * within each section:
63 * pap_*
64 * pa_*
65 * dip_*
66 * di_*
67 * mfp_*
68 * mf_*
69 * csp_*
70 * cs_*
71 */
72
73 /*
74 * _string (pap)
75 * _textdata, _qstring (csp)
76 */
77
78 static adns_status pap_string(const parseinfo *pai, int *cbyte_io, int max,
79 int *len_r, char **str_r) {
80 /* Neither len_r nor str_r may be null.
81 * End of datagram (overrun) is indicated by returning adns_s_invaliddata;
82 */
83 const byte *dgram= pai->dgram;
84 int l, cbyte;
85 char *str;
86
87 cbyte= *cbyte_io;
88
89 if (cbyte >= max) return adns_s_invaliddata;
90 GET_B(cbyte,l);
91 if (cbyte+l > max) return adns_s_invaliddata;
92
93 str= adns__alloc_interim(pai->qu, l+1);
94 if (!str) R_NOMEM;
95
96 str[l]= 0;
97 memcpy(str,dgram+cbyte,l);
98
99 *len_r= l;
100 *str_r= str;
101 *cbyte_io= cbyte+l;
102
103 return adns_s_ok;
104 }
105
106 static adns_status csp_textdata(vbuf *vb, const char *dp, int len) {
107 unsigned char ch;
108 char buf[10];
109 int cn;
110
111 CSP_ADDSTR("\"");
112 for (cn=0; cn<len; cn++) {
113 ch= *dp++;
114 if (ch >= 32 && ch <= 126 && ch != '"' && ch != '\\') {
115 if (!adns__vbuf_append(vb,&ch,1)) R_NOMEM;
116 } else if (ch == '\\') {
117 CSP_ADDSTR("\\\\");
118 } else {
119 sprintf(buf,"\\x%02x",ch);
120 CSP_ADDSTR(buf);
121 }
122 }
123 CSP_ADDSTR("\"");
124
125 return adns_s_ok;
126 }
127
128 static adns_status csp_qstring(vbuf *vb, const char *dp) {
129 return csp_textdata(vb, dp, strlen(dp));
130 }
131
132 /*
133 * _str (mf,cs)
134 */
135
136 static void mf_str(adns_query qu, void *datap) {
137 char **rrp= datap;
138
139 adns__makefinal_str(qu,rrp);
140 }
141
142 static adns_status cs_str(vbuf *vb, const void *datap) {
143 const char *const *rrp= datap;
144
145 return csp_qstring(vb,*rrp);
146 }
147
148 /*
149 * _intstr (mf,csp,cs)
150 */
151
152 static void mf_intstr(adns_query qu, void *datap) {
153 adns_rr_intstr *rrp= datap;
154
155 adns__makefinal_str(qu,&rrp->str);
156 }
157
158 static adns_status csp_intstr(vbuf *vb, const adns_rr_intstr *rrp) {
159 char buf[10];
160
161 sprintf(buf,"%u ",rrp->i);
162 CSP_ADDSTR(buf);
163 return csp_qstring(vb,rrp->str);
164 }
165
166 static adns_status cs_intstr(vbuf *vb, const void *datap) {
167 const adns_rr_intstr *rrp= datap;
168
169 return csp_intstr(vb,rrp);
170 }
171
172 /*
173 * _manyistr (mf,cs)
174 */
175
176 static void mf_manyistr(adns_query qu, void *datap) {
177 adns_rr_intstr **rrp= datap;
178 adns_rr_intstr *te, *table;
179 void *tablev;
180 int tc;
181
182 for (tc=0, te= *rrp; te->i >= 0; te++, tc++);
183 tablev= *rrp;
184 adns__makefinal_block(qu,&tablev,sizeof(*te)*(tc+1));
185 *rrp= table= tablev;
186 for (te= *rrp; te->i >= 0; te++)
187 adns__makefinal_str(qu,&te->str);
188 }
189
190 static adns_status cs_manyistr(vbuf *vb, const void *datap) {
191 const adns_rr_intstr *const *rrp= datap;
192 const adns_rr_intstr *current;
193 adns_status st;
194 int spc;
195
196 for (spc=0, current= *rrp; current->i >= 0; current++) {
197 if (spc) CSP_ADDSTR(" ");
198 st= csp_textdata(vb,current->str,current->i); if (st) return st;
199 }
200 return adns_s_ok;
201 }
202
203 /*
204 * _txt (pa)
205 */
206
207 static adns_status pa_txt(const parseinfo *pai, int cbyte, int max, void *datap) {
208 adns_rr_intstr **rrp= datap, *table, *te;
209 const byte *dgram= pai->dgram;
210 int ti, tc, l, startbyte;
211 adns_status st;
212
213 startbyte= cbyte;
214 if (cbyte >= max) return adns_s_invaliddata;
215 tc= 0;
216 while (cbyte < max) {
217 GET_B(cbyte,l);
218 cbyte+= l;
219 tc++;
220 }
221 if (cbyte != max) return adns_s_invaliddata;
222
223 table= adns__alloc_interim(pai->qu,sizeof(*table)*(tc+1));
224 if (!table) R_NOMEM;
225
226 for (cbyte=startbyte, ti=0, te=table; ti<tc; ti++, te++) {
227 st= pap_string(pai, &cbyte, max, &te->i, &te->str);
228 if (st) return st;
229 }
230 assert(cbyte == max);
231
232 te->i= -1;
233 te->str= 0;
234
235 *rrp= table;
236 return adns_s_ok;
237 }
238
239 /*
240 * _inaddr (pa,dip,di)
241 */
242
243 static adns_status pa_inaddr(const parseinfo *pai, int cbyte, int max, void *datap) {
244 struct in_addr *storeto= datap;
245
246 if (max-cbyte != 4) return adns_s_invaliddata;
247 memcpy(storeto, pai->dgram + cbyte, 4);
248 return adns_s_ok;
249 }
250
251 static int search_sortlist(adns_state ads, struct in_addr ad) {
252 const struct sortlist *slp;
253 int i;
254
255 for (i=0, slp=ads->sortlist;
256 i<ads->nsortlist && !((ad.s_addr & slp->mask.s_addr) == slp->base.s_addr);
257 i++, slp++);
258 return i;
259 }
260
261 static int dip_inaddr(adns_state ads, struct in_addr a, struct in_addr b) {
262 int ai, bi;
263
264 if (!ads->nsortlist) return 0;
265
266 ai= search_sortlist(ads,a);
267 bi= search_sortlist(ads,b);
268 return bi<ai;
269 }
270
271 static int di_inaddr(adns_state ads, const void *datap_a, const void *datap_b) {
272 const struct in_addr *ap= datap_a, *bp= datap_b;
273
274 return dip_inaddr(ads,*ap,*bp);
275 }
276
277 static adns_status cs_inaddr(vbuf *vb, const void *datap) {
278 const struct in_addr *rrp= datap, rr= *rrp;
279 const char *ia;
280
281 ia= inet_ntoa(rr); assert(ia);
282 CSP_ADDSTR(ia);
283 return adns_s_ok;
284 }
285
286 /*
287 * _addr (pa,di,csp,cs)
288 */
289
290 static adns_status pa_addr(const parseinfo *pai, int cbyte, int max, void *datap) {
291 adns_rr_addr *storeto= datap;
292 const byte *dgram= pai->dgram;
293
294 if (max-cbyte != 4) return adns_s_invaliddata;
295 storeto->len= sizeof(storeto->addr.inet);
296 memset(&storeto->addr,0,sizeof(storeto->addr.inet));
297 storeto->addr.inet.sin_family= AF_INET;
298 memcpy(&storeto->addr.inet.sin_addr,dgram+cbyte,4);
299 return adns_s_ok;
300 }
301
302 static int di_addr(adns_state ads, const void *datap_a, const void *datap_b) {
303 const adns_rr_addr *ap= datap_a, *bp= datap_b;
304
305 assert(ap->addr.sa.sa_family == AF_INET);
306 return dip_inaddr(ads, ap->addr.inet.sin_addr, bp->addr.inet.sin_addr);
307 }
308
309 static int div_addr(void *context, const void *datap_a, const void *datap_b) {
310 const adns_state ads= context;
311
312 return di_addr(ads, datap_a, datap_b);
313 }
314
315 static adns_status csp_addr(vbuf *vb, const adns_rr_addr *rrp) {
316 const char *ia;
317 static char buf[30];
318
319 switch (rrp->addr.inet.sin_family) {
320 case AF_INET:
321 CSP_ADDSTR("AF_INET ");
322 ia= inet_ntoa(rrp->addr.inet.sin_addr); assert(ia);
323 CSP_ADDSTR(ia);
324 break;
325 default:
326 sprintf(buf,"AF=%u",rrp->addr.sa.sa_family);
327 CSP_ADDSTR(buf);
328 break;
329 }
330 return adns_s_ok;
331 }
332
333 static adns_status cs_addr(vbuf *vb, const void *datap) {
334 const adns_rr_addr *rrp= datap;
335
336 return csp_addr(vb,rrp);
337 }
338
339 /*
340 * _domain (pap)
341 */
342
343 static adns_status pap_domain(const parseinfo *pai, int *cbyte_io, int max,
344 char **domain_r, parsedomain_flags flags) {
345 adns_status st;
346 char *dm;
347
348 st= adns__parse_domain(pai->qu->ads, pai->serv, pai->qu, &pai->qu->vb, flags,
349 pai->dgram,pai->dglen, cbyte_io, max);
350 if (st) return st;
351 if (!pai->qu->vb.used) return adns_s_invaliddata;
352
353 dm= adns__alloc_interim(pai->qu, pai->qu->vb.used+1);
354 if (!dm) R_NOMEM;
355
356 dm[pai->qu->vb.used]= 0;
357 memcpy(dm,pai->qu->vb.buf,pai->qu->vb.used);
358
359 *domain_r= dm;
360 return adns_s_ok;
361 }
362
363 /*
364 * _host_raw (pa)
365 */
366
367 static adns_status pa_host_raw(const parseinfo *pai, int cbyte, int max, void *datap) {
368 char **rrp= datap;
369 adns_status st;
370
371 st= pap_domain(pai, &cbyte, max, rrp,
372 pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
373 if (st) return st;
374
375 if (cbyte != max) return adns_s_invaliddata;
376 return adns_s_ok;
377 }
378
379 /*
380 * _hostaddr (pap,pa,dip,di,mfp,mf,csp,cs +icb_hostaddr, pap_findaddrs)
381 */
382
383 static adns_status pap_findaddrs(const parseinfo *pai, adns_rr_hostaddr *ha,
384 int *cbyte_io, int count, int dmstart) {
385 int rri, naddrs;
386 int type, class, rdlen, rdstart, ownermatched;
387 unsigned long ttl;
388 adns_status st;
389
390 for (rri=0, naddrs=-1; rri<count; rri++) {
391 st= adns__findrr_anychk(pai->qu, pai->serv, pai->dgram, pai->dglen, cbyte_io,
392 &type, &class, &ttl, &rdlen, &rdstart,
393 pai->dgram, pai->dglen, dmstart, &ownermatched);
394 if (st) return st;
395 if (!ownermatched || class != DNS_CLASS_IN || type != adns_r_a) {
396 if (naddrs>0) break; else continue;
397 }
398 if (naddrs == -1) {
399 naddrs= 0;
400 }
401 if (!adns__vbuf_ensure(&pai->qu->vb, (naddrs+1)*sizeof(adns_rr_addr))) R_NOMEM;
402 adns__update_expires(pai->qu,ttl,pai->now);
403 st= pa_addr(pai, rdstart,rdstart+rdlen,
404 pai->qu->vb.buf + naddrs*sizeof(adns_rr_addr));
405 if (st) return st;
406 naddrs++;
407 }
408 if (naddrs >= 0) {
409 ha->addrs= adns__alloc_interim(pai->qu, naddrs*sizeof(adns_rr_addr));
410 if (!ha->addrs) R_NOMEM;
411 memcpy(ha->addrs, pai->qu->vb.buf, naddrs*sizeof(adns_rr_addr));
412 ha->naddrs= naddrs;
413 ha->astatus= adns_s_ok;
414
415 adns__isort(ha->addrs, naddrs, sizeof(adns_rr_addr), pai->qu->vb.buf,
416 div_addr, pai->ads);
417 }
418 return adns_s_ok;
419 }
420
421 static void icb_hostaddr(adns_query parent, adns_query child) {
422 adns_answer *cans= child->answer;
423 adns_rr_hostaddr *rrp= child->ctx.info.hostaddr;
424 adns_state ads= parent->ads;
425
426 rrp->astatus= cans->status;
427 rrp->naddrs= cans->nrrs;
428 rrp->addrs= cans->rrs.addr;
429 adns__transfer_interim(child, parent, rrp->addrs, rrp->naddrs*sizeof(adns_rr_addr));
430
431 if (parent->children.head) {
432 LIST_LINK_TAIL(ads->childw,parent);
433 } else {
434 adns__query_done(parent);
435 }
436 }
437
438 static adns_status pap_hostaddr(const parseinfo *pai, int *cbyte_io,
439 int max, adns_rr_hostaddr *rrp) {
440 adns_status st;
441 int dmstart, cbyte;
442 qcontext ctx;
443 int id;
444 adns_query nqu;
445 adns_queryflags nflags;
446
447 dmstart= cbyte= *cbyte_io;
448 st= pap_domain(pai, &cbyte, max, &rrp->host,
449 pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
450 if (st) return st;
451 *cbyte_io= cbyte;
452
453 rrp->astatus= adns_s_ok;
454 rrp->naddrs= -1;
455 rrp->addrs= 0;
456
457 cbyte= pai->nsstart;
458
459 st= pap_findaddrs(pai, rrp, &cbyte, pai->nscount, dmstart);
460 if (st) return st;
461 if (rrp->naddrs != -1) return adns_s_ok;
462
463 st= pap_findaddrs(pai, rrp, &cbyte, pai->arcount, dmstart);
464 if (st) return st;
465 if (rrp->naddrs != -1) return adns_s_ok;
466
467 st= adns__mkquery_frdgram(pai->ads, &pai->qu->vb, &id,
468 pai->dgram, pai->dglen, dmstart,
469 adns_r_addr, adns_qf_quoteok_query);
470 if (st) return st;
471
472 ctx.ext= 0;
473 ctx.callback= icb_hostaddr;
474 ctx.info.hostaddr= rrp;
475
476 nflags= adns_qf_quoteok_query;
477 if (!(pai->qu->flags & adns_qf_cname_loose)) nflags |= adns_qf_cname_forbid;
478
479 st= adns__internal_submit(pai->ads, &nqu, adns__findtype(adns_r_addr),
480 &pai->qu->vb, id, nflags, pai->now, &ctx);
481 if (st) return st;
482
483 nqu->parent= pai->qu;
484 LIST_LINK_TAIL_PART(pai->qu->children,nqu,siblings.);
485
486 return adns_s_ok;
487 }
488
489 static adns_status pa_hostaddr(const parseinfo *pai, int cbyte, int max, void *datap) {
490 adns_rr_hostaddr *rrp= datap;
491 adns_status st;
492
493 st= pap_hostaddr(pai, &cbyte, max, rrp);
494 if (st) return st;
495 if (cbyte != max) return adns_s_invaliddata;
496
497 return adns_s_ok;
498 }
499
500 static int dip_hostaddr(adns_state ads, const adns_rr_hostaddr *ap, const adns_rr_hostaddr *bp) {
501 if (ap->astatus != bp->astatus) return ap->astatus;
502 if (ap->astatus) return 0;
503
504 assert(ap->addrs[0].addr.sa.sa_family == AF_INET);
505 assert(bp->addrs[0].addr.sa.sa_family == AF_INET);
506 return dip_inaddr(ads,
507 ap->addrs[0].addr.inet.sin_addr,
508 bp->addrs[0].addr.inet.sin_addr);
509 }
510
511 static int di_hostaddr(adns_state ads, const void *datap_a, const void *datap_b) {
512 const adns_rr_hostaddr *ap= datap_a, *bp= datap_b;
513
514 return dip_hostaddr(ads, ap,bp);
515 }
516
517 static void mfp_hostaddr(adns_query qu, adns_rr_hostaddr *rrp) {
518 void *tablev;
519
520 adns__makefinal_str(qu,&rrp->host);
521 tablev= rrp->addrs;
522 adns__makefinal_block(qu, &tablev, rrp->naddrs*sizeof(*rrp->addrs));
523 rrp->addrs= tablev;
524 }
525
526 static void mf_hostaddr(adns_query qu, void *datap) {
527 adns_rr_hostaddr *rrp= datap;
528
529 mfp_hostaddr(qu,rrp);
530 }
531
532 static adns_status csp_hostaddr(vbuf *vb, const adns_rr_hostaddr *rrp) {
533 const char *to_add;
534 adns_status st;
535 int i;
536
537 st= csp_qstring(vb,rrp->host); if (st) return st;
538
539 if (rrp->astatus) {
540 CSP_ADDSTR(" - ");
541 CSP_ADDSTR(adns_strerror(rrp->astatus));
542 } else {
543 assert(rrp->naddrs > 0);
544 for (i=0, to_add= ": "; i<rrp->naddrs; i++, to_add= ", ") {
545 CSP_ADDSTR(to_add);
546 st= csp_addr(vb,&rrp->addrs[i]);
547 }
548 }
549 return adns_s_ok;
550 }
551
552 static adns_status cs_hostaddr(vbuf *vb, const void *datap) {
553 const adns_rr_hostaddr *rrp= datap;
554
555 return csp_hostaddr(vb,rrp);
556 }
557
558 /*
559 * _mx_raw (pa,di)
560 */
561
562 static adns_status pa_mx_raw(const parseinfo *pai, int cbyte, int max, void *datap) {
563 const byte *dgram= pai->dgram;
564 adns_rr_intstr *rrp= datap;
565 adns_status st;
566 int pref;
567
568 if (cbyte+2 > max) return adns_s_invaliddata;
569 GET_W(cbyte,pref);
570 rrp->i= pref;
571 st= pap_domain(pai, &cbyte, max, &rrp->str,
572 pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
573 if (st) return st;
574
575 if (cbyte != max) return adns_s_invaliddata;
576 return adns_s_ok;
577 }
578
579 static int di_mx_raw(adns_state ads, const void *datap_a, const void *datap_b) {
580 const adns_rr_intstr *ap= datap_a, *bp= datap_b;
581
582 if (ap->i < bp->i) return 0;
583 if (ap->i > bp->i) return 1;
584 return 0;
585 }
586
587 /*
588 * _mx (pa,di)
589 */
590
591 static adns_status pa_mx(const parseinfo *pai, int cbyte, int max, void *datap) {
592 const byte *dgram= pai->dgram;
593 adns_rr_inthostaddr *rrp= datap;
594 adns_status st;
595 int pref;
596
597 if (cbyte+2 > max) return adns_s_invaliddata;
598 GET_W(cbyte,pref);
599 rrp->i= pref;
600 st= pap_hostaddr(pai, &cbyte, max, &rrp->ha);
601 if (st) return st;
602
603 if (cbyte != max) return adns_s_invaliddata;
604 return adns_s_ok;
605 }
606
607 static int di_mx(adns_state ads, const void *datap_a, const void *datap_b) {
608 const adns_rr_inthostaddr *ap= datap_a, *bp= datap_b;
609
610 if (ap->i < bp->i) return 0;
611 if (ap->i > bp->i) return 1;
612 return dip_hostaddr(ads, &ap->ha, &bp->ha);
613 }
614
615 /*
616 * _inthostaddr (mf,cs)
617 */
618
619 static void mf_inthostaddr(adns_query qu, void *datap) {
620 adns_rr_inthostaddr *rrp= datap;
621
622 mfp_hostaddr(qu,&rrp->ha);
623 }
624
625 static adns_status cs_inthostaddr(vbuf *vb, const void *datap) {
626 const adns_rr_inthostaddr *rrp= datap;
627 char buf[10];
628
629 sprintf(buf,"%u ",rrp->i);
630 CSP_ADDSTR(buf);
631
632 return csp_hostaddr(vb,&rrp->ha);
633 }
634
635 /*
636 * _ptr (pa, +icb_ptr)
637 */
638
639 static void icb_ptr(adns_query parent, adns_query child) {
640 adns_answer *cans= child->answer;
641 const adns_rr_addr *queried, *found;
642 adns_state ads= parent->ads;
643 int i;
644
645 if (cans->status == adns_s_nxdomain || cans->status == adns_s_nodata) {
646 adns__query_fail(parent,adns_s_inconsistent);
647 return;
648 } else if (cans->status) {
649 adns__query_fail(parent,cans->status);
650 return;
651 }
652
653 queried= &parent->ctx.info.ptr_parent_addr;
654 for (i=0, found=cans->rrs.addr; i<cans->nrrs; i++, found++) {
655 if (queried->len == found->len &&
656 !memcmp(&queried->addr,&found->addr,queried->len)) {
657 if (!parent->children.head) {
658 adns__query_done(parent);
659 return;
660 } else {
661 LIST_LINK_TAIL(ads->childw,parent);
662 return;
663 }
664 }
665 }
666
667 adns__query_fail(parent,adns_s_inconsistent);
668 }
669
670 static adns_status pa_ptr(const parseinfo *pai, int dmstart, int max, void *datap) {
671 static const char *(expectdomain[])= { DNS_INADDR_ARPA };
672
673 char **rrp= datap;
674 adns_status st;
675 adns_rr_addr *ap;
676 findlabel_state fls;
677 char *ep;
678 byte ipv[4];
679 char labbuf[4];
680 int cbyte, i, lablen, labstart, l, id;
681 adns_query nqu;
682 qcontext ctx;
683
684 cbyte= dmstart;
685 st= pap_domain(pai, &cbyte, max, rrp,
686 pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
687 if (st) return st;
688 if (cbyte != max) return adns_s_invaliddata;
689
690 ap= &pai->qu->ctx.info.ptr_parent_addr;
691 if (!ap->len) {
692 adns__findlabel_start(&fls, pai->ads, -1, pai->qu,
693 pai->qu->query_dgram, pai->qu->query_dglen,
694 pai->qu->query_dglen, DNS_HDRSIZE, 0);
695 for (i=0; i<4; i++) {
696 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
697 if (lablen<=0 || lablen>3) return adns_s_querydomainwrong;
698 memcpy(labbuf, pai->qu->query_dgram + labstart, lablen); labbuf[lablen]= 0;
699 ipv[3-i]= strtoul(labbuf,&ep,10); if (*ep) return adns_s_querydomainwrong;
700 if (lablen>1 && pai->qu->query_dgram[labstart]=='0')
701 return adns_s_querydomainwrong;
702 }
703 for (i=0; i<sizeof(expectdomain)/sizeof(*expectdomain); i++) {
704 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
705 l= strlen(expectdomain[i]);
706 if (lablen != l || memcmp(pai->qu->query_dgram + labstart, expectdomain[i], l))
707 return adns_s_querydomainwrong;
708 }
709 st= adns__findlabel_next(&fls,&lablen,0); assert(!st);
710 if (lablen) return adns_s_querydomainwrong;
711
712 ap->len= sizeof(struct sockaddr_in);
713 memset(&ap->addr,0,sizeof(ap->addr.inet));
714 ap->addr.inet.sin_family= AF_INET;
715 ap->addr.inet.sin_addr.s_addr=
716 htonl((ipv[0]<<24) | (ipv[1]<<16) | (ipv[2]<<8) | (ipv[3]));
717 }
718
719 st= adns__mkquery_frdgram(pai->ads, &pai->qu->vb, &id,
720 pai->dgram, pai->dglen, dmstart,
721 adns_r_addr, adns_qf_quoteok_query);
722 if (st) return st;
723
724 ctx.ext= 0;
725 ctx.callback= icb_ptr;
726 memset(&ctx.info,0,sizeof(ctx.info));
727 st= adns__internal_submit(pai->ads, &nqu, adns__findtype(adns_r_addr),
728 &pai->qu->vb, id,
729 adns_qf_quoteok_query, pai->now, &ctx);
730 if (st) return st;
731
732 nqu->parent= pai->qu;
733 LIST_LINK_TAIL_PART(pai->qu->children,nqu,siblings.);
734 return adns_s_ok;
735 }
736
737 /*
738 * _strpair (mf,cs)
739 */
740
741 static void mf_strpair(adns_query qu, void *datap) {
742 adns_rr_strpair *rrp= datap;
743
744 adns__makefinal_str(qu,&rrp->array[0]);
745 adns__makefinal_str(qu,&rrp->array[1]);
746 }
747
748 static adns_status cs_strpair(vbuf *vb, const void *datap) {
749 const adns_rr_strpair *rrp= datap;
750 adns_status st;
751
752 st= csp_qstring(vb,rrp->array[0]); if (st) return st;
753 CSP_ADDSTR(" ");
754 st= csp_qstring(vb,rrp->array[1]); if (st) return st;
755
756 return adns_s_ok;
757 }
758
759 /*
760 * _intstrpair (mf,cs)
761 */
762
763 static void mf_intstrpair(adns_query qu, void *datap) {
764 adns_rr_intstrpair *rrp= datap;
765
766 adns__makefinal_str(qu,&rrp->array[0].str);
767 adns__makefinal_str(qu,&rrp->array[1].str);
768 }
769
770 static adns_status cs_intstrpair(vbuf *vb, const void *datap) {
771 const adns_rr_intstrpair *rrp= datap;
772 adns_status st;
773
774 st= csp_intstr(vb,&rrp->array[0]); if (st) return st;
775 CSP_ADDSTR(" ");
776 st= csp_intstr(vb,&rrp->array[1]); if (st) return st;
777
778 return adns_s_ok;
779 }
780
781 /*
782 * _hinfo (pa)
783 */
784
785 static adns_status pa_hinfo(const parseinfo *pai, int cbyte, int max, void *datap) {
786 adns_rr_intstrpair *rrp= datap;
787 adns_status st;
788 int i;
789
790 for (i=0; i<2; i++) {
791 st= pap_string(pai, &cbyte, max, &rrp->array[i].i, &rrp->array[i].str);
792 if (st) return st;
793 }
794
795 if (cbyte != max) return adns_s_invaliddata;
796
797 return adns_s_ok;
798 }
799
800 /*
801 * _mailbox (pap)
802 */
803
804 static adns_status pap_mailbox822(const parseinfo *pai, int *cbyte_io, int max,
805 char **mb_r) {
806 int lablen, labstart, i, needquote, c, r, neednorm;
807 const unsigned char *p;
808 char *str;
809 findlabel_state fls;
810 adns_status st;
811 vbuf *vb;
812
813 vb= &pai->qu->vb;
814 vb->used= 0;
815 adns__findlabel_start(&fls, pai->ads,
816 -1, pai->qu,
817 pai->dgram, pai->dglen, max,
818 *cbyte_io, cbyte_io);
819 st= adns__findlabel_next(&fls,&lablen,&labstart);
820 if (!lablen) {
821 adns__vbuf_appendstr(vb,"<>");
822 goto x_ok;
823 }
824
825 neednorm= 1;
826 for (i=0, needquote=0, p= pai->dgram+labstart; i<lablen; i++) {
827 c= *p++;
828 if ((c&~128) < 32 || (c&~128) == 127) return adns_s_invaliddata;
829 if (c == '.' && !neednorm) neednorm= 1;
830 else if (strchr("()<>@,;:\\\".[]",c)) needquote++;
831 else neednorm= 0;
832 }
833
834 if (needquote || neednorm) {
835 r= adns__vbuf_ensure(vb, lablen+needquote+4); if (!r) R_NOMEM;
836 adns__vbuf_appendq(vb,"\"",1);
837 for (i=0, needquote=0, p= pai->dgram+labstart; i<lablen; i++, p++) {
838 c= *p;
839 if (strchr("()<>@,;:\\\".[]",c)) adns__vbuf_appendq(vb,"\\",1);
840 adns__vbuf_appendq(vb,p,1);
841 }
842 adns__vbuf_appendq(vb,"\"",1);
843 } else {
844 r= adns__vbuf_append(vb, pai->dgram+labstart, lablen); if (!r) R_NOMEM;
845 }
846
847 r= adns__vbuf_appendstr(vb,"@"); if (!r) R_NOMEM;
848
849 st= adns__parse_domain_more(&fls,pai->ads, pai->qu,vb,0, pai->dgram);
850 if (st) return st;
851
852 x_ok:
853 str= adns__alloc_interim(pai->qu, vb->used+1); if (!str) R_NOMEM;
854 memcpy(str,vb->buf,vb->used);
855 str[vb->used]= 0;
856 *mb_r= str;
857 return adns_s_ok;
858 }
859
860 static adns_status pap_mailbox(const parseinfo *pai, int *cbyte_io, int max,
861 char **mb_r) {
862 if (pai->qu->typei->type & adns__qtf_mail822) {
863 return pap_mailbox822(pai, cbyte_io, max, mb_r);
864 } else {
865 return pap_domain(pai, cbyte_io, max, mb_r,
866 pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
867 }
868 }
869
870 /*
871 * _rp (pa)
872 */
873
874 static adns_status pa_rp(const parseinfo *pai, int cbyte, int max, void *datap) {
875 adns_rr_strpair *rrp= datap;
876 adns_status st;
877
878 st= pap_mailbox(pai, &cbyte, max, &rrp->array[0]);
879 if (st) return st;
880
881 st= pap_domain(pai, &cbyte, max, &rrp->array[1], pdf_quoteok);
882 if (st) return st;
883
884 if (cbyte != max) return adns_s_invaliddata;
885 return adns_s_ok;
886 }
887
888 /*
889 * _soa (pa,mf,cs)
890 */
891
892 static adns_status pa_soa(const parseinfo *pai, int cbyte, int max, void *datap) {
893 adns_rr_soa *rrp= datap;
894 const byte *dgram= pai->dgram;
895 adns_status st;
896 int msw, lsw, i;
897
898 st= pap_domain(pai, &cbyte, max, &rrp->mname,
899 pai->qu->flags & adns_qf_quoteok_anshost ? pdf_quoteok : 0);
900 if (st) return st;
901
902 st= pap_mailbox(pai, &cbyte, max, &rrp->rname);
903 if (st) return st;
904
905 if (cbyte+20 != max) return adns_s_invaliddata;
906
907 for (i=0; i<5; i++) {
908 GET_W(cbyte,msw);
909 GET_W(cbyte,lsw);
910 (&rrp->serial)[i]= (msw<<16) | lsw;
911 }
912
913 return adns_s_ok;
914 }
915
916 static void mf_soa(adns_query qu, void *datap) {
917 adns_rr_soa *rrp= datap;
918
919 adns__makefinal_str(qu,&rrp->mname);
920 adns__makefinal_str(qu,&rrp->rname);
921 }
922
923 static adns_status cs_soa(vbuf *vb, const void *datap) {
924 const adns_rr_soa *rrp= datap;
925 char buf[20];
926 int i;
927 adns_status st;
928
929 st= csp_qstring(vb,rrp->mname); if (st) return st;
930 CSP_ADDSTR(" ");
931 st= csp_qstring(vb,rrp->rname); if (st) return st;
932
933 for (i=0; i<5; i++) {
934 sprintf(buf," %lu",(&rrp->serial)[i]);
935 CSP_ADDSTR(buf);
936 }
937
938 return adns_s_ok;
939 }
940
941 /*
942 * _flat (mf)
943 */
944
945 static void mf_flat(adns_query qu, void *data) { }
946
947 /*
948 * Now the table.
949 */
950
951 #define TYPESZ_M(member) (sizeof(*((adns_answer*)0)->rrs.member))
952
953 #define DEEP_MEMB(memb) TYPESZ_M(memb), mf_##memb, cs_##memb
954 #define FLAT_MEMB(memb) TYPESZ_M(memb), mf_flat, cs_##memb
955
956 #define DEEP_TYPE(code,rrt,fmt,memb,parser,comparer) \
957 { adns_r_##code, rrt, fmt, TYPESZ_M(memb), mf_##memb, cs_##memb, parser, comparer }
958 #define FLAT_TYPE(code,rrt,fmt,memb,parser,comparer) \
959 { adns_r_##code, rrt, fmt, TYPESZ_M(memb), mf_flat, cs_##memb, parser, comparer }
960
961 static const typeinfo typeinfos[] = {
962 /* Must be in ascending order of rrtype ! */
963 /* mem-mgmt code rrt fmt member parser comparer */
964
965 FLAT_TYPE(a, "A", 0, inaddr, pa_inaddr, di_inaddr ),
966 DEEP_TYPE(ns_raw, "NS", "raw", str, pa_host_raw, 0 ),
967 DEEP_TYPE(cname, "CNAME", 0, str, pa_host_raw, 0 ),
968 DEEP_TYPE(soa_raw, "SOA", "raw", soa, pa_soa, 0 ),
969 DEEP_TYPE(ptr_raw, "PTR", "raw", str, pa_host_raw, 0 ),
970 DEEP_TYPE(hinfo, "HINFO", 0, intstrpair, pa_hinfo, 0 ),
971 DEEP_TYPE(mx_raw, "MX", "raw", intstr, pa_mx_raw, di_mx_raw ),
972 DEEP_TYPE(txt, "TXT", 0, manyistr, pa_txt, 0 ),
973 DEEP_TYPE(rp_raw, "RP", "raw", strpair, pa_rp, 0 ),
974
975 FLAT_TYPE(addr, "A", "addr", addr, pa_addr, di_addr ),
976 DEEP_TYPE(ns, "NS", "+addr", hostaddr, pa_hostaddr, di_hostaddr ),
977 DEEP_TYPE(ptr, "PTR","checked", str, pa_ptr, 0 ),
978 DEEP_TYPE(mx, "MX", "+addr", inthostaddr, pa_mx, di_mx ),
979
980 DEEP_TYPE(soa, "SOA","822", soa, pa_soa, 0 ),
981 DEEP_TYPE(rp, "RP", "822", strpair, pa_rp, 0 ),
982 };
983
984 const typeinfo *adns__findtype(adns_rrtype type) {
985 const typeinfo *begin, *end, *mid;
986
987 begin= typeinfos; end= typeinfos+(sizeof(typeinfos)/sizeof(typeinfo));
988
989 while (begin < end) {
990 mid= begin + ((end-begin)>>1);
991 if (mid->type == type) return mid;
992 if (type > mid->type) begin= mid+1;
993 else end= mid;
994 }
995 return 0;
996 }