2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
30 # define SPRINTF(x) ((size_t)sprintf x)
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 *);
43 static int labellen(const u_char
*);
48 * Convert an encoded domain name to printable ascii as per RFC1035.
51 *\li Number of bytes written to buffer, or -1 (with errno set)
54 *\li The root is returned as "."
55 *\li All other domains are returned in non absolute form
58 ns_name_ntop(const u_char
*src
, char *dst
, size_t dstsiz
)
70 while ((n
= *cp
++) != 0) {
71 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
72 /* Some kind of compression pointer. */
73 __set_errno (EMSGSIZE
);
78 __set_errno (EMSGSIZE
);
83 if ((l
= labellen(cp
- 1)) < 0) {
84 __set_errno (EMSGSIZE
);
88 __set_errno (EMSGSIZE
);
91 for ((void)NULL
; l
> 0; l
--) {
95 __set_errno (EMSGSIZE
);
100 } else if (!printable(c
)) {
102 __set_errno (EMSGSIZE
);
106 *dn
++ = digits
[c
/ 100];
107 *dn
++ = digits
[(c
% 100) / 10];
108 *dn
++ = digits
[c
% 10];
111 __set_errno (EMSGSIZE
);
120 __set_errno (EMSGSIZE
);
126 __set_errno (EMSGSIZE
);
132 libresolv_hidden_def (ns_name_ntop
)
133 strong_alias (ns_name_ntop
, __ns_name_ntop
)
136 * Convert an ascii string into an encoded domain name as per RFC1035.
141 *\li 1 if string was fully qualified
142 *\li 0 is string was not fully qualified
145 *\li Enforces label and domain length limits.
149 ns_name_pton(const char *src
, u_char
*dst
, size_t dstsiz
)
151 u_char
*label
, *bp
, *eom
;
160 while ((c
= *src
++) != 0) {
162 if ((cp
= strchr(digits
, c
)) != NULL
) {
163 n
= (cp
- digits
) * 100;
164 if ((c
= *src
++) == 0 ||
165 (cp
= strchr(digits
, c
)) == NULL
) {
166 __set_errno (EMSGSIZE
);
169 n
+= (cp
- digits
) * 10;
170 if ((c
= *src
++) == 0 ||
171 (cp
= strchr(digits
, c
)) == NULL
) {
172 __set_errno (EMSGSIZE
);
177 __set_errno (EMSGSIZE
);
183 } else if (c
== '\\') {
186 } else if (c
== '.') {
187 c
= (bp
- label
- 1);
188 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
189 __set_errno (EMSGSIZE
);
193 __set_errno (EMSGSIZE
);
197 /* Fully qualified ? */
201 __set_errno (EMSGSIZE
);
206 if ((bp
- dst
) > MAXCDNAME
) {
207 __set_errno (EMSGSIZE
);
212 if (c
== 0 || *src
== '.') {
213 __set_errno (EMSGSIZE
);
220 __set_errno (EMSGSIZE
);
226 /* Trailing backslash. */
227 __set_errno (EMSGSIZE
);
230 c
= (bp
- label
- 1);
231 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
232 __set_errno (EMSGSIZE
);
236 __set_errno (EMSGSIZE
);
242 __set_errno (EMSGSIZE
);
247 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
248 __set_errno (EMSGSIZE
);
253 libresolv_hidden_def (ns_name_pton
)
256 * Convert a network strings labels into all lowercase.
259 *\li Number of bytes written to buffer, or -1 (with errno set)
262 *\li Enforces label and domain length limits.
266 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
279 __set_errno (EMSGSIZE
);
282 while ((n
= *cp
++) != 0) {
283 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
284 /* Some kind of compression pointer. */
285 __set_errno (EMSGSIZE
);
289 if ((l
= labellen(cp
- 1)) < 0) {
290 __set_errno (EMSGSIZE
);
294 __set_errno (EMSGSIZE
);
297 for ((void)NULL
; l
> 0; l
--) {
310 * Unpack a domain name from a message, source may be compressed.
313 *\li -1 if it fails, or consumed octets if it succeeds.
316 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
317 u_char
*dst
, size_t dstsiz
)
319 const u_char
*srcp
, *dstlim
;
321 int n
, len
, checked
, l
;
327 dstlim
= dst
+ dstsiz
;
328 if (srcp
< msg
|| srcp
>= eom
) {
329 __set_errno (EMSGSIZE
);
332 /* Fetch next label in domain name. */
333 while ((n
= *srcp
++) != 0) {
334 /* Check for indirection. */
335 switch (n
& NS_CMPRSFLGS
) {
338 if ((l
= labellen(srcp
- 1)) < 0) {
339 __set_errno (EMSGSIZE
);
342 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
343 __set_errno (EMSGSIZE
);
348 memcpy(dstp
, srcp
, l
);
355 __set_errno (EMSGSIZE
);
359 len
= srcp
- src
+ 1;
360 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
361 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
362 __set_errno (EMSGSIZE
);
367 * Check for loops in the compressed name;
368 * if we've looked at the whole message,
369 * there must be a loop.
371 if (checked
>= eom
- msg
) {
372 __set_errno (EMSGSIZE
);
378 __set_errno (EMSGSIZE
);
379 return (-1); /*%< flag error */
387 libresolv_hidden_def (ns_name_unpack
)
388 strong_alias (ns_name_unpack
, __ns_name_unpack
)
391 * Pack domain name 'domain' into 'comp_dn'.
394 *\li Size of the compressed name, or -1.
397 *\li 'dnptrs' is an array of pointers to previous compressed names.
398 *\li dnptrs[0] is a pointer to the beginning of the message. The array
400 *\li 'lastdnptr' is a pointer to the end of the array pointed to
404 *\li The list of pointers in dnptrs is updated for labels inserted into
405 * the message as we compress the name. If 'dnptr' is NULL, we don't
406 * try to compress names. If 'lastdnptr' is NULL, we don't update the
410 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
411 const u_char
**dnptrs
, const u_char
**lastdnptr
)
414 const u_char
**cpp
, **lpp
, *eob
, *msg
;
422 if (dnptrs
!= NULL
) {
423 if ((msg
= *dnptrs
++) != NULL
) {
424 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
426 lpp
= cpp
; /*%< end of list to search */
431 /* make sure the domain we are about to add is legal */
437 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
438 __set_errno (EMSGSIZE
);
441 if ((l0
= labellen(srcp
)) < 0) {
442 __set_errno (EINVAL
);
447 __set_errno (EMSGSIZE
);
453 /* from here on we need to reset compression pointer array on error */
456 /* Look to see if we can use pointers. */
458 if (n
!= 0 && msg
!= NULL
) {
459 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
460 (const u_char
* const *)lpp
);
462 if (dstp
+ 1 >= eob
) {
465 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
469 /* Not found, save it. */
470 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
471 (dstp
- msg
) < 0x4000 && first
) {
477 /* copy label to buffer */
478 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
479 /* Should not happen. */
483 if (n
+ 1 > eob
- dstp
) {
486 memcpy(dstp
, srcp
, n
+ 1);
495 __set_errno (EMSGSIZE
);
500 libresolv_hidden_def (ns_name_pack
)
503 * Expand compressed domain name to presentation format.
506 *\li Number of bytes read out of `src', or -1 (with errno set).
509 *\li Root domain returns as "." not "".
512 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
513 char *dst
, size_t dstsiz
)
515 u_char tmp
[NS_MAXCDNAME
];
518 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
520 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
524 libresolv_hidden_def (ns_name_uncompress
)
527 * Compress a domain name into wire format, using compression pointers.
530 *\li Number of bytes consumed in `dst' or -1 (with errno set).
533 *\li 'dnptrs' is an array of pointers to previous compressed names.
534 *\li dnptrs[0] is a pointer to the beginning of the message.
535 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
536 * array pointed to by 'dnptrs'. Side effect is to update the list of
537 * pointers for labels inserted into the message as we compress the name.
538 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
539 * is NULL, we don't update the list.
542 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
543 const u_char
**dnptrs
, const u_char
**lastdnptr
)
545 u_char tmp
[NS_MAXCDNAME
];
547 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
549 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
551 libresolv_hidden_def (ns_name_compress
)
554 * Reset dnptrs so that there are no active references to pointers at or
558 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
559 const u_char
**lastdnptr
)
561 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
562 if (*dnptrs
>= src
) {
571 * Advance *ptrptr to skip over the compressed name it points at.
574 *\li 0 on success, -1 (with errno set) on failure.
577 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
583 while (cp
< eom
&& (n
= *cp
++) != 0) {
584 /* Check for indirection. */
585 switch (n
& NS_CMPRSFLGS
) {
586 case 0: /*%< normal case, n == len */
589 case NS_CMPRSFLGS
: /*%< indirection */
592 default: /*%< illegal type */
593 __set_errno (EMSGSIZE
);
599 __set_errno (EMSGSIZE
);
605 libresolv_hidden_def (ns_name_skip
)
610 * Thinking in noninternationalized USASCII (per the DNS spec),
611 * is this character special ("in need of quoting") ?
619 case 0x22: /*%< '"' */
620 case 0x2E: /*%< '.' */
621 case 0x3B: /*%< ';' */
622 case 0x5C: /*%< '\\' */
623 case 0x28: /*%< '(' */
624 case 0x29: /*%< ')' */
625 /* Special modifiers in zone files. */
626 case 0x40: /*%< '@' */
627 case 0x24: /*%< '$' */
635 * Thinking in noninternationalized USASCII (per the DNS spec),
636 * is this character visible and not a space when printed ?
643 return (ch
> 0x20 && ch
< 0x7f);
647 * Thinking in noninternationalized USASCII (per the DNS spec),
648 * convert this character to lower case if it's upper case.
652 if (ch
>= 0x41 && ch
<= 0x5A)
658 * Search for the counted-label name in an array of compressed names.
661 *\li offset from msg if found, or -1.
664 *\li dnptrs is the pointer to the first name on the list,
665 *\li not the pointer to the start of the message.
668 dn_find(const u_char
*domain
, const u_char
*msg
,
669 const u_char
* const *dnptrs
,
670 const u_char
* const *lastdnptr
)
672 const u_char
*dn
, *cp
, *sp
;
673 const u_char
* const *cpp
;
676 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
679 * terminate search on:
681 * compression pointer
684 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
685 (sp
- msg
) < 0x4000) {
688 while ((n
= *cp
++) != 0) {
690 * check for indirection
692 switch (n
& NS_CMPRSFLGS
) {
693 case 0: /*%< normal case, n == len */
694 n
= labellen(cp
- 1); /*%< XXX */
698 for ((void)NULL
; n
> 0; n
--)
699 if (mklower(*dn
++) !=
702 /* Is next root for both ? */
703 if (*dn
== '\0' && *cp
== '\0')
708 case NS_CMPRSFLGS
: /*%< indirection */
709 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
712 default: /*%< illegal type */
713 __set_errno (EMSGSIZE
);
721 __set_errno (ENOENT
);
725 /* Return the length of the encoded label starting at LP, or -1 for
726 compression references and extended label types. */
728 labellen (const unsigned char *lp
)