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
);
150 * ns_name_pton(src, dst, dstsiz)
151 * Convert a ascii string into an encoded domain name as per RFC1035.
154 * 1 if string was fully qualified
155 * 0 is string was not fully qualified
157 * Enforces label and domain length limits.
161 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
162 u_char
*label
, *bp
, *eom
;
171 while ((c
= *src
++) != 0) {
173 if ((cp
= strchr(digits
, c
)) != NULL
) {
174 n
= (cp
- digits
) * 100;
175 if ((c
= *src
++) == 0 ||
176 (cp
= strchr(digits
, c
)) == NULL
) {
177 __set_errno (EMSGSIZE
);
180 n
+= (cp
- digits
) * 10;
181 if ((c
= *src
++) == 0 ||
182 (cp
= strchr(digits
, c
)) == NULL
) {
183 __set_errno (EMSGSIZE
);
188 __set_errno (EMSGSIZE
);
192 } else if (c
== '[' && label
== bp
- 1 && *src
== 'x') {
193 /* Theoretically we would have to handle \[o
194 as well but we do not since we do not need
199 while (isxdigit (*src
)) {
200 n
= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
202 if (! isxdigit(*src
)) {
203 __set_errno (EMSGSIZE
);
207 n
+= *src
> '9' ? *src
- 'a' + 10 : *src
- '0';
209 __set_errno (EMSGSIZE
);
215 *label
= (bp
- label
- 1) * 8;
216 if (*src
++ != ']' || *src
++ != '.') {
217 __set_errno (EMSGSIZE
);
223 __set_errno (EMSGSIZE
);
229 } else if (c
== '\\') {
232 } else if (c
== '.') {
233 c
= (bp
- label
- 1);
234 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
235 __set_errno (EMSGSIZE
);
239 __set_errno (EMSGSIZE
);
243 /* Fully qualified ? */
247 __set_errno (EMSGSIZE
);
252 if ((bp
- dst
) > MAXCDNAME
) {
253 __set_errno (EMSGSIZE
);
258 if (c
== 0 || *src
== '.') {
259 __set_errno (EMSGSIZE
);
266 __set_errno (EMSGSIZE
);
271 c
= (bp
- label
- 1);
272 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
273 __set_errno (EMSGSIZE
);
277 __set_errno (EMSGSIZE
);
283 __set_errno (EMSGSIZE
);
288 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
289 __set_errno (EMSGSIZE
);
296 * ns_name_ntol(src, dst, dstsiz)
297 * Convert a network strings labels into all lowercase.
299 * Number of bytes written to buffer, or -1 (with errno set)
301 * Enforces label and domain length limits.
305 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
) {
315 while ((n
= *cp
++) != 0) {
316 if ((n
& NS_CMPRSFLGS
) != 0) {
317 /* Some kind of compression pointer. */
318 __set_errno (EMSGSIZE
);
323 __set_errno (EMSGSIZE
);
326 for ((void)NULL
; n
> 0; n
--) {
339 * ns_name_unpack(msg, eom, src, dst, dstsiz)
340 * Unpack a domain name from a message, source may be compressed.
342 * -1 if it fails, or consumed octets if it succeeds.
345 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
346 u_char
*dst
, size_t dstsiz
)
348 const u_char
*srcp
, *dstlim
;
356 dstlim
= dst
+ dstsiz
;
357 if (srcp
< msg
|| srcp
>= eom
) {
358 __set_errno (EMSGSIZE
);
361 /* Fetch next label in domain name. */
362 while ((n
= *srcp
++) != 0) {
363 /* Check for indirection. */
364 switch (n
& NS_CMPRSFLGS
) {
367 if (dstp
+ 1 >= dstlim
) {
368 __set_errno (EMSGSIZE
);
375 __set_errno (EMSGSIZE
);
376 return (-1); /* flag error */
381 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
382 __set_errno (EMSGSIZE
);
386 dstp
= mempcpy(dstp
, srcp
- 1, n
+ 1);
392 __set_errno (EMSGSIZE
);
396 len
= srcp
- src
+ 1;
397 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
398 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
399 __set_errno (EMSGSIZE
);
404 * Check for loops in the compressed name;
405 * if we've looked at the whole message,
406 * there must be a loop.
408 if (checked
>= eom
- msg
) {
409 __set_errno (EMSGSIZE
);
415 __set_errno (EMSGSIZE
);
416 return (-1); /* flag error */
426 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
427 * Pack domain name 'domain' into 'comp_dn'.
429 * Size of the compressed name, or -1.
431 * 'dnptrs' is an array of pointers to previous compressed names.
432 * dnptrs[0] is a pointer to the beginning of the message. The array
434 * 'lastdnptr' is a pointer to the end of the array pointed to
437 * The list of pointers in dnptrs is updated for labels inserted into
438 * the message as we compress the name. If 'dnptr' is NULL, we don't
439 * try to compress names. If 'lastdnptr' is NULL, we don't update the
443 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
444 const u_char
**dnptrs
, const u_char
**lastdnptr
)
447 const u_char
**cpp
, **lpp
, *eob
, *msg
;
455 if (dnptrs
!= NULL
) {
456 if ((msg
= *dnptrs
++) != NULL
) {
457 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
459 lpp
= cpp
; /* end of list to search */
464 /* make sure the domain we are about to add is legal */
468 if ((n
& NS_CMPRSFLGS
) != 0 && n
!= 0x41) {
469 __set_errno (EMSGSIZE
);
476 __set_errno (EMSGSIZE
);
482 /* from here on we need to reset compression pointer array on error */
485 /* Look to see if we can use pointers. */
487 if (n
!= 0 && n
!= 0x41 && msg
!= NULL
) {
488 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
489 (const u_char
* const *)lpp
);
491 if (dstp
+ 1 >= eob
) {
494 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
498 /* Not found, save it. */
499 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
500 (dstp
- msg
) < 0x4000 && first
) {
506 /* copy label to buffer */
507 if ((n
& NS_CMPRSFLGS
) != 0 && n
!= 0x41) { /* Should not happen. */
516 if (dstp
+ 1 + n
>= eob
) {
519 memcpy(dstp
, srcp
, n
+ 1);
528 __set_errno (EMSGSIZE
);
535 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
536 * Expand compressed domain name to presentation format.
538 * Number of bytes read out of `src', or -1 (with errno set).
540 * Root domain returns as "." not "".
543 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
544 char *dst
, size_t dstsiz
)
546 u_char tmp
[NS_MAXCDNAME
];
549 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
551 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
557 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
558 * Compress a domain name into wire format, using compression pointers.
560 * Number of bytes consumed in `dst' or -1 (with errno set).
562 * 'dnptrs' is an array of pointers to previous compressed names.
563 * dnptrs[0] is a pointer to the beginning of the message.
564 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
565 * array pointed to by 'dnptrs'. Side effect is to update the list of
566 * pointers for labels inserted into the message as we compress the name.
567 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
568 * is NULL, we don't update the list.
571 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
572 const u_char
**dnptrs
, const u_char
**lastdnptr
)
574 u_char tmp
[NS_MAXCDNAME
];
576 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
578 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
582 * Reset dnptrs so that there are no active references to pointers at or
586 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
587 const u_char
**lastdnptr
)
589 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
590 if (*dnptrs
>= src
) {
599 * ns_name_skip(ptrptr, eom)
600 * Advance *ptrptr to skip over the compressed name it points at.
602 * 0 on success, -1 (with errno set) on failure.
605 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
610 while (cp
< eom
&& (n
= *cp
++) != 0) {
611 /* Check for indirection. */
612 switch (n
& NS_CMPRSFLGS
) {
613 case 0: /* normal case, n == len */
616 case NS_CMPRSFLGS
: /* indirection */
619 default: /* illegal type */
620 __set_errno (EMSGSIZE
);
626 __set_errno (EMSGSIZE
);
637 * Thinking in noninternationalized USASCII (per the DNS spec),
638 * is this characted special ("in need of quoting") ?
648 case 0x5C: /* '\\' */
649 /* Special modifiers in zone files. */
660 * Thinking in noninternationalized USASCII (per the DNS spec),
661 * is this character visible and not a space when printed ?
667 return (ch
> 0x20 && ch
< 0x7f);
671 * Thinking in noninternationalized USASCII (per the DNS spec),
672 * convert this character to lower case if it's upper case.
676 if (ch
>= 0x41 && ch
<= 0x5A)
682 * dn_find(domain, msg, dnptrs, lastdnptr)
683 * Search for the counted-label name in an array of compressed names.
685 * offset from msg if found, or -1.
687 * dnptrs is the pointer to the first name on the list,
688 * not the pointer to the start of the message.
691 dn_find(const u_char
*domain
, const u_char
*msg
,
692 const u_char
* const *dnptrs
,
693 const u_char
* const *lastdnptr
)
695 const u_char
*dn
, *cp
, *sp
;
696 const u_char
* const *cpp
;
699 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
702 * terminate search on:
704 * compression pointer
707 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
708 (sp
- msg
) < 0x4000) {
711 while ((n
= *cp
++) != 0) {
713 * check for indirection
715 switch (n
& NS_CMPRSFLGS
) {
716 case 0: /* normal case, n == len */
719 for ((void)NULL
; n
> 0; n
--)
720 if (mklower(*dn
++) !=
723 /* Is next root for both ? */
724 if (*dn
== '\0' && *cp
== '\0')
730 case NS_CMPRSFLGS
: /* indirection */
731 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
734 default: /* illegal type */
735 __set_errno (EMSGSIZE
);
743 __set_errno (ENOENT
);