ekg2  GIT master
sniff_dns.h
Idź do dokumentacji tego pliku.
1 void *mempcpy(void *dest, const void *src, size_t n); /* forward */
2 
3 
4 /* Copyrights notes: */
5 
6 /* symbols: digits[], special(), printable(), ns_name_unpack(), ns_name_ntop(), ns_name_uncompress()
7  * copied from glibc resolv/res_data.c
8  * under licence: */
9 
10 /*
11  * Copyright (c) 1996,1999 by Internet Software Consortium.
12  *
13  * Permission to use, copy, modify, and distribute this software for any
14  * purpose with or without fee is hereby granted, provided that the above
15  * copyright notice and this permission notice appear in all copies.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
18  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
20  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
21  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
22  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
23  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24  * SOFTWARE.
25  */
26 
27 /* symbol: dn_expand()
28  * copied from glibc resolv/res_comp.c
29  * under licences: */
30 
31 /*
32  * Copyright (c) 1985, 1993
33  * The Regents of the University of California. All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  * notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  * notice, this list of conditions and the following disclaimer in the
42  * documentation and/or other materials provided with the distribution.
43  * 4. Neither the name of the University nor the names of its contributors
44  * may be used to endorse or promote products derived from this software
45  * without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  */
59 
60 /*
61  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
62  *
63  * Permission to use, copy, modify, and distribute this software for any
64  * purpose with or without fee is hereby granted, provided that the above
65  * copyright notice and this permission notice appear in all copies, and that
66  * the name of Digital Equipment Corporation not be used in advertising or
67  * publicity pertaining to distribution of the document or software without
68  * specific, written prior permission.
69  *
70  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
71  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
72  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
73  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
74  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
75  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
76  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
77  * SOFTWARE.
78  */
79 
80 /*
81  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
82  *
83  * Permission to use, copy, modify, and distribute this software for any
84  * purpose with or without fee is hereby granted, provided that the above
85  * copyright notice and this permission notice appear in all copies.
86  *
87  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
88  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
89  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
90  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
91  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
92  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
93  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
94  * SOFTWARE.
95  */
96 
97 /* all struct and magic values
98  * copied from: glibc resolv/arpa/nameser.h && resolv/arpa/nameser_compat.h
99  * under licences: */
100 
101 /*
102  * Copyright (c) 1983, 1989, 1993
103  * The Regents of the University of California. All rights reserved.
104  *
105  * Redistribution and use in source and binary forms, with or without
106  * modification, are permitted provided that the following conditions
107  * are met:
108  * 1. Redistributions of source code must retain the above copyright
109  * notice, this list of conditions and the following disclaimer.
110  * 2. Redistributions in binary form must reproduce the above copyright
111  * notice, this list of conditions and the following disclaimer in the
112  * documentation and/or other materials provided with the distribution.
113  * 4. Neither the name of the University nor the names of its contributors
114  * may be used to endorse or promote products derived from this software
115  * without specific prior written permission.
116  *
117  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
118  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
119  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
120  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
121  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
122  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
123  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
124  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
125  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
126  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
127  * SUCH DAMAGE.
128  */
129 
130 /*
131  * Copyright (c) 1996-1999 by Internet Software Consortium.
132  *
133  * Permission to use, copy, modify, and distribute this software for any
134  * purpose with or without fee is hereby granted, provided that the above
135  * copyright notice and this permission notice appear in all copies.
136  *
137  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
138  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
139  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
140  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
141  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
142  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
143  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
144  * SOFTWARE.
145  */
146 
147 typedef struct {
148  unsigned id :16; /* query identification number */
149  /* fields in third byte */
150  unsigned rd :1; /* recursion desired */
151  unsigned tc :1; /* truncated message */
152  unsigned aa :1; /* authoritive answer */
153  unsigned opcode :4; /* purpose of message */
154  unsigned qr :1; /* response flag */
155  /* fields in fourth byte */
156  unsigned rcode :4; /* response code */
157  unsigned cd: 1; /* checking disabled by resolver */
158  unsigned ad: 1; /* authentic data from named */
159  unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */
160  unsigned ra :1; /* recursion available */
161  /* remaining bytes */
162  unsigned qdcount :16; /* number of question entries */
163  unsigned ancount :16; /* number of answer entries */
164  unsigned nscount :16; /* number of authority entries */
165  unsigned arcount :16; /* number of resource entries */
166 } DNS_HEADER;
167 
168 
169 #define DNS_HFIXEDSZ 12 /* #/bytes of fixed data in header */
170 #define DNS_QFIXEDSZ 4 /* #/bytes of fixed data in query */
171 
172 #define NOERROR 0 /* No error occurred */
173 #define FORMERR 1 /* format error */
174 #define SERVFAIL 2 /* Server failure */
175 #define NXDOMAIN 3 /* Name error */
176 #define NOTIMP 4 /* Unimplemented */
177 #define REFUSED 5 /* Operation refused */
178 
179 #define T_A 1 /* (IN) A */
180 #define T_NS 2 /* (IN) NS */
181 #define T_CNAME 5 /* (IN) CNAME */
182 #define T_PTR 12 /* (IN) PTR */
183 #define T_MX 15 /* (IN) MX */
184 #define T_AAAA 28 /* (IN) AAAA */
185 #define T_SRV 33 /* (IN) SRV */
186 
187 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
188 
189 #define NS_CMPRSFLGS 0xc0 /* Flag bits indicating name compression. */
190 
191 static const char digits[] = "0123456789";
192 
193 static int special(int ch) {
194  switch (ch) {
195  case 0x22: /* '"' */
196  case 0x2E: /* '.' */
197  case 0x3B: /* ';' */
198  case 0x5C: /* '\\' */
199  /* Special modifiers in zone files. */
200  case 0x40: /* '@' */
201  case 0x24: /* '$' */
202  return (1);
203  default:
204  return (0);
205  }
206 }
207 
208 static int printable(int ch) {
209  return (ch > 0x20 && ch < 0x7f);
210 }
211 
212 int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, u_char *dst, size_t dstsiz)
213 {
214  const u_char *srcp, *dstlim;
215  u_char *dstp;
216  int n, len, checked;
217 
218  len = -1;
219  checked = 0;
220  dstp = dst;
221  srcp = src;
222  dstlim = dst + dstsiz;
223  if (srcp < msg || srcp >= eom) {
224  errno = EMSGSIZE;
225  return (-1);
226  }
227  /* Fetch next label in domain name. */
228  while ((n = *srcp++) != 0) {
229  /* Check for indirection. */
230  switch (n & NS_CMPRSFLGS) {
231  case 0x40:
232  if (n == 0x41) {
233  if (dstp + 1 >= dstlim) {
234  errno = EMSGSIZE;
235  return (-1);
236  }
237  *dstp++ = 0x41;
238  n = *srcp++ / 8;
239  ++checked;
240  } else {
241  errno = EMSGSIZE;
242  return (-1); /* flag error */
243  }
244  /* FALLTHROUGH */
245  case 0:
246  /* Limit checks. */
247  if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
248  errno = EMSGSIZE;
249  return (-1);
250  }
251  checked += n + 1;
252  dstp = mempcpy(dstp, srcp - 1, n + 1);
253  srcp += n;
254  break;
255 
256  case NS_CMPRSFLGS:
257  if (srcp >= eom) {
258  errno = EMSGSIZE;
259  return (-1);
260  }
261  if (len < 0)
262  len = srcp - src + 1;
263  srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
264  if (srcp < msg || srcp >= eom) { /* Out of range. */
265  errno = EMSGSIZE;
266  return (-1);
267  }
268  checked += 2;
269  /*
270  * Check for loops in the compressed name;
271  * if we've looked at the whole message,
272  * there must be a loop.
273  */
274  if (checked >= eom - msg) {
275  errno = EMSGSIZE;
276  return (-1);
277  }
278  break;
279 
280  default:
281  errno = EMSGSIZE;
282  return (-1); /* flag error */
283  }
284  }
285  *dstp = '\0';
286  if (len < 0)
287  len = srcp - src;
288  return (len);
289 }
290 
291 int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
292  const u_char *cp;
293  char *dn, *eom;
294  u_char c;
295  u_int n;
296 
297  cp = src;
298  dn = dst;
299  eom = dst + dstsiz;
300 
301  while ((n = *cp++) != 0) {
302  if ((n & NS_CMPRSFLGS) != 0 && n != 0x41) {
303  /* Some kind of compression pointer. */
304  errno = EMSGSIZE;
305  return (-1);
306  }
307  if (dn != dst) {
308  if (dn >= eom) {
309  errno = EMSGSIZE;
310  return (-1);
311  }
312  *dn++ = '.';
313  }
314 
315  if (n == 0x41) {
316  n = *cp++ / 8;
317  if (dn + n * 2 + 4 >= eom) {
318  errno = EMSGSIZE;
319  return (-1);
320  }
321  *dn++ = '\\';
322  *dn++ = '[';
323  *dn++ = 'x';
324 
325  while (n-- > 0) {
326  c = *cp++;
327  unsigned u = c >> 4;
328  *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
329  u = c & 0xf;
330  *dn++ = u > 9 ? 'a' + u - 10 : '0' + u;
331  }
332 
333  *dn++ = ']';
334  continue;
335  }
336 
337  if (dn + n >= eom) {
338  errno = EMSGSIZE;
339  return (-1);
340  }
341  for ((void)NULL; n > 0; n--) {
342  c = *cp++;
343  if (special(c)) {
344  if (dn + 1 >= eom) {
345  errno = EMSGSIZE;
346  return (-1);
347  }
348  *dn++ = '\\';
349  *dn++ = (char)c;
350  } else if (!printable(c)) {
351  if (dn + 3 >= eom) {
352  errno = EMSGSIZE;
353  return (-1);
354  }
355  *dn++ = '\\';
356  *dn++ = digits[c / 100];
357  *dn++ = digits[(c % 100) / 10];
358  *dn++ = digits[c % 10];
359  } else {
360  if (dn >= eom) {
361  errno = EMSGSIZE;
362  return (-1);
363  }
364  *dn++ = (char)c;
365  }
366  }
367  }
368  if (dn == dst) {
369  if (dn >= eom) {
370  errno = EMSGSIZE;
371  return (-1);
372  }
373  *dn++ = '.';
374  }
375  if (dn >= eom) {
376  errno = EMSGSIZE;
377  return (-1);
378  }
379  *dn++ = '\0';
380  return (dn - dst);
381 }
382 
383 int ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src, char *dst, size_t dstsiz) {
384  u_char tmp[NS_MAXCDNAME];
385  int n;
386 
387  if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
388  return (-1);
389  if (ns_name_ntop(tmp, dst, dstsiz) == -1)
390  return (-1);
391  return (n);
392 }
393 
394 int dn_expand(const char *msg, const char *eom, const char *src, char *dst, int dstsiz) {
395  int n = ns_name_uncompress((u_char *) msg, (u_char *) eom, (u_char *) src, dst, (size_t)dstsiz);
396 
397  if (n > 0 && dst[0] == '.')
398  dst[0] = '\0';
399  return (n);
400 }
#define NS_MAXCDNAME
Definition: sniff_dns.h:187
static int special(int ch)
Definition: sniff_dns.h:193
unsigned aa
Definition: sniff_dns.h:152
unsigned tc
Definition: sniff_dns.h:151
unsigned rcode
Definition: sniff_dns.h:156
unsigned ra
Definition: sniff_dns.h:160
unsigned qdcount
Definition: sniff_dns.h:162
#define NULL
Definition: oralog.c:49
int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, u_char *dst, size_t dstsiz)
Definition: sniff_dns.h:212
unsigned rd
Definition: sniff_dns.h:150
static int printable(int ch)
Definition: sniff_dns.h:208
unsigned ancount
Definition: sniff_dns.h:163
char ch
Definition: completion.c:638
#define NS_CMPRSFLGS
Definition: sniff_dns.h:189
unsigned ad
Definition: sniff_dns.h:158
unsigned cd
Definition: sniff_dns.h:157
unsigned arcount
Definition: sniff_dns.h:165
void * mempcpy(void *dest, const void *src, size_t n)
int ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src, char *dst, size_t dstsiz)
Definition: sniff_dns.h:383
unsigned nscount
Definition: sniff_dns.h:164
unsigned qr
Definition: sniff_dns.h:154
int dn_expand(const char *msg, const char *eom, const char *src, char *dst, int dstsiz)
Definition: sniff_dns.h:394
int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
Definition: sniff_dns.h:291
static const char digits[]
Definition: sniff_dns.h:191
Definition: sniff_dns.h:147
unsigned unused
Definition: sniff_dns.h:159
unsigned opcode
Definition: sniff_dns.h:153
unsigned id
Definition: sniff_dns.h:148