chiark / gitweb /
957b57232994121321d1daab9632d889d9b5e906
[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, int flags,
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 (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 const char *adns__diag_domain(adns_state ads, int serv, vbuf *vb, int flags,
124                               const byte *dgram, int dglen, int cbyte) {
125   adns_status st;
126
127   st= adns__parse_domain(ads,serv,vb,qu->flags, dgram,dglen, &cbyte,dglen);
128   if (st) {
129     vb->used= 0;
130     adns__vbuf_appendstr(vb,"<bad format... ");
131     adns__vbuf_appendstr(vb,adns_strerror(st));
132     adns__vbuf_appendstr(vb,">");
133     adns__vbuf_append(vb,"",1);
134   }
135   if (!vb.used) {
136     adns__vbuf_appendstr(vb,"<truncated ...>");
137     adns__vbuf_append(vb,"",1);
138   }
139   return vb->buf;
140 }
141     
142 adns_status adns__findrr(adns_state ads, int serv,
143                          const byte *dgram, int dglen, int *cbyte_io,
144                          int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
145                          const byte *eo_dgram, int eo_dglen, int eo_cbyte,
146                          int *eo_matched_r) {
147   /* Finds the extent and some of the contents of an RR in a datagram
148    * and does some checks.  The datagram is *dgram, length dglen, and
149    * the RR starts at *cbyte_io (which is updated afterwards to point
150    * to the end of the RR).
151    *
152    * The type, class and RRdata length and start are returned iff
153    * the corresponding pointer variables are not null.  type_r and
154    * class_r may not be null.
155    *
156    * If the caller thinks they know what the owner of the RR ought to
157    * be they can pass in details in eo_*: this is another (or perhaps
158    * the same datagram), and a pointer to where the putative owner
159    * starts in that datagram.  In this case *eo_matched_r will be set
160    * to 1 if the datagram matched or 0 if it did not.  Either
161    * both eo_dgram and eo_matched_r must both be non-null, or they
162    * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
163    * The eo datagram and contained owner domain MUST be valid and
164    * untruncated.
165    *
166    * If there is truncation then *type_r will be set to -1 and
167    * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
168    * undefined.
169    *
170    * If an error is returned then *type_r will be undefined too.
171    */
172   findlabel_state fls, eo_fls;
173   int cbyte;
174   
175   int tmp, rdlen, mismatch;
176   int max, lablen, labstart, namelen, ch;
177   int eo_max, eo_lablen, eo_labstart, eo_namelen, eo_ch;
178   adns_status st;
179
180   cbyte= *cbyte_io;
181
182   ands__findlabel_start(&fls,ads,serv, dgram,dglen,dglen,cbyte,&cbyte);
183   if (eo_dgram) {
184     ands__findlabel_start(&eo_fls,ads,serv, 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 && 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   }
206   if (eo_matched_r) *eo_matched_r= !mismatch;
207    
208   if (cbyte+10>dglen) goto x_truncated;
209   GET_W(cbyte,tmp); *type_r= tmp;
210   GET_W(cbyte,tmp); *class_r= tmp;
211   cbyte+= 4; /* we skip the TTL */
212   GET_W(cbyte,rdlen); if (rdlen_r) *rdlen_r= tmp;
213   if (rdstart_r) *rdstart_r= cbyte;
214   cbyte+= rdlen;
215   if (cbyte>dglen) goto x_truncated;
216   *cbyte_io= cbyte;
217   return adns_s_ok;
218
219  x_truncated:
220   *type_r= -1;
221   return 0;;
222 }