source: rtems-libbsd/services/librpc/src/rpc/rpc_prot.c @ cd450d2

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since cd450d2 was cd450d2, checked in by Joel Sherrill <joel.sherrill@…>, on 08/06/12 at 00:10:35

librpc: now compiles

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