Merge branch 'vendor/DHCPCD'
[dragonfly.git] / lib / libc / nameser / ns_parse.c
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  * $Id: ns_parse.c,v 1.9 2007/08/27 03:32:26 marka Exp $
18  */
19
20 /* Import. */
21
22 #include "port_before.h"
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 #include "port_after.h"
34
35 /* Forward. */
36
37 static void     setsection(ns_msg *msg, ns_sect sect);
38
39 /* Macros. */
40
41 #if !defined(SOLARIS2) || defined(__COVERITY__)
42 #define RETERR(err) do { errno = (err); return (-1); } while (0)
43 #else
44 #define RETERR(err) \
45         do { errno = (err); if (errno == errno) return (-1); } while (0)
46 #endif
47
48 /* Public. */
49
50 /* These need to be in the same order as the nres.h:ns_flag enum. */
51 struct _ns_flagdata _ns_flagdata[16] = {
52         { 0x8000, 15 },         /*%< qr. */
53         { 0x7800, 11 },         /*%< opcode. */
54         { 0x0400, 10 },         /*%< aa. */
55         { 0x0200, 9 },          /*%< tc. */
56         { 0x0100, 8 },          /*%< rd. */
57         { 0x0080, 7 },          /*%< ra. */
58         { 0x0040, 6 },          /*%< z. */
59         { 0x0020, 5 },          /*%< ad. */
60         { 0x0010, 4 },          /*%< cd. */
61         { 0x000f, 0 },          /*%< rcode. */
62         { 0x0000, 0 },          /*%< expansion (1/6). */
63         { 0x0000, 0 },          /*%< expansion (2/6). */
64         { 0x0000, 0 },          /*%< expansion (3/6). */
65         { 0x0000, 0 },          /*%< expansion (4/6). */
66         { 0x0000, 0 },          /*%< expansion (5/6). */
67         { 0x0000, 0 },          /*%< expansion (6/6). */
68 };
69
70 int ns_msg_getflag(ns_msg handle, int flag) {
71         return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
72 }
73
74 int
75 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
76         const u_char *optr = ptr;
77
78         for ((void)NULL; count > 0; count--) {
79                 int b, rdlength;
80
81                 b = dn_skipname(ptr, eom);
82                 if (b < 0)
83                         RETERR(EMSGSIZE);
84                 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
85                 if (section != ns_s_qd) {
86                         if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
87                                 RETERR(EMSGSIZE);
88                         ptr += NS_INT32SZ/*TTL*/;
89                         NS_GET16(rdlength, ptr);
90                         ptr += rdlength/*RData*/;
91                 }
92         }
93         if (ptr > eom)
94                 RETERR(EMSGSIZE);
95         return (ptr - optr);
96 }
97
98 int
99 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
100         const u_char *eom = msg + msglen;
101         int i;
102
103         memset(handle, 0x5e, sizeof *handle);
104         handle->_msg = msg;
105         handle->_eom = eom;
106         if (msg + NS_INT16SZ > eom)
107                 RETERR(EMSGSIZE);
108         NS_GET16(handle->_id, msg);
109         if (msg + NS_INT16SZ > eom)
110                 RETERR(EMSGSIZE);
111         NS_GET16(handle->_flags, msg);
112         for (i = 0; i < ns_s_max; i++) {
113                 if (msg + NS_INT16SZ > eom)
114                         RETERR(EMSGSIZE);
115                 NS_GET16(handle->_counts[i], msg);
116         }
117         for (i = 0; i < ns_s_max; i++)
118                 if (handle->_counts[i] == 0)
119                         handle->_sections[i] = NULL;
120                 else {
121                         int b = ns_skiprr(msg, eom, (ns_sect)i,
122                                           handle->_counts[i]);
123
124                         if (b < 0)
125                                 return (-1);
126                         handle->_sections[i] = msg;
127                         msg += b;
128                 }
129         if (msg != eom)
130                 RETERR(EMSGSIZE);
131         setsection(handle, ns_s_max);
132         return (0);
133 }
134
135 int
136 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
137         int b;
138         int tmp;
139
140         /* Make section right. */
141         tmp = section;
142         if (tmp < 0 || section >= ns_s_max)
143                 RETERR(ENODEV);
144         if (section != handle->_sect)
145                 setsection(handle, section);
146
147         /* Make rrnum right. */
148         if (rrnum == -1)
149                 rrnum = handle->_rrnum;
150         if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
151                 RETERR(ENODEV);
152         if (rrnum < handle->_rrnum)
153                 setsection(handle, section);
154         if (rrnum > handle->_rrnum) {
155                 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
156                               rrnum - handle->_rrnum);
157
158                 if (b < 0)
159                         return (-1);
160                 handle->_msg_ptr += b;
161                 handle->_rrnum = rrnum;
162         }
163
164         /* Do the parse. */
165         b = dn_expand(handle->_msg, handle->_eom,
166                       handle->_msg_ptr, rr->name, NS_MAXDNAME);
167         if (b < 0)
168                 return (-1);
169         handle->_msg_ptr += b;
170         if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
171                 RETERR(EMSGSIZE);
172         NS_GET16(rr->type, handle->_msg_ptr);
173         NS_GET16(rr->rr_class, handle->_msg_ptr);
174         if (section == ns_s_qd) {
175                 rr->ttl = 0;
176                 rr->rdlength = 0;
177                 rr->rdata = NULL;
178         } else {
179                 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
180                         RETERR(EMSGSIZE);
181                 NS_GET32(rr->ttl, handle->_msg_ptr);
182                 NS_GET16(rr->rdlength, handle->_msg_ptr);
183                 if (handle->_msg_ptr + rr->rdlength > handle->_eom)
184                         RETERR(EMSGSIZE);
185                 rr->rdata = handle->_msg_ptr;
186                 handle->_msg_ptr += rr->rdlength;
187         }
188         if (++handle->_rrnum > handle->_counts[(int)section])
189                 setsection(handle, (ns_sect)((int)section + 1));
190
191         /* All done. */
192         return (0);
193 }
194
195 /* Private. */
196
197 static void
198 setsection(ns_msg *msg, ns_sect sect) {
199         msg->_sect = sect;
200         if (sect == ns_s_max) {
201                 msg->_rrnum = -1;
202                 msg->_msg_ptr = NULL;
203         } else {
204                 msg->_rrnum = 0;
205                 msg->_msg_ptr = msg->_sections[(int)sect];
206         }
207 }
208
209 /*! \file */