rpc_prot.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. /* @(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC */
  10. /*
  11. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  12. * unrestricted use provided that this legend is included on all tape
  13. * media and as a part of the software program in whole or part. Users
  14. * may copy or modify Sun RPC without charge, but are not authorized
  15. * to license or distribute it to anyone else except as part of a product or
  16. * program developed by the user.
  17. *
  18. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  19. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  21. *
  22. * Sun RPC is provided with no support and without any obligation on the
  23. * part of Sun Microsystems, Inc. to assist in its use, correction,
  24. * modification or enhancement.
  25. *
  26. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  27. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  28. * OR ANY PART THEREOF.
  29. *
  30. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  31. * or profits or other special, indirect and consequential damages, even if
  32. * Sun has been advised of the possibility of such damages.
  33. *
  34. * Sun Microsystems, Inc.
  35. * 2550 Garcia Avenue
  36. * Mountain View, California 94043
  37. */
  38. #if !defined(lint) && defined(SCCSIDS)
  39. static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
  40. #endif
  41. /*
  42. * rpc_prot.c
  43. *
  44. * Copyright (C) 1984, Sun Microsystems, Inc.
  45. *
  46. * This set of routines implements the rpc message definition,
  47. * its serializer and some common rpc utility routines.
  48. * The routines are meant for various implementations of rpc -
  49. * they are NOT for the rpc client or rpc service implementations!
  50. * Because authentication stuff is easy and is part of rpc, the opaque
  51. * routines are also in this program.
  52. */
  53. #include <rpc/rpc.h>
  54. /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
  55. /*
  56. * XDR an opaque authentication struct
  57. * (see auth.h)
  58. */
  59. bool_t xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
  60. {
  61. if (xdr_enum(xdrs, &(ap->oa_flavor)))
  62. return (xdr_bytes(xdrs, &ap->oa_base,
  63. &ap->oa_length, MAX_AUTH_BYTES));
  64. return (FALSE);
  65. }
  66. /*
  67. * XDR a DES block
  68. */
  69. bool_t xdr_des_block(XDR *xdrs, des_block *blkp)
  70. {
  71. return (xdr_opaque(xdrs, (char*) blkp, sizeof(des_block)));
  72. }
  73. /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
  74. /*
  75. * XDR the MSG_ACCEPTED part of a reply message union
  76. */
  77. static bool_t xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
  78. {
  79. /* personalized union, rather than calling xdr_union */
  80. if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
  81. return (FALSE);
  82. if (!xdr_enum(xdrs, (enum_t *) & (ar->ar_stat)))
  83. return (FALSE);
  84. switch (ar->ar_stat) {
  85. case SUCCESS:
  86. return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
  87. case PROG_MISMATCH:
  88. if (!xdr_u_long(xdrs, &(ar->ar_vers.low)))
  89. return (FALSE);
  90. return (xdr_u_long(xdrs, &(ar->ar_vers.high)));
  91. }
  92. return (TRUE); /* TRUE => open ended set of problems */
  93. }
  94. /*
  95. * XDR the MSG_DENIED part of a reply message union
  96. */
  97. static bool_t xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
  98. {
  99. /* personalized union, rather than calling xdr_union */
  100. if (!xdr_enum(xdrs, (enum_t *) & (rr->rj_stat)))
  101. return (FALSE);
  102. switch (rr->rj_stat) {
  103. case RPC_MISMATCH:
  104. if (!xdr_u_long(xdrs, &(rr->rj_vers.low)))
  105. return (FALSE);
  106. return (xdr_u_long(xdrs, &(rr->rj_vers.high)));
  107. case AUTH_ERROR:
  108. return (xdr_enum(xdrs, (enum_t *) & (rr->rj_why)));
  109. }
  110. return (FALSE);
  111. }
  112. static struct xdr_discrim reply_dscrm[3] = {
  113. {(int) MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply},
  114. {(int) MSG_DENIED, (xdrproc_t)xdr_rejected_reply},
  115. {__dontcare__, NULL_xdrproc_t}
  116. };
  117. /*
  118. * XDR a reply message
  119. */
  120. bool_t xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
  121. {
  122. if (xdr_u_long(xdrs, &(rmsg->rm_xid)) &&
  123. xdr_enum(xdrs, (enum_t *) & (rmsg->rm_direction)) &&
  124. (rmsg->rm_direction == REPLY))
  125. return (xdr_union(xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat),
  126. (char*) & (rmsg->rm_reply.ru), reply_dscrm,
  127. NULL_xdrproc_t));
  128. return (FALSE);
  129. }
  130. /*
  131. * Serializes the "static part" of a call message header.
  132. * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
  133. * The rm_xid is not really static, but the user can easily munge on the fly.
  134. */
  135. bool_t xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
  136. {
  137. cmsg->rm_direction = CALL;
  138. cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
  139. if (
  140. (xdrs->x_op == XDR_ENCODE) &&
  141. xdr_u_long(xdrs, &(cmsg->rm_xid)) &&
  142. xdr_enum(xdrs, (enum_t *) & (cmsg->rm_direction)) &&
  143. xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
  144. xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)))
  145. return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)));
  146. return (FALSE);
  147. }
  148. /* ************************** Client utility routine ************* */
  149. static void accepted(enum accept_stat acpt_stat, struct rpc_err *error)
  150. {
  151. switch (acpt_stat) {
  152. case PROG_UNAVAIL:
  153. error->re_status = RPC_PROGUNAVAIL;
  154. return;
  155. case PROG_MISMATCH:
  156. error->re_status = RPC_PROGVERSMISMATCH;
  157. return;
  158. case PROC_UNAVAIL:
  159. error->re_status = RPC_PROCUNAVAIL;
  160. return;
  161. case GARBAGE_ARGS:
  162. error->re_status = RPC_CANTDECODEARGS;
  163. return;
  164. case SYSTEM_ERR:
  165. error->re_status = RPC_SYSTEMERROR;
  166. return;
  167. case SUCCESS:
  168. error->re_status = RPC_SUCCESS;
  169. return;
  170. }
  171. /* something's wrong, but we don't know what ... */
  172. error->re_status = RPC_FAILED;
  173. error->re_lb.s1 = (long) MSG_ACCEPTED;
  174. error->re_lb.s2 = (long) acpt_stat;
  175. }
  176. static void rejected(enum reject_stat rjct_stat, struct rpc_err *error)
  177. {
  178. switch (rjct_stat) {
  179. case RPC_VERSMISMATCH:
  180. error->re_status = RPC_VERSMISMATCH;
  181. return;
  182. case AUTH_ERROR:
  183. error->re_status = RPC_AUTHERROR;
  184. return;
  185. }
  186. /* something's wrong, but we don't know what ... */
  187. error->re_status = RPC_FAILED;
  188. error->re_lb.s1 = (long) MSG_DENIED;
  189. error->re_lb.s2 = (long) rjct_stat;
  190. }
  191. /*
  192. * given a reply message, fills in the error
  193. */
  194. void _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
  195. {
  196. /* optimized for normal, SUCCESSful case */
  197. switch (msg->rm_reply.rp_stat) {
  198. case MSG_ACCEPTED:
  199. if (msg->acpted_rply.ar_stat == SUCCESS) {
  200. error->re_status = RPC_SUCCESS;
  201. return;
  202. };
  203. accepted((enum accept_stat)msg->acpted_rply.ar_stat, error);
  204. break;
  205. case MSG_DENIED:
  206. rejected((enum reject_stat)msg->rjcted_rply.rj_stat, error);
  207. break;
  208. default:
  209. error->re_status = RPC_FAILED;
  210. error->re_lb.s1 = (long) (msg->rm_reply.rp_stat);
  211. break;
  212. }
  213. switch (error->re_status) {
  214. case RPC_VERSMISMATCH:
  215. error->re_vers.low = msg->rjcted_rply.rj_vers.low;
  216. error->re_vers.high = msg->rjcted_rply.rj_vers.high;
  217. break;
  218. case RPC_AUTHERROR:
  219. error->re_why = msg->rjcted_rply.rj_why;
  220. break;
  221. case RPC_PROGVERSMISMATCH:
  222. error->re_vers.low = msg->acpted_rply.ar_vers.low;
  223. error->re_vers.high = msg->acpted_rply.ar_vers.high;
  224. break;
  225. }
  226. }