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 55/* These need to be in the same order as the nres.h:ns_flag enum. */ 56const 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 75int ns_msg_getflag(ns_msg handle, int flag) { 76 return((u_int32_t)((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); 77} 78 79int 80ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { 81 const u_char *optr = ptr; 82 83 for (; count > 0; count--) { 84 int b, rdlength; 85 86 b = dn_skipname(ptr, eom); 87 if (b < 0) 88 RETERR(EMSGSIZE); 89 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; 90 if (section != ns_s_qd) { 91 if (ptr + NS_INT32SZ + NS_INT16SZ > eom) 92 RETERR(EMSGSIZE); 93 ptr += NS_INT32SZ/*TTL*/; 94 NS_GET16(rdlength, ptr); 95 ptr += rdlength/*RData*/; 96 } 97 } 98 if (ptr > eom) 99 RETERR(EMSGSIZE); 100 _DIAGASSERT(__type_fit(int, ptr - optr)); 101 return (int)(ptr - optr); 102} 103 104int 105ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { 106 const u_char *eom = msg + msglen; 107 int i; 108 109 handle->_msg = msg; 110 handle->_eom = eom; 111 if (msg + NS_INT16SZ > eom) 112 RETERR(EMSGSIZE); 113 NS_GET16(handle->_id, msg); 114 if (msg + NS_INT16SZ > eom) 115 RETERR(EMSGSIZE); 116 NS_GET16(handle->_flags, msg); 117 for (i = 0; i < ns_s_max; i++) { 118 if (msg + NS_INT16SZ > eom) 119 RETERR(EMSGSIZE); 120 NS_GET16(handle->_counts[i], msg); 121 } 122 for (i = 0; i < ns_s_max; i++) 123 if (handle->_counts[i] == 0) 124 handle->_sections[i] = NULL; 125 else { 126 int b = ns_skiprr(msg, eom, (ns_sect)i, 127 handle->_counts[i]); 128 129 if (b < 0) 130 return (-1); 131 handle->_sections[i] = msg; 132 msg += b; 133 } 134 if (msg != eom) 135 RETERR(EMSGSIZE); 136 setsection(handle, ns_s_max); 137 return (0); 138} 139 140int 141ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { 142 int b; 143 int tmp; 144 145 /* Make section right. */ 146 tmp = section; 147 if (tmp < 0 || section >= ns_s_max) 148 RETERR(ENODEV); 149 if (section != handle->_sect) 150 setsection(handle, section); 151 152 /* Make rrnum right. */ 153 if (rrnum == -1) 154 rrnum = handle->_rrnum; 155 if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 156 RETERR(ENODEV); 157 if (rrnum < handle->_rrnum) 158 setsection(handle, section); 159 if (rrnum > handle->_rrnum) { 160 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 161 rrnum - handle->_rrnum); 162 163 if (b < 0) 164 return (-1); 165 handle->_msg_ptr += b; 166 handle->_rrnum = rrnum; 167 } 168 169 /* Do the parse. */ 170 b = dn_expand(handle->_msg, handle->_eom, 171 handle->_msg_ptr, rr->name, NS_MAXDNAME); 172 if (b < 0) 173 return (-1); 174 handle->_msg_ptr += b; 175 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 176 RETERR(EMSGSIZE); 177 NS_GET16(rr->type, handle->_msg_ptr); 178 NS_GET16(rr->rr_class, handle->_msg_ptr); 179 if (section == ns_s_qd) { 180 rr->ttl = 0; 181 rr->rdlength = 0; 182 rr->rdata = NULL; 183 } else { 184 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 185 RETERR(EMSGSIZE); 186 NS_GET32(rr->ttl, handle->_msg_ptr); 187 NS_GET16(rr->rdlength, handle->_msg_ptr); 188 if (handle->_msg_ptr + rr->rdlength > handle->_eom) 189 RETERR(EMSGSIZE); 190 rr->rdata = handle->_msg_ptr; 191 handle->_msg_ptr += rr->rdlength; 192 } 193 if (++handle->_rrnum > handle->_counts[(int)section]) 194 setsection(handle, (ns_sect)((int)section + 1)); 195 196 /* All done. */ 197 return (0); 198} 199 200/* 201 * This is identical to the above but uses network-format (uncompressed) names. 202 */ 203int 204ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) { 205 int b; 206 int tmp; 207 208 /* Make section right. */ 209 tmp = section; 210 if (tmp < 0 || section >= ns_s_max) 211 RETERR(ENODEV); 212 if (section != handle->_sect) 213 setsection(handle, section); 214 215 /* Make rrnum right. */ 216 if (rrnum == -1) 217 rrnum = handle->_rrnum; 218 if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 219 RETERR(ENODEV); 220 if (rrnum < handle->_rrnum) 221 setsection(handle, section); 222 if (rrnum > handle->_rrnum) { 223 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 224 rrnum - handle->_rrnum); 225 226 if (b < 0) 227 return (-1); 228 handle->_msg_ptr += b; 229 handle->_rrnum = rrnum; 230 } 231 232 /* Do the parse. */ 233 b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr, 234 rr->nname, NS_MAXNNAME, &rr->nnamel); 235 if (b < 0) 236 return (-1); 237 handle->_msg_ptr += b; 238 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 239 RETERR(EMSGSIZE); 240 NS_GET16(rr->type, handle->_msg_ptr); 241 NS_GET16(rr->rr_class, handle->_msg_ptr); 242 if (section == ns_s_qd) { 243 rr->ttl = 0; 244 rr->rdlength = 0; 245 rr->rdata = NULL; 246 } else { 247 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 248 RETERR(EMSGSIZE); 249 NS_GET32(rr->ttl, handle->_msg_ptr); 250 NS_GET16(rr->rdlength, handle->_msg_ptr); 251 if (handle->_msg_ptr + rr->rdlength > handle->_eom) 252 RETERR(EMSGSIZE); 253 rr->rdata = handle->_msg_ptr; 254 handle->_msg_ptr += rr->rdlength; 255 } 256 if (++handle->_rrnum > handle->_counts[(int)section]) 257 setsection(handle, (ns_sect)((int)section + 1)); 258 259 /* All done. */ 260 return (0); 261} 262 263/* Private. */ 264 265static void 266setsection(ns_msg *msg, ns_sect sect) { 267 msg->_sect = sect; 268 if (sect == ns_s_max) { 269 msg->_rrnum = -1; 270 msg->_msg_ptr = NULL; 271 } else { 272 msg->_rrnum = 0; 273 msg->_msg_ptr = msg->_sections[(int)sect]; 274 } 275} 276