ReactOS  0.4.13-dev-259-g5ca9c9c
reply.c
Go to the documentation of this file.
1 /*
2  * reply.c
3  * - main handling and parsing routine for received 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 <stdlib.h>
33 
34 #include "internal.h"
35 
36 void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
37  int serv, int viatcp, struct timeval now) {
38  int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
39  int id, f1, f2, qdcount, ancount, nscount, arcount;
40  int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
41  int rrtype, rrclass, rdlength, rdstart;
42  int anstart, nsstart, arstart;
43  int ownermatched, l, nrrs;
44  unsigned long ttl, soattl;
45  const typeinfo *typei;
46  adns_query qu, nqu;
48  adns_status st;
49  vbuf tempvb;
50  byte *newquery, *rrsdata;
51  parseinfo pai;
52 
53  if (dglen<DNS_HDRSIZE) {
54  adns__diag(ads,serv,0,"received datagram too short for message header (%d)",dglen);
55  return;
56  }
57  cbyte= 0;
58  GET_W(cbyte,id);
59  GET_B(cbyte,f1);
60  GET_B(cbyte,f2);
61  GET_W(cbyte,qdcount);
62  GET_W(cbyte,ancount);
63  GET_W(cbyte,nscount);
64  GET_W(cbyte,arcount);
66 
67  flg_qr= f1&0x80;
68  opcode= (f1&0x78)>>3;
69  flg_tc= f1&0x02;
70  flg_rd= f1&0x01;
71  flg_ra= f2&0x80;
72  rcode= (f2&0x0f);
73 
74  cname_here= 0;
75 
76  if (!flg_qr) {
77  adns__diag(ads,serv,0,"server sent us a query, not a response");
78  return;
79  }
80  if (opcode) {
81  adns__diag(ads,serv,0,"server sent us unknown opcode %d (wanted 0=QUERY)",opcode);
82  return;
83  }
84 
85  qu= 0;
86  /* See if we can find the relevant query, or leave qu=0 otherwise ... */
87 
88  if (qdcount == 1) {
89  for (qu= viatcp ? ads->tcpw.head : ads->udpw.head; qu; qu= nqu) {
90  nqu= qu->next;
91  if (qu->id != id) continue;
92  if (dglen < qu->query_dglen) continue;
94  dgram+DNS_HDRSIZE,
95  (size_t) qu->query_dglen-DNS_HDRSIZE))
96  continue;
97  if (viatcp) {
98  assert(qu->state == query_tcpw);
99  } else {
100  assert(qu->state == query_tosend);
101  if (!(qu->udpsent & (1<<serv))) continue;
102  }
103  break;
104  }
105  if (qu) {
106  /* We're definitely going to do something with this query now */
107  if (viatcp) LIST_UNLINK(ads->tcpw,qu);
108  else LIST_UNLINK(ads->udpw,qu);
109  }
110  }
111 
112  /* If we're going to ignore the packet, we return as soon as we have
113  * failed the query (if any) and printed the warning message (if
114  * any).
115  */
116  switch (rcode) {
117  case rcode_noerror:
118  case rcode_nxdomain:
119  break;
120  case rcode_formaterror:
121  adns__warn(ads,serv,qu,"server cannot understand our query (Format Error)");
123  return;
124  case rcode_servfail:
126  else adns__debug(ads,serv,qu,"server failure on unidentifiable query");
127  return;
128  case rcode_notimp:
129  adns__warn(ads,serv,qu,"server claims not to implement our query");
131  return;
132  case rcode_refused:
133  adns__debug(ads,serv,qu,"server refused our query");
135  return;
136  default:
137  adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
139  return;
140  }
141 
142  if (!qu) {
143  if (!qdcount) {
144  adns__diag(ads,serv,0,"server sent reply without quoting our question");
145  } else if (qdcount>1) {
146  adns__diag(ads,serv,0,"server claimed to answer %d questions with one message",
147  qdcount);
148  } else if (ads->iflags & adns_if_debug) {
149  adns__vbuf_init(&tempvb);
150  adns__debug(ads,serv,0,"reply not found, id %02x, query owner %s",
151  id, adns__diag_domain(ads,serv,0,&tempvb,dgram,dglen,DNS_HDRSIZE));
152  adns__vbuf_free(&tempvb);
153  }
154  return;
155  }
156 
157  /* We're definitely going to do something with this packet and this query now. */
158 
159  anstart= qu->query_dglen;
160  arstart= -1;
161 
162  /* Now, take a look at the answer section, and see if it is complete.
163  * If it has any CNAMEs we stuff them in the answer.
164  */
165  wantedrrs= 0;
166  cbyte= anstart;
167  for (rri= 0; rri<ancount; rri++) {
168  rrstart= cbyte;
169  st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
170  &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
171  &ownermatched);
172  if (st) { adns__query_fail(qu,st); return; }
173  if (rrtype == -1) goto x_truncated;
174 
175  if (rrclass != DNS_CLASS_IN) {
176  adns__diag(ads,serv,qu,"ignoring answer RR with wrong class %d (expected IN=%d)",
177  rrclass,DNS_CLASS_IN);
178  continue;
179  }
180  if (!ownermatched) {
181  if (ads->iflags & adns_if_debug) {
182  adns__debug(ads,serv,qu,"ignoring RR with an unexpected owner %s",
183  adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rrstart));
184  }
185  continue;
186  }
187  if (rrtype == adns_r_cname &&
189  if (qu->flags & adns_qf_cname_forbid) {
191  return;
192  } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
193  adns__debug(ads,serv,qu,"allegedly canonical name %s is actually alias for %s",
194  qu->answer->cname,
195  adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rdstart));
197  return;
198  } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
199  adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
200  adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rdstart));
201  } else {
202  qu->cname_begin= rdstart;
203  qu->cname_dglen= dglen;
204  st= adns__parse_domain(ads,serv,qu, &qu->vb,
206  dgram,dglen, &rdstart,rdstart+rdlength);
207  if (!qu->vb.used) goto x_truncated;
208  if (st) { adns__query_fail(qu,st); return; }
209  l= strlen((char*)qu->vb.buf)+1;
210  qu->answer->cname= adns__alloc_preserved(qu,(size_t) l);
211  if (!qu->answer->cname) { adns__query_fail(qu,adns_s_nomemory); return; }
212 
213  qu->cname_dgram= adns__alloc_mine(qu, (size_t) dglen);
214  memcpy(qu->cname_dgram,dgram,(size_t) dglen);
215 
216  memcpy(qu->answer->cname,qu->vb.buf, (size_t) l);
217  cname_here= 1;
218  adns__update_expires(qu,ttl,now);
219  /* If we find the answer section truncated after this point we restart
220  * the query at the CNAME; if beforehand then we obviously have to use
221  * TCP. If there is no truncation we can use the whole answer if
222  * it contains the relevant info.
223  */
224  }
225  } else if (rrtype == ((INT)qu->typei->type & (INT)adns__rrt_typemask)) {
226  wantedrrs++;
227  } else {
228  adns__debug(ads,serv,qu,"ignoring answer RR with irrelevant type %d",rrtype);
229  }
230  }
231 
232  /* We defer handling truncated responses here, in case there was a CNAME
233  * which we could use.
234  */
235  if (flg_tc) goto x_truncated;
236 
237  nsstart= cbyte;
238 
239  if (!wantedrrs) {
240  /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
241 
242  /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
243  foundsoa= 0; soattl= 0; foundns= 0;
244  for (rri= 0; rri<nscount; rri++) {
245  rrstart= cbyte;
246  st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
247  &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
248  if (st) { adns__query_fail(qu,st); return; }
249  if (rrtype==-1) goto x_truncated;
250  if (rrclass != DNS_CLASS_IN) {
251  adns__diag(ads,serv,qu,
252  "ignoring authority RR with wrong class %d (expected IN=%d)",
253  rrclass,DNS_CLASS_IN);
254  continue;
255  }
256  if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
257  else if (rrtype == adns_r_ns_raw) { foundns= 1; }
258  }
259 
260  if (rcode == rcode_nxdomain) {
261  /* We still wanted to look for the SOA so we could find the TTL. */
262  adns__update_expires(qu,soattl,now);
263 
264  if (qu->flags & adns_qf_search) {
266  } else {
268  }
269  return;
270  }
271 
272  if (foundsoa || !foundns) {
273  /* Aha ! A NODATA response, good. */
274  adns__update_expires(qu,soattl,now);
276  return;
277  }
278 
279  /* Now what ? No relevant answers, no SOA, and at least some NS's.
280  * Looks like a referral. Just one last chance ... if we came across
281  * a CNAME in this datagram then we should probably do our own CNAME
282  * lookup now in the hope that we won't get a referral again.
283  */
284  if (cname_here) goto x_restartquery;
285 
286  /* Bloody hell, I thought we asked for recursion ? */
287  if (!flg_ra) {
288  adns__diag(ads,serv,qu,"server is not willing to do recursive lookups for us");
290  } else {
291  if (!flg_rd)
292  adns__diag(ads,serv,qu,"server thinks we didn't ask for recursive lookup");
293  else
294  adns__debug(ads,serv,qu,"server claims to do recursion, but gave us a referral");
296  }
297  return;
298  }
299 
300  /* Now, we have some RRs which we wanted. */
301 
302  qu->answer->rrs.untyped= adns__alloc_interim(qu,(size_t) qu->typei->rrsz*wantedrrs);
303  if (!qu->answer->rrs.untyped) { adns__query_fail(qu,adns_s_nomemory); return; }
304 
305  typei= qu->typei;
306  cbyte= anstart;
307  rrsdata= qu->answer->rrs.bytes;
308 
309  pai.ads= qu->ads;
310  pai.qu= qu;
311  pai.serv= serv;
312  pai.dgram= dgram;
313  pai.dglen= dglen;
314  pai.nsstart= nsstart;
315  pai.nscount= nscount;
316  pai.arcount= arcount;
317  pai.now= now;
318 
319  for (rri=0, nrrs=0; rri<ancount; rri++) {
320  st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
321  &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
322  &ownermatched);
323  assert(!st); assert(rrtype != -1);
324  if (rrclass != DNS_CLASS_IN ||
325  rrtype != ((INT)qu->typei->type & (INT)adns__rrt_typemask) ||
326  !ownermatched)
327  continue;
328  adns__update_expires(qu,ttl,now);
329  st= typei->parse(&pai, rdstart,rdstart+rdlength, rrsdata+nrrs*typei->rrsz);
330  if (st) { adns__query_fail(qu,st); return; }
331  if (rdstart==-1) goto x_truncated;
332  nrrs++;
333  }
334  assert(nrrs==wantedrrs);
335  qu->answer->nrrs= nrrs;
336 
337  /* This may have generated some child queries ... */
338  if (qu->children.head) {
339  qu->state= query_childw;
340  LIST_LINK_TAIL(ads->childw,qu);
341  return;
342  }
343  adns__query_done(qu);
344  return;
345 
346  x_truncated:
347 
348  if (!flg_tc) {
349  adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
351  return;
352  }
353  qu->flags |= adns_qf_usevc;
354 
355  x_restartquery:
356  if (qu->cname_dgram) {
357  st= adns__mkquery_frdgram(qu->ads,&qu->vb,&qu->id,
358  qu->cname_dgram, qu->cname_dglen, qu->cname_begin,
359  qu->typei->type, qu->flags);
360  if (st) { adns__query_fail(qu,st); return; }
361 
362  newquery= realloc(qu->query_dgram, (size_t) qu->vb.used);
363  if (!newquery) { adns__query_fail(qu,adns_s_nomemory); return; }
364 
365  qu->query_dgram= newquery;
366  qu->query_dglen= qu->vb.used;
367  memcpy(newquery,qu->vb.buf, (size_t) qu->vb.used);
368  }
369 
370  if (qu->state == query_tcpw) qu->state= query_tosend;
371  qu->retries= 0;
373  adns__query_send(qu,now);
374 }
#define realloc
Definition: debug_ros.c:6
void adns__search_next(adns_state ads, adns_query qu, struct timeval now)
Definition: query.c:151
void adns__vbuf_free(vbuf *vb)
Definition: general.c:155
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
adns_query qu
Definition: internal.h:117
unsigned char * bytes
Definition: adns.h:319
dns_rcode
Definition: internal.h:90
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
adns_state ads
Definition: adh-query.c:35
int arcount
Definition: internal.h:120
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
adns_initflags iflags
Definition: internal.h:292
struct adns__query::@3825 children
int rcode
Definition: adh-main.c:36
void adns__vbuf_init(vbuf *vb)
Definition: general.c:112
int cname_begin
Definition: internal.h:213
int nscount
Definition: internal.h:120
void adns__procdgram(adns_state ads, const byte *dgram, int dglen, int serv, int viatcp, struct timeval now)
Definition: reply.c:36
enum adns__query::@3824 state
void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now)
Definition: query.c:458
#define assert(x)
Definition: debug.h:53
#define GET_B(cb, tv)
Definition: internal.h:716
int retries
Definition: internal.h:227
const typeinfo * typei
Definition: internal.h:191
int32_t INT
Definition: typedefs.h:56
void * adns__alloc_interim(adns_query qu, size_t sz)
Definition: query.c:342
void adns__query_done(adns_query qu)
Definition: query.c:503
int cname_dglen
Definition: internal.h:213
#define DNS_HDRSIZE
Definition: internal.h:76
#define LIST_LINK_TAIL(list, node)
Definition: dlist.h:51
const char * adns__diag_domain(adns_state ads, int serv, adns_query qu, vbuf *vb, const byte *dgram, int dglen, int cbyte)
Definition: general.c:162
void adns__reset_preserved(adns_query qu)
Definition: query.c:410
void adns__diag(adns_state ads, int serv, adns_query qu, const char *fmt,...)
Definition: general.c:102
adns_status
Definition: adns.h:215
time_t now
Definition: finger.c:65
unsigned long udpsent
Definition: internal.h:229
void * adns__alloc_preserved(adns_query qu, size_t sz)
Definition: query.c:352
void adns__debug(adns_state ads, int serv, adns_query qu, const char *fmt,...)
Definition: general.c:86
adns_status(* parse)(const parseinfo *pai, int cbyte, int max, void *store_r)
Definition: internal.h:141
#define DNS_CLASS_IN
Definition: internal.h:78
byte * cname_dgram
Definition: internal.h:212
void adns__warn(adns_state ads, int serv, adns_query qu, const char *fmt,...)
Definition: general.c:94
#define LIST_UNLINK(list, node)
Definition: dlist.h:50
adns_state ads
Definition: internal.h:182
static int cbyte
Definition: adnsresfilter.c:73
r l[0]
Definition: byte_order.h:167
union adns_answer::@3822 rrs
adns_query next
Definition: internal.h:184
int serv
Definition: internal.h:118
adns_answer * answer
Definition: internal.h:201
void * untyped
Definition: adns.h:318
byte * query_dgram
Definition: internal.h:192
int query_dglen
Definition: internal.h:193
char * cname
Definition: adns.h:312
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
Definition: internal.h:110
#define f1(x, y, z)
Definition: sha1.c:30
byte * buf
Definition: internal.h:112
#define GET_W(cb, tv)
Definition: internal.h:717
int nrrs
Definition: adns.h:316
adns_rrtype type
Definition: internal.h:125
int used
Definition: internal.h:111
adns_status adns__mkquery_frdgram(adns_state ads, vbuf *vb, int *id_r, const byte *qd_dgram, int qd_dglen, int qd_begin, adns_rrtype type, adns_queryflags flags)
Definition: transmit.c:138
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
int nsstart
Definition: internal.h:120
const byte * dgram
Definition: internal.h:119
void adns__query_send(adns_query qu, struct timeval now)
Definition: transmit.c:232
int rrsz
Definition: internal.h:128
GLenum GLuint id
Definition: glext.h:5579
void adns__query_fail(adns_query qu, adns_status stat)
Definition: query.c:547
void * adns__alloc_mine(adns_query qu, size_t sz)
Definition: query.c:362
int f2(S1 &, S2 &)
adns_state ads
Definition: internal.h:116
struct timeval now
Definition: internal.h:121
int dglen
Definition: internal.h:120