ns_parse.c 4.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 1996,1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include "config.h"

20 21
#ifdef HAVE_RESOLV

22 23 24 25 26 27 28 29 30 31 32 33 34
#include <sys/types.h>

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
# include <arpa/nameser.h>
#endif

#include <errno.h>
#ifdef HAVE_RESOLV_H
# include <resolv.h>
#endif
35
#include <stdarg.h>
36 37
#include <string.h>

38
#include "windef.h"
39 40 41 42
#include "winbase.h"
#include "winnls.h"

#include "dnsapi.h"
43

44 45 46 47 48 49 50 51
/* Forward. */

static void	setsection(ns_msg *msg, ns_sect sect);

/* Macros. */

#define RETERR(err) do { return (-1); } while (0)

52 53 54 55 56 57
#ifdef HAVE_NS_MSG__MSG_PTR
# define NS_PTR(ns_msg) ((ns_msg)->_msg_ptr)
#else
# define NS_PTR(ns_msg) ((ns_msg)->_ptr)
#endif

58
#define DNS_NS_GET16(s, cp) do { \
59
    register const u_char *t_cp = (cp); \
60 61
    (s) = ((WORD)t_cp[0] << 8) \
        | ((WORD)t_cp[1]) \
62 63 64 65 66
        ; \
    (cp) += NS_INT16SZ; \
} while (0)

#define DNS_NS_GET32(l, cp) do { \
67
    register const u_char *t_cp = (cp); \
68 69 70 71
    (l) = ((DWORD)t_cp[0] << 24) \
        | ((DWORD)t_cp[1] << 16) \
        | ((DWORD)t_cp[2] << 8) \
        | ((DWORD)t_cp[3]) \
72 73 74 75
        ; \
    (cp) += NS_INT32SZ; \
} while (0)

76 77
/* Public. */

78 79
static int
dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
80 81
	const u_char *optr = ptr;

82
	while (count-- > 0) {
83
		int rdlength;
84

85
		if (dns_ns_name_skip(&ptr, eom) < 0)
86
			RETERR(EMSGSIZE);
87
		ptr += NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
88 89 90 91
		if (section != ns_s_qd) {
			if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
				RETERR(EMSGSIZE);
			ptr += NS_INT32SZ/*TTL*/;
92
			DNS_NS_GET16(rdlength, ptr);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			ptr += rdlength/*RData*/;
		}
	}
	if (ptr > eom)
		RETERR(EMSGSIZE);
	return (ptr - optr);
}

int
dns_ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
	const u_char *eom = msg + msglen;
	int i;

	memset(handle, 0x5e, sizeof *handle);
	handle->_msg = msg;
	handle->_eom = eom;
	if (msg + NS_INT16SZ > eom)
		RETERR(EMSGSIZE);
111
	DNS_NS_GET16(handle->_id, msg);
112 113
	if (msg + NS_INT16SZ > eom)
		RETERR(EMSGSIZE);
114
	DNS_NS_GET16(handle->_flags, msg);
115 116 117
	for (i = 0; i < ns_s_max; i++) {
		if (msg + NS_INT16SZ > eom)
			RETERR(EMSGSIZE);
118
		DNS_NS_GET16(handle->_counts[i], msg);
119 120 121 122 123
	}
	for (i = 0; i < ns_s_max; i++)
		if (handle->_counts[i] == 0)
			handle->_sections[i] = NULL;
		else {
124
			int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
					  handle->_counts[i]);

			if (b < 0)
				return (-1);
			handle->_sections[i] = msg;
			msg += b;
		}
	if (msg != eom)
		RETERR(EMSGSIZE);
	setsection(handle, ns_s_max);
	return (0);
}

int
dns_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
	int b;

	/* Make section right. */
143
	if (section >= ns_s_max)
144 145 146 147 148 149 150 151 152 153 154 155
		RETERR(ENODEV);
	if (section != handle->_sect)
		setsection(handle, section);

	/* Make rrnum right. */
	if (rrnum == -1)
		rrnum = handle->_rrnum;
	if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
		RETERR(ENODEV);
	if (rrnum < handle->_rrnum)
		setsection(handle, section);
	if (rrnum > handle->_rrnum) {
156
		b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
157 158 159 160
			      rrnum - handle->_rrnum);

		if (b < 0)
			return (-1);
161
		NS_PTR(handle) += b;
162 163 164 165 166
		handle->_rrnum = rrnum;
	}

	/* Do the parse. */
	b = dn_expand(handle->_msg, handle->_eom,
167
		      NS_PTR(handle), rr->name, NS_MAXDNAME);
168 169
	if (b < 0)
		return (-1);
170 171
	NS_PTR(handle) += b;
	if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
172
		RETERR(EMSGSIZE);
173 174
	DNS_NS_GET16(rr->type, NS_PTR(handle));
	DNS_NS_GET16(rr->rr_class, NS_PTR(handle));
175 176 177 178 179
	if (section == ns_s_qd) {
		rr->ttl = 0;
		rr->rdlength = 0;
		rr->rdata = NULL;
	} else {
180
                if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
181
			RETERR(EMSGSIZE);
182 183
		DNS_NS_GET32(rr->ttl, NS_PTR(handle));
		DNS_NS_GET16(rr->rdlength, NS_PTR(handle));
184
		if (NS_PTR(handle) + rr->rdlength > handle->_eom)
185
			RETERR(EMSGSIZE);
186 187
		rr->rdata = NS_PTR(handle);
		NS_PTR(handle) += rr->rdlength;
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	}
	if (++handle->_rrnum > handle->_counts[(int)section])
		setsection(handle, (ns_sect)((int)section + 1));

	/* All done. */
	return (0);
}

/* Private. */

static void
setsection(ns_msg *msg, ns_sect sect) {
	msg->_sect = sect;
	if (sect == ns_s_max) {
		msg->_rrnum = -1;
203
		NS_PTR(msg) = NULL;
204 205
	} else {
		msg->_rrnum = 0;
206
		NS_PTR(msg) = msg->_sections[(int)sect];
207 208
	}
}
209 210

#endif