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