ns_parse.c revision 174224
1227825Stheraven/* 2227825Stheraven * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3227825Stheraven * Copyright (c) 1996,1999 by Internet Software Consortium. 4227825Stheraven * 5227825Stheraven * Permission to use, copy, modify, and distribute this software for any 6227825Stheraven * purpose with or without fee is hereby granted, provided that the above 7227825Stheraven * copyright notice and this permission notice appear in all copies. 8227825Stheraven * 9227825Stheraven * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10227825Stheraven * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11227825Stheraven * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12227825Stheraven * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13227825Stheraven * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14227825Stheraven * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15227825Stheraven * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16227825Stheraven */ 17227825Stheraven 18227825Stheraven#ifndef lint 19227825Stheravenstatic const char rcsid[] = "$Id: ns_parse.c,v 1.5.18.4 2007/08/27 03:34:24 marka Exp $"; 20227825Stheraven#endif 21227825Stheraven 22227825Stheraven/* Import. */ 23227825Stheraven 24227825Stheraven#include "port_before.h" 25227825Stheraven 26227825Stheraven#include <sys/types.h> 27227825Stheraven 28227825Stheraven#include <netinet/in.h> 29227825Stheraven#include <arpa/nameser.h> 30227825Stheraven 31227825Stheraven#include <errno.h> 32227825Stheraven#include <resolv.h> 33227825Stheraven#include <string.h> 34227825Stheraven 35227825Stheraven#include "port_after.h" 36227825Stheraven 37227825Stheraven/* Forward. */ 38227825Stheraven 39227825Stheravenstatic void setsection(ns_msg *msg, ns_sect sect); 40227825Stheraven 41227825Stheraven/* Macros. */ 42227825Stheraven 43227825Stheraven#if !defined(SOLARIS2) || defined(__COVERITY__) 44227825Stheraven#define RETERR(err) do { errno = (err); return (-1); } while (0) 45227825Stheraven#else 46227825Stheraven#define RETERR(err) \ 47227825Stheraven do { errno = (err); if (errno == errno) return (-1); } while (0) 48227825Stheraven#endif 49227825Stheraven 50227825Stheraven/* Public. */ 51227825Stheraven 52227825Stheraven/* These need to be in the same order as the nres.h:ns_flag enum. */ 53227825Stheravenstruct _ns_flagdata _ns_flagdata[16] = { 54227825Stheraven { 0x8000, 15 }, /*%< qr. */ 55227825Stheraven { 0x7800, 11 }, /*%< opcode. */ 56227825Stheraven { 0x0400, 10 }, /*%< aa. */ 57227825Stheraven { 0x0200, 9 }, /*%< tc. */ 58227825Stheraven { 0x0100, 8 }, /*%< rd. */ 59227825Stheraven { 0x0080, 7 }, /*%< ra. */ 60227825Stheraven { 0x0040, 6 }, /*%< z. */ 61227825Stheraven { 0x0020, 5 }, /*%< ad. */ 62227825Stheraven { 0x0010, 4 }, /*%< cd. */ 63227825Stheraven { 0x000f, 0 }, /*%< rcode. */ 64227825Stheraven { 0x0000, 0 }, /*%< expansion (1/6). */ 65227825Stheraven { 0x0000, 0 }, /*%< expansion (2/6). */ 66227825Stheraven { 0x0000, 0 }, /*%< expansion (3/6). */ 67227825Stheraven { 0x0000, 0 }, /*%< expansion (4/6). */ 68227825Stheraven { 0x0000, 0 }, /*%< expansion (5/6). */ 69227825Stheraven { 0x0000, 0 }, /*%< expansion (6/6). */ 70227825Stheraven}; 71227825Stheraven 72227825Stheravenint ns_msg_getflag(ns_msg handle, int flag) { 73227825Stheraven return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); 74227825Stheraven} 75227825Stheraven 76227825Stheravenint 77227825Stheravenns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { 78227825Stheraven const u_char *optr = ptr; 79227825Stheraven 80227825Stheraven for ((void)NULL; count > 0; count--) { 81227825Stheraven int b, rdlength; 82227825Stheraven 83227825Stheraven b = dn_skipname(ptr, eom); 84227825Stheraven if (b < 0) 85227825Stheraven RETERR(EMSGSIZE); 86227825Stheraven ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; 87227825Stheraven if (section != ns_s_qd) { 88227825Stheraven if (ptr + NS_INT32SZ + NS_INT16SZ > eom) 89227825Stheraven RETERR(EMSGSIZE); 90227825Stheraven ptr += NS_INT32SZ/*TTL*/; 91227825Stheraven NS_GET16(rdlength, ptr); 92227825Stheraven ptr += rdlength/*RData*/; 93227825Stheraven } 94227825Stheraven } 95227825Stheraven if (ptr > eom) 96227825Stheraven RETERR(EMSGSIZE); 97227825Stheraven return (ptr - optr); 98227825Stheraven} 99227825Stheraven 100227825Stheravenint 101227825Stheravenns_initparse(const u_char *msg, int msglen, ns_msg *handle) { 102227825Stheraven const u_char *eom = msg + msglen; 103227825Stheraven int i; 104227825Stheraven 105227825Stheraven memset(handle, 0x5e, sizeof *handle); 106227825Stheraven handle->_msg = msg; 107227825Stheraven handle->_eom = eom; 108227825Stheraven if (msg + NS_INT16SZ > eom) 109227825Stheraven RETERR(EMSGSIZE); 110227825Stheraven NS_GET16(handle->_id, msg); 111227825Stheraven if (msg + NS_INT16SZ > eom) 112227825Stheraven RETERR(EMSGSIZE); 113227825Stheraven NS_GET16(handle->_flags, msg); 114227825Stheraven for (i = 0; i < ns_s_max; i++) { 115227825Stheraven if (msg + NS_INT16SZ > eom) 116227825Stheraven RETERR(EMSGSIZE); 117227825Stheraven NS_GET16(handle->_counts[i], msg); 118227825Stheraven } 119227825Stheraven for (i = 0; i < ns_s_max; i++) 120227825Stheraven if (handle->_counts[i] == 0) 121227825Stheraven handle->_sections[i] = NULL; 122227825Stheraven else { 123227825Stheraven int b = ns_skiprr(msg, eom, (ns_sect)i, 124227825Stheraven handle->_counts[i]); 125227825Stheraven 126227825Stheraven if (b < 0) 127227825Stheraven return (-1); 128227825Stheraven handle->_sections[i] = msg; 129227825Stheraven msg += b; 130227825Stheraven } 131227825Stheraven if (msg != eom) 132227825Stheraven RETERR(EMSGSIZE); 133227825Stheraven setsection(handle, ns_s_max); 134227825Stheraven return (0); 135227825Stheraven} 136227825Stheraven 137227825Stheravenint 138227825Stheravenns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { 139227825Stheraven int b; 140227825Stheraven int tmp; 141227825Stheraven 142227825Stheraven /* Make section right. */ 143227825Stheraven tmp = section; 144227825Stheraven if (tmp < 0 || section >= ns_s_max) 145227825Stheraven RETERR(ENODEV); 146227825Stheraven if (section != handle->_sect) 147227825Stheraven setsection(handle, section); 148227825Stheraven 149227825Stheraven /* Make rrnum right. */ 150227825Stheraven if (rrnum == -1) 151227825Stheraven rrnum = handle->_rrnum; 152227825Stheraven if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 153227825Stheraven RETERR(ENODEV); 154227825Stheraven if (rrnum < handle->_rrnum) 155227825Stheraven setsection(handle, section); 156227825Stheraven if (rrnum > handle->_rrnum) { 157227825Stheraven b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 158227825Stheraven rrnum - handle->_rrnum); 159227825Stheraven 160227825Stheraven if (b < 0) 161227825Stheraven return (-1); 162227825Stheraven handle->_msg_ptr += b; 163227825Stheraven handle->_rrnum = rrnum; 164227825Stheraven } 165227825Stheraven 166227825Stheraven /* Do the parse. */ 167227825Stheraven b = dn_expand(handle->_msg, handle->_eom, 168227825Stheraven handle->_msg_ptr, rr->name, NS_MAXDNAME); 169227825Stheraven if (b < 0) 170227825Stheraven return (-1); 171227825Stheraven handle->_msg_ptr += b; 172227825Stheraven if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 173227825Stheraven RETERR(EMSGSIZE); 174227825Stheraven NS_GET16(rr->type, handle->_msg_ptr); 175227825Stheraven NS_GET16(rr->rr_class, handle->_msg_ptr); 176227825Stheraven if (section == ns_s_qd) { 177227825Stheraven rr->ttl = 0; 178227825Stheraven rr->rdlength = 0; 179227825Stheraven rr->rdata = NULL; 180227825Stheraven } else { 181227825Stheraven if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 182227825Stheraven RETERR(EMSGSIZE); 183227825Stheraven NS_GET32(rr->ttl, handle->_msg_ptr); 184227825Stheraven NS_GET16(rr->rdlength, handle->_msg_ptr); 185227825Stheraven if (handle->_msg_ptr + rr->rdlength > handle->_eom) 186227825Stheraven RETERR(EMSGSIZE); 187227825Stheraven rr->rdata = handle->_msg_ptr; 188227825Stheraven handle->_msg_ptr += rr->rdlength; 189227825Stheraven } 190227825Stheraven if (++handle->_rrnum > handle->_counts[(int)section]) 191227825Stheraven setsection(handle, (ns_sect)((int)section + 1)); 192227825Stheraven 193227825Stheraven /* All done. */ 194227825Stheraven return (0); 195227825Stheraven} 196227825Stheraven 197227825Stheraven/* Private. */ 198227825Stheraven 199227825Stheravenstatic void 200227825Stheravensetsection(ns_msg *msg, ns_sect sect) { 201227825Stheraven msg->_sect = sect; 202227825Stheraven if (sect == ns_s_max) { 203227825Stheraven msg->_rrnum = -1; 204227825Stheraven msg->_msg_ptr = NULL; 205227825Stheraven } else { 206227825Stheraven msg->_rrnum = 0; 207227825Stheraven msg->_msg_ptr = msg->_sections[(int)sect]; 208227825Stheraven } 209227825Stheraven} 210227825Stheraven 211227825Stheraven/*! \file */ 212227825Stheraven