Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / lib / libc / net / res_mkquery.c
bloba7afa9344f16b51e7ba0aad35d220b8f0640e389
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
27 * SUCH DAMAGE.
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
52 * SOFTWARE.
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
69 * SOFTWARE.
72 #include <sys/types.h>
73 #include <sys/param.h>
74 #include <netinet/in.h>
75 #include <arpa/nameser.h>
76 #include <netdb.h>
77 #include <resolv.h>
78 #include <stdio.h>
79 #include <string.h>
81 #include "res_config.h"
84 * Form all types of queries.
85 * Returns the size of the result or -1.
87 int
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 */
98 HEADER *hp;
99 u_char *cp;
100 int n;
101 u_char *dnptrs[20], **dpp, **lastdnptr;
103 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
104 h_errno = NETDB_INTERNAL;
105 return (-1);
107 #ifdef DEBUG
108 if (_res.options & RES_DEBUG)
109 printf(";; res_mkquery(%d, %s, %d, %d)\n",
110 op, dname, class, type);
111 #endif
113 * Initialize header fields.
115 if ((buf == NULL) || (buflen < HFIXEDSZ))
116 return (-1);
117 memset(buf, 0, HFIXEDSZ);
118 hp = (HEADER *) buf;
119 hp->id = htons(++_res.id);
120 hp->opcode = op;
121 hp->rd = (_res.options & RES_RECURSE) != 0;
122 hp->rcode = NOERROR;
123 cp = buf + HFIXEDSZ;
124 buflen -= HFIXEDSZ;
125 dpp = dnptrs;
126 *dpp++ = buf;
127 *dpp++ = NULL;
128 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
130 * perform opcode specific processing
132 switch (op) {
133 case QUERY: /*FALLTHROUGH*/
134 case NS_NOTIFY_OP:
135 if ((buflen -= QFIXEDSZ) < 0)
136 return (-1);
137 if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
138 return (-1);
139 cp += n;
140 buflen -= n;
141 __putshort(type, cp);
142 cp += INT16SZ;
143 __putshort(class, cp);
144 cp += INT16SZ;
145 hp->qdcount = htons(1);
146 if (op == QUERY || data == NULL)
147 break;
149 * Make an additional record for completion domain.
151 buflen -= RRFIXEDSZ;
152 n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr);
153 if (n < 0)
154 return (-1);
155 cp += n;
156 buflen -= n;
157 __putshort(T_NULL, cp);
158 cp += INT16SZ;
159 __putshort(class, cp);
160 cp += INT16SZ;
161 __putlong(0, cp);
162 cp += INT32SZ;
163 __putshort(0, cp);
164 cp += INT16SZ;
165 hp->arcount = htons(1);
166 break;
168 case IQUERY:
170 * Initialize answer section
172 if (buflen < 1 + RRFIXEDSZ + datalen)
173 return (-1);
174 *cp++ = '\0'; /* no domain name */
175 __putshort(type, cp);
176 cp += INT16SZ;
177 __putshort(class, cp);
178 cp += INT16SZ;
179 __putlong(0, cp);
180 cp += INT32SZ;
181 __putshort(datalen, cp);
182 cp += INT16SZ;
183 if (datalen) {
184 memcpy(cp, data, datalen);
185 cp += datalen;
187 hp->ancount = htons(1);
188 break;
190 default:
191 return (-1);
193 return (cp - buf);
197 * Weak aliases for applications that use certain private entry points,
198 * and fail to include <resolv.h>.
200 #undef res_mkquery
201 __weak_reference(__res_mkquery, res_mkquery);
203 /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
205 res_opt(int n0,
206 u_char *buf, /* buffer to put query */
207 int buflen, /* size of buffer */
208 int anslen) /* answer buffer length */
210 HEADER *hp;
211 u_char *cp;
213 hp = (HEADER *) buf;
214 cp = buf + n0;
215 buflen -= n0;
217 if (buflen < 1 + RRFIXEDSZ)
218 return -1;
220 *cp++ = 0; /* "." */
221 buflen--;
223 __putshort(T_OPT, cp); /* TYPE */
224 cp += INT16SZ;
225 if (anslen > 0xffff)
226 anslen = 0xffff; /* limit to 16bit value */
227 __putshort(anslen & 0xffff, cp); /* CLASS = UDP payload size */
228 cp += INT16SZ;
229 *cp++ = NOERROR; /* extended RCODE */
230 *cp++ = 0; /* EDNS version */
231 __putshort(0, cp); /* MBZ */
232 cp += INT16SZ;
233 __putshort(0, cp); /* RDLEN */
234 cp += INT16SZ;
235 hp->arcount = htons(ntohs(hp->arcount) + 1);
236 buflen -= RRFIXEDSZ;
238 return cp - buf;