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