xref: /csrg-svn/lib/libc/net/res_mkquery.c (revision 21387)
118548Sralph /*
2*21387Sdist  * Copyright (c) 1985 Regents of the University of California.
3*21387Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21387Sdist  * specifies the terms and conditions for redistribution.
518548Sralph  */
618548Sralph 
7*21387Sdist #ifndef lint
8*21387Sdist static char sccsid[] = "@(#)res_mkquery.c	5.1 (Berkeley) 05/30/85";
9*21387Sdist #endif not lint
10*21387Sdist 
1118143Sralph #include <stdio.h>
1218143Sralph #include <sys/types.h>
1318143Sralph #include <netinet/in.h>
1418143Sralph #include <nameser.h>
1518143Sralph #include <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 
3818143Sralph 	if (_res.options & RES_DEBUG)
3918531Sralph 		printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
4018143Sralph 	/*
4118143Sralph 	 * Initialize header fields.
4218143Sralph 	 */
4318143Sralph 	hp = (HEADER *) buf;
4418143Sralph 	hp->id = htons(++_res.id);
4518143Sralph 	hp->opcode = op;
4618143Sralph 	hp->qr = hp->aa = hp->tc = hp->ra = 0;
4718143Sralph 	hp->pr = (_res.options & RES_PRIMARY) != 0;
4818143Sralph 	hp->rd = (_res.options & RES_RECURSE) != 0;
4918143Sralph 	hp->rcode = NOERROR;
5018143Sralph 	hp->qdcount = 0;
5118143Sralph 	hp->ancount = 0;
5218143Sralph 	hp->nscount = 0;
5318143Sralph 	hp->arcount = 0;
5418143Sralph 	cp = buf + sizeof(HEADER);
5518143Sralph 	buflen -= sizeof(HEADER);
5618143Sralph 	dpp = dnptrs;
5718143Sralph 	*dpp++ = buf;
5818143Sralph 	*dpp++ = NULL;
5918341Sralph 	lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
6018143Sralph 	/*
6118528Sralph 	 * If the domain name contains no dots (single label), then
6218143Sralph 	 * append the default domain name to the one given.
6318143Sralph 	 */
6418528Sralph 	if ((_res.options & RES_DEFNAMES) && dname[0] != '\0' &&
6518528Sralph 	    index(dname, '.') == NULL) {
6618143Sralph 		if (!(_res.options & RES_INIT))
6718143Sralph 			res_init();
6818143Sralph 		if (_res.defdname[0] != '\0')
6918143Sralph 			dname = sprintf(dnbuf, "%s.%s", dname, _res.defdname);
7018143Sralph 	}
7118143Sralph 	/*
7218143Sralph 	 * perform opcode specific processing
7318143Sralph 	 */
7418143Sralph 	switch (op) {
7518143Sralph 	case QUERY:
7618143Sralph 	case CQUERYM:
7718143Sralph 	case CQUERYU:
7818143Sralph 		buflen -= QFIXEDSZ;
7918143Sralph 		if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
8018143Sralph 			return (-1);
8118143Sralph 		cp += n;
8218143Sralph 		buflen -= n;
8318528Sralph 		putshort(type, cp);
8418143Sralph 		cp += sizeof(u_short);
8518528Sralph 		putshort(class, cp);
8618143Sralph 		cp += sizeof(u_short);
8718143Sralph 		hp->qdcount = HTONS(1);
8818143Sralph 		if (op == QUERY || data == NULL)
8918143Sralph 			break;
9018143Sralph 		/*
9118143Sralph 		 * Make an additional record for completion domain.
9218143Sralph 		 */
9318528Sralph 		buflen -= RRFIXEDSZ;
9418143Sralph 		if ((n = dn_comp(data, cp, buflen, dnptrs, lastdnptr)) < 0)
9518143Sralph 			return (-1);
9618143Sralph 		cp += n;
9718143Sralph 		buflen -= n;
9818528Sralph 		putshort(T_NULL, cp);
9918143Sralph 		cp += sizeof(u_short);
10018528Sralph 		putshort(class, cp);
10118143Sralph 		cp += sizeof(u_short);
10218528Sralph 		putlong(0, cp);
10318143Sralph 		cp += sizeof(u_long);
10418528Sralph 		putshort(0, cp);
10518143Sralph 		cp += sizeof(u_short);
10618143Sralph 		hp->arcount = HTONS(1);
10718143Sralph 		break;
10818143Sralph 
10918143Sralph 	case IQUERY:
11018143Sralph 		/*
11118143Sralph 		 * Initialize answer section
11218143Sralph 		 */
11318143Sralph 		if (buflen < 1 + RRFIXEDSZ + datalen)
11418143Sralph 			return (-1);
11518143Sralph 		*cp++ = '\0';	/* no domain name */
11618528Sralph 		putshort(type, cp);
11718143Sralph 		cp += sizeof(u_short);
11818528Sralph 		putshort(class, cp);
11918143Sralph 		cp += sizeof(u_short);
12018528Sralph 		putlong(0, cp);
12118143Sralph 		cp += sizeof(u_long);
12218528Sralph 		putshort(datalen, cp);
12318143Sralph 		cp += sizeof(u_short);
12418143Sralph 		if (datalen) {
12518143Sralph 			bcopy(data, cp, datalen);
12618143Sralph 			cp += datalen;
12718143Sralph 		}
12818143Sralph 		hp->ancount = HTONS(1);
12918143Sralph 		break;
13018143Sralph 
13118143Sralph #ifdef notdef
13218143Sralph 	case UPDATED:
13318143Sralph 		/*
13418143Sralph 		 * Put record to be added or deleted in additional section
13518143Sralph 		 */
13618143Sralph 		buflen -= RRFIXEDSZ + datalen;
13718143Sralph 		if ((n = dn_comp(dname, cp, buflen, NULL, NULL)) < 0)
13818143Sralph 			return (-1);
13918143Sralph 		cp += n;
14018143Sralph 		*((u_short *)cp) = htons(type);
14118143Sralph 		cp += sizeof(u_short);
14218143Sralph 		*((u_short *)cp) = htons(class);
14318143Sralph 		cp += sizeof(u_short);
14418143Sralph 		*((u_long *)cp) = 0;
14518143Sralph 		cp += sizeof(u_long);
14618143Sralph 		*((u_short *)cp) = htons(datalen);
14718143Sralph 		cp += sizeof(u_short);
14818143Sralph 		if (datalen) {
14918143Sralph 			bcopy(data, cp, datalen);
15018143Sralph 			cp += datalen;
15118143Sralph 		}
15218143Sralph 		break;
15318143Sralph 
15418143Sralph 	case UPDATEM:
15518143Sralph 		/*
15618143Sralph 		 * Record to be modified followed by its replacement
15718143Sralph 		 */
15818143Sralph 		buflen -= RRFIXEDSZ + datalen;
15918143Sralph 		if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
16018143Sralph 			return (-1);
16118143Sralph 		cp += n;
16218143Sralph 		*((u_short *)cp) = htons(type);
16318143Sralph 		cp += sizeof(u_short);
16418143Sralph 		*((u_short *)cp) = htons(class);
16518143Sralph 		cp += sizeof(u_short);
16618143Sralph 		*((u_long *)cp) = 0;
16718143Sralph 		cp += sizeof(u_long);
16818143Sralph 		*((u_short *)cp) = htons(datalen);
16918143Sralph 		cp += sizeof(u_short);
17018143Sralph 		if (datalen) {
17118143Sralph 			bcopy(data, cp, datalen);
17218143Sralph 			cp += datalen;
17318143Sralph 		}
17418143Sralph 
17518143Sralph 	case UPDATEA:
17618143Sralph 		buflen -= RRFIXEDSZ + newrr->r_size;
17718143Sralph 		if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
17818143Sralph 			return (-1);
17918143Sralph 		cp += n;
18018143Sralph 		*((u_short *)cp) = htons(newrr->r_type);
18118143Sralph 		cp += sizeof(u_short);
18218143Sralph 		*((u_short *)cp) = htons(newrr->r_type);
18318143Sralph 		cp += sizeof(u_short);
18418143Sralph 		*((u_long *)cp) = htonl(newrr->r_ttl);
18518143Sralph 		cp += sizeof(u_long);
18618143Sralph 		*((u_short *)cp) = htons(newrr->r_size);
18718143Sralph 		cp += sizeof(u_short);
18818143Sralph 		if (newrr->r_size) {
18918143Sralph 			bcopy(newrr->r_data, cp, newrr->r_size);
19018143Sralph 			cp += newrr->r_size;
19118143Sralph 		}
19218143Sralph 		break;
19318143Sralph #endif
19418143Sralph 	}
19518143Sralph 	return (cp - buf);
19618143Sralph }
197