Implement SOA, RP, HINFO (but no mailbox quoting). Implement _finish. Cancel childr...
[adns] / src / parse.c
CommitLineData
98db6da3 1/*
2 * parse.c
3 * - parsing assistance functions (mainly for domains inside datagrams)
4 */
5/*
6 * This file is part of adns, which is Copyright (C) 1997, 1998 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 */
5c596e4d 22
23#include "internal.h"
24
25int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len) {
26 char qbuf[10];
27 int i, ch;
28
29 while (len) {
30 qbuf[0]= 0;
31 for (i=0; i<len; i++) {
32 ch= buf[i];
33 if (ch == '.' || ch == '"' || ch == '(' || ch == ')' ||
61093792 34 ch == '@' || ch == ';' || ch == '$' || ch == '\\') {
5c596e4d 35 sprintf(qbuf,"\\%c",ch);
36 break;
37 } else if (ch <= ' ' || ch >= 127) {
38 sprintf(qbuf,"\\%03o",ch);
39 break;
40 }
41 }
42 if (!adns__vbuf_append(vb,buf,i) || !adns__vbuf_append(vb,qbuf,strlen(qbuf)))
43 return 0;
61093792 44 if (i<len) i++;
45 buf+= i;
46 len-= i;
5c596e4d 47 }
48 return 1;
49}
50
11c8bf9b 51void adns__findlabel_start(findlabel_state *fls, adns_state ads,
52 int serv, adns_query qu,
403fa0e0 53 const byte *dgram, int dglen, int max,
54 int dmbegin, int *dmend_rlater) {
55 fls->ads= ads;
11c8bf9b 56 fls->qu= qu;
403fa0e0 57 fls->serv= serv;
58 fls->dgram= dgram;
59 fls->dglen= dglen;
60 fls->max= max;
61 fls->cbyte= dmbegin;
62 fls->namelen= 0;
63 fls->dmend_r= dmend_rlater;
403fa0e0 64}
5c596e4d 65
11c8bf9b 66adns_status adns__findlabel_next(findlabel_state *fls,
403fa0e0 67 int *lablen_r, int *labstart_r) {
1e9efa71 68 int lablen, jumped, jumpto;
11c8bf9b 69 const char *dgram;
403fa0e0 70
71 jumped= 0;
11c8bf9b 72 dgram= fls->dgram;
5c596e4d 73 for (;;) {
1e9efa71 74 if (fls->cbyte >= fls->dglen) goto x_truncated;
75 if (fls->cbyte >= fls->max) goto x_serverfaulty;
76 GET_B(fls->cbyte,lablen);
77 if (!(lablen & 0x0c0)) break;
78 if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownreply;
403fa0e0 79 if (jumped++) {
11c8bf9b 80 adns__diag(fls->ads,fls->serv,fls->qu,"compressed datagram contains loop");
403fa0e0 81 return adns_s_serverfaulty;
82 }
1e9efa71 83 if (fls->cbyte >= fls->dglen) goto x_truncated;
84 if (fls->cbyte >= fls->max) goto x_serverfaulty;
85 GET_B(fls->cbyte,jumpto);
86 jumpto |= (lablen&0x3f)<<8;
403fa0e0 87 if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
1e9efa71 88 fls->cbyte= jumpto;
403fa0e0 89 fls->dmend_r= 0; fls->max= fls->dglen+1;
5c596e4d 90 }
1e9efa71 91 if (labstart_r) *labstart_r= fls->cbyte;
5c596e4d 92 if (lablen) {
403fa0e0 93 if (fls->namelen) fls->namelen++;
94 fls->namelen+= lablen;
95 if (fls->namelen > DNS_MAXDOMAIN) return adns_s_domaintoolong;
96 fls->cbyte+= lablen;
97 if (fls->cbyte > fls->dglen) goto x_truncated;
98 if (fls->cbyte > fls->max) goto x_serverfaulty;
99 } else {
100 if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
5c596e4d 101 }
5c596e4d 102 *lablen_r= lablen;
1e9efa71 103/*if (labstart_r) fprintf(stderr,"label %d >%.*s<\n",lablen,lablen,fls->dgram+*labstart_r);*/
5c596e4d 104 return adns_s_ok;
105
106 x_truncated:
107 *lablen_r= -1;
108 return adns_s_ok;
403fa0e0 109
110 x_serverfaulty:
11c8bf9b 111 adns__diag(fls->ads,fls->serv,fls->qu,"label in domain runs beyond end of domain");
403fa0e0 112 return adns_s_serverfaulty;
5c596e4d 113}
114
11c8bf9b 115adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu,
cd363ffd 116 vbuf *vb, adns_queryflags flags,
11c8bf9b 117 const byte *dgram, int dglen, int *cbyte_io, int max) {
403fa0e0 118 findlabel_state fls;
119
11c8bf9b 120 int lablen, labstart, i, ch;
5c596e4d 121 adns_status st;
122
11c8bf9b 123 adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max, *cbyte_io,cbyte_io);
403fa0e0 124 vb->used= 0;
5c596e4d 125 for (;;) {
403fa0e0 126 st= adns__findlabel_next(&fls,&lablen,&labstart);
5c596e4d 127 if (st) return st;
403fa0e0 128 if (lablen<0) { vb->used=0; return adns_s_ok; }
5c596e4d 129 if (!lablen) break;
403fa0e0 130 if (vb->used)
11c8bf9b 131 if (!adns__vbuf_append(vb,".",1)) return adns_s_nolocalmem;
cd363ffd 132 if (flags & pdf_quoteok) {
11c8bf9b 133 if (!vbuf__append_quoted1035(vb,dgram+labstart,lablen))
5c596e4d 134 return adns_s_nolocalmem;
135 } else {
cfdca685 136 ch= dgram[labstart];
137 if (!ctype_alpha(ch) && !ctype_digit(ch)) return adns_s_invalidanswerdomain;
403fa0e0 138 for (i= labstart+1; i<labstart+lablen; i++) {
139 ch= dgram[i];
5c596e4d 140 if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
cfdca685 141 return adns_s_invalidanswerdomain;
5c596e4d 142 }
11c8bf9b 143 if (!adns__vbuf_append(vb,dgram+labstart,lablen))
5c596e4d 144 return adns_s_nolocalmem;
145 }
146 }
11c8bf9b 147 if (!adns__vbuf_append(vb,"",1)) return adns_s_nolocalmem;
5c596e4d 148 return adns_s_ok;
5c596e4d 149}
7f702335 150
26eb6bdc 151adns_status adns__findrr_anychk(adns_query qu, int serv,
152 const byte *dgram, int dglen, int *cbyte_io,
153 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
154 const byte *eo_dgram, int eo_dglen, int eo_cbyte,
155 int *eo_matched_r) {
403fa0e0 156 findlabel_state fls, eo_fls;
157 int cbyte;
158
159 int tmp, rdlen, mismatch;
11c8bf9b 160 int lablen, labstart, ch;
161 int eo_lablen, eo_labstart, eo_ch;
5c596e4d 162 adns_status st;
163
164 cbyte= *cbyte_io;
5c596e4d 165
11c8bf9b 166 adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte);
403fa0e0 167 if (eo_dgram) {
11c8bf9b 168 adns__findlabel_start(&eo_fls,qu->ads, -1,0, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
403fa0e0 169 mismatch= 0;
170 } else {
171 mismatch= 1;
172 }
173
5c596e4d 174 for (;;) {
403fa0e0 175 st= adns__findlabel_next(&fls,&lablen,&labstart);
5c596e4d 176 if (st) return st;
177 if (lablen<0) goto x_truncated;
178
179 if (!mismatch) {
403fa0e0 180 st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart);
181 assert(!st); assert(eo_lablen>=0);
5c596e4d 182 if (lablen != eo_lablen) mismatch= 1;
1e9efa71 183 while (!mismatch && eo_lablen-- > 0) {
5c596e4d 184 ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32;
185 eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32;
186 if (ch != eo_ch) mismatch= 1;
187 }
188 }
1e9efa71 189 if (!lablen) break;
5c596e4d 190 }
191 if (eo_matched_r) *eo_matched_r= !mismatch;
192
193 if (cbyte+10>dglen) goto x_truncated;
194 GET_W(cbyte,tmp); *type_r= tmp;
195 GET_W(cbyte,tmp); *class_r= tmp;
196 cbyte+= 4; /* we skip the TTL */
1e9efa71 197 GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen;
5c596e4d 198 if (rdstart_r) *rdstart_r= cbyte;
199 cbyte+= rdlen;
200 if (cbyte>dglen) goto x_truncated;
201 *cbyte_io= cbyte;
202 return adns_s_ok;
203
204 x_truncated:
205 *type_r= -1;
1e9efa71 206 return 0;
5c596e4d 207}
11c8bf9b 208
209adns_status adns__findrr(adns_query qu, int serv,
210 const byte *dgram, int dglen, int *cbyte_io,
211 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
212 int *ownermatchedquery_r) {
213 if (!ownermatchedquery_r) {
26eb6bdc 214 return adns__findrr_anychk(qu,serv,
215 dgram,dglen,cbyte_io,
216 type_r,class_r,rdlen_r,rdstart_r,
217 0,0,0, 0);
11c8bf9b 218 } else if (!qu->cname_dgram) {
26eb6bdc 219 return adns__findrr_anychk(qu,serv,
220 dgram,dglen,cbyte_io,
221 type_r,class_r,rdlen_r,rdstart_r,
222 qu->query_dgram,qu->query_dglen,DNS_HDRSIZE,
223 ownermatchedquery_r);
11c8bf9b 224 } else {
26eb6bdc 225 return adns__findrr_anychk(qu,serv,
226 dgram,dglen,cbyte_io,
227 type_r,class_r,rdlen_r,rdstart_r,
228 qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
229 ownermatchedquery_r);
11c8bf9b 230 }
231}