Home | History | Annotate | Line # | Download | only in rpc
rpcb_prot.c revision 1.1.4.2
      1 /*	$NetBSD: rpcb_prot.c,v 1.1.4.2 2000/06/23 16:17:49 minoura Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 /*
     32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     33  */
     34 
     35 /* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
     36 
     37 #if 0
     38 #if !defined(lint) && defined(SCCSIDS)
     39 static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
     40 #endif
     41 #endif
     42 
     43 /*
     44  * rpcb_prot.c
     45  * XDR routines for the rpcbinder version 3.
     46  *
     47  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
     48  */
     49 
     50 #include "namespace.h"
     51 
     52 #include <rpc/rpc.h>
     53 #include <rpc/types.h>
     54 #include <rpc/xdr.h>
     55 #include <rpc/rpcb_prot.h>
     56 
     57 #ifdef __weak_alias
     58 __weak_alias(xdr_rpcb,_xdr_rpcb)
     59 __weak_alias(xdr_rpcblist_ptr,_xdr_rpcblist_ptr)
     60 __weak_alias(xdr_rpcblist,_xdr_rpcblist)
     61 __weak_alias(xdr_rpcb_entry,_xdr_rpcb_entry)
     62 __weak_alias(xdr_rpcb_entry_list_ptr,_xdr_rpcb_entry_list_ptr)
     63 __weak_alias(xdr_rpcb_rmtcallargs,_xdr_rpcb_rmtcallargs)
     64 __weak_alias(xdr_rpcb_rmtcallres,_xdr_rpcb_rmtcallres)
     65 __weak_alias(xdr_netbuf,_xdr_netbuf)
     66 #endif
     67 
     68 
     69 bool_t
     70 xdr_rpcb(xdrs, objp)
     71 	XDR *xdrs;
     72 	RPCB *objp;
     73 {
     74 	if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
     75 		return (FALSE);
     76 	}
     77 	if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
     78 		return (FALSE);
     79 	}
     80 	if (!xdr_string(xdrs, &objp->r_netid, ~0)) {
     81 		return (FALSE);
     82 	}
     83 	if (!xdr_string(xdrs, &objp->r_addr, ~0)) {
     84 		return (FALSE);
     85 	}
     86 	if (!xdr_string(xdrs, &objp->r_owner, ~0)) {
     87 		return (FALSE);
     88 	}
     89 	return (TRUE);
     90 }
     91 
     92 /*
     93  * rpcblist_ptr implements a linked list.  The RPCL definition from
     94  * rpcb_prot.x is:
     95  *
     96  * struct rpcblist {
     97  * 	rpcb		rpcb_map;
     98  *	struct rpcblist *rpcb_next;
     99  * };
    100  * typedef rpcblist *rpcblist_ptr;
    101  *
    102  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
    103  * there's any data behind the pointer, followed by the data (if any exists).
    104  * The boolean can be interpreted as ``more data follows me''; if FALSE then
    105  * nothing follows the boolean; if TRUE then the boolean is followed by an
    106  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
    107  * rpcblist *").
    108  *
    109  * This could be implemented via the xdr_pointer type, though this would
    110  * result in one recursive call per element in the list.  Rather than do that
    111  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
    112  * serialize the rpcb elements.
    113  */
    114 
    115 bool_t
    116 xdr_rpcblist_ptr(xdrs, rp)
    117 	register XDR *xdrs;
    118 	register rpcblist_ptr *rp;
    119 {
    120 	/*
    121 	 * more_elements is pre-computed in case the direction is
    122 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
    123 	 * xdr_bool when the direction is XDR_DECODE.
    124 	 */
    125 	bool_t more_elements;
    126 	register int freeing = (xdrs->x_op == XDR_FREE);
    127 	rpcblist_ptr next;
    128 	rpcblist_ptr next_copy;
    129 
    130 	while (TRUE) {
    131 		more_elements = (bool_t)(*rp != NULL);
    132 		if (! xdr_bool(xdrs, &more_elements)) {
    133 			return (FALSE);
    134 		}
    135 		if (! more_elements) {
    136 			return (TRUE);  /* we are done */
    137 		}
    138 		/*
    139 		 * the unfortunate side effect of non-recursion is that in
    140 		 * the case of freeing we must remember the next object
    141 		 * before we free the current object ...
    142 		 */
    143 		if (freeing)
    144 			next = (*rp)->rpcb_next;
    145 		if (! xdr_reference(xdrs, (caddr_t *)rp,
    146 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
    147 			return (FALSE);
    148 		}
    149 		if (freeing) {
    150 			next_copy = next;
    151 			rp = &next_copy;
    152 			/*
    153 			 * Note that in the subsequent iteration, next_copy
    154 			 * gets nulled out by the xdr_reference
    155 			 * but next itself survives.
    156 			 */
    157 		} else {
    158 			rp = &((*rp)->rpcb_next);
    159 		}
    160 	}
    161 	/*NOTREACHED*/
    162 }
    163 
    164 /*
    165  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
    166  * functionality to xdr_rpcblist_ptr().
    167  */
    168 bool_t
    169 xdr_rpcblist(xdrs, rp)
    170 	register XDR *xdrs;
    171 	register RPCBLIST **rp;
    172 {
    173 	bool_t	dummy;
    174 
    175 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
    176 	return (dummy);
    177 }
    178 
    179 
    180 bool_t
    181 xdr_rpcb_entry(xdrs, objp)
    182 	XDR *xdrs;
    183 	rpcb_entry *objp;
    184 {
    185 	if (!xdr_string(xdrs, &objp->r_maddr, ~0)) {
    186 		return (FALSE);
    187 	}
    188 	if (!xdr_string(xdrs, &objp->r_nc_netid, ~0)) {
    189 		return (FALSE);
    190 	}
    191 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
    192 		return (FALSE);
    193 	}
    194 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, ~0)) {
    195 		return (FALSE);
    196 	}
    197 	if (!xdr_string(xdrs, &objp->r_nc_proto, ~0)) {
    198 		return (FALSE);
    199 	}
    200 	return (TRUE);
    201 }
    202 
    203 bool_t
    204 xdr_rpcb_entry_list_ptr(xdrs, rp)
    205 	register XDR *xdrs;
    206 	register rpcb_entry_list_ptr *rp;
    207 {
    208 	/*
    209 	 * more_elements is pre-computed in case the direction is
    210 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
    211 	 * xdr_bool when the direction is XDR_DECODE.
    212 	 */
    213 	bool_t more_elements;
    214 	register int freeing = (xdrs->x_op == XDR_FREE);
    215 	rpcb_entry_list_ptr next;
    216 	rpcb_entry_list_ptr next_copy;
    217 
    218 	while (TRUE) {
    219 		more_elements = (bool_t)(*rp != NULL);
    220 		if (! xdr_bool(xdrs, &more_elements)) {
    221 			return (FALSE);
    222 		}
    223 		if (! more_elements) {
    224 			return (TRUE);  /* we are done */
    225 		}
    226 		/*
    227 		 * the unfortunate side effect of non-recursion is that in
    228 		 * the case of freeing we must remember the next object
    229 		 * before we free the current object ...
    230 		 */
    231 		if (freeing)
    232 			next = (*rp)->rpcb_entry_next;
    233 		if (! xdr_reference(xdrs, (caddr_t *)rp,
    234 		    (u_int)sizeof (rpcb_entry_list),
    235 				    (xdrproc_t)xdr_rpcb_entry)) {
    236 			return (FALSE);
    237 		}
    238 		if (freeing) {
    239 			next_copy = next;
    240 			rp = &next_copy;
    241 			/*
    242 			 * Note that in the subsequent iteration, next_copy
    243 			 * gets nulled out by the xdr_reference
    244 			 * but next itself survives.
    245 			 */
    246 		} else {
    247 			rp = &((*rp)->rpcb_entry_next);
    248 		}
    249 	}
    250 	/*NOTREACHED*/
    251 }
    252 
    253 /*
    254  * XDR remote call arguments
    255  * written for XDR_ENCODE direction only
    256  */
    257 bool_t
    258 xdr_rpcb_rmtcallargs(xdrs, p)
    259 	XDR *xdrs;
    260 	struct rpcb_rmtcallargs *p;
    261 {
    262 	struct r_rpcb_rmtcallargs *objp = (struct r_rpcb_rmtcallargs *)p;
    263 	u_int lenposition, argposition, position;
    264 	int32_t *buf;
    265 
    266 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
    267 	if (buf == NULL) {
    268 		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
    269 			return (FALSE);
    270 		}
    271 		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
    272 			return (FALSE);
    273 		}
    274 		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
    275 			return (FALSE);
    276 		}
    277 	} else {
    278 		IXDR_PUT_U_LONG(buf, objp->prog);
    279 		IXDR_PUT_U_LONG(buf, objp->vers);
    280 		IXDR_PUT_U_LONG(buf, objp->proc);
    281 	}
    282 
    283 	/*
    284 	 * All the jugglery for just getting the size of the arguments
    285 	 */
    286 	lenposition = XDR_GETPOS(xdrs);
    287 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
    288 		return (FALSE);
    289 	}
    290 	argposition = XDR_GETPOS(xdrs);
    291 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
    292 		return (FALSE);
    293 	}
    294 	position = XDR_GETPOS(xdrs);
    295 	objp->args.args_len = (u_long)position - (u_long)argposition;
    296 	XDR_SETPOS(xdrs, lenposition);
    297 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
    298 		return (FALSE);
    299 	}
    300 	XDR_SETPOS(xdrs, position);
    301 	return (TRUE);
    302 }
    303 
    304 /*
    305  * XDR remote call results
    306  * written for XDR_DECODE direction only
    307  */
    308 bool_t
    309 xdr_rpcb_rmtcallres(xdrs, p)
    310 	XDR *xdrs;
    311 	struct rpcb_rmtcallres *p;
    312 {
    313 	bool_t dummy;
    314 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)p;
    315 
    316 	if (!xdr_string(xdrs, &objp->addr, ~0)) {
    317 		return (FALSE);
    318 	}
    319 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
    320 		return (FALSE);
    321 	}
    322 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
    323 	return (dummy);
    324 }
    325 
    326 bool_t
    327 xdr_netbuf(xdrs, objp)
    328 	XDR *xdrs;
    329 	struct netbuf *objp;
    330 {
    331 	bool_t dummy;
    332 
    333 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
    334 		return (FALSE);
    335 	}
    336 	dummy = xdr_bytes(xdrs, (char **)&(objp->buf),
    337 			(u_int *)&(objp->len), objp->maxlen);
    338 	return (dummy);
    339 }
    340