Correct ifdefs in rx.c for rx packet debugging
[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     xdrrx_getint64,
119     xdrrx_putint64,
120 #endif /* defined(KERNEL) && ((defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV)) */
121 #if !(defined(KERNEL) && defined(AFS_SUN57_ENV))
122     xdrrx_getint32,             /* deserialize an afs_int32 */
123     xdrrx_putint32,             /* serialize an afs_int32 */
124 #endif
125     xdrrx_getbytes,             /* deserialize counted bytes */
126     xdrrx_putbytes,             /* serialize counted bytes */
127     0,                          /* get offset in the stream: not supported. */
128     0,                          /* set offset in the stream: not supported. */
129     xdrrx_inline,               /* prime stream for inline macros */
130     0,                          /* destroy stream */
131 #if defined(KERNEL) && defined(AFS_SUN57_ENV)
132     0,
133     xdrrx_getint32,             /* deserialize an afs_int32 */
134     xdrrx_putint32,             /* serialize an afs_int32 */
135 #endif
136 };
137
138 /*
139  * Initialize an rx xdr handle, for a given rx call.  op must be XDR_ENCODE or XDR_DECODE.
140  * Call must have been returned by rx_MakeCall or rx_GetCall.
141  */
142 void
143 xdrrx_create(XDR * xdrs, struct rx_call *call,
144              enum xdr_op op)
145 {
146     xdrs->x_op = op;
147     xdrs->x_ops = &xdrrx_ops;
148     xdrs->x_private = (caddr_t) call;
149 }
150
151 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
152 #define STACK_TO_PIN    2*PAGESIZE      /* 8K */
153 int rx_pin_failed = 0;
154 #endif
155
156 #if (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV)
157 static bool_t
158 xdrrx_getint64(AFS_XDRS_T axdrs, long *lp)
159 {
160     XDR * xdrs = (XDR *)axdrs;
161     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
162     afs_int32 i;
163
164     if (rx_Read32(call, &i) == sizeof(i)) {
165         *lp = ntohl(i);
166         return TRUE;
167     }
168     return FALSE;
169 }
170
171 static bool_t
172 xdrrx_putint64(AFS_XDRS_T axdrs, long *lp)
173 {
174     XDR * xdrs = (XDR *)axdrs;
175     afs_int32 code, i = htonl(*lp);
176     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
177
178     code = (rx_Write32(call, &i) == sizeof(i));
179     return code;
180 }
181 #endif /* (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG != _MIPS_SZINT)) || defined(AFS_HPUX_64BIT_ENV) */
182
183 static bool_t
184 xdrrx_getint32(AFS_XDRS_T axdrs, afs_int32 * lp)
185 {
186     afs_int32 l;
187     XDR * xdrs = (XDR *)axdrs;
188     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
189 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
190     char *saddr = (char *)&l;
191     saddr -= STACK_TO_PIN;
192     /*
193      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
194      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
195      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
196      * 2K stack we could try to bring the next few stack pages in here before we call the rx
197      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
198      * out between here and calling splnet. So we now pin (and unpin) them instead to
199      * guarantee that they remain there.
200      */
201     if (pin(saddr, STACK_TO_PIN)) {
202         /* XXX There's little we can do by continue XXX */
203         saddr = NULL;
204         rx_pin_failed++;
205     }
206 #endif
207     if (rx_Read32(call, &l) == sizeof(l)) {
208         *lp = ntohl(l);
209 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
210         if (saddr)
211             unpin(saddr, STACK_TO_PIN);
212 #endif
213         return TRUE;
214     }
215 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
216     if (saddr)
217         unpin(saddr, STACK_TO_PIN);
218 #endif
219     return FALSE;
220 }
221
222 static bool_t
223 xdrrx_putint32(AFS_XDRS_T axdrs, afs_int32 * lp)
224 {
225     afs_int32 code, l = htonl(*lp);
226     XDR * xdrs = (XDR *)axdrs;
227     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
228 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
229     char *saddr = (char *)&code;
230     saddr -= STACK_TO_PIN;
231     /*
232      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
233      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
234      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
235      * 2K stack we could try to bring the next few stack pages in here before we call the rx
236      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
237      * out between here and calling splnet. So we now pin (and unpin) them instead to
238      * guarantee that they remain there.
239      */
240     if (pin(saddr, STACK_TO_PIN)) {
241         saddr = NULL;
242         rx_pin_failed++;
243     }
244 #endif
245     code = (rx_Write32(call, &l) == sizeof(l));
246 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
247     if (saddr)
248         unpin(saddr, STACK_TO_PIN);
249 #endif
250     return code;
251 }
252
253 static bool_t
254 xdrrx_getbytes(AFS_XDRS_T axdrs, caddr_t addr, u_int len)
255 {
256     afs_int32 code;
257     XDR * xdrs = (XDR *)axdrs;
258     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
259 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
260     char *saddr = (char *)&code;
261     saddr -= STACK_TO_PIN;
262     /*
263      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
264      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
265      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
266      * 2K stack we could try to bring the next few stack pages in here before we call the rx
267      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
268      * out between here and calling splnet. So we now pin (and unpin) them instead to
269      * guarantee that they remain there.
270      */
271     if (pin(saddr, STACK_TO_PIN)) {
272         /* XXX There's little we can do by continue XXX */
273         saddr = NULL;
274         rx_pin_failed++;
275     }
276 #endif
277     code = (rx_Read(call, addr, len) == len);
278 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
279     if (saddr)
280         unpin(saddr, STACK_TO_PIN);
281 #endif
282     return code;
283 }
284
285 static bool_t
286 xdrrx_putbytes(AFS_XDRS_T axdrs, caddr_t addr, u_int len)
287 {
288     afs_int32 code;
289     XDR * xdrs = (XDR *)axdrs;
290     struct rx_call *call = ((struct rx_call *)(xdrs)->x_private);
291 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
292     char *saddr = (char *)&code;
293     saddr -= STACK_TO_PIN;
294     /*
295      * Hack of hacks: Aix3.2 only guarantees that the next 2K of stack in pinned. Under 
296      * splnet (disables interrupts), which is set throughout rx, we can't swap in stack
297      * pages if we need so we panic. Since sometimes, under splnet, we'll use more than
298      * 2K stack we could try to bring the next few stack pages in here before we call the rx
299      * layer. Of course this doesn't guarantee that those stack pages won't be swapped
300      * out between here and calling splnet. So we now pin (and unpin) them instead to
301      * guarantee that they remain there.
302      */
303     if (pin(saddr, STACK_TO_PIN)) {
304         /* XXX There's little we can do by continue XXX */
305         saddr = NULL;
306         rx_pin_failed++;
307     }
308 #endif
309     code = (rx_Write(call, addr, len) == len);
310 #if     defined(KERNEL) && defined(AFS_AIX32_ENV)
311     if (saddr)
312         unpin(saddr, STACK_TO_PIN);
313 #endif
314     return code;
315 }
316
317 #ifdef undef                    /* not used */
318 static u_int
319 xdrrx_getpos(XDR * xdrs)
320 {
321     /* Not supported.  What error code should we return? (It doesn't matter:  it will never be called, anyway!) */
322     return -1;
323 }
324
325 static bool_t
326 xdrrx_setpos(XDR * xdrs, u_int pos)
327 {
328     /* Not supported */
329     return FALSE;
330 }
331 #endif
332
333 static AFS_RPC_INLINE_T *
334 xdrrx_inline(AFS_XDRS_T axdrs, u_int len)
335 {
336     /* I don't know what this routine is supposed to do, but the stdio module returns null, so we will, too */
337     return (0);
338 }