]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/BsdSocketLib/res_mkupdate.c
Fix send to properly wait while long transmits are in progress
[mirror_edk2.git] / StdLib / BsdSocketLib / res_mkupdate.c
1 /*
2 * Copyright (c) 1996 by Internet Software Consortium.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 * SOFTWARE.
16 */
17
18 /*
19 * Portions copyright (c) 1999, 2000
20 * Intel Corporation.
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 *
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 *
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 *
37 * This product includes software developed by Intel Corporation and
38 * its contributors.
39 *
40 * 4. Neither the name of Intel Corporation or its contributors may be
41 * used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
55 *
56 */
57
58 /*
59 * Based on the Dynamic DNS reference implementation by Viraj Bais
60 * <viraj_bais@ccm.fm.intel.com>
61 */
62
63 #if !defined(lint) && !defined(SABER)
64 static char rcsid[] = "$Id: res_mkupdate.c,v 1.1.1.1 2003/11/19 01:51:38 kyu3 Exp $";
65 #endif /* not lint */
66
67 #include <sys/types.h>
68 #include <sys/param.h>
69
70 #include <netinet/in.h>
71 #include <arpa/nameser.h>
72 #include <arpa/inet.h>
73
74 #include <errno.h>
75 #include <limits.h>
76 #include <netdb.h>
77 #include <resolv.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82 #include <ctype.h>
83
84 #include "res_config.h"
85
86 static int getnum_str(u_char **, u_char *);
87 static int getword_str(char *, int, u_char **, u_char *);
88
89 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
90
91 /*
92 * Form update packets.
93 * Returns the size of the resulting packet if no error
94 * On error,
95 * returns -1 if error in reading a word/number in rdata
96 * portion for update packets
97 * -2 if length of buffer passed is insufficient
98 * -3 if zone section is not the first section in
99 * the linked list, or section order has a problem
100 * -4 on a number overflow
101 * -5 unknown operation or no records
102 */
103 int
104 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
105 ns_updrec *rrecp_start = rrecp_in;
106 HEADER *hp;
107 u_char *cp, *sp1, *sp2, *startp, *endp;
108 int n, i, soanum, multiline;
109 ns_updrec *rrecp;
110 struct in_addr ina;
111 char buf2[MAXDNAME];
112 int section, numrrs = 0, counts[ns_s_max];
113 u_int16_t rtype, rclass;
114 u_int32_t n1, rttl;
115 u_char *dnptrs[20], **dpp, **lastdnptr;
116
117 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
118 h_errno = NETDB_INTERNAL;
119 return (-1);
120 }
121
122 /*
123 * Initialize header fields.
124 */
125 if ((buf == NULL) || (buflen < HFIXEDSZ))
126 return (-1);
127 memset(buf, 0, HFIXEDSZ);
128 hp = (HEADER *) buf;
129 hp->id = htons(++_res.id);
130 hp->opcode = ns_o_update;
131 hp->rcode = NOERROR;
132 sp1 = buf + 2*INT16SZ; /* save pointer to zocount */
133 cp = buf + HFIXEDSZ;
134 buflen -= HFIXEDSZ;
135 dpp = dnptrs;
136 *dpp++ = buf;
137 *dpp++ = NULL;
138 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
139
140 if (rrecp_start == NULL)
141 return (-5);
142 else if (rrecp_start->r_section != S_ZONE)
143 return (-3);
144
145 memset(counts, 0, sizeof counts);
146 for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {
147 numrrs++;
148 section = rrecp->r_section;
149 if (section < 0 || section >= ns_s_max)
150 return (-1);
151 counts[section]++;
152 for (i = section + 1; i < ns_s_max; i++)
153 if (counts[i])
154 return (-3);
155 rtype = rrecp->r_type;
156 rclass = rrecp->r_class;
157 rttl = rrecp->r_ttl;
158 /* overload class and type */
159 if (section == S_PREREQ) {
160 rttl = 0;
161 switch (rrecp->r_opcode) {
162 case YXDOMAIN:
163 rclass = C_ANY;
164 rtype = T_ANY;
165 rrecp->r_size = 0;
166 break;
167 case NXDOMAIN:
168 rclass = C_NONE;
169 rtype = T_ANY;
170 rrecp->r_size = 0;
171 break;
172 case NXRRSET:
173 rclass = C_NONE;
174 rrecp->r_size = 0;
175 break;
176 case YXRRSET:
177 if (rrecp->r_size == 0)
178 rclass = C_ANY;
179 break;
180 default:
181 fprintf(stderr,
182 "res_mkupdate: incorrect opcode: %d\n",
183 rrecp->r_opcode);
184 fflush(stderr);
185 return (-1);
186 }
187 } else if (section == S_UPDATE) {
188 switch (rrecp->r_opcode) {
189 case DELETE:
190 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
191 break;
192 case ADD:
193 break;
194 default:
195 fprintf(stderr,
196 "res_mkupdate: incorrect opcode: %d\n",
197 rrecp->r_opcode);
198 fflush(stderr);
199 return (-1);
200 }
201 }
202
203 /*
204 * XXX appending default domain to owner name is omitted,
205 * fqdn must be provided
206 */
207 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
208 lastdnptr)) < 0)
209 return (-1);
210 cp += n;
211 ShrinkBuffer(n + 2*INT16SZ);
212 PUTSHORT(rtype, cp);
213 PUTSHORT(rclass, cp);
214 if (section == S_ZONE) {
215 if (numrrs != 1 || rrecp->r_type != T_SOA)
216 return (-3);
217 continue;
218 }
219 ShrinkBuffer(INT32SZ + INT16SZ);
220 PUTLONG(rttl, cp);
221 sp2 = cp; /* save pointer to length byte */
222 cp += INT16SZ;
223 if (rrecp->r_size == 0) {
224 if (section == S_UPDATE && rclass != C_ANY)
225 return (-1);
226 else {
227 PUTSHORT(0, sp2);
228 continue;
229 }
230 }
231 startp = rrecp->r_data;
232 endp = startp + rrecp->r_size - 1;
233 /* XXX this should be done centrally. */
234 switch (rrecp->r_type) {
235 case T_A:
236 if (!getword_str(buf2, sizeof buf2, &startp, endp))
237 return (-1);
238 if (!inet_aton(buf2, &ina))
239 return (-1);
240 n1 = ntohl(ina.s_addr);
241 ShrinkBuffer(INT32SZ);
242 PUTLONG(n1, cp);
243 break;
244 case T_CNAME:
245 case T_MB:
246 case T_MG:
247 case T_MR:
248 case T_NS:
249 case T_PTR:
250 if (!getword_str(buf2, sizeof buf2, &startp, endp))
251 return (-1);
252 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
253 if (n < 0)
254 return (-1);
255 cp += n;
256 ShrinkBuffer(n);
257 break;
258 case T_MINFO:
259 case T_SOA:
260 case T_RP:
261 for (i = 0; i < 2; i++) {
262 if (!getword_str(buf2, sizeof buf2, &startp,
263 endp))
264 return (-1);
265 n = dn_comp(buf2, cp, buflen,
266 dnptrs, lastdnptr);
267 if (n < 0)
268 return (-1);
269 cp += n;
270 ShrinkBuffer(n);
271 }
272 if (rrecp->r_type == T_SOA) {
273 ShrinkBuffer(5 * INT32SZ);
274 while (isspace(*startp) || !*startp)
275 startp++;
276 if (*startp == '(') {
277 multiline = 1;
278 startp++;
279 } else
280 multiline = 0;
281 /* serial, refresh, retry, expire, minimum */
282 for (i = 0; i < 5; i++) {
283 soanum = getnum_str(&startp, endp);
284 if (soanum < 0)
285 return (-1);
286 PUTLONG(soanum, cp);
287 }
288 if (multiline) {
289 while (isspace(*startp) || !*startp)
290 startp++;
291 if (*startp != ')')
292 return (-1);
293 }
294 }
295 break;
296 case T_MX:
297 case T_AFSDB:
298 case T_RT:
299 n = getnum_str(&startp, endp);
300 if (n < 0)
301 return (-1);
302 PUTSHORT(n, cp);
303 ShrinkBuffer(INT16SZ);
304 if (!getword_str(buf2, sizeof buf2, &startp, endp))
305 return (-1);
306 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
307 if (n < 0)
308 return (-1);
309 cp += n;
310 ShrinkBuffer(n);
311 break;
312 case T_PX:
313 n = getnum_str(&startp, endp);
314 if (n < 0)
315 return (-1);
316 PUTSHORT(n, cp);
317 ShrinkBuffer(INT16SZ);
318 for (i = 0; i < 2; i++) {
319 if (!getword_str(buf2, sizeof buf2, &startp,
320 endp))
321 return (-1);
322 n = dn_comp(buf2, cp, buflen, dnptrs,
323 lastdnptr);
324 if (n < 0)
325 return (-1);
326 cp += n;
327 ShrinkBuffer(n);
328 }
329 break;
330 case T_WKS:
331 case T_HINFO:
332 case T_TXT:
333 case T_X25:
334 case T_ISDN:
335 case T_NSAP:
336 case T_LOC:
337 /* XXX - more fine tuning needed here */
338 ShrinkBuffer(rrecp->r_size);
339 memcpy(cp, rrecp->r_data, rrecp->r_size);
340 cp += rrecp->r_size;
341 break;
342 default:
343 return (-1);
344 } /*switch*/
345 n = (u_int16_t)((cp - sp2) - INT16SZ);
346 PUTSHORT(n, sp2);
347 } /*for*/
348
349 hp->qdcount = htons(counts[0]);
350 hp->ancount = htons(counts[1]);
351 hp->nscount = htons(counts[2]);
352 hp->arcount = htons(counts[3]);
353 return ((int)(cp - buf));
354 }
355
356 /*
357 * Get a whitespace delimited word from a string (not file)
358 * into buf. modify the start pointer to point after the
359 * word in the string.
360 */
361 static int
362 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
363 char *cp;
364 int c;
365
366 for (cp = buf; *startpp <= endp; ) {
367 c = **startpp;
368 if (isspace(c) || c == '\0') {
369 if (cp != buf) /* trailing whitespace */
370 break;
371 else { /* leading whitespace */
372 (*startpp)++;
373 continue;
374 }
375 }
376 (*startpp)++;
377 if (cp >= buf+size-1)
378 break;
379 *cp++ = (u_char)c;
380 }
381 *cp = '\0';
382 return (cp != buf);
383 }
384
385 /*
386 * Get a whitespace delimited number from a string (not file) into buf
387 * update the start pointer to point after the number in the string.
388 */
389 static int
390 getnum_str(u_char **startpp, u_char *endp) {
391 int c;
392 int n;
393 int seendigit = 0;
394 int m = 0;
395
396 for (n = 0; *startpp <= endp; ) {
397 c = **startpp;
398 if (isspace(c) || c == '\0') {
399 if (seendigit) /* trailing whitespace */
400 break;
401 else { /* leading whitespace */
402 (*startpp)++;
403 continue;
404 }
405 }
406 if (c == ';') {
407 while ((*startpp <= endp) &&
408 ((c = **startpp) != '\n'))
409 (*startpp)++;
410 if (seendigit)
411 break;
412 continue;
413 }
414 if (!isdigit(c)) {
415 if (c == ')' && seendigit) {
416 (*startpp)--;
417 break;
418 }
419 return (-1);
420 }
421 (*startpp)++;
422 n = n * 10 + (c - '0');
423 seendigit = 1;
424 }
425 return (n + m);
426 }
427
428 /*
429 * Allocate a resource record buffer & save rr info.
430 */
431 ns_updrec *
432 res_mkupdrec(int section, const char *dname,
433 u_int class, u_int type, u_long ttl) {
434 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
435
436 if (!rrecp || !(rrecp->r_dname = strdup(dname)))
437 return (NULL);
438 rrecp->r_class = (u_int16_t)class;
439 rrecp->r_type = (u_int16_t)type;
440 rrecp->r_ttl = (u_int32_t)ttl;
441 rrecp->r_section = (u_int8_t)section;
442 return (rrecp);
443 }
444
445 /*
446 * Free a resource record buffer created by res_mkupdrec.
447 */
448 void
449 res_freeupdrec(ns_updrec *rrecp) {
450 /* Note: freeing r_dp is the caller's responsibility. */
451 if (rrecp->r_dname != NULL)
452 free(rrecp->r_dname);
453 free(rrecp);
454 }