chiark / gitweb /
+ * Referrals with RD+RA set, or RCODE=Refused, don't generate warnings,
[adns.git] / src / parse.c
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 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 #include "internal.h"
29
30 int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len) {
31   char qbuf[10];
32   int i, ch;
33   
34   while (len) {
35     qbuf[0]= 0;
36     for (i=0; i<len; i++) {
37       ch= buf[i];
38       if (ch <= ' ' || ch >= 127) {
39         sprintf(qbuf,"\\%03o",ch);
40         break;
41       } else if (!ctype_domainunquoted(ch)) {
42         sprintf(qbuf,"\\%c",ch);
43         break;
44       }
45     }
46     if (!adns__vbuf_append(vb,buf,i) || !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, jumped, jumpto;
73   const char *dgram;
74
75   jumped= 0;
76   dgram= fls->dgram;
77   for (;;) {
78     if (fls->cbyte >= fls->dglen) goto x_truncated;
79     if (fls->cbyte >= fls->max) goto x_badresponse;
80     GET_B(fls->cbyte,lablen);
81     if (!(lablen & 0x0c0)) break;
82     if ((lablen & 0x0c0) != 0x0c0) return adns_s_unknownformat;
83     if (jumped++) {
84       adns__diag(fls->ads,fls->serv,fls->qu,"compressed datagram contains loop");
85       return adns_s_invalidresponse;
86     }
87     if (fls->cbyte >= fls->dglen) goto x_truncated;
88     if (fls->cbyte >= fls->max) goto x_badresponse;
89     GET_B(fls->cbyte,jumpto);
90     jumpto |= (lablen&0x3f)<<8;
91     if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
92     fls->cbyte= jumpto;
93     fls->dmend_r= 0; fls->max= fls->dglen+1;
94   }
95   if (labstart_r) *labstart_r= fls->cbyte;
96   if (lablen) {
97     if (fls->namelen) fls->namelen++;
98     fls->namelen+= lablen;
99     if (fls->namelen > DNS_MAXDOMAIN) return adns_s_answerdomaintoolong;
100     fls->cbyte+= lablen;
101     if (fls->cbyte > fls->dglen) goto x_truncated;
102     if (fls->cbyte > fls->max) goto x_badresponse;
103   } else {
104     if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
105   }
106   *lablen_r= lablen;
107 /*if (labstart_r) fprintf(stderr,"label %d >%.*s<\n",lablen,lablen,fls->dgram+*labstart_r);*/
108   return adns_s_ok;
109
110  x_truncated:
111   *lablen_r= -1;
112   return adns_s_ok;
113
114  x_badresponse: 
115   adns__diag(fls->ads,fls->serv,fls->qu,"label in domain runs beyond end of domain");
116   return adns_s_invalidresponse;
117 }
118
119 adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu,
120                                vbuf *vb, adns_queryflags flags,
121                                const byte *dgram, int dglen, int *cbyte_io, int max) {
122   findlabel_state fls;
123   
124   adns__findlabel_start(&fls,ads, serv,qu, dgram,dglen,max, *cbyte_io,cbyte_io);
125   vb->used= 0;
126   return adns__parse_domain_more(&fls,ads,qu, vb,flags,dgram);
127 }
128
129 adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads,
130                                     adns_query qu, vbuf *vb, parsedomain_flags flags,
131                                     const byte *dgram) {
132   int lablen, labstart, i, ch, first;
133   adns_status st;
134
135   first= 1;
136   for (;;) {
137     st= adns__findlabel_next(fls,&lablen,&labstart);
138     if (st) return st;
139     if (lablen<0) { vb->used=0; return adns_s_ok; }
140     if (!lablen) break;
141     if (first) {
142       first= 0;
143     } else {
144       if (!adns__vbuf_append(vb,".",1)) return adns_s_nomemory;
145     }
146     if (flags & pdf_quoteok) {
147       if (!vbuf__append_quoted1035(vb,dgram+labstart,lablen))
148         return adns_s_nomemory;
149     } else {
150       ch= dgram[labstart];
151       if (!ctype_alpha(ch) && !ctype_digit(ch)) return adns_s_answerdomaininvalid;
152       for (i= labstart+1; i<labstart+lablen; i++) {
153         ch= dgram[i];
154         if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
155           return adns_s_answerdomaininvalid;
156       }
157       if (!adns__vbuf_append(vb,dgram+labstart,lablen))
158         return adns_s_nomemory;
159     }
160   }
161   if (!adns__vbuf_append(vb,"",1)) return adns_s_nomemory;
162   return adns_s_ok;
163 }
164         
165 adns_status adns__findrr_anychk(adns_query qu, int serv,
166                                 const byte *dgram, int dglen, int *cbyte_io,
167                                 int *type_r, int *class_r, unsigned long *ttl_r,
168                                 int *rdlen_r, int *rdstart_r,
169                                 const byte *eo_dgram, int eo_dglen, int eo_cbyte,
170                                 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, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
185     mismatch= 0;
186   } else {
187     mismatch= 1;
188   }
189   
190   for (;;) {
191     st= adns__findlabel_next(&fls,&lablen,&labstart);
192     if (st) return st;
193     if (lablen<0) goto x_truncated;
194
195     if (!mismatch) {
196       st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart);
197       assert(!st); assert(eo_lablen>=0);
198       if (lablen != eo_lablen) mismatch= 1;
199       while (!mismatch && eo_lablen-- > 0) {
200         ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32;
201         eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32;
202         if (ch != eo_ch) mismatch= 1;
203       }
204     }
205     if (!lablen) break;
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
213   GET_L(cbyte,ttl);
214   if (ttl > MAXTTLBELIEVE) ttl= MAXTTLBELIEVE;
215   *ttl_r= ttl;
216   
217   GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= rdlen;
218   if (rdstart_r) *rdstart_r= cbyte;
219   cbyte+= rdlen;
220   if (cbyte>dglen) goto x_truncated;
221   *cbyte_io= cbyte;
222   return adns_s_ok;
223
224  x_truncated:
225   *type_r= -1;
226   return 0;
227 }
228
229 adns_status adns__findrr(adns_query qu, int serv,
230                          const byte *dgram, int dglen, int *cbyte_io,
231                          int *type_r, int *class_r, unsigned long *ttl_r,
232                          int *rdlen_r, int *rdstart_r,
233                          int *ownermatchedquery_r) {
234   if (!ownermatchedquery_r) {
235     return adns__findrr_anychk(qu,serv,
236                                dgram,dglen,cbyte_io,
237                                type_r,class_r,ttl_r,rdlen_r,rdstart_r,
238                                0,0,0, 0);
239   } else if (!qu->cname_dgram) {
240     return adns__findrr_anychk(qu,serv,
241                                dgram,dglen,cbyte_io,
242                                type_r,class_r,ttl_r,rdlen_r,rdstart_r,
243                                qu->query_dgram,qu->query_dglen,DNS_HDRSIZE,
244                                ownermatchedquery_r);
245   } else {
246     return adns__findrr_anychk(qu,serv,
247                                dgram,dglen,cbyte_io,
248                                type_r,class_r,ttl_r,rdlen_r,rdstart_r,
249                                qu->cname_dgram,qu->cname_dglen,qu->cname_begin,
250                                ownermatchedquery_r);
251   }
252 }