2 * Copyright (c) 1996,1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 #include <sys/types.h>
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
25 #ifdef HAVE_ARPA_NAMESER_H
26 # include <arpa/nameser.h>
38 static const char digits
[] = "0123456789";
42 static int special(int);
43 static int printable(int);
48 * dns_ns_name_ntop(src, dst, dstsiz)
49 * Convert an encoded domain name to printable ascii as per RFC1035.
51 * Number of bytes written to buffer, or -1 (with errno set)
53 * The root is returned as "."
54 * All other domains are returned in non absolute form
57 dns_ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
) {
67 while ((n
= *cp
++) != 0) {
68 if ((n
& NS_CMPRSFLGS
) != 0 && n
!= 0x41) {
69 /* Some kind of compression pointer. */
81 if (dn
+ n
* 2 + 4 >= eom
) {
92 *dn
++ = u
> 9 ? 'a' + u
- 10 : '0' + u
;
94 *dn
++ = u
> 9 ? 'a' + u
- 10 : '0' + u
;
104 for ((void)NULL
; n
> 0; n
--) {
112 } else if (!printable(c
)) {
117 *dn
++ = digits
[c
/ 100];
118 *dn
++ = digits
[(c
% 100) / 10];
119 *dn
++ = digits
[c
% 10];
142 * dns_ns_name_pton(src, dst, dstsiz)
143 * Convert a ascii string into an encoded domain name as per RFC1035.
146 * 1 if string was fully qualified
147 * 0 is string was not fully qualified
149 * Enforces label and domain length limits.
153 dns_ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
154 u_char
*label
, *bp
, *eom
;
163 while ((c
= *src
++) != 0) {
165 if ((cp
= strchr(digits
, c
)) != NULL
) {
166 n
= (cp
- digits
) * 100;
167 if ((c
= *src
++) == 0 ||
168 (cp
= strchr(digits
, c
)) == NULL
) {
171 n
+= (cp
- digits
) * 10;
172 if ((c
= *src
++) == 0 ||
173 (cp
= strchr(digits
, c
)) == NULL
) {
181 } else if (c
== '[' && label
== bp
- 1 && *src
== 'x') {
182 /* Theoretically we would have to handle \[o
183 as well but we do not since we do not need
188 while (isxdigit (*src
)) {
189 n
= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
191 if (! isxdigit(*src
)) {
195 n
+= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
202 *label
= (bp
- label
- 1) * 8;
203 if (*src
++ != ']' || *src
++ != '.') {
214 } else if (c
== '\\') {
217 } else if (c
== '.') {
218 c
= (bp
- label
- 1);
219 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
226 /* Fully qualified ? */
234 if ((bp
- dst
) > NS_MAXCDNAME
) {
239 if (c
== 0 || *src
== '.') {
250 c
= (bp
- label
- 1);
251 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
264 if ((bp
- dst
) > NS_MAXCDNAME
) { /* src too big */
272 * dns_ns_name_unpack(msg, eom, src, dst, dstsiz)
273 * Unpack a domain name from a message, source may be compressed.
275 * -1 if it fails, or consumed octets if it succeeds.
278 dns_ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
279 u_char
*dst
, size_t dstsiz
)
281 const u_char
*srcp
, *dstlim
;
289 dstlim
= dst
+ dstsiz
;
290 if (srcp
< msg
|| srcp
>= eom
) {
293 /* Fetch next label in domain name. */
294 while ((n
= *srcp
++) != 0) {
295 /* Check for indirection. */
296 switch (n
& NS_CMPRSFLGS
) {
299 if (dstp
+ 1 >= dstlim
) {
306 return (-1); /* flag error */
311 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
315 dstp
= memcpy(dstp
, srcp
- 1, n
+ 1);
325 len
= srcp
- src
+ 1;
326 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
327 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
332 * Check for loops in the compressed name;
333 * if we've looked at the whole message,
334 * there must be a loop.
336 if (checked
>= eom
- msg
) {
342 return (-1); /* flag error */
353 * dns_ns_name_uncompress(msg, eom, src, dst, dstsiz)
354 * Expand compressed domain name to presentation format.
356 * Number of bytes read out of `src', or -1 (with errno set).
358 * Root domain returns as "." not "".
361 dns_ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
362 char *dst
, size_t dstsiz
)
364 u_char tmp
[NS_MAXCDNAME
];
367 if ((n
= dns_ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
369 if (dns_ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
376 * dns_ns_name_skip(ptrptr, eom)
377 * Advance *ptrptr to skip over the compressed name it points at.
379 * 0 on success, -1 (with errno set) on failure.
382 dns_ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
387 while (cp
< eom
&& (n
= *cp
++) != 0) {
388 /* Check for indirection. */
389 switch (n
& NS_CMPRSFLGS
) {
390 case 0: /* normal case, n == len */
393 case NS_CMPRSFLGS
: /* indirection */
396 default: /* illegal type */
412 * Thinking in noninternationalized USASCII (per the DNS spec),
413 * is this character special ("in need of quoting") ?
423 case 0x5C: /* '\\' */
424 /* Special modifiers in zone files. */
435 * Thinking in noninternationalized USASCII (per the DNS spec),
436 * is this character visible and not a space when printed ?
442 return (ch
> 0x20 && ch
< 0x7f);