xref: /csrg-svn/lib/libc/net/res_comp.c (revision 21384)
118546Sralph /*
2*21384Sdist  * Copyright (c) 1985 Regents of the University of California.
3*21384Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21384Sdist  * specifies the terms and conditions for redistribution.
518546Sralph  */
618546Sralph 
7*21384Sdist #ifndef lint
8*21384Sdist static char sccsid[] = "@(#)res_comp.c	5.1 (Berkeley) 05/30/85";
9*21384Sdist #endif not lint
10*21384Sdist 
1118140Sralph #include <sys/types.h>
1218140Sralph #include <stdio.h>
1318140Sralph #include <ctype.h>
1418140Sralph #include <nameser.h>
1518140Sralph 
1618140Sralph /*
1718342Sralph  * Expand compressed domain name 'comp_dn' to full domain name.
1818342Sralph  * Expanded names are converted to upper case.
1918342Sralph  * 'msg' is a pointer to the begining of 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  */
2318140Sralph dn_expand(msg, comp_dn, exp_dn, length)
2418140Sralph 	char *msg, *comp_dn, *exp_dn;
2518140Sralph 	int length;
2618140Sralph {
2718140Sralph 	register char *cp, *dn;
2818140Sralph 	register int n, c;
2918140Sralph 	char *eom;
3018342Sralph 	int len = -1;
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);
5118140Sralph 			while (--n >= 0)
5218140Sralph 				if (islower(c = *cp++))
5318140Sralph 					*dn++ = toupper(c);
5418342Sralph 				else {
5518342Sralph 					if (c == '.') {
5618342Sralph 						if (dn+n+1 >= eom)
5718342Sralph 							return (-1);
5818342Sralph 						*dn++ = '\\';
5918342Sralph 					}
6018140Sralph 					*dn++ = c;
6118342Sralph 				}
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));
6818140Sralph 			break;
6918140Sralph 
7018140Sralph 		default:
7118140Sralph 			return (-1);			/* flag error */
7218140Sralph 		}
7318140Sralph 	}
7418140Sralph 	*dn = '\0';
7518342Sralph 	if (len < 0)
7618140Sralph 		len = cp - comp_dn;
7718140Sralph 	return (len);
7818140Sralph }
7918140Sralph 
8018140Sralph /*
8118342Sralph  * Compress domain name 'exp_dn' into 'comp_dn'.
8218342Sralph  * Return the size of the compressed name or -1.
8318342Sralph  * 'length' is the size of the array pointed to by 'comp_dn'.
8418342Sralph  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
8518140Sralph  * is a pointer to the beginning of the message. The list ends with NULL.
8618342Sralph  * 'lastdnptr' is a pointer to the end of the arrary pointed to
8718342Sralph  * by 'dnptrs'. Side effect is to update the list of pointers for
8818342Sralph  * labels inserted into the message as we compress the name.
8918342Sralph  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
9018342Sralph  * is NULL, we don't update the list.
9118140Sralph  */
9218140Sralph dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
9318140Sralph 	char *exp_dn, *comp_dn;
9418140Sralph 	int length;
9518140Sralph 	char **dnptrs, **lastdnptr;
9618140Sralph {
9718140Sralph 	register char *cp, *dn;
9818140Sralph 	register int c, l;
9918140Sralph 	char **cpp, **lpp, *sp, *eob;
10018140Sralph 	char *msg;
10118140Sralph 
10218140Sralph 	dn = exp_dn;
10318140Sralph 	cp = comp_dn;
10418140Sralph 	eob = comp_dn + length;
10518140Sralph 	if (dnptrs != NULL) {
10618140Sralph 		if ((msg = *dnptrs++) != NULL) {
10718140Sralph 			for (cpp = dnptrs; *cpp != NULL; cpp++)
10818140Sralph 				;
10918140Sralph 			lpp = cpp;	/* end of list to search */
11018140Sralph 		}
11118140Sralph 	} else
11218140Sralph 		msg = NULL;
11318140Sralph 	for (c = *dn++; c != '\0'; ) {
11418140Sralph 		/* look to see if we can use pointers */
11518140Sralph 		if (msg != NULL) {
11618140Sralph 			if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
11718140Sralph 				if (cp+1 >= eob)
11818140Sralph 					return (-1);
11918140Sralph 				*cp++ = (l >> 8) | INDIR_MASK;
12018140Sralph 				*cp++ = l;
12118140Sralph 				return (cp - comp_dn);
12218140Sralph 			}
12318140Sralph 			/* not found, save it */
12418140Sralph 			if (lastdnptr != NULL && cpp < lastdnptr-1) {
12518140Sralph 				*cpp++ = cp;
12618140Sralph 				*cpp = NULL;
12718140Sralph 			}
12818140Sralph 		}
12918140Sralph 		sp = cp++;	/* save ptr to length byte */
13018140Sralph 		do {
13118140Sralph 			if (c == '.') {
13218140Sralph 				c = *dn++;
13318140Sralph 				break;
13418140Sralph 			}
13518342Sralph 			if (c == '\\') {
13618342Sralph 				if ((c = *dn++) == '\0')
13718342Sralph 					break;
13818342Sralph 			}
13918140Sralph 			if (cp >= eob)
14018140Sralph 				return (-1);
14118140Sralph 			*cp++ = c;
14218140Sralph 		} while ((c = *dn++) != '\0');
14318342Sralph 		/* catch trailing '.'s but not '..' */
14418342Sralph 		if ((l = cp - sp - 1) == 0 && c == '\0') {
14518342Sralph 			cp--;
14618342Sralph 			break;
14718342Sralph 		}
14818342Sralph 		if (l <= 0 || l > MAXLABEL)
14918140Sralph 			return (-1);
15018140Sralph 		*sp = l;
15118140Sralph 	}
15218140Sralph 	if (cp >= eob)
15318140Sralph 		return (-1);
15418140Sralph 	*cp++ = '\0';
15518140Sralph 	return (cp - comp_dn);
15618140Sralph }
15718140Sralph 
15818140Sralph /*
15918342Sralph  * Skip over a compressed domain name. Return the size or -1.
16018140Sralph  */
16118342Sralph dn_skip(comp_dn)
16218342Sralph 	char *comp_dn;
16318140Sralph {
16418140Sralph 	register char *cp;
16518140Sralph 	register int n;
16618140Sralph 
16718342Sralph 	cp = comp_dn;
16818140Sralph 	while (n = *cp++) {
16918140Sralph 		/*
17018140Sralph 		 * check for indirection
17118140Sralph 		 */
17218140Sralph 		switch (n & INDIR_MASK) {
17318140Sralph 		case 0:		/* normal case, n == len */
17418140Sralph 			cp += n;
17518140Sralph 			continue;
17618140Sralph 		default:	/* illegal type */
17718140Sralph 			return (-1);
17818140Sralph 		case INDIR_MASK:	/* indirection */
17918140Sralph 			cp++;
18018140Sralph 		}
18118140Sralph 		break;
18218140Sralph 	}
18318342Sralph 	return (cp - comp_dn);
18418140Sralph }
18518140Sralph 
18618140Sralph /*
18718140Sralph  * Search for expanded name from a list of previously compressed names.
18818140Sralph  * Return the offset from msg if found or -1.
18918140Sralph  */
19018140Sralph dn_find(exp_dn, msg, dnptrs, lastdnptr)
19118140Sralph 	char *exp_dn, *msg;
19218140Sralph 	char **dnptrs, **lastdnptr;
19318140Sralph {
19418140Sralph 	register char *dn, *cp, **cpp;
19518140Sralph 	register int n;
19618140Sralph 	char *sp;
19718140Sralph 
19818140Sralph 	for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
19918140Sralph 		dn = exp_dn;
20018140Sralph 		sp = cp = *cpp;
20118140Sralph 		while (n = *cp++) {
20218140Sralph 			/*
20318140Sralph 			 * check for indirection
20418140Sralph 			 */
20518140Sralph 			switch (n & INDIR_MASK) {
20618140Sralph 			case 0:		/* normal case, n == len */
20718342Sralph 				while (--n >= 0) {
20818342Sralph 					if (*dn == '\\')
20918342Sralph 						dn++;
21018140Sralph 					if (*dn++ != *cp++)
21118140Sralph 						goto next;
21218342Sralph 				}
21318140Sralph 				if ((n = *dn++) == '\0' && *cp == '\0')
21418140Sralph 					return (sp - msg);
21518140Sralph 				if (n == '.')
21618140Sralph 					continue;
21718140Sralph 				goto next;
21818140Sralph 
21918140Sralph 			default:	/* illegal type */
22018140Sralph 				return (-1);
22118140Sralph 
22218140Sralph 			case INDIR_MASK:	/* indirection */
22318140Sralph 				cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
22418140Sralph 			}
22518140Sralph 		}
22618140Sralph 		if (*dn == '\0')
22718140Sralph 			return (sp - msg);
22818140Sralph 	next:	;
22918140Sralph 	}
23018140Sralph 	return (-1);
23118140Sralph }
23218528Sralph 
23318528Sralph /*
23418528Sralph  * Routines to insert/extract short/long's. Must account for byte
23518528Sralph  * order and non-alignment problems. This code at least has the
23618528Sralph  * advantage of being portable.
23718528Sralph  */
23818528Sralph 
23918528Sralph u_short
24018528Sralph getshort(msgp)
24118528Sralph 	char *msgp;
24218528Sralph {
24318528Sralph 	register u_char *p = (u_char *) msgp;
24418528Sralph 
24518528Sralph 	return ((*p++ << 8) | *p);
24618528Sralph }
24718528Sralph 
24818528Sralph u_long
24918528Sralph getlong(msgp)
25018528Sralph 	char *msgp;
25118528Sralph {
25218528Sralph 	register u_char *p = (u_char *) msgp;
25318528Sralph 
25418528Sralph 	return ((((((*p++ << 8) | *p++) << 8) | *p++) << 8) | *p);
25518528Sralph }
25618528Sralph 
25718528Sralph u_short
25818528Sralph putshort(s, msgp)
25918528Sralph 	register u_short s;
26018528Sralph 	register char *msgp;
26118528Sralph {
26218528Sralph 
26318528Sralph 	msgp[1] = s;
26418528Sralph 	msgp[0] = s >> 8;
26518528Sralph }
26618528Sralph 
26718528Sralph u_long
26818528Sralph putlong(l, msgp)
26918528Sralph 	register u_long l;
27018528Sralph 	register char *msgp;
27118528Sralph {
27218528Sralph 
27318528Sralph 	msgp[3] = l;
27418528Sralph 	msgp[2] = (l >>= 8);
27518528Sralph 	msgp[1] = (l >>= 8);
27618528Sralph 	msgp[0] = l >> 8;
27718528Sralph }
278