2 --- trafshow-5.2.3.orig/domain_resolver.c 2006-01-08 06:59:23.000000000 +0100
3 +++ trafshow-5.2.3/domain_resolver.c 2007-02-04 19:40:27.000000000 +0100
9 + * Copyright (c) 1985, 1993
10 + * The Regents of the University of California. All rights reserved.
12 + * Redistribution and use in source and binary forms, with or without
13 + * modification, are permitted provided that the following conditions
15 + * 1. Redistributions of source code must retain the above copyright
16 + * notice, this list of conditions and the following disclaimer.
17 + * 2. Redistributions in binary form must reproduce the above copyright
18 + * notice, this list of conditions and the following disclaimer in the
19 + * documentation and/or other materials provided with the distribution.
20 + * 4. Neither the name of the University nor the names of its contributors
21 + * may be used to endorse or promote products derived from this software
22 + * without specific prior written permission.
24 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 + * Portions Copyright (c) 1993 by Digital Equipment Corporation.
40 + * Permission to use, copy, modify, and distribute this software for any
41 + * purpose with or without fee is hereby granted, provided that the above
42 + * copyright notice and this permission notice appear in all copies, and that
43 + * the name of Digital Equipment Corporation not be used in advertising or
44 + * publicity pertaining to distribution of the document or software without
45 + * specific, written prior permission.
47 + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
48 + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
49 + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
50 + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
51 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
52 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
53 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
58 + * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
60 + * Permission to use, copy, modify, and distribute this software for any
61 + * purpose with or without fee is hereby granted, provided that the above
62 + * copyright notice and this permission notice appear in all copies.
64 + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
65 + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
66 + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
67 + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
68 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
69 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
70 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
75 + * Copyright (c) 1996,1999 by Internet Software Consortium.
77 + * Permission to use, copy, modify, and distribute this software for any
78 + * purpose with or without fee is hereby granted, provided that the above
79 + * copyright notice and this permission notice appear in all copies.
81 + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
82 + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
83 + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
84 + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
85 + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
86 + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
87 + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
93 + * DNS helper functions not implemented in uclibc
100 +static const char digits[] = "0123456789";
104 +static int special(int);
105 +static int printable(int);
106 +static int dn_find(const u_char *, const u_char *,
107 + const u_char * const *,
108 + const u_char * const *);
112 + * ns_name_ntop(src, dst, dstsiz)
113 + * Convert an encoded domain name to printable ascii as per RFC1035.
115 + * Number of bytes written to buffer, or -1 (with errno set)
117 + * The root is returned as "."
118 + * All other domains are returned in non absolute form
121 +ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
129 + eom = dst + dstsiz;
131 + while ((n = *cp++) != 0) {
132 + if ((n & NS_CMPRSFLGS) != 0) {
133 + /* Some kind of compression pointer. */
142 + if (dn + n >= eom) {
145 + for ((void)NULL; n > 0; n--) {
148 + if (dn + 1 >= eom) {
153 + } else if (!printable(c)) {
154 + if (dn + 3 >= eom) {
158 + *dn++ = digits[c / 100];
159 + *dn++ = digits[(c % 100) / 10];
160 + *dn++ = digits[c % 10];
183 + * ns_name_pton(src, dst, dstsiz)
184 + * Convert a ascii string into an encoded domain name as per RFC1035.
187 + * 1 if string was fully qualified
188 + * 0 is string was not fully qualified
190 + * Enforces label and domain length limits.
194 +ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
195 + u_char *label, *bp, *eom;
201 + eom = dst + dstsiz;
204 + while ((c = *src++) != 0) {
206 + if ((cp = strchr(digits, c)) != NULL) {
207 + n = (cp - digits) * 100;
208 + if ((c = *src++) == 0 ||
209 + (cp = strchr(digits, c)) == NULL) {
212 + n += (cp - digits) * 10;
213 + if ((c = *src++) == 0 ||
214 + (cp = strchr(digits, c)) == NULL) {
217 + n += (cp - digits);
224 + } else if (c == '\\') {
227 + } else if (c == '.') {
228 + c = (bp - label - 1);
229 + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
232 + if (label >= eom) {
236 + /* Fully qualified ? */
237 + if (*src == '\0') {
244 + if ((bp - dst) > MAXCDNAME) {
249 + if (c == 0 || *src == '.') {
260 + c = (bp - label - 1);
261 + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
264 + if (label >= eom) {
274 + if ((bp - dst) > MAXCDNAME) { /* src too big */
281 + * ns_name_ntol(src, dst, dstsiz)
282 + * Convert a network strings labels into all lowercase.
284 + * Number of bytes written to buffer, or -1 (with errno set)
286 + * Enforces label and domain length limits.
290 +ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
298 + eom = dst + dstsiz;
300 + while ((n = *cp++) != 0) {
301 + if ((n & NS_CMPRSFLGS) != 0) {
302 + /* Some kind of compression pointer. */
306 + if (dn + n >= eom) {
309 + for ((void)NULL; n > 0; n--) {
312 + *dn++ = tolower(c);
322 + * ns_name_unpack(msg, eom, src, dst, dstsiz)
323 + * Unpack a domain name from a message, source may be compressed.
325 + * -1 if it fails, or consumed octets if it succeeds.
328 +ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
329 + u_char *dst, size_t dstsiz)
331 + const u_char *srcp, *dstlim;
333 + int n, len, checked;
339 + dstlim = dst + dstsiz;
340 + if (srcp < msg || srcp >= eom) {
343 + /* Fetch next label in domain name. */
344 + while ((n = *srcp++) != 0) {
345 + /* Check for indirection. */
346 + switch (n & NS_CMPRSFLGS) {
348 + /* Limit checks. */
349 + if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
354 + memcpy(dstp, srcp, n);
364 + len = srcp - src + 1;
365 + srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
366 + if (srcp < msg || srcp >= eom) { /* Out of range. */
371 + * Check for loops in the compressed name;
372 + * if we've looked at the whole message,
373 + * there must be a loop.
375 + if (checked >= eom - msg) {
381 + return (-1); /* flag error */
391 + * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
392 + * Pack domain name 'domain' into 'comp_dn'.
394 + * Size of the compressed name, or -1.
396 + * 'dnptrs' is an array of pointers to previous compressed names.
397 + * dnptrs[0] is a pointer to the beginning of the message. The array
399 + * 'lastdnptr' is a pointer to the end of the array pointed to
402 + * The list of pointers in dnptrs is updated for labels inserted into
403 + * the message as we compress the name. If 'dnptr' is NULL, we don't
404 + * try to compress names. If 'lastdnptr' is NULL, we don't update the
408 +ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
409 + const u_char **dnptrs, const u_char **lastdnptr)
412 + const u_char **cpp, **lpp, *eob, *msg;
413 + const u_char *srcp;
414 + int n, l, first = 1;
418 + eob = dstp + dstsiz;
420 + if (dnptrs != NULL) {
421 + if ((msg = *dnptrs++) != NULL) {
422 + for (cpp = dnptrs; *cpp != NULL; cpp++)
424 + lpp = cpp; /* end of list to search */
429 + /* make sure the domain we are about to add is legal */
433 + if ((n & NS_CMPRSFLGS) != 0) {
437 + if (l > MAXCDNAME) {
443 + /* from here on we need to reset compression pointer array on error */
446 + /* Look to see if we can use pointers. */
448 + if (n != 0 && msg != NULL) {
449 + l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
450 + (const u_char * const *)lpp);
452 + if (dstp + 1 >= eob) {
455 + *dstp++ = (l >> 8) | NS_CMPRSFLGS;
457 + return (dstp - dst);
459 + /* Not found, save it. */
460 + if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
461 + (dstp - msg) < 0x4000 && first) {
467 + /* copy label to buffer */
468 + if (n & NS_CMPRSFLGS) { /* Should not happen. */
471 + if (dstp + 1 + n >= eob) {
474 + memcpy(dstp, srcp, n + 1);
485 + return (dstp - dst);
489 + * ns_name_uncompress(msg, eom, src, dst, dstsiz)
490 + * Expand compressed domain name to presentation format.
492 + * Number of bytes read out of `src', or -1 (with errno set).
494 + * Root domain returns as "." not "".
497 +ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
498 + char *dst, size_t dstsiz)
500 + u_char tmp[NS_MAXCDNAME];
503 + if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
505 + if (ns_name_ntop(tmp, dst, dstsiz) == -1)
511 + * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
512 + * Compress a domain name into wire format, using compression pointers.
514 + * Number of bytes consumed in `dst' or -1 (with errno set).
516 + * 'dnptrs' is an array of pointers to previous compressed names.
517 + * dnptrs[0] is a pointer to the beginning of the message.
518 + * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
519 + * array pointed to by 'dnptrs'. Side effect is to update the list of
520 + * pointers for labels inserted into the message as we compress the name.
521 + * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
522 + * is NULL, we don't update the list.
525 +ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
526 + const u_char **dnptrs, const u_char **lastdnptr)
528 + u_char tmp[NS_MAXCDNAME];
530 + if (ns_name_pton(src, tmp, sizeof tmp) == -1)
532 + return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
537 + * Thinking in noninternationalized USASCII (per the DNS spec),
538 + * is this characted special ("in need of quoting") ?
545 + case 0x22: /* '"' */
546 + case 0x2E: /* '.' */
547 + case 0x3B: /* ';' */
548 + case 0x5C: /* '\\' */
549 + /* Special modifiers in zone files. */
550 + case 0x40: /* '@' */
551 + case 0x24: /* '$' */
560 + * Thinking in noninternationalized USASCII (per the DNS spec),
561 + * is this character visible and not a space when printed ?
567 + return (ch > 0x20 && ch < 0x7f);
571 + * Thinking in noninternationalized USASCII (per the DNS spec),
572 + * convert this character to lower case if it's upper case.
576 + if (ch >= 0x41 && ch <= 0x5A)
577 + return (ch + 0x20);
582 + * dn_find(domain, msg, dnptrs, lastdnptr)
583 + * Search for the counted-label name in an array of compressed names.
585 + * offset from msg if found, or -1.
587 + * dnptrs is the pointer to the first name on the list,
588 + * not the pointer to the start of the message.
591 +dn_find(const u_char *domain, const u_char *msg,
592 + const u_char * const *dnptrs,
593 + const u_char * const *lastdnptr)
595 + const u_char *dn, *cp, *sp;
596 + const u_char * const *cpp;
599 + for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
602 + * terminate search on:
604 + * compression pointer
607 + while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
608 + (sp - msg) < 0x4000) {
611 + while ((n = *cp++) != 0) {
613 + * check for indirection
615 + switch (n & NS_CMPRSFLGS) {
616 + case 0: /* normal case, n == len */
619 + for ((void)NULL; n > 0; n--)
620 + if (mklower(*dn++) !=
623 + /* Is next root for both ? */
624 + if (*dn == '\0' && *cp == '\0')
630 + case NS_CMPRSFLGS: /* indirection */
631 + cp = msg + (((n & 0x3f) << 8) | *cp);
634 + default: /* illegal type */
646 + * Expand compressed domain name 'comp_dn' to full domain name.
647 + * 'msg' is a pointer to the begining of the message,
648 + * 'eomorig' points to the first location after the message,
649 + * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
650 + * Return size of compressed name or -1 if there was an error.
653 +dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
654 + char *dst, int dstsiz)
656 + int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
658 + if (n > 0 && dst[0] == '.')
664 + * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
665 + * Return the size of the compressed name or -1.
666 + * 'length' is the size of the array pointed to by 'comp_dn'.
669 +dn_comp(const char *src, u_char *dst, int dstsiz,
670 + u_char **dnptrs, u_char **lastdnptr)
672 + return (ns_name_compress(src, dst, (size_t)dstsiz,
673 + (const u_char **)dnptrs,
674 + (const u_char **)lastdnptr));
678 #include "domain_resolver.h"