1 /* $NetBSD: ns_name.c,v 1.7 2014/07/12 12:09:37 spz Exp $ */
3 * Copyright (c) 2004,2009 by Internet Systems Consortium, Inc. ("ISC")
4 * Copyright (c) 1996-2003 by Internet Software Consortium
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Internet Systems Consortium, Inc.
20 * Redwood City, CA 94063
25 #include <sys/cdefs.h>
26 __RCSID("$NetBSD: ns_name.c,v 1.7 2014/07/12 12:09:37 spz Exp $");
29 static const char rcsid
[] = "Id: ns_name.c,v 1.2 2009/10/28 04:12:29 sar Exp ";
32 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <sys/socket.h>
42 #include "arpa/nameser.h"
46 static const char digits
[] = "0123456789";
50 static int special(int);
51 static int printable(int);
52 static int dn_find(const u_char
*, const u_char
*,
53 const u_char
* const *,
54 const u_char
* const *);
59 * MRns_name_ntop(src, dst, dstsiz)
60 * Convert an encoded domain name to printable ascii as per RFC1035.
62 * Number of bytes written to buffer, or -1 (with errno set)
64 * The root is returned as "."
65 * All other domains are returned in non absolute form
68 MRns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
) {
78 while ((n
= *cp
++) != 0) {
79 if ((n
& NS_CMPRSFLGS
) != 0) {
80 /* Some kind of compression pointer. */
95 for ((void)NULL
; n
> 0; n
--) {
104 } else if (!printable(c
)) {
110 *dn
++ = digits
[c
/ 100];
111 *dn
++ = digits
[(c
% 100) / 10];
112 *dn
++ = digits
[c
% 10];
138 * MRns_name_pton(src, dst, dstsiz)
139 * Convert a ascii string into an encoded domain name as per RFC1035.
142 * 1 if string was fully qualified
143 * 0 is string was not fully qualified
145 * Enforces label and domain length limits.
149 MRns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
150 u_char
*label
, *bp
, *eom
;
159 while ((c
= *src
++) != 0) {
161 if ((cp
= strchr(digits
, c
)) != NULL
) {
162 n
= (cp
- digits
) * 100;
163 if ((c
= *src
++) == 0 ||
164 (cp
= strchr(digits
, c
)) == NULL
) {
168 n
+= (cp
- digits
) * 10;
169 if ((c
= *src
++) == 0 ||
170 (cp
= strchr(digits
, c
)) == NULL
) {
182 } else if (c
== '\\') {
185 } else if (c
== '.') {
186 c
= (bp
- label
- 1);
187 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
196 /* Fully qualified ? */
205 if ((bp
- dst
) > MAXCDNAME
) {
211 if (c
== 0 || *src
== '.') {
224 c
= (bp
- label
- 1);
225 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
241 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
250 * MRns_name_ntol(src, dst, dstsiz)
251 * Convert a network strings labels into all lowercase.
253 * Number of bytes written to buffer, or -1 (with errno set)
255 * Enforces label and domain length limits.
259 MRns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
) {
273 while ((n
= *cp
++) != 0) {
274 if ((n
& NS_CMPRSFLGS
) != 0) {
275 /* Some kind of compression pointer. */
284 for ((void)NULL
; n
> 0; n
--) {
298 * MRns_name_unpack(msg, eom, src, dst, dstsiz)
299 * Unpack a domain name from a message, source may be compressed.
301 * -1 if it fails, or consumed octets if it succeeds.
304 MRns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
305 u_char
*dst
, size_t dstsiz
)
307 const u_char
*srcp
, *dstlim
;
317 dstlim
= dst
+ dstsiz
;
318 if (srcp
< msg
|| srcp
>= eom
) {
322 /* Fetch next label in domain name. */
323 while ((n
= *srcp
++) != 0) {
324 /* Check for indirection. */
325 switch (n
& NS_CMPRSFLGS
) {
328 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
334 memcpy(dstp
, srcp
, n
);
345 len
= srcp
- src
+ 1;
346 n
= ((n
& 0x3f) << 8) | (*srcp
& 0xff);
347 if (n
>= eom
- msg
) { /* Out of range. */
354 * Check for loops in the compressed name;
355 * if we've looked at the whole message,
356 * there must be a loop.
358 if (checked
>= eom
- msg
) {
366 return (-1); /* flag error */
376 * MRns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
377 * Pack domain name 'domain' into 'comp_dn'.
379 * Size of the compressed name, or -1.
381 * 'dnptrs' is an array of pointers to previous compressed names.
382 * dnptrs[0] is a pointer to the beginning of the message. The array
384 * 'lastdnptr' is a pointer to the end of the array pointed to
387 * The list of pointers in dnptrs is updated for labels inserted into
388 * the message as we compress the name. If 'dnptr' is NULL, we don't
389 * try to compress names. If 'lastdnptr' is NULL, we don't update the
393 MRns_name_pack(const u_char
*src
, u_char
*dst
, unsigned dstsiz
,
394 const u_char
**dnptrs
, const u_char
**lastdnptr
)
397 const u_char
**cpp
, **lpp
, *eob
, *msg
;
406 if (dnptrs
!= NULL
) {
407 if ((msg
= *dnptrs
++) != NULL
) {
408 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
410 lpp
= cpp
; /* end of list to search */
415 /* make sure the domain we are about to add is legal */
419 if ((n
& NS_CMPRSFLGS
) != 0) {
431 /* from here on we need to reset compression pointer array on error */
434 /* Look to see if we can use pointers. */
436 if (n
!= 0 && msg
!= NULL
) {
437 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
438 (const u_char
* const *)lpp
);
440 if (dstp
+ 1 >= eob
) {
443 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
447 /* Not found, save it. */
448 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
449 (dstp
- msg
) < 0x4000) {
454 /* copy label to buffer */
455 if (n
& NS_CMPRSFLGS
) { /* Should not happen. */
458 if (dstp
+ 1 + n
>= eob
) {
461 memcpy(dstp
, srcp
, n
+ 1);
478 * MRns_name_uncompress(msg, eom, src, dst, dstsiz)
479 * Expand compressed domain name to presentation format.
481 * Number of bytes read out of `src', or -1 (with errno set).
483 * Root domain returns as "." not "".
486 MRns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
487 char *dst
, size_t dstsiz
)
489 u_char tmp
[NS_MAXCDNAME
];
492 if ((n
= MRns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
494 if (MRns_name_ntop(tmp
, dst
, dstsiz
) == -1)
501 * MRns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
502 * Compress a domain name into wire format, using compression pointers.
504 * Number of bytes consumed in `dst' or -1 (with errno set).
506 * 'dnptrs' is an array of pointers to previous compressed names.
507 * dnptrs[0] is a pointer to the beginning of the message.
508 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
509 * array pointed to by 'dnptrs'. Side effect is to update the list of
510 * pointers for labels inserted into the message as we compress the name.
511 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
512 * is NULL, we don't update the list.
515 MRns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
516 const u_char
**dnptrs
, const u_char
**lastdnptr
)
518 u_char tmp
[NS_MAXCDNAME
];
520 if (MRns_name_pton(src
, tmp
, sizeof tmp
) == -1)
522 return (MRns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
527 * MRns_name_skip(ptrptr, eom)
528 * Advance *ptrptr to skip over the compressed name it points at.
530 * 0 on success, -1 (with errno set) on failure.
533 MRns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
538 while (cp
< eom
&& (n
= *cp
++) != 0) {
539 /* Check for indirection. */
540 switch (n
& NS_CMPRSFLGS
) {
541 case 0: /* normal case, n == len */
544 case NS_CMPRSFLGS
: /* indirection */
547 default: /* illegal type */
566 * Thinking in noninternationalized USASCII (per the DNS spec),
567 * is this characted special ("in need of quoting") ?
577 case 0x5C: /* '\\' */
578 /* Special modifiers in zone files. */
589 * Thinking in noninternationalized USASCII (per the DNS spec),
590 * is this character visible and not a space when printed ?
596 return (ch
> 0x20 && ch
< 0x7f);
600 * Thinking in noninternationalized USASCII (per the DNS spec),
601 * convert this character to lower case if it's upper case.
605 if (ch
>= 0x41 && ch
<= 0x5A)
611 * dn_find(domain, msg, dnptrs, lastdnptr)
612 * Search for the counted-label name in an array of compressed names.
614 * offset from msg if found, or -1.
616 * dnptrs is the pointer to the first name on the list,
617 * not the pointer to the start of the message.
620 dn_find(const u_char
*domain
, const u_char
*msg
,
621 const u_char
* const *dnptrs
,
622 const u_char
* const *lastdnptr
)
624 const u_char
*dn
, *cp
, *sp
;
625 const u_char
* const *cpp
;
628 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
631 while ((n
= *cp
++) != 0) {
633 * check for indirection
635 switch (n
& NS_CMPRSFLGS
) {
636 case 0: /* normal case, n == len */
639 for ((void)NULL
; n
> 0; n
--)
640 if (mklower(*dn
++) != mklower(*cp
++))
642 /* Is next root for both ? */
643 if (*dn
== '\0' && *cp
== '\0')
649 case NS_CMPRSFLGS
: /* indirection */
650 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
653 default: /* illegal type */