Libinfo-324.tar.gz
[apple/libinfo.git] / dns.subproj / res_comp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * ++Copyright++ 1985, 1993
26 * -
27 * Copyright (c) 1985, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
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.
45 *
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
56 * SUCH DAMAGE.
57 * -
58 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
59 *
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.
66 *
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
74 * SOFTWARE.
75 * -
76 * --Copyright--
77 */
78
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 */
83
84 #include <sys/param.h>
85 #include <netinet/in.h>
86
87 #include <stdio.h>
88 #include <ctype.h>
89
90 #include "nameser8_compat.h"
91 #include "resolv8_compat.h"
92
93 #if defined(BSD) && (BSD >= 199103)
94 # include <unistd.h>
95 # include <string.h>
96 #else
97 # include "portability.h"
98 #endif
99
100 static int dn_find __P((u_char *exp_dn, u_char *msg,
101 u_char **dnptrs, u_char **lastdnptr));
102
103 /*
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.
109 */
110 int
111 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
112 const u_char *msg, *eomorig, *comp_dn;
113 char *exp_dn;
114 int length;
115 {
116 register const u_char *cp;
117 register char *dn;
118 register int n, c;
119 char *eom;
120 int len = -1, checked = 0;
121
122 dn = exp_dn;
123 cp = comp_dn;
124 eom = exp_dn + length;
125 /*
126 * fetch next label in domain name
127 */
128 while ((n = *cp++)) {
129 /*
130 * Check for indirection
131 */
132 switch (n & INDIR_MASK) {
133 case 0:
134 if (dn != exp_dn) {
135 if (dn >= eom)
136 return (-1);
137 *dn++ = '.';
138 }
139 if (dn+n >= eom)
140 return (-1);
141 checked += n + 1;
142 while (--n >= 0) {
143 if ((c = *cp++) == '.') {
144 if (dn + n + 2 >= eom)
145 return (-1);
146 *dn++ = '\\';
147 }
148 *dn++ = c;
149 if (cp >= eomorig) /* out of range */
150 return (-1);
151 }
152 break;
153
154 case INDIR_MASK:
155 if (len < 0)
156 len = cp - comp_dn + 1;
157 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
158 if (cp < msg || cp >= eomorig) /* out of range */
159 return (-1);
160 checked += 2;
161 /*
162 * Check for loops in the compressed name;
163 * if we've looked at the whole message,
164 * there must be a loop.
165 */
166 if (checked >= eomorig - msg)
167 return (-1);
168 break;
169
170 default:
171 return (-1); /* flag error */
172 }
173 }
174 *dn = '\0';
175 for (dn = exp_dn; (c = *dn) != '\0'; dn++)
176 if (isascii(c) && isspace(c))
177 return (-1);
178 if (len < 0)
179 len = cp - comp_dn;
180 return (len);
181 }
182
183 /*
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.
194 */
195 int
196 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
197 const char *exp_dn;
198 u_char *comp_dn, **dnptrs, **lastdnptr;
199 int length;
200 {
201 register u_char *cp, *dn;
202 register int c, l;
203 u_char **cpp, **lpp, *sp, *eob;
204 u_char *msg;
205
206 dn = (u_char *)exp_dn;
207 cp = comp_dn;
208 eob = cp + length;
209 lpp = cpp = NULL;
210 if (dnptrs != NULL) {
211 if ((msg = *dnptrs++) != NULL) {
212 for (cpp = dnptrs; *cpp != NULL; cpp++)
213 ;
214 lpp = cpp; /* end of list to search */
215 }
216 } else
217 msg = NULL;
218 for (c = *dn++; c != '\0'; ) {
219 /* look to see if we can use pointers */
220 if (msg != NULL) {
221 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
222 if (cp+1 >= eob)
223 return (-1);
224 *cp++ = (l >> 8) | INDIR_MASK;
225 *cp++ = l % 256;
226 return (cp - comp_dn);
227 }
228 /* not found, save it */
229 if (lastdnptr != NULL && cpp < lastdnptr-1) {
230 *cpp++ = cp;
231 *cpp = NULL;
232 }
233 }
234 sp = cp++; /* save ptr to length byte */
235 do {
236 if (c == '.') {
237 c = *dn++;
238 break;
239 }
240 if (c == '\\') {
241 if ((c = *dn++) == '\0')
242 break;
243 }
244 if (cp >= eob) {
245 if (msg != NULL)
246 *lpp = NULL;
247 return (-1);
248 }
249 *cp++ = c;
250 } while ((c = *dn++) != '\0');
251 /* catch trailing '.'s but not '..' */
252 if ((l = cp - sp - 1) == 0 && c == '\0') {
253 cp--;
254 break;
255 }
256 if (l <= 0 || l > MAXLABEL) {
257 if (msg != NULL)
258 *lpp = NULL;
259 return (-1);
260 }
261 *sp = l;
262 }
263 if (cp >= eob) {
264 if (msg != NULL)
265 *lpp = NULL;
266 return (-1);
267 }
268 *cp++ = '\0';
269 return (cp - comp_dn);
270 }
271
272 /*
273 * Skip over a compressed domain name. Return the size or -1.
274 */
275 int
276 __dn_skipname(comp_dn, eom)
277 const u_char *comp_dn, *eom;
278 {
279 register const u_char *cp;
280 register int n;
281
282 cp = comp_dn;
283 while (cp < eom && (n = *cp++)) {
284 /*
285 * check for indirection
286 */
287 switch (n & INDIR_MASK) {
288 case 0: /* normal case, n == len */
289 cp += n;
290 continue;
291 case INDIR_MASK: /* indirection */
292 cp++;
293 break;
294 default: /* illegal type */
295 return (-1);
296 }
297 break;
298 }
299 if (cp > eom)
300 return (-1);
301 return (cp - comp_dn);
302 }
303
304 static int
305 mklower(ch)
306 register int ch;
307 {
308 if (isascii(ch) && isupper(ch))
309 return (tolower(ch));
310 return (ch);
311 }
312
313 /*
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.
318 */
319 static int
320 dn_find(exp_dn, msg, dnptrs, lastdnptr)
321 u_char *exp_dn, *msg;
322 u_char **dnptrs, **lastdnptr;
323 {
324 register u_char *dn, *cp, **cpp;
325 register int n;
326 u_char *sp;
327
328 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
329 dn = exp_dn;
330 sp = cp = *cpp;
331 while ((n = *cp++)) {
332 /*
333 * check for indirection
334 */
335 switch (n & INDIR_MASK) {
336 case 0: /* normal case, n == len */
337 while (--n >= 0) {
338 if (*dn == '.')
339 goto next;
340 if (*dn == '\\')
341 dn++;
342 if (mklower(*dn++) != mklower(*cp++))
343 goto next;
344 }
345 if ((n = *dn++) == '\0' && *cp == '\0')
346 return (sp - msg);
347 if (n == '.')
348 continue;
349 goto next;
350
351 case INDIR_MASK: /* indirection */
352 cp = msg + (((n & 0x3f) << 8) | *cp);
353 break;
354
355 default: /* illegal type */
356 return (-1);
357 }
358 }
359 if (*dn == '\0')
360 return (sp - msg);
361 next: ;
362 }
363 return (-1);
364 }
365
366 /*
367 * Routines to insert/extract short/long's.
368 */
369
370 u_int16_t
371 _getshort(msgp)
372 register const u_char *msgp;
373 {
374 register u_int16_t u;
375
376 GETSHORT(u, msgp);
377 return (u);
378 }
379
380 #if defined(__APPLE__)
381 /*
382 * nExt machines have some funky library conventions, which we must maintain.
383 */
384 u_int16_t
385 res_getshort(msgp)
386 register const u_char *msgp;
387 {
388 return (_getshort(msgp));
389 }
390 #endif
391
392 u_int32_t
393 _getlong(msgp)
394 register const u_char *msgp;
395 {
396 register u_int32_t u;
397
398 GETLONG(u, msgp);
399 return (u);
400 }
401
402 void
403 #if defined(__STDC__) || defined(__cplusplus)
404 __putshort(register u_int16_t s, register u_char *msgp) /* must match proto */
405 #else
406 __putshort(s, msgp)
407 register u_int16_t s;
408 register u_char *msgp;
409 #endif
410 {
411 PUTSHORT(s, msgp);
412 }
413
414 void
415 __putlong(l, msgp)
416 register u_int32_t l;
417 register u_char *msgp;
418 {
419 PUTLONG(l, msgp);
420 }
421
422 #ifdef ultrix
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.
425 */
426 #undef putshort
427 void
428 #if defined(__STDC__) || defined(__cplusplus)
429 putshort(register u_short s, register u_char *msgp)
430 #else
431 putshort(s, msgp)
432 register u_short s;
433 register u_char *msgp;
434 #endif
435 {
436 __putshort(s, msgp);
437 }
438 #undef putlong
439 void
440 putlong(l, msgp)
441 register u_int32_t l;
442 register u_char *msgp;
443 {
444 __putlong(l, msgp);
445 }
446
447 #undef dn_skipname
448 dn_skipname(comp_dn, eom)
449 const u_char *comp_dn, *eom;
450 {
451 return (__dn_skipname(comp_dn, eom));
452 }
453 #endif /* Ultrix 4.0 hackery */