tst-leaks: raise timeout to 5 seconds
[glibc.git] / resolv / ns_parse.c
blob712469be1d88c58ad475a432c0468a00b35818fe
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996,1999 by Internet Software Consortium.
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.
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.
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
22 /* Import. */
24 #include <sys/types.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
29 #include <errno.h>
30 #include <resolv.h>
31 #include <string.h>
33 /* Forward. */
35 static void setsection(ns_msg *msg, ns_sect sect);
37 /* Macros. */
39 #define RETERR(err) do { __set_errno (err); return (-1); } while (0)
41 /* Public. */
43 /* These need to be in the same order as the nres.h:ns_flag enum. */
44 const struct _ns_flagdata _ns_flagdata[16] = {
45 { 0x8000, 15 }, /*%< qr. */
46 { 0x7800, 11 }, /*%< opcode. */
47 { 0x0400, 10 }, /*%< aa. */
48 { 0x0200, 9 }, /*%< tc. */
49 { 0x0100, 8 }, /*%< rd. */
50 { 0x0080, 7 }, /*%< ra. */
51 { 0x0040, 6 }, /*%< z. */
52 { 0x0020, 5 }, /*%< ad. */
53 { 0x0010, 4 }, /*%< cd. */
54 { 0x000f, 0 }, /*%< rcode. */
55 { 0x0000, 0 }, /*%< expansion (1/6). */
56 { 0x0000, 0 }, /*%< expansion (2/6). */
57 { 0x0000, 0 }, /*%< expansion (3/6). */
58 { 0x0000, 0 }, /*%< expansion (4/6). */
59 { 0x0000, 0 }, /*%< expansion (5/6). */
60 { 0x0000, 0 }, /*%< expansion (6/6). */
63 #undef ns_msg_getflag
64 int ns_msg_getflag(ns_msg handle, int flag) {
65 return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
68 int
69 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
70 const u_char *optr = ptr;
72 for ((void)NULL; count > 0; count--) {
73 int b, rdlength;
75 b = dn_skipname(ptr, eom);
76 if (b < 0)
77 RETERR(EMSGSIZE);
78 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
79 if (section != ns_s_qd) {
80 if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
81 RETERR(EMSGSIZE);
82 ptr += NS_INT32SZ/*TTL*/;
83 NS_GET16(rdlength, ptr);
84 ptr += rdlength/*RData*/;
87 if (ptr > eom)
88 RETERR(EMSGSIZE);
89 return (ptr - optr);
91 libresolv_hidden_def (ns_skiprr)
93 int
94 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
95 const u_char *eom = msg + msglen;
96 int i;
98 memset(handle, 0x5e, sizeof *handle);
99 handle->_msg = msg;
100 handle->_eom = eom;
101 if (msg + NS_INT16SZ > eom)
102 RETERR(EMSGSIZE);
103 NS_GET16(handle->_id, msg);
104 if (msg + NS_INT16SZ > eom)
105 RETERR(EMSGSIZE);
106 NS_GET16(handle->_flags, msg);
107 for (i = 0; i < ns_s_max; i++) {
108 if (msg + NS_INT16SZ > eom)
109 RETERR(EMSGSIZE);
110 NS_GET16(handle->_counts[i], msg);
112 for (i = 0; i < ns_s_max; i++)
113 if (handle->_counts[i] == 0)
114 handle->_sections[i] = NULL;
115 else {
116 int b = ns_skiprr(msg, eom, (ns_sect)i,
117 handle->_counts[i]);
119 if (b < 0)
120 return (-1);
121 handle->_sections[i] = msg;
122 msg += b;
124 if (msg != eom)
125 RETERR(EMSGSIZE);
126 setsection(handle, ns_s_max);
127 return (0);
129 libresolv_hidden_def (ns_initparse)
132 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
133 int b;
134 int tmp;
136 /* Make section right. */
137 tmp = section;
138 if (tmp < 0 || section >= ns_s_max)
139 RETERR(ENODEV);
140 if (section != handle->_sect)
141 setsection(handle, section);
143 /* Make rrnum right. */
144 if (rrnum == -1)
145 rrnum = handle->_rrnum;
146 if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
147 RETERR(ENODEV);
148 if (rrnum < handle->_rrnum)
149 setsection(handle, section);
150 if (rrnum > handle->_rrnum) {
151 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
152 rrnum - handle->_rrnum);
154 if (b < 0)
155 return (-1);
156 handle->_msg_ptr += b;
157 handle->_rrnum = rrnum;
160 /* Do the parse. */
161 b = dn_expand(handle->_msg, handle->_eom,
162 handle->_msg_ptr, rr->name, NS_MAXDNAME);
163 if (b < 0)
164 return (-1);
165 handle->_msg_ptr += b;
166 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
167 RETERR(EMSGSIZE);
168 NS_GET16(rr->type, handle->_msg_ptr);
169 NS_GET16(rr->rr_class, handle->_msg_ptr);
170 if (section == ns_s_qd) {
171 rr->ttl = 0;
172 rr->rdlength = 0;
173 rr->rdata = NULL;
174 } else {
175 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
176 RETERR(EMSGSIZE);
177 NS_GET32(rr->ttl, handle->_msg_ptr);
178 NS_GET16(rr->rdlength, handle->_msg_ptr);
179 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
180 RETERR(EMSGSIZE);
181 rr->rdata = handle->_msg_ptr;
182 handle->_msg_ptr += rr->rdlength;
184 if (++handle->_rrnum > handle->_counts[(int)section])
185 setsection(handle, (ns_sect)((int)section + 1));
187 /* All done. */
188 return (0);
190 libresolv_hidden_def (ns_parserr)
192 /* Private. */
194 static void
195 setsection(ns_msg *msg, ns_sect sect) {
196 msg->_sect = sect;
197 if (sect == ns_s_max) {
198 msg->_rrnum = -1;
199 msg->_msg_ptr = NULL;
200 } else {
201 msg->_rrnum = 0;
202 msg->_msg_ptr = msg->_sections[(int)sect];
206 /*! \file */