2 * Copyright (c) 1996 by Internet Software Consortium.
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.
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
17 * $FreeBSD: src/lib/libc/net/res_mkupdate.c,v 1.2.2.1 2001/03/05 10:47:11 obrien Exp $
18 * $DragonFly: src/lib/libcr/net/Attic/res_mkupdate.c,v 1.2 2003/06/17 04:26:44 dillon Exp $
22 * Based on the Dynamic DNS reference implementation by Viraj Bais
23 * <viraj_bais@ccm.fm.intel.com>
26 #include <sys/types.h>
27 #include <sys/param.h>
29 #include <netinet/in.h>
30 #include <arpa/nameser.h>
31 #include <arpa/inet.h>
42 #include "res_config.h"
44 static int getnum_str(u_char **, u_char *);
45 static int getword_str(char *, int, u_char **, u_char *);
47 #define ShrinkBuffer(x) if ((buflen -= x) < 0) return (-2);
50 * Form update packets.
51 * Returns the size of the resulting packet if no error
53 * returns -1 if error in reading a word/number in rdata
54 * portion for update packets
55 * -2 if length of buffer passed is insufficient
56 * -3 if zone section is not the first section in
57 * the linked list, or section order has a problem
58 * -4 on a number overflow
59 * -5 unknown operation or no records
62 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
63 ns_updrec *rrecp_start = rrecp_in;
65 u_char c, *cp, *cp1, *sp1, *sp2, *startp, *endp;
66 int n, i, j, found, soanum, multiline;
67 ns_updrec *rrecp, *tmprrecp, *recptr = NULL;
70 int section, numrrs = 0, counts[ns_s_max];
71 u_int16_t rtype, rclass;
73 u_char *dnptrs[20], **dpp, **lastdnptr;
75 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
76 h_errno = NETDB_INTERNAL;
81 * Initialize header fields.
83 if ((buf == NULL) || (buflen < HFIXEDSZ))
85 memset(buf, 0, HFIXEDSZ);
87 hp->id = htons(++_res.id);
88 hp->opcode = ns_o_update;
90 sp1 = buf + 2*INT16SZ; /* save pointer to zocount */
96 lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
98 if (rrecp_start == NULL)
100 else if (rrecp_start->r_section != S_ZONE)
103 memset(counts, 0, sizeof counts);
104 for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {
106 section = rrecp->r_section;
107 if (section < 0 || section >= ns_s_max)
110 for (i = section + 1; i < ns_s_max; i++)
113 rtype = rrecp->r_type;
114 rclass = rrecp->r_class;
116 /* overload class and type */
117 if (section == S_PREREQ) {
119 switch (rrecp->r_opcode) {
135 if (rrecp->r_size == 0)
140 "res_mkupdate: incorrect opcode: %d\n",
145 } else if (section == S_UPDATE) {
146 switch (rrecp->r_opcode) {
148 rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
154 "res_mkupdate: incorrect opcode: %d\n",
162 * XXX appending default domain to owner name is omitted,
163 * fqdn must be provided
165 if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
169 ShrinkBuffer(n + 2*INT16SZ);
171 PUTSHORT(rclass, cp);
172 if (section == S_ZONE) {
173 if (numrrs != 1 || rrecp->r_type != T_SOA)
177 ShrinkBuffer(INT32SZ + INT16SZ);
179 sp2 = cp; /* save pointer to length byte */
181 if (rrecp->r_size == 0) {
182 if (section == S_UPDATE && rclass != C_ANY)
189 startp = rrecp->r_data;
190 endp = startp + rrecp->r_size - 1;
191 /* XXX this should be done centrally. */
192 switch (rrecp->r_type) {
194 if (!getword_str(buf2, sizeof buf2, &startp, endp))
196 if (!inet_aton(buf2, &ina))
198 n1 = ntohl(ina.s_addr);
199 ShrinkBuffer(INT32SZ);
208 if (!getword_str(buf2, sizeof buf2, &startp, endp))
210 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
219 for (i = 0; i < 2; i++) {
220 if (!getword_str(buf2, sizeof buf2, &startp,
223 n = dn_comp(buf2, cp, buflen,
230 if (rrecp->r_type == T_SOA) {
231 ShrinkBuffer(5 * INT32SZ);
232 while (isspace(*startp) || !*startp)
234 if (*startp == '(') {
239 /* serial, refresh, retry, expire, minimum */
240 for (i = 0; i < 5; i++) {
241 soanum = getnum_str(&startp, endp);
247 while (isspace(*startp) || !*startp)
257 n = getnum_str(&startp, endp);
261 ShrinkBuffer(INT16SZ);
262 if (!getword_str(buf2, sizeof buf2, &startp, endp))
264 n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
271 n = getnum_str(&startp, endp);
275 ShrinkBuffer(INT16SZ);
276 for (i = 0; i < 2; i++) {
277 if (!getword_str(buf2, sizeof buf2, &startp,
280 n = dn_comp(buf2, cp, buflen, dnptrs,
295 /* XXX - more fine tuning needed here */
296 ShrinkBuffer(rrecp->r_size);
297 memcpy(cp, rrecp->r_data, rrecp->r_size);
303 n = (u_int16_t)((cp - sp2) - INT16SZ);
307 hp->qdcount = htons(counts[0]);
308 hp->ancount = htons(counts[1]);
309 hp->nscount = htons(counts[2]);
310 hp->arcount = htons(counts[3]);
315 * Get a whitespace delimited word from a string (not file)
316 * into buf. modify the start pointer to point after the
317 * word in the string.
320 getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
324 for (cp = buf; *startpp <= endp; ) {
326 if (isspace(c) || c == '\0') {
327 if (cp != buf) /* trailing whitespace */
329 else { /* leading whitespace */
335 if (cp >= buf+size-1)
344 * Get a whitespace delimited number from a string (not file) into buf
345 * update the start pointer to point after the number in the string.
348 getnum_str(u_char **startpp, u_char *endp) {
354 for (n = 0; *startpp <= endp; ) {
356 if (isspace(c) || c == '\0') {
357 if (seendigit) /* trailing whitespace */
359 else { /* leading whitespace */
365 while ((*startpp <= endp) &&
366 ((c = **startpp) != '\n'))
373 if (c == ')' && seendigit) {
380 n = n * 10 + (c - '0');
387 * Allocate a resource record buffer & save rr info.
390 res_mkupdrec(int section, const char *dname,
391 u_int class, u_int type, u_long ttl) {
392 ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
394 if (!rrecp || !(rrecp->r_dname = strdup(dname)))
396 rrecp->r_class = class;
397 rrecp->r_type = type;
399 rrecp->r_section = section;
404 * Free a resource record buffer created by res_mkupdrec.
407 res_freeupdrec(ns_updrec *rrecp) {
408 /* Note: freeing r_dp is the caller's responsibility. */
409 if (rrecp->r_dname != NULL)
410 free(rrecp->r_dname);