revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / res_comp.c
blob4af634b9182a1ad33c6713c9c0ed384daf36b368
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
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,
19 * MA 02111-1307, USA.
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
29 * are met:
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
53 * SUCH DAMAGE.
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 */
60 #include <conf.h>
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.
81 int
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;
86 register int n, c;
87 u_char *eom;
88 int len = -1, checked = 0;
90 dn = exp_dn;
91 cp = (u_char *)comp_dn;
92 eom = exp_dn + length;
94 * fetch next label in domain name
96 while (n = *cp++) {
98 * Check for indirection
100 switch (n & INDIR_MASK) {
101 case 0:
102 if (dn != exp_dn) {
103 if (dn >= eom)
104 return (-1);
105 *dn++ = '.';
107 if (dn+n >= eom)
108 return (-1);
109 checked += n + 1;
110 while (--n >= 0) {
111 if ((c = *cp++) == '.') {
112 if (dn + n + 2 >= eom)
113 return (-1);
114 *dn++ = '\\';
116 *dn++ = c;
117 if (cp >= eomorig) /* out of range */
118 return(-1);
120 break;
122 case INDIR_MASK:
123 if (len < 0)
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 */
127 return(-1);
128 checked += 2;
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)
135 return (-1);
136 break;
138 default:
139 return (-1); /* flag error */
142 *dn = '\0';
143 if (len < 0)
144 len = cp - comp_dn;
145 return (len);
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;
165 register int c, l;
166 u_char **cpp, **lpp, *sp, *eob;
167 u_char *msg;
169 dn = (u_char *)exp_dn;
170 cp = comp_dn;
171 eob = cp + length;
172 if (dnptrs != NULL) {
173 if ((msg = *dnptrs++) != NULL) {
174 for (cpp = dnptrs; *cpp != NULL; cpp++)
176 lpp = cpp; /* end of list to search */
178 } else
179 msg = NULL;
180 for (c = *dn++; c != '\0'; ) {
181 /* look to see if we can use pointers */
182 if (msg != NULL) {
183 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
184 if (cp+1 >= eob)
185 return (-1);
186 *cp++ = (l >> 8) | INDIR_MASK;
187 *cp++ = l % 256;
188 return (cp - comp_dn);
190 /* not found, save it */
191 if (lastdnptr != NULL && cpp < lastdnptr-1) {
192 *cpp++ = cp;
193 *cpp = NULL;
196 sp = cp++; /* save ptr to length byte */
197 do {
198 if (c == '.') {
199 c = *dn++;
200 break;
202 if (c == '\\') {
203 if ((c = *dn++) == '\0')
204 break;
206 if (cp >= eob) {
207 if (msg != NULL)
208 *lpp = NULL;
209 return (-1);
211 *cp++ = c;
212 } while ((c = *dn++) != '\0');
213 /* catch trailing '.'s but not '..' */
214 if ((l = cp - sp - 1) == 0 && c == '\0') {
215 cp--;
216 break;
218 if (l <= 0 || l > MAXLABEL) {
219 if (msg != NULL)
220 *lpp = NULL;
221 return (-1);
223 *sp = l;
225 if (cp >= eob) {
226 if (msg != NULL)
227 *lpp = NULL;
228 return (-1);
230 *cp++ = '\0';
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)
240 register u_char *cp;
241 register int n;
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 */
250 cp += n;
251 continue;
252 default: /* illegal type */
253 return (-1);
254 case INDIR_MASK: /* indirection */
255 cp++;
257 break;
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.
268 static int
269 dn_find(u_char *exp_dn, u_char *msg,
270 u_char **dnptrs, u_char **lastdnptr)
272 register u_char *dn, *cp, **cpp;
273 register int n;
274 u_char *sp;
276 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
277 dn = exp_dn;
278 sp = cp = *cpp;
279 while (n = *cp++) {
281 * check for indirection
283 switch (n & INDIR_MASK) {
284 case 0: /* normal case, n == len */
285 while (--n >= 0) {
286 if (*dn == '.')
287 goto next;
288 if (*dn == '\\')
289 dn++;
290 if (*dn++ != *cp++)
291 goto next;
293 if ((n = *dn++) == '\0' && *cp == '\0')
294 return (sp - msg);
295 if (n == '.')
296 continue;
297 goto next;
299 default: /* illegal type */
300 return (-1);
302 case INDIR_MASK: /* indirection */
303 cp = msg + (((n & 0x3f) << 8) | *cp);
306 if (*dn == '\0')
307 return (sp - msg);
308 next: ;
310 return (-1);
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.
318 * used by sendmail.
321 u_short
322 _getshort(u_char *msgp)
324 register u_char *p = (u_char *) msgp;
325 register u_short u;
327 u = *p++ << 8;
328 return ((u_short)(u | *p));
331 u_long
332 _getlong(u_char *msgp)
334 register u_char *p = (u_char *) msgp;
335 register u_long u;
337 u = *p++; u <<= 8;
338 u |= *p++; u <<= 8;
339 u |= *p++; u <<= 8;
340 return (u | *p);
343 void
344 __putshort(register u_short s, register u_char *msgp)
346 msgp[1] = s;
347 msgp[0] = s >> 8;
350 void
351 __putlong(register u_long l, register u_char *msgp)
353 msgp[3] = l;
354 msgp[2] = (l >>= 8);
355 msgp[1] = (l >>= 8);
356 msgp[0] = l >> 8;