1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1996 by Internet Software Consortium. |
---|
5 | * |
---|
6 | * Permission to use, copy, modify, and distribute this software for any |
---|
7 | * purpose with or without fee is hereby granted, provided that the above |
---|
8 | * copyright notice and this permission notice appear in all copies. |
---|
9 | * |
---|
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
---|
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
---|
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
---|
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
---|
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
---|
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
---|
17 | * SOFTWARE. |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <sys/types.h> |
---|
25 | |
---|
26 | #include <netinet/in.h> |
---|
27 | #include <arpa/nameser.h> |
---|
28 | |
---|
29 | #include <errno.h> |
---|
30 | #include <resolv.h> |
---|
31 | #include <string.h> |
---|
32 | |
---|
33 | /* These need to be in the same order as the nres.h:ns_flag enum. */ |
---|
34 | struct _ns_flagdata _ns_flagdata[16] = { |
---|
35 | { 0x8000, 15 }, /* qr. */ |
---|
36 | { 0x7800, 11 }, /* opcode. */ |
---|
37 | { 0x0400, 10 }, /* aa. */ |
---|
38 | { 0x0200, 9 }, /* tc. */ |
---|
39 | { 0x0100, 8 }, /* rd. */ |
---|
40 | { 0x0080, 7 }, /* ra. */ |
---|
41 | { 0x0040, 6 }, /* z. */ |
---|
42 | { 0x0020, 5 }, /* ad. */ |
---|
43 | { 0x0010, 4 }, /* cd. */ |
---|
44 | { 0x000f, 0 }, /* rcode. */ |
---|
45 | { 0x0000, 0 }, /* expansion (1/6). */ |
---|
46 | { 0x0000, 0 }, /* expansion (2/6). */ |
---|
47 | { 0x0000, 0 }, /* expansion (3/6). */ |
---|
48 | { 0x0000, 0 }, /* expansion (4/6). */ |
---|
49 | { 0x0000, 0 }, /* expansion (5/6). */ |
---|
50 | { 0x0000, 0 }, /* expansion (6/6). */ |
---|
51 | }; |
---|
52 | |
---|
53 | static int |
---|
54 | skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { |
---|
55 | const u_char *optr = ptr; |
---|
56 | |
---|
57 | for ((void)NULL; count > 0; count--) { |
---|
58 | int b, rdlength; |
---|
59 | |
---|
60 | b = dn_skipname(ptr, eom); |
---|
61 | if (b < 0) |
---|
62 | goto emsgsize; |
---|
63 | ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; |
---|
64 | if (section != ns_s_qd) { |
---|
65 | if (ptr + NS_INT32SZ > eom) |
---|
66 | goto emsgsize; |
---|
67 | ptr += NS_INT32SZ/*TTL*/; |
---|
68 | if (ptr + NS_INT16SZ > eom) |
---|
69 | goto emsgsize; |
---|
70 | NS_GET16(rdlength, ptr); |
---|
71 | ptr += rdlength/*RData*/; |
---|
72 | } |
---|
73 | } |
---|
74 | if (ptr > eom) |
---|
75 | goto emsgsize; |
---|
76 | return (ptr - optr); |
---|
77 | emsgsize: |
---|
78 | errno = EMSGSIZE; |
---|
79 | return (-1); |
---|
80 | } |
---|
81 | |
---|
82 | int |
---|
83 | ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { |
---|
84 | const u_char *eom = msg + msglen; |
---|
85 | int i; |
---|
86 | |
---|
87 | memset(handle, 0x5e, sizeof *handle); |
---|
88 | handle->_msg = msg; |
---|
89 | handle->_eom = eom; |
---|
90 | if (msg + NS_INT16SZ > eom) |
---|
91 | goto emsgsize; |
---|
92 | NS_GET16(handle->_id, msg); |
---|
93 | if (msg + NS_INT16SZ > eom) |
---|
94 | goto emsgsize; |
---|
95 | NS_GET16(handle->_flags, msg); |
---|
96 | for (i = 0; i < ns_s_max; i++) { |
---|
97 | if (msg + NS_INT16SZ > eom) |
---|
98 | goto emsgsize; |
---|
99 | NS_GET16(handle->_counts[i], msg); |
---|
100 | } |
---|
101 | for (i = 0; i < ns_s_max; i++) |
---|
102 | if (handle->_counts[i] == 0) |
---|
103 | handle->_sections[i] = NULL; |
---|
104 | else { |
---|
105 | int b = skiprr(msg, eom, (ns_sect)i, |
---|
106 | handle->_counts[i]); |
---|
107 | |
---|
108 | if (b < 0) |
---|
109 | return (-1); |
---|
110 | handle->_sections[i] = msg; |
---|
111 | msg += b; |
---|
112 | } |
---|
113 | if (msg != eom) |
---|
114 | goto emsgsize; |
---|
115 | handle->_sect = ns_s_max; |
---|
116 | handle->_rrnum = -1; |
---|
117 | handle->_ptr = NULL; |
---|
118 | return (0); |
---|
119 | emsgsize: |
---|
120 | errno = EMSGSIZE; |
---|
121 | return (-1); |
---|
122 | } |
---|
123 | |
---|
124 | int |
---|
125 | ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { |
---|
126 | int b; |
---|
127 | |
---|
128 | /* Make section right. */ |
---|
129 | if ( /* section < 0 || */ section >= ns_s_max) |
---|
130 | goto enodev; |
---|
131 | if ((int)section != (int)handle->_sect) { |
---|
132 | handle->_sect = section; |
---|
133 | handle->_rrnum = 0; |
---|
134 | handle->_ptr = handle->_sections[(int)section]; |
---|
135 | } |
---|
136 | |
---|
137 | /* Make rrnum right. */ |
---|
138 | if (rrnum == -1) |
---|
139 | rrnum = handle->_rrnum; |
---|
140 | if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) |
---|
141 | goto enodev; |
---|
142 | if (rrnum < handle->_rrnum) { |
---|
143 | handle->_rrnum = 0; |
---|
144 | handle->_ptr = handle->_sections[(int)section]; |
---|
145 | } |
---|
146 | |
---|
147 | b = skiprr(handle->_msg, handle->_eom, section, |
---|
148 | rrnum - handle->_rrnum); |
---|
149 | if (b < 0) |
---|
150 | return (-1); |
---|
151 | handle->_ptr += b; |
---|
152 | handle->_rrnum = rrnum; |
---|
153 | |
---|
154 | /* Do the parse. */ |
---|
155 | b = dn_expand(handle->_msg, handle->_eom, |
---|
156 | handle->_ptr, rr->name, NS_MAXDNAME); |
---|
157 | if (b < 0) |
---|
158 | return (-1); |
---|
159 | handle->_ptr += b; |
---|
160 | if (handle->_ptr + NS_INT16SZ > handle->_eom) |
---|
161 | goto emsgsize; |
---|
162 | NS_GET16(rr->type, handle->_ptr); |
---|
163 | if (handle->_ptr + NS_INT16SZ > handle->_eom) |
---|
164 | goto emsgsize; |
---|
165 | NS_GET16(rr->rr_class, handle->_ptr); |
---|
166 | if (section == ns_s_qd) { |
---|
167 | rr->ttl = 0; |
---|
168 | rr->rdlength = 0; |
---|
169 | rr->rdata = NULL; |
---|
170 | } else { |
---|
171 | if (handle->_ptr + NS_INT32SZ > handle->_eom) |
---|
172 | goto emsgsize; |
---|
173 | NS_GET32(rr->ttl, handle->_ptr); |
---|
174 | if (handle->_ptr + NS_INT16SZ > handle->_eom) |
---|
175 | goto emsgsize; |
---|
176 | NS_GET16(rr->rdlength, handle->_ptr); |
---|
177 | if (handle->_ptr + rr->rdlength > handle->_eom) |
---|
178 | goto emsgsize; |
---|
179 | rr->rdata = handle->_ptr; |
---|
180 | handle->_ptr += rr->rdlength; |
---|
181 | } |
---|
182 | handle->_rrnum++; |
---|
183 | |
---|
184 | /* All done. */ |
---|
185 | return (0); |
---|
186 | enodev: |
---|
187 | errno = ENODEV; |
---|
188 | return (-1); |
---|
189 | emsgsize: |
---|
190 | errno = EMSGSIZE; |
---|
191 | return (-1); |
---|
192 | } |
---|