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