e576be50 |
1 | /* |
2 | * parse.c |
3 | * - parsing assistance functions (mainly for domains inside datagrams) |
4 | */ |
5 | /* |
d942707d |
6 | * This file is |
7 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
8 | * |
9 | * It is part of adns, which is |
10 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
11 | * Copyright (C) 1999 Tony Finch <dot@dotat.at> |
e576be50 |
12 | * |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by |
15 | * the Free Software Foundation; either version 2, or (at your option) |
16 | * any later version. |
17 | * |
18 | * This program is distributed in the hope that it will be useful, |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | * GNU General Public License for more details. |
22 | * |
23 | * You should have received a copy of the GNU General Public License |
24 | * along with this program; if not, write to the Free Software Foundation, |
25 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
26 | */ |
98a3f706 |
27 | |
28 | #include "internal.h" |
29 | |
30 | int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len) { |
31 | char qbuf[10]; |
32 | int i, ch; |
33 | |
34 | while (len) { |
35 | qbuf[0]= 0; |
36 | for (i=0; i<len; i++) { |
37 | ch= buf[i]; |
38 | if (ch == '.' || ch == '"' || ch == '(' || ch == ')' || |
9ec44266 |
39 | ch == '@' || ch == ';' || ch == '$' || ch == '\\') { |
98a3f706 |
40 | sprintf(qbuf,"\\%c",ch); |
41 | break; |
42 | } else if (ch <= ' ' || ch >= 127) { |
43 | sprintf(qbuf,"\\%03o",ch); |
44 | break; |
45 | } |
46 | } |
47 | if (!adns__vbuf_append(vb,buf,i) || !adns__vbuf_append(vb,qbuf,strlen(qbuf))) |
48 | return 0; |
9ec44266 |
49 | if (i<len) i++; |
50 | buf+= i; |
51 | len-= i; |
98a3f706 |
52 | } |
53 | return 1; |
54 | } |
55 | |
3955725c |
56 | void adns__findlabel_start(findlabel_state *fls, adns_state ads, |
57 | int serv, adns_query qu, |
f1e474dd |
58 | const byte *dgram, int dglen, int max, |
59 | int dmbegin, int *dmend_rlater) { |
60 | fls->ads= ads; |
3955725c |
61 | fls->qu= qu; |
f1e474dd |
62 | fls->serv= serv; |
63 | fls->dgram= dgram; |
64 | fls->dglen= dglen; |
65 | fls->max= max; |
66 | fls->cbyte= dmbegin; |
67 | fls->namelen= 0; |
68 | fls->dmend_r= dmend_rlater; |
f1e474dd |
69 | } |
98a3f706 |
70 | |
3955725c |
71 | adns_status adns__findlabel_next(findlabel_state *fls, |
f1e474dd |
72 | int *lablen_r, int *labstart_r) { |
86e7b8d9 |
73 | int lablen, jumped, jumpto; |
3955725c |
74 | const char *dgram; |
f1e474dd |
75 | |
76 | jumped= 0; |
3955725c |
77 | dgram= fls->dgram; |
98a3f706 |
78 | for (;;) { |
86e7b8d9 |
79 | if (fls->cbyte >= fls->dglen) goto x_truncated; |
ea1e31e3 |
80 | if (fls->cbyte >= fls->max) goto x_badresponse; |
86e7b8d9 |
81 | GET_B(fls->cbyte,lablen); |
82 | if (!(lablen & 0x0c0)) break; |
ea1e31e3 |
83 | if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownformat; |
f1e474dd |
84 | if (jumped++) { |
3955725c |
85 | adns__diag(fls->ads,fls->serv,fls->qu,"compressed datagram contains loop"); |
ea1e31e3 |
86 | return adns_s_invalidresponse; |
f1e474dd |
87 | } |
86e7b8d9 |
88 | if (fls->cbyte >= fls->dglen) goto x_truncated; |
ea1e31e3 |
89 | if (fls->cbyte >= fls->max) goto x_badresponse; |
86e7b8d9 |
90 | GET_B(fls->cbyte,jumpto); |
91 | jumpto |= (lablen&0x3f)<<8; |
f1e474dd |
92 | if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte; |
86e7b8d9 |
93 | fls->cbyte= jumpto; |
f1e474dd |
94 | fls->dmend_r= 0; fls->max= fls->dglen+1; |
98a3f706 |
95 | } |
86e7b8d9 |
96 | if (labstart_r) *labstart_r= fls->cbyte; |
98a3f706 |
97 | if (lablen) { |
f1e474dd |
98 | if (fls->namelen) fls->namelen++; |
99 | fls->namelen+= lablen; |
ea1e31e3 |
100 | if (fls->namelen > DNS_MAXDOMAIN) return adns_s_answerdomaintoolong; |
f1e474dd |
101 | fls->cbyte+= lablen; |
102 | if (fls->cbyte > fls->dglen) goto x_truncated; |
ea1e31e3 |
103 | if (fls->cbyte > fls->max) goto x_badresponse; |
f1e474dd |
104 | } else { |
105 | if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte; |
98a3f706 |
106 | } |
98a3f706 |
107 | *lablen_r= lablen; |
86e7b8d9 |
108 | /*if (labstart_r) fprintf(stderr,"label %d >%.*s<\n",lablen,lablen,fls->dgram+*labstart_r);*/ |
98a3f706 |
109 | return adns_s_ok; |
110 | |
111 | x_truncated: |
112 | *lablen_r= -1; |
113 | return adns_s_ok; |
f1e474dd |
114 | |
ea1e31e3 |
115 | x_badresponse: |
3955725c |
116 | adns__diag(fls->ads,fls->serv,fls->qu,"label in domain runs beyond end of domain"); |
ea1e31e3 |
117 | return adns_s_invalidresponse; |
98a3f706 |
118 | } |
119 | |
3955725c |
120 | adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu, |
828d89bd |
121 | vbuf *vb, adns_queryflags flags, |
3955725c |
122 | const byte *dgram, int dglen, int *cbyte_io, int max) { |
f1e474dd |
123 | findlabel_state fls; |
124 | |
3955725c |
125 | adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max, *cbyte_io,cbyte_io); |
f1e474dd |
126 | vb->used= 0; |
eaa44731 |
127 | return adns__parse_domain_more(&fls,ads,qu, vb,flags,dgram); |
128 | } |
129 | |
130 | adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads, |
131 | adns_query qu, vbuf *vb, parsedomain_flags flags, |
132 | const byte *dgram) { |
133 | int lablen, labstart, i, ch, first; |
134 | adns_status st; |
135 | |
136 | first= 1; |
98a3f706 |
137 | for (;;) { |
eaa44731 |
138 | st= adns__findlabel_next(fls,&lablen,&labstart); |
98a3f706 |
139 | if (st) return st; |
f1e474dd |
140 | if (lablen<0) { vb->used=0; return adns_s_ok; } |
98a3f706 |
141 | if (!lablen) break; |
eaa44731 |
142 | if (first) { |
143 | first= 0; |
144 | } else { |
ea1e31e3 |
145 | if (!adns__vbuf_append(vb,".",1)) return adns_s_nomemory; |
eaa44731 |
146 | } |
828d89bd |
147 | if (flags & pdf_quoteok) { |
3955725c |
148 | if (!vbuf__append_quoted1035(vb,dgram+labstart,lablen)) |
ea1e31e3 |
149 | return adns_s_nomemory; |
98a3f706 |
150 | } else { |
ffbda80c |
151 | ch= dgram[labstart]; |
ea1e31e3 |
152 | if (!ctype_alpha(ch) && !ctype_digit(ch)) return adns_s_answerdomaininvalid; |
f1e474dd |
153 | for (i= labstart+1; i<labstart+lablen; i++) { |
154 | ch= dgram[i]; |
98a3f706 |
155 | if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch)) |
ea1e31e3 |
156 | return adns_s_answerdomaininvalid; |
98a3f706 |
157 | } |
3955725c |
158 | if (!adns__vbuf_append(vb,dgram+labstart,lablen)) |
ea1e31e3 |
159 | return adns_s_nomemory; |
98a3f706 |
160 | } |
161 | } |
ea1e31e3 |
162 | if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory; |
98a3f706 |
163 | return adns_s_ok; |
98a3f706 |
164 | } |
31144a72 |
165 | |
1dfe95d8 |
166 | adns_status adns__findrr_anychk(adns_query qu, int serv, |
167 | const byte *dgram, int dglen, int *cbyte_io, |
73dba56e |
168 | int *type_r, int *class_r, unsigned long *ttl_r, |
169 | int *rdlen_r, int *rdstart_r, |
1dfe95d8 |
170 | const byte *eo_dgram, int eo_dglen, int eo_cbyte, |
171 | int *eo_matched_r) { |
f1e474dd |
172 | findlabel_state fls, eo_fls; |
173 | int cbyte; |
174 | |
175 | int tmp, rdlen, mismatch; |
73dba56e |
176 | unsigned long ttl; |
3955725c |
177 | int lablen, labstart, ch; |
178 | int eo_lablen, eo_labstart, eo_ch; |
98a3f706 |
179 | adns_status st; |
180 | |
181 | cbyte= *cbyte_io; |
98a3f706 |
182 | |
3955725c |
183 | adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte); |
f1e474dd |
184 | if (eo_dgram) { |
3955725c |
185 | adns__findlabel_start(&eo_fls,qu->ads, -1,0, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0); |
f1e474dd |
186 | mismatch= 0; |
187 | } else { |
188 | mismatch= 1; |
189 | } |
190 | |
98a3f706 |
191 | for (;;) { |
f1e474dd |
192 | st= adns__findlabel_next(&fls,&lablen,&labstart); |
98a3f706 |
193 | if (st) return st; |
194 | if (lablen<0) goto x_truncated; |
195 | |
196 | if (!mismatch) { |
f1e474dd |
197 | st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart); |
198 | assert(!st); assert(eo_lablen>=0); |
98a3f706 |
199 | if (lablen != eo_lablen) mismatch= 1; |
86e7b8d9 |
200 | while (!mismatch && eo_lablen-- > 0) { |
98a3f706 |
201 | ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32; |
202 | eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32; |
203 | if (ch != eo_ch) mismatch= 1; |
204 | } |
205 | } |
86e7b8d9 |
206 | if (!lablen) break; |
98a3f706 |
207 | } |
208 | if (eo_matched_r) *eo_matched_r= !mismatch; |
209 | |
210 | if (cbyte+10>dglen) goto x_truncated; |
211 | GET_W(cbyte,tmp); *type_r= tmp; |
212 | GET_W(cbyte,tmp); *class_r= tmp; |
73dba56e |
213 | |
214 | GET_L(cbyte,ttl); |
215 | if (ttl > MAXTTLBELIEVE) ttl= MAXTTLBELIEVE; |
216 | *ttl_r= ttl; |
217 | |
86e7b8d9 |
218 | GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen; |
98a3f706 |
219 | if (rdstart_r) *rdstart_r= cbyte; |
220 | cbyte+= rdlen; |
221 | if (cbyte>dglen) goto x_truncated; |
222 | *cbyte_io= cbyte; |
223 | return adns_s_ok; |
224 | |
225 | x_truncated: |
226 | *type_r= -1; |
86e7b8d9 |
227 | return 0; |
98a3f706 |
228 | } |
3955725c |
229 | |
230 | adns_status adns__findrr(adns_query qu, int serv, |
231 | const byte *dgram, int dglen, int *cbyte_io, |
73dba56e |
232 | int *type_r, int *class_r, unsigned long *ttl_r, |
233 | int *rdlen_r, int *rdstart_r, |
3955725c |
234 | int *ownermatchedquery_r) { |
235 | if (!ownermatchedquery_r) { |
1dfe95d8 |
236 | return adns__findrr_anychk(qu,serv, |
237 | dgram,dglen,cbyte_io, |
73dba56e |
238 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
1dfe95d8 |
239 | 0,0,0, 0); |
3955725c |
240 | } else if (!qu->cname_dgram) { |
1dfe95d8 |
241 | return adns__findrr_anychk(qu,serv, |
242 | dgram,dglen,cbyte_io, |
73dba56e |
243 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
1dfe95d8 |
244 | qu->query_dgram,qu->query_dglen,DNS_HDRSIZE, |
245 | ownermatchedquery_r); |
3955725c |
246 | } else { |
1dfe95d8 |
247 | return adns__findrr_anychk(qu,serv, |
248 | dgram,dglen,cbyte_io, |
73dba56e |
249 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
1dfe95d8 |
250 | qu->cname_dgram,qu->cname_dglen,qu->cname_begin, |
251 | ownermatchedquery_r); |
3955725c |
252 | } |
253 | } |