xref: /openbsd-src/lib/libc/rpc/xdr.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: xdr.3,v 1.13 2000/04/18 03:01:34 aaron Exp $
2.\" Mostly converted to mandoc by Theo de Raadt, Tue Feb 24 04:04:46 MST 1998
3.\"
4.Dd February 16, 1988
5.Dt XDR 3
6.Os
7.Sh NAME
8.Nm xdr_array ,
9.Nm xdr_bool ,
10.Nm xdr_bytes ,
11.Nm xdr_char ,
12.Nm xdr_destroy ,
13.Nm xdr_double ,
14.Nm xdr_enum ,
15.Nm xdr_float ,
16.Nm xdr_free ,
17.Nm xdr_getpos ,
18.Nm xdr_inline ,
19.Nm xdr_int ,
20.Nm xdr_long ,
21.Nm xdrmem_create ,
22.Nm xdr_opaque ,
23.Nm xdr_pointer ,
24.Nm xdrrec_create ,
25.Nm xdrrec_endofrecord ,
26.Nm xdrrec_eof ,
27.Nm xdrrec_skiprecord ,
28.Nm xdr_reference ,
29.Nm xdr_setpos ,
30.Nm xdr_short ,
31.Nm xdrstdio_create ,
32.Nm xdr_string ,
33.Nm xdr_u_char ,
34.Nm xdr_u_int ,
35.Nm xdr_u_long ,
36.Nm xdr_u_short ,
37.Nm xdr_union ,
38.Nm xdr_vector ,
39.Nm xdr_void ,
40.Nm xdr_wrapstring
41.Nd library routines for external data representation
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <rpc/xdr.h>
45.Ft int
46.Fn xdr_array "XDR *xdrs" "char **arrp" "u_int *sizep" "u_int maxsize" "u_int elsize" "xdrproc_t elproc"
47.Ft int
48.Fn xdr_bool "XDR *xdrs" "bool_t *bp"
49.Ft int
50.Fn xdr_bytes "XDR *xdrs" "char **sp" "u_int *sizep" "u_int maxsize"
51.Ft int
52.Fn xdr_char "XDR *xdrs" "char *cp"
53.Ft void
54.Fn xdr_destroy "XDR *xdrs"
55.Ft int
56.Fn xdr_double "XDR *xdrs" "double *dp"
57.Ft int
58.Fn xdr_enum "XDR *xdrs" "enum_t *ep"
59.Ft int
60.Fn xdr_float "XDR *xdrs" "float *fp"
61.Ft void
62.Fn xdr_free "xdrproc_t proc" "char *objp"
63.Ft u_int
64.Fn xdr_getpos "XDR *xdrs"
65.Ft long *
66.Fn xdr_inline "XDR *xdrs" "int len"
67.Ft int
68.Fn xdr_int "XDR *xdrs" "int *ip"
69.Ft int
70.Fn xdr_long "XDR *xdrs" "long *lp"
71.Ft void
72.Fn xdrmem_create "XDR *xdrs" "char *addr" "u_int size" "enum xdr_op op"
73.Ft int
74.Fn xdr_opaque "XDR *xdrs" "char *cp" "u_int cnt"
75.Ft int
76.Fn xdr_pointer "XDR *xdrs" "char **objpp" "u_int objsize" "xdrproc_t xdrobj"
77.Ft void
78.Fn xdrrec_create "XDR *xdrs" "u_int sendsize" "u_int recvsize" "char *handle" "int (*readit)()" "int (*writeit)()"
79.Ft int
80.Fn xdrrec_endofrecord "XDR *xdrs" "int sendnow"
81.Ft int
82.Fn xdrrec_eof "XDR *xdrs" "int empty"
83.Ft int
84.Fn xdrrec_skiprecord "XDR *xdrs"
85.Ft int
86.Fn xdr_reference "XDR *xdrs" "char **pp" "u_int size" "xdrproc_t proc"
87.Ft int
88.Fn xdr_setpos "XDR *xdrs" "u_int pos"
89.Ft int
90.Fn xdr_short "XDR *xdrs" "short *sp"
91.Ft void
92.Fn xdrstdio_create "XDR *xdrs" "FILE *file" "enum xdr_op op"
93.Ft int
94.Fn xdr_string "XDR *xdrs" "char **sp" "u_int maxsize"
95.Ft int
96.Fn xdr_u_char "XDR *xdrs" "unsigned char *ucp"
97.Ft int
98.Fn xdr_u_int "XDR *xdrs" "unsigned int *up"
99.Ft int
100.Fn xdr_u_long "XDR *xdrs" "unsigned long *ulp"
101.Ft int
102.Fn xdr_u_short "XDR *xdrs" "unsigned short *usp"
103.Ft int
104.Fn xdr_union "XDR *xdrs" "int *dscmp" "char *unp" "struct xdr_discrim *choices" "bool_t (*defaultarm)()"
105.Ft int
106.Fn xdr_vector "XDR *xdrs" "char *arrp" "u_int size" "u_int elsize" "xdrproc_t elproc"
107.Ft int
108.Fn xdr_void "void"
109.Ft int
110.Fn xdr_wrapstring "XDR *xdrs" "char **sp"
111.Sh DESCRIPTION
112These routines allow C programmers to describe
113arbitrary data structures in a machine-independent fashion.
114Data for remote procedure calls are transmitted using these
115routines.
116.Pp
117.Fn xdr_array
118is a filter primitive that translates between variable-length arrays
119and their corresponding external representations.
120The parameter
121.Fa arrp
122is the address of the pointer to the array, while
123.Fa sizep
124is the address of the element count of the array;
125this element count cannot exceed
126.Fa maxsize .
127The parameter
128.Fa elsize
129is the size of each of the array's elements, and
130.Fa elproc
131is an
132.Tn XDR
133filter that translates between
134the array elements' C form, and their external
135representation.
136This routine returns one if it succeeds, zero otherwise.
137.Pp
138.Fn xdr_bool
139is a filter primitive that translates between booleans (C integers)
140and their external representations.
141When encoding data, this
142filter produces values of either one or zero.
143This routine returns one if it succeeds, zero otherwise.
144.Pp
145.Fn xdr_bytes
146is a filter primitive that translates between counted byte
147strings and their external representations.
148The parameter
149.Fa sp
150is the address of the string pointer.
151The length of the string is located at address
152.Fa sizep ;
153strings cannot be longer than
154.Fa maxsize .
155This routine returns one if it succeeds, zero otherwise.
156.Pp
157.Fn xdr_char
158is a filter primitive that translates between C characters
159and their external representations.
160This routine returns one if it succeeds, zero otherwise.
161Note: encoded characters are not packed, and occupy 4 bytes each.
162For arrays of characters, it is worthwhile to consider
163.Fn xdr_bytes ,
164.Fn xdr_opaque ,
165or
166.Fn xdr_string .
167.Pp
168.Fn xdr_destroy
169is a macro that invokes the destroy routine associated with the
170.Tn XDR
171stream
172.Fa xdrs .
173Destruction usually involves freeing private data structures
174associated with the stream.
175Using
176.Fa xdrs
177after invoking
178.Fn xdr_destroy
179is undefined.
180.Pp
181.Fn xdr_double
182is a filter primitive that translates between C
183.Li double
184precision numbers and their external representations.
185This routine returns one if it succeeds, zero otherwise.
186.Pp
187.Fn xdr_enum
188is a filter primitive that translates between the C
189.Li enum
190type (actually an integer) and its external representations.
191This routine returns one if it succeeds, zero otherwise.
192.Pp
193.Fn xdr_float
194is a filter primitive that translates between the C
195.Li float
196type and its external representations.
197This routine returns one if it succeeds, zero otherwise.
198.Pp
199.Fn xdr_free
200is a generic freeing routine.
201The first argument is the
202.Tn XDR
203routine for the object being freed.
204The second argument
205is a pointer to the object itself.
206Note: the pointer passed to this routine is
207.Fa not
208freed, but what it points to is freed (recursively).
209.Pp
210.Fn xdr_getpos
211is a macro that invokes the get-position routine associated with the
212.Tn XDR
213stream
214.Fa xdrs .
215The routine returns an unsigned integer,
216which indicates the position of the
217.Tn XDR
218byte stream.
219A desirable feature of
220.Tn XDR
221streams is that simple arithmetic works with this number,
222although the
223.Tn XDR
224stream instances need not guarantee this.
225.Pp
226.Fn xdr_inline
227is a macro that invokes the in-line routine associated with the
228.Tn XDR
229stream
230.Fa xdrs .
231The routine returns a pointer
232to a contiguous piece of the stream's buffer;
233.Fa len
234is the byte length of the desired buffer.
235Note: pointer is cast to
236.Li long * .
237Warning:
238.Fn xdr_inline
239may return
240.Dv NULL
241if it cannot allocate a contiguous piece of a buffer.
242Therefore the behavior may vary among stream instances;
243it exists for the sake of efficiency.
244.Pp
245.Fn xdr_int
246is a filter primitive that translates between C integers
247and their external representations.
248This routine returns one if it succeeds, zero otherwise.
249.Pp
250.Fn xdr_long
251is a filter primitive that translates between C
252.Li long
253integers and their external representations.
254This routine returns one if it succeeds, zero otherwise.
255.Pp
256.Fn xdrmem_create
257is a routine which initializes the
258.Tn XDR
259stream object pointed to by
260.Fa xdrs .
261The stream's data is written to, or read from,
262a chunk of memory at location
263.Fa addr
264whose length is no more than
265.Fa size
266bytes long.
267The
268.Fa op
269determines the direction of the
270.Tn XDR
271stream
272(either
273.Dv XDR_ENCODE ,
274.Dv XDR_DECODE ,
275or
276.Dv XDR_FREE Ns ).
277.Pp
278.Fn xdr_opaque
279is a filter primitive that translates between fixed size opaque
280data
281and its external representation.
282The parameter
283.Fa cp
284is the address of the opaque object, and
285.Fa cnt
286is its size in bytes.
287This routine returns one if it succeeds, zero otherwise.
288.Pp
289.Fn xdr_pointer
290is like
291.Fn xdr_reference
292execpt that it serializes
293.Dv NULL
294pointers, whereas
295.Fn xdr_reference
296does not.
297Thus,
298.Fn xdr_pointer
299can represent
300recursive data structures, such as binary trees or
301linked lists.
302.Pp
303.Fn xdrrec_create
304is a routine which initializes the
305.Tn XDR
306stream object pointed to by
307.Fa xdrs .
308The stream's data is written to a buffer of size
309.Fa sendsize ;
310a value of zero indicates the system should use a suitable
311default.
312The stream's data is read from a buffer of size
313.Fa recvsize ;
314it too can be set to a suitable default by passing a zero
315value.
316When a stream's output buffer is full,
317.Fn (*writeit)
318is called.
319Similarly, when a stream's input buffer is empty,
320.Fn (*readit)
321is called.
322The behavior of these two routines is similar to the system calls
323.Fn read
324and
325.Fn write ,
326except that
327.Fa handle
328is passed to the former routines as the first parameter.
329Note: the
330.Tn XDR
331stream's
332.Fa op
333field must be set by the caller.
334Warning: this
335.Tn XDR
336stream implements an intermediate record stream.
337Therefore there are additional bytes in the stream
338to provide record boundary information.
339.Pp
340.Fn xdrrec_endofrecord
341is a routine which can be invoked only on
342streams created by
343.Fn xdrrec_create .
344The data in the output buffer is marked as a completed record,
345and the output buffer is optionally written out if
346.Fa sendnow
347is non-zero.
348This routine returns one if it succeeds, zero otherwise.
349.Pp
350.Fn xdrrec_eof
351is a routine which can be invoked only on
352streams created by
353.Fn xdrrec_create .
354After consuming the rest of the current record in the stream,
355this routine returns one if the stream has no more input,
356zero otherwise.
357.Pp
358.Fn xdrrec_skiprecord
359is a routine which can be invoked only on
360streams created by
361.Fn xdrrec_create .
362It tells the
363.Tn XDR
364implementation that the rest of the current record
365in the stream's input buffer should be discarded.
366This routine returns one if it succeeds, zero otherwise.
367.Pp
368.Fn xdr_reference
369is a primitive that provides pointer chasing within structures.
370The parameter
371.Fa pp
372is the address of the pointer;
373.Fa size
374is the size of the structure that
375.Fa *pp
376points to; and
377.Fa proc
378is an
379.Tn XDR
380procedure that filters the structure
381between its C form and its external representation.
382This routine returns one if it succeeds, zero otherwise.
383Warning: this routine does not understand
384.Dv NULL
385pointers.
386Use
387.Fn xdr_pointer
388instead.
389.Pp
390.Fn xdr_setpos
391is a macro that invokes the set position routine associated with the
392.Tn XDR
393stream
394.Fa xdrs .
395The parameter
396.Fa pos
397is a position value obtained from
398.Fn xdr_getpos .
399This routine returns one if the
400.Tn XDR
401stream could be repositioned,
402and zero otherwise.
403Warning: it is difficult to reposition some types of
404.Tn XDR
405streams, so this routine may fail with one
406type of stream and succeed with another.
407.Pp
408.Fn xdr_short
409is a filter primitive that translates between C
410.Li short
411integers and their external representations.
412This routine returns one if it succeeds, zero otherwise.
413.Pp
414.Fn xdrstdio_create
415is a routine which initializes the
416.Tn XDR
417stream object pointed to by
418.Fa xdrs .
419The
420.Tn XDR
421stream data is written to, or read from, the Standard I/O
422stream
423.Fa file .
424The parameter
425.Fa op
426determines the direction of the
427.Tn XDR
428stream (either
429.Dv XDR_ENCODE ,
430.Dv XDR_DECODE ,
431or
432.Dv XDR_FREE Ns ).
433Warning: the destroy routine associated with such
434.Tn XDR
435streams calls
436.Fn fflush
437on the
438.Fa file
439stream, but never
440.Fn fclose .
441.Pp
442.Fn xdr_string
443is a filter primitive that translates between C strings and their
444corresponding external representations.
445Strings cannot be longer than
446.Fa maxsize .
447Note:
448.Fa sp
449is the address of the string's pointer.
450This routine returns one if it succeeds, zero otherwise.
451.Pp
452.Fn xdr_u_char
453is a filter primitive that translates between
454.Li unsigned
455C characters and their external representations.
456This routine returns one if it succeeds, zero otherwise.
457.Pp
458.Fn xdr_u_int
459is a filter primitive that translates between C
460.Li unsigned
461integers and their external representations.
462This routine returns one if it succeeds, zero otherwise.
463.Pp
464.Fn xdr_u_long
465is a filter primitive that translates between C
466.Li unsigned long
467integers and their external representations.
468This routine returns one if it succeeds, zero otherwise.
469.Pp
470.Fn xdr_u_short
471is a filter primitive that translates between C
472.Li unsigned short
473integers and their external representations.
474This routine returns one if it succeeds, zero otherwise.
475.Pp
476.Fn xdr_union
477is a filter primitive that translates between a discriminated C
478.Li union
479and its corresponding external representation.
480It first translates the discriminant of the union located at
481.Fa dscmp .
482This discriminant is always an
483.Li enum_t .
484Next the union located at
485.Fa unp
486is translated.
487The parameter
488.Fa choices
489is a pointer to an array of
490.Li struct xdr_discrim
491structures.
492Each structure contains an ordered pair of
493.Ft [ value , proc ].
494If the union's discriminant is equal to the associated
495.Fa value ,
496then the
497.Fa proc
498is called to translate the union.
499The end of the
500.Li struct xdr_discrim
501structure array is denoted by a routine of value
502.Dv NULL .
503If the discriminant is not found in the
504.Fa choices
505array, then the
506.Fn (*defaultarm)
507procedure is called (if it is not
508.Dv NULL Ns ).
509Returns one if it succeeds, zero otherwise.
510.Pp
511.Fn xdr_vector
512is a filter primitive that translates between fixed-length arrays
513and their corresponding external representations.
514The parameter
515.Fa arrp
516is the address of the pointer to the array, while
517.Fa size
518is the element count of the array.
519The parameter
520.Fa elsize
521is the size of each of the array's elements, and
522.Fa elproc
523is an
524.Tn XDR
525filter that translates between the array elements' C form, and their
526external representation.
527This routine returns one if it succeeds, zero otherwise.
528.Pp
529.Fn xdr_void
530is a routine which always returns one.
531It may be passed to
532.Tn RPC
533routines that require a function parameter, but where nothing is to be done.
534.Pp
535.Fn xdr_wrapstring
536is a primitive that calls
537.Pa "xdr_string(xdrs, sp, MAXUN.UNSIGNED );"
538where
539.Tn MAXUN.UNSIGNED
540is the maximum value of an unsigned integer.
541.Fn xdr_wrapstring
542is handy because the
543.Tn RPC
544package passes a maximum of two
545.Tn XDR
546routines as parameters, and
547.Fn xdr_string ,
548one of the most frequently used primitives, requires three.
549Returns one if it succeeds, zero otherwise.
550.Sh SEE ALSO
551.Xr rpc 3
552.Rs
553.%T eXternal Data Representation Standard: Protocol Specification
554.Re
555.Rs
556.%T eXternal Data Representation: Sun Technical Notes
557.Re
558.Rs
559.%A "Sun Microsystems, Inc."
560.%T "XDR: External Data Representation Standard"
561.%R "RFC1014, USC-ISI"
562.Re
563