xref: /csrg-svn/lib/libc/net/res_comp.c (revision 42627)
118546Sralph /*
221384Sdist  * Copyright (c) 1985 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
5*42627Sbostic  * %sccs.include.redist.c%
618546Sralph  */
718546Sralph 
826631Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42627Sbostic static char sccsid[] = "@(#)res_comp.c	6.17 (Berkeley) 06/01/90";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121384Sdist 
1218140Sralph #include <sys/types.h>
1318140Sralph #include <stdio.h>
1424079Skjd #include <arpa/nameser.h>
1518140Sralph 
1641093Sleres static dn_find();
1741093Sleres 
1818140Sralph /*
1918342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
2018342Sralph  * 'msg' is a pointer to the begining of the message,
2126112Skarels  * 'eomorig' points to the first location after the message,
2218342Sralph  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2318140Sralph  * Return size of compressed name or -1 if there was an error.
2418140Sralph  */
2526112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length)
2632647Skarels 	u_char *msg, *eomorig, *comp_dn, *exp_dn;
2726112Skarels 	int length;
2818140Sralph {
2932647Skarels 	register u_char *cp, *dn;
3018140Sralph 	register int n, c;
3132647Skarels 	u_char *eom;
3231686Skarels 	int len = -1, checked = 0;
3318140Sralph 
3418140Sralph 	dn = exp_dn;
3518140Sralph 	cp = comp_dn;
3639788Sbloom 	eom = exp_dn + length;
3718140Sralph 	/*
3818140Sralph 	 * fetch next label in domain name
3918140Sralph 	 */
4018140Sralph 	while (n = *cp++) {
4118140Sralph 		/*
4218140Sralph 		 * Check for indirection
4318140Sralph 		 */
4418140Sralph 		switch (n & INDIR_MASK) {
4518140Sralph 		case 0:
4618342Sralph 			if (dn != exp_dn) {
4718342Sralph 				if (dn >= eom)
4818342Sralph 					return (-1);
4918140Sralph 				*dn++ = '.';
5018342Sralph 			}
5118140Sralph 			if (dn+n >= eom)
5218140Sralph 				return (-1);
5331686Skarels 			checked += n + 1;
5426064Skjd 			while (--n >= 0) {
5526812Sbloom 				if ((c = *cp++) == '.') {
5626812Sbloom 					if (dn+n+1 >= eom)
5726812Sbloom 						return (-1);
5826812Sbloom 					*dn++ = '\\';
5918342Sralph 				}
6026812Sbloom 				*dn++ = c;
6126112Skarels 				if (cp >= eomorig)	/* out of range */
6226064Skjd 					return(-1);
6326064Skjd 			}
6418140Sralph 			break;
6518140Sralph 
6618140Sralph 		case INDIR_MASK:
6718342Sralph 			if (len < 0)
6818140Sralph 				len = cp - comp_dn + 1;
6918140Sralph 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
7026112Skarels 			if (cp < msg || cp >= eomorig)	/* out of range */
7126064Skjd 				return(-1);
7231686Skarels 			checked += 2;
7331686Skarels 			/*
7431686Skarels 			 * Check for loops in the compressed name;
7531686Skarels 			 * if we've looked at the whole message,
7631686Skarels 			 * there must be a loop.
7731686Skarels 			 */
7831686Skarels 			if (checked >= eomorig - msg)
7931686Skarels 				return (-1);
8018140Sralph 			break;
8118140Sralph 
8218140Sralph 		default:
8318140Sralph 			return (-1);			/* flag error */
8418140Sralph 		}
8518140Sralph 	}
8618140Sralph 	*dn = '\0';
8718342Sralph 	if (len < 0)
8818140Sralph 		len = cp - comp_dn;
8918140Sralph 	return (len);
9018140Sralph }
9118140Sralph 
9218140Sralph /*
9318342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
9418342Sralph  * Return the size of the compressed name or -1.
9518342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
9618342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
9718140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
9818342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
9918342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
10018342Sralph  * labels inserted into the message as we compress the name.
10118342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
10218342Sralph  * is NULL, we don't update the list.
10318140Sralph  */
10418140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
10532647Skarels 	u_char *exp_dn, *comp_dn;
10618140Sralph 	int length;
10732647Skarels 	u_char **dnptrs, **lastdnptr;
10818140Sralph {
10932647Skarels 	register u_char *cp, *dn;
11018140Sralph 	register int c, l;
11132647Skarels 	u_char **cpp, **lpp, *sp, *eob;
11232647Skarels 	u_char *msg;
11318140Sralph 
11418140Sralph 	dn = exp_dn;
11518140Sralph 	cp = comp_dn;
11625360Sbloom 	eob = cp + length;
11718140Sralph 	if (dnptrs != NULL) {
11818140Sralph 		if ((msg = *dnptrs++) != NULL) {
11918140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
12018140Sralph 				;
12118140Sralph 			lpp = cpp;	/* end of list to search */
12218140Sralph 		}
12318140Sralph 	} else
12418140Sralph 		msg = NULL;
12518140Sralph 	for (c = *dn++; c != '\0'; ) {
12618140Sralph 		/* look to see if we can use pointers */
12718140Sralph 		if (msg != NULL) {
12818140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
12918140Sralph 				if (cp+1 >= eob)
13018140Sralph 					return (-1);
13118140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
13226240Skjd 				*cp++ = l % 256;
13318140Sralph 				return (cp - comp_dn);
13418140Sralph 			}
13518140Sralph 			/* not found, save it */
13618140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
13718140Sralph 				*cpp++ = cp;
13818140Sralph 				*cpp = NULL;
13918140Sralph 			}
14018140Sralph 		}
14118140Sralph 		sp = cp++;	/* save ptr to length byte */
14218140Sralph 		do {
14318140Sralph 			if (c == '.') {
14418140Sralph 				c = *dn++;
14518140Sralph 				break;
14618140Sralph 			}
14718342Sralph 			if (c == '\\') {
14818342Sralph 				if ((c = *dn++) == '\0')
14918342Sralph 					break;
15018342Sralph 			}
15139788Sbloom 			if (cp >= eob) {
15239788Sbloom 				if (msg != NULL)
15339788Sbloom 					*lpp = NULL;
15418140Sralph 				return (-1);
15539788Sbloom 			}
15618140Sralph 			*cp++ = c;
15718140Sralph 		} while ((c = *dn++) != '\0');
15818342Sralph 		/* catch trailing '.'s but not '..' */
15918342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
16018342Sralph 			cp--;
16118342Sralph 			break;
16218342Sralph 		}
16339788Sbloom 		if (l <= 0 || l > MAXLABEL) {
16439788Sbloom 			if (msg != NULL)
16539788Sbloom 				*lpp = NULL;
16618140Sralph 			return (-1);
16739788Sbloom 		}
16818140Sralph 		*sp = l;
16918140Sralph 	}
17039788Sbloom 	if (cp >= eob) {
17139788Sbloom 		if (msg != NULL)
17239788Sbloom 			*lpp = NULL;
17318140Sralph 		return (-1);
17439788Sbloom 	}
17518140Sralph 	*cp++ = '\0';
17618140Sralph 	return (cp - comp_dn);
17718140Sralph }
17818140Sralph 
17918140Sralph /*
18018342Sralph  * Skip over a compressed domain name. Return the size or -1.
18118140Sralph  */
18232647Skarels dn_skipname(comp_dn, eom)
18332647Skarels 	u_char *comp_dn, *eom;
18418140Sralph {
18532647Skarels 	register u_char *cp;
18618140Sralph 	register int n;
18718140Sralph 
18818342Sralph 	cp = comp_dn;
18932647Skarels 	while (cp < eom && (n = *cp++)) {
19018140Sralph 		/*
19118140Sralph 		 * check for indirection
19218140Sralph 		 */
19318140Sralph 		switch (n & INDIR_MASK) {
19418140Sralph 		case 0:		/* normal case, n == len */
19518140Sralph 			cp += n;
19618140Sralph 			continue;
19718140Sralph 		default:	/* illegal type */
19818140Sralph 			return (-1);
19918140Sralph 		case INDIR_MASK:	/* indirection */
20018140Sralph 			cp++;
20118140Sralph 		}
20218140Sralph 		break;
20318140Sralph 	}
20418342Sralph 	return (cp - comp_dn);
20518140Sralph }
20618140Sralph 
20718140Sralph /*
20818140Sralph  * Search for expanded name from a list of previously compressed names.
20918140Sralph  * Return the offset from msg if found or -1.
21033617Skarels  * dnptrs is the pointer to the first name on the list,
21133617Skarels  * not the pointer to the start of the message.
21218140Sralph  */
21333617Skarels static
21418140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
21532647Skarels 	u_char *exp_dn, *msg;
21632647Skarels 	u_char **dnptrs, **lastdnptr;
21718140Sralph {
21832647Skarels 	register u_char *dn, *cp, **cpp;
21918140Sralph 	register int n;
22032647Skarels 	u_char *sp;
22118140Sralph 
22233617Skarels 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
22318140Sralph 		dn = exp_dn;
22418140Sralph 		sp = cp = *cpp;
22518140Sralph 		while (n = *cp++) {
22618140Sralph 			/*
22718140Sralph 			 * check for indirection
22818140Sralph 			 */
22918140Sralph 			switch (n & INDIR_MASK) {
23018140Sralph 			case 0:		/* normal case, n == len */
23118342Sralph 				while (--n >= 0) {
23239788Sbloom 					if (*dn == '.')
23339788Sbloom 						goto next;
23418342Sralph 					if (*dn == '\\')
23518342Sralph 						dn++;
23618140Sralph 					if (*dn++ != *cp++)
23718140Sralph 						goto next;
23818342Sralph 				}
23918140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
24018140Sralph 					return (sp - msg);
24118140Sralph 				if (n == '.')
24218140Sralph 					continue;
24318140Sralph 				goto next;
24418140Sralph 
24518140Sralph 			default:	/* illegal type */
24618140Sralph 				return (-1);
24718140Sralph 
24818140Sralph 			case INDIR_MASK:	/* indirection */
24932647Skarels 				cp = msg + (((n & 0x3f) << 8) | *cp);
25018140Sralph 			}
25118140Sralph 		}
25218140Sralph 		if (*dn == '\0')
25318140Sralph 			return (sp - msg);
25418140Sralph 	next:	;
25518140Sralph 	}
25618140Sralph 	return (-1);
25718140Sralph }
25818528Sralph 
25918528Sralph /*
26018528Sralph  * Routines to insert/extract short/long's. Must account for byte
26118528Sralph  * order and non-alignment problems. This code at least has the
26218528Sralph  * advantage of being portable.
26333727Sbostic  *
26433727Sbostic  * used by sendmail.
26518528Sralph  */
26618528Sralph 
26718528Sralph u_short
26830441Skjd _getshort(msgp)
26932647Skarels 	u_char *msgp;
27018528Sralph {
27118528Sralph 	register u_char *p = (u_char *) msgp;
27225360Sbloom #ifdef vax
27325360Sbloom 	/*
27425360Sbloom 	 * vax compiler doesn't put shorts in registers
27525360Sbloom 	 */
27625360Sbloom 	register u_long u;
27725360Sbloom #else
27824735Sbloom 	register u_short u;
27925360Sbloom #endif
28018528Sralph 
28124735Sbloom 	u = *p++ << 8;
28225360Sbloom 	return ((u_short)(u | *p));
28318528Sralph }
28418528Sralph 
28518528Sralph u_long
28630441Skjd _getlong(msgp)
28732647Skarels 	u_char *msgp;
28818528Sralph {
28918528Sralph 	register u_char *p = (u_char *) msgp;
29023870Skjd 	register u_long u;
29118528Sralph 
29223870Skjd 	u = *p++; u <<= 8;
29323870Skjd 	u |= *p++; u <<= 8;
29423870Skjd 	u |= *p++; u <<= 8;
29523870Skjd 	return (u | *p);
29618528Sralph }
29718528Sralph 
29823870Skjd 
29918528Sralph putshort(s, msgp)
30018528Sralph 	register u_short s;
30132647Skarels 	register u_char *msgp;
30218528Sralph {
30318528Sralph 
30418528Sralph 	msgp[1] = s;
30518528Sralph 	msgp[0] = s >> 8;
30618528Sralph }
30718528Sralph 
30818528Sralph putlong(l, msgp)
30918528Sralph 	register u_long l;
31032647Skarels 	register u_char *msgp;
31118528Sralph {
31218528Sralph 
31318528Sralph 	msgp[3] = l;
31418528Sralph 	msgp[2] = (l >>= 8);
31518528Sralph 	msgp[1] = (l >>= 8);
31618528Sralph 	msgp[0] = l >> 8;
31718528Sralph }
318