17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate *
9*9525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate #include <netinet/in.h>
237c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate #include <errno.h>
267c478bd9Sstevel@tonic-gate #include <resolv.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include "port_after.h"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate /* Forward. */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate static void setsection(ns_msg *msg, ns_sect sect);
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /* Macros. */
367c478bd9Sstevel@tonic-gate
37*9525b14bSRao Shoaib #if !defined(SOLARIS2) || defined(__COVERITY__)
387c478bd9Sstevel@tonic-gate #define RETERR(err) do { errno = (err); return (-1); } while (0)
397c478bd9Sstevel@tonic-gate #else
40*9525b14bSRao Shoaib #define RETERR(err) \
41*9525b14bSRao Shoaib do { errno = (err); if (errno == errno) return (-1); } while (0)
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate
44*9525b14bSRao Shoaib #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
45*9525b14bSRao Shoaib #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
46*9525b14bSRao Shoaib
477c478bd9Sstevel@tonic-gate /* Public. */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /* These need to be in the same order as the nres.h:ns_flag enum. */
507c478bd9Sstevel@tonic-gate struct _ns_flagdata _ns_flagdata[16] = {
51*9525b14bSRao Shoaib { 0x8000, 15 }, /*%< qr. */
52*9525b14bSRao Shoaib { 0x7800, 11 }, /*%< opcode. */
53*9525b14bSRao Shoaib { 0x0400, 10 }, /*%< aa. */
54*9525b14bSRao Shoaib { 0x0200, 9 }, /*%< tc. */
55*9525b14bSRao Shoaib { 0x0100, 8 }, /*%< rd. */
56*9525b14bSRao Shoaib { 0x0080, 7 }, /*%< ra. */
57*9525b14bSRao Shoaib { 0x0040, 6 }, /*%< z. */
58*9525b14bSRao Shoaib { 0x0020, 5 }, /*%< ad. */
59*9525b14bSRao Shoaib { 0x0010, 4 }, /*%< cd. */
60*9525b14bSRao Shoaib { 0x000f, 0 }, /*%< rcode. */
61*9525b14bSRao Shoaib { 0x0000, 0 }, /*%< expansion (1/6). */
62*9525b14bSRao Shoaib { 0x0000, 0 }, /*%< expansion (2/6). */
63*9525b14bSRao Shoaib { 0x0000, 0 }, /*%< expansion (3/6). */
64*9525b14bSRao Shoaib { 0x0000, 0 }, /*%< expansion (4/6). */
65*9525b14bSRao Shoaib { 0x0000, 0 }, /*%< expansion (5/6). */
66*9525b14bSRao Shoaib { 0x0000, 0 }, /*%< expansion (6/6). */
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate
ns_msg_getflag(ns_msg handle,int flag)697c478bd9Sstevel@tonic-gate int ns_msg_getflag(ns_msg handle, int flag) {
707c478bd9Sstevel@tonic-gate return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate int
ns_skiprr(const u_char * ptr,const u_char * eom,ns_sect section,int count)747c478bd9Sstevel@tonic-gate ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
757c478bd9Sstevel@tonic-gate const u_char *optr = ptr;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate for ((void)NULL; count > 0; count--) {
787c478bd9Sstevel@tonic-gate int b, rdlength;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate b = dn_skipname(ptr, eom);
817c478bd9Sstevel@tonic-gate if (b < 0)
827c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
837c478bd9Sstevel@tonic-gate ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
847c478bd9Sstevel@tonic-gate if (section != ns_s_qd) {
857c478bd9Sstevel@tonic-gate if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
867c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
877c478bd9Sstevel@tonic-gate ptr += NS_INT32SZ/*TTL*/;
887c478bd9Sstevel@tonic-gate NS_GET16(rdlength, ptr);
897c478bd9Sstevel@tonic-gate ptr += rdlength/*RData*/;
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate if (ptr > eom)
937c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
947c478bd9Sstevel@tonic-gate return (ptr - optr);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate int
ns_initparse(const u_char * msg,int msglen,ns_msg * handle)987c478bd9Sstevel@tonic-gate ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
997c478bd9Sstevel@tonic-gate const u_char *eom = msg + msglen;
1007c478bd9Sstevel@tonic-gate int i;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate handle->_msg = msg;
1037c478bd9Sstevel@tonic-gate handle->_eom = eom;
1047c478bd9Sstevel@tonic-gate if (msg + NS_INT16SZ > eom)
1057c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1067c478bd9Sstevel@tonic-gate NS_GET16(handle->_id, msg);
1077c478bd9Sstevel@tonic-gate if (msg + NS_INT16SZ > eom)
1087c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1097c478bd9Sstevel@tonic-gate NS_GET16(handle->_flags, msg);
1107c478bd9Sstevel@tonic-gate for (i = 0; i < ns_s_max; i++) {
1117c478bd9Sstevel@tonic-gate if (msg + NS_INT16SZ > eom)
1127c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1137c478bd9Sstevel@tonic-gate NS_GET16(handle->_counts[i], msg);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate for (i = 0; i < ns_s_max; i++)
1167c478bd9Sstevel@tonic-gate if (handle->_counts[i] == 0)
1177c478bd9Sstevel@tonic-gate handle->_sections[i] = NULL;
1187c478bd9Sstevel@tonic-gate else {
1197c478bd9Sstevel@tonic-gate int b = ns_skiprr(msg, eom, (ns_sect)i,
1207c478bd9Sstevel@tonic-gate handle->_counts[i]);
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate if (b < 0)
1237c478bd9Sstevel@tonic-gate return (-1);
1247c478bd9Sstevel@tonic-gate handle->_sections[i] = msg;
1257c478bd9Sstevel@tonic-gate msg += b;
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate if (msg != eom)
1287c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1297c478bd9Sstevel@tonic-gate setsection(handle, ns_s_max);
1307c478bd9Sstevel@tonic-gate return (0);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate int
ns_parserr(ns_msg * handle,ns_sect section,int rrnum,ns_rr * rr)1347c478bd9Sstevel@tonic-gate ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
1357c478bd9Sstevel@tonic-gate int b;
1367c478bd9Sstevel@tonic-gate int tmp;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /* Make section right. */
139*9525b14bSRao Shoaib tmp = section;
140*9525b14bSRao Shoaib if (tmp < 0 || section >= ns_s_max)
1417c478bd9Sstevel@tonic-gate RETERR(ENODEV);
1427c478bd9Sstevel@tonic-gate if (section != handle->_sect)
1437c478bd9Sstevel@tonic-gate setsection(handle, section);
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /* Make rrnum right. */
1467c478bd9Sstevel@tonic-gate if (rrnum == -1)
1477c478bd9Sstevel@tonic-gate rrnum = handle->_rrnum;
1487c478bd9Sstevel@tonic-gate if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
1497c478bd9Sstevel@tonic-gate RETERR(ENODEV);
1507c478bd9Sstevel@tonic-gate if (rrnum < handle->_rrnum)
1517c478bd9Sstevel@tonic-gate setsection(handle, section);
1527c478bd9Sstevel@tonic-gate if (rrnum > handle->_rrnum) {
1537c478bd9Sstevel@tonic-gate b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
1547c478bd9Sstevel@tonic-gate rrnum - handle->_rrnum);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (b < 0)
1577c478bd9Sstevel@tonic-gate return (-1);
1587c478bd9Sstevel@tonic-gate handle->_msg_ptr += b;
1597c478bd9Sstevel@tonic-gate handle->_rrnum = rrnum;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /* Do the parse. */
1637c478bd9Sstevel@tonic-gate b = dn_expand(handle->_msg, handle->_eom,
1647c478bd9Sstevel@tonic-gate handle->_msg_ptr, rr->name, NS_MAXDNAME);
1657c478bd9Sstevel@tonic-gate if (b < 0)
1667c478bd9Sstevel@tonic-gate return (-1);
1677c478bd9Sstevel@tonic-gate handle->_msg_ptr += b;
1687c478bd9Sstevel@tonic-gate if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
1697c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1707c478bd9Sstevel@tonic-gate NS_GET16(rr->type, handle->_msg_ptr);
1717c478bd9Sstevel@tonic-gate NS_GET16(rr->rr_class, handle->_msg_ptr);
1727c478bd9Sstevel@tonic-gate if (section == ns_s_qd) {
1737c478bd9Sstevel@tonic-gate rr->ttl = 0;
1747c478bd9Sstevel@tonic-gate rr->rdlength = 0;
1757c478bd9Sstevel@tonic-gate rr->rdata = NULL;
1767c478bd9Sstevel@tonic-gate } else {
1777c478bd9Sstevel@tonic-gate if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
1787c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1797c478bd9Sstevel@tonic-gate NS_GET32(rr->ttl, handle->_msg_ptr);
1807c478bd9Sstevel@tonic-gate NS_GET16(rr->rdlength, handle->_msg_ptr);
1817c478bd9Sstevel@tonic-gate if (handle->_msg_ptr + rr->rdlength > handle->_eom)
1827c478bd9Sstevel@tonic-gate RETERR(EMSGSIZE);
1837c478bd9Sstevel@tonic-gate rr->rdata = handle->_msg_ptr;
1847c478bd9Sstevel@tonic-gate handle->_msg_ptr += rr->rdlength;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate if (++handle->_rrnum > handle->_counts[(int)section])
1877c478bd9Sstevel@tonic-gate setsection(handle, (ns_sect)((int)section + 1));
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /* All done. */
1907c478bd9Sstevel@tonic-gate return (0);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
193*9525b14bSRao Shoaib /*
194*9525b14bSRao Shoaib * This is identical to the above but uses network-format (uncompressed) names.
195*9525b14bSRao Shoaib */
196*9525b14bSRao Shoaib int
ns_parserr2(ns_msg * handle,ns_sect section,int rrnum,ns_rr2 * rr)197*9525b14bSRao Shoaib ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
198*9525b14bSRao Shoaib int b;
199*9525b14bSRao Shoaib int tmp;
200*9525b14bSRao Shoaib
201*9525b14bSRao Shoaib /* Make section right. */
202*9525b14bSRao Shoaib if ((tmp = section) < 0 || section >= ns_s_max)
203*9525b14bSRao Shoaib RETERR(ENODEV);
204*9525b14bSRao Shoaib if (section != handle->_sect)
205*9525b14bSRao Shoaib setsection(handle, section);
206*9525b14bSRao Shoaib
207*9525b14bSRao Shoaib /* Make rrnum right. */
208*9525b14bSRao Shoaib if (rrnum == -1)
209*9525b14bSRao Shoaib rrnum = handle->_rrnum;
210*9525b14bSRao Shoaib if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
211*9525b14bSRao Shoaib RETERR(ENODEV);
212*9525b14bSRao Shoaib if (rrnum < handle->_rrnum)
213*9525b14bSRao Shoaib setsection(handle, section);
214*9525b14bSRao Shoaib if (rrnum > handle->_rrnum) {
215*9525b14bSRao Shoaib b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
216*9525b14bSRao Shoaib rrnum - handle->_rrnum);
217*9525b14bSRao Shoaib
218*9525b14bSRao Shoaib if (b < 0)
219*9525b14bSRao Shoaib return (-1);
220*9525b14bSRao Shoaib handle->_msg_ptr += b;
221*9525b14bSRao Shoaib handle->_rrnum = rrnum;
222*9525b14bSRao Shoaib }
223*9525b14bSRao Shoaib
224*9525b14bSRao Shoaib /* Do the parse. */
225*9525b14bSRao Shoaib b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
226*9525b14bSRao Shoaib rr->nname, NS_MAXNNAME, &rr->nnamel);
227*9525b14bSRao Shoaib if (b < 0)
228*9525b14bSRao Shoaib return (-1);
229*9525b14bSRao Shoaib handle->_msg_ptr += b;
230*9525b14bSRao Shoaib if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
231*9525b14bSRao Shoaib RETERR(EMSGSIZE);
232*9525b14bSRao Shoaib NS_GET16(rr->type, handle->_msg_ptr);
233*9525b14bSRao Shoaib NS_GET16(rr->rr_class, handle->_msg_ptr);
234*9525b14bSRao Shoaib if (section == ns_s_qd) {
235*9525b14bSRao Shoaib rr->ttl = 0;
236*9525b14bSRao Shoaib rr->rdlength = 0;
237*9525b14bSRao Shoaib rr->rdata = NULL;
238*9525b14bSRao Shoaib } else {
239*9525b14bSRao Shoaib if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
240*9525b14bSRao Shoaib RETERR(EMSGSIZE);
241*9525b14bSRao Shoaib NS_GET32(rr->ttl, handle->_msg_ptr);
242*9525b14bSRao Shoaib NS_GET16(rr->rdlength, handle->_msg_ptr);
243*9525b14bSRao Shoaib if (handle->_msg_ptr + rr->rdlength > handle->_eom)
244*9525b14bSRao Shoaib RETERR(EMSGSIZE);
245*9525b14bSRao Shoaib rr->rdata = handle->_msg_ptr;
246*9525b14bSRao Shoaib handle->_msg_ptr += rr->rdlength;
247*9525b14bSRao Shoaib }
248*9525b14bSRao Shoaib if (++handle->_rrnum > handle->_counts[(int)section])
249*9525b14bSRao Shoaib setsection(handle, (ns_sect)((int)section + 1));
250*9525b14bSRao Shoaib
251*9525b14bSRao Shoaib /* All done. */
252*9525b14bSRao Shoaib return (0);
253*9525b14bSRao Shoaib }
254*9525b14bSRao Shoaib
2557c478bd9Sstevel@tonic-gate /* Private. */
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate static void
setsection(ns_msg * msg,ns_sect sect)2587c478bd9Sstevel@tonic-gate setsection(ns_msg *msg, ns_sect sect) {
2597c478bd9Sstevel@tonic-gate msg->_sect = sect;
2607c478bd9Sstevel@tonic-gate if (sect == ns_s_max) {
2617c478bd9Sstevel@tonic-gate msg->_rrnum = -1;
2627c478bd9Sstevel@tonic-gate msg->_msg_ptr = NULL;
2637c478bd9Sstevel@tonic-gate } else {
2647c478bd9Sstevel@tonic-gate msg->_rrnum = 0;
2657c478bd9Sstevel@tonic-gate msg->_msg_ptr = msg->_sections[(int)sect];
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate }
268*9525b14bSRao Shoaib
269*9525b14bSRao Shoaib /*! \file */
270