ReactOS  0.4.14-dev-77-gd9e7c48
parse.c
Go to the documentation of this file.
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 
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 
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 
119  const byte *dgram, int dglen, int *cbyte_io, int max) {
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 
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))
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 
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 
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,
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 }
adns_status adns__findrr_anychk(adns_query qu, int serv, const byte *dgram, int dglen, int *cbyte_io, int *type_r, int *class_r, unsigned long *ttl_r, int *rdlen_r, int *rdstart_r, const byte *eo_dgram, int eo_dglen, int eo_cbyte, int *eo_matched_r)
Definition: parse.c:163
#define max(a, b)
Definition: svc.c:63
adns_state ads
Definition: adh-query.c:35
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
const GLint * first
Definition: glext.h:5794
parsedomain_flags
Definition: internal.h:577
int cname_begin
Definition: internal.h:213
adns_status adns__findrr(adns_query qu, int serv, const byte *dgram, int dglen, int *cbyte_io, int *type_r, int *class_r, unsigned long *ttl_r, int *rdlen_r, int *rdstart_r, int *ownermatchedquery_r)
Definition: parse.c:227
#define assert(x)
Definition: debug.h:53
#define GET_B(cb, tv)
Definition: internal.h:716
adns_status adns__findlabel_next(findlabel_state *fls, int *lablen_r, int *labstart_r)
Definition: parse.c:74
int cname_dglen
Definition: internal.h:213
#define DNS_HDRSIZE
Definition: internal.h:76
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static int fls(int x)
Definition: bitops.h:157
void adns__findlabel_start(findlabel_state *fls, adns_state ads, int serv, adns_query qu, const byte *dgram, int dglen, int max, int dmbegin, int *dmend_rlater)
Definition: parse.c:59
_STLP_INLINE_LOOP _STLP_STD::pair< _InputIter1, _InputIter2 > mismatch(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2)
Definition: _algobase.h:522
void adns__diag(adns_state ads, int serv, adns_query qu, const char *fmt,...)
Definition: general.c:102
adns_status
Definition: adns.h:215
int adns__vbuf_append(vbuf *vb, const byte *data, int len)
Definition: general.c:131
byte * cname_dgram
Definition: internal.h:212
static int ctype_domainunquoted(int c)
Definition: internal.h:703
adns_state ads
Definition: internal.h:182
static int cbyte
Definition: adnsresfilter.c:73
#define for
Definition: utility.h:88
#define MAXTTLBELIEVE
Definition: internal.h:70
GLbitfield flags
Definition: glext.h:7161
byte * query_dgram
Definition: internal.h:192
int query_dglen
Definition: internal.h:193
GLenum GLsizei len
Definition: glext.h:6722
Definition: internal.h:110
int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len)
Definition: parse.c:34
static int ctype_digit(int c)
Definition: internal.h:698
adns_queryflags
Definition: adns.h:100
#define GET_W(cb, tv)
Definition: internal.h:717
#define DNS_MAXDOMAIN
Definition: internal.h:75
int used
Definition: internal.h:111
#define GET_L(cb, tv)
Definition: internal.h:718
adns_status adns__parse_domain_more(findlabel_state *fls, adns_state ads, adns_query qu, vbuf *vb, parsedomain_flags flags, const byte *dgram)
Definition: parse.c:127
static int ctype_alpha(int c)
Definition: internal.h:699
adns_status adns__parse_domain(adns_state ads, int serv, adns_query qu, vbuf *vb, adns_queryflags flags, const byte *dgram, int dglen, int *cbyte_io, int max)
Definition: parse.c:117