2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)res_mkquery.c 8.1 (Berkeley) 6/4/93
30 * $From: Id: res_mkquery.c,v 8.9 1997/04/24 22:22:36 vixie Exp $
31 * $FreeBSD: src/lib/libc/net/res_mkquery.c,v 1.15.2.2 2002/09/20 10:45:35 nectar Exp $
32 * $DragonFly: src/lib/libc/net/res_mkquery.c,v 1.5 2005/11/13 02:04:47 swildner Exp $
36 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies, and that
41 * the name of Digital Equipment Corporation not be used in advertising or
42 * publicity pertaining to distribution of the document or software without
43 * specific, written prior permission.
45 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
46 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
48 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
49 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
50 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
51 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
56 * Portions Copyright (c) 1996 by Internet Software Consortium.
58 * Permission to use, copy, modify, and distribute this software for any
59 * purpose with or without fee is hereby granted, provided that the above
60 * copyright notice and this permission notice appear in all copies.
62 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
63 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
64 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
65 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
67 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <netinet/in.h>
75 #include <arpa/nameser.h>
81 #include "res_config.h"
84 * Form all types of queries.
85 * Returns the size of the result or -1.
88 res_mkquery(int op, /* opcode of query */
89 const char *dname, /* domain name */
90 int class, /* class of query */
91 int type, /* type of query */
92 const u_char *data, /* resource record data */
93 int datalen, /* length of data */
94 const u_char *newrr_in, /* new rr for modify or append */
95 u_char *buf, /* buffer to put query */
96 int buflen) /* size of buffer */
101 u_char *dnptrs[20], **dpp, **lastdnptr;
103 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
104 h_errno = NETDB_INTERNAL;
108 if (_res.options & RES_DEBUG)
109 printf(";; res_mkquery(%d, %s, %d, %d)\n",
110 op, dname, class, type);
113 * Initialize header fields.
115 if ((buf == NULL) || (buflen < HFIXEDSZ))
117 memset(buf, 0, HFIXEDSZ);
119 hp->id = htons(++_res.id);
121 hp->rd = (_res.options & RES_RECURSE) != 0;
128 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
130 * perform opcode specific processing
133 case QUERY: /*FALLTHROUGH*/
135 if ((buflen -= QFIXEDSZ) < 0)
137 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
141 __putshort(type, cp);
143 __putshort(class, cp);
145 hp->qdcount = htons(1);
146 if (op == QUERY || data == NULL)
149 * Make an additional record for completion domain.
152 n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
157 __putshort(T_NULL, cp);
159 __putshort(class, cp);
165 hp->arcount = htons(1);
170 * Initialize answer section
172 if (buflen < 1 + RRFIXEDSZ + datalen)
174 *cp++ = '\0'; /* no domain name */
175 __putshort(type, cp);
177 __putshort(class, cp);
181 __putshort(datalen, cp);
184 memcpy(cp, data, datalen);
187 hp->ancount = htons(1);
197 * Weak aliases for applications that use certain private entry points,
198 * and fail to include <resolv.h>.
201 __weak_reference(__res_mkquery, res_mkquery);
203 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
206 u_char *buf, /* buffer to put query */
207 int buflen, /* size of buffer */
208 int anslen) /* answer buffer length */
217 if (buflen < 1 + RRFIXEDSZ)
223 __putshort(T_OPT, cp); /* TYPE */
226 anslen = 0xffff; /* limit to 16bit value */
227 __putshort(anslen & 0xffff, cp); /* CLASS = UDP payload size */
229 *cp++ = NOERROR; /* extended RCODE */
230 *cp++ = 0; /* EDNS version */
231 __putshort(0, cp); /* MBZ */
233 __putshort(0, cp); /* RDLEN */
235 hp->arcount = htons(ntohs(hp->arcount) + 1);