xref: /csrg-svn/lib/libc/net/res_comp.c (revision 47045)
118546Sralph /*
221384Sdist  * Copyright (c) 1985 Regents of the University of California.
333679Sbostic  * All rights reserved.
433679Sbostic  *
542627Sbostic  * %sccs.include.redist.c%
618546Sralph  */
718546Sralph 
826631Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*47045Sbostic static char sccsid[] = "@(#)res_comp.c	6.21 (Berkeley) 03/06/91";
1033679Sbostic #endif /* LIBC_SCCS and not lint */
1121384Sdist 
1218140Sralph #include <sys/types.h>
1346604Sbostic #include <arpa/nameser.h>
1446604Sbostic #include <netinet/in.h>
1546604Sbostic #include <resolv.h>
1618140Sralph #include <stdio.h>
1718140Sralph 
1841093Sleres static dn_find();
1941093Sleres 
2018140Sralph /*
2118342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
2218342Sralph  * 'msg' is a pointer to the begining of the message,
2326112Skarels  * 'eomorig' points to the first location after the message,
2418342Sralph  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2518140Sralph  * Return size of compressed name or -1 if there was an error.
2618140Sralph  */
2726112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length)
2846604Sbostic 	const u_char *msg, *eomorig, *comp_dn;
2946604Sbostic 	u_char *exp_dn;
3026112Skarels 	int length;
3118140Sralph {
3232647Skarels 	register u_char *cp, *dn;
3318140Sralph 	register int n, c;
3432647Skarels 	u_char *eom;
3531686Skarels 	int len = -1, checked = 0;
3618140Sralph 
3718140Sralph 	dn = exp_dn;
3846604Sbostic 	cp = (u_char *)comp_dn;
3939788Sbloom 	eom = exp_dn + length;
4018140Sralph 	/*
4118140Sralph 	 * fetch next label in domain name
4218140Sralph 	 */
4318140Sralph 	while (n = *cp++) {
4418140Sralph 		/*
4518140Sralph 		 * Check for indirection
4618140Sralph 		 */
4718140Sralph 		switch (n & INDIR_MASK) {
4818140Sralph 		case 0:
4918342Sralph 			if (dn != exp_dn) {
5018342Sralph 				if (dn >= eom)
5118342Sralph 					return (-1);
5218140Sralph 				*dn++ = '.';
5318342Sralph 			}
5418140Sralph 			if (dn+n >= eom)
5518140Sralph 				return (-1);
5631686Skarels 			checked += n + 1;
5726064Skjd 			while (--n >= 0) {
5826812Sbloom 				if ((c = *cp++) == '.') {
5944325Sbloom 					if (dn + n + 2 >= eom)
6026812Sbloom 						return (-1);
6126812Sbloom 					*dn++ = '\\';
6218342Sralph 				}
6326812Sbloom 				*dn++ = c;
6426112Skarels 				if (cp >= eomorig)	/* out of range */
6526064Skjd 					return(-1);
6626064Skjd 			}
6718140Sralph 			break;
6818140Sralph 
6918140Sralph 		case INDIR_MASK:
7018342Sralph 			if (len < 0)
7118140Sralph 				len = cp - comp_dn + 1;
7246604Sbostic 			cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff));
7326112Skarels 			if (cp < msg || cp >= eomorig)	/* out of range */
7426064Skjd 				return(-1);
7531686Skarels 			checked += 2;
7631686Skarels 			/*
7731686Skarels 			 * Check for loops in the compressed name;
7831686Skarels 			 * if we've looked at the whole message,
7931686Skarels 			 * there must be a loop.
8031686Skarels 			 */
8131686Skarels 			if (checked >= eomorig - msg)
8231686Skarels 				return (-1);
8318140Sralph 			break;
8418140Sralph 
8518140Sralph 		default:
8618140Sralph 			return (-1);			/* flag error */
8718140Sralph 		}
8818140Sralph 	}
8918140Sralph 	*dn = '\0';
9018342Sralph 	if (len < 0)
9118140Sralph 		len = cp - comp_dn;
9218140Sralph 	return (len);
9318140Sralph }
9418140Sralph 
9518140Sralph /*
9618342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
9718342Sralph  * Return the size of the compressed name or -1.
9818342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
9918342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
10018140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
10118342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
10218342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
10318342Sralph  * labels inserted into the message as we compress the name.
10418342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
10518342Sralph  * is NULL, we don't update the list.
10618140Sralph  */
10718140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
10846604Sbostic 	const u_char *exp_dn;
10946604Sbostic 	u_char *comp_dn, **dnptrs, **lastdnptr;
11018140Sralph 	int length;
11118140Sralph {
11232647Skarels 	register u_char *cp, *dn;
11318140Sralph 	register int c, l;
11432647Skarels 	u_char **cpp, **lpp, *sp, *eob;
11532647Skarels 	u_char *msg;
11618140Sralph 
11746604Sbostic 	dn = (u_char *)exp_dn;
11818140Sralph 	cp = comp_dn;
11925360Sbloom 	eob = cp + length;
12018140Sralph 	if (dnptrs != NULL) {
12118140Sralph 		if ((msg = *dnptrs++) != NULL) {
12218140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
12318140Sralph 				;
12418140Sralph 			lpp = cpp;	/* end of list to search */
12518140Sralph 		}
12618140Sralph 	} else
12718140Sralph 		msg = NULL;
12818140Sralph 	for (c = *dn++; c != '\0'; ) {
12918140Sralph 		/* look to see if we can use pointers */
13018140Sralph 		if (msg != NULL) {
13118140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
13218140Sralph 				if (cp+1 >= eob)
13318140Sralph 					return (-1);
13418140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
13526240Skjd 				*cp++ = l % 256;
13618140Sralph 				return (cp - comp_dn);
13718140Sralph 			}
13818140Sralph 			/* not found, save it */
13918140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
14018140Sralph 				*cpp++ = cp;
14118140Sralph 				*cpp = NULL;
14218140Sralph 			}
14318140Sralph 		}
14418140Sralph 		sp = cp++;	/* save ptr to length byte */
14518140Sralph 		do {
14618140Sralph 			if (c == '.') {
14718140Sralph 				c = *dn++;
14818140Sralph 				break;
14918140Sralph 			}
15018342Sralph 			if (c == '\\') {
15118342Sralph 				if ((c = *dn++) == '\0')
15218342Sralph 					break;
15318342Sralph 			}
15439788Sbloom 			if (cp >= eob) {
15539788Sbloom 				if (msg != NULL)
15639788Sbloom 					*lpp = NULL;
15718140Sralph 				return (-1);
15839788Sbloom 			}
15918140Sralph 			*cp++ = c;
16018140Sralph 		} while ((c = *dn++) != '\0');
16118342Sralph 		/* catch trailing '.'s but not '..' */
16218342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
16318342Sralph 			cp--;
16418342Sralph 			break;
16518342Sralph 		}
16639788Sbloom 		if (l <= 0 || l > MAXLABEL) {
16739788Sbloom 			if (msg != NULL)
16839788Sbloom 				*lpp = NULL;
16918140Sralph 			return (-1);
17039788Sbloom 		}
17118140Sralph 		*sp = l;
17218140Sralph 	}
17339788Sbloom 	if (cp >= eob) {
17439788Sbloom 		if (msg != NULL)
17539788Sbloom 			*lpp = NULL;
17618140Sralph 		return (-1);
17739788Sbloom 	}
17818140Sralph 	*cp++ = '\0';
17918140Sralph 	return (cp - comp_dn);
18018140Sralph }
18118140Sralph 
18218140Sralph /*
18318342Sralph  * Skip over a compressed domain name. Return the size or -1.
18418140Sralph  */
18546496Sbostic __dn_skipname(comp_dn, eom)
186*47045Sbostic 	const u_char *comp_dn, *eom;
18718140Sralph {
18832647Skarels 	register u_char *cp;
18918140Sralph 	register int n;
19018140Sralph 
191*47045Sbostic 	cp = (u_char *)comp_dn;
19232647Skarels 	while (cp < eom && (n = *cp++)) {
19318140Sralph 		/*
19418140Sralph 		 * check for indirection
19518140Sralph 		 */
19618140Sralph 		switch (n & INDIR_MASK) {
19718140Sralph 		case 0:		/* normal case, n == len */
19818140Sralph 			cp += n;
19918140Sralph 			continue;
20018140Sralph 		default:	/* illegal type */
20118140Sralph 			return (-1);
20218140Sralph 		case INDIR_MASK:	/* indirection */
20318140Sralph 			cp++;
20418140Sralph 		}
20518140Sralph 		break;
20618140Sralph 	}
20718342Sralph 	return (cp - comp_dn);
20818140Sralph }
20918140Sralph 
21018140Sralph /*
21118140Sralph  * Search for expanded name from a list of previously compressed names.
21218140Sralph  * Return the offset from msg if found or -1.
21333617Skarels  * dnptrs is the pointer to the first name on the list,
21433617Skarels  * not the pointer to the start of the message.
21518140Sralph  */
21633617Skarels static
21718140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
21832647Skarels 	u_char *exp_dn, *msg;
21932647Skarels 	u_char **dnptrs, **lastdnptr;
22018140Sralph {
22132647Skarels 	register u_char *dn, *cp, **cpp;
22218140Sralph 	register int n;
22332647Skarels 	u_char *sp;
22418140Sralph 
22533617Skarels 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
22618140Sralph 		dn = exp_dn;
22718140Sralph 		sp = cp = *cpp;
22818140Sralph 		while (n = *cp++) {
22918140Sralph 			/*
23018140Sralph 			 * check for indirection
23118140Sralph 			 */
23218140Sralph 			switch (n & INDIR_MASK) {
23318140Sralph 			case 0:		/* normal case, n == len */
23418342Sralph 				while (--n >= 0) {
23539788Sbloom 					if (*dn == '.')
23639788Sbloom 						goto next;
23718342Sralph 					if (*dn == '\\')
23818342Sralph 						dn++;
23918140Sralph 					if (*dn++ != *cp++)
24018140Sralph 						goto next;
24118342Sralph 				}
24218140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
24318140Sralph 					return (sp - msg);
24418140Sralph 				if (n == '.')
24518140Sralph 					continue;
24618140Sralph 				goto next;
24718140Sralph 
24818140Sralph 			default:	/* illegal type */
24918140Sralph 				return (-1);
25018140Sralph 
25118140Sralph 			case INDIR_MASK:	/* indirection */
25232647Skarels 				cp = msg + (((n & 0x3f) << 8) | *cp);
25318140Sralph 			}
25418140Sralph 		}
25518140Sralph 		if (*dn == '\0')
25618140Sralph 			return (sp - msg);
25718140Sralph 	next:	;
25818140Sralph 	}
25918140Sralph 	return (-1);
26018140Sralph }
26118528Sralph 
26218528Sralph /*
26318528Sralph  * Routines to insert/extract short/long's. Must account for byte
26418528Sralph  * order and non-alignment problems. This code at least has the
26518528Sralph  * advantage of being portable.
26633727Sbostic  *
26733727Sbostic  * used by sendmail.
26818528Sralph  */
26918528Sralph 
27018528Sralph u_short
27130441Skjd _getshort(msgp)
27232647Skarels 	u_char *msgp;
27318528Sralph {
27418528Sralph 	register u_char *p = (u_char *) msgp;
27525360Sbloom #ifdef vax
27625360Sbloom 	/*
27725360Sbloom 	 * vax compiler doesn't put shorts in registers
27825360Sbloom 	 */
27925360Sbloom 	register u_long u;
28025360Sbloom #else
28124735Sbloom 	register u_short u;
28225360Sbloom #endif
28318528Sralph 
28424735Sbloom 	u = *p++ << 8;
28525360Sbloom 	return ((u_short)(u | *p));
28618528Sralph }
28718528Sralph 
28818528Sralph u_long
28930441Skjd _getlong(msgp)
29032647Skarels 	u_char *msgp;
29118528Sralph {
29218528Sralph 	register u_char *p = (u_char *) msgp;
29323870Skjd 	register u_long u;
29418528Sralph 
29523870Skjd 	u = *p++; u <<= 8;
29623870Skjd 	u |= *p++; u <<= 8;
29723870Skjd 	u |= *p++; u <<= 8;
29823870Skjd 	return (u | *p);
29918528Sralph }
30018528Sralph 
301*47045Sbostic void
302*47045Sbostic #ifdef __STDC__
303*47045Sbostic __putshort(register u_short s, register u_char *msgp)
304*47045Sbostic #else
30546496Sbostic __putshort(s, msgp)
30618528Sralph 	register u_short s;
30732647Skarels 	register u_char *msgp;
308*47045Sbostic #endif
30918528Sralph {
31018528Sralph 	msgp[1] = s;
31118528Sralph 	msgp[0] = s >> 8;
31218528Sralph }
31318528Sralph 
314*47045Sbostic void
31546496Sbostic __putlong(l, msgp)
31618528Sralph 	register u_long l;
31732647Skarels 	register u_char *msgp;
31818528Sralph {
31918528Sralph 	msgp[3] = l;
32018528Sralph 	msgp[2] = (l >>= 8);
32118528Sralph 	msgp[1] = (l >>= 8);
32218528Sralph 	msgp[0] = l >> 8;
32318528Sralph }
324