]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_comp.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * ++Copyright++ 1985, 1993
28 * Copyright (c) 1985, 1993
29 * The Regents of the University of California. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
61 * Permission to use, copy, modify, and distribute this software for any
62 * purpose with or without fee is hereby granted, provided that the above
63 * copyright notice and this permission notice appear in all copies, and that
64 * the name of Digital Equipment Corporation not be used in advertising or
65 * publicity pertaining to distribution of the document or software without
66 * specific, written prior permission.
68 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
69 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
70 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
71 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
72 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
73 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
74 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
80 #if defined(LIBC_SCCS) && !defined(lint)
81 static char sccsid
[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
82 static char rcsid
[] = "$Id: res_comp.c,v 1.4 2003/02/18 17:29:24 majka Exp $";
83 #endif /* LIBC_SCCS and not lint */
85 #include <sys/param.h>
86 #include <netinet/in.h>
87 #include <arpa/nameser8_compat.h>
90 #include <resolv8_compat.h>
93 #if defined(BSD) && (BSD >= 199103)
97 # include "portability.h"
100 static int dn_find
__P((u_char
*exp_dn
, u_char
*msg
,
101 u_char
**dnptrs
, u_char
**lastdnptr
));
104 * Expand compressed domain name 'comp_dn' to full domain name.
105 * 'msg' is a pointer to the begining of the message,
106 * 'eomorig' points to the first location after the message,
107 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
108 * Return size of compressed name or -1 if there was an error.
111 dn_expand(msg
, eomorig
, comp_dn
, exp_dn
, length
)
112 const u_char
*msg
, *eomorig
, *comp_dn
;
116 register const u_char
*cp
;
120 int len
= -1, checked
= 0;
124 eom
= exp_dn
+ length
;
126 * fetch next label in domain name
128 while ((n
= *cp
++)) {
130 * Check for indirection
132 switch (n
& INDIR_MASK
) {
143 if ((c
= *cp
++) == '.') {
144 if (dn
+ n
+ 2 >= eom
)
149 if (cp
>= eomorig
) /* out of range */
156 len
= cp
- comp_dn
+ 1;
157 cp
= msg
+ (((n
& 0x3f) << 8) | (*cp
& 0xff));
158 if (cp
< msg
|| cp
>= eomorig
) /* out of range */
162 * Check for loops in the compressed name;
163 * if we've looked at the whole message,
164 * there must be a loop.
166 if (checked
>= eomorig
- msg
)
171 return (-1); /* flag error */
175 for (dn
= exp_dn
; (c
= *dn
) != '\0'; dn
++)
176 if (isascii(c
) && isspace(c
))
184 * Compress domain name 'exp_dn' into 'comp_dn'.
185 * Return the size of the compressed name or -1.
186 * 'length' is the size of the array pointed to by 'comp_dn'.
187 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
188 * is a pointer to the beginning of the message. The list ends with NULL.
189 * 'lastdnptr' is a pointer to the end of the arrary pointed to
190 * by 'dnptrs'. Side effect is to update the list of pointers for
191 * labels inserted into the message as we compress the name.
192 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
193 * is NULL, we don't update the list.
196 dn_comp(exp_dn
, comp_dn
, length
, dnptrs
, lastdnptr
)
198 u_char
*comp_dn
, **dnptrs
, **lastdnptr
;
201 register u_char
*cp
, *dn
;
203 u_char
**cpp
, **lpp
, *sp
, *eob
;
206 dn
= (u_char
*)exp_dn
;
210 if (dnptrs
!= NULL
) {
211 if ((msg
= *dnptrs
++) != NULL
) {
212 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
214 lpp
= cpp
; /* end of list to search */
218 for (c
= *dn
++; c
!= '\0'; ) {
219 /* look to see if we can use pointers */
221 if ((l
= dn_find(dn
-1, msg
, dnptrs
, lpp
)) >= 0) {
224 *cp
++ = (l
>> 8) | INDIR_MASK
;
226 return (cp
- comp_dn
);
228 /* not found, save it */
229 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
-1) {
234 sp
= cp
++; /* save ptr to length byte */
241 if ((c
= *dn
++) == '\0')
250 } while ((c
= *dn
++) != '\0');
251 /* catch trailing '.'s but not '..' */
252 if ((l
= cp
- sp
- 1) == 0 && c
== '\0') {
256 if (l
<= 0 || l
> MAXLABEL
) {
269 return (cp
- comp_dn
);
273 * Skip over a compressed domain name. Return the size or -1.
276 __dn_skipname(comp_dn
, eom
)
277 const u_char
*comp_dn
, *eom
;
279 register const u_char
*cp
;
283 while (cp
< eom
&& (n
= *cp
++)) {
285 * check for indirection
287 switch (n
& INDIR_MASK
) {
288 case 0: /* normal case, n == len */
291 case INDIR_MASK
: /* indirection */
294 default: /* illegal type */
301 return (cp
- comp_dn
);
308 if (isascii(ch
) && isupper(ch
))
309 return (tolower(ch
));
314 * Search for expanded name from a list of previously compressed names.
315 * Return the offset from msg if found or -1.
316 * dnptrs is the pointer to the first name on the list,
317 * not the pointer to the start of the message.
320 dn_find(exp_dn
, msg
, dnptrs
, lastdnptr
)
321 u_char
*exp_dn
, *msg
;
322 u_char
**dnptrs
, **lastdnptr
;
324 register u_char
*dn
, *cp
, **cpp
;
328 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
331 while ((n
= *cp
++)) {
333 * check for indirection
335 switch (n
& INDIR_MASK
) {
336 case 0: /* normal case, n == len */
342 if (mklower(*dn
++) != mklower(*cp
++))
345 if ((n
= *dn
++) == '\0' && *cp
== '\0')
351 case INDIR_MASK
: /* indirection */
352 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
355 default: /* illegal type */
367 * Routines to insert/extract short/long's.
372 register const u_char
*msgp
;
374 register u_int16_t u
;
380 #if defined(__APPLE__)
382 * nExt machines have some funky library conventions, which we must maintain.
386 register const u_char
*msgp
;
388 return (_getshort(msgp
));
394 register const u_char
*msgp
;
396 register u_int32_t u
;
403 #if defined(__STDC__) || defined(__cplusplus)
404 __putshort(register u_int16_t s
, register u_char
*msgp
) /* must match proto */
407 register u_int16_t s
;
408 register u_char
*msgp
;
416 register u_int32_t l
;
417 register u_char
*msgp
;
423 /* ultrix 4.0 had some icky packaging in its libc.a. alias for it here.
424 * there is more gunk of this kind over in res_debug.c.
428 #if defined(__STDC__) || defined(__cplusplus)
429 putshort(register u_short s
, register u_char
*msgp
)
433 register u_char
*msgp
;
441 register u_int32_t l
;
442 register u_char
*msgp
;
448 dn_skipname(comp_dn
, eom
)
449 const u_char
*comp_dn
, *eom
;
451 return (__dn_skipname(comp_dn
, eom
));
453 #endif /* Ultrix 4.0 hackery */