2 * ++Copyright++ 1985, 1993
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies, and that
36 * the name of Digital Equipment Corporation not be used in advertising or
37 * publicity pertaining to distribution of the document or software without
38 * specific, written prior permission.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
43 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char sccsid
[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
54 static char rcsid
[] = "$Id$";
55 #endif /* LIBC_SCCS and not lint */
57 #include <sys/types.h>
58 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/nameser.h>
67 #if defined(BSD) && (BSD >= 199103)
71 # include "../conf/portability.h"
74 extern int __ns_name_ntop
__P((const u_char
*, char *, size_t));
75 static int ns_name_pton
__P((const char *, u_char
*, size_t));
76 extern int __ns_name_unpack
__P((const u_char
*, const u_char
*,
77 const u_char
*, u_char
*, size_t));
78 static int ns_name_pack
__P((const u_char
*, u_char
*, int,
79 const u_char
**, const u_char
**));
80 static int ns_name_uncompress
__P((const u_char
*, const u_char
*,
81 const u_char
*, char *, size_t));
82 static int ns_name_compress
__P((const char *, u_char
*, size_t,
83 const u_char
**, const u_char
**));
84 static int ns_name_skip
__P((const u_char
**, const u_char
*));
87 * Expand compressed domain name 'comp_dn' to full domain name.
88 * 'msg' is a pointer to the begining of the message,
89 * 'eomorig' points to the first location after the message,
90 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
91 * Return size of compressed name or -1 if there was an error.
94 dn_expand(msg
, eom
, src
, dst
, dstsiz
)
101 int n
= ns_name_uncompress(msg
, eom
, src
, dst
, (size_t)dstsiz
);
103 if (n
> 0 && dst
[0] == '.')
109 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
110 * Return the size of the compressed name or -1.
111 * 'length' is the size of the array pointed to by 'comp_dn'.
114 dn_comp(src
, dst
, dstsiz
, dnptrs
, lastdnptr
)
121 return (ns_name_compress(src
, dst
, (size_t)dstsiz
,
122 (const u_char
**)dnptrs
,
123 (const u_char
**)lastdnptr
));
127 * Skip over a compressed domain name. Return the size or -1.
130 __dn_skipname(ptr
, eom
)
134 const u_char
*saveptr
= ptr
;
136 if (ns_name_skip(&ptr
, eom
) == -1)
138 return (ptr
- saveptr
);
142 * Verify that a domain name uses an acceptable character set.
146 * Note the conspicuous absence of ctype macros in these definitions. On
147 * non-ASCII hosts, we can't depend on string literals or ctype macros to
148 * tell us anything about network-format data. The rest of the BIND system
149 * is not careful about this, but for some reason, we're doing it right here.
152 #define hyphenchar(c) ((c) == 0x2d)
153 #define underscorechar(c) ((c) == 0x5f)
154 #define bslashchar(c) ((c) == 0x5c)
155 #define periodchar(c) ((c) == PERIOD)
156 #define asterchar(c) ((c) == 0x2a)
157 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
158 || ((c) >= 0x61 && (c) <= 0x7a))
159 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
161 #define borderchar(c) (alphachar(c) || digitchar(c))
162 #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
163 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
169 int ppch
= '\0', pch
= PERIOD
, ch
= *dn
++;
174 if (periodchar(ch
)) {
176 } else if (periodchar(pch
)) {
179 } else if (periodchar(nch
) || nch
== '\0') {
186 ppch
= pch
, pch
= ch
, ch
= nch
;
192 * hostname-like (A, MX, WKS) owners can have "*" as their first label
193 * but must otherwise be as a host name.
199 if (asterchar(dn
[0])) {
200 if (periodchar(dn
[1]))
201 return (res_hnok(dn
+2));
205 return (res_hnok(dn
));
209 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
210 * label, but the rest of the name has to look like a host name.
218 /* "." is a valid missing representation */
222 /* otherwise <label>.<hostname> */
223 while ((ch
= *dn
++) != '\0') {
226 if (!escaped
&& periodchar(ch
))
230 else if (bslashchar(ch
))
234 return (res_hnok(dn
));
239 * This function is quite liberal, since RFC 1034's character sets are only
248 while ((ch
= *dn
++) != '\0')
255 * Routines to insert/extract short/long's.
260 register const u_char
*msgp
;
262 register u_int16_t u
;
270 * nExt machines have some funky library conventions, which we must maintain.
274 register const u_char
*msgp
;
276 return (_getshort(msgp
));
282 register const u_char
*msgp
;
284 register u_int32_t u
;
291 #if defined(__STDC__) || defined(__cplusplus)
292 __putshort(register u_int16_t s
, register u_char
*msgp
) /* must match proto */
295 register u_int16_t s
;
296 register u_char
*msgp
;
304 register u_int32_t l
;
305 register u_char
*msgp
;
310 /* ++ From BIND 8.1.1. ++ */
312 * Copyright (c) 1996 by Internet Software Consortium.
314 * Permission to use, copy, modify, and distribute this software for any
315 * purpose with or without fee is hereby granted, provided that the above
316 * copyright notice and this permission notice appear in all copies.
318 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
319 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
320 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
321 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
322 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
323 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
324 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
328 /*"Id: ns_name.c,v 1.1 1997/12/13 02:41:13 vixie Exp vixie"*/
330 /*#include "port_before.h"*/
332 /*#include <sys/types.h>*/
334 /*#include <netinet/in.h>*/
335 /*#include <arpa/nameser.h>*/
337 /*#include <errno.h>*/
338 /*#include <resolv.h>*/
339 /*#include <string.h>*/
341 /*#include "port_after.h"*/
343 #define NS_CMPRSFLGS 0xc0 /* Flag bits indicating name compression. */
344 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
348 static const char digits
[] = "0123456789";
352 static int special(int);
353 static int printable(int);
354 static int dn_find(const u_char
*, const u_char
*,
355 const u_char
* const *,
356 const u_char
* const *);
361 * ns_name_ntop(src, dst, dstsiz)
362 * Convert an encoded domain name to printable ascii as per RFC1035.
364 * Number of bytes written to buffer, or -1 (with errno set)
366 * The root is returned as "."
367 * All other domains are returned in non absolute form
370 __ns_name_ntop(src
, dst
, dstsiz
)
384 while ((n
= *cp
++) != 0) {
385 if ((n
& NS_CMPRSFLGS
) != 0) {
386 /* Some kind of compression pointer. */
387 __set_errno (EINVAL
);
392 __set_errno (EMSGSIZE
);
398 __set_errno (EMSGSIZE
);
401 for ((void)NULL
; n
> 0; n
--) {
405 __set_errno (EMSGSIZE
);
410 } else if (!printable(c
)) {
412 __set_errno (EMSGSIZE
);
416 *dn
++ = digits
[c
/ 100];
417 *dn
++ = digits
[(c
% 100) / 10];
418 *dn
++ = digits
[c
% 10];
421 __set_errno (EMSGSIZE
);
430 __set_errno (EMSGSIZE
);
436 __set_errno (EMSGSIZE
);
444 * ns_name_pton(src, dst, dstsiz)
445 * Convert a ascii string into an encoded domain name as per RFC1035.
448 * 1 if string was fully qualified
449 * 0 is string was not fully qualified
451 * Enforces label and domain length limits.
455 ns_name_pton(src
, dst
, dstsiz
)
460 u_char
*label
, *bp
, *eom
;
469 while ((c
= *src
++) != 0) {
471 if ((cp
= strchr(digits
, c
)) != NULL
) {
472 n
= (cp
- digits
) * 100;
473 if ((c
= *src
++) == 0 ||
474 (cp
= strchr(digits
, c
)) == NULL
) {
475 __set_errno (EMSGSIZE
);
478 n
+= (cp
- digits
) * 10;
479 if ((c
= *src
++) == 0 ||
480 (cp
= strchr(digits
, c
)) == NULL
) {
481 __set_errno (EMSGSIZE
);
486 __set_errno (EMSGSIZE
);
492 } else if (c
== '\\') {
495 } else if (c
== '.') {
496 c
= (bp
- label
- 1);
497 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
498 __set_errno (EMSGSIZE
);
502 __set_errno (EMSGSIZE
);
506 /* Fully qualified ? */
510 __set_errno (EMSGSIZE
);
515 if ((bp
- dst
) > MAXCDNAME
) {
516 __set_errno (EMSGSIZE
);
522 __set_errno (EMSGSIZE
);
529 __set_errno (EMSGSIZE
);
534 c
= (bp
- label
- 1);
535 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
536 __set_errno (EMSGSIZE
);
540 __set_errno (EMSGSIZE
);
546 __set_errno (EMSGSIZE
);
551 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
552 __set_errno (EMSGSIZE
);
559 * ns_name_unpack(msg, eom, src, dst, dstsiz)
560 * Unpack a domain name from a message, source may be compressed.
562 * -1 if it fails, or consumed octets if it succeeds.
565 __ns_name_unpack(msg
, eom
, src
, dst
, dstsiz
)
572 const u_char
*srcp
, *dstlim
;
575 /* We don't want warnings! */
578 int n
, c
, len
, checked
;
585 dstlim
= dst
+ dstsiz
;
586 if (srcp
< msg
|| srcp
>= eom
) {
587 __set_errno (EMSGSIZE
);
590 /* Fetch next label in domain name. */
591 while ((n
= *srcp
++) != 0) {
592 /* Check for indirection. */
593 switch (n
& NS_CMPRSFLGS
) {
596 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
597 __set_errno (EMSGSIZE
);
602 memcpy(dstp
, srcp
, n
);
609 __set_errno (EMSGSIZE
);
613 len
= srcp
- src
+ 1;
614 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
615 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
616 __set_errno (EMSGSIZE
);
621 * Check for loops in the compressed name;
622 * if we've looked at the whole message,
623 * there must be a loop.
625 if (checked
>= eom
- msg
) {
626 __set_errno (EMSGSIZE
);
632 __set_errno (EMSGSIZE
);
633 return (-1); /* flag error */
643 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
644 * Pack domain name 'domain' into 'comp_dn'.
646 * Size of the compressed name, or -1.
648 * 'dnptrs' is an array of pointers to previous compressed names.
649 * dnptrs[0] is a pointer to the beginning of the message. The array
651 * 'lastdnptr' is a pointer to the end of the array pointed to
654 * The list of pointers in dnptrs is updated for labels inserted into
655 * the message as we compress the name. If 'dnptr' is NULL, we don't
656 * try to compress names. If 'lastdnptr' is NULL, we don't update the
660 ns_name_pack(src
, dst
, dstsiz
, dnptrs
, lastdnptr
)
664 const u_char
**dnptrs
;
665 const u_char
**lastdnptr
;
668 const u_char
**cpp
, **lpp
, *eob
, *msg
;
676 if (dnptrs
!= NULL
) {
677 if ((msg
= *dnptrs
++) != NULL
) {
678 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
680 lpp
= cpp
; /* end of list to search */
685 /* make sure the domain we are about to add is legal */
689 if ((n
& NS_CMPRSFLGS
) != 0) {
690 __set_errno (EMSGSIZE
);
695 __set_errno (EMSGSIZE
);
703 /* Look to see if we can use pointers. */
705 if (n
!= 0 && msg
!= NULL
) {
706 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
707 (const u_char
* const *)lpp
);
709 if (dstp
+ 1 >= eob
) {
710 __set_errno (EMSGSIZE
);
713 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
717 /* Not found, save it. */
718 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
719 (dstp
- msg
) < 0x4000) {
724 /* copy label to buffer */
725 if (n
& NS_CMPRSFLGS
) { /* Should not happen. */
726 __set_errno (EMSGSIZE
);
729 if (dstp
+ 1 + n
>= eob
) {
730 __set_errno (EMSGSIZE
);
733 memcpy(dstp
, srcp
, n
+ 1);
741 __set_errno (EMSGSIZE
);
748 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
749 * Expand compressed domain name to presentation format.
751 * Number of bytes read out of `src', or -1 (with errno set).
753 * Root domain returns as "." not "".
756 ns_name_uncompress(msg
, eom
, src
, dst
, dstsiz
)
763 u_char tmp
[NS_MAXCDNAME
];
766 if ((n
= __ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
768 if (__ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
774 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
775 * Compress a domain name into wire format, using compression pointers.
777 * Number of bytes consumed in `dst' or -1 (with errno set).
779 * 'dnptrs' is an array of pointers to previous compressed names.
780 * dnptrs[0] is a pointer to the beginning of the message.
781 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
782 * array pointed to by 'dnptrs'. Side effect is to update the list of
783 * pointers for labels inserted into the message as we compress the name.
784 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
785 * is NULL, we don't update the list.
788 ns_name_compress(src
, dst
, dstsiz
, dnptrs
, lastdnptr
)
792 const u_char
**dnptrs
;
793 const u_char
**lastdnptr
;
795 u_char tmp
[NS_MAXCDNAME
];
797 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
799 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
803 * ns_name_skip(ptrptr, eom)
804 * Advance *ptrptr to skip over the compressed name it points at.
806 * 0 on success, -1 (with errno set) on failure.
809 ns_name_skip(ptrptr
, eom
)
810 const u_char
**ptrptr
;
817 while (cp
< eom
&& (n
= *cp
++) != 0) {
818 /* Check for indirection. */
819 switch (n
& NS_CMPRSFLGS
) {
820 case 0: /* normal case, n == len */
823 case NS_CMPRSFLGS
: /* indirection */
826 default: /* illegal type */
827 __set_errno (EMSGSIZE
);
833 __set_errno (EMSGSIZE
);
844 * Thinking in noninternationalized USASCII (per the DNS spec),
845 * is this characted special ("in need of quoting") ?
857 case 0x5C: /* '\\' */
858 /* Special modifiers in zone files. */
869 * Thinking in noninternationalized USASCII (per the DNS spec),
870 * is this character visible and not a space when printed ?
878 return (ch
> 0x20 && ch
< 0x7f);
882 * Thinking in noninternationalized USASCII (per the DNS spec),
883 * convert this character to lower case if it's upper case.
889 if (ch
>= 0x41 && ch
<= 0x5A)
895 * dn_find(domain, msg, dnptrs, lastdnptr)
896 * Search for the counted-label name in an array of compressed names.
898 * offset from msg if found, or -1.
900 * dnptrs is the pointer to the first name on the list,
901 * not the pointer to the start of the message.
904 dn_find(domain
, msg
, dnptrs
, lastdnptr
)
905 const u_char
*domain
;
907 const u_char
* const *dnptrs
;
908 const u_char
* const *lastdnptr
;
910 const u_char
*dn
, *cp
, *sp
;
911 const u_char
* const *cpp
;
914 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
917 while ((n
= *cp
++) != 0) {
919 * check for indirection
921 switch (n
& NS_CMPRSFLGS
) {
922 case 0: /* normal case, n == len */
925 for ((void)NULL
; n
> 0; n
--)
926 if (mklower(*dn
++) != mklower(*cp
++))
928 /* Is next root for both ? */
929 if (*dn
== '\0' && *cp
== '\0')
935 case NS_CMPRSFLGS
: /* indirection */
936 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
939 default: /* illegal type */
940 __set_errno (EMSGSIZE
);
946 __set_errno (ENOENT
);
950 /* -- From BIND 8.1.1. -- */