diff options
Diffstat (limited to 'src/lib/libc/net/res_mkquery.c')
-rw-r--r-- | src/lib/libc/net/res_mkquery.c | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c new file mode 100644 index 0000000000..a6c37379f9 --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
@@ -0,0 +1,241 @@ | |||
1 | /* $OpenBSD: res_mkquery.c,v 1.16 2005/03/30 02:58:28 tedu Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
57 | static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $"; | ||
58 | #else | ||
59 | static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.16 2005/03/30 02:58:28 tedu Exp $"; | ||
60 | #endif | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <sys/types.h> | ||
64 | #include <sys/param.h> | ||
65 | #include <netinet/in.h> | ||
66 | #include <arpa/nameser.h> | ||
67 | |||
68 | #include <stdio.h> | ||
69 | #include <netdb.h> | ||
70 | #include <resolv.h> | ||
71 | #include <string.h> | ||
72 | |||
73 | #include "thread_private.h" | ||
74 | |||
75 | /* | ||
76 | * Form all types of queries. | ||
77 | * Returns the size of the result or -1. | ||
78 | */ | ||
79 | /* ARGSUSED */ | ||
80 | int | ||
81 | res_mkquery(int op, | ||
82 | const char *dname, /* opcode of query */ | ||
83 | int class, /* domain name */ | ||
84 | int type, /* class and type of query */ | ||
85 | const u_char *data, /* resource record data */ | ||
86 | int datalen, /* length of data */ | ||
87 | const u_char *newrr_in, /* new rr for modify or append */ | ||
88 | u_char *buf, /* buffer to put query */ | ||
89 | int buflen) /* size of buffer */ | ||
90 | { | ||
91 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
92 | HEADER *hp; | ||
93 | u_char *cp, *ep; | ||
94 | int n; | ||
95 | u_char *dnptrs[20], **dpp, **lastdnptr; | ||
96 | |||
97 | if (_res_init(0) == -1) { | ||
98 | h_errno = NETDB_INTERNAL; | ||
99 | return (-1); | ||
100 | } | ||
101 | #ifdef DEBUG | ||
102 | if (_resp->options & RES_DEBUG) | ||
103 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
104 | op, dname, class, type); | ||
105 | #endif | ||
106 | /* | ||
107 | * Initialize header fields. | ||
108 | * | ||
109 | * A special random number generator is used to create non predictable | ||
110 | * and non repeating ids over a long period. It also avoids reuse | ||
111 | * by switching between two distinct number cycles. | ||
112 | */ | ||
113 | |||
114 | if ((buf == NULL) || (buflen < HFIXEDSZ)) | ||
115 | return (-1); | ||
116 | bzero(buf, HFIXEDSZ); | ||
117 | hp = (HEADER *) buf; | ||
118 | _resp->id = res_randomid(); | ||
119 | hp->id = htons(_resp->id); | ||
120 | hp->opcode = op; | ||
121 | hp->rd = (_resp->options & RES_RECURSE) != 0; | ||
122 | hp->rcode = NOERROR; | ||
123 | cp = buf + HFIXEDSZ; | ||
124 | ep = buf + buflen; | ||
125 | dpp = dnptrs; | ||
126 | *dpp++ = buf; | ||
127 | *dpp++ = NULL; | ||
128 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; | ||
129 | /* | ||
130 | * perform opcode specific processing | ||
131 | */ | ||
132 | switch (op) { | ||
133 | case QUERY: /*FALLTHROUGH*/ | ||
134 | case NS_NOTIFY_OP: | ||
135 | if (ep - cp < QFIXEDSZ) | ||
136 | return (-1); | ||
137 | if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, | ||
138 | lastdnptr)) < 0) | ||
139 | return (-1); | ||
140 | cp += 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; | ||
148 | /* | ||
149 | * Make an additional record for completion domain. | ||
150 | */ | ||
151 | if (ep - cp < RRFIXEDSZ) | ||
152 | return (-1); | ||
153 | n = dn_comp((char *)data, cp, ep - cp - RRFIXEDSZ, dnptrs, | ||
154 | lastdnptr); | ||
155 | if (n < 0) | ||
156 | return (-1); | ||
157 | cp += n; | ||
158 | __putshort(T_NULL, cp); | ||
159 | cp += INT16SZ; | ||
160 | __putshort(class, cp); | ||
161 | cp += INT16SZ; | ||
162 | __putlong(0, cp); | ||
163 | cp += INT32SZ; | ||
164 | __putshort(0, cp); | ||
165 | cp += INT16SZ; | ||
166 | hp->arcount = htons(1); | ||
167 | break; | ||
168 | |||
169 | case IQUERY: | ||
170 | /* | ||
171 | * Initialize answer section | ||
172 | */ | ||
173 | if (ep - cp < 1 + RRFIXEDSZ + datalen) | ||
174 | return (-1); | ||
175 | *cp++ = '\0'; /* no domain name */ | ||
176 | __putshort(type, cp); | ||
177 | cp += INT16SZ; | ||
178 | __putshort(class, cp); | ||
179 | cp += INT16SZ; | ||
180 | __putlong(0, cp); | ||
181 | cp += INT32SZ; | ||
182 | __putshort(datalen, cp); | ||
183 | cp += INT16SZ; | ||
184 | if (datalen) { | ||
185 | bcopy(data, cp, datalen); | ||
186 | cp += datalen; | ||
187 | } | ||
188 | hp->ancount = htons(1); | ||
189 | break; | ||
190 | |||
191 | default: | ||
192 | return (-1); | ||
193 | } | ||
194 | return (cp - buf); | ||
195 | } | ||
196 | |||
197 | /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ | ||
198 | int | ||
199 | res_opt(int n0, | ||
200 | u_char *buf, /* buffer to put query */ | ||
201 | int buflen, /* size of buffer */ | ||
202 | int anslen) /* answer buffer length */ | ||
203 | { | ||
204 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
205 | HEADER *hp; | ||
206 | u_char *cp, *ep; | ||
207 | |||
208 | hp = (HEADER *) buf; | ||
209 | cp = buf + n0; | ||
210 | ep = buf + buflen; | ||
211 | |||
212 | if (ep - cp < 1 + RRFIXEDSZ) | ||
213 | return -1; | ||
214 | |||
215 | *cp++ = 0; /* "." */ | ||
216 | |||
217 | __putshort(T_OPT, cp); /* TYPE */ | ||
218 | cp += INT16SZ; | ||
219 | if (anslen > 0xffff) | ||
220 | anslen = 0xffff; /* limit to 16bit value */ | ||
221 | __putshort(anslen & 0xffff, cp); /* CLASS = UDP payload size */ | ||
222 | cp += INT16SZ; | ||
223 | *cp++ = NOERROR; /* extended RCODE */ | ||
224 | *cp++ = 0; /* EDNS version */ | ||
225 | if (_resp->options & RES_USE_DNSSEC) { | ||
226 | #ifdef DEBUG | ||
227 | if (_resp->options & RES_DEBUG) | ||
228 | printf(";; res_opt()... ENDS0 DNSSEC OK\n"); | ||
229 | #endif /* DEBUG */ | ||
230 | __putshort(DNS_MESSAGEEXTFLAG_DO, cp); /* EDNS Z field */ | ||
231 | cp += INT16SZ; | ||
232 | } else { | ||
233 | __putshort(0, cp); /* EDNS Z field */ | ||
234 | cp += INT16SZ; | ||
235 | } | ||
236 | __putshort(0, cp); /* RDLEN */ | ||
237 | cp += INT16SZ; | ||
238 | hp->arcount = htons(ntohs(hp->arcount) + 1); | ||
239 | |||
240 | return cp - buf; | ||
241 | } | ||