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
);
225 c
= (bp
- label
- 1);
226 if ((c
& NS_CMPRSFLGS
) != 0) { /*%< Label too big. */
227 __set_errno (EMSGSIZE
);
231 __set_errno (EMSGSIZE
);
237 __set_errno (EMSGSIZE
);
242 if ((bp
- dst
) > MAXCDNAME
) { /*%< src too big */
243 __set_errno (EMSGSIZE
);
248 libresolv_hidden_def (ns_name_pton
)
251 * Convert a network strings labels into all lowercase.
254 *\li Number of bytes written to buffer, or -1 (with errno set)
257 *\li Enforces label and domain length limits.
261 ns_name_ntol(const u_char
*src
, u_char
*dst
, size_t dstsiz
)
274 __set_errno (EMSGSIZE
);
277 while ((n
= *cp
++) != 0) {
278 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
279 /* Some kind of compression pointer. */
280 __set_errno (EMSGSIZE
);
284 if ((l
= labellen(cp
- 1)) < 0) {
285 __set_errno (EMSGSIZE
);
289 __set_errno (EMSGSIZE
);
292 for ((void)NULL
; l
> 0; l
--) {
305 * Unpack a domain name from a message, source may be compressed.
308 *\li -1 if it fails, or consumed octets if it succeeds.
311 ns_name_unpack(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
312 u_char
*dst
, size_t dstsiz
)
314 const u_char
*srcp
, *dstlim
;
316 int n
, len
, checked
, l
;
322 dstlim
= dst
+ dstsiz
;
323 if (srcp
< msg
|| srcp
>= eom
) {
324 __set_errno (EMSGSIZE
);
327 /* Fetch next label in domain name. */
328 while ((n
= *srcp
++) != 0) {
329 /* Check for indirection. */
330 switch (n
& NS_CMPRSFLGS
) {
333 if ((l
= labellen(srcp
- 1)) < 0) {
334 __set_errno (EMSGSIZE
);
337 if (dstp
+ l
+ 1 >= dstlim
|| srcp
+ l
>= eom
) {
338 __set_errno (EMSGSIZE
);
343 memcpy(dstp
, srcp
, l
);
350 __set_errno (EMSGSIZE
);
354 len
= srcp
- src
+ 1;
355 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
356 if (srcp
< msg
|| srcp
>= eom
) { /*%< Out of range. */
357 __set_errno (EMSGSIZE
);
362 * Check for loops in the compressed name;
363 * if we've looked at the whole message,
364 * there must be a loop.
366 if (checked
>= eom
- msg
) {
367 __set_errno (EMSGSIZE
);
373 __set_errno (EMSGSIZE
);
374 return (-1); /*%< flag error */
382 libresolv_hidden_def (ns_name_unpack
)
383 strong_alias (ns_name_unpack
, __ns_name_unpack
)
386 * Pack domain name 'domain' into 'comp_dn'.
389 *\li Size of the compressed name, or -1.
392 *\li 'dnptrs' is an array of pointers to previous compressed names.
393 *\li dnptrs[0] is a pointer to the beginning of the message. The array
395 *\li 'lastdnptr' is a pointer to the end of the array pointed to
399 *\li The list of pointers in dnptrs is updated for labels inserted into
400 * the message as we compress the name. If 'dnptr' is NULL, we don't
401 * try to compress names. If 'lastdnptr' is NULL, we don't update the
405 ns_name_pack(const u_char
*src
, u_char
*dst
, int dstsiz
,
406 const u_char
**dnptrs
, const u_char
**lastdnptr
)
409 const u_char
**cpp
, **lpp
, *eob
, *msg
;
417 if (dnptrs
!= NULL
) {
418 if ((msg
= *dnptrs
++) != NULL
) {
419 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
421 lpp
= cpp
; /*%< end of list to search */
426 /* make sure the domain we are about to add is legal */
432 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
433 __set_errno (EMSGSIZE
);
436 if ((l0
= labellen(srcp
)) < 0) {
437 __set_errno (EINVAL
);
442 __set_errno (EMSGSIZE
);
448 /* from here on we need to reset compression pointer array on error */
451 /* Look to see if we can use pointers. */
453 if (n
!= 0 && msg
!= NULL
) {
454 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
455 (const u_char
* const *)lpp
);
457 if (dstp
+ 1 >= eob
) {
460 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
464 /* Not found, save it. */
465 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
466 (dstp
- msg
) < 0x4000 && first
) {
472 /* copy label to buffer */
473 if ((n
& NS_CMPRSFLGS
) == NS_CMPRSFLGS
) {
474 /* Should not happen. */
478 if (dstp
+ 1 + n
>= eob
) {
481 memcpy(dstp
, srcp
, n
+ 1);
490 __set_errno (EMSGSIZE
);
495 libresolv_hidden_def (ns_name_pack
)
498 * Expand compressed domain name to presentation format.
501 *\li Number of bytes read out of `src', or -1 (with errno set).
504 *\li Root domain returns as "." not "".
507 ns_name_uncompress(const u_char
*msg
, const u_char
*eom
, const u_char
*src
,
508 char *dst
, size_t dstsiz
)
510 u_char tmp
[NS_MAXCDNAME
];
513 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
515 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
519 libresolv_hidden_def (ns_name_uncompress
)
522 * Compress a domain name into wire format, using compression pointers.
525 *\li Number of bytes consumed in `dst' or -1 (with errno set).
528 *\li 'dnptrs' is an array of pointers to previous compressed names.
529 *\li dnptrs[0] is a pointer to the beginning of the message.
530 *\li The list ends with NULL. 'lastdnptr' is a pointer to the end of the
531 * array pointed to by 'dnptrs'. Side effect is to update the list of
532 * pointers for labels inserted into the message as we compress the name.
533 *\li If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
534 * is NULL, we don't update the list.
537 ns_name_compress(const char *src
, u_char
*dst
, size_t dstsiz
,
538 const u_char
**dnptrs
, const u_char
**lastdnptr
)
540 u_char tmp
[NS_MAXCDNAME
];
542 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
544 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
546 libresolv_hidden_def (ns_name_compress
)
549 * Reset dnptrs so that there are no active references to pointers at or
553 ns_name_rollback(const u_char
*src
, const u_char
**dnptrs
,
554 const u_char
**lastdnptr
)
556 while (dnptrs
< lastdnptr
&& *dnptrs
!= NULL
) {
557 if (*dnptrs
>= src
) {
566 * Advance *ptrptr to skip over the compressed name it points at.
569 *\li 0 on success, -1 (with errno set) on failure.
572 ns_name_skip(const u_char
**ptrptr
, const u_char
*eom
)
578 while (cp
< eom
&& (n
= *cp
++) != 0) {
579 /* Check for indirection. */
580 switch (n
& NS_CMPRSFLGS
) {
581 case 0: /*%< normal case, n == len */
584 case NS_CMPRSFLGS
: /*%< indirection */
587 default: /*%< illegal type */
588 __set_errno (EMSGSIZE
);
594 __set_errno (EMSGSIZE
);
600 libresolv_hidden_def (ns_name_skip
)
605 * Thinking in noninternationalized USASCII (per the DNS spec),
606 * is this character special ("in need of quoting") ?
614 case 0x22: /*%< '"' */
615 case 0x2E: /*%< '.' */
616 case 0x3B: /*%< ';' */
617 case 0x5C: /*%< '\\' */
618 case 0x28: /*%< '(' */
619 case 0x29: /*%< ')' */
620 /* Special modifiers in zone files. */
621 case 0x40: /*%< '@' */
622 case 0x24: /*%< '$' */
630 * Thinking in noninternationalized USASCII (per the DNS spec),
631 * is this character visible and not a space when printed ?
638 return (ch
> 0x20 && ch
< 0x7f);
642 * Thinking in noninternationalized USASCII (per the DNS spec),
643 * convert this character to lower case if it's upper case.
647 if (ch
>= 0x41 && ch
<= 0x5A)
653 * Search for the counted-label name in an array of compressed names.
656 *\li offset from msg if found, or -1.
659 *\li dnptrs is the pointer to the first name on the list,
660 *\li not the pointer to the start of the message.
663 dn_find(const u_char
*domain
, const u_char
*msg
,
664 const u_char
* const *dnptrs
,
665 const u_char
* const *lastdnptr
)
667 const u_char
*dn
, *cp
, *sp
;
668 const u_char
* const *cpp
;
671 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
674 * terminate search on:
676 * compression pointer
679 while (*sp
!= 0 && (*sp
& NS_CMPRSFLGS
) == 0 &&
680 (sp
- msg
) < 0x4000) {
683 while ((n
= *cp
++) != 0) {
685 * check for indirection
687 switch (n
& NS_CMPRSFLGS
) {
688 case 0: /*%< normal case, n == len */
689 n
= labellen(cp
- 1); /*%< XXX */
693 for ((void)NULL
; n
> 0; n
--)
694 if (mklower(*dn
++) !=
697 /* Is next root for both ? */
698 if (*dn
== '\0' && *cp
== '\0')
703 case NS_CMPRSFLGS
: /*%< indirection */
704 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
707 default: /*%< illegal type */
708 __set_errno (EMSGSIZE
);
716 __set_errno (ENOENT
);
720 /* Return the length of the encoded label starting at LP, or -1 for
721 compression references and extended label types. */
723 labellen (const unsigned char *lp
)