2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
5 * Copyright (C) 2005 Neil Cafferkey
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * Copyright (c) 1985 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid
[] = "@(#)res_comp.c 6.22 (Berkeley) 3/19/91";
58 #endif /* LIBC_SCCS and not lint */
62 #include <sys/param.h>
63 #include <arpa/nameser.h>
64 #include <netinet/in.h>
66 #include <exec/semaphores.h>
68 #include <kern/amiga_netdb.h>
69 #include <api/resolv.h>
71 static int dn_find(u_char
*exp_dn
, u_char
*msg
,
72 u_char
**dnptrs
, u_char
**lastdnptr
);
75 * Expand compressed domain name 'comp_dn' to full domain name.
76 * 'msg' is a pointer to the begining of the message,
77 * 'eomorig' points to the first location after the message,
78 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
79 * Return size of compressed name or -1 if there was an error.
82 dn_expand(const u_char
*msg
, const u_char
*eomorig
, const u_char
*comp_dn
,
83 u_char
*exp_dn
, int length
)
85 register u_char
*cp
, *dn
;
88 int len
= -1, checked
= 0;
91 cp
= (u_char
*)comp_dn
;
92 eom
= exp_dn
+ length
;
94 * fetch next label in domain name
98 * Check for indirection
100 switch (n
& INDIR_MASK
) {
111 if ((c
= *cp
++) == '.') {
112 if (dn
+ n
+ 2 >= eom
)
117 if (cp
>= eomorig
) /* out of range */
124 len
= cp
- comp_dn
+ 1;
125 cp
= (u_char
*)msg
+ (((n
& 0x3f) << 8) | (*cp
& 0xff));
126 if (cp
< msg
|| cp
>= eomorig
) /* out of range */
130 * Check for loops in the compressed name;
131 * if we've looked at the whole message,
132 * there must be a loop.
134 if (checked
>= eomorig
- msg
)
139 return (-1); /* flag error */
149 * Compress domain name 'exp_dn' into 'comp_dn'.
150 * Return the size of the compressed name or -1.
151 * 'length' is the size of the array pointed to by 'comp_dn'.
152 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
153 * is a pointer to the beginning of the message. The list ends with NULL.
154 * 'lastdnptr' is a pointer to the end of the arrary pointed to
155 * by 'dnptrs'. Side effect is to update the list of pointers for
156 * labels inserted into the message as we compress the name.
157 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
158 * is NULL, we don't update the list.
161 dn_comp(const u_char
*exp_dn
, u_char
*comp_dn
, int length
,
162 u_char
**dnptrs
, u_char
**lastdnptr
)
164 register u_char
*cp
, *dn
;
166 u_char
**cpp
, **lpp
, *sp
, *eob
;
169 dn
= (u_char
*)exp_dn
;
172 if (dnptrs
!= NULL
) {
173 if ((msg
= *dnptrs
++) != NULL
) {
174 for (cpp
= dnptrs
; *cpp
!= NULL
; cpp
++)
176 lpp
= cpp
; /* end of list to search */
180 for (c
= *dn
++; c
!= '\0'; ) {
181 /* look to see if we can use pointers */
183 if ((l
= dn_find(dn
-1, msg
, dnptrs
, lpp
)) >= 0) {
186 *cp
++ = (l
>> 8) | INDIR_MASK
;
188 return (cp
- comp_dn
);
190 /* not found, save it */
191 if (lastdnptr
!= NULL
&& cpp
< lastdnptr
-1) {
196 sp
= cp
++; /* save ptr to length byte */
203 if ((c
= *dn
++) == '\0')
212 } while ((c
= *dn
++) != '\0');
213 /* catch trailing '.'s but not '..' */
214 if ((l
= cp
- sp
- 1) == 0 && c
== '\0') {
218 if (l
<= 0 || l
> MAXLABEL
) {
231 return (cp
- comp_dn
);
235 * Skip over a compressed domain name. Return the size or -1.
238 __dn_skipname(const u_char
*comp_dn
, const u_char
*eom
)
243 cp
= (u_char
*)comp_dn
;
244 while (cp
< eom
&& (n
= *cp
++)) {
246 * check for indirection
248 switch (n
& INDIR_MASK
) {
249 case 0: /* normal case, n == len */
252 default: /* illegal type */
254 case INDIR_MASK
: /* indirection */
259 return (cp
- comp_dn
);
263 * Search for expanded name from a list of previously compressed names.
264 * Return the offset from msg if found or -1.
265 * dnptrs is the pointer to the first name on the list,
266 * not the pointer to the start of the message.
269 dn_find(u_char
*exp_dn
, u_char
*msg
,
270 u_char
**dnptrs
, u_char
**lastdnptr
)
272 register u_char
*dn
, *cp
, **cpp
;
276 for (cpp
= dnptrs
; cpp
< lastdnptr
; cpp
++) {
281 * check for indirection
283 switch (n
& INDIR_MASK
) {
284 case 0: /* normal case, n == len */
293 if ((n
= *dn
++) == '\0' && *cp
== '\0')
299 default: /* illegal type */
302 case INDIR_MASK
: /* indirection */
303 cp
= msg
+ (((n
& 0x3f) << 8) | *cp
);
314 * Routines to insert/extract short/long's. Must account for byte
315 * order and non-alignment problems. This code at least has the
316 * advantage of being portable.
322 _getshort(u_char
*msgp
)
324 register u_char
*p
= (u_char
*) msgp
;
328 return ((u_short
)(u
| *p
));
332 _getlong(u_char
*msgp
)
334 register u_char
*p
= (u_char
*) msgp
;
344 __putshort(register u_short s
, register u_char
*msgp
)
351 __putlong(register u_long l
, register u_char
*msgp
)