1 /*- 2 * SPDX-License-Identifier: ISC 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 /* Import. */ 21 22 #include "port_before.h" 23 24 #include <sys/types.h> 25 26 #include <netinet/in.h> 27 #include <arpa/nameser.h> 28 29 #include <errno.h> 30 #include <resolv.h> 31 #include <string.h> 32 33 #include "port_after.h" 34 35 /* Forward. */ 36 37 static void setsection(ns_msg *msg, ns_sect sect); 38 39 /* Macros. */ 40 41 #define RETERR(err) do { errno = (err); return (-1); } while (0) 42 43 #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */ 44 #define PARSE_FMT_WIRE 1 /* Parse using network-format names */ 45 46 /* Public. */ 47 48 /* These need to be in the same order as the nres.h:ns_flag enum. */ 49 struct _ns_flagdata _ns_flagdata[16] = { 50 { 0x8000, 15 }, /*%< qr. */ 51 { 0x7800, 11 }, /*%< opcode. */ 52 { 0x0400, 10 }, /*%< aa. */ 53 { 0x0200, 9 }, /*%< tc. */ 54 { 0x0100, 8 }, /*%< rd. */ 55 { 0x0080, 7 }, /*%< ra. */ 56 { 0x0040, 6 }, /*%< z. */ 57 { 0x0020, 5 }, /*%< ad. */ 58 { 0x0010, 4 }, /*%< cd. */ 59 { 0x000f, 0 }, /*%< rcode. */ 60 { 0x0000, 0 }, /*%< expansion (1/6). */ 61 { 0x0000, 0 }, /*%< expansion (2/6). */ 62 { 0x0000, 0 }, /*%< expansion (3/6). */ 63 { 0x0000, 0 }, /*%< expansion (4/6). */ 64 { 0x0000, 0 }, /*%< expansion (5/6). */ 65 { 0x0000, 0 }, /*%< expansion (6/6). */ 66 }; 67 68 int ns_msg_getflag(ns_msg handle, int flag) { 69 return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift); 70 } 71 72 int 73 ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) { 74 const u_char *optr = ptr; 75 76 for ((void)NULL; count > 0; count--) { 77 int b, rdlength; 78 79 b = dn_skipname(ptr, eom); 80 if (b < 0) 81 RETERR(EMSGSIZE); 82 ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; 83 if (section != ns_s_qd) { 84 if (ptr + NS_INT32SZ + NS_INT16SZ > eom) 85 RETERR(EMSGSIZE); 86 ptr += NS_INT32SZ/*TTL*/; 87 NS_GET16(rdlength, ptr); 88 ptr += rdlength/*RData*/; 89 } 90 } 91 if (ptr > eom) 92 RETERR(EMSGSIZE); 93 return (ptr - optr); 94 } 95 96 int 97 ns_initparse(const u_char *msg, int msglen, ns_msg *handle) { 98 const u_char *eom = msg + msglen; 99 int i; 100 101 handle->_msg = msg; 102 handle->_eom = eom; 103 if (msg + NS_INT16SZ > eom) 104 RETERR(EMSGSIZE); 105 NS_GET16(handle->_id, msg); 106 if (msg + NS_INT16SZ > eom) 107 RETERR(EMSGSIZE); 108 NS_GET16(handle->_flags, msg); 109 for (i = 0; i < ns_s_max; i++) { 110 if (msg + NS_INT16SZ > eom) 111 RETERR(EMSGSIZE); 112 NS_GET16(handle->_counts[i], msg); 113 } 114 for (i = 0; i < ns_s_max; i++) 115 if (handle->_counts[i] == 0) 116 handle->_sections[i] = NULL; 117 else { 118 int b = ns_skiprr(msg, eom, (ns_sect)i, 119 handle->_counts[i]); 120 121 if (b < 0) 122 return (-1); 123 handle->_sections[i] = msg; 124 msg += b; 125 } 126 if (msg != eom) 127 RETERR(EMSGSIZE); 128 setsection(handle, ns_s_max); 129 return (0); 130 } 131 132 int 133 ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { 134 int b; 135 int tmp; 136 137 /* Make section right. */ 138 tmp = section; 139 if (tmp < 0 || section >= ns_s_max) 140 RETERR(ENODEV); 141 if (section != handle->_sect) 142 setsection(handle, section); 143 144 /* Make rrnum right. */ 145 if (rrnum == -1) 146 rrnum = handle->_rrnum; 147 if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 148 RETERR(ENODEV); 149 if (rrnum < handle->_rrnum) 150 setsection(handle, section); 151 if (rrnum > handle->_rrnum) { 152 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 153 rrnum - handle->_rrnum); 154 155 if (b < 0) 156 return (-1); 157 handle->_msg_ptr += b; 158 handle->_rrnum = rrnum; 159 } 160 161 /* Do the parse. */ 162 b = dn_expand(handle->_msg, handle->_eom, 163 handle->_msg_ptr, rr->name, NS_MAXDNAME); 164 if (b < 0) 165 return (-1); 166 handle->_msg_ptr += b; 167 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 168 RETERR(EMSGSIZE); 169 NS_GET16(rr->type, handle->_msg_ptr); 170 NS_GET16(rr->rr_class, handle->_msg_ptr); 171 if (section == ns_s_qd) { 172 rr->ttl = 0; 173 rr->rdlength = 0; 174 rr->rdata = NULL; 175 } else { 176 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 177 RETERR(EMSGSIZE); 178 NS_GET32(rr->ttl, handle->_msg_ptr); 179 NS_GET16(rr->rdlength, handle->_msg_ptr); 180 if (handle->_msg_ptr + rr->rdlength > handle->_eom) 181 RETERR(EMSGSIZE); 182 rr->rdata = handle->_msg_ptr; 183 handle->_msg_ptr += rr->rdlength; 184 } 185 if (++handle->_rrnum > handle->_counts[(int)section]) 186 setsection(handle, (ns_sect)((int)section + 1)); 187 188 /* All done. */ 189 return (0); 190 } 191 192 /* 193 * This is identical to the above but uses network-format (uncompressed) names. 194 */ 195 int 196 ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) { 197 int b; 198 int tmp; 199 200 /* Make section right. */ 201 if ((tmp = section) < 0 || section >= ns_s_max) 202 RETERR(ENODEV); 203 if (section != handle->_sect) 204 setsection(handle, section); 205 206 /* Make rrnum right. */ 207 if (rrnum == -1) 208 rrnum = handle->_rrnum; 209 if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) 210 RETERR(ENODEV); 211 if (rrnum < handle->_rrnum) 212 setsection(handle, section); 213 if (rrnum > handle->_rrnum) { 214 b = ns_skiprr(handle->_msg_ptr, handle->_eom, section, 215 rrnum - handle->_rrnum); 216 217 if (b < 0) 218 return (-1); 219 handle->_msg_ptr += b; 220 handle->_rrnum = rrnum; 221 } 222 223 /* Do the parse. */ 224 b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr, 225 rr->nname, NS_MAXNNAME, &rr->nnamel); 226 if (b < 0) 227 return (-1); 228 handle->_msg_ptr += b; 229 if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom) 230 RETERR(EMSGSIZE); 231 NS_GET16(rr->type, handle->_msg_ptr); 232 NS_GET16(rr->rr_class, handle->_msg_ptr); 233 if (section == ns_s_qd) { 234 rr->ttl = 0; 235 rr->rdlength = 0; 236 rr->rdata = NULL; 237 } else { 238 if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom) 239 RETERR(EMSGSIZE); 240 NS_GET32(rr->ttl, handle->_msg_ptr); 241 NS_GET16(rr->rdlength, handle->_msg_ptr); 242 if (handle->_msg_ptr + rr->rdlength > handle->_eom) 243 RETERR(EMSGSIZE); 244 rr->rdata = handle->_msg_ptr; 245 handle->_msg_ptr += rr->rdlength; 246 } 247 if (++handle->_rrnum > handle->_counts[(int)section]) 248 setsection(handle, (ns_sect)((int)section + 1)); 249 250 /* All done. */ 251 return (0); 252 } 253 254 /* Private. */ 255 256 static void 257 setsection(ns_msg *msg, ns_sect sect) { 258 msg->_sect = sect; 259 if (sect == ns_s_max) { 260 msg->_rrnum = -1; 261 msg->_msg_ptr = NULL; 262 } else { 263 msg->_rrnum = 0; 264 msg->_msg_ptr = msg->_sections[(int)sect]; 265 } 266 } 267 268 /*! \file */ 269