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>
70 #if defined(BSD) && (BSD >= 199103)
74 # include "../conf/portability.h"
77 static int dn_find
__P((u_char
*exp_dn
, u_char
*msg
,
78 u_char
**dnptrs
, u_char
**lastdnptr
));
81 * Expand compressed domain name 'comp_dn' to full domain name.
82 * 'msg' is a pointer to the begining of the message,
83 * 'eomorig' points to the first location after the message,
84 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
85 * Return size of compressed name or -1 if there was an error.
88 dn_expand(msg
, eomorig
, comp_dn
, exp_dn
, length
)
89 const u_char
*msg
, *eomorig
, *comp_dn
;
93 register const u_char
*cp
;
97 int len
= -1, checked
= 0, octets
= 0;
101 eom
= exp_dn
+ length
;
103 * fetch next label in domain name
107 * Check for indirection
109 switch (n
& INDIR_MASK
) {
112 if (octets
> MAXCDNAME
)
123 if (((c
= *cp
++) == '.') || (c
== '\\')) {
124 if (dn
+ n
+ 2 >= eom
)
129 if (cp
>= eomorig
) /* out of range */
136 len
= cp
- comp_dn
+ 1;
137 cp
= msg
+ (((n
& 0x3f) << 8) | (*cp
& 0xff));
138 if (cp
< msg
|| cp
>= eomorig
) /* out of range */
142 * Check for loops in the compressed name;
143 * if we've looked at the whole message,
144 * there must be a loop.
146 if (checked
>= eomorig
- msg
)
151 return (-1); /* flag error */
161 * Compress domain name 'exp_dn' into 'comp_dn'.
162 * Return the size of the compressed name or -1.
163 * 'length' is the size of the array pointed to by 'comp_dn'.
164 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
165 * is a pointer to the beginning of the message. The list ends with NULL.
166 * 'lastdnptr' is a pointer to the end of the array pointed to
167 * by 'dnptrs'. Side effect is to update the list of pointers for
168 * labels inserted into the message as we compress the name.
169 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
170 * is NULL, we don't update the list.
173 dn_comp(exp_dn
, comp_dn
, length
, dnptrs
, lastdnptr
)
175 u_char
*comp_dn
, **dnptrs
, **lastdnptr
;
178 register u_char
*cp
, *dn
;
180 u_char
**cpp
, **lpp
, *sp
, *eob
;
183 dn
= (u_char
*)exp_dn
;
185 if (length
> MAXCDNAME
)
189 if (dnptrs
!= NULL
) {
190 if ((msg
= *dnptrs
++) != NULL
) {
191 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
193 lpp
= cpp
; /* end of list to search */
197 for (c
= *dn
++; c
!= '\0'; ) {
198 /* look to see if we can use pointers */
200 if ((l
= dn_find(dn
-1, msg
, dnptrs
, lpp
)) >= 0) {
203 *cp
++ = (l
>> 8) | INDIR_MASK
;
205 return (cp
- comp_dn
);
207 /* not found, save it */
208 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
-1) {
213 sp
= cp
++; /* save ptr to length byte */
220 if ((c
= *dn
++) == '\0')
229 } while ((c
= *dn
++) != '\0');
230 /* catch trailing '.'s but not '..' */
231 if ((l
= cp
- sp
- 1) == 0 && c
== '\0') {
235 if (l
<= 0 || l
> MAXLABEL
) {
248 return (cp
- comp_dn
);
252 * Skip over a compressed domain name. Return the size or -1.
255 __dn_skipname(comp_dn
, eom
)
256 const u_char
*comp_dn
, *eom
;
258 register const u_char
*cp
;
262 while (cp
< eom
&& (n
= *cp
++)) {
264 * check for indirection
266 switch (n
& INDIR_MASK
) {
267 case 0: /* normal case, n == len */
270 case INDIR_MASK
: /* indirection */
273 default: /* illegal type */
280 return (cp
- comp_dn
);
287 if (isascii(ch
) && isupper(ch
))
288 return (tolower(ch
));
293 * Search for expanded name from a list of previously compressed names.
294 * Return the offset from msg if found or -1.
295 * dnptrs is the pointer to the first name on the list,
296 * not the pointer to the start of the message.
299 dn_find(exp_dn
, msg
, dnptrs
, lastdnptr
)
300 u_char
*exp_dn
, *msg
;
301 u_char
**dnptrs
, **lastdnptr
;
303 register u_char
*dn
, *cp
, **cpp
;
307 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
312 * check for indirection
314 switch (n
& INDIR_MASK
) {
315 case 0: /* normal case, n == len */
321 if (mklower(*dn
++) != mklower(*cp
++))
324 if ((n
= *dn
++) == '\0' && *cp
== '\0')
330 case INDIR_MASK
: /* indirection */
331 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
334 default: /* illegal type */
346 * Verify that a domain name uses an acceptable character set.
350 * Note the conspicuous absence of ctype macros in these definitions. On
351 * non-ASCII hosts, we can't depend on string literals or ctype macros to
352 * tell us anything about network-format data. The rest of the BIND system
353 * is not careful about this, but for some reason, we're doing it right here.
356 #define hyphenchar(c) ((c) == 0x2d)
357 #define bslashchar(c) ((c) == 0x5c)
358 #define periodchar(c) ((c) == PERIOD)
359 #define asterchar(c) ((c) == 0x2a)
360 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
361 || ((c) >= 0x61 && (c) <= 0x7a))
362 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
364 #define borderchar(c) (alphachar(c) || digitchar(c))
365 #define middlechar(c) (borderchar(c) || hyphenchar(c))
366 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
372 int ppch
= '\0', pch
= PERIOD
, ch
= *dn
++;
377 if (periodchar(ch
)) {
379 } else if (periodchar(pch
)) {
382 } else if (periodchar(nch
) || nch
== '\0') {
389 ppch
= pch
, pch
= ch
, ch
= nch
;
395 * hostname-like (A, MX, WKS) owners can have "*" as their first label
396 * but must otherwise be as a host name.
402 if (asterchar(dn
[0])) {
403 if (periodchar(dn
[1]))
404 return (res_hnok(dn
+2));
408 return (res_hnok(dn
));
412 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
413 * label, but the rest of the name has to look like a host name.
421 /* "." is a valid missing representation */
425 /* otherwise <label>.<hostname> */
426 while ((ch
= *dn
++) != '\0') {
429 if (!escaped
&& periodchar(ch
))
433 else if (bslashchar(ch
))
437 return (res_hnok(dn
));
442 * This function is quite liberal, since RFC 1034's character sets are only
451 while ((ch
= *dn
++) != '\0')
458 * Routines to insert/extract short/long's.
463 register const u_char
*msgp
;
465 register u_int16_t u
;
473 * nExt machines have some funky library conventions, which we must maintain.
477 register const u_char
*msgp
;
479 return (_getshort(msgp
));
485 register const u_char
*msgp
;
487 register u_int32_t u
;
494 #if defined(__STDC__) || defined(__cplusplus)
495 __putshort(register u_int16_t s
, register u_char
*msgp
) /* must match proto */
498 register u_int16_t s
;
499 register u_char
*msgp
;
507 register u_int32_t l
;
508 register u_char
*msgp
;