libspf2  1.2.11
__ns_initparse.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1996,1999 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17 
18 #ifndef lint
19 static const char rcsid[] = "$Id: ns_parse.c,v 1.3.2.1 2003/06/27 03:51:42 marka Exp $";
20 #endif
21 
22 /* Import. */
23 
24 /* #include "port_before.h" */
25 #include "config.h"
26 
27 #include <sys/types.h>
28 
29 #include <netinet/in.h>
30 #include "arpa_nameser.h"
31 
32 #include <errno.h>
33 /* #include <resolv.h> */
34 #ifdef HAVE_STRING_H
35 # include <string.h> /* strstr / strdup */
36 #else
37 # ifdef HAVE_STRINGS_H
38 # include <strings.h> /* strstr / strdup */
39 # endif
40 #endif
41 
42 
43 /* #include "port_after.h" */
44 
45 /* Forward. */
46 
47 static void setsection(ns_msg *msg, ns_sect sect);
48 
49 /* Macros. */
50 
51 #define RETERR(err) do { errno = (err); return (-1); } while (0)
52 
53 /* Public. */
54 
55 /* These need to be in the same order as the nres.h:ns_flag enum. */
56 struct _ns_flagdata _ns_flagdata[16] = {
57  { 0x8000, 15 }, /* qr. */
58  { 0x7800, 11 }, /* opcode. */
59  { 0x0400, 10 }, /* aa. */
60  { 0x0200, 9 }, /* tc. */
61  { 0x0100, 8 }, /* rd. */
62  { 0x0080, 7 }, /* ra. */
63  { 0x0040, 6 }, /* z. */
64  { 0x0020, 5 }, /* ad. */
65  { 0x0010, 4 }, /* cd. */
66  { 0x000f, 0 }, /* rcode. */
67  { 0x0000, 0 }, /* expansion (1/6). */
68  { 0x0000, 0 }, /* expansion (2/6). */
69  { 0x0000, 0 }, /* expansion (3/6). */
70  { 0x0000, 0 }, /* expansion (4/6). */
71  { 0x0000, 0 }, /* expansion (5/6). */
72  { 0x0000, 0 }, /* expansion (6/6). */
73 };
74 
75 int
76 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
77  const u_char *optr = ptr;
78 
79  for ((void)NULL; count > 0; count--) {
80  int b, rdlength;
81 
82  b = dn_skipname(ptr, eom);
83  if (b < 0)
84  RETERR(EMSGSIZE);
85  ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
86  if (section != ns_s_qd) {
87  if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
88  RETERR(EMSGSIZE);
89  ptr += NS_INT32SZ/*TTL*/;
90  NS_GET16(rdlength, ptr);
91  ptr += rdlength/*RData*/;
92  }
93  }
94  if (ptr > eom)
95  RETERR(EMSGSIZE);
96  return (ptr - optr);
97 }
98 
99 int
100 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
101  const u_char *eom = msg + msglen;
102  int i;
103 
104  memset(handle, 0x5e, sizeof *handle);
105  handle->_msg = msg;
106  handle->_eom = eom;
107  if (msg + NS_INT16SZ > eom)
108  RETERR(EMSGSIZE);
109  NS_GET16(handle->_id, msg);
110  if (msg + NS_INT16SZ > eom)
111  RETERR(EMSGSIZE);
112  NS_GET16(handle->_flags, msg);
113  for (i = 0; i < ns_s_max; i++) {
114  if (msg + NS_INT16SZ > eom)
115  RETERR(EMSGSIZE);
116  NS_GET16(handle->_counts[i], msg);
117  }
118  for (i = 0; i < ns_s_max; i++)
119  if (handle->_counts[i] == 0)
120  handle->_sections[i] = NULL;
121  else {
122  int b = ns_skiprr(msg, eom, (ns_sect)i,
123  handle->_counts[i]);
124 
125  if (b < 0)
126  return (-1);
127  handle->_sections[i] = msg;
128  msg += b;
129  }
130  if (msg != eom)
131  RETERR(EMSGSIZE);
132  setsection(handle, ns_s_max);
133  return (0);
134 }
135 
136 int
137 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
138  int b;
139  int tmp;
140 
141  /* Make section right. */
142  if ((tmp = section) < 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 }
const u_char * rdata
Definition: arpa_nameser.h:192
u_int16_t type
Definition: arpa_nameser.h:188
char name[NS_MAXDNAME]
Definition: arpa_nameser.h:187
int ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr)
#define NS_INT32SZ
Definition: arpa_nameser.h:134
u_int16_t _flags
Definition: arpa_nameser.h:164
u_int16_t _id
Definition: arpa_nameser.h:164
int ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count)
u_int16_t _counts[ns_s_max]
Definition: arpa_nameser.h:164
u_int32_t ttl
Definition: arpa_nameser.h:190
u_int16_t rdlength
Definition: arpa_nameser.h:191
ns_sect
Definition: arpa_nameser.h:146
#define RETERR(err)
const u_char * _msg_ptr
Definition: arpa_nameser.h:168
const u_char * _sections[ns_s_max]
Definition: arpa_nameser.h:165
#define dn_skipname
Definition: arpa_nameser.h:67
ns_sect _sect
Definition: arpa_nameser.h:166
int ns_initparse(const u_char *msg, int msglen, ns_msg *handle)
const u_char * _eom
Definition: arpa_nameser.h:163
const u_char * _msg
Definition: arpa_nameser.h:163
#define NULL
Definition: spf_internal.h:28
void *void * ptr
Definition: realloc.c:8
u_int16_t rr_class
Definition: arpa_nameser.h:189
#define NS_GET32(l, cp)
Definition: arpa_nameser.h:493
#define NS_INT16SZ
Definition: arpa_nameser.h:135
#define NS_MAXDNAME
Definition: arpa_nameser.h:127
NS_GET16(dst, src)
int _rrnum
Definition: arpa_nameser.h:167