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>
34 static const char digits
[] = "0123456789";
38 static int special(int);
39 static int printable(int);
40 static int dn_find(const u_char
*, const u_char
*,
41 const u_char
* const *,
42 const u_char
* const *);
47 * ns_name_ntop(src, dst, dstsiz)
48 * Convert an encoded domain name to printable ascii as per RFC1035.
50 * Number of bytes written to buffer, or -1 (with errno set)
52 * The root is returned as "."
53 * All other domains are returned in non absolute form
56 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
) {
66 while ((n
= *cp
++) != 0) {
67 if ((n
& NS_CMPRSFLGS
) != 0) {
68 /* Some kind of compression pointer. */
69 __set_errno (EMSGSIZE
);
74 __set_errno (EMSGSIZE
);
80 __set_errno (EMSGSIZE
);
83 for ((void)NULL
; n
> 0; n
--) {
87 __set_errno (EMSGSIZE
);
92 } else if (!printable(c
)) {
94 __set_errno (EMSGSIZE
);
98 *dn
++ = digits
[c
/ 100];
99 *dn
++ = digits
[(c
% 100) / 10];
100 *dn
++ = digits
[c
% 10];
103 __set_errno (EMSGSIZE
);
112 __set_errno (EMSGSIZE
);
118 __set_errno (EMSGSIZE
);
126 * ns_name_pton(src, dst, dstsiz)
127 * Convert a ascii string into an encoded domain name as per RFC1035.
130 * 1 if string was fully qualified
131 * 0 is string was not fully qualified
133 * Enforces label and domain length limits.
137 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
) {
138 u_char
*label
, *bp
, *eom
;
147 while ((c
= *src
++) != 0) {
149 if ((cp
= strchr(digits
, c
)) != NULL
) {
150 n
= (cp
- digits
) * 100;
151 if ((c
= *src
++) == 0 ||
152 (cp
= strchr(digits
, c
)) == NULL
) {
153 __set_errno (EMSGSIZE
);
156 n
+= (cp
- digits
) * 10;
157 if ((c
= *src
++) == 0 ||
158 (cp
= strchr(digits
, c
)) == NULL
) {
159 __set_errno (EMSGSIZE
);
164 __set_errno (EMSGSIZE
);
170 } else if (c
== '\\') {
173 } else if (c
== '.') {
174 c
= (bp
- label
- 1);
175 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
176 __set_errno (EMSGSIZE
);
180 __set_errno (EMSGSIZE
);
184 /* Fully qualified ? */
188 __set_errno (EMSGSIZE
);
193 if ((bp
- dst
) > MAXCDNAME
) {
194 __set_errno (EMSGSIZE
);
199 if (c
== 0 || *src
== '.') {
200 __set_errno (EMSGSIZE
);
207 __set_errno (EMSGSIZE
);
212 c
= (bp
- label
- 1);
213 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
214 __set_errno (EMSGSIZE
);
218 __set_errno (EMSGSIZE
);
224 __set_errno (EMSGSIZE
);
229 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
230 __set_errno (EMSGSIZE
);
237 * ns_name_ntol(src, dst, dstsiz)
238 * Convert a network strings labels into all lowercase.
240 * Number of bytes written to buffer, or -1 (with errno set)
242 * Enforces label and domain length limits.
246 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
) {
256 while ((n
= *cp
++) != 0) {
257 if ((n
& NS_CMPRSFLGS
) != 0) {
258 /* Some kind of compression pointer. */
259 __set_errno (EMSGSIZE
);
264 __set_errno (EMSGSIZE
);
267 for ((void)NULL
; n
> 0; n
--) {
280 * ns_name_unpack(msg, eom, src, dst, dstsiz)
281 * Unpack a domain name from a message, source may be compressed.
283 * -1 if it fails, or consumed octets if it succeeds.
286 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
287 u_char
*dst
, size_t dstsiz
)
289 const u_char
*srcp
, *dstlim
;
297 dstlim
= dst
+ dstsiz
;
298 if (srcp
< msg
|| srcp
>= eom
) {
299 __set_errno (EMSGSIZE
);
302 /* Fetch next label in domain name. */
303 while ((n
= *srcp
++) != 0) {
304 /* Check for indirection. */
305 switch (n
& NS_CMPRSFLGS
) {
308 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
309 __set_errno (EMSGSIZE
);
314 memcpy(dstp
, srcp
, n
);
321 __set_errno (EMSGSIZE
);
325 len
= srcp
- src
+ 1;
326 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
327 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
328 __set_errno (EMSGSIZE
);
333 * Check for loops in the compressed name;
334 * if we've looked at the whole message,
335 * there must be a loop.
337 if (checked
>= eom
- msg
) {
338 __set_errno (EMSGSIZE
);
344 __set_errno (EMSGSIZE
);
345 return (-1); /* flag error */
355 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
356 * Pack domain name 'domain' into 'comp_dn'.
358 * Size of the compressed name, or -1.
360 * 'dnptrs' is an array of pointers to previous compressed names.
361 * dnptrs[0] is a pointer to the beginning of the message. The array
363 * 'lastdnptr' is a pointer to the end of the array pointed to
366 * The list of pointers in dnptrs is updated for labels inserted into
367 * the message as we compress the name. If 'dnptr' is NULL, we don't
368 * try to compress names. If 'lastdnptr' is NULL, we don't update the
372 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
373 const u_char
**dnptrs
, const u_char
**lastdnptr
)
376 const u_char
**cpp
, **lpp
, *eob
, *msg
;
384 if (dnptrs
!= NULL
) {
385 if ((msg
= *dnptrs
++) != NULL
) {
386 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
388 lpp
= cpp
; /* end of list to search */
393 /* make sure the domain we are about to add is legal */
397 if ((n
& NS_CMPRSFLGS
) != 0) {
398 __set_errno (EMSGSIZE
);
403 __set_errno (EMSGSIZE
);
409 /* from here on we need to reset compression pointer array on error */
412 /* Look to see if we can use pointers. */
414 if (n
!= 0 && msg
!= NULL
) {
415 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
416 (const u_char
* const *)lpp
);
418 if (dstp
+ 1 >= eob
) {
421 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
425 /* Not found, save it. */
426 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
427 (dstp
- msg
) < 0x4000 && first
) {
433 /* copy label to buffer */
434 if (n
& NS_CMPRSFLGS
) { /* Should not happen. */
437 if (dstp
+ 1 + n
>= eob
) {
440 memcpy(dstp
, srcp
, n
+ 1);
449 __set_errno (EMSGSIZE
);
456 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
457 * Expand compressed domain name to presentation format.
459 * Number of bytes read out of `src', or -1 (with errno set).
461 * Root domain returns as "." not "".
464 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
465 char *dst
, size_t dstsiz
)
467 u_char tmp
[NS_MAXCDNAME
];
470 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
472 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
478 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
479 * Compress a domain name into wire format, using compression pointers.
481 * Number of bytes consumed in `dst' or -1 (with errno set).
483 * 'dnptrs' is an array of pointers to previous compressed names.
484 * dnptrs[0] is a pointer to the beginning of the message.
485 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
486 * array pointed to by 'dnptrs'. Side effect is to update the list of
487 * pointers for labels inserted into the message as we compress the name.
488 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
489 * is NULL, we don't update the list.
492 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
493 const u_char
**dnptrs
, const u_char
**lastdnptr
)
495 u_char tmp
[NS_MAXCDNAME
];
497 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
499 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
503 * Reset dnptrs so that there are no active references to pointers at or
507 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
508 const u_char
**lastdnptr
)
510 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
511 if (*dnptrs
>= src
) {
520 * ns_name_skip(ptrptr, eom)
521 * Advance *ptrptr to skip over the compressed name it points at.
523 * 0 on success, -1 (with errno set) on failure.
526 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
) {
531 while (cp
< eom
&& (n
= *cp
++) != 0) {
532 /* Check for indirection. */
533 switch (n
& NS_CMPRSFLGS
) {
534 case 0: /* normal case, n == len */
537 case NS_CMPRSFLGS
: /* indirection */
540 default: /* illegal type */
541 __set_errno (EMSGSIZE
);
547 __set_errno (EMSGSIZE
);
558 * Thinking in noninternationalized USASCII (per the DNS spec),
559 * is this characted special ("in need of quoting") ?
569 case 0x5C: /* '\\' */
570 /* Special modifiers in zone files. */
581 * Thinking in noninternationalized USASCII (per the DNS spec),
582 * is this character visible and not a space when printed ?
588 return (ch
> 0x20 && ch
< 0x7f);
592 * Thinking in noninternationalized USASCII (per the DNS spec),
593 * convert this character to lower case if it's upper case.
597 if (ch
>= 0x41 && ch
<= 0x5A)
603 * dn_find(domain, msg, dnptrs, lastdnptr)
604 * Search for the counted-label name in an array of compressed names.
606 * offset from msg if found, or -1.
608 * dnptrs is the pointer to the first name on the list,
609 * not the pointer to the start of the message.
612 dn_find(const u_char
*domain
, const u_char
*msg
,
613 const u_char
* const *dnptrs
,
614 const u_char
* const *lastdnptr
)
616 const u_char
*dn
, *cp
, *sp
;
617 const u_char
* const *cpp
;
620 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
623 * terminate search on:
625 * compression pointer
628 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
629 (sp
- msg
) < 0x4000) {
632 while ((n
= *cp
++) != 0) {
634 * check for indirection
636 switch (n
& NS_CMPRSFLGS
) {
637 case 0: /* normal case, n == len */
640 for ((void)NULL
; n
> 0; n
--)
641 if (mklower(*dn
++) !=
644 /* Is next root for both ? */
645 if (*dn
== '\0' && *cp
== '\0')
651 case NS_CMPRSFLGS
: /* indirection */
652 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
655 default: /* illegal type */
656 __set_errno (EMSGSIZE
);
664 __set_errno (ENOENT
);