8835f93c98a63356b0143f9677094b37b0362d12
[openafs.git] / src / rx / xdr_rx.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * xdr_rx.c.  XDR using RX. 
12  */
13
14 #include <afsconfig.h>
15 #ifdef  KERNEL
16 #include "afs/param.h"
17 #else
18 #include <afs/param.h>
19 #endif
20
21
22 #ifdef KERNEL
23 #include "afs/sysincludes.h"
24 #ifndef UKERNEL
25 #include "h/types.h"
26 #include "h/uio.h"
27 #ifdef  AFS_OSF_ENV
28 #include <net/net_globals.h>
29 #endif /* AFS_OSF_ENV */
30 #ifdef AFS_LINUX20_ENV
31 #include "h/socket.h"
32 #else
33 #include "rpc/types.h"
34 #endif
35 #ifdef  AFS_OSF_ENV
36 #undef kmem_alloc
37 #undef kmem_free
38 #undef mem_alloc
39 #undef mem_free
40 #undef register
41 #endif /* AFS_OSF_ENV */
42 #ifdef AFS_LINUX22_ENV
43 #ifndef quad_t
44 #define quad_t __quad_t
45 #define u_quad_t __u_quad_t
46 #endif
47 #endif
48 #include "rx/xdr.h"
49 #include "netinet/in.h"
50 #else /* !UKERNEL */
51 #include "rpc/types.h"
52 #include "rpc/xdr.h"
53 #endif /* !UKERNEL */
54 #include "rx/rx.h"
55
56 #else /* KERNEL */
57 #include <sys/types.h>
58 #include <stdio.h>
59 #ifndef AFS_NT40_ENV
60 #include <netinet/in.h>
61 #endif
62 #include "rx.h"
63 #include "xdr.h"
64 #endif /* KERNEL */
65
66 /*
67  * This section should really go in the param.*.h files.
68  */
69 #if defined(KERNEL)
70 /*
71  * kernel version needs to agree with <rpc/xdr.h>
72  * except on Linux which does XDR differently from everyone else
73  */
74 # if defined(AFS_LINUX20_ENV) && !defined(UKERNEL)
75 #  define AFS_XDRS_T void *
76 # else
77 #  define AFS_XDRS_T XDR *
78 # endif
79 # if defined(AFS_SUN57_ENV)
80 #  define AFS_RPC_INLINE_T rpc_inline_t
81 # elif defined(AFS_DUX40_ENV)
82 #  define AFS_RPC_INLINE_T int
83 # elif defined(AFS_LINUX20_ENV) && !defined(UKERNEL)
84 #  define AFS_RPC_INLINE_T afs_int32
85 # elif defined(AFS_LINUX20_ENV)
86 #  define AFS_RPC_INLINE_T int32_t *
87 # else
88 #  define AFS_RPC_INLINE_T long
89 # endif
90 #else /* KERNEL */
91 /*
92  * user version needs to agree with "xdr.h", i.e. <rx/xdr.h>
93  */
94 #  define AFS_XDRS_T void *
95 #  define AFS_RPC_INLINE_T afs_int32
96 #endif /* KERNEL */
97
98 /* Static prototypes */
99 #if (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV)
100 static bool_t xdrrx_getint64(AFS_XDRS_T axdrs, long *lp);
101 static bool_t xdrrx_putint64(AFS_XDRS_T axdrs, long *lp);
102 #endif /* (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV) */
103
104 static bool_t xdrrx_getint32(AFS_XDRS_T axdrs, afs_int32 * lp);
105 static bool_t xdrrx_putint32(AFS_XDRS_T axdrs, afs_int32 * lp);
106 static bool_t xdrrx_getbytes(AFS_XDRS_T axdrs, caddr_t addr,
107                              u_int len);
108 static bool_t xdrrx_putbytes(AFS_XDRS_T axdrs, caddr_t addr,
109                              u_int len);
110 static AFS_RPC_INLINE_T *xdrrx_inline(AFS_XDRS_T axdrs, u_int len);
111
112
113 /*
114  * Ops vector for stdio type XDR
115  */
116 static struct xdr_ops xdrrx_ops = {
117 #if defined(KERNEL) && ((defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV))
118     .x_getint64 = xdrrx_getint64,
119     .x_putint64 = xdrrx_putint64,
120 #endif /* defined(KERNEL) && ((defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV)) */
121 #if defined(UKERNEL)
122     .x_getlong = xdrrx_getint32,
123     .x_putlong = xdrrx_putint32,
124 #elif !(defined(KERNEL) && defined(AFS_SUN57_ENV))
125     .x_getint32 = xdrrx_getint32,       /* deserialize an afs_int32 */
126     .x_putint32 = xdrrx_putint32,       /* serialize an afs_int32 */
127 #endif
128     .x_getbytes = xdrrx_getbytes,       /* deserialize counted bytes */
129     .x_putbytes = xdrrx_putbytes,       /* serialize counted bytes */
130     .x_getpostn = NULL,         /* get offset in the stream: not supported. */
131     .x_setpostn = NULL,         /* set offset in the stream: not supported. */
132     .x_inline = xdrrx_inline,           /* prime stream for inline macros */
133     .x_destroy = NULL,                  /* destroy stream */
134 #if defined(KERNEL) && defined(AFS_SUN57_ENV)
135     .x_control = NULL,
136     .x_getint32 = xdrrx_getint32,       /* deserialize an afs_int32 */
137     .x_putint32 = xdrrx_putint32,       /* serialize an afs_int32 */
138 #endif
139 };
140
141 /*
142  * Initialize an rx xdr handle, for a given rx call.  op must be XDR_ENCODE or XDR_DECODE.
143  * Call must have been returned by rx_MakeCall or rx_GetCall.
144  */
145 void
146 xdrrx_create(XDR * xdrs, struct rx_call *call,
147              enum xdr_op op)
148 {
149     xdrs->x_op = op;
150     xdrs->x_ops = &xdrrx_ops;
151     xdrs->x_private = (caddr_t) call;
152 }
153
154 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
155 #define STACK_TO_PIN    2*PAGESIZE      /* 8K */
156 int rx_pin_failed = 0;
157 #endif
158
159 #if (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV)
160 static bool_t
161 xdrrx_getint64(AFS_XDRS_T axdrs, long *lp)
162 {
163     XDR * xdrs = (XDR *)axdrs;
164     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
165     afs_int32 i;
166
167     if (rx_Read32(call, &i) == sizeof(i)) {
168         *lp = ntohl(i);
169         return TRUE;
170     }
171     return FALSE;
172 }
173
174 static bool_t
175 xdrrx_putint64(AFS_XDRS_T axdrs, long *lp)
176 {
177     XDR * xdrs = (XDR *)axdrs;
178     afs_int32 code, i = htonl(*lp);
179     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
180
181     code = (rx_Write32(call, &i) == sizeof(i));
182     return code;
183 }
184 #endif /* (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV) */
185
186 static bool_t
187 xdrrx_getint32(AFS_XDRS_T axdrs, afs_int32 * lp)
188 {
189     afs_int32 l;
190     XDR * xdrs = (XDR *)axdrs;
191     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
192 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
193     char *saddr = (char *)&l;
194     saddr -= STACK_TO_PIN;
195     /*
196      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
197      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
198      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
199      * 2K stack we could try to bring the next few stack pages in here before we call the rx
200      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
201      * out between here and calling splnet. So we now pin (and unpin) them instead to
202      * guarantee that they remain there.
203      */
204     if (pin(saddr, STACK_TO_PIN)) {
205         /* XXX There's little we can do by continue XXX */
206         saddr = NULL;
207         rx_pin_failed++;
208     }
209 #endif
210     if (rx_Read32(call, &l) == sizeof(l)) {
211         *lp = ntohl(l);
212 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
213         if (saddr)
214             unpin(saddr, STACK_TO_PIN);
215 #endif
216         return TRUE;
217     }
218 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
219     if (saddr)
220         unpin(saddr, STACK_TO_PIN);
221 #endif
222     return FALSE;
223 }
224
225 static bool_t
226 xdrrx_putint32(AFS_XDRS_T axdrs, afs_int32 * lp)
227 {
228     afs_int32 code, l = htonl(*lp);
229     XDR * xdrs = (XDR *)axdrs;
230     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
231 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
232     char *saddr = (char *)&code;
233     saddr -= STACK_TO_PIN;
234     /*
235      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
236      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
237      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
238      * 2K stack we could try to bring the next few stack pages in here before we call the rx
239      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
240      * out between here and calling splnet. So we now pin (and unpin) them instead to
241      * guarantee that they remain there.
242      */
243     if (pin(saddr, STACK_TO_PIN)) {
244         saddr = NULL;
245         rx_pin_failed++;
246     }
247 #endif
248     code = (rx_Write32(call, &l) == sizeof(l));
249 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
250     if (saddr)
251         unpin(saddr, STACK_TO_PIN);
252 #endif
253     return code;
254 }
255
256 static bool_t
257 xdrrx_getbytes(AFS_XDRS_T axdrs, caddr_t addr, u_int len)
258 {
259     afs_int32 code;
260     XDR * xdrs = (XDR *)axdrs;
261     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
262 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
263     char *saddr = (char *)&code;
264     saddr -= STACK_TO_PIN;
265     /*
266      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
267      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
268      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
269      * 2K stack we could try to bring the next few stack pages in here before we call the rx
270      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
271      * out between here and calling splnet. So we now pin (and unpin) them instead to
272      * guarantee that they remain there.
273      */
274     if (pin(saddr, STACK_TO_PIN)) {
275         /* XXX There's little we can do by continue XXX */
276         saddr = NULL;
277         rx_pin_failed++;
278     }
279 #endif
280     code = (rx_Read(call, addr, len) == len);
281 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
282     if (saddr)
283         unpin(saddr, STACK_TO_PIN);
284 #endif
285     return code;
286 }
287
288 static bool_t
289 xdrrx_putbytes(AFS_XDRS_T axdrs, caddr_t addr, u_int len)
290 {
291     afs_int32 code;
292     XDR * xdrs = (XDR *)axdrs;
293     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
294 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
295     char *saddr = (char *)&code;
296     saddr -= STACK_TO_PIN;
297     /*
298      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
299      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
300      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
301      * 2K stack we could try to bring the next few stack pages in here before we call the rx
302      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
303      * out between here and calling splnet. So we now pin (and unpin) them instead to
304      * guarantee that they remain there.
305      */
306     if (pin(saddr, STACK_TO_PIN)) {
307         /* XXX There's little we can do by continue XXX */
308         saddr = NULL;
309         rx_pin_failed++;
310     }
311 #endif
312     code = (rx_Write(call, addr, len) == len);
313 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
314     if (saddr)
315         unpin(saddr, STACK_TO_PIN);
316 #endif
317     return code;
318 }
319
320 #ifdef undef                    /* not used */
321 static u_int
322 xdrrx_getpos(XDR * xdrs)
323 {
324     /* Not supported.  What error code should we return? (It doesn't matter:  it will never be called, anyway!) */
325     return -1;
326 }
327
328 static bool_t
329 xdrrx_setpos(XDR * xdrs, u_int pos)
330 {
331     /* Not supported */
332     return FALSE;
333 }
334 #endif
335
336 static AFS_RPC_INLINE_T *
337 xdrrx_inline(AFS_XDRS_T axdrs, u_int len)
338 {
339     /* I don't know what this routine is supposed to do, but the stdio module returns null, so we will, too */
340     return (0);
341 }