diff options
Diffstat (limited to 'src/lib/libc/net/res_mkquery.c')
-rw-r--r-- | src/lib/libc/net/res_mkquery.c | 195 |
1 files changed, 195 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..3e7e2ae5d3 --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* $OpenBSD: res_mkquery.c,v 1.8 1997/04/13 22:37:21 provos 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. All advertising materials mentioning features or use of this software | ||
18 | * must display the following acknowledgement: | ||
19 | * This product includes software developed by the University of | ||
20 | * California, Berkeley and its contributors. | ||
21 | * 4. Neither the name of the University nor the names of its contributors | ||
22 | * may be used to endorse or promote products derived from this software | ||
23 | * without specific prior written permission. | ||
24 | * | ||
25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
35 | * SUCH DAMAGE. | ||
36 | * - | ||
37 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
38 | * | ||
39 | * Permission to use, copy, modify, and distribute this software for any | ||
40 | * purpose with or without fee is hereby granted, provided that the above | ||
41 | * copyright notice and this permission notice appear in all copies, and that | ||
42 | * the name of Digital Equipment Corporation not be used in advertising or | ||
43 | * publicity pertaining to distribution of the document or software without | ||
44 | * specific, written prior permission. | ||
45 | * | ||
46 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
47 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
48 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
49 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
50 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
51 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
52 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
53 | * SOFTWARE. | ||
54 | * - | ||
55 | * --Copyright-- | ||
56 | */ | ||
57 | |||
58 | #if defined(LIBC_SCCS) && !defined(lint) | ||
59 | #if 0 | ||
60 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
61 | static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $"; | ||
62 | #else | ||
63 | static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.8 1997/04/13 22:37:21 provos Exp $"; | ||
64 | #endif | ||
65 | #endif /* LIBC_SCCS and not lint */ | ||
66 | |||
67 | #include <sys/types.h> | ||
68 | #include <sys/param.h> | ||
69 | #include <netinet/in.h> | ||
70 | #include <arpa/nameser.h> | ||
71 | |||
72 | #include <stdio.h> | ||
73 | #include <netdb.h> | ||
74 | #include <resolv.h> | ||
75 | #include <string.h> | ||
76 | |||
77 | /* | ||
78 | * Form all types of queries. | ||
79 | * Returns the size of the result or -1. | ||
80 | */ | ||
81 | /* ARGSUSED */ | ||
82 | int | ||
83 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | ||
84 | int op; /* opcode of query */ | ||
85 | const char *dname; /* domain name */ | ||
86 | int class, type; /* class and type of query */ | ||
87 | const u_char *data; /* resource record data */ | ||
88 | int datalen; /* length of data */ | ||
89 | const u_char *newrr_in; /* new rr for modify or append */ | ||
90 | u_char *buf; /* buffer to put query */ | ||
91 | int buflen; /* size of buffer */ | ||
92 | { | ||
93 | register HEADER *hp; | ||
94 | register u_char *cp; | ||
95 | register int n; | ||
96 | u_char *dnptrs[20], **dpp, **lastdnptr; | ||
97 | |||
98 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) { | ||
99 | h_errno = NETDB_INTERNAL; | ||
100 | return (-1); | ||
101 | } | ||
102 | #ifdef DEBUG | ||
103 | if (_res.options & RES_DEBUG) | ||
104 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
105 | op, dname, class, type); | ||
106 | #endif | ||
107 | /* | ||
108 | * Initialize header fields. | ||
109 | * | ||
110 | * A special random number generator is used to create non predictable | ||
111 | * and non repeating ids over a long period. It also avoids reuse | ||
112 | * by switching between two distinct number cycles. | ||
113 | */ | ||
114 | |||
115 | if ((buf == NULL) || (buflen < HFIXEDSZ)) | ||
116 | return (-1); | ||
117 | bzero(buf, HFIXEDSZ); | ||
118 | hp = (HEADER *) buf; | ||
119 | _res.id = res_randomid(); | ||
120 | hp->id = htons(_res.id); | ||
121 | hp->opcode = op; | ||
122 | hp->rd = (_res.options & RES_RECURSE) != 0; | ||
123 | hp->rcode = NOERROR; | ||
124 | cp = buf + HFIXEDSZ; | ||
125 | buflen -= HFIXEDSZ; | ||
126 | dpp = dnptrs; | ||
127 | *dpp++ = buf; | ||
128 | *dpp++ = NULL; | ||
129 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; | ||
130 | /* | ||
131 | * perform opcode specific processing | ||
132 | */ | ||
133 | switch (op) { | ||
134 | case QUERY: /*FALLTHROUGH*/ | ||
135 | case NS_NOTIFY_OP: | ||
136 | if ((buflen -= QFIXEDSZ) < 0) | ||
137 | return (-1); | ||
138 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
139 | return (-1); | ||
140 | cp += n; | ||
141 | buflen -= n; | ||
142 | __putshort(type, cp); | ||
143 | cp += INT16SZ; | ||
144 | __putshort(class, cp); | ||
145 | cp += INT16SZ; | ||
146 | hp->qdcount = htons(1); | ||
147 | if (op == QUERY || data == NULL) | ||
148 | break; | ||
149 | /* | ||
150 | * Make an additional record for completion domain. | ||
151 | */ | ||
152 | buflen -= RRFIXEDSZ; | ||
153 | n = dn_comp((char *)data, cp, buflen, dnptrs, lastdnptr); | ||
154 | if (n < 0) | ||
155 | return (-1); | ||
156 | cp += n; | ||
157 | buflen -= 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 (buflen < 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 | } | ||