1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate  * As of BIND 8.2.2, ISC (a) removed res_mkupdate(), res_update(), and
10*0Sstevel@tonic-gate  * res_mkupdrec() from what they consider the supported interface. The
11*0Sstevel@tonic-gate  * functions still exist, but their calling interface has changed, since
12*0Sstevel@tonic-gate  * the ns_updrec structure has changed.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * It seems probable that res_mkupdate()  etc. will return, though possibly
15*0Sstevel@tonic-gate  * with other changes, in some future BIND release. In order to avoid
16*0Sstevel@tonic-gate  * going to PSARC twice (once to remove the functions, and then again to
17*0Sstevel@tonic-gate  * add them back), we retain the old interface as a wrapper around the
18*0Sstevel@tonic-gate  * new one.
19*0Sstevel@tonic-gate  */
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #include <port_before.h>
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #include <malloc.h>
24*0Sstevel@tonic-gate #include <strings.h>
25*0Sstevel@tonic-gate #include <sys/types.h>
26*0Sstevel@tonic-gate #include <netinet/in.h>
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <res_update.h>
29*0Sstevel@tonic-gate #undef	ns_updrec
30*0Sstevel@tonic-gate #undef	res_mkupdate
31*0Sstevel@tonic-gate #undef	res_update
32*0Sstevel@tonic-gate #undef	res_mkupdrec
33*0Sstevel@tonic-gate #undef	res_freeupdrec
34*0Sstevel@tonic-gate #include <arpa/nameser.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <port_after.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate void	res_freeupdrec(ns_updrec *);
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static int
42*0Sstevel@tonic-gate old2new(ns_updrec *old, __ISC_ns_updrec *new) {
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	if (old->r_dname != 0) {
45*0Sstevel@tonic-gate 		if ((new->r_dname = strdup(old->r_dname)) == 0)
46*0Sstevel@tonic-gate 			return (-1);
47*0Sstevel@tonic-gate 	} else {
48*0Sstevel@tonic-gate 		new->r_dname = 0;
49*0Sstevel@tonic-gate 	}
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	new->r_glink.prev =
52*0Sstevel@tonic-gate 	new->r_glink.next =
53*0Sstevel@tonic-gate 	new->r_link.prev  =
54*0Sstevel@tonic-gate 	new->r_link.next  = 0;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 	new->r_section	= old->r_section;
57*0Sstevel@tonic-gate 	new->r_class	= old->r_class;
58*0Sstevel@tonic-gate 	new->r_type	= old->r_type;
59*0Sstevel@tonic-gate 	new->r_ttl	= old->r_ttl;
60*0Sstevel@tonic-gate 	new->r_data	= old->r_data;
61*0Sstevel@tonic-gate 	new->r_size	= old->r_size;
62*0Sstevel@tonic-gate 	new->r_opcode	= old->r_opcode;
63*0Sstevel@tonic-gate 	new->r_dp	= old->r_dp;
64*0Sstevel@tonic-gate 	new->r_deldp	= old->r_deldp;
65*0Sstevel@tonic-gate 	new->r_zone	= old->r_zone;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	return (0);
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static int
72*0Sstevel@tonic-gate new2old(__ISC_ns_updrec *new, ns_updrec *old) {
73*0Sstevel@tonic-gate 	/* XXX r_prev and r_next unchanged */
74*0Sstevel@tonic-gate 	if (new->r_dname != 0) {
75*0Sstevel@tonic-gate 		if ((old->r_dname = strdup(new->r_dname)) == 0)
76*0Sstevel@tonic-gate 			return (-1);
77*0Sstevel@tonic-gate 	} else {
78*0Sstevel@tonic-gate 		old->r_dname = 0;
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	old->r_section	= new->r_section;
81*0Sstevel@tonic-gate 	old->r_class	= new->r_class;
82*0Sstevel@tonic-gate 	old->r_type	= new->r_type;
83*0Sstevel@tonic-gate 	old->r_ttl	= new->r_ttl;
84*0Sstevel@tonic-gate 	old->r_data	= new->r_data;
85*0Sstevel@tonic-gate 	old->r_size	= new->r_size;
86*0Sstevel@tonic-gate 	old->r_opcode	= new->r_opcode;
87*0Sstevel@tonic-gate 	old->r_grpnext	= 0;			/* XXX */
88*0Sstevel@tonic-gate 	old->r_dp	= new->r_dp;
89*0Sstevel@tonic-gate 	old->r_deldp	= new->r_deldp;
90*0Sstevel@tonic-gate 	old->r_zone	= new->r_zone;
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	return (0);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate static void
97*0Sstevel@tonic-gate delete_list(__ISC_ns_updrec *list) {
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	__ISC_ns_updrec	*next;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	for (; list != 0; list = next) {
102*0Sstevel@tonic-gate 		next = list->r_link.next;
103*0Sstevel@tonic-gate 		__ISC_res_freeupdrec(list);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static __ISC_ns_updrec *
109*0Sstevel@tonic-gate copy_list(ns_updrec *old) {
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	__ISC_ns_updrec *list = 0, *r, *p;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (old == 0)
114*0Sstevel@tonic-gate 		return (0);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	for (p = 0; old != 0; old = old->r_next, p = r) {
117*0Sstevel@tonic-gate 		if ((r = calloc(1, sizeof (*r))) == 0 ||
118*0Sstevel@tonic-gate 			old2new(old, r) != 0) {
119*0Sstevel@tonic-gate 			free(r);
120*0Sstevel@tonic-gate 			delete_list(list);
121*0Sstevel@tonic-gate 			return (0);
122*0Sstevel@tonic-gate 		}
123*0Sstevel@tonic-gate 		r->r_link.prev = p;
124*0Sstevel@tonic-gate 		r->r_link.next = 0;
125*0Sstevel@tonic-gate 		if (p != 0)
126*0Sstevel@tonic-gate 			p->r_link.next = r;
127*0Sstevel@tonic-gate 		else
128*0Sstevel@tonic-gate 			list = r;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	return (list);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate int
136*0Sstevel@tonic-gate res_mkupdate(ns_updrec  *rrecp_in, u_char *buf, int length) {
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	__ISC_ns_updrec	*r;
139*0Sstevel@tonic-gate 	int		ret;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if ((r = copy_list(rrecp_in)) == 0)
142*0Sstevel@tonic-gate 		return (-1);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	ret = __ISC_res_mkupdate(r, buf, length);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	delete_list(r);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	return (ret);
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate int
153*0Sstevel@tonic-gate res_update(ns_updrec *rrecp_in) {
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	__ISC_ns_updrec	*r;
156*0Sstevel@tonic-gate 	int		ret;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if ((r = copy_list(rrecp_in)) == 0)
159*0Sstevel@tonic-gate 		return (-1);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	ret = __ISC_res_update(r);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	delete_list(r);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	return (ret);
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate ns_updrec *
171*0Sstevel@tonic-gate res_mkupdrec(int section, const char *dname, uint_t class, uint_t type,
172*0Sstevel@tonic-gate 		uint_t ttl) {
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	__ISC_ns_updrec	*n;
175*0Sstevel@tonic-gate 	ns_updrec	*o;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	n = __ISC_res_mkupdrec(section, dname, class, type, ttl);
178*0Sstevel@tonic-gate 	if (n == 0)
179*0Sstevel@tonic-gate 		return (0);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	if ((o = calloc(1, sizeof (*o))) != 0) {
182*0Sstevel@tonic-gate 		if (new2old(n, o) != 0) {
183*0Sstevel@tonic-gate 			res_freeupdrec(o);
184*0Sstevel@tonic-gate 			o = 0;
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	__ISC_res_freeupdrec(n);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	return (o);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate void
195*0Sstevel@tonic-gate res_freeupdrec(ns_updrec *rrecp) {
196*0Sstevel@tonic-gate 	if (rrecp == 0)
197*0Sstevel@tonic-gate 		return;
198*0Sstevel@tonic-gate 	/* Note: freeing r_dp is the caller's responsibility. */
199*0Sstevel@tonic-gate 	if (rrecp->r_dname != NULL)
200*0Sstevel@tonic-gate 		free(rrecp->r_dname);
201*0Sstevel@tonic-gate 	free(rrecp);
202*0Sstevel@tonic-gate }
203