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
18 #if !defined(_LIBC) && !defined(lint)
19 static const char rcsid
[] = "$BINDId: ns_name.c,v 8.15 2000/03/30 22:53:46 vixie Exp $";
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
35 static const char digits
[] = "0123456789";
39 static int special(int);
40 static int printable(int);
41 static int dn_find(const u_char
*, const u_char
*,
42 const u_char
* const *,
43 const u_char
* const *);
48 * 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 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. */
70 __set_errno (EMSGSIZE
);
75 __set_errno (EMSGSIZE
);
83 if (dn
+ n
* 2 + 4 >= eom
) {
84 __set_errno (EMSGSIZE
);
94 *dn
++ = u
> 9 ? 'a' + u
- 10 : '0' + u
;
96 *dn
++ = u
> 9 ? 'a' + u
- 10 : '0' + u
;
104 __set_errno (EMSGSIZE
);
107 for ((void)NULL
; n
> 0; n
--) {
111 __set_errno (EMSGSIZE
);
116 } else if (!printable(c
)) {
118 __set_errno (EMSGSIZE
);
122 *dn
++ = digits
[c
/ 100];
123 *dn
++ = digits
[(c
% 100) / 10];
124 *dn
++ = digits
[c
% 10];
127 __set_errno (EMSGSIZE
);
136 __set_errno (EMSGSIZE
);
142 __set_errno (EMSGSIZE
);
148 libresolv_hidden_def (ns_name_ntop
)
151 * ns_name_pton(src, dst, dstsiz)
152 * Convert a ascii string into an encoded domain name as per RFC1035.
155 * 1 if string was fully qualified
156 * 0 is string was not fully qualified
158 * Enforces label and domain length limits.
162 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
163 u_char
*label
, *bp
, *eom
;
172 while ((c
= *src
++) != 0) {
174 if ((cp
= strchr(digits
, c
)) != NULL
) {
175 n
= (cp
- digits
) * 100;
176 if ((c
= *src
++) == 0 ||
177 (cp
= strchr(digits
, c
)) == NULL
) {
178 __set_errno (EMSGSIZE
);
181 n
+= (cp
- digits
) * 10;
182 if ((c
= *src
++) == 0 ||
183 (cp
= strchr(digits
, c
)) == NULL
) {
184 __set_errno (EMSGSIZE
);
189 __set_errno (EMSGSIZE
);
193 } else if (c
== '[' && label
== bp
- 1 && *src
== 'x') {
194 /* Theoretically we would have to handle \[o
195 as well but we do not since we do not need
200 while (isxdigit (*src
)) {
201 n
= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
203 if (! isxdigit(*src
)) {
204 __set_errno (EMSGSIZE
);
208 n
+= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
210 __set_errno (EMSGSIZE
);
216 *label
= (bp
- label
- 1) * 8;
217 if (*src
++ != ']' || *src
++ != '.') {
218 __set_errno (EMSGSIZE
);
224 __set_errno (EMSGSIZE
);
230 } else if (c
== '\\') {
233 } else if (c
== '.') {
234 c
= (bp
- label
- 1);
235 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
236 __set_errno (EMSGSIZE
);
240 __set_errno (EMSGSIZE
);
244 /* Fully qualified ? */
248 __set_errno (EMSGSIZE
);
253 if ((bp
- dst
) > MAXCDNAME
) {
254 __set_errno (EMSGSIZE
);
259 if (c
== 0 || *src
== '.') {
260 __set_errno (EMSGSIZE
);
267 __set_errno (EMSGSIZE
);
272 c
= (bp
- label
- 1);
273 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
274 __set_errno (EMSGSIZE
);
278 __set_errno (EMSGSIZE
);
284 __set_errno (EMSGSIZE
);
289 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
290 __set_errno (EMSGSIZE
);
297 * ns_name_ntol(src, dst, dstsiz)
298 * Convert a network strings labels into all lowercase.
300 * Number of bytes written to buffer, or -1 (with errno set)
302 * Enforces label and domain length limits.
306 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
) {
316 while ((n
= *cp
++) != 0) {
317 if ((n
& NS_CMPRSFLGS
) != 0) {
318 /* Some kind of compression pointer. */
319 __set_errno (EMSGSIZE
);
324 __set_errno (EMSGSIZE
);
327 for ((void)NULL
; n
> 0; n
--) {
340 * ns_name_unpack(msg, eom, src, dst, dstsiz)
341 * Unpack a domain name from a message, source may be compressed.
343 * -1 if it fails, or consumed octets if it succeeds.
346 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
347 u_char
*dst
, size_t dstsiz
)
349 const u_char
*srcp
, *dstlim
;
357 dstlim
= dst
+ dstsiz
;
358 if (srcp
< msg
|| srcp
>= eom
) {
359 __set_errno (EMSGSIZE
);
362 /* Fetch next label in domain name. */
363 while ((n
= *srcp
++) != 0) {
364 /* Check for indirection. */
365 switch (n
& NS_CMPRSFLGS
) {
368 if (dstp
+ 1 >= dstlim
) {
369 __set_errno (EMSGSIZE
);
376 __set_errno (EMSGSIZE
);
377 return (-1); /* flag error */
382 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
383 __set_errno (EMSGSIZE
);
387 dstp
= mempcpy(dstp
, srcp
- 1, n
+ 1);
393 __set_errno (EMSGSIZE
);
397 len
= srcp
- src
+ 1;
398 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
399 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
400 __set_errno (EMSGSIZE
);
405 * Check for loops in the compressed name;
406 * if we've looked at the whole message,
407 * there must be a loop.
409 if (checked
>= eom
- msg
) {
410 __set_errno (EMSGSIZE
);
416 __set_errno (EMSGSIZE
);
417 return (-1); /* flag error */
425 libresolv_hidden_def (ns_name_unpack
)
428 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
429 * Pack domain name 'domain' into 'comp_dn'.
431 * Size of the compressed name, or -1.
433 * 'dnptrs' is an array of pointers to previous compressed names.
434 * dnptrs[0] is a pointer to the beginning of the message. The array
436 * 'lastdnptr' is a pointer to the end of the array pointed to
439 * The list of pointers in dnptrs is updated for labels inserted into
440 * the message as we compress the name. If 'dnptr' is NULL, we don't
441 * try to compress names. If 'lastdnptr' is NULL, we don't update the
445 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
446 const u_char
**dnptrs
, const u_char
**lastdnptr
)
449 const u_char
**cpp
, **lpp
, *eob
, *msg
;
457 if (dnptrs
!= NULL
) {
458 if ((msg
= *dnptrs
++) != NULL
) {
459 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
461 lpp
= cpp
; /* end of list to search */
466 /* make sure the domain we are about to add is legal */
470 if ((n
& NS_CMPRSFLGS
) != 0 && n
!= 0x41) {
471 __set_errno (EMSGSIZE
);
478 __set_errno (EMSGSIZE
);
484 /* from here on we need to reset compression pointer array on error */
487 /* Look to see if we can use pointers. */
489 if (n
!= 0 && n
!= 0x41 && msg
!= NULL
) {
490 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
491 (const u_char
* const *)lpp
);
493 if (dstp
+ 1 >= eob
) {
496 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
500 /* Not found, save it. */
501 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
502 (dstp
- msg
) < 0x4000 && first
) {
508 /* copy label to buffer */
509 if ((n
& NS_CMPRSFLGS
) != 0 && n
!= 0x41) { /* Should not happen. */
518 if (dstp
+ 1 + n
>= eob
) {
521 memcpy(dstp
, srcp
, n
+ 1);
530 __set_errno (EMSGSIZE
);
537 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
538 * Expand compressed domain name to presentation format.
540 * Number of bytes read out of `src', or -1 (with errno set).
542 * Root domain returns as "." not "".
545 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
546 char *dst
, size_t dstsiz
)
548 u_char tmp
[NS_MAXCDNAME
];
551 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
553 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
559 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
560 * Compress a domain name into wire format, using compression pointers.
562 * Number of bytes consumed in `dst' or -1 (with errno set).
564 * 'dnptrs' is an array of pointers to previous compressed names.
565 * dnptrs[0] is a pointer to the beginning of the message.
566 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
567 * array pointed to by 'dnptrs'. Side effect is to update the list of
568 * pointers for labels inserted into the message as we compress the name.
569 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
570 * is NULL, we don't update the list.
573 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
574 const u_char
**dnptrs
, const u_char
**lastdnptr
)
576 u_char tmp
[NS_MAXCDNAME
];
578 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
580 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
584 * Reset dnptrs so that there are no active references to pointers at or
588 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
589 const u_char
**lastdnptr
)
591 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
592 if (*dnptrs
>= src
) {
601 * ns_name_skip(ptrptr, eom)
602 * Advance *ptrptr to skip over the compressed name it points at.
604 * 0 on success, -1 (with errno set) on failure.
607 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
612 while (cp
< eom
&& (n
= *cp
++) != 0) {
613 /* Check for indirection. */
614 switch (n
& NS_CMPRSFLGS
) {
615 case 0: /* normal case, n == len */
618 case NS_CMPRSFLGS
: /* indirection */
621 default: /* illegal type */
622 __set_errno (EMSGSIZE
);
628 __set_errno (EMSGSIZE
);
639 * Thinking in noninternationalized USASCII (per the DNS spec),
640 * is this characted special ("in need of quoting") ?
650 case 0x5C: /* '\\' */
651 /* Special modifiers in zone files. */
662 * Thinking in noninternationalized USASCII (per the DNS spec),
663 * is this character visible and not a space when printed ?
669 return (ch
> 0x20 && ch
< 0x7f);
673 * Thinking in noninternationalized USASCII (per the DNS spec),
674 * convert this character to lower case if it's upper case.
678 if (ch
>= 0x41 && ch
<= 0x5A)
684 * dn_find(domain, msg, dnptrs, lastdnptr)
685 * Search for the counted-label name in an array of compressed names.
687 * offset from msg if found, or -1.
689 * dnptrs is the pointer to the first name on the list,
690 * not the pointer to the start of the message.
693 dn_find(const u_char
*domain
, const u_char
*msg
,
694 const u_char
* const *dnptrs
,
695 const u_char
* const *lastdnptr
)
697 const u_char
*dn
, *cp
, *sp
;
698 const u_char
* const *cpp
;
701 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
704 * terminate search on:
706 * compression pointer
709 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
710 (sp
- msg
) < 0x4000) {
713 while ((n
= *cp
++) != 0) {
715 * check for indirection
717 switch (n
& NS_CMPRSFLGS
) {
718 case 0: /* normal case, n == len */
721 for ((void)NULL
; n
> 0; n
--)
722 if (mklower(*dn
++) !=
725 /* Is next root for both ? */
726 if (*dn
== '\0' && *cp
== '\0')
732 case NS_CMPRSFLGS
: /* indirection */
733 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
736 default: /* illegal type */
737 __set_errno (EMSGSIZE
);
745 __set_errno (ENOENT
);