98db6da3 |
1 | /* |
2 | * parse.c |
3 | * - parsing assistance functions (mainly for domains inside datagrams) |
4 | */ |
5 | /* |
a79ac5ba |
6 | * This file is |
89435c42 |
7 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
a79ac5ba |
8 | * |
9 | * It is part of adns, which is |
89435c42 |
10 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
c6826df6 |
11 | * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
98db6da3 |
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 | */ |
5c596e4d |
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]; |
996e3b68 |
38 | if (ch <= ' ' || ch >= 127) { |
5c596e4d |
39 | sprintf(qbuf,"\\%03o",ch); |
40 | break; |
996e3b68 |
41 | } else if (!ctype_domainunquoted(ch)) { |
42 | sprintf(qbuf,"\\%c",ch); |
43 | break; |
5c596e4d |
44 | } |
45 | } |
9c344a42 |
46 | if (!adns__vbuf_append(vb,buf,i) || |
47 | !adns__vbuf_append(vb,qbuf,strlen(qbuf))) |
5c596e4d |
48 | return 0; |
61093792 |
49 | if (i<len) i++; |
50 | buf+= i; |
51 | len-= i; |
5c596e4d |
52 | } |
53 | return 1; |
54 | } |
55 | |
11c8bf9b |
56 | void adns__findlabel_start(findlabel_state *fls, adns_state ads, |
57 | int serv, adns_query qu, |
403fa0e0 |
58 | const byte *dgram, int dglen, int max, |
59 | int dmbegin, int *dmend_rlater) { |
60 | fls->ads= ads; |
11c8bf9b |
61 | fls->qu= qu; |
403fa0e0 |
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; |
403fa0e0 |
69 | } |
5c596e4d |
70 | |
11c8bf9b |
71 | adns_status adns__findlabel_next(findlabel_state *fls, |
403fa0e0 |
72 | int *lablen_r, int *labstart_r) { |
84a760b6 |
73 | int lablen, jumpto; |
11c8bf9b |
74 | const char *dgram; |
403fa0e0 |
75 | |
11c8bf9b |
76 | dgram= fls->dgram; |
5c596e4d |
77 | for (;;) { |
1e9efa71 |
78 | if (fls->cbyte >= fls->dglen) goto x_truncated; |
9b86645c |
79 | if (fls->cbyte >= fls->max) goto x_badresponse; |
1e9efa71 |
80 | GET_B(fls->cbyte,lablen); |
81 | if (!(lablen & 0x0c0)) break; |
9b86645c |
82 | if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownformat; |
1e9efa71 |
83 | if (fls->cbyte >= fls->dglen) goto x_truncated; |
9b86645c |
84 | if (fls->cbyte >= fls->max) goto x_badresponse; |
1e9efa71 |
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; |
9b86645c |
95 | if (fls->namelen > DNS_MAXDOMAIN) return adns_s_answerdomaintoolong; |
403fa0e0 |
96 | fls->cbyte+= lablen; |
97 | if (fls->cbyte > fls->dglen) goto x_truncated; |
9b86645c |
98 | if (fls->cbyte > fls->max) goto x_badresponse; |
403fa0e0 |
99 | } else { |
100 | if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte; |
5c596e4d |
101 | } |
5c596e4d |
102 | *lablen_r= lablen; |
103 | return adns_s_ok; |
104 | |
105 | x_truncated: |
106 | *lablen_r= -1; |
107 | return adns_s_ok; |
403fa0e0 |
108 | |
9b86645c |
109 | x_badresponse: |
9c344a42 |
110 | adns__diag(fls->ads,fls->serv,fls->qu, |
111 | "label in domain runs beyond end of domain"); |
9b86645c |
112 | return adns_s_invalidresponse; |
5c596e4d |
113 | } |
114 | |
11c8bf9b |
115 | adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu, |
abfa75eb |
116 | vbuf *vb, parsedomain_flags flags, |
9c344a42 |
117 | const byte *dgram, int dglen, int *cbyte_io, |
118 | int max) { |
403fa0e0 |
119 | findlabel_state fls; |
120 | |
9c344a42 |
121 | adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max, |
122 | *cbyte_io,cbyte_io); |
403fa0e0 |
123 | vb->used= 0; |
6f2c243f |
124 | return adns__parse_domain_more(&fls,ads,qu, vb,flags,dgram); |
125 | } |
126 | |
127 | adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads, |
9c344a42 |
128 | adns_query qu, vbuf *vb, |
129 | parsedomain_flags flags, |
6f2c243f |
130 | const byte *dgram) { |
131 | int lablen, labstart, i, ch, first; |
132 | adns_status st; |
133 | |
134 | first= 1; |
5c596e4d |
135 | for (;;) { |
6f2c243f |
136 | st= adns__findlabel_next(fls,&lablen,&labstart); |
5c596e4d |
137 | if (st) return st; |
403fa0e0 |
138 | if (lablen<0) { vb->used=0; return adns_s_ok; } |
5c596e4d |
139 | if (!lablen) break; |
6f2c243f |
140 | if (first) { |
141 | first= 0; |
142 | } else { |
9b86645c |
143 | if (!adns__vbuf_append(vb,".",1)) return adns_s_nomemory; |
6f2c243f |
144 | } |
cd363ffd |
145 | if (flags & pdf_quoteok) { |
11c8bf9b |
146 | if (!vbuf__append_quoted1035(vb,dgram+labstart,lablen)) |
9b86645c |
147 | return adns_s_nomemory; |
5c596e4d |
148 | } else { |
cfdca685 |
149 | ch= dgram[labstart]; |
9c344a42 |
150 | if (!ctype_alpha(ch) && !ctype_digit(ch)) |
151 | return adns_s_answerdomaininvalid; |
403fa0e0 |
152 | for (i= labstart+1; i<labstart+lablen; i++) { |
153 | ch= dgram[i]; |
5c596e4d |
154 | if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch)) |
9b86645c |
155 | return adns_s_answerdomaininvalid; |
5c596e4d |
156 | } |
11c8bf9b |
157 | if (!adns__vbuf_append(vb,dgram+labstart,lablen)) |
9b86645c |
158 | return adns_s_nomemory; |
5c596e4d |
159 | } |
160 | } |
9b86645c |
161 | if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory; |
5c596e4d |
162 | return adns_s_ok; |
5c596e4d |
163 | } |
7f702335 |
164 | |
26eb6bdc |
165 | adns_status adns__findrr_anychk(adns_query qu, int serv, |
166 | const byte *dgram, int dglen, int *cbyte_io, |
9c344a42 |
167 | int *type_r, int *class_r, |
168 | unsigned long *ttl_r, |
2c7b101b |
169 | int *rdlen_r, int *rdstart_r, |
9c344a42 |
170 | const byte *eo_dgram, int eo_dglen, |
171 | int eo_cbyte, int *eo_matched_r) { |
403fa0e0 |
172 | findlabel_state fls, eo_fls; |
173 | int cbyte; |
174 | |
175 | int tmp, rdlen, mismatch; |
2c7b101b |
176 | unsigned long ttl; |
11c8bf9b |
177 | int lablen, labstart, ch; |
178 | int eo_lablen, eo_labstart, eo_ch; |
5c596e4d |
179 | adns_status st; |
180 | |
181 | cbyte= *cbyte_io; |
5c596e4d |
182 | |
11c8bf9b |
183 | adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte); |
403fa0e0 |
184 | if (eo_dgram) { |
9c344a42 |
185 | adns__findlabel_start(&eo_fls,qu->ads, -1,0, |
186 | eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0); |
403fa0e0 |
187 | mismatch= 0; |
188 | } else { |
189 | mismatch= 1; |
190 | } |
191 | |
5c596e4d |
192 | for (;;) { |
403fa0e0 |
193 | st= adns__findlabel_next(&fls,&lablen,&labstart); |
5c596e4d |
194 | if (st) return st; |
195 | if (lablen<0) goto x_truncated; |
196 | |
197 | if (!mismatch) { |
403fa0e0 |
198 | st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart); |
199 | assert(!st); assert(eo_lablen>=0); |
5c596e4d |
200 | if (lablen != eo_lablen) mismatch= 1; |
1e9efa71 |
201 | while (!mismatch && eo_lablen-- > 0) { |
5c596e4d |
202 | ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32; |
203 | eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32; |
204 | if (ch != eo_ch) mismatch= 1; |
205 | } |
206 | } |
1e9efa71 |
207 | if (!lablen) break; |
5c596e4d |
208 | } |
209 | if (eo_matched_r) *eo_matched_r= !mismatch; |
210 | |
211 | if (cbyte+10>dglen) goto x_truncated; |
212 | GET_W(cbyte,tmp); *type_r= tmp; |
213 | GET_W(cbyte,tmp); *class_r= tmp; |
2c7b101b |
214 | |
215 | GET_L(cbyte,ttl); |
216 | if (ttl > MAXTTLBELIEVE) ttl= MAXTTLBELIEVE; |
217 | *ttl_r= ttl; |
218 | |
1e9efa71 |
219 | GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen; |
5c596e4d |
220 | if (rdstart_r) *rdstart_r= cbyte; |
221 | cbyte+= rdlen; |
222 | if (cbyte>dglen) goto x_truncated; |
223 | *cbyte_io= cbyte; |
224 | return adns_s_ok; |
225 | |
226 | x_truncated: |
227 | *type_r= -1; |
1e9efa71 |
228 | return 0; |
5c596e4d |
229 | } |
11c8bf9b |
230 | |
231 | adns_status adns__findrr(adns_query qu, int serv, |
232 | const byte *dgram, int dglen, int *cbyte_io, |
2c7b101b |
233 | int *type_r, int *class_r, unsigned long *ttl_r, |
234 | int *rdlen_r, int *rdstart_r, |
11c8bf9b |
235 | int *ownermatchedquery_r) { |
236 | if (!ownermatchedquery_r) { |
26eb6bdc |
237 | return adns__findrr_anychk(qu,serv, |
238 | dgram,dglen,cbyte_io, |
2c7b101b |
239 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
26eb6bdc |
240 | 0,0,0, 0); |
11c8bf9b |
241 | } else if (!qu->cname_dgram) { |
26eb6bdc |
242 | return adns__findrr_anychk(qu,serv, |
243 | dgram,dglen,cbyte_io, |
2c7b101b |
244 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
26eb6bdc |
245 | qu->query_dgram,qu->query_dglen,DNS_HDRSIZE, |
246 | ownermatchedquery_r); |
11c8bf9b |
247 | } else { |
26eb6bdc |
248 | return adns__findrr_anychk(qu,serv, |
249 | dgram,dglen,cbyte_io, |
2c7b101b |
250 | type_r,class_r,ttl_r,rdlen_r,rdstart_r, |
26eb6bdc |
251 | qu->cname_dgram,qu->cname_dglen,qu->cname_begin, |
252 | ownermatchedquery_r); |
11c8bf9b |
253 | } |
254 | } |