xref: /csrg-svn/lib/libc/net/res_comp.c (revision 33727)
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
633679Sbostic  * provided that this notice is preserved and that due credit is given
733679Sbostic  * to the University of California at Berkeley. The name of the University
833679Sbostic  * may not be used to endorse or promote products derived from this
933679Sbostic  * software without specific prior written permission. This software
1033679Sbostic  * is provided ``as is'' without express or implied warranty.
1118546Sralph  */
1218546Sralph 
1326631Sdonn #if defined(LIBC_SCCS) && !defined(lint)
14*33727Sbostic static char sccsid[] = "@(#)res_comp.c	6.13 (Berkeley) 03/13/88";
1533679Sbostic #endif /* LIBC_SCCS and not lint */
1621384Sdist 
1718140Sralph #include <sys/types.h>
1818140Sralph #include <stdio.h>
1924079Skjd #include <arpa/nameser.h>
2018140Sralph 
2123870Skjd 
2218140Sralph /*
2318342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
2418342Sralph  * 'msg' is a pointer to the begining of the message,
2526112Skarels  * 'eomorig' points to the first location after the message,
2618342Sralph  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
2718140Sralph  * Return size of compressed name or -1 if there was an error.
2818140Sralph  */
2926112Skarels dn_expand(msg, eomorig, comp_dn, exp_dn, length)
3032647Skarels 	u_char *msg, *eomorig, *comp_dn, *exp_dn;
3126112Skarels 	int length;
3218140Sralph {
3332647Skarels 	register u_char *cp, *dn;
3418140Sralph 	register int n, c;
3532647Skarels 	u_char *eom;
3631686Skarels 	int len = -1, checked = 0;
3718140Sralph 
3818140Sralph 	dn = exp_dn;
3918140Sralph 	cp = comp_dn;
4018140Sralph 	eom = exp_dn + length - 1;
4118140Sralph 	/*
4218140Sralph 	 * fetch next label in domain name
4318140Sralph 	 */
4418140Sralph 	while (n = *cp++) {
4518140Sralph 		/*
4618140Sralph 		 * Check for indirection
4718140Sralph 		 */
4818140Sralph 		switch (n & INDIR_MASK) {
4918140Sralph 		case 0:
5018342Sralph 			if (dn != exp_dn) {
5118342Sralph 				if (dn >= eom)
5218342Sralph 					return (-1);
5318140Sralph 				*dn++ = '.';
5418342Sralph 			}
5518140Sralph 			if (dn+n >= eom)
5618140Sralph 				return (-1);
5731686Skarels 			checked += n + 1;
5826064Skjd 			while (--n >= 0) {
5926812Sbloom 				if ((c = *cp++) == '.') {
6026812Sbloom 					if (dn+n+1 >= eom)
6126812Sbloom 						return (-1);
6226812Sbloom 					*dn++ = '\\';
6318342Sralph 				}
6426812Sbloom 				*dn++ = c;
6526112Skarels 				if (cp >= eomorig)	/* out of range */
6626064Skjd 					return(-1);
6726064Skjd 			}
6818140Sralph 			break;
6918140Sralph 
7018140Sralph 		case INDIR_MASK:
7118342Sralph 			if (len < 0)
7218140Sralph 				len = cp - comp_dn + 1;
7318140Sralph 			cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
7426112Skarels 			if (cp < msg || cp >= eomorig)	/* out of range */
7526064Skjd 				return(-1);
7631686Skarels 			checked += 2;
7731686Skarels 			/*
7831686Skarels 			 * Check for loops in the compressed name;
7931686Skarels 			 * if we've looked at the whole message,
8031686Skarels 			 * there must be a loop.
8131686Skarels 			 */
8231686Skarels 			if (checked >= eomorig - msg)
8331686Skarels 				return (-1);
8418140Sralph 			break;
8518140Sralph 
8618140Sralph 		default:
8718140Sralph 			return (-1);			/* flag error */
8818140Sralph 		}
8918140Sralph 	}
9018140Sralph 	*dn = '\0';
9118342Sralph 	if (len < 0)
9218140Sralph 		len = cp - comp_dn;
9318140Sralph 	return (len);
9418140Sralph }
9518140Sralph 
9618140Sralph /*
9718342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
9818342Sralph  * Return the size of the compressed name or -1.
9918342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
10018342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
10118140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
10218342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
10318342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
10418342Sralph  * labels inserted into the message as we compress the name.
10518342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
10618342Sralph  * is NULL, we don't update the list.
10718140Sralph  */
10818140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
10932647Skarels 	u_char *exp_dn, *comp_dn;
11018140Sralph 	int length;
11132647Skarels 	u_char **dnptrs, **lastdnptr;
11218140Sralph {
11332647Skarels 	register u_char *cp, *dn;
11418140Sralph 	register int c, l;
11532647Skarels 	u_char **cpp, **lpp, *sp, *eob;
11632647Skarels 	u_char *msg;
11718140Sralph 
11818140Sralph 	dn = exp_dn;
11918140Sralph 	cp = comp_dn;
12025360Sbloom 	eob = cp + length;
12118140Sralph 	if (dnptrs != NULL) {
12218140Sralph 		if ((msg = *dnptrs++) != NULL) {
12318140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
12418140Sralph 				;
12518140Sralph 			lpp = cpp;	/* end of list to search */
12618140Sralph 		}
12718140Sralph 	} else
12818140Sralph 		msg = NULL;
12918140Sralph 	for (c = *dn++; c != '\0'; ) {
13018140Sralph 		/* look to see if we can use pointers */
13118140Sralph 		if (msg != NULL) {
13218140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
13318140Sralph 				if (cp+1 >= eob)
13418140Sralph 					return (-1);
13518140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
13626240Skjd 				*cp++ = l % 256;
13718140Sralph 				return (cp - comp_dn);
13818140Sralph 			}
13918140Sralph 			/* not found, save it */
14018140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
14118140Sralph 				*cpp++ = cp;
14218140Sralph 				*cpp = NULL;
14318140Sralph 			}
14418140Sralph 		}
14518140Sralph 		sp = cp++;	/* save ptr to length byte */
14618140Sralph 		do {
14718140Sralph 			if (c == '.') {
14818140Sralph 				c = *dn++;
14918140Sralph 				break;
15018140Sralph 			}
15118342Sralph 			if (c == '\\') {
15218342Sralph 				if ((c = *dn++) == '\0')
15318342Sralph 					break;
15418342Sralph 			}
15518140Sralph 			if (cp >= eob)
15618140Sralph 				return (-1);
15718140Sralph 			*cp++ = c;
15818140Sralph 		} while ((c = *dn++) != '\0');
15918342Sralph 		/* catch trailing '.'s but not '..' */
16018342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
16118342Sralph 			cp--;
16218342Sralph 			break;
16318342Sralph 		}
16418342Sralph 		if (l <= 0 || l > MAXLABEL)
16518140Sralph 			return (-1);
16618140Sralph 		*sp = l;
16718140Sralph 	}
16818140Sralph 	if (cp >= eob)
16918140Sralph 		return (-1);
17018140Sralph 	*cp++ = '\0';
17118140Sralph 	return (cp - comp_dn);
17218140Sralph }
17318140Sralph 
17418140Sralph /*
17518342Sralph  * Skip over a compressed domain name. Return the size or -1.
17618140Sralph  */
17732647Skarels dn_skipname(comp_dn, eom)
17832647Skarels 	u_char *comp_dn, *eom;
17918140Sralph {
18032647Skarels 	register u_char *cp;
18118140Sralph 	register int n;
18218140Sralph 
18318342Sralph 	cp = comp_dn;
18432647Skarels 	while (cp < eom && (n = *cp++)) {
18518140Sralph 		/*
18618140Sralph 		 * check for indirection
18718140Sralph 		 */
18818140Sralph 		switch (n & INDIR_MASK) {
18918140Sralph 		case 0:		/* normal case, n == len */
19018140Sralph 			cp += n;
19118140Sralph 			continue;
19218140Sralph 		default:	/* illegal type */
19318140Sralph 			return (-1);
19418140Sralph 		case INDIR_MASK:	/* indirection */
19518140Sralph 			cp++;
19618140Sralph 		}
19718140Sralph 		break;
19818140Sralph 	}
19918342Sralph 	return (cp - comp_dn);
20018140Sralph }
20118140Sralph 
20218140Sralph /*
20318140Sralph  * Search for expanded name from a list of previously compressed names.
20418140Sralph  * Return the offset from msg if found or -1.
20533617Skarels  * dnptrs is the pointer to the first name on the list,
20633617Skarels  * not the pointer to the start of the message.
20718140Sralph  */
20833617Skarels static
20918140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
21032647Skarels 	u_char *exp_dn, *msg;
21132647Skarels 	u_char **dnptrs, **lastdnptr;
21218140Sralph {
21332647Skarels 	register u_char *dn, *cp, **cpp;
21418140Sralph 	register int n;
21532647Skarels 	u_char *sp;
21618140Sralph 
21733617Skarels 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
21818140Sralph 		dn = exp_dn;
21918140Sralph 		sp = cp = *cpp;
22018140Sralph 		while (n = *cp++) {
22118140Sralph 			/*
22218140Sralph 			 * check for indirection
22318140Sralph 			 */
22418140Sralph 			switch (n & INDIR_MASK) {
22518140Sralph 			case 0:		/* normal case, n == len */
22618342Sralph 				while (--n >= 0) {
22718342Sralph 					if (*dn == '\\')
22818342Sralph 						dn++;
22918140Sralph 					if (*dn++ != *cp++)
23018140Sralph 						goto next;
23118342Sralph 				}
23218140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
23318140Sralph 					return (sp - msg);
23418140Sralph 				if (n == '.')
23518140Sralph 					continue;
23618140Sralph 				goto next;
23718140Sralph 
23818140Sralph 			default:	/* illegal type */
23918140Sralph 				return (-1);
24018140Sralph 
24118140Sralph 			case INDIR_MASK:	/* indirection */
24232647Skarels 				cp = msg + (((n & 0x3f) << 8) | *cp);
24318140Sralph 			}
24418140Sralph 		}
24518140Sralph 		if (*dn == '\0')
24618140Sralph 			return (sp - msg);
24718140Sralph 	next:	;
24818140Sralph 	}
24918140Sralph 	return (-1);
25018140Sralph }
25118528Sralph 
25218528Sralph /*
25318528Sralph  * Routines to insert/extract short/long's. Must account for byte
25418528Sralph  * order and non-alignment problems. This code at least has the
25518528Sralph  * advantage of being portable.
256*33727Sbostic  *
257*33727Sbostic  * used by sendmail.
25818528Sralph  */
25918528Sralph 
26018528Sralph u_short
26130441Skjd _getshort(msgp)
26232647Skarels 	u_char *msgp;
26318528Sralph {
26418528Sralph 	register u_char *p = (u_char *) msgp;
26525360Sbloom #ifdef vax
26625360Sbloom 	/*
26725360Sbloom 	 * vax compiler doesn't put shorts in registers
26825360Sbloom 	 */
26925360Sbloom 	register u_long u;
27025360Sbloom #else
27124735Sbloom 	register u_short u;
27225360Sbloom #endif
27318528Sralph 
27424735Sbloom 	u = *p++ << 8;
27525360Sbloom 	return ((u_short)(u | *p));
27618528Sralph }
27718528Sralph 
27818528Sralph u_long
27930441Skjd _getlong(msgp)
28032647Skarels 	u_char *msgp;
28118528Sralph {
28218528Sralph 	register u_char *p = (u_char *) msgp;
28323870Skjd 	register u_long u;
28418528Sralph 
28523870Skjd 	u = *p++; u <<= 8;
28623870Skjd 	u |= *p++; u <<= 8;
28723870Skjd 	u |= *p++; u <<= 8;
28823870Skjd 	return (u | *p);
28918528Sralph }
29018528Sralph 
29123870Skjd 
29218528Sralph putshort(s, msgp)
29318528Sralph 	register u_short s;
29432647Skarels 	register u_char *msgp;
29518528Sralph {
29618528Sralph 
29718528Sralph 	msgp[1] = s;
29818528Sralph 	msgp[0] = s >> 8;
29918528Sralph }
30018528Sralph 
30118528Sralph putlong(l, msgp)
30218528Sralph 	register u_long l;
30332647Skarels 	register u_char *msgp;
30418528Sralph {
30518528Sralph 
30618528Sralph 	msgp[3] = l;
30718528Sralph 	msgp[2] = (l >>= 8);
30818528Sralph 	msgp[1] = (l >>= 8);
30918528Sralph 	msgp[0] = l >> 8;
31018528Sralph }
311