xref: /csrg-svn/lib/libc/net/res_comp.c (revision 39788)
118546Sralph /*
221384Sdist  * Copyright (c) 1985 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
533679Sbostic  * Redistribution and use in source and binary forms are permitted
634817Sbostic  * provided that the above copyright notice and this paragraph are
734817Sbostic  * duplicated in all such forms and that any documentation,
834817Sbostic  * advertising materials, and other materials related to such
934817Sbostic  * distribution and use acknowledge that the software was developed
1034817Sbostic  * by the University of California, Berkeley.  The name of the
1134817Sbostic  * University may not be used to endorse or promote products derived
1234817Sbostic  * from this software without specific prior written permission.
1334817Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434817Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534817Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1618546Sralph  */
1718546Sralph 
1826631Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*39788Sbloom static char sccsid[] = "@(#)res_comp.c	6.15 (Berkeley) 12/27/89";
2033679Sbostic #endif /* LIBC_SCCS and not lint */
2121384Sdist 
2218140Sralph #include <sys/types.h>
2318140Sralph #include <stdio.h>
2424079Skjd #include <arpa/nameser.h>
2518140Sralph 
2618140Sralph /*
2718342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
2818342Sralph  * 'msg' is a pointer to the begining of the message,
2926112Skarels  * 'eomorig' points to the first location after the message,
3018342Sralph  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
3118140Sralph  * Return size of compressed name or -1 if there was an error.
3218140Sralph  */
3326112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length)
3432647Skarels 	u_char *msg, *eomorig, *comp_dn, *exp_dn;
3526112Skarels 	int length;
3618140Sralph {
3732647Skarels 	register u_char *cp, *dn;
3818140Sralph 	register int n, c;
3932647Skarels 	u_char *eom;
4031686Skarels 	int len = -1, checked = 0;
4118140Sralph 
4218140Sralph 	dn = exp_dn;
4318140Sralph 	cp = comp_dn;
44*39788Sbloom 	eom = exp_dn + length;
4518140Sralph 	/*
4618140Sralph 	 * fetch next label in domain name
4718140Sralph 	 */
4818140Sralph 	while (n = *cp++) {
4918140Sralph 		/*
5018140Sralph 		 * Check for indirection
5118140Sralph 		 */
5218140Sralph 		switch (n & INDIR_MASK) {
5318140Sralph 		case 0:
5418342Sralph 			if (dn != exp_dn) {
5518342Sralph 				if (dn >= eom)
5618342Sralph 					return (-1);
5718140Sralph 				*dn++ = '.';
5818342Sralph 			}
5918140Sralph 			if (dn+n >= eom)
6018140Sralph 				return (-1);
6131686Skarels 			checked += n + 1;
6226064Skjd 			while (--n >= 0) {
6326812Sbloom 				if ((c = *cp++) == '.') {
6426812Sbloom 					if (dn+n+1 >= eom)
6526812Sbloom 						return (-1);
6626812Sbloom 					*dn++ = '\\';
6718342Sralph 				}
6826812Sbloom 				*dn++ = c;
6926112Skarels 				if (cp >= eomorig)	/* out of range */
7026064Skjd 					return(-1);
7126064Skjd 			}
7218140Sralph 			break;
7318140Sralph 
7418140Sralph 		case INDIR_MASK:
7518342Sralph 			if (len < 0)
7618140Sralph 				len = cp - comp_dn + 1;
7718140Sralph 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
7826112Skarels 			if (cp < msg || cp >= eomorig)	/* out of range */
7926064Skjd 				return(-1);
8031686Skarels 			checked += 2;
8131686Skarels 			/*
8231686Skarels 			 * Check for loops in the compressed name;
8331686Skarels 			 * if we've looked at the whole message,
8431686Skarels 			 * there must be a loop.
8531686Skarels 			 */
8631686Skarels 			if (checked >= eomorig - msg)
8731686Skarels 				return (-1);
8818140Sralph 			break;
8918140Sralph 
9018140Sralph 		default:
9118140Sralph 			return (-1);			/* flag error */
9218140Sralph 		}
9318140Sralph 	}
9418140Sralph 	*dn = '\0';
9518342Sralph 	if (len < 0)
9618140Sralph 		len = cp - comp_dn;
9718140Sralph 	return (len);
9818140Sralph }
9918140Sralph 
10018140Sralph /*
10118342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
10218342Sralph  * Return the size of the compressed name or -1.
10318342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
10418342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
10518140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
10618342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
10718342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
10818342Sralph  * labels inserted into the message as we compress the name.
10918342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
11018342Sralph  * is NULL, we don't update the list.
11118140Sralph  */
11218140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
11332647Skarels 	u_char *exp_dn, *comp_dn;
11418140Sralph 	int length;
11532647Skarels 	u_char **dnptrs, **lastdnptr;
11618140Sralph {
11732647Skarels 	register u_char *cp, *dn;
11818140Sralph 	register int c, l;
11932647Skarels 	u_char **cpp, **lpp, *sp, *eob;
12032647Skarels 	u_char *msg;
12118140Sralph 
12218140Sralph 	dn = exp_dn;
12318140Sralph 	cp = comp_dn;
12425360Sbloom 	eob = cp + length;
12518140Sralph 	if (dnptrs != NULL) {
12618140Sralph 		if ((msg = *dnptrs++) != NULL) {
12718140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
12818140Sralph 				;
12918140Sralph 			lpp = cpp;	/* end of list to search */
13018140Sralph 		}
13118140Sralph 	} else
13218140Sralph 		msg = NULL;
13318140Sralph 	for (c = *dn++; c != '\0'; ) {
13418140Sralph 		/* look to see if we can use pointers */
13518140Sralph 		if (msg != NULL) {
13618140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
13718140Sralph 				if (cp+1 >= eob)
13818140Sralph 					return (-1);
13918140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
14026240Skjd 				*cp++ = l % 256;
14118140Sralph 				return (cp - comp_dn);
14218140Sralph 			}
14318140Sralph 			/* not found, save it */
14418140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
14518140Sralph 				*cpp++ = cp;
14618140Sralph 				*cpp = NULL;
14718140Sralph 			}
14818140Sralph 		}
14918140Sralph 		sp = cp++;	/* save ptr to length byte */
15018140Sralph 		do {
15118140Sralph 			if (c == '.') {
15218140Sralph 				c = *dn++;
15318140Sralph 				break;
15418140Sralph 			}
15518342Sralph 			if (c == '\\') {
15618342Sralph 				if ((c = *dn++) == '\0')
15718342Sralph 					break;
15818342Sralph 			}
159*39788Sbloom 			if (cp >= eob) {
160*39788Sbloom 				if (msg != NULL)
161*39788Sbloom 					*lpp = NULL;
16218140Sralph 				return (-1);
163*39788Sbloom 			}
16418140Sralph 			*cp++ = c;
16518140Sralph 		} while ((c = *dn++) != '\0');
16618342Sralph 		/* catch trailing '.'s but not '..' */
16718342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
16818342Sralph 			cp--;
16918342Sralph 			break;
17018342Sralph 		}
171*39788Sbloom 		if (l <= 0 || l > MAXLABEL) {
172*39788Sbloom 			if (msg != NULL)
173*39788Sbloom 				*lpp = NULL;
17418140Sralph 			return (-1);
175*39788Sbloom 		}
17618140Sralph 		*sp = l;
17718140Sralph 	}
178*39788Sbloom 	if (cp >= eob) {
179*39788Sbloom 		if (msg != NULL)
180*39788Sbloom 			*lpp = NULL;
18118140Sralph 		return (-1);
182*39788Sbloom 	}
18318140Sralph 	*cp++ = '\0';
18418140Sralph 	return (cp - comp_dn);
18518140Sralph }
18618140Sralph 
18718140Sralph /*
18818342Sralph  * Skip over a compressed domain name. Return the size or -1.
18918140Sralph  */
19032647Skarels dn_skipname(comp_dn, eom)
19132647Skarels 	u_char *comp_dn, *eom;
19218140Sralph {
19332647Skarels 	register u_char *cp;
19418140Sralph 	register int n;
19518140Sralph 
19618342Sralph 	cp = comp_dn;
19732647Skarels 	while (cp < eom && (n = *cp++)) {
19818140Sralph 		/*
19918140Sralph 		 * check for indirection
20018140Sralph 		 */
20118140Sralph 		switch (n & INDIR_MASK) {
20218140Sralph 		case 0:		/* normal case, n == len */
20318140Sralph 			cp += n;
20418140Sralph 			continue;
20518140Sralph 		default:	/* illegal type */
20618140Sralph 			return (-1);
20718140Sralph 		case INDIR_MASK:	/* indirection */
20818140Sralph 			cp++;
20918140Sralph 		}
21018140Sralph 		break;
21118140Sralph 	}
21218342Sralph 	return (cp - comp_dn);
21318140Sralph }
21418140Sralph 
21518140Sralph /*
21618140Sralph  * Search for expanded name from a list of previously compressed names.
21718140Sralph  * Return the offset from msg if found or -1.
21833617Skarels  * dnptrs is the pointer to the first name on the list,
21933617Skarels  * not the pointer to the start of the message.
22018140Sralph  */
22133617Skarels static
22218140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
22332647Skarels 	u_char *exp_dn, *msg;
22432647Skarels 	u_char **dnptrs, **lastdnptr;
22518140Sralph {
22632647Skarels 	register u_char *dn, *cp, **cpp;
22718140Sralph 	register int n;
22832647Skarels 	u_char *sp;
22918140Sralph 
23033617Skarels 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
23118140Sralph 		dn = exp_dn;
23218140Sralph 		sp = cp = *cpp;
23318140Sralph 		while (n = *cp++) {
23418140Sralph 			/*
23518140Sralph 			 * check for indirection
23618140Sralph 			 */
23718140Sralph 			switch (n & INDIR_MASK) {
23818140Sralph 			case 0:		/* normal case, n == len */
23918342Sralph 				while (--n >= 0) {
240*39788Sbloom 					if (*dn == '.')
241*39788Sbloom 						goto next;
24218342Sralph 					if (*dn == '\\')
24318342Sralph 						dn++;
24418140Sralph 					if (*dn++ != *cp++)
24518140Sralph 						goto next;
24618342Sralph 				}
24718140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
24818140Sralph 					return (sp - msg);
24918140Sralph 				if (n == '.')
25018140Sralph 					continue;
25118140Sralph 				goto next;
25218140Sralph 
25318140Sralph 			default:	/* illegal type */
25418140Sralph 				return (-1);
25518140Sralph 
25618140Sralph 			case INDIR_MASK:	/* indirection */
25732647Skarels 				cp = msg + (((n & 0x3f) << 8) | *cp);
25818140Sralph 			}
25918140Sralph 		}
26018140Sralph 		if (*dn == '\0')
26118140Sralph 			return (sp - msg);
26218140Sralph 	next:	;
26318140Sralph 	}
26418140Sralph 	return (-1);
26518140Sralph }
26618528Sralph 
26718528Sralph /*
26818528Sralph  * Routines to insert/extract short/long's. Must account for byte
26918528Sralph  * order and non-alignment problems. This code at least has the
27018528Sralph  * advantage of being portable.
27133727Sbostic  *
27233727Sbostic  * used by sendmail.
27318528Sralph  */
27418528Sralph 
27518528Sralph u_short
27630441Skjd _getshort(msgp)
27732647Skarels 	u_char *msgp;
27818528Sralph {
27918528Sralph 	register u_char *p = (u_char *) msgp;
28025360Sbloom #ifdef vax
28125360Sbloom 	/*
28225360Sbloom 	 * vax compiler doesn't put shorts in registers
28325360Sbloom 	 */
28425360Sbloom 	register u_long u;
28525360Sbloom #else
28624735Sbloom 	register u_short u;
28725360Sbloom #endif
28818528Sralph 
28924735Sbloom 	u = *p++ << 8;
29025360Sbloom 	return ((u_short)(u | *p));
29118528Sralph }
29218528Sralph 
29318528Sralph u_long
29430441Skjd _getlong(msgp)
29532647Skarels 	u_char *msgp;
29618528Sralph {
29718528Sralph 	register u_char *p = (u_char *) msgp;
29823870Skjd 	register u_long u;
29918528Sralph 
30023870Skjd 	u = *p++; u <<= 8;
30123870Skjd 	u |= *p++; u <<= 8;
30223870Skjd 	u |= *p++; u <<= 8;
30323870Skjd 	return (u | *p);
30418528Sralph }
30518528Sralph 
30623870Skjd 
30718528Sralph putshort(s, msgp)
30818528Sralph 	register u_short s;
30932647Skarels 	register u_char *msgp;
31018528Sralph {
31118528Sralph 
31218528Sralph 	msgp[1] = s;
31318528Sralph 	msgp[0] = s >> 8;
31418528Sralph }
31518528Sralph 
31618528Sralph putlong(l, msgp)
31718528Sralph 	register u_long l;
31832647Skarels 	register u_char *msgp;
31918528Sralph {
32018528Sralph 
32118528Sralph 	msgp[3] = l;
32218528Sralph 	msgp[2] = (l >>= 8);
32318528Sralph 	msgp[1] = (l >>= 8);
32418528Sralph 	msgp[0] = l >> 8;
32518528Sralph }
326