update from main archive 961217
[glibc.git] / resolv / res_comp.c
blobd96fe6e43267000db37a6dc05bef001d40ebe436
1 /*
2 * ++Copyright++ 1985, 1993
3 * -
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 * -
35 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies, and that
40 * the name of Digital Equipment Corporation not be used in advertising or
41 * publicity pertaining to distribution of the document or software without
42 * specific, written prior permission.
44 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
45 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
47 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
48 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
49 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
50 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
51 * SOFTWARE.
52 * -
53 * --Copyright--
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
58 static char rcsid[] = "$Id$";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/types.h>
62 #include <sys/param.h>
63 #include <netinet/in.h>
64 #include <arpa/nameser.h>
66 #include <stdio.h>
67 #include <resolv.h>
68 #include <ctype.h>
70 #if defined(BSD) && (BSD >= 199103)
71 # include <unistd.h>
72 # include <string.h>
73 #else
74 # include "../conf/portability.h"
75 #endif
77 static int dn_find __P((u_char *exp_dn, u_char *msg,
78 u_char **dnptrs, u_char **lastdnptr));
81 * Expand compressed domain name 'comp_dn' to full domain name.
82 * 'msg' is a pointer to the begining of the message,
83 * 'eomorig' points to the first location after the message,
84 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
85 * Return size of compressed name or -1 if there was an error.
87 int
88 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
89 const u_char *msg, *eomorig, *comp_dn;
90 char *exp_dn;
91 int length;
93 register const u_char *cp;
94 register char *dn;
95 register int n, c;
96 char *eom;
97 int len = -1, checked = 0;
99 dn = exp_dn;
100 cp = comp_dn;
101 eom = exp_dn + length;
103 * fetch next label in domain name
105 while (n = *cp++) {
107 * Check for indirection
109 switch (n & INDIR_MASK) {
110 case 0:
111 if (dn != exp_dn) {
112 if (dn >= eom)
113 return (-1);
114 *dn++ = '.';
116 if (dn+n >= eom)
117 return (-1);
118 checked += n + 1;
119 while (--n >= 0) {
120 if (((c = *cp++) == '.') || (c == '\\')) {
121 if (dn + n + 2 >= eom)
122 return (-1);
123 *dn++ = '\\';
125 *dn++ = c;
126 if (cp >= eomorig) /* out of range */
127 return (-1);
129 break;
131 case INDIR_MASK:
132 if (len < 0)
133 len = cp - comp_dn + 1;
134 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
135 if (cp < msg || cp >= eomorig) /* out of range */
136 return (-1);
137 checked += 2;
139 * Check for loops in the compressed name;
140 * if we've looked at the whole message,
141 * there must be a loop.
143 if (checked >= eomorig - msg)
144 return (-1);
145 break;
147 default:
148 return (-1); /* flag error */
151 *dn = '\0';
152 if (len < 0)
153 len = cp - comp_dn;
154 return (len);
158 * Compress domain name 'exp_dn' into 'comp_dn'.
159 * Return the size of the compressed name or -1.
160 * 'length' is the size of the array pointed to by 'comp_dn'.
161 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
162 * is a pointer to the beginning of the message. The list ends with NULL.
163 * 'lastdnptr' is a pointer to the end of the arrary pointed to
164 * by 'dnptrs'. Side effect is to update the list of pointers for
165 * labels inserted into the message as we compress the name.
166 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
167 * is NULL, we don't update the list.
170 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
171 const char *exp_dn;
172 u_char *comp_dn, **dnptrs, **lastdnptr;
173 int length;
175 register u_char *cp, *dn;
176 register int c, l;
177 u_char **cpp, **lpp, *sp, *eob;
178 u_char *msg;
180 dn = (u_char *)exp_dn;
181 cp = comp_dn;
182 eob = cp + length;
183 lpp = cpp = NULL;
184 if (dnptrs != NULL) {
185 if ((msg = *dnptrs++) != NULL) {
186 for (cpp = dnptrs; *cpp != NULL; cpp++)
188 lpp = cpp; /* end of list to search */
190 } else
191 msg = NULL;
192 for (c = *dn++; c != '\0'; ) {
193 /* look to see if we can use pointers */
194 if (msg != NULL) {
195 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
196 if (cp+1 >= eob)
197 return (-1);
198 *cp++ = (l >> 8) | INDIR_MASK;
199 *cp++ = l % 256;
200 return (cp - comp_dn);
202 /* not found, save it */
203 if (lastdnptr != NULL && cpp < lastdnptr-1) {
204 *cpp++ = cp;
205 *cpp = NULL;
208 sp = cp++; /* save ptr to length byte */
209 do {
210 if (c == '.') {
211 c = *dn++;
212 break;
214 if (c == '\\') {
215 if ((c = *dn++) == '\0')
216 break;
218 if (cp >= eob) {
219 if (msg != NULL)
220 *lpp = NULL;
221 return (-1);
223 *cp++ = c;
224 } while ((c = *dn++) != '\0');
225 /* catch trailing '.'s but not '..' */
226 if ((l = cp - sp - 1) == 0 && c == '\0') {
227 cp--;
228 break;
230 if (l <= 0 || l > MAXLABEL) {
231 if (msg != NULL)
232 *lpp = NULL;
233 return (-1);
235 *sp = l;
237 if (cp >= eob) {
238 if (msg != NULL)
239 *lpp = NULL;
240 return (-1);
242 *cp++ = '\0';
243 return (cp - comp_dn);
247 * Skip over a compressed domain name. Return the size or -1.
250 __dn_skipname(comp_dn, eom)
251 const u_char *comp_dn, *eom;
253 register const u_char *cp;
254 register int n;
256 cp = comp_dn;
257 while (cp < eom && (n = *cp++)) {
259 * check for indirection
261 switch (n & INDIR_MASK) {
262 case 0: /* normal case, n == len */
263 cp += n;
264 continue;
265 case INDIR_MASK: /* indirection */
266 cp++;
267 break;
268 default: /* illegal type */
269 return (-1);
271 break;
273 if (cp > eom)
274 return (-1);
275 return (cp - comp_dn);
278 static int
279 mklower(ch)
280 register int ch;
282 if (isascii(ch) && isupper(ch))
283 return (tolower(ch));
284 return (ch);
288 * Search for expanded name from a list of previously compressed names.
289 * Return the offset from msg if found or -1.
290 * dnptrs is the pointer to the first name on the list,
291 * not the pointer to the start of the message.
293 static int
294 dn_find(exp_dn, msg, dnptrs, lastdnptr)
295 u_char *exp_dn, *msg;
296 u_char **dnptrs, **lastdnptr;
298 register u_char *dn, *cp, **cpp;
299 register int n;
300 u_char *sp;
302 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
303 dn = exp_dn;
304 sp = cp = *cpp;
305 while (n = *cp++) {
307 * check for indirection
309 switch (n & INDIR_MASK) {
310 case 0: /* normal case, n == len */
311 while (--n >= 0) {
312 if (*dn == '.')
313 goto next;
314 if (*dn == '\\')
315 dn++;
316 if (mklower(*dn++) != mklower(*cp++))
317 goto next;
319 if ((n = *dn++) == '\0' && *cp == '\0')
320 return (sp - msg);
321 if (n == '.')
322 continue;
323 goto next;
325 case INDIR_MASK: /* indirection */
326 cp = msg + (((n & 0x3f) << 8) | *cp);
327 break;
329 default: /* illegal type */
330 return (-1);
333 if (*dn == '\0')
334 return (sp - msg);
335 next: ;
337 return (-1);
341 * Verify that a domain name uses an acceptable character set.
345 * Note the conspicuous absence of ctype macros in these definitions. On
346 * non-ASCII hosts, we can't depend on string literals or ctype macros to
347 * tell us anything about network-format data. The rest of the BIND system
348 * is not careful about this, but for some reason, we're doing it right here.
350 #define PERIOD 0x2e
351 #define hyphenchar(c) ((c) == 0x2d)
352 #define bslashchar(c) ((c) == 0x5c)
353 #define periodchar(c) ((c) == PERIOD)
354 #define asterchar(c) ((c) == 0x2a)
355 #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
356 || ((c) >= 0x61 && (c) <= 0x7a))
357 #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
359 #define borderchar(c) (alphachar(c) || digitchar(c))
360 #define middlechar(c) (borderchar(c) || hyphenchar(c))
361 #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
364 res_hnok(dn)
365 const char *dn;
367 int ppch = '\0', pch = PERIOD, ch = *dn++;
369 while (ch != '\0') {
370 int nch = *dn++;
372 if (periodchar(ch)) {
373 /* NULL */;
374 } else if (periodchar(pch)) {
375 if (!borderchar(ch))
376 return (0);
377 } else if (periodchar(nch) || nch == '\0') {
378 if (!borderchar(ch))
379 return (0);
380 } else {
381 if (!middlechar(ch))
382 return (0);
384 ppch = pch, pch = ch, ch = nch;
386 return (1);
390 * hostname-like (A, MX, WKS) owners can have "*" as their first label
391 * but must otherwise be as a host name.
394 res_ownok(dn)
395 const char *dn;
397 if (asterchar(dn[0])) {
398 if (periodchar(dn[1]))
399 return (res_hnok(dn+2));
400 if (dn[1] == '\0')
401 return (1);
403 return (res_hnok(dn));
407 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
408 * label, but the rest of the name has to look like a host name.
411 res_mailok(dn)
412 const char *dn;
414 int ch, escaped = 0;
416 /* "." is a valid missing representation */
417 if (*dn == '\0')
418 return(1);
420 /* otherwise <label>.<hostname> */
421 while ((ch = *dn++) != '\0') {
422 if (!domainchar(ch))
423 return (0);
424 if (!escaped && periodchar(ch))
425 break;
426 if (escaped)
427 escaped = 0;
428 else if (bslashchar(ch))
429 escaped = 1;
431 if (periodchar(ch))
432 return (res_hnok(dn));
433 return(0);
437 * This function is quite liberal, since RFC 1034's character sets are only
438 * recommendations.
441 res_dnok(dn)
442 const char *dn;
444 int ch;
446 while ((ch = *dn++) != '\0')
447 if (!domainchar(ch))
448 return (0);
449 return (1);
453 * Routines to insert/extract short/long's.
456 u_int16_t
457 _getshort(msgp)
458 register const u_char *msgp;
460 register u_int16_t u;
462 GETSHORT(u, msgp);
463 return (u);
466 #ifdef NeXT
468 * nExt machines have some funky library conventions, which we must maintain.
470 u_int16_t
471 res_getshort(msgp)
472 register const u_char *msgp;
474 return (_getshort(msgp));
476 #endif
478 u_int32_t
479 _getlong(msgp)
480 register const u_char *msgp;
482 register u_int32_t u;
484 GETLONG(u, msgp);
485 return (u);
488 void
489 #if defined(__STDC__) || defined(__cplusplus)
490 __putshort(register u_int16_t s, register u_char *msgp) /* must match proto */
491 #else
492 __putshort(s, msgp)
493 register u_int16_t s;
494 register u_char *msgp;
495 #endif
497 PUTSHORT(s, msgp);
500 void
501 __putlong(l, msgp)
502 register u_int32_t l;
503 register u_char *msgp;
505 PUTLONG(l, msgp);
508 #ifdef ultrix
509 /* ultrix 4.0 had some icky packaging in its libc.a. alias for it here.
510 * there is more gunk of this kind over in res_debug.c.
512 #undef putshort
513 void
514 #if defined(__STDC__) || defined(__cplusplus)
515 putshort(register u_short s, register u_char *msgp)
516 #else
517 putshort(s, msgp)
518 register u_short s;
519 register u_char *msgp;
520 #endif
522 __putshort(s, msgp);
524 #undef putlong
525 void
526 putlong(l, msgp)
527 register u_int32_t l;
528 register u_char *msgp;
530 __putlong(l, msgp);
533 #undef dn_comp
535 dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
536 const char *exp_dn;
537 u_char *comp_dn, **dnptrs, **lastdnptr;
538 int length;
540 return (__dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr));
543 #undef dn_expand
545 dn_expand(msg, eomorig, comp_dn, exp_dn, length)
546 const u_char *msg, *eomorig, *comp_dn;
547 char *exp_dn;
548 int length;
550 return (__dn_expand(msg, eomorig, comp_dn, exp_dn, length));
553 #undef dn_skipname
554 dn_skipname(comp_dn, eom)
555 const u_char *comp_dn, *eom;
557 return (__dn_skipname(comp_dn, eom));
559 #endif /* Ultrix 4.0 hackery */