]>
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 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * ++Copyright++ 1985, 1993
27 * Copyright (c) 1985, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
60 * Permission to use, copy, modify, and distribute this software for any
61 * purpose with or without fee is hereby granted, provided that the above
62 * copyright notice and this permission notice appear in all copies, and that
63 * the name of Digital Equipment Corporation not be used in advertising or
64 * publicity pertaining to distribution of the document or software without
65 * specific, written prior permission.
67 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
68 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
70 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
71 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
72 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
73 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
79 #if defined(LIBC_SCCS) && !defined(lint)
80 static char sccsid
[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
81 static char rcsid
[] = "$Id: res_comp.c,v 1.4 2003/02/18 17:29:24 majka Exp $";
82 #endif /* LIBC_SCCS and not lint */
84 #include <sys/param.h>
85 #include <netinet/in.h>
86 #include <arpa/nameser8_compat.h>
89 #include <resolv8_compat.h>
92 #if defined(BSD) && (BSD >= 199103)
96 # include "portability.h"
99 static int dn_find
__P((u_char
*exp_dn
, u_char
*msg
,
100 u_char
**dnptrs
, u_char
**lastdnptr
));
103 * Expand compressed domain name 'comp_dn' to full domain name.
104 * 'msg' is a pointer to the begining of the message,
105 * 'eomorig' points to the first location after the message,
106 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
107 * Return size of compressed name or -1 if there was an error.
110 dn_expand(msg
, eomorig
, comp_dn
, exp_dn
, length
)
111 const u_char
*msg
, *eomorig
, *comp_dn
;
115 register const u_char
*cp
;
119 int len
= -1, checked
= 0;
123 eom
= exp_dn
+ length
;
125 * fetch next label in domain name
127 while ((n
= *cp
++)) {
129 * Check for indirection
131 switch (n
& INDIR_MASK
) {
142 if ((c
= *cp
++) == '.') {
143 if (dn
+ n
+ 2 >= eom
)
148 if (cp
>= eomorig
) /* out of range */
155 len
= cp
- comp_dn
+ 1;
156 cp
= msg
+ (((n
& 0x3f) << 8) | (*cp
& 0xff));
157 if (cp
< msg
|| cp
>= eomorig
) /* out of range */
161 * Check for loops in the compressed name;
162 * if we've looked at the whole message,
163 * there must be a loop.
165 if (checked
>= eomorig
- msg
)
170 return (-1); /* flag error */
174 for (dn
= exp_dn
; (c
= *dn
) != '\0'; dn
++)
175 if (isascii(c
) && isspace(c
))
183 * Compress domain name 'exp_dn' into 'comp_dn'.
184 * Return the size of the compressed name or -1.
185 * 'length' is the size of the array pointed to by 'comp_dn'.
186 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
187 * is a pointer to the beginning of the message. The list ends with NULL.
188 * 'lastdnptr' is a pointer to the end of the arrary pointed to
189 * by 'dnptrs'. Side effect is to update the list of pointers for
190 * labels inserted into the message as we compress the name.
191 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
192 * is NULL, we don't update the list.
195 dn_comp(exp_dn
, comp_dn
, length
, dnptrs
, lastdnptr
)
197 u_char
*comp_dn
, **dnptrs
, **lastdnptr
;
200 register u_char
*cp
, *dn
;
202 u_char
**cpp
, **lpp
, *sp
, *eob
;
205 dn
= (u_char
*)exp_dn
;
209 if (dnptrs
!= NULL
) {
210 if ((msg
= *dnptrs
++) != NULL
) {
211 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
213 lpp
= cpp
; /* end of list to search */
217 for (c
= *dn
++; c
!= '\0'; ) {
218 /* look to see if we can use pointers */
220 if ((l
= dn_find(dn
-1, msg
, dnptrs
, lpp
)) >= 0) {
223 *cp
++ = (l
>> 8) | INDIR_MASK
;
225 return (cp
- comp_dn
);
227 /* not found, save it */
228 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
-1) {
233 sp
= cp
++; /* save ptr to length byte */
240 if ((c
= *dn
++) == '\0')
249 } while ((c
= *dn
++) != '\0');
250 /* catch trailing '.'s but not '..' */
251 if ((l
= cp
- sp
- 1) == 0 && c
== '\0') {
255 if (l
<= 0 || l
> MAXLABEL
) {
268 return (cp
- comp_dn
);
272 * Skip over a compressed domain name. Return the size or -1.
275 __dn_skipname(comp_dn
, eom
)
276 const u_char
*comp_dn
, *eom
;
278 register const u_char
*cp
;
282 while (cp
< eom
&& (n
= *cp
++)) {
284 * check for indirection
286 switch (n
& INDIR_MASK
) {
287 case 0: /* normal case, n == len */
290 case INDIR_MASK
: /* indirection */
293 default: /* illegal type */
300 return (cp
- comp_dn
);
307 if (isascii(ch
) && isupper(ch
))
308 return (tolower(ch
));
313 * Search for expanded name from a list of previously compressed names.
314 * Return the offset from msg if found or -1.
315 * dnptrs is the pointer to the first name on the list,
316 * not the pointer to the start of the message.
319 dn_find(exp_dn
, msg
, dnptrs
, lastdnptr
)
320 u_char
*exp_dn
, *msg
;
321 u_char
**dnptrs
, **lastdnptr
;
323 register u_char
*dn
, *cp
, **cpp
;
327 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
330 while ((n
= *cp
++)) {
332 * check for indirection
334 switch (n
& INDIR_MASK
) {
335 case 0: /* normal case, n == len */
341 if (mklower(*dn
++) != mklower(*cp
++))
344 if ((n
= *dn
++) == '\0' && *cp
== '\0')
350 case INDIR_MASK
: /* indirection */
351 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
354 default: /* illegal type */
366 * Routines to insert/extract short/long's.
371 register const u_char
*msgp
;
373 register u_int16_t u
;
379 #if defined(__APPLE__)
381 * nExt machines have some funky library conventions, which we must maintain.
385 register const u_char
*msgp
;
387 return (_getshort(msgp
));
393 register const u_char
*msgp
;
395 register u_int32_t u
;
402 #if defined(__STDC__) || defined(__cplusplus)
403 __putshort(register u_int16_t s
, register u_char
*msgp
) /* must match proto */
406 register u_int16_t s
;
407 register u_char
*msgp
;
415 register u_int32_t l
;
416 register u_char
*msgp
;
422 /* ultrix 4.0 had some icky packaging in its libc.a. alias for it here.
423 * there is more gunk of this kind over in res_debug.c.
427 #if defined(__STDC__) || defined(__cplusplus)
428 putshort(register u_short s
, register u_char
*msgp
)
432 register u_char
*msgp
;
440 register u_int32_t l
;
441 register u_char
*msgp
;
447 dn_skipname(comp_dn
, eom
)
448 const u_char
*comp_dn
, *eom
;
450 return (__dn_skipname(comp_dn
, eom
));
452 #endif /* Ultrix 4.0 hackery */