diff options
author | deraadt <> | 1995-10-18 08:42:23 +0000 |
---|---|---|
committer | deraadt <> | 1995-10-18 08:42:23 +0000 |
commit | 0527d29da443886d92e9a418180c5b25a5f8d270 (patch) | |
tree | 86b3a64928451a669cefa27900e5884036b4e349 /src/lib/libc/net/res_mkquery.c | |
download | openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.gz openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.tar.bz2 openbsd-0527d29da443886d92e9a418180c5b25a5f8d270.zip |
initial import of NetBSD tree
Diffstat (limited to 'src/lib/libc/net/res_mkquery.c')
-rw-r--r-- | src/lib/libc/net/res_mkquery.c | 236 |
1 files changed, 236 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..25f025e147 --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
@@ -0,0 +1,236 @@ | |||
1 | /* $NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1985, 1993 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | * - | ||
35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies, and that | ||
40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
41 | * publicity pertaining to distribution of the document or software without | ||
42 | * specific, written prior permission. | ||
43 | * | ||
44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
51 | * SOFTWARE. | ||
52 | * - | ||
53 | * --Copyright-- | ||
54 | */ | ||
55 | |||
56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
57 | #if 0 | ||
58 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
59 | static char rcsid[] = "$Id: res_mkquery.c,v 4.9.1.2 1993/05/17 10:00:01 vixie Exp "; | ||
60 | #else | ||
61 | static char rcsid[] = "$NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $"; | ||
62 | #endif | ||
63 | #endif /* LIBC_SCCS and not lint */ | ||
64 | |||
65 | #include <sys/param.h> | ||
66 | #include <netinet/in.h> | ||
67 | #include <arpa/nameser.h> | ||
68 | #include <resolv.h> | ||
69 | #include <stdio.h> | ||
70 | #include <string.h> | ||
71 | |||
72 | /* | ||
73 | * Form all types of queries. | ||
74 | * Returns the size of the result or -1. | ||
75 | */ | ||
76 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | ||
77 | int op; /* opcode of query */ | ||
78 | const char *dname; /* domain name */ | ||
79 | int class, type; /* class and type of query */ | ||
80 | const char *data; /* resource record data */ | ||
81 | int datalen; /* length of data */ | ||
82 | const char *newrr_in; /* new rr for modify or append */ | ||
83 | char *buf; /* buffer to put query */ | ||
84 | int buflen; /* size of buffer */ | ||
85 | { | ||
86 | register HEADER *hp; | ||
87 | register char *cp; | ||
88 | register int n; | ||
89 | struct rrec *newrr = (struct rrec *) newrr_in; | ||
90 | char *dnptrs[10], **dpp, **lastdnptr; | ||
91 | |||
92 | #ifdef DEBUG | ||
93 | if (_res.options & RES_DEBUG) | ||
94 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
95 | op, dname, class, type); | ||
96 | #endif | ||
97 | /* | ||
98 | * Initialize header fields. | ||
99 | */ | ||
100 | if ((buf == NULL) || (buflen < sizeof(HEADER))) | ||
101 | return(-1); | ||
102 | bzero(buf, sizeof(HEADER)); | ||
103 | hp = (HEADER *) buf; | ||
104 | hp->id = htons(++_res.id); | ||
105 | hp->opcode = op; | ||
106 | hp->pr = (_res.options & RES_PRIMARY) != 0; | ||
107 | hp->rd = (_res.options & RES_RECURSE) != 0; | ||
108 | hp->rcode = NOERROR; | ||
109 | cp = buf + sizeof(HEADER); | ||
110 | buflen -= sizeof(HEADER); | ||
111 | dpp = dnptrs; | ||
112 | *dpp++ = buf; | ||
113 | *dpp++ = NULL; | ||
114 | lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]); | ||
115 | /* | ||
116 | * perform opcode specific processing | ||
117 | */ | ||
118 | switch (op) { | ||
119 | case QUERY: | ||
120 | if ((buflen -= QFIXEDSZ) < 0) | ||
121 | return(-1); | ||
122 | if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen, | ||
123 | (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) | ||
124 | return (-1); | ||
125 | cp += n; | ||
126 | buflen -= n; | ||
127 | __putshort(type, (u_char *)cp); | ||
128 | cp += sizeof(u_int16_t); | ||
129 | __putshort(class, (u_char *)cp); | ||
130 | cp += sizeof(u_int16_t); | ||
131 | hp->qdcount = htons(1); | ||
132 | if (op == QUERY || data == NULL) | ||
133 | break; | ||
134 | /* | ||
135 | * Make an additional record for completion domain. | ||
136 | */ | ||
137 | buflen -= RRFIXEDSZ; | ||
138 | if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen, | ||
139 | (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) | ||
140 | return (-1); | ||
141 | cp += n; | ||
142 | buflen -= n; | ||
143 | __putshort(T_NULL, (u_char *)cp); | ||
144 | cp += sizeof(u_int16_t); | ||
145 | __putshort(class, (u_char *)cp); | ||
146 | cp += sizeof(u_int16_t); | ||
147 | __putlong(0, (u_char *)cp); | ||
148 | cp += sizeof(u_int32_t); | ||
149 | __putshort(0, (u_char *)cp); | ||
150 | cp += sizeof(u_int16_t); | ||
151 | hp->arcount = htons(1); | ||
152 | break; | ||
153 | |||
154 | case IQUERY: | ||
155 | /* | ||
156 | * Initialize answer section | ||
157 | */ | ||
158 | if (buflen < 1 + RRFIXEDSZ + datalen) | ||
159 | return (-1); | ||
160 | *cp++ = '\0'; /* no domain name */ | ||
161 | __putshort(type, (u_char *)cp); | ||
162 | cp += sizeof(u_int16_t); | ||
163 | __putshort(class, (u_char *)cp); | ||
164 | cp += sizeof(u_int16_t); | ||
165 | __putlong(0, (u_char *)cp); | ||
166 | cp += sizeof(u_int32_t); | ||
167 | __putshort(datalen, (u_char *)cp); | ||
168 | cp += sizeof(u_int16_t); | ||
169 | if (datalen) { | ||
170 | bcopy(data, cp, datalen); | ||
171 | cp += datalen; | ||
172 | } | ||
173 | hp->ancount = htons(1); | ||
174 | break; | ||
175 | |||
176 | #ifdef ALLOW_UPDATES | ||
177 | /* | ||
178 | * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA | ||
179 | * (Record to be modified is followed by its replacement in msg.) | ||
180 | */ | ||
181 | case UPDATEM: | ||
182 | case UPDATEMA: | ||
183 | |||
184 | case UPDATED: | ||
185 | /* | ||
186 | * The res code for UPDATED and UPDATEDA is the same; user | ||
187 | * calls them differently: specifies data for UPDATED; server | ||
188 | * ignores data if specified for UPDATEDA. | ||
189 | */ | ||
190 | case UPDATEDA: | ||
191 | buflen -= RRFIXEDSZ + datalen; | ||
192 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
193 | return (-1); | ||
194 | cp += n; | ||
195 | __putshort(type, cp); | ||
196 | cp += sizeof(u_int16_t); | ||
197 | __putshort(class, cp); | ||
198 | cp += sizeof(u_int16_t); | ||
199 | __putlong(0, cp); | ||
200 | cp += sizeof(u_int32_t); | ||
201 | __putshort(datalen, cp); | ||
202 | cp += sizeof(u_int16_t); | ||
203 | if (datalen) { | ||
204 | bcopy(data, cp, datalen); | ||
205 | cp += datalen; | ||
206 | } | ||
207 | if ( (op == UPDATED) || (op == UPDATEDA) ) { | ||
208 | hp->ancount = htons(0); | ||
209 | break; | ||
210 | } | ||
211 | /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */ | ||
212 | |||
213 | case UPDATEA: /* Add new resource record */ | ||
214 | buflen -= RRFIXEDSZ + datalen; | ||
215 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
216 | return (-1); | ||
217 | cp += n; | ||
218 | __putshort(newrr->r_type, cp); | ||
219 | cp += sizeof(u_int16_t); | ||
220 | __putshort(newrr->r_class, cp); | ||
221 | cp += sizeof(u_int16_t); | ||
222 | __putlong(0, cp); | ||
223 | cp += sizeof(u_int32_t); | ||
224 | __putshort(newrr->r_size, cp); | ||
225 | cp += sizeof(u_int16_t); | ||
226 | if (newrr->r_size) { | ||
227 | bcopy(newrr->r_data, cp, newrr->r_size); | ||
228 | cp += newrr->r_size; | ||
229 | } | ||
230 | hp->ancount = htons(0); | ||
231 | break; | ||
232 | |||
233 | #endif /* ALLOW_UPDATES */ | ||
234 | } | ||
235 | return (cp - buf); | ||
236 | } | ||