xref: /csrg-svn/lib/libc/net/res_mkquery.c (revision 24734)
118548Sralph /*
221387Sdist  * Copyright (c) 1985 Regents of the University of California.
321387Sdist  * All rights reserved.  The Berkeley software License Agreement
421387Sdist  * specifies the terms and conditions for redistribution.
518548Sralph  */
618548Sralph 
721387Sdist #ifndef lint
8*24734Sbloom static char sccsid[] = "@(#)res_mkquery.c	5.4 (Berkeley) 09/14/85";
921387Sdist #endif not lint
1021387Sdist 
1118143Sralph #include <stdio.h>
1218143Sralph #include <sys/types.h>
1318143Sralph #include <netinet/in.h>
1424082Skjd #include <arpa/nameser.h>
1524082Skjd #include <arpa/resolv.h>
1618143Sralph 
1718143Sralph /*
1818143Sralph  * Form all types of queries.
1918143Sralph  * Returns the size of the result or -1.
2018143Sralph  */
2118531Sralph res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
2218143Sralph 	int op;			/* opcode of query */
2318143Sralph 	char *dname;		/* domain name */
2418143Sralph 	int class, type;	/* class and type of query */
2518143Sralph 	char *data;		/* resource record data */
2618143Sralph 	int datalen;		/* length of data */
2718143Sralph 	struct rrec *newrr;	/* new rr for modify or append */
2818143Sralph 	char *buf;		/* buffer to put query */
2918143Sralph 	int buflen;		/* size of buffer */
3018143Sralph {
3118143Sralph 	register HEADER *hp;
3218143Sralph 	register char *cp;
3318143Sralph 	register int n;
3418143Sralph 	char dnbuf[MAXDNAME];
3518143Sralph 	char *dnptrs[10], **dpp, **lastdnptr;
3618528Sralph 	extern char *index();
3718143Sralph 
38*24734Sbloom #ifdef DEBUG
3918143Sralph 	if (_res.options & RES_DEBUG)
4018531Sralph 		printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
41*24734Sbloom #endif
4218143Sralph 	/*
4318143Sralph 	 * Initialize header fields.
4418143Sralph 	 */
4518143Sralph 	hp = (HEADER *) buf;
4618143Sralph 	hp->id = htons(++_res.id);
4718143Sralph 	hp->opcode = op;
4818143Sralph 	hp->qr = hp->aa = hp->tc = hp->ra = 0;
4918143Sralph 	hp->pr = (_res.options & RES_PRIMARY) != 0;
5018143Sralph 	hp->rd = (_res.options & RES_RECURSE) != 0;
5118143Sralph 	hp->rcode = NOERROR;
5218143Sralph 	hp->qdcount = 0;
5318143Sralph 	hp->ancount = 0;
5418143Sralph 	hp->nscount = 0;
5518143Sralph 	hp->arcount = 0;
5618143Sralph 	cp = buf + sizeof(HEADER);
5718143Sralph 	buflen -= sizeof(HEADER);
5818143Sralph 	dpp = dnptrs;
5918143Sralph 	*dpp++ = buf;
6018143Sralph 	*dpp++ = NULL;
6118341Sralph 	lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
6218143Sralph 	/*
6318528Sralph 	 * If the domain name contains no dots (single label), then
6418143Sralph 	 * append the default domain name to the one given.
6518143Sralph 	 */
6618528Sralph 	if ((_res.options & RES_DEFNAMES) && dname[0] != '\0' &&
6718528Sralph 	    index(dname, '.') == NULL) {
6818143Sralph 		if (!(_res.options & RES_INIT))
69*24734Sbloom 			if (res_init() == -1)
70*24734Sbloom 				return(-1);
7118143Sralph 		if (_res.defdname[0] != '\0')
7218143Sralph 			dname = sprintf(dnbuf, "%s.%s", dname, _res.defdname);
7318143Sralph 	}
7418143Sralph 	/*
7518143Sralph 	 * perform opcode specific processing
7618143Sralph 	 */
7718143Sralph 	switch (op) {
7818143Sralph 	case QUERY:
7918143Sralph 	case CQUERYM:
8018143Sralph 	case CQUERYU:
8118143Sralph 		buflen -= QFIXEDSZ;
8218143Sralph 		if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
8318143Sralph 			return (-1);
8418143Sralph 		cp += n;
8518143Sralph 		buflen -= n;
8618528Sralph 		putshort(type, cp);
8718143Sralph 		cp += sizeof(u_short);
8818528Sralph 		putshort(class, cp);
8918143Sralph 		cp += sizeof(u_short);
9023872Skjd 		hp->qdcount = htons(1);
9118143Sralph 		if (op == QUERY || data == NULL)
9218143Sralph 			break;
9318143Sralph 		/*
9418143Sralph 		 * Make an additional record for completion domain.
9518143Sralph 		 */
9618528Sralph 		buflen -= RRFIXEDSZ;
9718143Sralph 		if ((n = dn_comp(data, cp, buflen, dnptrs, lastdnptr)) < 0)
9818143Sralph 			return (-1);
9918143Sralph 		cp += n;
10018143Sralph 		buflen -= n;
10118528Sralph 		putshort(T_NULL, cp);
10218143Sralph 		cp += sizeof(u_short);
10318528Sralph 		putshort(class, cp);
10418143Sralph 		cp += sizeof(u_short);
10518528Sralph 		putlong(0, cp);
10618143Sralph 		cp += sizeof(u_long);
10718528Sralph 		putshort(0, cp);
10818143Sralph 		cp += sizeof(u_short);
10923872Skjd 		hp->arcount = htons(1);
11018143Sralph 		break;
11118143Sralph 
11218143Sralph 	case IQUERY:
11318143Sralph 		/*
11418143Sralph 		 * Initialize answer section
11518143Sralph 		 */
11618143Sralph 		if (buflen < 1 + RRFIXEDSZ + datalen)
11718143Sralph 			return (-1);
11818143Sralph 		*cp++ = '\0';	/* no domain name */
11918528Sralph 		putshort(type, cp);
12018143Sralph 		cp += sizeof(u_short);
12118528Sralph 		putshort(class, cp);
12218143Sralph 		cp += sizeof(u_short);
12318528Sralph 		putlong(0, cp);
12418143Sralph 		cp += sizeof(u_long);
12518528Sralph 		putshort(datalen, cp);
12618143Sralph 		cp += sizeof(u_short);
12718143Sralph 		if (datalen) {
12818143Sralph 			bcopy(data, cp, datalen);
12918143Sralph 			cp += datalen;
13018143Sralph 		}
13123872Skjd 		hp->ancount = htons(1);
13218143Sralph 		break;
13318143Sralph 
13418143Sralph #ifdef notdef
13518143Sralph 	case UPDATED:
13618143Sralph 		/*
13718143Sralph 		 * Put record to be added or deleted in additional section
13818143Sralph 		 */
13918143Sralph 		buflen -= RRFIXEDSZ + datalen;
14018143Sralph 		if ((n = dn_comp(dname, cp, buflen, NULL, NULL)) < 0)
14118143Sralph 			return (-1);
14218143Sralph 		cp += n;
14318143Sralph 		*((u_short *)cp) = htons(type);
14418143Sralph 		cp += sizeof(u_short);
14518143Sralph 		*((u_short *)cp) = htons(class);
14618143Sralph 		cp += sizeof(u_short);
14718143Sralph 		*((u_long *)cp) = 0;
14818143Sralph 		cp += sizeof(u_long);
14918143Sralph 		*((u_short *)cp) = htons(datalen);
15018143Sralph 		cp += sizeof(u_short);
15118143Sralph 		if (datalen) {
15218143Sralph 			bcopy(data, cp, datalen);
15318143Sralph 			cp += datalen;
15418143Sralph 		}
15518143Sralph 		break;
15618143Sralph 
15718143Sralph 	case UPDATEM:
15818143Sralph 		/*
15918143Sralph 		 * Record to be modified followed by its replacement
16018143Sralph 		 */
16118143Sralph 		buflen -= RRFIXEDSZ + datalen;
16218143Sralph 		if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
16318143Sralph 			return (-1);
16418143Sralph 		cp += n;
16518143Sralph 		*((u_short *)cp) = htons(type);
16618143Sralph 		cp += sizeof(u_short);
16718143Sralph 		*((u_short *)cp) = htons(class);
16818143Sralph 		cp += sizeof(u_short);
16918143Sralph 		*((u_long *)cp) = 0;
17018143Sralph 		cp += sizeof(u_long);
17118143Sralph 		*((u_short *)cp) = htons(datalen);
17218143Sralph 		cp += sizeof(u_short);
17318143Sralph 		if (datalen) {
17418143Sralph 			bcopy(data, cp, datalen);
17518143Sralph 			cp += datalen;
17618143Sralph 		}
17718143Sralph 
17818143Sralph 	case UPDATEA:
17918143Sralph 		buflen -= RRFIXEDSZ + newrr->r_size;
18018143Sralph 		if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
18118143Sralph 			return (-1);
18218143Sralph 		cp += n;
18318143Sralph 		*((u_short *)cp) = htons(newrr->r_type);
18418143Sralph 		cp += sizeof(u_short);
18518143Sralph 		*((u_short *)cp) = htons(newrr->r_type);
18618143Sralph 		cp += sizeof(u_short);
18718143Sralph 		*((u_long *)cp) = htonl(newrr->r_ttl);
18818143Sralph 		cp += sizeof(u_long);
18918143Sralph 		*((u_short *)cp) = htons(newrr->r_size);
19018143Sralph 		cp += sizeof(u_short);
19118143Sralph 		if (newrr->r_size) {
19218143Sralph 			bcopy(newrr->r_data, cp, newrr->r_size);
19318143Sralph 			cp += newrr->r_size;
19418143Sralph 		}
19518143Sralph 		break;
19618143Sralph #endif
19718143Sralph 	}
19818143Sralph 	return (cp - buf);
19918143Sralph }
200