Import TBBT (NFS trace replay).
[bluesky.git] / TBBT / trace_play / rpc / rpc_prot.c
1 #ifndef lint
2 static char sfs_rpc_prot_id[] = "@(#)rpc_prot.c     2.1     97/10/23";
3 #endif
4 /* @(#)rpc_prot.c       2.3 88/08/07 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * rpc_prot.c
57  *
58  * Copyright (C) 1984, Sun Microsystems, Inc.
59  *
60  * This set of routines implements the rpc message definition,
61  * its serializer and some common rpc utility routines.
62  * The routines are meant for various implementations of rpc -
63  * they are NOT for the rpc client or rpc service implementations!
64  * Because authentication stuff is easy and is part of rpc, the opaque
65  * routines are also in this program.
66  */
67
68 #include "rpc/rpc.h"
69
70 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
71
72 struct opaque_auth _null_auth;
73
74 /*
75  * XDR an opaque authentication struct
76  * (see auth.h)
77  */
78 bool_t
79 xdr_opaque_auth(
80         XDR *xdrs,
81         struct opaque_auth *ap)
82 {
83
84         if (xdr_enum(xdrs, &(ap->oa_flavor)))
85                 return (xdr_bytes(xdrs, (void *)&ap->oa_base,
86                         &ap->oa_length, MAX_AUTH_BYTES));
87         return (FALSE);
88 }
89
90 /*
91  * XDR a DES block
92  */
93 bool_t
94 xdr_des_block(
95         XDR *xdrs,
96         des_block *blkp)
97 {
98         return (xdr_opaque(xdrs, (void *)blkp, sizeof(des_block)));
99 }
100
101 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
102
103 /*
104  * XDR the MSG_ACCEPTED part of a reply message union
105  */
106 bool_t 
107 xdr_accepted_reply(
108         XDR *xdrs,   
109         struct accepted_reply *ar)
110 {
111
112         /* personalized union, rather than calling xdr_union */
113         if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
114                 return (FALSE);
115         if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
116                 return (FALSE);
117         switch (ar->ar_stat) {
118
119         case SUCCESS:
120                 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
121
122         case PROG_MISMATCH:
123                 if (! xdr_uint32_t(xdrs, &(ar->ar_vers.low)))
124                         return (FALSE);
125                 return (xdr_uint32_t(xdrs, &(ar->ar_vers.high)));
126         }
127         return (TRUE);  /* TRUE => open ended set of problems */
128 }
129
130 /*
131  * XDR the MSG_DENIED part of a reply message union
132  */
133 bool_t 
134 xdr_rejected_reply(
135         XDR *xdrs,
136         struct rejected_reply *rr)
137 {
138
139         /* personalized union, rather than calling xdr_union */
140         if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
141                 return (FALSE);
142         switch (rr->rj_stat) {
143
144         case RPC_MISMATCH:
145                 if (! xdr_uint32_t(xdrs, &(rr->rj_vers.low)))
146                         return (FALSE);
147                 return (xdr_uint32_t(xdrs, &(rr->rj_vers.high)));
148
149         case AUTH_ERROR:
150                 return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
151         }
152         return (FALSE);
153 }
154
155 static struct xdr_discrim reply_dscrm[3] = {
156         { (int)MSG_ACCEPTED, xdr_accepted_reply },
157         { (int)MSG_DENIED, xdr_rejected_reply },
158         { __dontcare__, NULL_xdrproc_t } };
159
160 /*
161  * XDR a reply message
162  */
163 bool_t
164 xdr_replymsg(
165         XDR *xdrs,
166         struct rpc_msg *rmsg)
167 {
168         if (
169             xdr_uint32_t(xdrs, &(rmsg->rm_xid)) && 
170             xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
171             (rmsg->rm_direction == REPLY) )
172                 return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
173                    (void *)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t));
174         return (FALSE);
175 }
176
177
178 /*
179  * Serializes the "static part" of a call message header.
180  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
181  * The rm_xid is not really static, but the user can easily munge on the fly.
182  */
183 bool_t
184 xdr_callhdr(
185         XDR *xdrs,
186         struct rpc_msg *cmsg)
187 {
188
189         cmsg->rm_direction = CALL;
190         cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
191         if (
192             (xdrs->x_op == XDR_ENCODE) &&
193             xdr_uint32_t(xdrs, &(cmsg->rm_xid)) &&
194             xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
195             xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
196             xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
197             return (xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_vers)));
198         return (FALSE);
199 }
200
201 /* ************************** Client utility routine ************* */
202
203 static void
204 accepted(
205         enum accept_stat acpt_stat,
206         struct rpc_err *error)
207 {
208
209         switch (acpt_stat) {
210
211         case PROG_UNAVAIL:
212                 error->re_status = RPC_PROGUNAVAIL;
213                 return;
214
215         case PROG_MISMATCH:
216                 error->re_status = RPC_PROGVERSMISMATCH;
217                 return;
218
219         case PROC_UNAVAIL:
220                 error->re_status = RPC_PROCUNAVAIL;
221                 return;
222
223         case GARBAGE_ARGS:
224                 error->re_status = RPC_CANTDECODEARGS;
225                 return;
226
227         case SYSTEM_ERR:
228                 error->re_status = RPC_SYSTEMERROR;
229                 return;
230
231         case SUCCESS:
232                 error->re_status = RPC_SUCCESS;
233                 return;
234         }
235         /* something's wrong, but we don't know what ... */
236         error->re_status = RPC_FAILED;
237         error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
238         error->re_lb.s2 = (int32_t)acpt_stat;
239 }
240
241 static void 
242 rejected(
243         enum reject_stat rjct_stat,
244         struct rpc_err *error)
245 {
246
247         if ((int)rjct_stat == (int)RPC_VERSMISMATCH) {
248                 error->re_status = RPC_VERSMISMATCH;
249                 return;
250         }
251         if (rjct_stat == AUTH_ERROR) {
252                 error->re_status = RPC_AUTHERROR;
253                 return;
254         }
255
256         /* something's wrong, but we don't know what ... */
257         error->re_status = RPC_FAILED;
258         error->re_lb.s1 = (int32_t)MSG_DENIED;
259         error->re_lb.s2 = (int32_t)rjct_stat;
260 }
261
262 /*
263  * given a reply message, fills in the error
264  */
265 void
266 _seterr_reply(
267         struct rpc_msg *msg,
268         struct rpc_err *error)
269 {
270
271         /* optimized for normal, SUCCESSful case */
272         switch (msg->rm_reply.rp_stat) {
273
274         case MSG_ACCEPTED:
275                 if (msg->acpted_rply.ar_stat == SUCCESS) {
276                         error->re_status = RPC_SUCCESS;
277                         return;
278                 };
279                 accepted(msg->acpted_rply.ar_stat, error);
280                 break;
281
282         case MSG_DENIED:
283                 rejected(msg->rjcted_rply.rj_stat, error);
284                 break;
285
286         default:
287                 error->re_status = RPC_FAILED;
288                 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
289                 break;
290         }
291         switch (error->re_status) {
292
293         case RPC_VERSMISMATCH:
294                 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
295                 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
296                 break;
297
298         case RPC_AUTHERROR:
299                 error->re_why = msg->rjcted_rply.rj_why;
300                 break;
301
302         case RPC_PROGVERSMISMATCH:
303                 error->re_vers.low = msg->acpted_rply.ar_vers.low;
304                 error->re_vers.high = msg->acpted_rply.ar_vers.high;
305                 break;
306         }
307 }