about summary refs log tree commit diff
path: root/resolv/ns_name_unpack.c
blob: 1c1dd3ee8a90c7727b136031351b12e8680823fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* De-compressing DNS domain names into binary-encoded uncompressed name.
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1996,1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <arpa/nameser.h>
#include <errno.h>
#include <shlib-compat.h>
#include <stddef.h>
#include <string.h>

/* Unpack a domain name from a message, source may be compressed.
   Returns -1 if it fails, or consumed octets if it succeeds.  */
int
___ns_name_unpack (const unsigned char *msg, const unsigned char *eom,
                   const unsigned char *src, unsigned char *dst, size_t dstsiz)
{
  const unsigned char *srcp, *dstlim;
  unsigned char *dstp;
  int n, len, checked;

  len = -1;
  checked = 0;
  dstp = dst;
  srcp = src;
  dstlim = dst + dstsiz;
  if (srcp < msg || srcp >= eom)
    {
      __set_errno (EMSGSIZE);
      return -1;
    }
  /* Fetch next label in domain name.  */
  while ((n = *srcp++) != 0)
    {
      /* Check for indirection.  */
      switch (n & NS_CMPRSFLGS)
        {
        case 0:
          /* Limit checks.  */
          if (n >= 64)
            {
              __set_errno (EMSGSIZE);
              return -1;
            }
          /* NB: n + 1 and >= to cover the *dstp = '\0' assignment
             below.  */
          if (n + 1 >= dstlim - dstp || n >= eom - srcp)
            {
              __set_errno (EMSGSIZE);
              return -1;
            }
          checked += n + 1;
          *dstp++ = n;
          memcpy (dstp, srcp, n);
          dstp += n;
          srcp += n;
          break;

        case NS_CMPRSFLGS:
          if (srcp >= eom)
            {
              __set_errno (EMSGSIZE);
              return -1;
            }
          if (len < 0)
            len = srcp - src + 1;
          {
            int target = ((n & 0x3f) << 8) | *srcp;
            if (target >= eom - msg)
              {
              /* Out of range.  */
                __set_errno (EMSGSIZE);
                return -1;
            }
            srcp = msg + target;
          }
          checked += 2;
          /* Check for loops in the compressed name; if we've looked
             at the whole message, there must be a loop.  */
          if (checked >= eom - msg)
            {
              __set_errno (EMSGSIZE);
              return -1;
            }
          break;

        default:
          __set_errno (EMSGSIZE);
          return -1;
        }
    }
  *dstp = '\0';
  if (len < 0)
    len = srcp - src;
  return len;
}
versioned_symbol (libc, ___ns_name_unpack, ns_name_unpack, GLIBC_2_34);
versioned_symbol (libc, ___ns_name_unpack, __ns_name_unpack, GLIBC_PRIVATE);
libc_hidden_ver (___ns_name_unpack, __ns_name_unpack)

#if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_9, GLIBC_2_34)
compat_symbol (libresolv, ___ns_name_unpack, ns_name_unpack, GLIBC_2_9);
#endif