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