res_mkquery.c revision 1.4 1 1.4 christos /* $NetBSD: res_mkquery.c,v 1.4 2004/05/21 15:35:05 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1985, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. All advertising materials mentioning features or use of this software
16 1.1 christos * must display the following acknowledgement:
17 1.1 christos * This product includes software developed by the University of
18 1.1 christos * California, Berkeley and its contributors.
19 1.1 christos * 4. Neither the name of the University nor the names of its contributors
20 1.1 christos * may be used to endorse or promote products derived from this software
21 1.1 christos * without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 christos * SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /*
37 1.1 christos * Portions Copyright (c) 1993 by Digital Equipment Corporation.
38 1.1 christos *
39 1.1 christos * Permission to use, copy, modify, and distribute this software for any
40 1.1 christos * purpose with or without fee is hereby granted, provided that the above
41 1.1 christos * copyright notice and this permission notice appear in all copies, and that
42 1.1 christos * the name of Digital Equipment Corporation not be used in advertising or
43 1.1 christos * publicity pertaining to distribution of the document or software without
44 1.1 christos * specific, written prior permission.
45 1.1 christos *
46 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
47 1.1 christos * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
48 1.1 christos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
49 1.1 christos * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
50 1.1 christos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
51 1.1 christos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
52 1.1 christos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53 1.1 christos * SOFTWARE.
54 1.1 christos */
55 1.1 christos
56 1.1 christos /*
57 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
58 1.1 christos * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
59 1.1 christos *
60 1.1 christos * Permission to use, copy, modify, and distribute this software for any
61 1.1 christos * purpose with or without fee is hereby granted, provided that the above
62 1.1 christos * copyright notice and this permission notice appear in all copies.
63 1.1 christos *
64 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
65 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
66 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
67 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
68 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
69 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
70 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 1.1 christos */
72 1.1 christos
73 1.3 christos #include <sys/cdefs.h>
74 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
75 1.3 christos #ifdef notdef
76 1.1 christos static const char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93";
77 1.1 christos static const char rcsid[] = "Id: res_mkquery.c,v 1.1.2.2.4.2 2004/03/16 12:34:18 marka Exp";
78 1.3 christos #else
79 1.4 christos __RCSID("$NetBSD: res_mkquery.c,v 1.4 2004/05/21 15:35:05 christos Exp $");
80 1.3 christos #endif
81 1.1 christos #endif /* LIBC_SCCS and not lint */
82 1.1 christos
83 1.1 christos #include "port_before.h"
84 1.4 christos
85 1.4 christos #include "namespace.h"
86 1.1 christos #include <sys/types.h>
87 1.1 christos #include <sys/param.h>
88 1.1 christos #include <netinet/in.h>
89 1.1 christos #include <arpa/nameser.h>
90 1.1 christos #include <netdb.h>
91 1.1 christos #include <resolv.h>
92 1.1 christos #include <stdio.h>
93 1.1 christos #include <string.h>
94 1.1 christos #include "port_after.h"
95 1.1 christos
96 1.4 christos #ifdef __weak_alias
97 1.4 christos __weak_alias(res_nmkquery,_res_nmkquery)
98 1.4 christos __weak_alias(res_nopt,_res_nopt)
99 1.4 christos #endif
100 1.4 christos
101 1.1 christos /* Options. Leave them on. */
102 1.1 christos #define DEBUG
103 1.1 christos
104 1.1 christos extern const char *_res_opcodes[];
105 1.1 christos
106 1.1 christos /*
107 1.1 christos * Form all types of queries.
108 1.1 christos * Returns the size of the result or -1.
109 1.1 christos */
110 1.1 christos int
111 1.1 christos res_nmkquery(res_state statp,
112 1.1 christos int op, /* opcode of query */
113 1.1 christos const char *dname, /* domain name */
114 1.1 christos int class, int type, /* class and type of query */
115 1.1 christos const u_char *data, /* resource record data */
116 1.1 christos int datalen, /* length of data */
117 1.1 christos const u_char *newrr_in, /* new rr for modify or append */
118 1.1 christos u_char *buf, /* buffer to put query */
119 1.1 christos int buflen) /* size of buffer */
120 1.1 christos {
121 1.1 christos register HEADER *hp;
122 1.1 christos register u_char *cp, *ep;
123 1.1 christos register int n;
124 1.1 christos u_char *dnptrs[20], **dpp, **lastdnptr;
125 1.1 christos
126 1.1 christos UNUSED(newrr_in);
127 1.1 christos
128 1.1 christos #ifdef DEBUG
129 1.1 christos if (statp->options & RES_DEBUG)
130 1.1 christos printf(";; res_nmkquery(%s, %s, %s, %s)\n",
131 1.1 christos _res_opcodes[op], dname, p_class(class), p_type(type));
132 1.1 christos #endif
133 1.1 christos /*
134 1.1 christos * Initialize header fields.
135 1.1 christos */
136 1.1 christos if ((buf == NULL) || (buflen < HFIXEDSZ))
137 1.1 christos return (-1);
138 1.1 christos memset(buf, 0, HFIXEDSZ);
139 1.2 christos hp = (HEADER *)(void *)buf;
140 1.1 christos hp->id = htons(++statp->id);
141 1.1 christos hp->opcode = op;
142 1.1 christos hp->rd = (statp->options & RES_RECURSE) != 0U;
143 1.1 christos hp->rcode = NOERROR;
144 1.1 christos cp = buf + HFIXEDSZ;
145 1.1 christos ep = buf + buflen;
146 1.1 christos dpp = dnptrs;
147 1.1 christos *dpp++ = buf;
148 1.1 christos *dpp++ = NULL;
149 1.1 christos lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
150 1.1 christos /*
151 1.1 christos * perform opcode specific processing
152 1.1 christos */
153 1.1 christos switch (op) {
154 1.1 christos case QUERY: /*FALLTHROUGH*/
155 1.1 christos case NS_NOTIFY_OP:
156 1.1 christos if (ep - cp < QFIXEDSZ)
157 1.1 christos return (-1);
158 1.1 christos if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs,
159 1.1 christos lastdnptr)) < 0)
160 1.1 christos return (-1);
161 1.1 christos cp += n;
162 1.1 christos ns_put16(type, cp);
163 1.1 christos cp += INT16SZ;
164 1.1 christos ns_put16(class, cp);
165 1.1 christos cp += INT16SZ;
166 1.1 christos hp->qdcount = htons(1);
167 1.1 christos if (op == QUERY || data == NULL)
168 1.1 christos break;
169 1.1 christos /*
170 1.1 christos * Make an additional record for completion domain.
171 1.1 christos */
172 1.1 christos if ((ep - cp) < RRFIXEDSZ)
173 1.1 christos return (-1);
174 1.1 christos n = dn_comp((const char *)data, cp, ep - cp - RRFIXEDSZ,
175 1.1 christos dnptrs, lastdnptr);
176 1.1 christos if (n < 0)
177 1.1 christos return (-1);
178 1.1 christos cp += n;
179 1.1 christos ns_put16(T_NULL, cp);
180 1.1 christos cp += INT16SZ;
181 1.1 christos ns_put16(class, cp);
182 1.1 christos cp += INT16SZ;
183 1.1 christos ns_put32(0, cp);
184 1.1 christos cp += INT32SZ;
185 1.1 christos ns_put16(0, cp);
186 1.1 christos cp += INT16SZ;
187 1.1 christos hp->arcount = htons(1);
188 1.1 christos break;
189 1.1 christos
190 1.1 christos case IQUERY:
191 1.1 christos /*
192 1.1 christos * Initialize answer section
193 1.1 christos */
194 1.1 christos if (ep - cp < 1 + RRFIXEDSZ + datalen)
195 1.1 christos return (-1);
196 1.1 christos *cp++ = '\0'; /* no domain name */
197 1.1 christos ns_put16(type, cp);
198 1.1 christos cp += INT16SZ;
199 1.1 christos ns_put16(class, cp);
200 1.1 christos cp += INT16SZ;
201 1.1 christos ns_put32(0, cp);
202 1.1 christos cp += INT32SZ;
203 1.1 christos ns_put16(datalen, cp);
204 1.1 christos cp += INT16SZ;
205 1.1 christos if (datalen) {
206 1.2 christos memcpy(cp, data, (size_t)datalen);
207 1.1 christos cp += datalen;
208 1.1 christos }
209 1.1 christos hp->ancount = htons(1);
210 1.1 christos break;
211 1.1 christos
212 1.1 christos default:
213 1.1 christos return (-1);
214 1.1 christos }
215 1.1 christos return (cp - buf);
216 1.1 christos }
217 1.1 christos
218 1.1 christos #ifdef RES_USE_EDNS0
219 1.1 christos /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */
220 1.1 christos #ifndef T_OPT
221 1.1 christos #define T_OPT 41
222 1.1 christos #endif
223 1.1 christos
224 1.1 christos int
225 1.1 christos res_nopt(res_state statp,
226 1.1 christos int n0, /* current offset in buffer */
227 1.1 christos u_char *buf, /* buffer to put query */
228 1.1 christos int buflen, /* size of buffer */
229 1.1 christos int anslen) /* UDP answer buffer size */
230 1.1 christos {
231 1.1 christos register HEADER *hp;
232 1.1 christos register u_char *cp, *ep;
233 1.1 christos u_int16_t flags = 0;
234 1.1 christos
235 1.1 christos #ifdef DEBUG
236 1.1 christos if ((statp->options & RES_DEBUG) != 0U)
237 1.1 christos printf(";; res_nopt()\n");
238 1.1 christos #endif
239 1.1 christos
240 1.2 christos hp = (HEADER *)(void *)buf;
241 1.1 christos cp = buf + n0;
242 1.1 christos ep = buf + buflen;
243 1.1 christos
244 1.1 christos if ((ep - cp) < 1 + RRFIXEDSZ)
245 1.1 christos return (-1);
246 1.1 christos
247 1.1 christos *cp++ = 0; /* "." */
248 1.1 christos
249 1.1 christos ns_put16(T_OPT, cp); /* TYPE */
250 1.1 christos cp += INT16SZ;
251 1.1 christos ns_put16(anslen & 0xffff, cp); /* CLASS = UDP payload size */
252 1.1 christos cp += INT16SZ;
253 1.1 christos *cp++ = NOERROR; /* extended RCODE */
254 1.1 christos *cp++ = 0; /* EDNS version */
255 1.1 christos if (statp->options & RES_USE_DNSSEC) {
256 1.1 christos #ifdef DEBUG
257 1.1 christos if (statp->options & RES_DEBUG)
258 1.1 christos printf(";; res_opt()... ENDS0 DNSSEC\n");
259 1.1 christos #endif
260 1.1 christos flags |= NS_OPT_DNSSEC_OK;
261 1.1 christos }
262 1.1 christos ns_put16(flags, cp);
263 1.1 christos cp += INT16SZ;
264 1.1 christos ns_put16(0, cp); /* RDLEN */
265 1.1 christos cp += INT16SZ;
266 1.1 christos hp->arcount = htons(ntohs(hp->arcount) + 1);
267 1.1 christos
268 1.1 christos return (cp - buf);
269 1.1 christos }
270 1.1 christos #endif
271