chiark / gitweb /
ad23419d78319288c30fdb73abfb55ba4fa4b15a
[adns.git] / src / parse.c
1 /**/
2
3 #include "internal.h"
4
5 int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len) {
6   char qbuf[10];
7   int i, ch;
8   
9   while (len) {
10     qbuf[0]= 0;
11     for (i=0; i<len; i++) {
12       ch= buf[i];
13       if (ch == '.' || ch == '"' || ch == '(' || ch == ')' ||
14           ch == '@' || ch == ';' || ch == '$') {
15         sprintf(qbuf,"\\%c",ch);
16         break;
17       } else if (ch <= ' ' || ch >= 127) {
18         sprintf(qbuf,"\\%03o",ch);
19         break;
20       }
21     }
22     if (!adns__vbuf_append(vb,buf,i) || !adns__vbuf_append(vb,qbuf,strlen(qbuf)))
23       return 0;
24     buf+= i; len-= i;
25   }
26   return 1;
27 }
28
29 void adns__findlabel_start(findlabel_state *fls,
30                            adns_state ads, int serv,
31                            const byte *dgram, int dglen, int max,
32                            int dmbegin, int *dmend_rlater) {
33   fls->ads= ads;
34   fls->serv= serv;
35   fls->dgram= dgram;
36   fls->dglen= dglen;
37   fls->max= max;
38   fls->cbyte= dmbegin;
39   fls->namelen= 0;
40   fls->dmend_r= dmend_rlater;
41   fls->namelen_r= namelen_rlater;
42 }
43
44 adns_status adns__findlabel_next(findlabel_state fls,
45                                  int *lablen_r, int *labstart_r) {
46   int lablen, jumped;
47
48   jumped= 0;
49   for (;;) {
50     fls->cbyte += 2;
51     if (fls->cbyte > fls->dglen) goto x_truncated;
52     if (fls->cbyte > fls->max) goto x_serverfaulty;
53     GET_W(fls->cbyte-2,lablen);
54     if (!(lablen & 0x0c000)) break;
55     if ((lablen & 0x0c000) != 0x0c000) return adns_s_unknownreply;
56     if (jumped++) {
57       adns__diag(ads,serv,"compressed datagram contains loop");
58       return adns_s_serverfaulty;
59     }
60     if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
61     fls->cbyte= DNS_HDRSIZE+(lablen&0x3fff);
62     fls->dmend_r= 0; fls->max= fls->dglen+1;
63   }
64   if (lablen) {
65     if (fls->namelen) fls->namelen++;
66     fls->namelen+= lablen;
67     if (fls->namelen > DNS_MAXDOMAIN) return adns_s_domaintoolong;
68     fls->cbyte+= lablen;
69     if (fls->cbyte > fls->dglen) goto x_truncated;
70     if (fls->cbyte > fls->max) goto x_serverfaulty;
71   } else {
72     if (fls->dmend_r) *(fls->dmend_r)= fls->cbyte;
73     if (fls->namelen_r) *(fls->namelen_r)= fls->namelen;
74   }
75   if (labstart_r) *labstart_r= fls->cbyte;
76   *lablen_r= lablen;
77   return adns_s_ok;
78
79  x_truncated:
80   *lablen_r= -1;
81   return adns_s_ok;
82
83  x_serverfaulty: 
84   adns__diag(ads,serv,"label in domain runs beyond end of domain");
85   return adns_s_serverfaulty;
86 }
87
88 adns_status adns__parse_domain(adns_state ads, int serv, vbuf *vb,
89                                const byte *dgram, int dglen,
90                                int *cbyte_io, int max) {
91   findlabel_state fls;
92   
93   int cbyte, lablen, labstart, namelen, i, ch;
94   adns_status st;
95
96   ands__findlabel_start(&fls,ads,serv, dgram,dglen,max, *cbyte_io,cbyte_io);
97   vb->used= 0;
98   for (;;) {
99     st= adns__findlabel_next(&fls,&lablen,&labstart);
100     if (st) return st;
101     if (lablen<0) { vb->used=0; return adns_s_ok; }
102     if (!lablen) break;
103     if (vb->used)
104       if (!adns__vbuf_append(&qu->ans,".",1)) return adns_s_nolocalmem;
105     if (qu->flags & adns_qf_anyquote) {
106       if (!vbuf__append_quoted1035(&qu->ans,dgram+labstart,lablen))
107         return adns_s_nolocalmem;
108     } else {
109       if (!ctype_alpha(dgram[labstart])) return adns_s_invaliddomain;
110       for (i= labstart+1; i<labstart+lablen; i++) {
111         ch= dgram[i];
112         if (ch != '-' && !ctype_alpha(ch) && !ctype_digit(ch))
113           return adns_s_invaliddomain;
114       }
115       if (!adns__vbuf_append(&qu->ans,dgram+labstart,lablen))
116         return adns_s_nolocalmem;
117     }
118   }
119   if (!adns__vbuf_append(&qu->ans,"",1)) return adns_s_nolocalmem;
120   return adns_s_ok;
121 }
122     
123 adns_status adns__findrr(adns_state ads, int serv,
124                          const byte *dgram, int dglen, int *cbyte_io,
125                          int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
126                          const byte *eo_dgram, int eo_dglen, int eo_cbyte,
127                          int *eo_matched_r) {
128   /* Finds the extent and some of the contents of an RR in a datagram
129    * and does some checks.  The datagram is *dgram, length dglen, and
130    * the RR starts at *cbyte_io (which is updated afterwards to point
131    * to the end of the RR).
132    *
133    * The type, class and RRdata length and start are returned iff
134    * the corresponding pointer variables are not null.  type_r and
135    * class_r may not be null.
136    *
137    * If the caller thinks they know what the owner of the RR ought to
138    * be they can pass in details in eo_*: this is another (or perhaps
139    * the same datagram), and a pointer to where the putative owner
140    * starts in that datagram.  In this case *eo_matched_r will be set
141    * to 1 if the datagram matched or 0 if it did not.  Either
142    * both eo_dgram and eo_matched_r must both be non-null, or they
143    * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
144    * The eo datagram and contained owner domain MUST be valid and
145    * untruncated.
146    *
147    * If there is truncation then *type_r will be set to -1 and
148    * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
149    * undefined.
150    *
151    * If an error is returned then *type_r will be undefined too.
152    */
153   findlabel_state fls, eo_fls;
154   int cbyte;
155   
156   int tmp, rdlen, mismatch;
157   int max, lablen, labstart, namelen, ch;
158   int eo_max, eo_lablen, eo_labstart, eo_namelen, eo_ch;
159   adns_status st;
160
161   cbyte= *cbyte_io;
162
163   ands__findlabel_start(&fls,ads,serv, dgram,dglen,dglen,cbyte,&cbyte);
164   if (eo_dgram) {
165     ands__findlabel_start(&eo_fls,ads,serv, eo_dgram,eo_dglen,eo_dglen,eo_cbyte,0);
166     mismatch= 0;
167   } else {
168     mismatch= 1;
169   }
170   
171   for (;;) {
172     st= adns__findlabel_next(&fls,&lablen,&labstart);
173     if (st) return st;
174     if (lablen<0) goto x_truncated;
175
176     if (!mismatch) {
177       st= adns__findlabel_next(&eo_fls,&eo_lablen,&eo_labstart);
178       assert(!st); assert(eo_lablen>=0);
179       if (lablen != eo_lablen) mismatch= 1;
180       while (!mismatch && lablen-- > 0) {
181         ch= dgram[labstart++]; if (ctype_alpha(ch)) ch &= ~32;
182         eo_ch= eo_dgram[eo_labstart++]; if (ctype_alpha(eo_ch)) eo_ch &= ~32;
183         if (ch != eo_ch) mismatch= 1;
184       }
185     }
186   }
187   if (eo_matched_r) *eo_matched_r= !mismatch;
188    
189   if (cbyte+10>dglen) goto x_truncated;
190   GET_W(cbyte,tmp); *type_r= tmp;
191   GET_W(cbyte,tmp); *class_r= tmp;
192   cbyte+= 4; /* we skip the TTL */
193   GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= tmp;
194   if (rdstart_r) *rdstart_r= cbyte;
195   cbyte+= rdlen;
196   if (cbyte>dglen) goto x_truncated;
197   *cbyte_io= cbyte;
198   return adns_s_ok;
199
200  x_truncated:
201   *type_r= -1;
202   return 0;;
203 }