Home | History | Annotate | Line # | Download | only in rpc
rpcb_prot.c revision 1.11
      1  1.11      tron /*	$NetBSD: rpcb_prot.c,v 1.11 2013/03/11 20:19:29 tron Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*
      4  1.11      tron  * Copyright (c) 2010, Oracle America, Inc.
      5  1.11      tron  *
      6  1.11      tron  * Redistribution and use in source and binary forms, with or without
      7  1.11      tron  * modification, are permitted provided that the following conditions are
      8  1.11      tron  * met:
      9  1.11      tron  *
     10  1.11      tron  *     * Redistributions of source code must retain the above copyright
     11  1.11      tron  *       notice, this list of conditions and the following disclaimer.
     12  1.11      tron  *     * Redistributions in binary form must reproduce the above
     13  1.11      tron  *       copyright notice, this list of conditions and the following
     14  1.11      tron  *       disclaimer in the documentation and/or other materials
     15  1.11      tron  *       provided with the distribution.
     16  1.11      tron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.11      tron  *       contributors may be used to endorse or promote products derived
     18  1.11      tron  *       from this software without specific prior written permission.
     19  1.11      tron  *
     20  1.11      tron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.11      tron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.11      tron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.11      tron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.11      tron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.11      tron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.11      tron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.11      tron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.11      tron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.11      tron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.11      tron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.11      tron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1      fvdl  */
     33   1.1      fvdl /*
     34   1.1      fvdl  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
     35   1.1      fvdl  */
     36   1.1      fvdl 
     37   1.1      fvdl /* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
     38   1.1      fvdl 
     39   1.5    itojun #include <sys/cdefs.h>
     40   1.5    itojun #if defined(LIBC_SCCS) && !defined(lint)
     41   1.1      fvdl #if 0
     42   1.1      fvdl static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
     43   1.5    itojun #else
     44  1.11      tron __RCSID("$NetBSD: rpcb_prot.c,v 1.11 2013/03/11 20:19:29 tron Exp $");
     45   1.1      fvdl #endif
     46   1.1      fvdl #endif
     47   1.1      fvdl 
     48   1.1      fvdl /*
     49   1.1      fvdl  * rpcb_prot.c
     50   1.1      fvdl  * XDR routines for the rpcbinder version 3.
     51   1.1      fvdl  *
     52   1.1      fvdl  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
     53   1.1      fvdl  */
     54   1.1      fvdl 
     55   1.1      fvdl #include "namespace.h"
     56   1.1      fvdl 
     57   1.1      fvdl #include <rpc/rpc.h>
     58   1.1      fvdl #include <rpc/types.h>
     59   1.1      fvdl #include <rpc/xdr.h>
     60   1.1      fvdl #include <rpc/rpcb_prot.h>
     61   1.1      fvdl 
     62   1.4     lukem #include <assert.h>
     63   1.4     lukem 
     64   1.1      fvdl #ifdef __weak_alias
     65   1.1      fvdl __weak_alias(xdr_rpcb,_xdr_rpcb)
     66   1.1      fvdl __weak_alias(xdr_rpcblist_ptr,_xdr_rpcblist_ptr)
     67   1.1      fvdl __weak_alias(xdr_rpcblist,_xdr_rpcblist)
     68   1.1      fvdl __weak_alias(xdr_rpcb_entry,_xdr_rpcb_entry)
     69   1.1      fvdl __weak_alias(xdr_rpcb_entry_list_ptr,_xdr_rpcb_entry_list_ptr)
     70   1.1      fvdl __weak_alias(xdr_rpcb_rmtcallargs,_xdr_rpcb_rmtcallargs)
     71   1.1      fvdl __weak_alias(xdr_rpcb_rmtcallres,_xdr_rpcb_rmtcallres)
     72   1.1      fvdl __weak_alias(xdr_netbuf,_xdr_netbuf)
     73   1.1      fvdl #endif
     74   1.1      fvdl 
     75   1.1      fvdl 
     76   1.1      fvdl bool_t
     77  1.10       abs xdr_rpcb(XDR *xdrs, 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.10       abs xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
    125   1.1      fvdl {
    126   1.1      fvdl 	/*
    127   1.1      fvdl 	 * more_elements is pre-computed in case the direction is
    128   1.1      fvdl 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
    129   1.1      fvdl 	 * xdr_bool when the direction is XDR_DECODE.
    130   1.1      fvdl 	 */
    131   1.1      fvdl 	bool_t more_elements;
    132   1.4     lukem 	int freeing;
    133   1.1      fvdl 	rpcblist_ptr next;
    134   1.1      fvdl 	rpcblist_ptr next_copy;
    135   1.1      fvdl 
    136   1.4     lukem 	_DIAGASSERT(xdrs != NULL);
    137   1.4     lukem 	/* XXX: rp may be NULL ??? */
    138   1.4     lukem 
    139   1.4     lukem 	freeing = (xdrs->x_op == XDR_FREE);
    140   1.6     lukem 	next = NULL;
    141   1.4     lukem 
    142   1.2  christos 	for (;;) {
    143   1.1      fvdl 		more_elements = (bool_t)(*rp != NULL);
    144   1.1      fvdl 		if (! xdr_bool(xdrs, &more_elements)) {
    145   1.1      fvdl 			return (FALSE);
    146   1.1      fvdl 		}
    147   1.1      fvdl 		if (! more_elements) {
    148   1.1      fvdl 			return (TRUE);  /* we are done */
    149   1.1      fvdl 		}
    150   1.1      fvdl 		/*
    151   1.1      fvdl 		 * the unfortunate side effect of non-recursion is that in
    152   1.1      fvdl 		 * the case of freeing we must remember the next object
    153   1.1      fvdl 		 * before we free the current object ...
    154   1.1      fvdl 		 */
    155   1.7  christos 		if (freeing && *rp)
    156   1.1      fvdl 			next = (*rp)->rpcb_next;
    157   1.1      fvdl 		if (! xdr_reference(xdrs, (caddr_t *)rp,
    158   1.1      fvdl 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
    159   1.1      fvdl 			return (FALSE);
    160   1.1      fvdl 		}
    161   1.1      fvdl 		if (freeing) {
    162   1.1      fvdl 			next_copy = next;
    163   1.1      fvdl 			rp = &next_copy;
    164   1.1      fvdl 			/*
    165   1.1      fvdl 			 * Note that in the subsequent iteration, next_copy
    166   1.1      fvdl 			 * gets nulled out by the xdr_reference
    167   1.1      fvdl 			 * but next itself survives.
    168   1.1      fvdl 			 */
    169   1.7  christos 		} else if (*rp) {
    170   1.1      fvdl 			rp = &((*rp)->rpcb_next);
    171   1.1      fvdl 		}
    172   1.1      fvdl 	}
    173   1.1      fvdl 	/*NOTREACHED*/
    174   1.1      fvdl }
    175   1.1      fvdl 
    176   1.1      fvdl /*
    177   1.1      fvdl  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
    178   1.1      fvdl  * functionality to xdr_rpcblist_ptr().
    179   1.1      fvdl  */
    180   1.1      fvdl bool_t
    181  1.10       abs xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
    182   1.1      fvdl {
    183   1.1      fvdl 	bool_t	dummy;
    184   1.1      fvdl 
    185   1.1      fvdl 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
    186   1.1      fvdl 	return (dummy);
    187   1.1      fvdl }
    188   1.1      fvdl 
    189   1.1      fvdl 
    190   1.1      fvdl bool_t
    191  1.10       abs xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
    192   1.1      fvdl {
    193   1.4     lukem 
    194   1.4     lukem 	_DIAGASSERT(objp != NULL);
    195   1.4     lukem 
    196   1.2  christos 	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
    197   1.1      fvdl 		return (FALSE);
    198   1.1      fvdl 	}
    199   1.2  christos 	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
    200   1.1      fvdl 		return (FALSE);
    201   1.1      fvdl 	}
    202   1.1      fvdl 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
    203   1.1      fvdl 		return (FALSE);
    204   1.1      fvdl 	}
    205   1.2  christos 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
    206   1.1      fvdl 		return (FALSE);
    207   1.1      fvdl 	}
    208   1.2  christos 	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
    209   1.1      fvdl 		return (FALSE);
    210   1.1      fvdl 	}
    211   1.1      fvdl 	return (TRUE);
    212   1.1      fvdl }
    213   1.1      fvdl 
    214   1.1      fvdl bool_t
    215  1.10       abs xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
    216   1.1      fvdl {
    217   1.1      fvdl 	/*
    218   1.1      fvdl 	 * more_elements is pre-computed in case the direction is
    219   1.1      fvdl 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
    220   1.1      fvdl 	 * xdr_bool when the direction is XDR_DECODE.
    221   1.1      fvdl 	 */
    222   1.1      fvdl 	bool_t more_elements;
    223   1.4     lukem 	int freeing;
    224   1.1      fvdl 	rpcb_entry_list_ptr next;
    225   1.1      fvdl 	rpcb_entry_list_ptr next_copy;
    226   1.1      fvdl 
    227   1.4     lukem 	_DIAGASSERT(xdrs != NULL);
    228   1.4     lukem 	/* XXX: rp is allowed to be NULL ??? */
    229   1.4     lukem 
    230   1.4     lukem 	freeing = (xdrs->x_op == XDR_FREE);
    231   1.6     lukem 	next = NULL;
    232   1.4     lukem 
    233   1.2  christos 	for (;;) {
    234   1.1      fvdl 		more_elements = (bool_t)(*rp != NULL);
    235   1.1      fvdl 		if (! xdr_bool(xdrs, &more_elements)) {
    236   1.1      fvdl 			return (FALSE);
    237   1.1      fvdl 		}
    238   1.1      fvdl 		if (! more_elements) {
    239   1.1      fvdl 			return (TRUE);  /* we are done */
    240   1.1      fvdl 		}
    241   1.1      fvdl 		/*
    242   1.1      fvdl 		 * the unfortunate side effect of non-recursion is that in
    243   1.1      fvdl 		 * the case of freeing we must remember the next object
    244   1.1      fvdl 		 * before we free the current object ...
    245   1.1      fvdl 		 */
    246   1.8  christos 		if (freeing && *rp)
    247   1.1      fvdl 			next = (*rp)->rpcb_entry_next;
    248   1.1      fvdl 		if (! xdr_reference(xdrs, (caddr_t *)rp,
    249   1.1      fvdl 		    (u_int)sizeof (rpcb_entry_list),
    250   1.1      fvdl 				    (xdrproc_t)xdr_rpcb_entry)) {
    251   1.1      fvdl 			return (FALSE);
    252   1.1      fvdl 		}
    253   1.1      fvdl 		if (freeing) {
    254   1.1      fvdl 			next_copy = next;
    255   1.1      fvdl 			rp = &next_copy;
    256   1.1      fvdl 			/*
    257   1.1      fvdl 			 * Note that in the subsequent iteration, next_copy
    258   1.1      fvdl 			 * gets nulled out by the xdr_reference
    259   1.1      fvdl 			 * but next itself survives.
    260   1.1      fvdl 			 */
    261   1.8  christos 		} else if (*rp) {
    262   1.1      fvdl 			rp = &((*rp)->rpcb_entry_next);
    263   1.1      fvdl 		}
    264   1.1      fvdl 	}
    265   1.1      fvdl 	/*NOTREACHED*/
    266   1.1      fvdl }
    267   1.1      fvdl 
    268   1.1      fvdl /*
    269   1.1      fvdl  * XDR remote call arguments
    270   1.1      fvdl  * written for XDR_ENCODE direction only
    271   1.1      fvdl  */
    272   1.1      fvdl bool_t
    273  1.10       abs xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
    274   1.1      fvdl {
    275   1.2  christos 	struct r_rpcb_rmtcallargs *objp =
    276   1.2  christos 	    (struct r_rpcb_rmtcallargs *)(void *)p;
    277   1.1      fvdl 	u_int lenposition, argposition, position;
    278   1.1      fvdl 	int32_t *buf;
    279   1.1      fvdl 
    280   1.4     lukem 	_DIAGASSERT(p != NULL);
    281   1.4     lukem 
    282   1.1      fvdl 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
    283   1.1      fvdl 	if (buf == NULL) {
    284   1.1      fvdl 		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
    285   1.1      fvdl 			return (FALSE);
    286   1.1      fvdl 		}
    287   1.1      fvdl 		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
    288   1.1      fvdl 			return (FALSE);
    289   1.1      fvdl 		}
    290   1.1      fvdl 		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
    291   1.1      fvdl 			return (FALSE);
    292   1.1      fvdl 		}
    293   1.1      fvdl 	} else {
    294   1.3      fvdl 		IXDR_PUT_U_INT32(buf, objp->prog);
    295   1.3      fvdl 		IXDR_PUT_U_INT32(buf, objp->vers);
    296   1.3      fvdl 		IXDR_PUT_U_INT32(buf, objp->proc);
    297   1.1      fvdl 	}
    298   1.1      fvdl 
    299   1.1      fvdl 	/*
    300   1.1      fvdl 	 * All the jugglery for just getting the size of the arguments
    301   1.1      fvdl 	 */
    302   1.1      fvdl 	lenposition = XDR_GETPOS(xdrs);
    303   1.1      fvdl 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
    304   1.1      fvdl 		return (FALSE);
    305   1.1      fvdl 	}
    306   1.1      fvdl 	argposition = XDR_GETPOS(xdrs);
    307   1.1      fvdl 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
    308   1.1      fvdl 		return (FALSE);
    309   1.1      fvdl 	}
    310   1.1      fvdl 	position = XDR_GETPOS(xdrs);
    311   1.2  christos 	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
    312   1.1      fvdl 	XDR_SETPOS(xdrs, lenposition);
    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 	XDR_SETPOS(xdrs, position);
    317   1.1      fvdl 	return (TRUE);
    318   1.1      fvdl }
    319   1.1      fvdl 
    320   1.1      fvdl /*
    321   1.1      fvdl  * XDR remote call results
    322   1.1      fvdl  * written for XDR_DECODE direction only
    323   1.1      fvdl  */
    324   1.1      fvdl bool_t
    325  1.10       abs xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
    326   1.1      fvdl {
    327   1.1      fvdl 	bool_t dummy;
    328   1.2  christos 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
    329   1.1      fvdl 
    330   1.4     lukem 	_DIAGASSERT(p != NULL);
    331   1.4     lukem 
    332   1.2  christos 	if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
    333   1.1      fvdl 		return (FALSE);
    334   1.1      fvdl 	}
    335   1.1      fvdl 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
    336   1.1      fvdl 		return (FALSE);
    337   1.1      fvdl 	}
    338   1.1      fvdl 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
    339   1.1      fvdl 	return (dummy);
    340   1.1      fvdl }
    341   1.1      fvdl 
    342   1.1      fvdl bool_t
    343  1.10       abs xdr_netbuf(XDR *xdrs, struct netbuf *objp)
    344   1.1      fvdl {
    345   1.1      fvdl 	bool_t dummy;
    346   1.4     lukem 
    347   1.4     lukem 	_DIAGASSERT(objp != NULL);
    348   1.1      fvdl 
    349   1.1      fvdl 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
    350   1.1      fvdl 		return (FALSE);
    351   1.1      fvdl 	}
    352   1.9       mrg 	dummy = xdr_bytes(xdrs, (char **)(void *)&(objp->buf),
    353   1.1      fvdl 			(u_int *)&(objp->len), objp->maxlen);
    354   1.1      fvdl 	return (dummy);
    355   1.1      fvdl }
    356