1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18: #if !defined(_LIBC) && !defined(lint)
19: static const char rcsid[] = "$BINDId: ns_parse.c,v 8.13 1999/10/13 16:39:35 vixie Exp $";
20: #endif
21:
22:
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:
34:
35: static void setsection(ns_msg *msg, ns_sect sect);
36:
37:
38:
39: #define RETERR(err) do { __set_errno (err); return (-1); } while (0)
40:
41:
42:
43:
44: struct _ns_flagdata _ns_flagdata[16] = {
45: { 0x8000, 15 },
46: { 0x7800, 11 },
47: { 0x0400, 10 },
48: { 0x0200, 9 },
49: { 0x0100, 8 },
50: { 0x0080, 7 },
51: { 0x0040, 6 },
52: { 0x0020, 5 },
53: { 0x0010, 4 },
54: { 0x000f, 0 },
55: { 0x0000, 0 },
56: { 0x0000, 0 },
57: { 0x0000, 0 },
58: { 0x0000, 0 },
59: { 0x0000, 0 },
60: { 0x0000, 0 },
61: };
62:
63: int
64: ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
65: const u_char *optr = ptr;
66:
67: for ((void)NULL; count > 0; count--) {
68: int b, rdlength;
69:
70: b = dn_skipname(ptr, eom);
71: if (b < 0)
72: RETERR(EMSGSIZE);
73: ptr += b + NS_INT16SZ + NS_INT16SZ;
74: if (section != ns_s_qd) {
75: if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
76: RETERR(EMSGSIZE);
77: ptr += NS_INT32SZ;
78: NS_GET16(rdlength, ptr);
79: ptr += rdlength;
80: }
81: }
82: if (ptr > eom)
83: RETERR(EMSGSIZE);
84: return (ptr - optr);
85: }
86:
87: int
88: ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
89: const u_char *eom = msg + msglen;
90: int i;
91:
92: memset(handle, 0x5e, sizeof *handle);
93: handle->_msg = msg;
94: handle->_eom = eom;
95: if (msg + NS_INT16SZ > eom)
96: RETERR(EMSGSIZE);
97: NS_GET16(handle->_id, msg);
98: if (msg + NS_INT16SZ > eom)
99: RETERR(EMSGSIZE);
100: NS_GET16(handle->_flags, msg);
101: for (i = 0; i < ns_s_max; i++) {
102: if (msg + NS_INT16SZ > eom)
103: RETERR(EMSGSIZE);
104: NS_GET16(handle->_counts[i], msg);
105: }
106: for (i = 0; i < ns_s_max; i++)
107: if (handle->_counts[i] == 0)
108: handle->_sections[i] = NULL;
109: else {
110: int b = ns_skiprr(msg, eom, (ns_sect)i,
111: handle->_counts[i]);
112:
113: if (b < 0)
114: return (-1);
115: handle->_sections[i] = msg;
116: msg += b;
117: }
118: if (msg != eom)
119: RETERR(EMSGSIZE);
120: setsection(handle, ns_s_max);
121: return (0);
122: }
123:
124: int
125: ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
126: int b;
127:
128:
129: if (section < 0 || section >= ns_s_max)
130: RETERR(ENODEV);
131: if (section != handle->_sect)
132: setsection(handle, section);
133:
134:
135: if (rrnum == -1)
136: rrnum = handle->_rrnum;
137: if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
138: RETERR(ENODEV);
139: if (rrnum < handle->_rrnum)
140: setsection(handle, section);
141: if (rrnum > handle->_rrnum) {
142: b = ns_skiprr(handle->_ptr, handle->_eom, section,
143: rrnum - handle->_rrnum);
144:
145: if (b < 0)
146: return (-1);
147: handle->_ptr += b;
148: handle->_rrnum = rrnum;
149: }
150:
151:
152: b = dn_expand(handle->_msg, handle->_eom,
153: handle->_ptr, rr->name, NS_MAXDNAME);
154: if (b < 0)
155: return (-1);
156: handle->_ptr += b;
157: if (handle->_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
158: RETERR(EMSGSIZE);
159: NS_GET16(rr->type, handle->_ptr);
160: NS_GET16(rr->rr_class, handle->_ptr);
161: if (section == ns_s_qd) {
162: rr->ttl = 0;
163: rr->rdlength = 0;
164: rr->rdata = NULL;
165: } else {
166: if (handle->_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
167: RETERR(EMSGSIZE);
168: NS_GET32(rr->ttl, handle->_ptr);
169: NS_GET16(rr->rdlength, handle->_ptr);
170: if (handle->_ptr + rr->rdlength > handle->_eom)
171: RETERR(EMSGSIZE);
172: rr->rdata = handle->_ptr;
173: handle->_ptr += rr->rdlength;
174: }
175: if (++handle->_rrnum > handle->_counts[(int)section])
176: setsection(handle, (ns_sect)((int)section + 1));
177:
178:
179: return (0);
180: }
181:
182:
183:
184: static void
185: setsection(ns_msg *msg, ns_sect sect) {
186: msg->_sect = sect;
187: if (sect == ns_s_max) {
188: msg->_rrnum = -1;
189: msg->_ptr = NULL;
190: } else {
191: msg->_rrnum = 0;
192: msg->_ptr = msg->_sections[(int)sect];
193: }
194: }