10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate * Copyright (c) 1996,1999 by Internet Software Consortium.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate *
9*11038SRao.Shoaib@Sun.COM * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate */
170Sstevel@tonic-gate
180Sstevel@tonic-gate #ifndef lint
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate
220Sstevel@tonic-gate /* Import. */
230Sstevel@tonic-gate
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <netinet/in.h>
290Sstevel@tonic-gate #include <arpa/nameser.h>
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <resolv.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include "port_after.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /* Forward. */
380Sstevel@tonic-gate
390Sstevel@tonic-gate static void setsection(ns_msg *msg, ns_sect sect);
400Sstevel@tonic-gate
410Sstevel@tonic-gate /* Macros. */
420Sstevel@tonic-gate
43*11038SRao.Shoaib@Sun.COM #if !defined(SOLARIS2) || defined(__COVERITY__)
440Sstevel@tonic-gate #define RETERR(err) do { errno = (err); return (-1); } while (0)
450Sstevel@tonic-gate #else
46*11038SRao.Shoaib@Sun.COM #define RETERR(err) \
47*11038SRao.Shoaib@Sun.COM do { errno = (err); if (errno == errno) return (-1); } while (0)
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate
50*11038SRao.Shoaib@Sun.COM #define PARSE_FMT_PRESO 0 /* Parse using presentation-format names */
51*11038SRao.Shoaib@Sun.COM #define PARSE_FMT_WIRE 1 /* Parse using network-format names */
52*11038SRao.Shoaib@Sun.COM
530Sstevel@tonic-gate /* Public. */
540Sstevel@tonic-gate
550Sstevel@tonic-gate /* These need to be in the same order as the nres.h:ns_flag enum. */
560Sstevel@tonic-gate struct _ns_flagdata _ns_flagdata[16] = {
57*11038SRao.Shoaib@Sun.COM { 0x8000, 15 }, /*%< qr. */
58*11038SRao.Shoaib@Sun.COM { 0x7800, 11 }, /*%< opcode. */
59*11038SRao.Shoaib@Sun.COM { 0x0400, 10 }, /*%< aa. */
60*11038SRao.Shoaib@Sun.COM { 0x0200, 9 }, /*%< tc. */
61*11038SRao.Shoaib@Sun.COM { 0x0100, 8 }, /*%< rd. */
62*11038SRao.Shoaib@Sun.COM { 0x0080, 7 }, /*%< ra. */
63*11038SRao.Shoaib@Sun.COM { 0x0040, 6 }, /*%< z. */
64*11038SRao.Shoaib@Sun.COM { 0x0020, 5 }, /*%< ad. */
65*11038SRao.Shoaib@Sun.COM { 0x0010, 4 }, /*%< cd. */
66*11038SRao.Shoaib@Sun.COM { 0x000f, 0 }, /*%< rcode. */
67*11038SRao.Shoaib@Sun.COM { 0x0000, 0 }, /*%< expansion (1/6). */
68*11038SRao.Shoaib@Sun.COM { 0x0000, 0 }, /*%< expansion (2/6). */
69*11038SRao.Shoaib@Sun.COM { 0x0000, 0 }, /*%< expansion (3/6). */
70*11038SRao.Shoaib@Sun.COM { 0x0000, 0 }, /*%< expansion (4/6). */
71*11038SRao.Shoaib@Sun.COM { 0x0000, 0 }, /*%< expansion (5/6). */
72*11038SRao.Shoaib@Sun.COM { 0x0000, 0 }, /*%< expansion (6/6). */
730Sstevel@tonic-gate };
740Sstevel@tonic-gate
ns_msg_getflag(ns_msg handle,int flag)750Sstevel@tonic-gate int ns_msg_getflag(ns_msg handle, int flag) {
760Sstevel@tonic-gate return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate int
ns_skiprr(const u_char * ptr,const u_char * eom,ns_sect section,int count)800Sstevel@tonic-gate ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
810Sstevel@tonic-gate const u_char *optr = ptr;
820Sstevel@tonic-gate
830Sstevel@tonic-gate for ((void)NULL; count > 0; count--) {
840Sstevel@tonic-gate int b, rdlength;
850Sstevel@tonic-gate
860Sstevel@tonic-gate b = dn_skipname(ptr, eom);
870Sstevel@tonic-gate if (b < 0)
880Sstevel@tonic-gate RETERR(EMSGSIZE);
890Sstevel@tonic-gate ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
900Sstevel@tonic-gate if (section != ns_s_qd) {
910Sstevel@tonic-gate if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
920Sstevel@tonic-gate RETERR(EMSGSIZE);
930Sstevel@tonic-gate ptr += NS_INT32SZ/*TTL*/;
940Sstevel@tonic-gate NS_GET16(rdlength, ptr);
950Sstevel@tonic-gate ptr += rdlength/*RData*/;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate }
980Sstevel@tonic-gate if (ptr > eom)
990Sstevel@tonic-gate RETERR(EMSGSIZE);
1000Sstevel@tonic-gate return (ptr - optr);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate int
ns_initparse(const u_char * msg,int msglen,ns_msg * handle)1040Sstevel@tonic-gate ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
1050Sstevel@tonic-gate const u_char *eom = msg + msglen;
1060Sstevel@tonic-gate int i;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate handle->_msg = msg;
1090Sstevel@tonic-gate handle->_eom = eom;
1100Sstevel@tonic-gate if (msg + NS_INT16SZ > eom)
1110Sstevel@tonic-gate RETERR(EMSGSIZE);
1120Sstevel@tonic-gate NS_GET16(handle->_id, msg);
1130Sstevel@tonic-gate if (msg + NS_INT16SZ > eom)
1140Sstevel@tonic-gate RETERR(EMSGSIZE);
1150Sstevel@tonic-gate NS_GET16(handle->_flags, msg);
1160Sstevel@tonic-gate for (i = 0; i < ns_s_max; i++) {
1170Sstevel@tonic-gate if (msg + NS_INT16SZ > eom)
1180Sstevel@tonic-gate RETERR(EMSGSIZE);
1190Sstevel@tonic-gate NS_GET16(handle->_counts[i], msg);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate for (i = 0; i < ns_s_max; i++)
1220Sstevel@tonic-gate if (handle->_counts[i] == 0)
1230Sstevel@tonic-gate handle->_sections[i] = NULL;
1240Sstevel@tonic-gate else {
1250Sstevel@tonic-gate int b = ns_skiprr(msg, eom, (ns_sect)i,
1260Sstevel@tonic-gate handle->_counts[i]);
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if (b < 0)
1290Sstevel@tonic-gate return (-1);
1300Sstevel@tonic-gate handle->_sections[i] = msg;
1310Sstevel@tonic-gate msg += b;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate if (msg != eom)
1340Sstevel@tonic-gate RETERR(EMSGSIZE);
1350Sstevel@tonic-gate setsection(handle, ns_s_max);
1360Sstevel@tonic-gate return (0);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate int
ns_parserr(ns_msg * handle,ns_sect section,int rrnum,ns_rr * rr)1400Sstevel@tonic-gate ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
1410Sstevel@tonic-gate int b;
1420Sstevel@tonic-gate int tmp;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /* Make section right. */
145*11038SRao.Shoaib@Sun.COM tmp = section;
146*11038SRao.Shoaib@Sun.COM if (tmp < 0 || section >= ns_s_max)
1470Sstevel@tonic-gate RETERR(ENODEV);
1480Sstevel@tonic-gate if (section != handle->_sect)
1490Sstevel@tonic-gate setsection(handle, section);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /* Make rrnum right. */
1520Sstevel@tonic-gate if (rrnum == -1)
1530Sstevel@tonic-gate rrnum = handle->_rrnum;
1540Sstevel@tonic-gate if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
1550Sstevel@tonic-gate RETERR(ENODEV);
1560Sstevel@tonic-gate if (rrnum < handle->_rrnum)
1570Sstevel@tonic-gate setsection(handle, section);
1580Sstevel@tonic-gate if (rrnum > handle->_rrnum) {
1590Sstevel@tonic-gate b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
1600Sstevel@tonic-gate rrnum - handle->_rrnum);
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (b < 0)
1630Sstevel@tonic-gate return (-1);
1640Sstevel@tonic-gate handle->_msg_ptr += b;
1650Sstevel@tonic-gate handle->_rrnum = rrnum;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /* Do the parse. */
1690Sstevel@tonic-gate b = dn_expand(handle->_msg, handle->_eom,
1700Sstevel@tonic-gate handle->_msg_ptr, rr->name, NS_MAXDNAME);
1710Sstevel@tonic-gate if (b < 0)
1720Sstevel@tonic-gate return (-1);
1730Sstevel@tonic-gate handle->_msg_ptr += b;
1740Sstevel@tonic-gate if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
1750Sstevel@tonic-gate RETERR(EMSGSIZE);
1760Sstevel@tonic-gate NS_GET16(rr->type, handle->_msg_ptr);
1770Sstevel@tonic-gate NS_GET16(rr->rr_class, handle->_msg_ptr);
1780Sstevel@tonic-gate if (section == ns_s_qd) {
1790Sstevel@tonic-gate rr->ttl = 0;
1800Sstevel@tonic-gate rr->rdlength = 0;
1810Sstevel@tonic-gate rr->rdata = NULL;
1820Sstevel@tonic-gate } else {
1830Sstevel@tonic-gate if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
1840Sstevel@tonic-gate RETERR(EMSGSIZE);
1850Sstevel@tonic-gate NS_GET32(rr->ttl, handle->_msg_ptr);
1860Sstevel@tonic-gate NS_GET16(rr->rdlength, handle->_msg_ptr);
1870Sstevel@tonic-gate if (handle->_msg_ptr + rr->rdlength > handle->_eom)
1880Sstevel@tonic-gate RETERR(EMSGSIZE);
1890Sstevel@tonic-gate rr->rdata = handle->_msg_ptr;
1900Sstevel@tonic-gate handle->_msg_ptr += rr->rdlength;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate if (++handle->_rrnum > handle->_counts[(int)section])
1930Sstevel@tonic-gate setsection(handle, (ns_sect)((int)section + 1));
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /* All done. */
1960Sstevel@tonic-gate return (0);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
199*11038SRao.Shoaib@Sun.COM /*
200*11038SRao.Shoaib@Sun.COM * This is identical to the above but uses network-format (uncompressed) names.
201*11038SRao.Shoaib@Sun.COM */
202*11038SRao.Shoaib@Sun.COM int
ns_parserr2(ns_msg * handle,ns_sect section,int rrnum,ns_rr2 * rr)203*11038SRao.Shoaib@Sun.COM ns_parserr2(ns_msg *handle, ns_sect section, int rrnum, ns_rr2 *rr) {
204*11038SRao.Shoaib@Sun.COM int b;
205*11038SRao.Shoaib@Sun.COM int tmp;
206*11038SRao.Shoaib@Sun.COM
207*11038SRao.Shoaib@Sun.COM /* Make section right. */
208*11038SRao.Shoaib@Sun.COM if ((tmp = section) < 0 || section >= ns_s_max)
209*11038SRao.Shoaib@Sun.COM RETERR(ENODEV);
210*11038SRao.Shoaib@Sun.COM if (section != handle->_sect)
211*11038SRao.Shoaib@Sun.COM setsection(handle, section);
212*11038SRao.Shoaib@Sun.COM
213*11038SRao.Shoaib@Sun.COM /* Make rrnum right. */
214*11038SRao.Shoaib@Sun.COM if (rrnum == -1)
215*11038SRao.Shoaib@Sun.COM rrnum = handle->_rrnum;
216*11038SRao.Shoaib@Sun.COM if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
217*11038SRao.Shoaib@Sun.COM RETERR(ENODEV);
218*11038SRao.Shoaib@Sun.COM if (rrnum < handle->_rrnum)
219*11038SRao.Shoaib@Sun.COM setsection(handle, section);
220*11038SRao.Shoaib@Sun.COM if (rrnum > handle->_rrnum) {
221*11038SRao.Shoaib@Sun.COM b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
222*11038SRao.Shoaib@Sun.COM rrnum - handle->_rrnum);
223*11038SRao.Shoaib@Sun.COM
224*11038SRao.Shoaib@Sun.COM if (b < 0)
225*11038SRao.Shoaib@Sun.COM return (-1);
226*11038SRao.Shoaib@Sun.COM handle->_msg_ptr += b;
227*11038SRao.Shoaib@Sun.COM handle->_rrnum = rrnum;
228*11038SRao.Shoaib@Sun.COM }
229*11038SRao.Shoaib@Sun.COM
230*11038SRao.Shoaib@Sun.COM /* Do the parse. */
231*11038SRao.Shoaib@Sun.COM b = ns_name_unpack2(handle->_msg, handle->_eom, handle->_msg_ptr,
232*11038SRao.Shoaib@Sun.COM rr->nname, NS_MAXNNAME, &rr->nnamel);
233*11038SRao.Shoaib@Sun.COM if (b < 0)
234*11038SRao.Shoaib@Sun.COM return (-1);
235*11038SRao.Shoaib@Sun.COM handle->_msg_ptr += b;
236*11038SRao.Shoaib@Sun.COM if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
237*11038SRao.Shoaib@Sun.COM RETERR(EMSGSIZE);
238*11038SRao.Shoaib@Sun.COM NS_GET16(rr->type, handle->_msg_ptr);
239*11038SRao.Shoaib@Sun.COM NS_GET16(rr->rr_class, handle->_msg_ptr);
240*11038SRao.Shoaib@Sun.COM if (section == ns_s_qd) {
241*11038SRao.Shoaib@Sun.COM rr->ttl = 0;
242*11038SRao.Shoaib@Sun.COM rr->rdlength = 0;
243*11038SRao.Shoaib@Sun.COM rr->rdata = NULL;
244*11038SRao.Shoaib@Sun.COM } else {
245*11038SRao.Shoaib@Sun.COM if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
246*11038SRao.Shoaib@Sun.COM RETERR(EMSGSIZE);
247*11038SRao.Shoaib@Sun.COM NS_GET32(rr->ttl, handle->_msg_ptr);
248*11038SRao.Shoaib@Sun.COM NS_GET16(rr->rdlength, handle->_msg_ptr);
249*11038SRao.Shoaib@Sun.COM if (handle->_msg_ptr + rr->rdlength > handle->_eom)
250*11038SRao.Shoaib@Sun.COM RETERR(EMSGSIZE);
251*11038SRao.Shoaib@Sun.COM rr->rdata = handle->_msg_ptr;
252*11038SRao.Shoaib@Sun.COM handle->_msg_ptr += rr->rdlength;
253*11038SRao.Shoaib@Sun.COM }
254*11038SRao.Shoaib@Sun.COM if (++handle->_rrnum > handle->_counts[(int)section])
255*11038SRao.Shoaib@Sun.COM setsection(handle, (ns_sect)((int)section + 1));
256*11038SRao.Shoaib@Sun.COM
257*11038SRao.Shoaib@Sun.COM /* All done. */
258*11038SRao.Shoaib@Sun.COM return (0);
259*11038SRao.Shoaib@Sun.COM }
260*11038SRao.Shoaib@Sun.COM
2610Sstevel@tonic-gate /* Private. */
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate static void
setsection(ns_msg * msg,ns_sect sect)2640Sstevel@tonic-gate setsection(ns_msg *msg, ns_sect sect) {
2650Sstevel@tonic-gate msg->_sect = sect;
2660Sstevel@tonic-gate if (sect == ns_s_max) {
2670Sstevel@tonic-gate msg->_rrnum = -1;
2680Sstevel@tonic-gate msg->_msg_ptr = NULL;
2690Sstevel@tonic-gate } else {
2700Sstevel@tonic-gate msg->_rrnum = 0;
2710Sstevel@tonic-gate msg->_msg_ptr = msg->_sections[(int)sect];
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate }
274*11038SRao.Shoaib@Sun.COM
275*11038SRao.Shoaib@Sun.COM /*! \file */
276