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