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