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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid
[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid
[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <netinet/in.h>
64 #include <arpa/nameser.h>
71 #if defined(BSD) && (BSD >= 199103)
75 # include "../conf/portability.h"
78 static int ns_name_ntop
__P((const u_char
*, char *, size_t));
79 static int ns_name_pton
__P((const char *, u_char
*, size_t));
80 static int ns_name_unpack
__P((const u_char
*, const u_char
*,
81 const u_char
*, u_char
*, size_t));
82 static int ns_name_pack
__P((const u_char
*, u_char
*, int,
83 const u_char
**, const u_char
**));
84 static int ns_name_uncompress
__P((const u_char
*, const u_char
*,
85 const u_char
*, char *, size_t));
86 static int ns_name_compress
__P((const char *, u_char
*, size_t,
87 const u_char
**, const u_char
**));
88 static int ns_name_skip
__P((const u_char
**, const u_char
*));
91 * Expand compressed domain name 'comp_dn' to full domain name.
92 * 'msg' is a pointer to the begining of the message,
93 * 'eomorig' points to the first location after the message,
94 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
95 * Return size of compressed name or -1 if there was an error.
98 dn_expand(msg
, eom
, src
, dst
, dstsiz
)
105 int n
= ns_name_uncompress(msg
, eom
, src
, dst
, (size_t)dstsiz
);
107 if (n
> 0 && dst
[0] == '.')
113 * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
114 * Return the size of the compressed name or -1.
115 * 'length' is the size of the array pointed to by 'comp_dn'.
118 dn_comp(src
, dst
, dstsiz
, dnptrs
, lastdnptr
)
125 return (ns_name_compress(src
, dst
, (size_t)dstsiz
,
126 (const u_char
**)dnptrs
,
127 (const u_char
**)lastdnptr
));
131 * Skip over a compressed domain name. Return the size or -1.
134 __dn_skipname(ptr
, eom
)
138 const u_char
*saveptr
= ptr
;
140 if (ns_name_skip(&ptr
, eom
) == -1)
142 return (ptr
- saveptr
);
146 * Verify that a domain name uses an acceptable character set.
150 * Note the conspicuous absence of ctype macros in these definitions. On
151 * non-ASCII hosts, we can't depend on string literals or ctype macros to
152 * tell us anything about network-format data. The rest of the BIND system
153 * is not careful about this, but for some reason, we're doing it right here.
156 #define hyphenchar(c) ((c) == 0x2d)
157 #define underscorechar(c) ((c) == 0x5f)
158 #define bslashchar(c) ((c) == 0x5c)
159 #define periodchar(c) ((c) == PERIOD)
160 #define asterchar(c) ((c) == 0x2a)
161 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
162 || ((c) >= 0x61 && (c) <= 0x7a))
163 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
165 #define borderchar(c) (alphachar(c) || digitchar(c))
166 #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
167 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
173 int ppch
= '\0', pch
= PERIOD
, ch
= *dn
++;
178 if (periodchar(ch
)) {
180 } else if (periodchar(pch
)) {
183 } else if (periodchar(nch
) || nch
== '\0') {
190 ppch
= pch
, pch
= ch
, ch
= nch
;
196 * hostname-like (A, MX, WKS) owners can have "*" as their first label
197 * but must otherwise be as a host name.
203 if (asterchar(dn
[0])) {
204 if (periodchar(dn
[1]))
205 return (res_hnok(dn
+2));
209 return (res_hnok(dn
));
213 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
214 * label, but the rest of the name has to look like a host name.
222 /* "." is a valid missing representation */
226 /* otherwise <label>.<hostname> */
227 while ((ch
= *dn
++) != '\0') {
230 if (!escaped
&& periodchar(ch
))
234 else if (bslashchar(ch
))
238 return (res_hnok(dn
));
243 * This function is quite liberal, since RFC 1034's character sets are only
252 while ((ch
= *dn
++) != '\0')
259 * Routines to insert/extract short/long's.
264 register const u_char
*msgp
;
266 register u_int16_t u
;
274 * nExt machines have some funky library conventions, which we must maintain.
278 register const u_char
*msgp
;
280 return (_getshort(msgp
));
286 register const u_char
*msgp
;
288 register u_int32_t u
;
295 #if defined(__STDC__) || defined(__cplusplus)
296 __putshort(register u_int16_t s
, register u_char
*msgp
) /* must match proto */
299 register u_int16_t s
;
300 register u_char
*msgp
;
308 register u_int32_t l
;
309 register u_char
*msgp
;
314 /* ++ From BIND 8.1.1. ++ */
316 * Copyright (c) 1996 by Internet Software Consortium.
318 * Permission to use, copy, modify, and distribute this software for any
319 * purpose with or without fee is hereby granted, provided that the above
320 * copyright notice and this permission notice appear in all copies.
322 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
323 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
324 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
325 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
326 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
327 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
328 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
332 /*"Id: ns_name.c,v 1.1 1997/12/13 02:41:13 vixie Exp vixie"*/
334 /*#include "port_before.h"*/
336 /*#include <sys/types.h>*/
338 /*#include <netinet/in.h>*/
339 /*#include <arpa/nameser.h>*/
341 /*#include <errno.h>*/
342 /*#include <resolv.h>*/
343 /*#include <string.h>*/
345 /*#include "port_after.h"*/
347 #define NS_CMPRSFLGS 0xc0 /* Flag bits indicating name compression. */
348 #define NS_MAXCDNAME 255 /* maximum compressed domain name */
352 static char digits
[] = "0123456789";
356 static int special(int);
357 static int printable(int);
358 static int dn_find(const u_char
*, const u_char
*,
359 const u_char
* const *,
360 const u_char
* const *);
365 * ns_name_ntop(src, dst, dstsiz)
366 * Convert an encoded domain name to printable ascii as per RFC1035.
368 * Number of bytes written to buffer, or -1 (with errno set)
370 * The root is returned as "."
371 * All other domains are returned in non absolute form
374 ns_name_ntop(src
, dst
, dstsiz
)
388 while ((n
= *cp
++) != 0) {
389 if ((n
& NS_CMPRSFLGS
) != 0) {
390 /* Some kind of compression pointer. */
391 __set_errno (EMSGSIZE
);
396 __set_errno (EMSGSIZE
);
402 __set_errno (EMSGSIZE
);
405 for ((void)NULL
; n
> 0; n
--) {
409 __set_errno (EMSGSIZE
);
414 } else if (!printable(c
)) {
416 __set_errno (EMSGSIZE
);
420 *dn
++ = digits
[c
/ 100];
421 *dn
++ = digits
[(c
% 100) / 10];
422 *dn
++ = digits
[c
% 10];
425 __set_errno (EMSGSIZE
);
434 __set_errno (EMSGSIZE
);
440 __set_errno (EMSGSIZE
);
448 * ns_name_pton(src, dst, dstsiz)
449 * Convert a ascii string into an encoded domain name as per RFC1035.
452 * 1 if string was fully qualified
453 * 0 is string was not fully qualified
455 * Enforces label and domain length limits.
459 ns_name_pton(src
, dst
, dstsiz
)
464 u_char
*label
, *bp
, *eom
;
473 while ((c
= *src
++) != 0) {
475 if ((cp
= strchr(digits
, c
)) != NULL
) {
476 n
= (cp
- digits
) * 100;
477 if ((c
= *src
++) == 0 ||
478 (cp
= strchr(digits
, c
)) == NULL
) {
479 __set_errno (EMSGSIZE
);
482 n
+= (cp
- digits
) * 10;
483 if ((c
= *src
++) == 0 ||
484 (cp
= strchr(digits
, c
)) == NULL
) {
485 __set_errno (EMSGSIZE
);
490 __set_errno (EMSGSIZE
);
496 } else if (c
== '\\') {
499 } else if (c
== '.') {
500 c
= (bp
- label
- 1);
501 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
502 __set_errno (EMSGSIZE
);
506 __set_errno (EMSGSIZE
);
510 /* Fully qualified ? */
514 __set_errno (EMSGSIZE
);
519 if ((bp
- dst
) > MAXCDNAME
) {
520 __set_errno (EMSGSIZE
);
526 __set_errno (EMSGSIZE
);
533 __set_errno (EMSGSIZE
);
538 c
= (bp
- label
- 1);
539 if ((c
& NS_CMPRSFLGS
) != 0) { /* Label too big. */
540 __set_errno (EMSGSIZE
);
544 __set_errno (EMSGSIZE
);
550 __set_errno (EMSGSIZE
);
555 if ((bp
- dst
) > MAXCDNAME
) { /* src too big */
556 __set_errno (EMSGSIZE
);
563 * ns_name_unpack(msg, eom, src, dst, dstsiz)
564 * Unpack a domain name from a message, source may be compressed.
566 * -1 if it fails, or consumed octets if it succeeds.
569 ns_name_unpack(msg
, eom
, src
, dst
, dstsiz
)
576 const u_char
*srcp
, *dstlim
;
579 /* We don't want warnings! */
582 int n
, c
, len
, checked
;
589 dstlim
= dst
+ dstsiz
;
590 if (srcp
< msg
|| srcp
>= eom
) {
591 __set_errno (EMSGSIZE
);
594 /* Fetch next label in domain name. */
595 while ((n
= *srcp
++) != 0) {
596 /* Check for indirection. */
597 switch (n
& NS_CMPRSFLGS
) {
600 if (dstp
+ n
+ 1 >= dstlim
|| srcp
+ n
>= eom
) {
601 __set_errno (EMSGSIZE
);
606 memcpy(dstp
, srcp
, n
);
613 __set_errno (EMSGSIZE
);
617 len
= srcp
- src
+ 1;
618 srcp
= msg
+ (((n
& 0x3f) << 8) | (*srcp
& 0xff));
619 if (srcp
< msg
|| srcp
>= eom
) { /* Out of range. */
620 __set_errno (EMSGSIZE
);
625 * Check for loops in the compressed name;
626 * if we've looked at the whole message,
627 * there must be a loop.
629 if (checked
>= eom
- msg
) {
630 __set_errno (EMSGSIZE
);
636 __set_errno (EMSGSIZE
);
637 return (-1); /* flag error */
647 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
648 * Pack domain name 'domain' into 'comp_dn'.
650 * Size of the compressed name, or -1.
652 * 'dnptrs' is an array of pointers to previous compressed names.
653 * dnptrs[0] is a pointer to the beginning of the message. The array
655 * 'lastdnptr' is a pointer to the end of the array pointed to
658 * The list of pointers in dnptrs is updated for labels inserted into
659 * the message as we compress the name. If 'dnptr' is NULL, we don't
660 * try to compress names. If 'lastdnptr' is NULL, we don't update the
664 ns_name_pack(src
, dst
, dstsiz
, dnptrs
, lastdnptr
)
668 const u_char
**dnptrs
;
669 const u_char
**lastdnptr
;
672 const u_char
**cpp
, **lpp
, *eob
, *msg
;
680 if (dnptrs
!= NULL
) {
681 if ((msg
= *dnptrs
++) != NULL
) {
682 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
684 lpp
= cpp
; /* end of list to search */
689 /* make sure the domain we are about to add is legal */
693 if ((n
& NS_CMPRSFLGS
) != 0) {
694 __set_errno (EMSGSIZE
);
699 __set_errno (EMSGSIZE
);
707 /* Look to see if we can use pointers. */
709 if (n
!= 0 && msg
!= NULL
) {
710 l
= dn_find(srcp
, msg
, (const u_char
* const *)dnptrs
,
711 (const u_char
* const *)lpp
);
713 if (dstp
+ 1 >= eob
) {
714 __set_errno (EMSGSIZE
);
717 *dstp
++ = (l
>> 8) | NS_CMPRSFLGS
;
721 /* Not found, save it. */
722 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
- 1 &&
723 (dstp
- msg
) < 0x4000) {
728 /* copy label to buffer */
729 if (n
& NS_CMPRSFLGS
) { /* Should not happen. */
730 __set_errno (EMSGSIZE
);
733 if (dstp
+ 1 + n
>= eob
) {
734 __set_errno (EMSGSIZE
);
737 memcpy(dstp
, srcp
, n
+ 1);
745 __set_errno (EMSGSIZE
);
752 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
753 * Expand compressed domain name to presentation format.
755 * Number of bytes read out of `src', or -1 (with errno set).
757 * Root domain returns as "." not "".
760 ns_name_uncompress(msg
, eom
, src
, dst
, dstsiz
)
767 u_char tmp
[NS_MAXCDNAME
];
770 if ((n
= ns_name_unpack(msg
, eom
, src
, tmp
, sizeof tmp
)) == -1)
772 if (ns_name_ntop(tmp
, dst
, dstsiz
) == -1)
778 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
779 * Compress a domain name into wire format, using compression pointers.
781 * Number of bytes consumed in `dst' or -1 (with errno set).
783 * 'dnptrs' is an array of pointers to previous compressed names.
784 * dnptrs[0] is a pointer to the beginning of the message.
785 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
786 * array pointed to by 'dnptrs'. Side effect is to update the list of
787 * pointers for labels inserted into the message as we compress the name.
788 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
789 * is NULL, we don't update the list.
792 ns_name_compress(src
, dst
, dstsiz
, dnptrs
, lastdnptr
)
796 const u_char
**dnptrs
;
797 const u_char
**lastdnptr
;
799 u_char tmp
[NS_MAXCDNAME
];
801 if (ns_name_pton(src
, tmp
, sizeof tmp
) == -1)
803 return (ns_name_pack(tmp
, dst
, dstsiz
, dnptrs
, lastdnptr
));
807 * ns_name_skip(ptrptr, eom)
808 * Advance *ptrptr to skip over the compressed name it points at.
810 * 0 on success, -1 (with errno set) on failure.
813 ns_name_skip(ptrptr
, eom
)
814 const u_char
**ptrptr
;
821 while (cp
< eom
&& (n
= *cp
++) != 0) {
822 /* Check for indirection. */
823 switch (n
& NS_CMPRSFLGS
) {
824 case 0: /* normal case, n == len */
827 case NS_CMPRSFLGS
: /* indirection */
830 default: /* illegal type */
831 __set_errno (EMSGSIZE
);
837 __set_errno (EMSGSIZE
);
848 * Thinking in noninternationalized USASCII (per the DNS spec),
849 * is this characted special ("in need of quoting") ?
861 case 0x5C: /* '\\' */
862 /* Special modifiers in zone files. */
873 * Thinking in noninternationalized USASCII (per the DNS spec),
874 * is this character visible and not a space when printed ?
882 return (ch
> 0x20 && ch
< 0x7f);
886 * Thinking in noninternationalized USASCII (per the DNS spec),
887 * convert this character to lower case if it's upper case.
893 if (ch
>= 0x41 && ch
<= 0x5A)
899 * dn_find(domain, msg, dnptrs, lastdnptr)
900 * Search for the counted-label name in an array of compressed names.
902 * offset from msg if found, or -1.
904 * dnptrs is the pointer to the first name on the list,
905 * not the pointer to the start of the message.
908 dn_find(domain
, msg
, dnptrs
, lastdnptr
)
909 const u_char
*domain
;
911 const u_char
* const *dnptrs
;
912 const u_char
* const *lastdnptr
;
914 const u_char
*dn
, *cp
, *sp
;
915 const u_char
* const *cpp
;
918 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
921 while ((n
= *cp
++) != 0) {
923 * check for indirection
925 switch (n
& NS_CMPRSFLGS
) {
926 case 0: /* normal case, n == len */
929 for ((void)NULL
; n
> 0; n
--)
930 if (mklower(*dn
++) != mklower(*cp
++))
932 /* Is next root for both ? */
933 if (*dn
== '\0' && *cp
== '\0')
939 case NS_CMPRSFLGS
: /* indirection */
940 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
943 default: /* illegal type */
944 __set_errno (EMSGSIZE
);
950 __set_errno (ENOENT
);
954 /* -- From BIND 8.1.1. -- */