Rearrangement more or less sorted, back to trying to get it to compile.
[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
f1e474dd 49void adns__findlabel_start(findlabel_state *fls,
50 adns_state ads, int serv,
51 const byte *dgram, int dglen, int max,
52 int dmbegin, int *dmend_rlater) {
53 fls->ads= ads;
54 fls->serv= serv;
55 fls->dgram= dgram;
56 fls->dglen= dglen;
57 fls->max= max;
58 fls->cbyte= dmbegin;
59 fls->namelen= 0;
60 fls->dmend_r= dmend_rlater;
61 fls->namelen_r= namelen_rlater;
62}
98a3f706 63
f1e474dd 64adns_status adns__findlabel_next(findlabel_state fls,
65 int *lablen_r, int *labstart_r) {
66 int lablen, jumped;
67
68 jumped= 0;
98a3f706 69 for (;;) {
f1e474dd 70 fls->cbyte += 2;
71 if (fls->cbyte > fls->dglen) goto x_truncated;
72 if (fls->cbyte > fls->max) goto x_serverfaulty;
73 GET_W(fls->cbyte-2,lablen);
98a3f706 74 if (!(lablen & 0x0c000)) break;
75 if ((lablen & 0x0c000) != 0x0c000) return adns_s_unknownreply;
f1e474dd 76 if (jumped++) {
68442019 77 adns__diag(ads,fls->serv,fls->qu,"compressed datagram contains loop");
f1e474dd 78 return adns_s_serverfaulty;
79 }
80 if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
81 fls->cbyte= DNS_HDRSIZE+(lablen&0x3fff);
82 fls->dmend_r= 0; fls->max= fls->dglen+1;
98a3f706 83 }
98a3f706 84 if (lablen) {
f1e474dd 85 if (fls->namelen) fls->namelen++;
86 fls->namelen+= lablen;
87 if (fls->namelen > DNS_MAXDOMAIN) return adns_s_domaintoolong;
88 fls->cbyte+= lablen;
89 if (fls->cbyte > fls->dglen) goto x_truncated;
90 if (fls->cbyte > fls->max) goto x_serverfaulty;
91 } else {
92 if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
93 if (fls->namelen_r) *(fls->namelen_r)= fls->namelen;
98a3f706 94 }
f1e474dd 95 if (labstart_r) *labstart_r= fls->cbyte;
98a3f706 96 *lablen_r= lablen;
97 return adns_s_ok;
98
99 x_truncated:
100 *lablen_r= -1;
101 return adns_s_ok;
f1e474dd 102
103 x_serverfaulty:
68442019 104 adns__diag(ads,fls->serv,fls->qu,"label in domain runs beyond end of domain");
f1e474dd 105 return adns_s_serverfaulty;
98a3f706 106}
107
31144a72 108adns_status adns__parse_domain(adns_state ads, int serv, vbuf *vb, int flags,
f1e474dd 109 const byte *dgram, int dglen,
110 int *cbyte_io, int max) {
111 findlabel_state fls;
112
113 int cbyte, lablen, labstart, namelen, i, ch;
98a3f706 114 adns_status st;
115
f1e474dd 116 ands__findlabel_start(&fls,ads,serv, dgram,dglen,max, *cbyte_io,cbyte_io);
117 vb->used= 0;
98a3f706 118 for (;;) {
f1e474dd 119 st= adns__findlabel_next(&fls,&lablen,&labstart);
98a3f706 120 if (st) return st;
f1e474dd 121 if (lablen<0) { vb->used=0; return adns_s_ok; }
98a3f706 122 if (!lablen) break;
f1e474dd 123 if (vb->used)
98a3f706 124 if (!adns__vbuf_append(&qu->ans,".",1)) return adns_s_nolocalmem;
31144a72 125 if (flags & adns_qf_anyquote) {
98a3f706 126 if (!vbuf__append_quoted1035(&qu->ans,dgram+labstart,lablen))
127 return adns_s_nolocalmem;
128 } else {
129 if (!ctype_alpha(dgram[labstart])) return adns_s_invaliddomain;
f1e474dd 130 for (i= labstart+1; i<labstart+lablen; i++) {
131 ch= dgram[i];
98a3f706 132 if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
133 return adns_s_invaliddomain;
134 }
135 if (!adns__vbuf_append(&qu->ans,dgram+labstart,lablen))
136 return adns_s_nolocalmem;
137 }
138 }
98a3f706 139 if (!adns__vbuf_append(&qu->ans,"",1)) return adns_s_nolocalmem;
98a3f706 140 return adns_s_ok;
98a3f706 141}
31144a72 142
f1e474dd 143adns_status adns__findrr(adns_state ads, int serv,
144 const byte *dgram, int dglen, int *cbyte_io,
145 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
146 const byte *eo_dgram, int eo_dglen, int eo_cbyte,
147 int *eo_matched_r) {
148 /* Finds the extent and some of the contents of an RR in a datagram
149 * and does some checks. The datagram is *dgram, length dglen, and
150 * the RR starts at *cbyte_io (which is updated afterwards to point
151 * to the end of the RR).
152 *
153 * The type, class and RRdata length and start are returned iff
154 * the corresponding pointer variables are not null. type_r and
155 * class_r may not be null.
156 *
157 * If the caller thinks they know what the owner of the RR ought to
158 * be they can pass in details in eo_*: this is another (or perhaps
159 * the same datagram), and a pointer to where the putative owner
160 * starts in that datagram. In this case *eo_matched_r will be set
161 * to 1 if the datagram matched or 0 if it did not. Either
162 * both eo_dgram and eo_matched_r must both be non-null, or they
163 * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
164 * The eo datagram and contained owner domain MUST be valid and
165 * untruncated.
166 *
167 * If there is truncation then *type_r will be set to -1 and
168 * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
169 * undefined.
170 *
171 * If an error is returned then *type_r will be undefined too.
98a3f706 172 */
f1e474dd 173 findlabel_state fls, eo_fls;
174 int cbyte;
175
176 int tmp, rdlen, mismatch;
98a3f706 177 int max, lablen, labstart, namelen, ch;
178 int eo_max, eo_lablen, eo_labstart, eo_namelen, eo_ch;
179 adns_status st;
180
181 cbyte= *cbyte_io;
98a3f706 182
f1e474dd 183 ands__findlabel_start(&fls,ads,serv, dgram,dglen,dglen,cbyte,&cbyte);
184 if (eo_dgram) {
185 ands__findlabel_start(&eo_fls,ads,serv, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
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;
200 while (!mismatch && lablen-- > 0) {
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 }
206 }
207 if (eo_matched_r) *eo_matched_r= !mismatch;
208
209 if (cbyte+10>dglen) goto x_truncated;
210 GET_W(cbyte,tmp); *type_r= tmp;
211 GET_W(cbyte,tmp); *class_r= tmp;
212 cbyte+= 4; /* we skip the TTL */
213 GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= tmp;
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;
222 return 0;;
223}