xref: /titanic_41/usr/src/lib/libresolv2/common/nameser/ns_rdata.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
1*9525b14bSRao Shoaib /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3*9525b14bSRao Shoaib  * Use is subject to license terms.
4*9525b14bSRao Shoaib  */
5*9525b14bSRao Shoaib 
6*9525b14bSRao Shoaib 
7*9525b14bSRao Shoaib /*
8*9525b14bSRao Shoaib  * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
9*9525b14bSRao Shoaib  *
10*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
11*9525b14bSRao Shoaib  * purpose with or without fee is hereby granted, provided that the above
12*9525b14bSRao Shoaib  * copyright notice and this permission notice appear in all copies.
13*9525b14bSRao Shoaib  *
14*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
17*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
19*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
21*9525b14bSRao Shoaib  */
22*9525b14bSRao Shoaib 
23*9525b14bSRao Shoaib #ifndef lint
24*9525b14bSRao Shoaib static const char rcsid[] = "$Id: ns_rdata.c,v 1.2 2009/01/23 23:49:15 tbox Exp $";
25*9525b14bSRao Shoaib #endif
26*9525b14bSRao Shoaib 
27*9525b14bSRao Shoaib #include "port_before.h"
28*9525b14bSRao Shoaib 
29*9525b14bSRao Shoaib #if __OpenBSD__
30*9525b14bSRao Shoaib #include <sys/types.h>
31*9525b14bSRao Shoaib #endif
32*9525b14bSRao Shoaib #include <netinet/in.h>
33*9525b14bSRao Shoaib #include <arpa/nameser.h>
34*9525b14bSRao Shoaib 
35*9525b14bSRao Shoaib #include <errno.h>
36*9525b14bSRao Shoaib #include <resolv.h>
37*9525b14bSRao Shoaib #include <string.h>
38*9525b14bSRao Shoaib 
39*9525b14bSRao Shoaib #include "port_after.h"
40*9525b14bSRao Shoaib 
41*9525b14bSRao Shoaib #define CONSUME_SRC \
42*9525b14bSRao Shoaib 	do { \
43*9525b14bSRao Shoaib 		rdata += n, rdlen -= n; \
44*9525b14bSRao Shoaib 	} while (0)
45*9525b14bSRao Shoaib 
46*9525b14bSRao Shoaib #define CONSUME_DST \
47*9525b14bSRao Shoaib 	do { \
48*9525b14bSRao Shoaib 		nrdata += n, nrdsiz -= n, nrdlen += n; \
49*9525b14bSRao Shoaib 	} while (0)
50*9525b14bSRao Shoaib 
51*9525b14bSRao Shoaib #define UNPACK_DNAME \
52*9525b14bSRao Shoaib 	do { \
53*9525b14bSRao Shoaib 		size_t t; \
54*9525b14bSRao Shoaib 		\
55*9525b14bSRao Shoaib 		if ((n = ns_name_unpack2(msg,eom,rdata,nrdata,nrdsiz,&t))<0) {\
56*9525b14bSRao Shoaib 			errno = EMSGSIZE; \
57*9525b14bSRao Shoaib 			return (-1); \
58*9525b14bSRao Shoaib 		} \
59*9525b14bSRao Shoaib 		CONSUME_SRC; \
60*9525b14bSRao Shoaib 		n = t; \
61*9525b14bSRao Shoaib 		CONSUME_DST; \
62*9525b14bSRao Shoaib 	} while (0)
63*9525b14bSRao Shoaib 
64*9525b14bSRao Shoaib #define UNPACK_SOME(x) \
65*9525b14bSRao Shoaib 	do { \
66*9525b14bSRao Shoaib 		n = (x); \
67*9525b14bSRao Shoaib 		if ((size_t)n > rdlen || (size_t)n > nrdsiz) { \
68*9525b14bSRao Shoaib 			errno = EMSGSIZE; \
69*9525b14bSRao Shoaib 			return (-1); \
70*9525b14bSRao Shoaib 		} \
71*9525b14bSRao Shoaib 		memcpy(nrdata, rdata, n); \
72*9525b14bSRao Shoaib 		CONSUME_SRC; CONSUME_DST; \
73*9525b14bSRao Shoaib 	} while (0)
74*9525b14bSRao Shoaib 
75*9525b14bSRao Shoaib #define	UNPACK_REST(x) \
76*9525b14bSRao Shoaib 	do { \
77*9525b14bSRao Shoaib 		n = (x); \
78*9525b14bSRao Shoaib 		if ((size_t)n != rdlen) { \
79*9525b14bSRao Shoaib 			errno = EMSGSIZE; \
80*9525b14bSRao Shoaib 			return (-1); \
81*9525b14bSRao Shoaib 		} \
82*9525b14bSRao Shoaib 		memcpy(nrdata, rdata, n); \
83*9525b14bSRao Shoaib 		CONSUME_SRC; CONSUME_DST; \
84*9525b14bSRao Shoaib 	} while (0)
85*9525b14bSRao Shoaib 
86*9525b14bSRao Shoaib ssize_t
ns_rdata_unpack(const u_char * msg,const u_char * eom,ns_type type,const u_char * rdata,size_t rdlen,u_char * nrdata,size_t nrdsiz)87*9525b14bSRao Shoaib ns_rdata_unpack(const u_char *msg, const u_char *eom,
88*9525b14bSRao Shoaib 		ns_type type, const u_char *rdata, size_t rdlen,
89*9525b14bSRao Shoaib 		u_char *nrdata, size_t nrdsiz)
90*9525b14bSRao Shoaib {
91*9525b14bSRao Shoaib 	size_t nrdlen = 0;
92*9525b14bSRao Shoaib 	int n;
93*9525b14bSRao Shoaib 
94*9525b14bSRao Shoaib 	switch (type) {
95*9525b14bSRao Shoaib 	case ns_t_a:
96*9525b14bSRao Shoaib 		UNPACK_REST(NS_INADDRSZ);
97*9525b14bSRao Shoaib 		break;
98*9525b14bSRao Shoaib 	case ns_t_aaaa:
99*9525b14bSRao Shoaib 		UNPACK_REST(NS_IN6ADDRSZ);
100*9525b14bSRao Shoaib 		break;
101*9525b14bSRao Shoaib 	case ns_t_cname:
102*9525b14bSRao Shoaib 	case ns_t_mb:
103*9525b14bSRao Shoaib 	case ns_t_mg:
104*9525b14bSRao Shoaib 	case ns_t_mr:
105*9525b14bSRao Shoaib 	case ns_t_ns:
106*9525b14bSRao Shoaib 	case ns_t_ptr:
107*9525b14bSRao Shoaib 	case ns_t_dname:
108*9525b14bSRao Shoaib 		UNPACK_DNAME;
109*9525b14bSRao Shoaib 		break;
110*9525b14bSRao Shoaib 	case ns_t_soa:
111*9525b14bSRao Shoaib 		UNPACK_DNAME;
112*9525b14bSRao Shoaib 		UNPACK_DNAME;
113*9525b14bSRao Shoaib 		UNPACK_SOME(NS_INT32SZ * 5);
114*9525b14bSRao Shoaib 		break;
115*9525b14bSRao Shoaib 	case ns_t_mx:
116*9525b14bSRao Shoaib 	case ns_t_afsdb:
117*9525b14bSRao Shoaib 	case ns_t_rt:
118*9525b14bSRao Shoaib 		UNPACK_SOME(NS_INT16SZ);
119*9525b14bSRao Shoaib 		UNPACK_DNAME;
120*9525b14bSRao Shoaib 		break;
121*9525b14bSRao Shoaib 	case ns_t_px:
122*9525b14bSRao Shoaib 		UNPACK_SOME(NS_INT16SZ);
123*9525b14bSRao Shoaib 		UNPACK_DNAME;
124*9525b14bSRao Shoaib 		UNPACK_DNAME;
125*9525b14bSRao Shoaib 		break;
126*9525b14bSRao Shoaib 	case ns_t_srv:
127*9525b14bSRao Shoaib 		UNPACK_SOME(NS_INT16SZ * 3);
128*9525b14bSRao Shoaib 		UNPACK_DNAME;
129*9525b14bSRao Shoaib 		break;
130*9525b14bSRao Shoaib 	case ns_t_minfo:
131*9525b14bSRao Shoaib 	case ns_t_rp:
132*9525b14bSRao Shoaib 		UNPACK_DNAME;
133*9525b14bSRao Shoaib 		UNPACK_DNAME;
134*9525b14bSRao Shoaib 		break;
135*9525b14bSRao Shoaib 	default:
136*9525b14bSRao Shoaib 		UNPACK_SOME(rdlen);
137*9525b14bSRao Shoaib 		break;
138*9525b14bSRao Shoaib 	}
139*9525b14bSRao Shoaib 	if (rdlen > 0) {
140*9525b14bSRao Shoaib 		errno = EMSGSIZE;
141*9525b14bSRao Shoaib 		return (-1);
142*9525b14bSRao Shoaib 	}
143*9525b14bSRao Shoaib 	return (nrdlen);
144*9525b14bSRao Shoaib }
145*9525b14bSRao Shoaib 
146*9525b14bSRao Shoaib #define	EQUAL_CONSUME \
147*9525b14bSRao Shoaib 	do { \
148*9525b14bSRao Shoaib 		rdata1 += n, rdlen1 -= n; \
149*9525b14bSRao Shoaib 		rdata2 += n, rdlen2 -= n; \
150*9525b14bSRao Shoaib 	} while (0)
151*9525b14bSRao Shoaib 
152*9525b14bSRao Shoaib #define	EQUAL_DNAME \
153*9525b14bSRao Shoaib 	do { \
154*9525b14bSRao Shoaib 		ssize_t n; \
155*9525b14bSRao Shoaib 		\
156*9525b14bSRao Shoaib 		if (rdlen1 != rdlen2) \
157*9525b14bSRao Shoaib 			return (0); \
158*9525b14bSRao Shoaib 		n = ns_name_eq(rdata1, rdlen1, rdata2, rdlen2); \
159*9525b14bSRao Shoaib 		if (n <= 0) \
160*9525b14bSRao Shoaib 			return (n); \
161*9525b14bSRao Shoaib 		n = rdlen1; \
162*9525b14bSRao Shoaib 		EQUAL_CONSUME; \
163*9525b14bSRao Shoaib 	} while (0)
164*9525b14bSRao Shoaib 
165*9525b14bSRao Shoaib #define	EQUAL_SOME(x) \
166*9525b14bSRao Shoaib 	do { \
167*9525b14bSRao Shoaib 		size_t n = (x); \
168*9525b14bSRao Shoaib 		\
169*9525b14bSRao Shoaib 		if (n > rdlen1 || n > rdlen2) { \
170*9525b14bSRao Shoaib 			errno = EMSGSIZE; \
171*9525b14bSRao Shoaib 			return (-1); \
172*9525b14bSRao Shoaib 		} \
173*9525b14bSRao Shoaib 		if (memcmp(rdata1, rdata2, n) != 0) \
174*9525b14bSRao Shoaib 			return (0); \
175*9525b14bSRao Shoaib 		EQUAL_CONSUME; \
176*9525b14bSRao Shoaib 	} while (0)
177*9525b14bSRao Shoaib 
178*9525b14bSRao Shoaib int
ns_rdata_equal(ns_type type,const u_char * rdata1,size_t rdlen1,const u_char * rdata2,size_t rdlen2)179*9525b14bSRao Shoaib ns_rdata_equal(ns_type type,
180*9525b14bSRao Shoaib 	       const u_char *rdata1, size_t rdlen1,
181*9525b14bSRao Shoaib 	       const u_char *rdata2, size_t rdlen2)
182*9525b14bSRao Shoaib {
183*9525b14bSRao Shoaib 	switch (type) {
184*9525b14bSRao Shoaib 	case ns_t_cname:
185*9525b14bSRao Shoaib 	case ns_t_mb:
186*9525b14bSRao Shoaib 	case ns_t_mg:
187*9525b14bSRao Shoaib 	case ns_t_mr:
188*9525b14bSRao Shoaib 	case ns_t_ns:
189*9525b14bSRao Shoaib 	case ns_t_ptr:
190*9525b14bSRao Shoaib 	case ns_t_dname:
191*9525b14bSRao Shoaib 		EQUAL_DNAME;
192*9525b14bSRao Shoaib 		break;
193*9525b14bSRao Shoaib 	case ns_t_soa:
194*9525b14bSRao Shoaib 		/* "There can be only one." --Highlander */
195*9525b14bSRao Shoaib 		break;
196*9525b14bSRao Shoaib 	case ns_t_mx:
197*9525b14bSRao Shoaib 	case ns_t_afsdb:
198*9525b14bSRao Shoaib 	case ns_t_rt:
199*9525b14bSRao Shoaib 		EQUAL_SOME(NS_INT16SZ);
200*9525b14bSRao Shoaib 		EQUAL_DNAME;
201*9525b14bSRao Shoaib 		break;
202*9525b14bSRao Shoaib 	case ns_t_px:
203*9525b14bSRao Shoaib 		EQUAL_SOME(NS_INT16SZ);
204*9525b14bSRao Shoaib 		EQUAL_DNAME;
205*9525b14bSRao Shoaib 		EQUAL_DNAME;
206*9525b14bSRao Shoaib 		break;
207*9525b14bSRao Shoaib 	case ns_t_srv:
208*9525b14bSRao Shoaib 		EQUAL_SOME(NS_INT16SZ * 3);
209*9525b14bSRao Shoaib 		EQUAL_DNAME;
210*9525b14bSRao Shoaib 		break;
211*9525b14bSRao Shoaib 	case ns_t_minfo:
212*9525b14bSRao Shoaib 	case ns_t_rp:
213*9525b14bSRao Shoaib 		EQUAL_DNAME;
214*9525b14bSRao Shoaib 		EQUAL_DNAME;
215*9525b14bSRao Shoaib 		break;
216*9525b14bSRao Shoaib 	default:
217*9525b14bSRao Shoaib 		EQUAL_SOME(rdlen1);
218*9525b14bSRao Shoaib 		break;
219*9525b14bSRao Shoaib 	}
220*9525b14bSRao Shoaib 	if (rdlen1 != 0 || rdlen2 != 0)
221*9525b14bSRao Shoaib 		return (0);
222*9525b14bSRao Shoaib 	return (1);
223*9525b14bSRao Shoaib }
224*9525b14bSRao Shoaib 
225*9525b14bSRao Shoaib #define REFERS_DNAME \
226*9525b14bSRao Shoaib 	do { \
227*9525b14bSRao Shoaib 		int n; \
228*9525b14bSRao Shoaib 		\
229*9525b14bSRao Shoaib 		n = ns_name_eq(rdata, rdlen, nname, NS_MAXNNAME); \
230*9525b14bSRao Shoaib 		if (n < 0) \
231*9525b14bSRao Shoaib 			return (-1); \
232*9525b14bSRao Shoaib 		if (n > 0) \
233*9525b14bSRao Shoaib 			return (1); \
234*9525b14bSRao Shoaib 		n = dn_skipname(rdata, rdata + rdlen); \
235*9525b14bSRao Shoaib 		if (n < 0) \
236*9525b14bSRao Shoaib 			return (-1); \
237*9525b14bSRao Shoaib 		CONSUME_SRC; \
238*9525b14bSRao Shoaib 	} while (0)
239*9525b14bSRao Shoaib 
240*9525b14bSRao Shoaib #define	REFERS_SOME(x) \
241*9525b14bSRao Shoaib 	do { \
242*9525b14bSRao Shoaib 		size_t n = (x); \
243*9525b14bSRao Shoaib 		\
244*9525b14bSRao Shoaib 		if (n > rdlen) { \
245*9525b14bSRao Shoaib 			errno = EMSGSIZE; \
246*9525b14bSRao Shoaib 			return (-1); \
247*9525b14bSRao Shoaib 		} \
248*9525b14bSRao Shoaib 		CONSUME_SRC; \
249*9525b14bSRao Shoaib 	} while (0)
250*9525b14bSRao Shoaib 
251*9525b14bSRao Shoaib int
ns_rdata_refers(ns_type type,const u_char * rdata,size_t rdlen,const u_char * nname)252*9525b14bSRao Shoaib ns_rdata_refers(ns_type type,
253*9525b14bSRao Shoaib 		const u_char *rdata, size_t rdlen,
254*9525b14bSRao Shoaib 		const u_char *nname)
255*9525b14bSRao Shoaib {
256*9525b14bSRao Shoaib 	switch (type) {
257*9525b14bSRao Shoaib 	case ns_t_cname:
258*9525b14bSRao Shoaib 	case ns_t_mb:
259*9525b14bSRao Shoaib 	case ns_t_mg:
260*9525b14bSRao Shoaib 	case ns_t_mr:
261*9525b14bSRao Shoaib 	case ns_t_ns:
262*9525b14bSRao Shoaib 	case ns_t_ptr:
263*9525b14bSRao Shoaib 	case ns_t_dname:
264*9525b14bSRao Shoaib 		REFERS_DNAME;
265*9525b14bSRao Shoaib 		break;
266*9525b14bSRao Shoaib 	case ns_t_soa:
267*9525b14bSRao Shoaib 		REFERS_DNAME;
268*9525b14bSRao Shoaib 		REFERS_DNAME;
269*9525b14bSRao Shoaib 		REFERS_SOME(NS_INT32SZ * 5);
270*9525b14bSRao Shoaib 		break;
271*9525b14bSRao Shoaib 	case ns_t_mx:
272*9525b14bSRao Shoaib 	case ns_t_afsdb:
273*9525b14bSRao Shoaib 	case ns_t_rt:
274*9525b14bSRao Shoaib 		REFERS_SOME(NS_INT16SZ);
275*9525b14bSRao Shoaib 		REFERS_DNAME;
276*9525b14bSRao Shoaib 		break;
277*9525b14bSRao Shoaib 	case ns_t_px:
278*9525b14bSRao Shoaib 		REFERS_SOME(NS_INT16SZ);
279*9525b14bSRao Shoaib 		REFERS_DNAME;
280*9525b14bSRao Shoaib 		REFERS_DNAME;
281*9525b14bSRao Shoaib 		break;
282*9525b14bSRao Shoaib 	case ns_t_srv:
283*9525b14bSRao Shoaib 		REFERS_SOME(NS_INT16SZ * 3);
284*9525b14bSRao Shoaib 		REFERS_DNAME;
285*9525b14bSRao Shoaib 		break;
286*9525b14bSRao Shoaib 	case ns_t_minfo:
287*9525b14bSRao Shoaib 	case ns_t_rp:
288*9525b14bSRao Shoaib 		REFERS_DNAME;
289*9525b14bSRao Shoaib 		REFERS_DNAME;
290*9525b14bSRao Shoaib 		break;
291*9525b14bSRao Shoaib 	default:
292*9525b14bSRao Shoaib 		REFERS_SOME(rdlen);
293*9525b14bSRao Shoaib 		break;
294*9525b14bSRao Shoaib 	}
295*9525b14bSRao Shoaib 	if (rdlen != 0) {
296*9525b14bSRao Shoaib 		errno = EMSGSIZE;
297*9525b14bSRao Shoaib 		return (-1);
298*9525b14bSRao Shoaib 	}
299*9525b14bSRao Shoaib 	return (0);
300*9525b14bSRao Shoaib }
301