1/* $NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $ */ 2 3/* 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (c) 1996,1999 by Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20#include <sys/cdefs.h> 21#ifndef lint 22#ifdef notdef 23static const char rcsid[] = "Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp"; 24#else 25__RCSID("$NetBSD: ns_parse.c,v 1.9 2012/03/13 21:13:39 christos Exp $"); 26#endif 27#endif 28 29/* Import. */ 30 31#include <sys/types.h> 32 33#include <netinet/in.h> 34#include <arpa/nameser.h> 35 36#include <assert.h> 37#include <errno.h> 38#ifdef ANDROID_CHANGES 39#include "resolv_private.h" 40#else 41#include <resolv.h> 42#endif 43#include <string.h> 44 45/* Forward. */ 46 47static void setsection(ns_msg *msg, ns_sect sect); 48 49/* Macros. */ 50 51#define RETERR(err) do { errno = (err); return (-1); } while (/*NOTREACHED*//*CONSTCOND*/0) 52 53/* Public. */ 54 55struct _ns_flagdata { int mask, shift; }; 56 57/* These need to be in the same order as the nres.h:ns_flag enum. */ 58const struct _ns_flagdata _ns_flagdata[16] = { 59 { 0x8000, 15 }, /* qr. */ 60 { 0x7800, 11 }, /* opcode. */ 61 { 0x0400, 10 }, /* aa. */ 62 { 0x0200, 9 }, /* tc. */ 63 { 0x0100, 8 }, /* rd. */ 64 { 0x0080, 7 }, /* ra. */ 65 { 0x0040, 6 }, /* z. */ 66 { 0x0020, 5 }, /* ad. */ 67 { 0x0010, 4 }, /* cd. */ 68 { 0x000f, 0 }, /* rcode. */ 69 { 0x0000, 0 }, /* expansion (1/6). */ 70 { 0x0000, 0 }, /* expansion (2/6). */ 71 { 0x0000, 0 }, /* expansion (3/6). */ 72 { 0x0000, 0 }, /* expansion (4/6). */ 73 { 0x0000, 0 }, /* expansion (5/6). */ 74 { 0x0000, 0 }, /* expansion (6/6). */ 75}; 76 77int ns_msg_getflag(ns_msg handle, int flag) { 78 return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); 79} 80 81int 82ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { 83 const u_char *optr = ptr; 84 85 for (; count > 0; count--) { 86 int b, rdlength; 87 88 b = dn_skipname(ptr, eom); 89 if (b < 0) 90 RETERR(EMSGSIZE); 91 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; 92 if (section != ns_s_qd) { 93 if (ptr + NS_INT32SZ + NS_INT16SZ > eom) 94 RETERR(EMSGSIZE); 95 ptr += NS_INT32SZ/*TTL*/; 96 NS_GET16(rdlength, ptr); 97 ptr += rdlength/*RData*/; 98 } 99 } 100 if (ptr > eom) 101 RETERR(EMSGSIZE); 102 _DIAGASSERT(__type_fit(int, ptr - optr)); 103 return (int)(ptr - optr); 104} 105 106int 107ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { 108 const u_char *eom = msg + msglen; 109 int i; 110 111 handle->_msg = msg; 112 handle->_eom = eom; 113 if (msg + NS_INT16SZ > eom) 114 RETERR(EMSGSIZE); 115 NS_GET16(handle->_id, msg); 116 if (msg + NS_INT16SZ > eom) 117 RETERR(EMSGSIZE); 118 NS_GET16(handle->_flags, msg); 119 for (i = 0; i < ns_s_max; i++) { 120 if (msg + NS_INT16SZ > eom) 121 RETERR(EMSGSIZE); 122 NS_GET16(handle->_counts[i], msg); 123 } 124 for (i = 0; i < ns_s_max; i++) 125 if (handle->_counts[i] == 0) 126 handle->_sections[i] = NULL; 127 else { 128 int b = ns_skiprr(msg, eom, (ns_sect)i, 129 handle->_counts[i]); 130 131 if (b < 0) 132 return (-1); 133 handle->_sections[i] = msg; 134 msg += b; 135 } 136 if (msg != eom) 137 RETERR(EMSGSIZE); 138 setsection(handle, ns_s_max); 139 return (0); 140} 141 142int 143ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { 144 int b; 145 int tmp; 146 147 /* Make section right. */ 148 tmp = section; 149 if (tmp < 0 || section >= ns_s_max) 150 RETERR(ENODEV); 151 if (section != handle->_sect) 152 setsection(handle, section); 153 154 /* Make rrnum right. */ 155 if (rrnum == -1) 156 rrnum = handle->_rrnum; 157 if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 158 RETERR(ENODEV); 159 if (rrnum < handle->_rrnum) 160 setsection(handle, section); 161 if (rrnum > handle->_rrnum) { 162 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 163 rrnum - handle->_rrnum); 164 165 if (b < 0) 166 return (-1); 167 handle->_msg_ptr += b; 168 handle->_rrnum = rrnum; 169 } 170 171 /* Do the parse. */ 172 b = dn_expand(handle->_msg, handle->_eom, 173 handle->_msg_ptr, rr->name, NS_MAXDNAME); 174 if (b < 0) 175 return (-1); 176 handle->_msg_ptr += b; 177 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 178 RETERR(EMSGSIZE); 179 NS_GET16(rr->type, handle->_msg_ptr); 180 NS_GET16(rr->rr_class, handle->_msg_ptr); 181 if (section == ns_s_qd) { 182 rr->ttl = 0; 183 rr->rdlength = 0; 184 rr->rdata = NULL; 185 } else { 186 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 187 RETERR(EMSGSIZE); 188 NS_GET32(rr->ttl, handle->_msg_ptr); 189 NS_GET16(rr->rdlength, handle->_msg_ptr); 190 if (handle->_msg_ptr + rr->rdlength > handle->_eom) 191 RETERR(EMSGSIZE); 192 rr->rdata = handle->_msg_ptr; 193 handle->_msg_ptr += rr->rdlength; 194 } 195 if (++handle->_rrnum > handle->_counts[(int)section]) 196 setsection(handle, (ns_sect)((int)section + 1)); 197 198 /* All done. */ 199 return (0); 200} 201 202/* 203 * This is identical to the above but uses network-format (uncompressed) names. 204 */ 205int 206ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) { 207 int b; 208 int tmp; 209 210 /* Make section right. */ 211 tmp = section; 212 if (tmp < 0 || section >= ns_s_max) 213 RETERR(ENODEV); 214 if (section != handle->_sect) 215 setsection(handle, section); 216 217 /* Make rrnum right. */ 218 if (rrnum == -1) 219 rrnum = handle->_rrnum; 220 if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 221 RETERR(ENODEV); 222 if (rrnum < handle->_rrnum) 223 setsection(handle, section); 224 if (rrnum > handle->_rrnum) { 225 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 226 rrnum - handle->_rrnum); 227 228 if (b < 0) 229 return (-1); 230 handle->_msg_ptr += b; 231 handle->_rrnum = rrnum; 232 } 233 234 /* Do the parse. */ 235 b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr, 236 rr->nname, NS_MAXNNAME, &rr->nnamel); 237 if (b < 0) 238 return (-1); 239 handle->_msg_ptr += b; 240 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 241 RETERR(EMSGSIZE); 242 NS_GET16(rr->type, handle->_msg_ptr); 243 NS_GET16(rr->rr_class, handle->_msg_ptr); 244 if (section == ns_s_qd) { 245 rr->ttl = 0; 246 rr->rdlength = 0; 247 rr->rdata = NULL; 248 } else { 249 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 250 RETERR(EMSGSIZE); 251 NS_GET32(rr->ttl, handle->_msg_ptr); 252 NS_GET16(rr->rdlength, handle->_msg_ptr); 253 if (handle->_msg_ptr + rr->rdlength > handle->_eom) 254 RETERR(EMSGSIZE); 255 rr->rdata = handle->_msg_ptr; 256 handle->_msg_ptr += rr->rdlength; 257 } 258 if (++handle->_rrnum > handle->_counts[(int)section]) 259 setsection(handle, (ns_sect)((int)section + 1)); 260 261 /* All done. */ 262 return (0); 263} 264 265/* Private. */ 266 267static void 268setsection(ns_msg *msg, ns_sect sect) { 269 msg->_sect = sect; 270 if (sect == ns_s_max) { 271 msg->_rrnum = -1; 272 msg->_msg_ptr = NULL; 273 } else { 274 msg->_rrnum = 0; 275 msg->_msg_ptr = msg->_sections[(int)sect]; 276 } 277} 278