rpcb_prot.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* $NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (c) 2009, Sun Microsystems, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither the name of Sun Microsystems, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Copyright (c) 1986-1991 by Sun Microsystems Inc.
  33. */
  34. /* #ident "@(#)rpcb_prot.c 1.13 94/04/24 SMI" */
  35. #if defined(LIBC_SCCS) && !defined(lint)
  36. static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
  37. #endif
  38. #include <sys/cdefs.h>
  39. __FBSDID("$FreeBSD$");
  40. /*
  41. * rpcb_prot.c
  42. * XDR routines for the rpcbinder version 3.
  43. *
  44. * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
  45. */
  46. #include <sys/param.h>
  47. #include <sys/systm.h>
  48. #include <sys/kernel.h>
  49. #include <sys/malloc.h>
  50. #include <rpc/rpc.h>
  51. #include <rpc/rpc_com.h>
  52. #include <rpc/rpcb_prot.h>
  53. bool_t
  54. xdr_portmap(XDR *xdrs, struct portmap *regs)
  55. {
  56. if (xdr_u_long(xdrs, &regs->pm_prog) &&
  57. xdr_u_long(xdrs, &regs->pm_vers) &&
  58. xdr_u_long(xdrs, &regs->pm_prot))
  59. return (xdr_u_long(xdrs, &regs->pm_port));
  60. return (FALSE);
  61. }
  62. bool_t
  63. xdr_rpcb(XDR *xdrs, RPCB *objp)
  64. {
  65. if (!xdr_uint32_t(xdrs, &objp->r_prog)) {
  66. return (FALSE);
  67. }
  68. if (!xdr_uint32_t(xdrs, &objp->r_vers)) {
  69. return (FALSE);
  70. }
  71. if (!xdr_string(xdrs, &objp->r_netid, RPC_MAXDATASIZE)) {
  72. return (FALSE);
  73. }
  74. if (!xdr_string(xdrs, &objp->r_addr, RPC_MAXDATASIZE)) {
  75. return (FALSE);
  76. }
  77. if (!xdr_string(xdrs, &objp->r_owner, RPC_MAXDATASIZE)) {
  78. return (FALSE);
  79. }
  80. return (TRUE);
  81. }
  82. /*
  83. * rpcblist_ptr implements a linked list. The RPCL definition from
  84. * rpcb_prot.x is:
  85. *
  86. * struct rpcblist {
  87. * rpcb rpcb_map;
  88. * struct rpcblist *rpcb_next;
  89. * };
  90. * typedef rpcblist *rpcblist_ptr;
  91. *
  92. * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
  93. * there's any data behind the pointer, followed by the data (if any exists).
  94. * The boolean can be interpreted as ``more data follows me''; if FALSE then
  95. * nothing follows the boolean; if TRUE then the boolean is followed by an
  96. * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
  97. * rpcblist *").
  98. *
  99. * This could be implemented via the xdr_pointer type, though this would
  100. * result in one recursive call per element in the list. Rather than do that
  101. * we can ``unwind'' the recursion into a while loop and use xdr_reference to
  102. * serialize the rpcb elements.
  103. */
  104. bool_t
  105. xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
  106. {
  107. /*
  108. * more_elements is pre-computed in case the direction is
  109. * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
  110. * xdr_bool when the direction is XDR_DECODE.
  111. */
  112. bool_t more_elements;
  113. int freeing = (xdrs->x_op == XDR_FREE);
  114. rpcblist_ptr next;
  115. rpcblist_ptr next_copy;
  116. next = NULL;
  117. for (;;) {
  118. more_elements = (bool_t)(*rp != NULL);
  119. if (! xdr_bool(xdrs, &more_elements)) {
  120. return (FALSE);
  121. }
  122. if (! more_elements) {
  123. return (TRUE); /* we are done */
  124. }
  125. /*
  126. * the unfortunate side effect of non-recursion is that in
  127. * the case of freeing we must remember the next object
  128. * before we free the current object ...
  129. */
  130. if (freeing && *rp)
  131. next = (*rp)->rpcb_next;
  132. if (! xdr_reference(xdrs, (caddr_t *)rp,
  133. (u_int)sizeof (RPCBLIST), (xdrproc_t)xdr_rpcb)) {
  134. return (FALSE);
  135. }
  136. if (freeing) {
  137. next_copy = next;
  138. rp = &next_copy;
  139. /*
  140. * Note that in the subsequent iteration, next_copy
  141. * gets nulled out by the xdr_reference
  142. * but next itself survives.
  143. */
  144. } else if (*rp) {
  145. rp = &((*rp)->rpcb_next);
  146. }
  147. }
  148. /*NOTREACHED*/
  149. }
  150. #if 0
  151. /*
  152. * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
  153. * functionality to xdr_rpcblist_ptr().
  154. */
  155. bool_t
  156. xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
  157. {
  158. bool_t dummy;
  159. dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
  160. return (dummy);
  161. }
  162. #endif
  163. bool_t
  164. xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
  165. {
  166. if (!xdr_string(xdrs, &objp->r_maddr, RPC_MAXDATASIZE)) {
  167. return (FALSE);
  168. }
  169. if (!xdr_string(xdrs, &objp->r_nc_netid, RPC_MAXDATASIZE)) {
  170. return (FALSE);
  171. }
  172. if (!xdr_uint32_t(xdrs, &objp->r_nc_semantics)) {
  173. return (FALSE);
  174. }
  175. if (!xdr_string(xdrs, &objp->r_nc_protofmly, RPC_MAXDATASIZE)) {
  176. return (FALSE);
  177. }
  178. if (!xdr_string(xdrs, &objp->r_nc_proto, RPC_MAXDATASIZE)) {
  179. return (FALSE);
  180. }
  181. return (TRUE);
  182. }
  183. bool_t
  184. xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
  185. {
  186. /*
  187. * more_elements is pre-computed in case the direction is
  188. * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
  189. * xdr_bool when the direction is XDR_DECODE.
  190. */
  191. bool_t more_elements;
  192. int freeing = (xdrs->x_op == XDR_FREE);
  193. rpcb_entry_list_ptr next;
  194. rpcb_entry_list_ptr next_copy;
  195. next = NULL;
  196. for (;;) {
  197. more_elements = (bool_t)(*rp != NULL);
  198. if (! xdr_bool(xdrs, &more_elements)) {
  199. return (FALSE);
  200. }
  201. if (! more_elements) {
  202. return (TRUE); /* we are done */
  203. }
  204. /*
  205. * the unfortunate side effect of non-recursion is that in
  206. * the case of freeing we must remember the next object
  207. * before we free the current object ...
  208. */
  209. if (freeing)
  210. next = (*rp)->rpcb_entry_next;
  211. if (! xdr_reference(xdrs, (caddr_t *)rp,
  212. (u_int)sizeof (rpcb_entry_list),
  213. (xdrproc_t)xdr_rpcb_entry)) {
  214. return (FALSE);
  215. }
  216. if (freeing && *rp) {
  217. next_copy = next;
  218. rp = &next_copy;
  219. /*
  220. * Note that in the subsequent iteration, next_copy
  221. * gets nulled out by the xdr_reference
  222. * but next itself survives.
  223. */
  224. } else if (*rp) {
  225. rp = &((*rp)->rpcb_entry_next);
  226. }
  227. }
  228. /*NOTREACHED*/
  229. }