chiark / gitweb /
update copyright dates
[adns.git] / src / parse.c
1 /*
2  * parse.c
3  * - parsing assistance functions (mainly for domains inside datagrams)
4  */
5 /*
6  *  This file is part of adns, which is
7  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
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.)
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
14  *  the Free Software Foundation; either version 2, or (at your option)
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
23  *  along with this program; if not, write to the Free Software Foundation,
24  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
25  */
26
27 #include "internal.h"
28
29 int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len) {
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];
37       if (ch <= ' ' || ch >= 127) {
38         sprintf(qbuf,"\\%03o",ch);
39         break;
40       } else if (!ctype_domainunquoted(ch)) {
41         sprintf(qbuf,"\\%c",ch);
42         break;
43       }
44     }
45     if (!adns__vbuf_append(vb,buf,i) ||
46         !adns__vbuf_append(vb,qbuf,strlen(qbuf)))
47       return 0;
48     if (i<len) i++;
49     buf+= i;
50     len-= i;
51   }
52   return 1;
53 }
54
55 void adns__findlabel_start(findlabel_state *fls, adns_state ads,
56                            int serv, adns_query qu,
57                            const byte *dgram, int dglen, int max,
58                            int dmbegin, int *dmend_rlater) {
59   fls->ads= ads;
60   fls->qu= qu;
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;
68 }
69
70 adns_status adns__findlabel_next(findlabel_state *fls,
71                                  int *lablen_r, int *labstart_r) {
72   int lablen, jumpto;
73   const char *dgram;
74
75   dgram= fls->dgram;
76   for (;;) {
77     if (fls->cbyte >= fls->dglen) goto x_truncated;
78     if (fls->cbyte >= fls->max) goto x_badresponse;
79     GET_B(fls->cbyte,lablen);
80     if (!(lablen & 0x0c0)) break;
81     if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownformat;
82     if (fls->cbyte >= fls->dglen) goto x_truncated;
83     if (fls->cbyte >= fls->max) goto x_badresponse;
84     GET_B(fls->cbyte,jumpto);
85     jumpto |= (lablen&0x3f)<<8;
86     if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
87     fls->cbyte= jumpto;
88     fls->dmend_r= 0; fls->max= fls->dglen+1;
89   }
90   if (labstart_r) *labstart_r= fls->cbyte;
91   if (lablen) {
92     if (fls->namelen) fls->namelen++;
93     fls->namelen+= lablen;
94     if (fls->namelen > DNS_MAXDOMAIN) return adns_s_answerdomaintoolong;
95     fls->cbyte+= lablen;
96     if (fls->cbyte > fls->dglen) goto x_truncated;
97     if (fls->cbyte > fls->max) goto x_badresponse;
98   } else {
99     if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
100   }
101   *lablen_r= lablen;
102   return adns_s_ok;
103
104  x_truncated:
105   *lablen_r= -1;
106   return adns_s_ok;
107
108  x_badresponse: 
109   adns__diag(fls->ads,fls->serv,fls->qu,
110              "label in domain runs beyond end of domain");
111   return adns_s_invalidresponse;
112 }
113
114 adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu,
115                                vbuf *vb, parsedomain_flags flags,
116                                const byte *dgram, int dglen, int *cbyte_io,
117                                int max) {
118   findlabel_state fls;
119   
120   adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max,
121                         *cbyte_io,cbyte_io);
122   vb->used= 0;
123   return adns__parse_domain_more(&fls,ads,qu, vb,flags,dgram);
124 }
125
126 adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads,
127                                     adns_query qu, vbuf *vb,
128                                     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,".",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))
150         return adns_s_answerdomaininvalid;
151       for (i= labstart+1; i<labstart+lablen; i++) {
152         ch= dgram[i];
153         if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
154           return adns_s_answerdomaininvalid;
155       }
156       if (!adns__vbuf_append(vb,dgram+labstart,lablen))
157         return adns_s_nomemory;
158     }
159   }
160   if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory;
161   return adns_s_ok;
162 }
163         
164 adns_status adns__findrr_anychk(adns_query qu, int serv,
165                                 const byte *dgram, int dglen, int *cbyte_io,
166                                 int *type_r, int *class_r,
167                                 unsigned long *ttl_r,
168                                 int *rdlen_r, int *rdstart_r,
169                                 const byte *eo_dgram, int eo_dglen,
170                                 int eo_cbyte, int *eo_matched_r) {
171   findlabel_state fls, eo_fls;
172   int cbyte;
173   
174   int tmp, rdlen, mismatch;
175   unsigned long ttl;
176   int lablen, labstart, ch;
177   int eo_lablen, eo_labstart, eo_ch;
178   adns_status st;
179
180   cbyte= *cbyte_io;
181
182   adns__findlabel_start(&fls,qu->ads, serv,qu, dgram,dglen,dglen,cbyte,&cbyte);
183   if (eo_dgram) {
184     adns__findlabel_start(&eo_fls,qu->ads, -1,0,
185                           eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
186     mismatch= 0;
187   } else {
188     mismatch= 1;
189   }
190   
191   for (;;) {
192     st= adns__findlabel_next(&fls,&lablen,&labstart);
193     if (st) return st;
194     if (lablen<0) goto x_truncated;
195
196     if (!mismatch) {
197       st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart);
198       assert(!st); assert(eo_lablen>=0);
199       if (lablen != eo_lablen) mismatch= 1;
200       while (!mismatch && eo_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     if (!lablen) break;
207   }
208   if (eo_matched_r) *eo_matched_r= !mismatch;
209    
210   if (cbyte+10>dglen) goto x_truncated;
211   GET_W(cbyte,tmp); *type_r= tmp;
212   GET_W(cbyte,tmp); *class_r= tmp;
213
214   GET_L(cbyte,ttl);
215   if (ttl > MAXTTLBELIEVE) ttl= MAXTTLBELIEVE;
216   *ttl_r= ttl;
217   
218   GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen;
219   if (rdstart_r) *rdstart_r= cbyte;
220   cbyte+= rdlen;
221   if (cbyte>dglen) goto x_truncated;
222   *cbyte_io= cbyte;
223   return adns_s_ok;
224
225  x_truncated:
226   *type_r= -1;
227   return 0;
228 }
229
230 adns_status adns__findrr(adns_query qu, int serv,
231                          const byte *dgram, int dglen, int *cbyte_io,
232                          int *type_r, int *class_r, unsigned long *ttl_r,
233                          int *rdlen_r, int *rdstart_r,
234                          int *ownermatchedquery_r) {
235   if (!ownermatchedquery_r) {
236     return adns__findrr_anychk(qu,serv,
237                                dgram,dglen,cbyte_io,
238                                type_r,class_r,ttl_r,rdlen_r,rdstart_r,
239                                0,0,0, 0);
240   } else if (!qu->cname_dgram) {
241     return adns__findrr_anychk(qu,serv,
242                                dgram,dglen,cbyte_io,
243                                type_r,class_r,ttl_r,rdlen_r,rdstart_r,
244                                qu->query_dgram,qu->query_dglen,DNS_HDRSIZE,
245                                ownermatchedquery_r);
246   } else {
247     return adns__findrr_anychk(qu,serv,
248                                dgram,dglen,cbyte_io,
249                                type_r,class_r,ttl_r,rdlen_r,rdstart_r,
250                                qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
251                                ownermatchedquery_r);
252   }
253 }