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