e576be50 |
1 | /* |
2 | * parse.c |
3 | * - parsing assistance functions (mainly for domains inside datagrams) |
4 | */ |
5 | /* |
a719a4be |
6 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
e576be50 |
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 | */ |
98a3f706 |
22 | |
23 | #include "internal.h" |
24 | |
25 | int 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 == ')' || |
9ec44266 |
34 | ch == '@' || ch == ';' || ch == '$' || ch == '\\') { |
98a3f706 |
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; |
9ec44266 |
44 | if (i<len) i++; |
45 | buf+= i; |
46 | len-= i; |
98a3f706 |
47 | } |
48 | return 1; |
49 | } |
50 | |
3955725c |
51 | void adns__findlabel_start(findlabel_state *fls, adns_state ads, |
52 | int serv, adns_query qu, |
f1e474dd |
53 | const byte *dgram, int dglen, int max, |
54 | int dmbegin, int *dmend_rlater) { |
55 | fls->ads= ads; |
3955725c |
56 | fls->qu= qu; |
f1e474dd |
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; |
f1e474dd |
64 | } |
98a3f706 |
65 | |
3955725c |
66 | adns_status adns__findlabel_next(findlabel_state *fls, |
f1e474dd |
67 | int *lablen_r, int *labstart_r) { |
86e7b8d9 |
68 | int lablen, jumped, jumpto; |
3955725c |
69 | const char *dgram; |
f1e474dd |
70 | |
71 | jumped= 0; |
3955725c |
72 | dgram= fls->dgram; |
98a3f706 |
73 | for (;;) { |
86e7b8d9 |
74 | if (fls->cbyte >= fls->dglen) goto x_truncated; |
ea1e31e3 |
75 | if (fls->cbyte >= fls->max) goto x_badresponse; |
86e7b8d9 |
76 | GET_B(fls->cbyte,lablen); |
77 | if (!(lablen & 0x0c0)) break; |
ea1e31e3 |
78 | if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownformat; |
f1e474dd |
79 | if (jumped++) { |
3955725c |
80 | adns__diag(fls->ads,fls->serv,fls->qu,"compressed datagram contains loop"); |
ea1e31e3 |
81 | return adns_s_invalidresponse; |
f1e474dd |
82 | } |
86e7b8d9 |
83 | if (fls->cbyte >= fls->dglen) goto x_truncated; |
ea1e31e3 |
84 | if (fls->cbyte >= fls->max) goto x_badresponse; |
86e7b8d9 |
85 | GET_B(fls->cbyte,jumpto); |
86 | jumpto |= (lablen&0x3f)<<8; |
f1e474dd |
87 | if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte; |
86e7b8d9 |
88 | fls->cbyte= jumpto; |
f1e474dd |
89 | fls->dmend_r= 0; fls->max= fls->dglen+1; |
98a3f706 |
90 | } |
86e7b8d9 |
91 | if (labstart_r) *labstart_r= fls->cbyte; |
98a3f706 |
92 | if (lablen) { |
f1e474dd |
93 | if (fls->namelen) fls->namelen++; |
94 | fls->namelen+= lablen; |
ea1e31e3 |
95 | if (fls->namelen > DNS_MAXDOMAIN) return adns_s_answerdomaintoolong; |
f1e474dd |
96 | fls->cbyte+= lablen; |
97 | if (fls->cbyte > fls->dglen) goto x_truncated; |
ea1e31e3 |
98 | if (fls->cbyte > fls->max) goto x_badresponse; |
f1e474dd |
99 | } else { |
100 | if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte; |
98a3f706 |
101 | } |
98a3f706 |
102 | *lablen_r= lablen; |
86e7b8d9 |
103 | /*if (labstart_r) fprintf(stderr,"label %d >%.*s<\n",lablen,lablen,fls->dgram+*labstart_r);*/ |
98a3f706 |
104 | return adns_s_ok; |
105 | |
106 | x_truncated: |
107 | *lablen_r= -1; |
108 | return adns_s_ok; |
f1e474dd |
109 | |
ea1e31e3 |
110 | x_badresponse: |
3955725c |
111 | adns__diag(fls->ads,fls->serv,fls->qu,"label in domain runs beyond end of domain"); |
ea1e31e3 |
112 | return adns_s_invalidresponse; |
98a3f706 |
113 | } |
114 | |
3955725c |
115 | adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu, |
828d89bd |
116 | vbuf *vb, adns_queryflags flags, |
3955725c |
117 | const byte *dgram, int dglen, int *cbyte_io, int max) { |
f1e474dd |
118 | findlabel_state fls; |
119 | |
3955725c |
120 | adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max, *cbyte_io,cbyte_io); |
f1e474dd |
121 | vb->used= 0; |
eaa44731 |
122 | return adns__parse_domain_more(&fls,ads,qu, vb,flags,dgram); |
123 | } |
124 | |
125 | adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads, |
126 | adns_query qu, vbuf *vb, parsedomain_flags flags, |
127 | const byte *dgram) { |
128 | int lablen, labstart, i, ch, first; |
129 | adns_status st; |
130 | |
131 | first= 1; |
98a3f706 |
132 | for (;;) { |
eaa44731 |
133 | st= adns__findlabel_next(fls,&lablen,&labstart); |
98a3f706 |
134 | if (st) return st; |
f1e474dd |
135 | if (lablen<0) { vb->used=0; return adns_s_ok; } |
98a3f706 |
136 | if (!lablen) break; |
eaa44731 |
137 | if (first) { |
138 | first= 0; |
139 | } else { |
ea1e31e3 |
140 | if (!adns__vbuf_append(vb,".",1)) return adns_s_nomemory; |
eaa44731 |
141 | } |
828d89bd |
142 | if (flags & pdf_quoteok) { |
3955725c |
143 | if (!vbuf__append_quoted1035(vb,dgram+labstart,lablen)) |
ea1e31e3 |
144 | return adns_s_nomemory; |
98a3f706 |
145 | } else { |
ffbda80c |
146 | ch= dgram[labstart]; |
ea1e31e3 |
147 | if (!ctype_alpha(ch) && !ctype_digit(ch)) return adns_s_answerdomaininvalid; |
f1e474dd |
148 | for (i= labstart+1; i<labstart+lablen; i++) { |
149 | ch= dgram[i]; |
98a3f706 |
150 | if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch)) |
ea1e31e3 |
151 | return adns_s_answerdomaininvalid; |
98a3f706 |
152 | } |
3955725c |
153 | if (!adns__vbuf_append(vb,dgram+labstart,lablen)) |
ea1e31e3 |
154 | return adns_s_nomemory; |
98a3f706 |
155 | } |
156 | } |
ea1e31e3 |
157 | if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory; |
98a3f706 |
158 | return adns_s_ok; |
98a3f706 |
159 | } |
31144a72 |
160 | |
1dfe95d8 |
161 | adns_status adns__findrr_anychk(adns_query qu, int serv, |
162 | const byte *dgram, int dglen, int *cbyte_io, |
73dba56e |
163 | int *type_r, int *class_r, unsigned long *ttl_r, |
164 | int *rdlen_r, int *rdstart_r, |
1dfe95d8 |
165 | const byte *eo_dgram, int eo_dglen, int eo_cbyte, |
166 | int *eo_matched_r) { |
f1e474dd |
167 | findlabel_state fls, eo_fls; |
168 | int cbyte; |
169 | |
170 | int tmp, rdlen, mismatch; |
73dba56e |
171 | unsigned long ttl; |
3955725c |
172 | int lablen, labstart, ch; |
173 | int eo_lablen, eo_labstart, eo_ch; |
98a3f706 |
174 | adns_status st; |
175 | |
176 | cbyte= *cbyte_io; |
98a3f706 |
177 | |
3955725c |
178 | adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte); |
f1e474dd |
179 | if (eo_dgram) { |
3955725c |
180 | adns__findlabel_start(&eo_fls,qu->ads, -1,0, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0); |
f1e474dd |
181 | mismatch= 0; |
182 | } else { |
183 | mismatch= 1; |
184 | } |
185 | |
98a3f706 |
186 | for (;;) { |
f1e474dd |
187 | st= adns__findlabel_next(&fls,&lablen,&labstart); |
98a3f706 |
188 | if (st) return st; |
189 | if (lablen<0) goto x_truncated; |
190 | |
191 | if (!mismatch) { |
f1e474dd |
192 | st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart); |
193 | assert(!st); assert(eo_lablen>=0); |
98a3f706 |
194 | if (lablen != eo_lablen) mismatch= 1; |
86e7b8d9 |
195 | while (!mismatch && eo_lablen-- > 0) { |
98a3f706 |
196 | ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32; |
197 | eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32; |
198 | if (ch != eo_ch) mismatch= 1; |
199 | } |
200 | } |
86e7b8d9 |
201 | if (!lablen) break; |
98a3f706 |
202 | } |
203 | if (eo_matched_r) *eo_matched_r= !mismatch; |
204 | |
205 | if (cbyte+10>dglen) goto x_truncated; |
206 | GET_W(cbyte,tmp); *type_r= tmp; |
207 | GET_W(cbyte,tmp); *class_r= tmp; |
73dba56e |
208 | |
209 | GET_L(cbyte,ttl); |
210 | if (ttl > MAXTTLBELIEVE) ttl= MAXTTLBELIEVE; |
211 | *ttl_r= ttl; |
212 | |
86e7b8d9 |
213 | GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen; |
98a3f706 |
214 | if (rdstart_r) *rdstart_r= cbyte; |
215 | cbyte+= rdlen; |
216 | if (cbyte>dglen) goto x_truncated; |
217 | *cbyte_io= cbyte; |
218 | return adns_s_ok; |
219 | |
220 | x_truncated: |
221 | *type_r= -1; |
86e7b8d9 |
222 | return 0; |
98a3f706 |
223 | } |
3955725c |
224 | |
225 | adns_status adns__findrr(adns_query qu, int serv, |
226 | const byte *dgram, int dglen, int *cbyte_io, |
73dba56e |
227 | int *type_r, int *class_r, unsigned long *ttl_r, |
228 | int *rdlen_r, int *rdstart_r, |
3955725c |
229 | int *ownermatchedquery_r) { |
230 | if (!ownermatchedquery_r) { |
1dfe95d8 |
231 | return adns__findrr_anychk(qu,serv, |
232 | dgram,dglen,cbyte_io, |
73dba56e |
233 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
1dfe95d8 |
234 | 0,0,0, 0); |
3955725c |
235 | } else if (!qu->cname_dgram) { |
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 | qu->query_dgram,qu->query_dglen,DNS_HDRSIZE, |
240 | ownermatchedquery_r); |
3955725c |
241 | } else { |
1dfe95d8 |
242 | return adns__findrr_anychk(qu,serv, |
243 | dgram,dglen,cbyte_io, |
73dba56e |
244 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
1dfe95d8 |
245 | qu->cname_dgram,qu->cname_dglen,qu->cname_begin, |
246 | ownermatchedquery_r); |
3955725c |
247 | } |
248 | } |