OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [libc/] [res_mkupdate.c] - Blame information for rev 173

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.