source: rtems/cpukit/libnetworking/libc/res_mkupdate.c @ 09fdb5e8

4.104.114.84.95
Last change on this file since 09fdb5e8 was 09fdb5e8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/30/07 at 05:15:58

Eliminate SCCS, LINT. Add HAVE_CONFIG_H.

  • Property mode set to 100644
File size: 10.2 KB
Line 
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 * Based on the Dynamic DNS reference implementation by Viraj Bais
20 * <viraj_bais@ccm.fm.intel.com>
21 */
22
23#if HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <sys/types.h>
28#include <sys/param.h>
29
30#include <netinet/in.h>
31#include <arpa/nameser.h>
32#include <arpa/inet.h>
33
34#include <errno.h>
35#include <limits.h>
36#include <netdb.h>
37#include <resolv.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <ctype.h>
43
44#include "res_config.h"
45
46static int getnum_str(u_char **, u_char *);
47static int getword_str(char *, int, u_char **, u_char *);
48
49#define ShrinkBuffer(x)  if ((buflen -= x) < 0) return (-2);
50
51/*
52 * Form update packets.
53 * Returns the size of the resulting packet if no error
54 * On error,
55 *      returns -1 if error in reading a word/number in rdata
56 *                 portion for update packets
57 *              -2 if length of buffer passed is insufficient
58 *              -3 if zone section is not the first section in
59 *                 the linked list, or section order has a problem
60 *              -4 on a number overflow
61 *              -5 unknown operation or no records
62 */
63int
64res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
65        ns_updrec *rrecp_start = rrecp_in;
66        HEADER *hp;
67        u_char *cp, *sp1, *sp2, *startp, *endp;
68        int n, i, soanum, multiline;
69        ns_updrec *rrecp;
70        struct in_addr ina;
71        char buf2[MAXDNAME];
72        int section, numrrs = 0, counts[ns_s_max];
73        u_int16_t rtype, rclass;
74        u_int32_t n1, rttl;
75        u_char *dnptrs[20], **dpp, **lastdnptr;
76
77        if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
78                h_errno = NETDB_INTERNAL;
79                return (-1);
80        }
81
82        /*
83         * Initialize header fields.
84         */
85        if ((buf == NULL) || (buflen < HFIXEDSZ))
86                return (-1);
87        memset(buf, 0, HFIXEDSZ);
88        hp = (HEADER *) buf;
89        hp->id = htons(++_res.id);
90        hp->opcode = ns_o_update;
91        hp->rcode = NOERROR;
92        sp1 = buf + 2*INT16SZ;  /* save pointer to zocount */
93        cp = buf + HFIXEDSZ;
94        buflen -= HFIXEDSZ;
95        dpp = dnptrs;
96        *dpp++ = buf;
97        *dpp++ = NULL;
98        lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0];
99
100        if (rrecp_start == NULL)
101                return (-5);
102        else if (rrecp_start->r_section != S_ZONE)
103                return (-3);
104
105        memset(counts, 0, sizeof counts);
106        for (rrecp = rrecp_start; rrecp; rrecp = rrecp->r_grpnext) {
107                numrrs++;
108                section = rrecp->r_section;
109                if (section < 0 || section >= ns_s_max)
110                        return (-1);
111                counts[section]++;
112                for (i = section + 1; i < ns_s_max; i++)
113                        if (counts[i])
114                                return (-3);
115                rtype = rrecp->r_type;
116                rclass = rrecp->r_class;
117                rttl = rrecp->r_ttl;
118                /* overload class and type */
119                if (section == S_PREREQ) {
120                        rttl = 0;
121                        switch (rrecp->r_opcode) {
122                        case YXDOMAIN:
123                                rclass = C_ANY;
124                                rtype = T_ANY;
125                                rrecp->r_size = 0;
126                                break;
127                        case NXDOMAIN:
128                                rclass = C_NONE;
129                                rtype = T_ANY;
130                                rrecp->r_size = 0;
131                                break;
132                        case NXRRSET:
133                                rclass = C_NONE;
134                                rrecp->r_size = 0;
135                                break;
136                        case YXRRSET:
137                                if (rrecp->r_size == 0)
138                                        rclass = C_ANY;
139                                break;
140                        default:
141                                fprintf(stderr,
142                                        "res_mkupdate: incorrect opcode: %d\n",
143                                        rrecp->r_opcode);
144                                fflush(stderr);
145                                return (-1);
146                        }
147                } else if (section == S_UPDATE) {
148                        switch (rrecp->r_opcode) {
149                        case DELETE:
150                                rclass = rrecp->r_size == 0 ? C_ANY : C_NONE;
151                                break;
152                        case ADD:
153                                break;
154                        default:
155                                fprintf(stderr,
156                                        "res_mkupdate: incorrect opcode: %d\n",
157                                        rrecp->r_opcode);
158                                fflush(stderr);
159                                return (-1);
160                        }
161                }
162
163                /*
164                 * XXX  appending default domain to owner name is omitted,
165                 *      fqdn must be provided
166                 */
167                if ((n = dn_comp(rrecp->r_dname, cp, buflen, dnptrs,
168                                 lastdnptr)) < 0)
169                        return (-1);
170                cp += n;
171                ShrinkBuffer(n + 2*INT16SZ);
172                PUTSHORT(rtype, cp);
173                PUTSHORT(rclass, cp);
174                if (section == S_ZONE) {
175                        if (numrrs != 1 || rrecp->r_type != T_SOA)
176                                return (-3);
177                        continue;
178                }
179                ShrinkBuffer(INT32SZ + INT16SZ);
180                PUTLONG(rttl, cp);
181                sp2 = cp;  /* save pointer to length byte */
182                cp += INT16SZ;
183                if (rrecp->r_size == 0) {
184                        if (section == S_UPDATE && rclass != C_ANY)
185                                return (-1);
186                        else {
187                                PUTSHORT(0, sp2);
188                                continue;
189                        }
190                }
191                startp = rrecp->r_data;
192                endp = startp + rrecp->r_size - 1;
193                /* XXX this should be done centrally. */
194                switch (rrecp->r_type) {
195                case T_A:
196                        if (!getword_str(buf2, sizeof buf2, &startp, endp))
197                                return (-1);
198                        if (!inet_aton(buf2, &ina))
199                                return (-1);
200                        n1 = ntohl(ina.s_addr);
201                        ShrinkBuffer(INT32SZ);
202                        PUTLONG(n1, cp);
203                        break;
204                case T_CNAME:
205                case T_MB:
206                case T_MG:
207                case T_MR:
208                case T_NS:
209                case T_PTR:
210                        if (!getword_str(buf2, sizeof buf2, &startp, endp))
211                                return (-1);
212                        n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
213                        if (n < 0)
214                                return (-1);
215                        cp += n;
216                        ShrinkBuffer(n);
217                        break;
218                case T_MINFO:
219                case T_SOA:
220                case T_RP:
221                        for (i = 0; i < 2; i++) {
222                                if (!getword_str(buf2, sizeof buf2, &startp,
223                                                 endp))
224                                return (-1);
225                                n = dn_comp(buf2, cp, buflen,
226                                            dnptrs, lastdnptr);
227                                if (n < 0)
228                                        return (-1);
229                                cp += n;
230                                ShrinkBuffer(n);
231                        }
232                        if (rrecp->r_type == T_SOA) {
233                                ShrinkBuffer(5 * INT32SZ);
234                                while (isspace(*startp) || !*startp)
235                                        startp++;
236                                if (*startp == '(') {
237                                        multiline = 1;
238                                        startp++;
239                                } else
240                                        multiline = 0;
241                                /* serial, refresh, retry, expire, minimum */
242                                for (i = 0; i < 5; i++) {
243                                        soanum = getnum_str(&startp, endp);
244                                        if (soanum < 0)
245                                                return (-1);
246                                        PUTLONG(soanum, cp);
247                                }
248                                if (multiline) {
249                                        while (isspace(*startp) || !*startp)
250                                                startp++;
251                                        if (*startp != ')')
252                                                return (-1);
253                                }
254                        }
255                        break;
256                case T_MX:
257                case T_AFSDB:
258                case T_RT:
259                        n = getnum_str(&startp, endp);
260                        if (n < 0)
261                                return (-1);
262                        PUTSHORT(n, cp);
263                        ShrinkBuffer(INT16SZ);
264                        if (!getword_str(buf2, sizeof buf2, &startp, endp))
265                                return (-1);
266                        n = dn_comp(buf2, cp, buflen, dnptrs, lastdnptr);
267                        if (n < 0)
268                                return (-1);
269                        cp += n;
270                        ShrinkBuffer(n);
271                        break;
272                case T_PX:
273                        n = getnum_str(&startp, endp);
274                        if (n < 0)
275                                return (-1);
276                        PUTSHORT(n, cp);
277                        ShrinkBuffer(INT16SZ);
278                        for (i = 0; i < 2; i++) {
279                                if (!getword_str(buf2, sizeof buf2, &startp,
280                                                 endp))
281                                        return (-1);
282                                n = dn_comp(buf2, cp, buflen, dnptrs,
283                                            lastdnptr);
284                                if (n < 0)
285                                        return (-1);
286                                cp += n;
287                                ShrinkBuffer(n);
288                        }
289                        break;
290                case T_WKS:
291                case T_HINFO:
292                case T_TXT:
293                case T_X25:
294                case T_ISDN:
295                case T_NSAP:
296                case T_LOC:
297                        /* XXX - more fine tuning needed here */
298                        ShrinkBuffer(rrecp->r_size);
299                        memcpy(cp, rrecp->r_data, rrecp->r_size);
300                        cp += rrecp->r_size;
301                        break;
302                default:
303                        return (-1);
304                } /*switch*/
305                n = (u_int16_t)((cp - sp2) - INT16SZ);
306                PUTSHORT(n, sp2);
307        } /*for*/
308               
309        hp->qdcount = htons(counts[0]);
310        hp->ancount = htons(counts[1]);
311        hp->nscount = htons(counts[2]);
312        hp->arcount = htons(counts[3]);
313        return (cp - buf);
314}
315
316/*
317 * Get a whitespace delimited word from a string (not file)
318 * into buf. modify the start pointer to point after the
319 * word in the string.
320 */
321static int
322getword_str(char *buf, int size, u_char **startpp, u_char *endp) {
323        char *cp;
324        int c;
325 
326        for (cp = buf; *startpp <= endp; ) {
327                c = **startpp;
328                if (isspace(c) || c == '\0') {
329                        if (cp != buf) /* trailing whitespace */
330                                break;
331                        else { /* leading whitespace */
332                                (*startpp)++;
333                                continue;
334                        }
335                }
336                (*startpp)++;
337                if (cp >= buf+size-1)
338                        break;
339                *cp++ = (u_char)c;
340        }
341        *cp = '\0';
342        return (cp != buf);
343}
344
345/*
346 * Get a whitespace delimited number from a string (not file) into buf
347 * update the start pointer to point after the number in the string.
348 */
349static int
350getnum_str(u_char **startpp, u_char *endp) {
351        int c, n;
352        int seendigit = 0;
353        int m = 0;
354
355        for (n = 0; *startpp <= endp; ) {
356                c = **startpp;
357                if (isspace(c) || c == '\0') {
358                        if (seendigit) /* trailing whitespace */
359                                break;
360                        else { /* leading whitespace */
361                                (*startpp)++;
362                                continue;
363                        }
364                }
365                if (c == ';') {
366                        while ((*startpp <= endp) &&
367                               ((c = **startpp) != '\n'))
368                                        (*startpp)++;
369                        if (seendigit)
370                                break;
371                        continue;
372                }
373                if (!isdigit(c)) {
374                        if (c == ')' && seendigit) {
375                                (*startpp)--;
376                                break;
377                        }
378                        return (-1);
379                }       
380                (*startpp)++;
381                n = n * 10 + (c - '0');
382                seendigit = 1;
383        }
384        return (n + m);
385}
386
387/*
388 * Allocate a resource record buffer & save rr info.
389 */
390ns_updrec *
391res_mkupdrec(int section, const char *dname,
392             u_int class, u_int type, u_long ttl) {
393        ns_updrec *rrecp = (ns_updrec *)calloc(1, sizeof(ns_updrec));
394
395        if (!rrecp || !(rrecp->r_dname = strdup(dname)))
396                return (NULL);
397        rrecp->r_class = class;
398        rrecp->r_type = type;
399        rrecp->r_ttl = ttl;
400        rrecp->r_section = section;
401        return (rrecp);
402}
403
404/*
405 * Free a resource record buffer created by res_mkupdrec.
406 */
407void
408res_freeupdrec(ns_updrec *rrecp) {
409        /* Note: freeing r_dp is the caller's responsibility. */
410        if (rrecp->r_dname != NULL)
411                free(rrecp->r_dname);
412        free(rrecp);
413}
Note: See TracBrowser for help on using the repository browser.