2006-05-29 10:47:19 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
|
|
|
|
2006-06-05 12:50:00 +00:00
|
|
|
#ifdef HAVE_RESOLV
|
|
|
|
|
2006-05-29 10:47:19 +00:00
|
|
|
#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
|
2011-09-06 23:41:39 +00:00
|
|
|
#include <stdarg.h>
|
2006-05-29 10:47:19 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2006-10-20 12:51:44 +00:00
|
|
|
#include "windef.h"
|
2011-09-06 23:41:39 +00:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnls.h"
|
|
|
|
|
|
|
|
#include "dnsapi.h"
|
2006-10-20 12:51:44 +00:00
|
|
|
|
2006-05-29 10:47:19 +00:00
|
|
|
/* Forward. */
|
|
|
|
|
|
|
|
static void setsection(ns_msg *msg, ns_sect sect);
|
|
|
|
|
|
|
|
/* Macros. */
|
|
|
|
|
|
|
|
#define RETERR(err) do { return (-1); } while (0)
|
|
|
|
|
2006-05-31 12:43:21 +00:00
|
|
|
#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
|
|
|
|
|
2006-10-05 13:36:39 +00:00
|
|
|
#define DNS_NS_GET16(s, cp) do { \
|
2007-12-10 22:03:24 +00:00
|
|
|
register const u_char *t_cp = (cp); \
|
2006-10-20 12:51:44 +00:00
|
|
|
(s) = ((WORD)t_cp[0] << 8) \
|
|
|
|
| ((WORD)t_cp[1]) \
|
2006-10-05 13:36:39 +00:00
|
|
|
; \
|
|
|
|
(cp) += NS_INT16SZ; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DNS_NS_GET32(l, cp) do { \
|
2007-12-10 22:03:24 +00:00
|
|
|
register const u_char *t_cp = (cp); \
|
2006-10-20 12:51:44 +00:00
|
|
|
(l) = ((DWORD)t_cp[0] << 24) \
|
|
|
|
| ((DWORD)t_cp[1] << 16) \
|
|
|
|
| ((DWORD)t_cp[2] << 8) \
|
|
|
|
| ((DWORD)t_cp[3]) \
|
2006-10-05 13:36:39 +00:00
|
|
|
; \
|
|
|
|
(cp) += NS_INT32SZ; \
|
|
|
|
} while (0)
|
|
|
|
|
2006-05-29 10:47:19 +00:00
|
|
|
/* Public. */
|
|
|
|
|
2006-05-31 12:25:49 +00:00
|
|
|
static int
|
|
|
|
dns_ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
|
2006-05-29 10:47:19 +00:00
|
|
|
const u_char *optr = ptr;
|
|
|
|
|
2008-11-01 23:24:20 +00:00
|
|
|
while (count-- > 0) {
|
2011-09-06 23:41:39 +00:00
|
|
|
int rdlength;
|
2006-05-29 10:47:19 +00:00
|
|
|
|
2011-09-06 23:41:39 +00:00
|
|
|
if (dns_ns_name_skip(&ptr, eom) < 0)
|
2006-05-29 10:47:19 +00:00
|
|
|
RETERR(EMSGSIZE);
|
2011-09-06 23:41:39 +00:00
|
|
|
ptr += NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
|
2006-05-29 10:47:19 +00:00
|
|
|
if (section != ns_s_qd) {
|
|
|
|
if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
|
|
|
|
RETERR(EMSGSIZE);
|
|
|
|
ptr += NS_INT32SZ/*TTL*/;
|
2006-10-05 13:36:39 +00:00
|
|
|
DNS_NS_GET16(rdlength, ptr);
|
2006-05-29 10:47:19 +00:00
|
|
|
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);
|
2006-10-05 13:36:39 +00:00
|
|
|
DNS_NS_GET16(handle->_id, msg);
|
2006-05-29 10:47:19 +00:00
|
|
|
if (msg + NS_INT16SZ > eom)
|
|
|
|
RETERR(EMSGSIZE);
|
2006-10-05 13:36:39 +00:00
|
|
|
DNS_NS_GET16(handle->_flags, msg);
|
2006-05-29 10:47:19 +00:00
|
|
|
for (i = 0; i < ns_s_max; i++) {
|
|
|
|
if (msg + NS_INT16SZ > eom)
|
|
|
|
RETERR(EMSGSIZE);
|
2006-10-05 13:36:39 +00:00
|
|
|
DNS_NS_GET16(handle->_counts[i], msg);
|
2006-05-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < ns_s_max; i++)
|
|
|
|
if (handle->_counts[i] == 0)
|
|
|
|
handle->_sections[i] = NULL;
|
|
|
|
else {
|
2006-05-31 12:25:49 +00:00
|
|
|
int b = dns_ns_skiprr(msg, eom, (ns_sect)i,
|
2006-05-29 10:47:19 +00:00
|
|
|
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. */
|
2008-12-05 07:12:36 +00:00
|
|
|
if (section >= ns_s_max)
|
2006-05-29 10:47:19 +00:00
|
|
|
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) {
|
2006-05-31 12:43:21 +00:00
|
|
|
b = dns_ns_skiprr(NS_PTR(handle), handle->_eom, section,
|
2006-05-29 10:47:19 +00:00
|
|
|
rrnum - handle->_rrnum);
|
|
|
|
|
|
|
|
if (b < 0)
|
|
|
|
return (-1);
|
2006-05-31 12:43:21 +00:00
|
|
|
NS_PTR(handle) += b;
|
2006-05-29 10:47:19 +00:00
|
|
|
handle->_rrnum = rrnum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the parse. */
|
|
|
|
b = dn_expand(handle->_msg, handle->_eom,
|
2006-05-31 12:43:21 +00:00
|
|
|
NS_PTR(handle), rr->name, NS_MAXDNAME);
|
2006-05-29 10:47:19 +00:00
|
|
|
if (b < 0)
|
|
|
|
return (-1);
|
2006-05-31 12:43:21 +00:00
|
|
|
NS_PTR(handle) += b;
|
|
|
|
if (NS_PTR(handle) + NS_INT16SZ + NS_INT16SZ > handle->_eom)
|
2006-05-29 10:47:19 +00:00
|
|
|
RETERR(EMSGSIZE);
|
2006-10-05 13:36:39 +00:00
|
|
|
DNS_NS_GET16(rr->type, NS_PTR(handle));
|
|
|
|
DNS_NS_GET16(rr->rr_class, NS_PTR(handle));
|
2006-05-29 10:47:19 +00:00
|
|
|
if (section == ns_s_qd) {
|
|
|
|
rr->ttl = 0;
|
|
|
|
rr->rdlength = 0;
|
|
|
|
rr->rdata = NULL;
|
|
|
|
} else {
|
2006-05-31 12:43:21 +00:00
|
|
|
if (NS_PTR(handle) + NS_INT32SZ + NS_INT16SZ > handle->_eom)
|
2006-05-29 10:47:19 +00:00
|
|
|
RETERR(EMSGSIZE);
|
2006-10-05 13:36:39 +00:00
|
|
|
DNS_NS_GET32(rr->ttl, NS_PTR(handle));
|
|
|
|
DNS_NS_GET16(rr->rdlength, NS_PTR(handle));
|
2006-05-31 12:43:21 +00:00
|
|
|
if (NS_PTR(handle) + rr->rdlength > handle->_eom)
|
2006-05-29 10:47:19 +00:00
|
|
|
RETERR(EMSGSIZE);
|
2006-05-31 12:43:21 +00:00
|
|
|
rr->rdata = NS_PTR(handle);
|
|
|
|
NS_PTR(handle) += rr->rdlength;
|
2006-05-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
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;
|
2006-05-31 12:43:21 +00:00
|
|
|
NS_PTR(msg) = NULL;
|
2006-05-29 10:47:19 +00:00
|
|
|
} else {
|
|
|
|
msg->_rrnum = 0;
|
2006-05-31 12:43:21 +00:00
|
|
|
NS_PTR(msg) = msg->_sections[(int)sect];
|
2006-05-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-05 12:50:00 +00:00
|
|
|
|
|
|
|
#endif
|