xref: /csrg-svn/lib/libc/net/res_comp.c (revision 32647)
118546Sralph /*
221384Sdist  * Copyright (c) 1985 Regents of the University of California.
321384Sdist  * All rights reserved.  The Berkeley software License Agreement
421384Sdist  * specifies the terms and conditions for redistribution.
518546Sralph  */
618546Sralph 
726631Sdonn #if defined(LIBC_SCCS) && !defined(lint)
8*32647Skarels static char sccsid[] = "@(#)res_comp.c	6.10 (Berkeley) 11/19/87";
926631Sdonn #endif LIBC_SCCS and not lint
1021384Sdist 
1118140Sralph #include <sys/types.h>
1218140Sralph #include <stdio.h>
1324079Skjd #include <arpa/nameser.h>
1418140Sralph 
1523870Skjd 
1618140Sralph /*
1718342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
1818342Sralph  * 'msg' is a pointer to the begining of the message,
1926112Skarels  * 'eomorig' points to the first location after the message,
2018342Sralph  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2118140Sralph  * Return size of compressed name or -1 if there was an error.
2218140Sralph  */
2326112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length)
24*32647Skarels 	u_char *msg, *eomorig, *comp_dn, *exp_dn;
2526112Skarels 	int length;
2618140Sralph {
27*32647Skarels 	register u_char *cp, *dn;
2818140Sralph 	register int n, c;
29*32647Skarels 	u_char *eom;
3031686Skarels 	int len = -1, checked = 0;
3118140Sralph 
3218140Sralph 	dn = exp_dn;
3318140Sralph 	cp = comp_dn;
3418140Sralph 	eom = exp_dn + length - 1;
3518140Sralph 	/*
3618140Sralph 	 * fetch next label in domain name
3718140Sralph 	 */
3818140Sralph 	while (n = *cp++) {
3918140Sralph 		/*
4018140Sralph 		 * Check for indirection
4118140Sralph 		 */
4218140Sralph 		switch (n & INDIR_MASK) {
4318140Sralph 		case 0:
4418342Sralph 			if (dn != exp_dn) {
4518342Sralph 				if (dn >= eom)
4618342Sralph 					return (-1);
4718140Sralph 				*dn++ = '.';
4818342Sralph 			}
4918140Sralph 			if (dn+n >= eom)
5018140Sralph 				return (-1);
5131686Skarels 			checked += n + 1;
5226064Skjd 			while (--n >= 0) {
5326812Sbloom 				if ((c = *cp++) == '.') {
5426812Sbloom 					if (dn+n+1 >= eom)
5526812Sbloom 						return (-1);
5626812Sbloom 					*dn++ = '\\';
5718342Sralph 				}
5826812Sbloom 				*dn++ = c;
5926112Skarels 				if (cp >= eomorig)	/* out of range */
6026064Skjd 					return(-1);
6126064Skjd 			}
6218140Sralph 			break;
6318140Sralph 
6418140Sralph 		case INDIR_MASK:
6518342Sralph 			if (len < 0)
6618140Sralph 				len = cp - comp_dn + 1;
6718140Sralph 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
6826112Skarels 			if (cp < msg || cp >= eomorig)	/* out of range */
6926064Skjd 				return(-1);
7031686Skarels 			checked += 2;
7131686Skarels 			/*
7231686Skarels 			 * Check for loops in the compressed name;
7331686Skarels 			 * if we've looked at the whole message,
7431686Skarels 			 * there must be a loop.
7531686Skarels 			 */
7631686Skarels 			if (checked >= eomorig - msg)
7731686Skarels 				return (-1);
7818140Sralph 			break;
7918140Sralph 
8018140Sralph 		default:
8118140Sralph 			return (-1);			/* flag error */
8218140Sralph 		}
8318140Sralph 	}
8418140Sralph 	*dn = '\0';
8518342Sralph 	if (len < 0)
8618140Sralph 		len = cp - comp_dn;
8718140Sralph 	return (len);
8818140Sralph }
8918140Sralph 
9018140Sralph /*
9118342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
9218342Sralph  * Return the size of the compressed name or -1.
9318342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
9418342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
9518140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
9618342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
9718342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
9818342Sralph  * labels inserted into the message as we compress the name.
9918342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
10018342Sralph  * is NULL, we don't update the list.
10118140Sralph  */
10218140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
103*32647Skarels 	u_char *exp_dn, *comp_dn;
10418140Sralph 	int length;
105*32647Skarels 	u_char **dnptrs, **lastdnptr;
10618140Sralph {
107*32647Skarels 	register u_char *cp, *dn;
10818140Sralph 	register int c, l;
109*32647Skarels 	u_char **cpp, **lpp, *sp, *eob;
110*32647Skarels 	u_char *msg;
11118140Sralph 
11218140Sralph 	dn = exp_dn;
11318140Sralph 	cp = comp_dn;
11425360Sbloom 	eob = cp + length;
11518140Sralph 	if (dnptrs != NULL) {
11618140Sralph 		if ((msg = *dnptrs++) != NULL) {
11718140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
11818140Sralph 				;
11918140Sralph 			lpp = cpp;	/* end of list to search */
12018140Sralph 		}
12118140Sralph 	} else
12218140Sralph 		msg = NULL;
12318140Sralph 	for (c = *dn++; c != '\0'; ) {
12418140Sralph 		/* look to see if we can use pointers */
12518140Sralph 		if (msg != NULL) {
12618140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
12718140Sralph 				if (cp+1 >= eob)
12818140Sralph 					return (-1);
12918140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
13026240Skjd 				*cp++ = l % 256;
13118140Sralph 				return (cp - comp_dn);
13218140Sralph 			}
13318140Sralph 			/* not found, save it */
13418140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
13518140Sralph 				*cpp++ = cp;
13618140Sralph 				*cpp = NULL;
13718140Sralph 			}
13818140Sralph 		}
13918140Sralph 		sp = cp++;	/* save ptr to length byte */
14018140Sralph 		do {
14118140Sralph 			if (c == '.') {
14218140Sralph 				c = *dn++;
14318140Sralph 				break;
14418140Sralph 			}
14518342Sralph 			if (c == '\\') {
14618342Sralph 				if ((c = *dn++) == '\0')
14718342Sralph 					break;
14818342Sralph 			}
14918140Sralph 			if (cp >= eob)
15018140Sralph 				return (-1);
15118140Sralph 			*cp++ = c;
15218140Sralph 		} while ((c = *dn++) != '\0');
15318342Sralph 		/* catch trailing '.'s but not '..' */
15418342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
15518342Sralph 			cp--;
15618342Sralph 			break;
15718342Sralph 		}
15818342Sralph 		if (l <= 0 || l > MAXLABEL)
15918140Sralph 			return (-1);
16018140Sralph 		*sp = l;
16118140Sralph 	}
16218140Sralph 	if (cp >= eob)
16318140Sralph 		return (-1);
16418140Sralph 	*cp++ = '\0';
16518140Sralph 	return (cp - comp_dn);
16618140Sralph }
16718140Sralph 
16818140Sralph /*
16918342Sralph  * Skip over a compressed domain name. Return the size or -1.
17018140Sralph  */
171*32647Skarels dn_skipname(comp_dn, eom)
172*32647Skarels 	u_char *comp_dn, *eom;
17318140Sralph {
174*32647Skarels 	register u_char *cp;
17518140Sralph 	register int n;
17618140Sralph 
17718342Sralph 	cp = comp_dn;
178*32647Skarels 	while (cp < eom && (n = *cp++)) {
17918140Sralph 		/*
18018140Sralph 		 * check for indirection
18118140Sralph 		 */
18218140Sralph 		switch (n & INDIR_MASK) {
18318140Sralph 		case 0:		/* normal case, n == len */
18418140Sralph 			cp += n;
18518140Sralph 			continue;
18618140Sralph 		default:	/* illegal type */
18718140Sralph 			return (-1);
18818140Sralph 		case INDIR_MASK:	/* indirection */
18918140Sralph 			cp++;
19018140Sralph 		}
19118140Sralph 		break;
19218140Sralph 	}
19318342Sralph 	return (cp - comp_dn);
19418140Sralph }
19518140Sralph 
19618140Sralph /*
19718140Sralph  * Search for expanded name from a list of previously compressed names.
19818140Sralph  * Return the offset from msg if found or -1.
19918140Sralph  */
20018140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
201*32647Skarels 	u_char *exp_dn, *msg;
202*32647Skarels 	u_char **dnptrs, **lastdnptr;
20318140Sralph {
204*32647Skarels 	register u_char *dn, *cp, **cpp;
20518140Sralph 	register int n;
206*32647Skarels 	u_char *sp;
20718140Sralph 
20826240Skjd 	for (cpp = dnptrs + 1; cpp < lastdnptr; cpp++) {
20918140Sralph 		dn = exp_dn;
21018140Sralph 		sp = cp = *cpp;
21118140Sralph 		while (n = *cp++) {
21218140Sralph 			/*
21318140Sralph 			 * check for indirection
21418140Sralph 			 */
21518140Sralph 			switch (n & INDIR_MASK) {
21618140Sralph 			case 0:		/* normal case, n == len */
21718342Sralph 				while (--n >= 0) {
21818342Sralph 					if (*dn == '\\')
21918342Sralph 						dn++;
22018140Sralph 					if (*dn++ != *cp++)
22118140Sralph 						goto next;
22218342Sralph 				}
22318140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
22418140Sralph 					return (sp - msg);
22518140Sralph 				if (n == '.')
22618140Sralph 					continue;
22718140Sralph 				goto next;
22818140Sralph 
22918140Sralph 			default:	/* illegal type */
23018140Sralph 				return (-1);
23118140Sralph 
23218140Sralph 			case INDIR_MASK:	/* indirection */
233*32647Skarels 				cp = msg + (((n & 0x3f) << 8) | *cp);
23418140Sralph 			}
23518140Sralph 		}
23618140Sralph 		if (*dn == '\0')
23718140Sralph 			return (sp - msg);
23818140Sralph 	next:	;
23918140Sralph 	}
24018140Sralph 	return (-1);
24118140Sralph }
24218528Sralph 
24318528Sralph /*
24418528Sralph  * Routines to insert/extract short/long's. Must account for byte
24518528Sralph  * order and non-alignment problems. This code at least has the
24618528Sralph  * advantage of being portable.
24718528Sralph  */
24818528Sralph 
24918528Sralph u_short
25030441Skjd _getshort(msgp)
251*32647Skarels 	u_char *msgp;
25218528Sralph {
25318528Sralph 	register u_char *p = (u_char *) msgp;
25425360Sbloom #ifdef vax
25525360Sbloom 	/*
25625360Sbloom 	 * vax compiler doesn't put shorts in registers
25725360Sbloom 	 */
25825360Sbloom 	register u_long u;
25925360Sbloom #else
26024735Sbloom 	register u_short u;
26125360Sbloom #endif
26218528Sralph 
26324735Sbloom 	u = *p++ << 8;
26425360Sbloom 	return ((u_short)(u | *p));
26518528Sralph }
26618528Sralph 
26718528Sralph u_long
26830441Skjd _getlong(msgp)
269*32647Skarels 	u_char *msgp;
27018528Sralph {
27118528Sralph 	register u_char *p = (u_char *) msgp;
27223870Skjd 	register u_long u;
27318528Sralph 
27423870Skjd 	u = *p++; u <<= 8;
27523870Skjd 	u |= *p++; u <<= 8;
27623870Skjd 	u |= *p++; u <<= 8;
27723870Skjd 	return (u | *p);
27818528Sralph }
27918528Sralph 
28023870Skjd 
28118528Sralph putshort(s, msgp)
28218528Sralph 	register u_short s;
283*32647Skarels 	register u_char *msgp;
28418528Sralph {
28518528Sralph 
28618528Sralph 	msgp[1] = s;
28718528Sralph 	msgp[0] = s >> 8;
28818528Sralph }
28918528Sralph 
29018528Sralph putlong(l, msgp)
29118528Sralph 	register u_long l;
292*32647Skarels 	register u_char *msgp;
29318528Sralph {
29418528Sralph 
29518528Sralph 	msgp[3] = l;
29618528Sralph 	msgp[2] = (l >>= 8);
29718528Sralph 	msgp[1] = (l >>= 8);
29818528Sralph 	msgp[0] = l >> 8;
29918528Sralph }
300