Import changes from chiark: cvs rdiff -u -r tochiark-1998-11-08 -r
[adns] / src / parse.c
CommitLineData
e576be50 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 */
98a3f706 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 == ')' ||
34 ch == '@' || ch == ';' || ch == '$') {
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;
44 buf+= i; len-= i;
45 }
46 return 1;
47}
48
3955725c 49void adns__findlabel_start(findlabel_state *fls, adns_state ads,
50 int serv, adns_query qu,
f1e474dd 51 const byte *dgram, int dglen, int max,
52 int dmbegin, int *dmend_rlater) {
53 fls->ads= ads;
3955725c 54 fls->qu= qu;
f1e474dd 55 fls->serv= serv;
56 fls->dgram= dgram;
57 fls->dglen= dglen;
58 fls->max= max;
59 fls->cbyte= dmbegin;
60 fls->namelen= 0;
61 fls->dmend_r= dmend_rlater;
f1e474dd 62}
98a3f706 63
3955725c 64adns_status adns__findlabel_next(findlabel_state *fls,
f1e474dd 65 int *lablen_r, int *labstart_r) {
86e7b8d9 66 int lablen, jumped, jumpto;
3955725c 67 const char *dgram;
f1e474dd 68
69 jumped= 0;
3955725c 70 dgram= fls->dgram;
98a3f706 71 for (;;) {
86e7b8d9 72 if (fls->cbyte >= fls->dglen) goto x_truncated;
73 if (fls->cbyte >= fls->max) goto x_serverfaulty;
74 GET_B(fls->cbyte,lablen);
75 if (!(lablen & 0x0c0)) break;
76 if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownreply;
f1e474dd 77 if (jumped++) {
3955725c 78 adns__diag(fls->ads,fls->serv,fls->qu,"compressed datagram contains loop");
f1e474dd 79 return adns_s_serverfaulty;
80 }
86e7b8d9 81 if (fls->cbyte >= fls->dglen) goto x_truncated;
82 if (fls->cbyte >= fls->max) goto x_serverfaulty;
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;
93 if (fls->namelen > DNS_MAXDOMAIN) return adns_s_domaintoolong;
94 fls->cbyte+= lablen;
95 if (fls->cbyte > fls->dglen) goto x_truncated;
96 if (fls->cbyte > fls->max) goto x_serverfaulty;
97 } else {
98 if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
98a3f706 99 }
98a3f706 100 *lablen_r= lablen;
86e7b8d9 101/*if (labstart_r) fprintf(stderr,"label %d >%.*s<\n",lablen,lablen,fls->dgram+*labstart_r);*/
98a3f706 102 return adns_s_ok;
103
104 x_truncated:
105 *lablen_r= -1;
106 return adns_s_ok;
f1e474dd 107
108 x_serverfaulty:
3955725c 109 adns__diag(fls->ads,fls->serv,fls->qu,"label in domain runs beyond end of domain");
f1e474dd 110 return adns_s_serverfaulty;
98a3f706 111}
112
3955725c 113adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu,
114 vbuf *vb, int flags,
115 const byte *dgram, int dglen, int *cbyte_io, int max) {
f1e474dd 116 findlabel_state fls;
117
3955725c 118 int lablen, labstart, i, ch;
98a3f706 119 adns_status st;
120
3955725c 121 adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max, *cbyte_io,cbyte_io);
f1e474dd 122 vb->used= 0;
98a3f706 123 for (;;) {
f1e474dd 124 st= adns__findlabel_next(&fls,&lablen,&labstart);
98a3f706 125 if (st) return st;
f1e474dd 126 if (lablen<0) { vb->used=0; return adns_s_ok; }
98a3f706 127 if (!lablen) break;
f1e474dd 128 if (vb->used)
3955725c 129 if (!adns__vbuf_append(vb,".",1)) return adns_s_nolocalmem;
31144a72 130 if (flags & adns_qf_anyquote) {
3955725c 131 if (!vbuf__append_quoted1035(vb,dgram+labstart,lablen))
98a3f706 132 return adns_s_nolocalmem;
133 } else {
134 if (!ctype_alpha(dgram[labstart])) return adns_s_invaliddomain;
f1e474dd 135 for (i= labstart+1; i<labstart+lablen; i++) {
136 ch= dgram[i];
98a3f706 137 if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
138 return adns_s_invaliddomain;
139 }
3955725c 140 if (!adns__vbuf_append(vb,dgram+labstart,lablen))
98a3f706 141 return adns_s_nolocalmem;
142 }
143 }
3955725c 144 if (!adns__vbuf_append(vb,"",1)) return adns_s_nolocalmem;
98a3f706 145 return adns_s_ok;
98a3f706 146}
31144a72 147
3955725c 148static adns_status findrr_intern(adns_query qu, int serv,
149 const byte *dgram, int dglen, int *cbyte_io,
150 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
151 const byte *eo_dgram, int eo_dglen, int eo_cbyte,
152 int *eo_matched_r) {
153 /* Like adns__findrr_checked, except that the datagram to compare
154 * with can be specified explicitly.
f1e474dd 155 *
156 * If the caller thinks they know what the owner of the RR ought to
157 * be they can pass in details in eo_*: this is another (or perhaps
158 * the same datagram), and a pointer to where the putative owner
159 * starts in that datagram. In this case *eo_matched_r will be set
160 * to 1 if the datagram matched or 0 if it did not. Either
161 * both eo_dgram and eo_matched_r must both be non-null, or they
162 * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
163 * The eo datagram and contained owner domain MUST be valid and
164 * untruncated.
98a3f706 165 */
f1e474dd 166 findlabel_state fls, eo_fls;
167 int cbyte;
168
169 int tmp, rdlen, mismatch;
3955725c 170 int lablen, labstart, ch;
171 int eo_lablen, eo_labstart, eo_ch;
98a3f706 172 adns_status st;
173
174 cbyte= *cbyte_io;
98a3f706 175
3955725c 176 adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte);
f1e474dd 177 if (eo_dgram) {
3955725c 178 adns__findlabel_start(&eo_fls,qu->ads, -1,0, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
f1e474dd 179 mismatch= 0;
180 } else {
181 mismatch= 1;
182 }
183
98a3f706 184 for (;;) {
f1e474dd 185 st= adns__findlabel_next(&fls,&lablen,&labstart);
98a3f706 186 if (st) return st;
187 if (lablen<0) goto x_truncated;
188
189 if (!mismatch) {
f1e474dd 190 st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart);
191 assert(!st); assert(eo_lablen>=0);
98a3f706 192 if (lablen != eo_lablen) mismatch= 1;
86e7b8d9 193 while (!mismatch && eo_lablen-- > 0) {
98a3f706 194 ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32;
195 eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32;
196 if (ch != eo_ch) mismatch= 1;
197 }
198 }
86e7b8d9 199 if (!lablen) break;
98a3f706 200 }
201 if (eo_matched_r) *eo_matched_r= !mismatch;
202
203 if (cbyte+10>dglen) goto x_truncated;
204 GET_W(cbyte,tmp); *type_r= tmp;
205 GET_W(cbyte,tmp); *class_r= tmp;
206 cbyte+= 4; /* we skip the TTL */
86e7b8d9 207 GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen;
98a3f706 208 if (rdstart_r) *rdstart_r= cbyte;
209 cbyte+= rdlen;
210 if (cbyte>dglen) goto x_truncated;
211 *cbyte_io= cbyte;
212 return adns_s_ok;
213
214 x_truncated:
215 *type_r= -1;
86e7b8d9 216 return 0;
98a3f706 217}
3955725c 218
219adns_status adns__findrr(adns_query qu, int serv,
220 const byte *dgram, int dglen, int *cbyte_io,
221 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
222 int *ownermatchedquery_r) {
223 if (!ownermatchedquery_r) {
224 return findrr_intern(qu,serv,
225 dgram,dglen,cbyte_io,
226 type_r,class_r,rdlen_r,rdstart_r,
227 0,0,0, 0);
228 } else if (!qu->cname_dgram) {
229 return findrr_intern(qu,serv,
230 dgram,dglen,cbyte_io,
231 type_r,class_r,rdlen_r,rdstart_r,
232 qu->query_dgram,qu->query_dglen,DNS_HDRSIZE,
233 ownermatchedquery_r);
234 } else {
235 return findrr_intern(qu,serv,
236 dgram,dglen,cbyte_io,
237 type_r,class_r,rdlen_r,rdstart_r,
238 qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
239 ownermatchedquery_r);
240 }
241}