source: rtems-libbsd/freebsd/lib/libc/nameser/ns_parse.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

  • Property mode set to 100644
File size: 7.1 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef lint
21static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
22#endif
23#include <sys/cdefs.h>
24__FBSDID("$FreeBSD$");
25
26/* Import. */
27
28#include "port_before.h"
29
30#include <sys/types.h>
31
32#include <netinet/in.h>
33#include <arpa/nameser.h>
34
35#include <errno.h>
36#include <resolv.h>
37#include <string.h>
38
39#include "port_after.h"
40
41/* Forward. */
42
43static void     setsection(ns_msg *msg, ns_sect sect);
44
45/* Macros. */
46
47#if !defined(SOLARIS2) || defined(__COVERITY__)
48#define RETERR(err) do { errno = (err); return (-1); } while (0)
49#else
50#define RETERR(err) \
51        do { errno = (err); if (errno == errno) return (-1); } while (0)
52#endif
53
54#define PARSE_FMT_PRESO 0       /* Parse using presentation-format names */
55#define PARSE_FMT_WIRE 1        /* Parse using network-format names */
56
57/* Public. */
58
59/* These need to be in the same order as the nres.h:ns_flag enum. */
60struct _ns_flagdata _ns_flagdata[16] = {
61        { 0x8000, 15 },         /*%< qr. */
62        { 0x7800, 11 },         /*%< opcode. */
63        { 0x0400, 10 },         /*%< aa. */
64        { 0x0200, 9 },          /*%< tc. */
65        { 0x0100, 8 },          /*%< rd. */
66        { 0x0080, 7 },          /*%< ra. */
67        { 0x0040, 6 },          /*%< z. */
68        { 0x0020, 5 },          /*%< ad. */
69        { 0x0010, 4 },          /*%< cd. */
70        { 0x000f, 0 },          /*%< rcode. */
71        { 0x0000, 0 },          /*%< expansion (1/6). */
72        { 0x0000, 0 },          /*%< expansion (2/6). */
73        { 0x0000, 0 },          /*%< expansion (3/6). */
74        { 0x0000, 0 },          /*%< expansion (4/6). */
75        { 0x0000, 0 },          /*%< expansion (5/6). */
76        { 0x0000, 0 },          /*%< expansion (6/6). */
77};
78
79int ns_msg_getflag(ns_msg handle, int flag) {
80        return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
81}
82
83int
84ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
85        const u_char *optr = ptr;
86
87        for ((void)NULL; count > 0; count--) {
88                int b, rdlength;
89
90                b = dn_skipname(ptr, eom);
91                if (b < 0)
92                        RETERR(EMSGSIZE);
93                ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
94                if (section != ns_s_qd) {
95                        if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
96                                RETERR(EMSGSIZE);
97                        ptr += NS_INT32SZ/*TTL*/;
98                        NS_GET16(rdlength, ptr);
99                        ptr += rdlength/*RData*/;
100                }
101        }
102        if (ptr > eom)
103                RETERR(EMSGSIZE);
104        return (ptr - optr);
105}
106
107int
108ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
109        const u_char *eom = msg + msglen;
110        int i;
111
112        handle->_msg = msg;
113        handle->_eom = eom;
114        if (msg + NS_INT16SZ > eom)
115                RETERR(EMSGSIZE);
116        NS_GET16(handle->_id, msg);
117        if (msg + NS_INT16SZ > eom)
118                RETERR(EMSGSIZE);
119        NS_GET16(handle->_flags, msg);
120        for (i = 0; i < ns_s_max; i++) {
121                if (msg + NS_INT16SZ > eom)
122                        RETERR(EMSGSIZE);
123                NS_GET16(handle->_counts[i], msg);
124        }
125        for (i = 0; i < ns_s_max; i++)
126                if (handle->_counts[i] == 0)
127                        handle->_sections[i] = NULL;
128                else {
129                        int b = ns_skiprr(msg, eom, (ns_sect)i,
130                                          handle->_counts[i]);
131
132                        if (b < 0)
133                                return (-1);
134                        handle->_sections[i] = msg;
135                        msg += b;
136                }
137        if (msg != eom)
138                RETERR(EMSGSIZE);
139        setsection(handle, ns_s_max);
140        return (0);
141}
142
143int
144ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
145        int b;
146        int tmp;
147
148        /* Make section right. */
149        tmp = section;
150        if (tmp < 0 || section >= ns_s_max)
151                RETERR(ENODEV);
152        if (section != handle->_sect)
153                setsection(handle, section);
154
155        /* Make rrnum right. */
156        if (rrnum == -1)
157                rrnum = handle->_rrnum;
158        if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
159                RETERR(ENODEV);
160        if (rrnum < handle->_rrnum)
161                setsection(handle, section);
162        if (rrnum > handle->_rrnum) {
163                b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
164                              rrnum - handle->_rrnum);
165
166                if (b < 0)
167                        return (-1);
168                handle->_msg_ptr += b;
169                handle->_rrnum = rrnum;
170        }
171
172        /* Do the parse. */
173        b = dn_expand(handle->_msg, handle->_eom,
174                      handle->_msg_ptr, rr->name, NS_MAXDNAME);
175        if (b < 0)
176                return (-1);
177        handle->_msg_ptr += b;
178        if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
179                RETERR(EMSGSIZE);
180        NS_GET16(rr->type, handle->_msg_ptr);
181        NS_GET16(rr->rr_class, handle->_msg_ptr);
182        if (section == ns_s_qd) {
183                rr->ttl = 0;
184                rr->rdlength = 0;
185                rr->rdata = NULL;
186        } else {
187                if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
188                        RETERR(EMSGSIZE);
189                NS_GET32(rr->ttl, handle->_msg_ptr);
190                NS_GET16(rr->rdlength, handle->_msg_ptr);
191                if (handle->_msg_ptr + rr->rdlength > handle->_eom)
192                        RETERR(EMSGSIZE);
193                rr->rdata = handle->_msg_ptr;
194                handle->_msg_ptr += rr->rdlength;
195        }
196        if (++handle->_rrnum > handle->_counts[(int)section])
197                setsection(handle, (ns_sect)((int)section + 1));
198
199        /* All done. */
200        return (0);
201}
202
203/*
204 * This is identical to the above but uses network-format (uncompressed) names.
205 */
206int
207ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
208        int b;
209        int tmp;
210
211        /* Make section right. */
212        if ((tmp = section) < 0 || section >= ns_s_max)
213                RETERR(ENODEV);
214        if (section != handle->_sect)
215                setsection(handle, section);
216
217        /* Make rrnum right. */
218        if (rrnum == -1)
219                rrnum = handle->_rrnum;
220        if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
221                RETERR(ENODEV);
222        if (rrnum < handle->_rrnum)
223                setsection(handle, section);
224        if (rrnum > handle->_rrnum) {
225                b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
226                              rrnum - handle->_rrnum);
227
228                if (b < 0)
229                        return (-1);
230                handle->_msg_ptr += b;
231                handle->_rrnum = rrnum;
232        }
233
234        /* Do the parse. */
235        b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
236                            rr->nname, NS_MAXNNAME, &rr->nnamel);
237        if (b < 0)
238                return (-1);
239        handle->_msg_ptr += b;
240        if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
241                RETERR(EMSGSIZE);
242        NS_GET16(rr->type, handle->_msg_ptr);
243        NS_GET16(rr->rr_class, handle->_msg_ptr);
244        if (section == ns_s_qd) {
245                rr->ttl = 0;
246                rr->rdlength = 0;
247                rr->rdata = NULL;
248        } else {
249                if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
250                        RETERR(EMSGSIZE);
251                NS_GET32(rr->ttl, handle->_msg_ptr);
252                NS_GET16(rr->rdlength, handle->_msg_ptr);
253                if (handle->_msg_ptr + rr->rdlength > handle->_eom)
254                        RETERR(EMSGSIZE);
255                rr->rdata = handle->_msg_ptr;
256                handle->_msg_ptr += rr->rdlength;
257        }
258        if (++handle->_rrnum > handle->_counts[(int)section])
259                setsection(handle, (ns_sect)((int)section + 1));
260
261        /* All done. */
262        return (0);
263}
264
265/* Private. */
266
267static void
268setsection(ns_msg *msg, ns_sect sect) {
269        msg->_sect = sect;
270        if (sect == ns_s_max) {
271                msg->_rrnum = -1;
272                msg->_msg_ptr = NULL;
273        } else {
274                msg->_rrnum = 0;
275                msg->_msg_ptr = msg->_sections[(int)sect];
276        }
277}
278
279/*! \file */
Note: See TracBrowser for help on using the repository browser.