8916504a390d9fcea16c98b5abaa9f52fbc3f622
[platform/upstream/glibc.git] / sunrpc / rpc_cmsg.c
1 /* @(#)rpc_callmsg.c    2.1 88/07/29 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * rpc_callmsg.c
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  *
39  */
40
41 #include <string.h>
42 #include <sys/param.h>
43 #include <rpc/rpc.h>
44
45 /*
46  * XDR a call message
47  */
48 bool_t
49 xdr_callmsg (XDR *xdrs, struct rpc_msg *cmsg)
50 {
51   int32_t *buf;
52   struct opaque_auth *oa;
53
54   if (xdrs->x_op == XDR_ENCODE)
55     {
56       if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES)
57         {
58           return (FALSE);
59         }
60       if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES)
61         {
62           return (FALSE);
63         }
64       buf = XDR_INLINE (xdrs, 8 * BYTES_PER_XDR_UNIT
65                         + RNDUP (cmsg->rm_call.cb_cred.oa_length)
66                         + 2 * BYTES_PER_XDR_UNIT
67                         + RNDUP (cmsg->rm_call.cb_verf.oa_length));
68       if (buf != NULL)
69         {
70           IXDR_PUT_LONG (buf, cmsg->rm_xid);
71           IXDR_PUT_ENUM (buf, cmsg->rm_direction);
72           if (cmsg->rm_direction != CALL)
73             return FALSE;
74           IXDR_PUT_LONG (buf, cmsg->rm_call.cb_rpcvers);
75           if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION)
76             return FALSE;
77           IXDR_PUT_LONG (buf, cmsg->rm_call.cb_prog);
78           IXDR_PUT_LONG (buf, cmsg->rm_call.cb_vers);
79           IXDR_PUT_LONG (buf, cmsg->rm_call.cb_proc);
80           oa = &cmsg->rm_call.cb_cred;
81           IXDR_PUT_ENUM (buf, oa->oa_flavor);
82           IXDR_PUT_INT32 (buf, oa->oa_length);
83           if (oa->oa_length)
84             {
85               memcpy ((caddr_t) buf, oa->oa_base, oa->oa_length);
86               buf = (int32_t *) ((char *) buf + RNDUP (oa->oa_length));
87             }
88           oa = &cmsg->rm_call.cb_verf;
89           IXDR_PUT_ENUM (buf, oa->oa_flavor);
90           IXDR_PUT_INT32 (buf, oa->oa_length);
91           if (oa->oa_length)
92             {
93               memcpy ((caddr_t) buf, oa->oa_base, oa->oa_length);
94               /* no real need....
95                  buf = (long *) ((char *) buf + RNDUP(oa->oa_length));
96                */
97             }
98           return TRUE;
99         }
100     }
101   if (xdrs->x_op == XDR_DECODE)
102     {
103       buf = XDR_INLINE (xdrs, 8 * BYTES_PER_XDR_UNIT);
104       if (buf != NULL)
105         {
106           cmsg->rm_xid = IXDR_GET_LONG (buf);
107           cmsg->rm_direction = IXDR_GET_ENUM (buf, enum msg_type);
108           if (cmsg->rm_direction != CALL)
109             {
110               return FALSE;
111             }
112           cmsg->rm_call.cb_rpcvers = IXDR_GET_LONG (buf);
113           if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION)
114             {
115               return FALSE;
116             }
117           cmsg->rm_call.cb_prog = IXDR_GET_LONG (buf);
118           cmsg->rm_call.cb_vers = IXDR_GET_LONG (buf);
119           cmsg->rm_call.cb_proc = IXDR_GET_LONG (buf);
120           oa = &cmsg->rm_call.cb_cred;
121           oa->oa_flavor = IXDR_GET_ENUM (buf, enum_t);
122           oa->oa_length = IXDR_GET_INT32 (buf);
123           if (oa->oa_length)
124             {
125               if (oa->oa_length > MAX_AUTH_BYTES)
126                 return FALSE;
127               if (oa->oa_base == NULL)
128                 {
129                   oa->oa_base = (caddr_t)
130                     mem_alloc (oa->oa_length);
131                 }
132               buf = XDR_INLINE (xdrs, RNDUP (oa->oa_length));
133               if (buf == NULL)
134                 {
135                   if (xdr_opaque (xdrs, oa->oa_base,
136                                   oa->oa_length) == FALSE)
137                     return FALSE;
138                 }
139               else
140                 {
141                   memcpy (oa->oa_base, (caddr_t) buf, oa->oa_length);
142                   /* no real need....
143                      buf = (long *) ((char *) buf
144                      + RNDUP(oa->oa_length));
145                    */
146                 }
147             }
148           oa = &cmsg->rm_call.cb_verf;
149           buf = XDR_INLINE (xdrs, 2 * BYTES_PER_XDR_UNIT);
150           if (buf == NULL)
151             {
152               if (xdr_enum (xdrs, &oa->oa_flavor) == FALSE ||
153                   xdr_u_int (xdrs, &oa->oa_length) == FALSE)
154                 {
155                   return FALSE;
156                 }
157             }
158           else
159             {
160               oa->oa_flavor = IXDR_GET_ENUM (buf, enum_t);
161               oa->oa_length = IXDR_GET_INT32 (buf);
162             }
163           if (oa->oa_length)
164             {
165               if (oa->oa_length > MAX_AUTH_BYTES)
166                 return FALSE;
167               if (oa->oa_base == NULL)
168                 {
169                   oa->oa_base = (caddr_t)
170                     mem_alloc (oa->oa_length);
171                 }
172               buf = XDR_INLINE (xdrs, RNDUP (oa->oa_length));
173               if (buf == NULL)
174                 {
175                   if (xdr_opaque (xdrs, oa->oa_base,
176                                   oa->oa_length) == FALSE)
177                     return FALSE;
178                 }
179               else
180                 {
181                   memcpy (oa->oa_base, (caddr_t) buf, oa->oa_length);
182                   /* no real need...
183                      buf = (long *) ((char *) buf
184                      + RNDUP(oa->oa_length));
185                    */
186                 }
187             }
188           return TRUE;
189         }
190     }
191   if (
192        xdr_u_long (xdrs, &(cmsg->rm_xid)) &&
193        xdr_enum (xdrs, (enum_t *) & (cmsg->rm_direction)) &&
194        (cmsg->rm_direction == CALL) &&
195        xdr_u_long (xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
196        (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
197        xdr_u_long (xdrs, &(cmsg->rm_call.cb_prog)) &&
198        xdr_u_long (xdrs, &(cmsg->rm_call.cb_vers)) &&
199        xdr_u_long (xdrs, &(cmsg->rm_call.cb_proc)) &&
200        xdr_opaque_auth (xdrs, &(cmsg->rm_call.cb_cred)))
201     return xdr_opaque_auth (xdrs, &(cmsg->rm_call.cb_verf));
202   return FALSE;
203 }