home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-11-30 | 48.6 KB | 1,995 lines |
- .\" @(#)c3.xdr.spec 1.1 86/02/25 SMI; for 3.0 FCS
- .PL RIGHT
- .EQ
- delim $$
- .EN
- .TL
- External Data Representation
- .br
- Protocol Specification
- .bp
- .NH
- Introduction
- .LP
- This manual describes library routines that allow a C programmer to
- describe arbitrary data structures in a machine-independent fashion.
- The eXternal Data Representation (XDR) standard
- is the backbone of Sun's Remote Procedure Call package,
- in the sense that data for remote procedure calls
- is transmitted using the standard.
- XDR library routines should be used to transmit data
- that is accessed (read or written) by more than one type of machine.
- .LP
- This manual contains a description of XDR library routines,
- a guide to accessing currently available XDR streams,
- information on defining new streams and data types,
- and a formal definition of the XDR standard.
- XDR was designed to work across different languages,
- operating systems, and machine architectures.
- Most users (particularly RPC users)
- only need the information in sections 2 and 3 of this document.
- Programmers wishing to implement RPC and XDR on new machines
- will need the information in sections 4 through 6.
- Advanced topics, not necessary for all implementations,
- are covered in section 7.
- .LP
- On Sun systems,
- C programs that want to use XDR routines
- must include the file
- .LW <rpc/rpc.h> ,
- which contains all the necessary interfaces to the XDR system.
- Since the C library
- .LW libc.a
- contains all the XDR routines,
- compile as normal.
- .LS
- % \f(LBcc\0\fIprogram\fP.c\fL
- .Lf
- .NH 2
- Justification
- .LP
- Consider the following two programs,
- .LW writer :
- .LS
- #include <stdio.h>
- .sp.5
- main() /* writer.c */
- {
- long i;
- .sp.5
- for (i = 0; i < 8; i++) {
- if (fwrite((char *)&i, sizeof(i), 1, stdout) != 1) {
- fprintf(stderr, "failed!\en");
- exit(1);
- }
- }
- }
- .Lf
- and
- .LW reader :
- .LS
- #include <stdio.h>
- .sp.5
- main() /* reader.c */
- {
- long i, j;
- .sp.5
- for (j = 0; j < 8; j++) {
- if (fread((char *)&i, sizeof (i), 1, stdin) != 1) {
- fprintf(stderr, "failed!\en");
- exit(1);
- }
- printf("%ld ", i);
- }
- printf("\en");
- }
- .Lf
- The two programs appear to be portable, because
- (a) they pass
- .LW lint
- checking, and
- (b) they exhibit the same behavior when executed
- on two different hardware architectures, a Sun and a VAX.
- .LP
- Piping the output of the
- .LW writer
- program to the
- .LW reader
- program gives identical results on a Sun or a VAX.\(dd
- .FS \(dd
- VAX is a trademark of Digital Equipment Corporation.
- .FE
- .LS
- sun% writer | reader
- 0 1 2 3 4 5 6 7
- sun%
- .Lf
- .LS
- vax% writer | reader
- 0 1 2 3 4 5 6 7
- vax%
- .Lf
- With the advent of local area networks and Berkeley's 4.2 BSD
- .UX
- came the concept of ``network pipes'' \(em
- a process produces data on one machine,
- and a second process consumes data on another machine.
- A network pipe can be constructed with
- .LW writer
- and
- .LW reader .
- Here are the results if the first produces data on a Sun,
- and the second consumes data on a VAX.
- .LS
- sun% writer | rsh vax reader
- 0 16777216 33554432 50331648 67108864 83886080 100663296
- 117440512
- sun%
- .Lf
- Identical results can be obtained by executing
- .LW writer
- on the VAX and
- .LW reader
- on the Sun.
- These results occur because the byte ordering
- of long integers differs between the VAX and the Sun,
- even though word size is the same.
- Note that $16777216$ is $2 sup 24$ \(em
- when four bytes are reversed, the 1 winds up in the 24th bit.
- .LP
- Whenever data is shared by two or more machine types,
- there is a need for portable data.
- Programs can be made data-portable by replacing the
- .LW read()
- and
- .LW write()
- calls with calls to an XDR library routine
- .LW xdr_long() ,
- a filter that knows the standard representation
- of a long integer in its external form.
- Here are the revised versions of
- .LW writer :
- .LS
- #include <stdio.h>
- #include <rpc/rpc.h> /* xdr is a sub-library of rpc */
- .sp.5
- main() /* writer.c */
- {
- XDR xdrs;
- long i;
- .sp.5
- xdrstdio_create(&xdrs, stdout, XDR_ENCODE);
- for (i = 0; i < 8; i++) {
- if (!xdr_long(&xdrs, &i)) {
- fprintf(stderr, "failed!\en");
- exit(1);
- }
- }
- }
- .Lf
- and
- .LW reader :
- .LS
- #include <stdio.h>
- #include <rpc/rpc.h> /* xdr is a sub-library of rpc */
- .sp.5
- main() /* reader.c */
- {
- XDR xdrs;
- long i, j;
- .sp.5
- xdrstdio_create(&xdrs, stdin, XDR_DECODE);
- for (j = 0; j < 8; j++) {
- if (!xdr_long(&xdrs, &i)) {
- fprintf(stderr, "failed!\en");
- exit(1);
- }
- printf("%ld ", i);
- }
- printf("\en");
- }
- .Lf
- The new programs were executed on a Sun,
- on a VAX, and from a Sun to a VAX;
- the results are shown below.
- .LS
- sun% writer | reader
- 0 1 2 3 4 5 6 7
- sun%
- .Lf
- .LS
- vax% writer | reader
- 0 1 2 3 4 5 6 7
- vax%
- .Lf
- .LS
- sun% writer | rsh vax reader
- 0 1 2 3 4 5 6 7
- sun%
- .Lf
- Dealing with integers is just the tip of the portable-data iceberg.
- Arbitrary data structures present portability problems,
- particularly with respect to alignment and pointers.
- Alignment on word boundaries may cause the
- size of a structure to vary from machine to machine.
- Pointers are convenient to use,
- but have no meaning outside the machine where they are defined.
- .NH 2
- The XDR Library
- .LP
- The XDR library solves data portability problems.
- It allows you to write and read arbitrary C constructs
- in a consistent, specified, well-documented manner.
- Thus, it makes sense to use the library even when the data
- is not shared among machines on a network.
- .LP
- The XDR library has filter routines for
- strings (null-terminated arrays of bytes),
- structures, unions, and arrays, to name a few.
- Using more primitive routines,
- you can write your own specific XDR routines
- to describe arbitrary data structures,
- including elements of arrays, arms of unions,
- or objects pointed at from other structures.
- The structures themselves may contain arrays of arbitrary elements,
- or pointers to other structures.
- .LP
- Let's examine the two programs more closely.
- There is a family of XDR stream creation routines
- in which each member treats the stream of bits differently.
- In our example, data is manipulated using standard I/O routines,
- so we use
- .LW xdrstdio_create() .
- The parameters to XDR stream creation routines
- vary according to their function.
- In our example,
- .LW xdrstdio_create()
- takes a pointer to an XDR structure that it initializes,
- a pointer to a
- .LW FILE
- that the input or output is performed on, and the operation.
- The operation may be
- .LW XDR_ENCODE
- for serializing in the
- .LW writer
- program, or
- .LW XDR_DECODE
- for deserializing in the
- .LW reader
- program.
- .LP
- Note: RPC clients never need to create XDR streams;
- the RPC system itself creates these streams,
- which are then passed to the clients.
- .LP
- The
- .LW xdr_long()
- primitive is characteristic of most XDR library
- primitives and all client XDR routines.
- First, the routine returns
- .LW FALSE
- (0) if it fails, and
- .LW TRUE
- (1) if it succeeds.
- Second, for each data type,
- .LW xxx ,
- there is an associated XDR routine of the form:
- .LS
- xdr_xxx(xdrs, fp)
- XDR *xdrs;
- xxx *fp;
- {
- }
- .Lf
- In our case,
- .LW xxx
- is long, and the corresponding XDR routine is
- a primitive,
- .LW xdr_long .
- The client could also define an arbitrary structure
- .LW xxx
- in which case the client would also supply the routine
- .LW xdr_xxx ,
- describing each field by calling XDR routines
- of the appropriate type.
- In all cases the first parameter,
- .LW xdrs
- can be treated as an opaque handle,
- and passed to the primitive routines.
- .LP
- XDR routines are direction independent;
- that is, the same routines are called to serialize or deserialize data.
- This feature is critical to software engineering of portable data.
- The idea is to call the same routine for either operation \(em
- this almost guarantees that serialized data can also be deserialized.
- One routine is used by both producer and consumer of networked data.
- This is implemented by always passing the address
- of an object rather than the object itself \(em
- only in the case of deserialization is the object modified.
- This feature is not shown in our trivial example,
- but its value becomes obvious when nontrivial data structures
- are passed among machines.
- If needed, you can obtain the direction of the XDR operation.
- See section 3.7 for details.
- .LP
- Let's look at a slightly more complicated example.
- Assume that a person's gross assets and liabilities
- are to be exchanged among processes.
- Also assume that these values are important enough
- to warrant their own data type:
- .LS
- struct gnumbers {
- long g_assets;
- long g_liabilities;
- };
- .Lf
- The corresponding XDR routine describing this structure would be:
- .LS
- bool_t /* TRUE is success, FALSE is failure */
- xdr_gnumbers(xdrs, gp)
- XDR *xdrs;
- struct gnumbers *gp;
- {
- if (xdr_long(xdrs, &gp->g_assets) &&
- xdr_long(xdrs, &gp->g_liabilities))
- return(TRUE);
- return(FALSE);
- }
- .Lf
- Note that the parameter
- .LW xdrs
- is never inspected or modified;
- it is only passed on to the subcomponent routines.
- It is imperative to inspect the return value of each XDR routine call,
- and to give up immediately and return
- .LW FALSE
- if the subroutine fails.
- .LP
- This example also shows that the type
- .LW bool_t
- is declared as an integer whose only values are
- .LW TRUE
- (1) and
- .LW FALSE
- (0). This document uses the following definitions:
- .LS
- #define bool_t int
- #define TRUE 1
- #define FALSE 0
- .sp.5
- #define enum_t int /* enum_t used for generic enums */
- .Lf
- .LP
- Keeping these conventions in mind,
- .LW xdr_gnumbers()
- can be rewritten as follows:
- .LS
- xdr_gnumbers(xdrs, gp)
- XDR *xdrs;
- struct gnumbers *gp;
- {
- return(xdr_long(xdrs, &gp->g_assets) &&
- xdr_long(xdrs, &gp->g_liabilities));
- }
- .Lf
- This document uses both coding styles.
- .bp
- .NH
- XDR Library Primitives
- .LP
- This section gives a synopsis of each XDR primitive.
- It starts with basic data types and moves on to constructed data types.
- Finally, XDR utilities are discussed.
- The interface to these primitives
- and utilities is defined in the include file
- .LW <rpc/xdr.h> ,
- automatically included by
- .LW <rpc/rpc.h> .
- .NH 2
- Number Filters
- .LP
- The XDR library provides primitives to translate between numbers
- and their corresponding external representations.
- Primitives cover the set of numbers in:
- .EQ
- [signed, unsigned] * [short, int, long]
- .EN
- Specifically, the six primitives are:
- .LS
- bool_t xdr_int(xdrs, ip)
- XDR *xdrs;
- int *ip;
- .sp.5
- bool_t xdr_u_int(xdrs, up)
- XDR *xdrs;
- unsigned *up;
- .sp.5
- bool_t xdr_long(xdrs, lip)
- XDR *xdrs;
- long *lip;
- .sp.5
- bool_t xdr_u_long(xdrs, lup)
- XDR *xdrs;
- u_long *lup;
- .sp.5
- bool_t xdr_short(xdrs, sip)
- XDR *xdrs;
- short *sip;
- .sp.5
- bool_t xdr_u_short(xdrs, sup)
- XDR *xdrs;
- u_short *sup;
- .Lf
- The first parameter,
- .LW xdrs ,
- is an XDR stream handle.
- The second parameter is the address of the number
- that provides data to the stream or receives data from it.
- All routines return
- .LW TRUE
- if they complete successfully, and
- .LW FALSE
- otherwise.
- .NH 2
- Floating Point Filters
- .LP
- The XDR library also provides primitive routines
- for C's floating point types:
- .LS
- bool_t xdr_float(xdrs, fp)
- XDR *xdrs;
- float *fp;
- .sp.5
- bool_t xdr_double(xdrs, dp)
- XDR *xdrs;
- double *dp;
- .Lf
- The first parameter,
- .LW xdrs
- is an XDR stream handle.
- The second parameter is the address
- of the floating point number that provides data to the stream
- or receives data from it.
- All routines return
- .LW TRUE
- if they complete successfully, and
- .LW FALSE
- otherwise.
- .LP
- Note: Since the numbers are represented in IEEE floating point,
- routines may fail when decoding a valid IEEE representation
- into a machine-specific representation, or vice-versa.
- .NH 2
- Enumeration Filters
- .LP
- The XDR library provides a primitive for generic enumerations.
- The primitive assumes that a C
- .LW enum
- has the same representation inside the machine as a C integer.
- The boolean type is an important instance of the
- .LW enum .
- The external representation of a boolean is always one
- .LW TRUE ) (
- or zero
- .LW FALSE ). (
- .LS
- #define bool_t int
- #define FALSE 0
- #define TRUE 1
- .sp.5
- #define enum_t int
- .sp.5
- bool_t xdr_enum(xdrs, ep)
- XDR *xdrs;
- enum_t *ep;
- .sp.5
- bool_t xdr_bool(xdrs, bp)
- XDR *xdrs;
- bool_t *bp;
- .Lf
- The second parameters
- .LW ep
- and
- .LW bp
- are addresses of the associated type
- that provides data to, or receives data from, the stream
- .LW xdrs .
- The routines return
- .LW TRUE
- if they complete successfully, and
- .LW FALSE
- otherwise.
- .NH 2
- No Data
- .LP
- Occasionally, an XDR routine must be supplied to the RPC system,
- even when no data is passed or required.
- The library provides such a routine:
- .LS
- bool_t xdr_void(); /* always returns TRUE */
- .Lf
- .NH 2
- Constructed Data Type Filters
- .LP
- Constructed or compound data type primitives
- require more parameters and perform more complicated functions
- then the primitives discussed above.
- This section includes primitives for
- strings, arrays, unions, and pointers to structures.
- .LP
- Constructed data type primitives may use memory management.
- In many cases, memory is allocated when deserializing data with
- .LW XDR_DECODE .
- Therefore, the XDR package must provide means to deallocate memory.
- This is done by an XDR operation,
- .LW XDR_FREE .
- To review, the three XDR directional operations are
- .LW XDR_ENCODE ,
- .LW XDR_DECODE ,
- and
- .LW XDR_FREE .
- .NH 3
- Strings
- .LP
- In C, a string is defined as a sequence of bytes
- terminated by a null byte,
- which is not considered when calculating string length.
- However, when a string is passed or manipulated,
- a pointer to it is employed.
- Therefore, the XDR library defines a string to be a
- .LW "char *" ,
- and not a sequence of characters.
- The external representation of a string is drastically different
- from its internal representation.
- Externally, strings are represented as
- sequences of ASCII characters,
- while internally, they are represented with character pointers.
- Conversion between the two representations
- is accomplished with the routine
- .LW xdr_string() :
- .LS
- bool_t xdr_string(xdrs, sp, maxlength)
- XDR *xdrs;
- char **sp;
- u_int maxlength;
- .Lf
- The first parameter
- .LW xdrs
- is the XDR stream handle.
- The second parameter
- .LW sp
- is a pointer to a string (type
- .LW "char **" ).
- The third parameter
- .LW maxlength
- specifies the maximum number of bytes allowed during encoding or decoding;
- its value is usually specified by a protocol.
- For example, a protocol specification may say
- that a file name may be no longer than 255 characters.
- The routine returns
- .LW FALSE
- if the number of characters exceeds
- .LW maxlength ,
- and
- .LW TRUE
- if it doesn't.
- .LP
- The behavior of
- .LW xdr_string()
- is similar to the behavior of other routines
- discussed in this section. The direction
- .LW XDR_ENCODE
- is easiest to understand. The parameter
- .LW sp
- points to a string of a certain length;
- if it does not exceed
- .LW maxlength ,
- the bytes are serialized.
- .LP
- The effect of deserializing a string is subtle.
- First the length of the incoming string is determined;
- it must not exceed
- .LW maxlength .
- Next
- .LW sp
- is dereferenced; if the the value is
- .LW NULL ,
- then a string of the appropriate length is allocated and
- .LW *sp
- is set to this string.
- If the original value of
- .LW *sp
- is non-null, then the XDR package assumes
- that a target area has been allocated,
- which can hold strings no longer than
- .LW maxlength .
- In either case, the string is decoded into the target area.
- The routine then appends a null character to the string.
- .LP
- In the
- .LW XDR_FREE
- operation, the string is obtained by dereferencing
- .LW sp .
- If the string is not
- .LW NULL ,
- it is freed and
- .LW *sp
- is set to
- .LW NULL .
- In this operation,
- .LW xdr_string
- ignores the
- .LW maxlength
- parameter.
- .NH 3
- Byte Arrays
- .LP
- Often variable-length arrays of bytes are preferable to strings.
- Byte arrays differ from strings in the following three ways:
- 1) the length of the array (the byte count) is explicitly
- located in an unsigned integer,
- 2) the byte sequence is not terminated by a null character, and
- 3) the external representation of the bytes is the same as their
- internal representation.
- The primitive
- .LW xdr_bytes()
- converts between the internal and external
- representations of byte arrays:
- .LS
- bool_t xdr_bytes(xdrs, bpp, lp, maxlength)
- XDR *xdrs;
- char **bpp;
- u_int *lp;
- u_int maxlength;
- .Lf
- The usage of the first, second and fourth parameters
- are identical to the first, second and third parameters of
- .LW xdr_string() ,
- respectively.
- The length of the byte area is obtained by dereferencing
- .LW lp
- when serializing;
- .LW *lp
- is set to the byte length when deserializing.
- .NH 3
- Arrays
- .LP
- The XDR library package provides a primitive
- for handling arrays of arbitrary elements.
- The
- .LW xdr_bytes()
- routine treats a subset of generic arrays,
- in which the size of array elements is known to be 1,
- and the external description of each element is built-in.
- The generic array primitive,
- .LW xdr_array()
- requires parameters identical to those of
- .LW xdr_bytes()
- plus two more:
- the size of array elements,
- and an XDR routine to handle each of the elements.
- This routine is called to encode or decode
- each element of the array.
- .LS
- bool_t
- xdr_array(xdrs, ap, lp, maxlength, elementsiz, xdr_element)
- XDR *xdrs;
- char **ap;
- u_int *lp;
- u_int maxlength;
- u_int elementsiz;
- bool_t (*xdr_element)();
- .Lf
- The parameter
- .LW ap
- is the address of the pointer to the array.
- If
- .LW *ap
- is
- .LW NULL
- when the array is being deserialized,
- XDR allocates an array of the appropriate size and sets
- .LW *ap
- to that array.
- The element count of the array is obtained from
- .LW *lp
- when the array is serialized;
- .LW *lp
- is set to the array length when the array is deserialized.
- The parameter
- .LW maxlength
- is the maximum number of elements that the array is allowed to have;
- .LW elementsiz
- is the byte size of each element of the array
- (the C function
- .LW sizeof()
- can be used to obtain this value).
- The routine
- .LW xdr_element
- is called to serialize, deserialize, or free
- each element of the array.
- .NH 4
- Examples
- .LP
- Before defining more constructed data types,
- it is appropriate to present three examples.
- .LP
- .I "Example A"
- .LP
- A user on a networked machine can be identified by
- (a) the machine name, such as
- .LW krypton :
- see
- .I gethostname (3);
- (b) the user's UID: see
- .I geteuid (2);
- and (c) the group numbers to which the user belongs: see
- .I getgroups (2).
- A structure with this information and its associated XDR routine
- could be coded like this:
- .LS
- struct netuser {
- char *nu_machinename;
- int nu_uid;
- u_int nu_glen;
- int *nu_gids;
- };
- #define NLEN 255 /* machine names < 256 chars */
- #define NGRPS 20 /* user can't be in > 20 groups */
- .sp.5
- bool_t
- xdr_netuser(xdrs, nup)
- XDR *xdrs;
- struct netuser *nup;
- {
- return(xdr_string(xdrs, &nup->nu_machinename, NLEN) &&
- xdr_int(xdrs, &nup->nu_uid) &&
- xdr_array(xdrs, &nup->nu_gids, &nup->nu_glen, NGRPS,
- sizeof (int), xdr_int));
- }
- .Lf
- .LP
- .I "Example B"
- .LP
- A party of network users could be implemented
- as an array of
- .LW netuser
- structure.
- The declaration and its associated XDR routines
- are as follows:
- .LS
- struct party {
- u_int p_len;
- struct netuser *p_nusers;
- };
- #define PLEN 500 /* max number of users in a party */
- .sp.5
- bool_t
- xdr_party(xdrs, pp)
- XDR *xdrs;
- struct party *pp;
- {
- return(xdr_array(xdrs, &pp->p_nusers, &pp->p_len, PLEN,
- sizeof (struct netuser), xdr_netuser));
- }
- .Lf
- .LP
- .I "Example C"
- .LP
- The well-known parameters to
- .LW main() ,
- .LW argc
- and
- .LW argv
- can be combined into a structure.
- An array of these structures can make up a history of commands.
- The declarations and XDR routines might look like:
- .LS no
- struct cmd {
- u_int c_argc;
- char **c_argv;
- };
- #define ALEN 1000 /* args cannot be > 1000 chars */
- #define NARGC 100 /* commands cannot have > 100 args */
- .sp.5
- struct history {
- u_int h_len;
- struct cmd *h_cmds;
- };
- #define NCMDS 75 /* history is no more than 75 commands */
- .sp.5
- bool_t
- xdr_wrap_string(xdrs, sp)
- XDR *xdrs;
- char **sp;
- {
- return(xdr_string(xdrs, sp, ALEN));
- }
- .sp.5
- bool_t
- xdr_cmd(xdrs, cp)
- XDR *xdrs;
- struct cmd *cp;
- {
- return(xdr_array(xdrs, &cp->c_argv, &cp->c_argc, NARGC,
- sizeof (char *), xdr_wrap_string));
- }
- .sp.5
- bool_t
- xdr_history(xdrs, hp)
- XDR *xdrs;
- struct history *hp;
- {
- return(xdr_array(xdrs, &hp->h_cmds, &hp->h_len, NCMDS,
- sizeof (struct cmd), xdr_cmd));
- }
- .Lf
- The most confusing part of this example is that the routine
- .LW xdr_wrap_string()
- is needed to package the
- .LW xdr_string()
- routine, because the implementation of
- .LW xdr_array()
- only passes two parameters to the array element description routine;
- .LW xdr_wrap_string()
- supplies the third parameter to
- .LW xdr_string() .
- .LP
- By now the recursive nature of the XDR library should be obvious.
- Let's continue with more constructed data types.
- .NH 3
- Opaque Data
- .LP
- In some protocols, handles are passed from a server to client.
- The client passes the handle back to the server at some later time.
- Handles are never inspected by clients;
- they are obtained and submitted.
- That is to say, handles are opaque.
- The primitive
- .LW xdr_opaque()
- is used for describing fixed sized, opaque bytes.
- .LS
- bool_t xdr_opaque(xdrs, p, len)
- XDR *xdrs;
- char *p;
- u_int len;
- .Lf
- The parameter
- .LW p
- is the location of the bytes;
- .LW len
- is the number of bytes in the opaque object.
- By definition, the actual data
- contained in the opaque object are not machine portable.
- .NH 3
- Fixed Sized Arrays
- .LP
- The XDR library does not provide a primitive for fixed-length arrays
- (the primitive
- .LW xdr_array()
- is for varying-length arrays).
- Example A could be rewritten to use fixed-sized arrays
- in the following fashion:
- .LS no
- #define NLEN 255 /* machine names must be < 256 chars */
- #define NGRPS 20 /* user can't belong to > 20 groups */
- .sp.5
- struct netuser {
- char *nu_machinename;
- int nu_uid;
- int nu_gids[NGRPS];
- };
- .sp.5
- bool_t
- xdr_netuser(xdrs, nup)
- XDR *xdrs;
- struct netuser *nup;
- {
- int i;
- .sp.5
- if (!xdr_string(xdrs, &nup->nu_machinename, NLEN))
- return(FALSE);
- if (!xdr_int(xdrs, &nup->nu_uid))
- return(FALSE);
- for (i = 0; i < NGRPS; i++) {
- if (!xdr_int(xdrs, &nup->nu_gids[i]))
- return(FALSE);
- }
- return(TRUE);
- }
- .Lf
- .LP
- Exercise:
- Rewrite example A so that it uses varying-length arrays and so that the
- .LW netuser
- structure contains the actual
- .LW nu_gids
- array body as in the example above.
- .NH 3
- Discriminated Unions
- .LP
- The XDR library supports discriminated unions.
- A discriminated union is a C union and an
- .LW enum_t
- value that selects an ``arm'' of the union.
- .LS
- struct xdr_discrim {
- enum_t value;
- bool_t (*proc)();
- };
- .sp.5
- bool_t xdr_union(xdrs, dscmp, unp, arms, defaultarm)
- XDR *xdrs;
- enum_t *dscmp;
- char *unp;
- struct xdr_discrim *arms;
- bool_t (*defaultarm)(); /* may equal NULL */
- .Lf
- First the routine translates the discriminant of the union located at
- .LW *dscmp .
- The discriminant is always an
- .LW enum_t .
- Next the union located at
- .LW *unp
- is translated.
- The parameter
- .LW arms
- is a pointer to an array of
- .LW xdr_discrim
- structures.
- Each structure contains an order pair of
- .LW [value,proc] .
- If the union's discriminant is equal to the associated
- .LW value ,
- then the
- .LW proc
- is called to translate the union.
- The end of the
- .LW xdr_discrim
- structure array is denoted by a routine of value
- .LW NULL
- (0). If the discriminant is not found in the
- .LW arms
- array, then the
- .LW defaultarm
- procedure is called if it is non-null;
- otherwise the routine returns
- .LW FALSE .
- .LP
- .I "Example D"
- .LP
- Suppose the type of a union may be integer,
- character pointer (a string), or a
- .LW gnumbers
- structure.
- Also, assume the union and its current type
- are declared in a structure.
- The declaration is:
- .LS
- enum utype { INTEGER=1, STRING=2, GNUMBERS=3 };
- .sp.5
- struct u_tag {
- enum utype utype; /* the union's discriminant */
- union {
- int ival;
- char *pval;
- struct gnumbers gn;
- } uval;
- };
- .Lf
- The following constructs and XDR procedure (de)serialize
- the discriminated union:
- .LS
- struct xdr_discrim u_tag_arms[4] = {
- { INTEGER, xdr_int },
- { GNUMBERS, xdr_gnumbers }
- { STRING, xdr_wrap_string },
- { __dontcare__, NULL }
- /* always terminate arms with a NULL xdr_proc */
- }
- .sp.5
- bool_t
- xdr_u_tag(xdrs, utp)
- XDR *xdrs;
- struct u_tag *utp;
- {
- return(xdr_union(xdrs, &utp->utype, &utp->uval,
- u_tag_arms, NULL));
- }
- .Lf
- The routine
- .LW xdr_gnumbers()
- was presented in Section 2;
- .LW xdr_wrap_string()
- was presented in example C.
- The default arm parameter to
- .LW xdr_union()
- (the last parameter) is
- .LW NULL
- in this example. Therefore the value of the union's discriminant
- may legally take on only values listed in the
- .LW u_tag_arms
- array. This example also demonstrates that
- the elements of the arm's array do not need to be sorted.
- .LP
- It is worth pointing out that the values of the discriminant
- may be sparse, though in this example they are not.
- It is always good
- practice to assign explicitly integer values to each element of the
- discriminant's type.
- This practice both documents the external
- representation of the discriminant and guarantees that different
- C compilers emit identical discriminant values.
- .LP
- Exercise: Implement
- .LW xdr_union()
- using the other primitives in this section.
- .NH 3
- Pointers
- .LP
- In C it is often convenient to put pointers
- to another structure within a structure.
- The primitive
- .LW xdr_reference()
- makes it easy to serialize, deserialize, and free
- these referenced structures.
- .LS
- bool_t xdr_reference(xdrs, pp, size, proc)
- XDR *xdrs;
- char **pp;
- u_int ssize;
- bool_t (*proc)();
- .Lf
- .LP
- Parameter
- .LW pp
- is the address of
- the pointer to the structure;
- parameter
- .LW ssize
- is the size in bytes of the structure
- (use the C function
- .LW sizeof()
- to obtain this value); and
- .LW proc
- is the XDR routine that describes the structure.
- When decoding data, storage is allocated if
- .LW *pp
- is
- .LW NULL .
- .LP
- There is no need for a primitive
- .LW xdr_struct()
- to describe structures within structures,
- because pointers are always sufficient.
- .LP
- Exercise: Implement
- .LW xdr_reference()
- using
- .LW xdr_array() .
- Warning:
- .LW xdr_reference()
- and
- .LW xdr_array()
- are NOT interchangeable external representations of data.
- .LP
- .I "Example E"
- .LP
- Suppose there is a structure containing a person's name
- and a pointer to a
- .LW gnumbers
- structure containing the person's gross assets and liabilities.
- The construct is:
- .LS
- struct pgn {
- char *name;
- struct gnumbers *gnp;
- };
- .Lf
- The corresponding XDR routine for this structure is:
- .LS
- bool_t
- xdr_pgn(xdrs, pp)
- XDR *xdrs;
- struct pgn *pp;
- {
- if (xdr_string(xdrs, &pp->name, NLEN) &&
- xdr_reference(xdrs, &pp->gnp,
- sizeof(struct gnumbers), xdr_gnumbers))
- return(TRUE);
- return(FALSE);
- }
- .Lf
- .NH 4
- Pointer Semantics and XDR
- .LP
- In many applications,
- C programmers attach double meaning to the values of a pointer.
- Typically the value
- .LW NULL
- (or zero) means data is not needed,
- yet some application-specific interpretation applies.
- In essence, the C programmer is encoding
- a discriminated union efficiently
- by overloading the interpretation of the value of a pointer.
- For instance, in example E a
- .LW NULL
- pointer value for
- .LW gnp
- could indicate that
- the person's assets and liabilities are unknown.
- That is, the pointer value encodes two things:
- whether or not the data is known;
- and if it is known, where it is located in memory.
- Linked lists are an extreme example of the use
- of application-specific pointer interpretation.
- .LP
- The primitive
- .LW xdr_reference()
- cannot and does not attach any special
- meaning to a null-value pointer during serialization.
- That is, passing an address of a pointer whose value is
- .LW NULL
- to
- .LW xdr_reference()
- when serialing data will most likely cause a memory fault and, on
- .UX ,
- a core dump for debugging.
- .LP
- It is the explicit responsibility of the programmer
- to expand non-dereferenceable pointers into their specific semantics.
- This usually involves describing data with a two-armed discriminated union.
- One arm is used when the pointer is valid;
- the other is used when the pointer is invalid
- .LW NULL ). (
- Section 7 has an example (linked lists encoding) that deals
- with invalid pointer interpretation.
- .LP
- Exercise:
- After reading Section 7, return here and extend example E so that
- it can correctly deal with null pointer values.
- .LP
- Exercise:
- Using the
- .LW xdr_union() ,
- .LW xdr_reference()
- and
- .LW xdr_void()
- primitives, implement a generic pointer handling primitive
- that implicitly deals with
- .LW NULL
- pointers. The XDR library does not provide such a primitive
- because it does not want to give the illusion
- that pointers have meaning in the external world.
- .NH 2
- Non-filter Primitives
- .LP
- XDR streams can be manipulated with
- the primitives discussed in this section.
- .LS
- u_int xdr_getpos(xdrs)
- XDR *xdrs;
- .sp.5
- bool_t xdr_setpos(xdrs, pos)
- XDR *xdrs;
- u_int pos;
- .sp.5
- xdr_destroy(xdrs)
- XDR *xdrs;
- .Lf
- The routine
- .LW xdr_getpos()
- returns an unsigned integer
- that describes the current position in the data stream.
- Warning: In some XDR streams, the returned value of
- .LW xdr_getpos()
- is meaningless;
- the routine returns a \-1 in this case
- (though \-1 should be a legitimate value).
- .LP
- The routine
- .LW xdr_setpos()
- sets a stream position to
- .LW pos .
- Warning: In some XDR streams, setting a position is impossible;
- in such cases,
- .LW xdr_setpos()
- will return
- .LW FALSE .
- This routine will also fail if the requested position is out-of-bounds.
- The definition of bounds varies from stream to stream.
- .LP
- The
- .LW xdr_destroy()
- primitive destroys the XDR stream.
- Usage of the stream
- after calling this routine is undefined.
- .NH 2
- XDR Operation Directions
- .LP
- At times you may wish to optimize XDR routines by taking
- advantage of the direction of the operation \(em
- .LW XDR_ENCODE ,
- .LW XDR_DECODE ,
- or
- .LW XDR_FREE .
- The value
- .LW xdrs->x_op
- always contains the
- direction of the XDR operation.
- Programmers are not encouraged to take advantage of this information.
- Therefore, no example is presented here.
- However, an example in Section 7
- demonstrates the usefulness of the
- .LW xdrs->x_op
- field.
- .bp
- .NH
- XDR Stream Access
- .LP
- An XDR stream is obtained by calling the appropriate creation routine.
- These creation routines take arguments that are tailored to the
- specific properties of the stream.
- .LP
- Streams currently exist for (de)serialization of data to or from
- standard I/O
- .LW FILE
- streams, TCP/IP connections and
- .UX
- files, and memory.
- Section 5 documents the XDR object and how to make
- new XDR streams when they are required.
- .NH 2
- Standard I/O Streams
- .LP
- XDR streams can be interfaced to standard I/O using the
- .LW xdrstdio_create()
- routine as follows:
- .LS
- #include <stdio.h>
- #include <rpc/rpc.h> /* xdr streams part of rpc */
- .sp.5
- void
- xdrstdio_create(xdrs, fp, x_op)
- XDR *xdrs;
- FILE *fp;
- enum xdr_op x_op;
- .Lf
- The routine
- .LW xdrstdio_create()
- initializes an XDR stream pointed to by
- .LW xdrs .
- The XDR stream interfaces to the standard I/O library.
- Parameter
- .LW fp
- is an open file, and
- .LW x_op
- is an XDR direction.
- .NH 2
- Memory Streams
- .LP
- Memory streams allow the streaming of data into or out of
- a specified area of memory:
- .LS
- #include <rpc/rpc.h>
- .sp.5
- void
- xdrmem_create(xdrs, addr, len, x_op)
- XDR *xdrs;
- char *addr;
- u_int len;
- enum xdr_op x_op;
- .Lf
- The routine
- .LW xdrmem_create()
- initializes an XDR stream in local memory.
- The memory is pointed to by parameter
- .LW addr ;
- parameter
- .LW len
- is the length in bytes of the memory.
- The parameters
- .LW xdrs
- and
- .LW x_op
- are identical to the corresponding parameters of
- .LW xdrstdio_create() .
- Currently, the UDP/IP implementation of RPC uses
- .LW xdrmem_create() .
- Complete call or result messages are built in memory before calling the
- .LW sendto()
- system routine.
- .NH 2
- Record (TCP/IP) Streams
- .LP
- A record stream is an XDR stream built on top of
- a record marking standard that is built on top of the
- .UX
- file or 4.2 BSD connection interface.
- .LS
- #include <rpc/rpc.h> /* xdr streams part of rpc */
- .sp.5
- xdrrec_create(xdrs,
- sendsize, recvsize, iohandle, readproc, writeproc)
- XDR *xdrs;
- u_int sendsize, recvsize;
- char *iohandle;
- int (*readproc)(), (*writeproc)();
- .Lf
- The routine
- .LW xdrrec_create()
- provides an XDR stream interface that allows for a bidirectional,
- arbitrarily long sequence of records.
- The contents of the records are meant to be data in XDR form.
- The stream's primary use is for interfacing RPC to TCP connections.
- However, it can be used to stream data into or out of normal
- .UX
- files.
- .LP
- The parameter
- .LW xdrs
- is similar to the corresponding parameter described above.
- The stream does its own data buffering similar to that of standard I/O.
- The parameters
- .LW sendsize
- and
- .LW recvsize
- determine the size in bytes of the output and input buffers, respectively;
- if their values are zero (0), then predetermined defaults are used.
- When a buffer needs to be filled or flushed, the routine
- .LW readproc
- or
- .LW writeproc
- is called, respectively.
- The usage and behavior of these
- routines are similar to the
- .UX
- system calls
- .LW read()
- and
- .LW write() .
- However,
- the first parameter to each of these routines is the opaque parameter
- .LW iohandle .
- The other two parameters
- .LW buf "" (
- and
- .LW nbytes )
- and the results
- (byte count) are identical to the system routines.
- If
- .LW xxx
- is
- .LW readproc
- or
- .LW writeproc ,
- then it has the following form:
- .LS
- /*
- * returns the actual number of bytes transferred.
- * -1 is an error
- */
- int
- xxx(iohandle, buf, len)
- char *iohandle;
- char *buf;
- int nbytes;
- .Lf
- The XDR stream provides means for delimiting records in the byte stream.
- The implementation details of delimiting records in a stream
- are discussed in appendix 1.
- The primitives that are specific to record streams are as follows:
- .LS
- bool_t
- xdrrec_endofrecord(xdrs, flushnow)
- XDR *xdrs;
- bool_t flushnow;
- .sp.5
- bool_t
- xdrrec_skiprecord(xdrs)
- XDR *xdrs;
- .sp.5
- bool_t
- xdrrec_eof(xdrs)
- XDR *xdrs;
- .Lf
- The routine
- .LW xdrrec_endofrecord()
- causes the current outgoing data to be marked as a record.
- If the parameter
- .LW flushnow
- is
- .LW TRUE ,
- then the stream's
- .LW writeproc()
- will be called; otherwise,
- .LW writeproc()
- will be called when the output buffer has been filled.
- .LP
- The routine
- .LW xdrrec_skiprecord()
- causes an input stream's position to be moved past
- the current record boundary and onto the
- beginning of the next record in the stream.
- .LP
- If there is no more data in the stream's input buffer,
- then the routine
- .LW xdrrec_eof()
- returns
- .LW TRUE .
- That is not to say that there is no more data
- in the underlying file descriptor.
- .bp
- .NH
- XDR Stream Implementation
- .LP
- This section provides the abstract data types needed
- to implement new instances of XDR streams.
- .NH 2
- The XDR Object
- .LP
- The following structure defines the interface to an XDR stream:
- .LS
- enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 };
- .sp.5
- typedef struct {
- enum xdr_op x_op; /* operation; fast added param */
- struct xdr_ops {
- bool_t (*x_getlong)(); /* get long from stream */
- bool_t (*x_putlong)(); /* put long to stream */
- bool_t (*x_getbytes)(); /* get bytes from stream */
- bool_t (*x_putbytes)(); /* put bytes to stream */
- u_int (*x_getpostn)(); /* return stream offset */
- bool_t (*x_setpostn)(); /* reposition offset */
- caddr_t (*x_inline)(); /* ptr to buffered data */
- VOID (*x_destroy)(); /* free private area */
- } *x_ops;
- caddr_t x_public; /* users' data */
- caddr_t x_private; /* pointer to private data */
- caddr_t x_base; /* private for position info */
- int x_handy; /* extra private word */
- } XDR;
- .Lf
- The
- .LW x_op
- field is the current operation being performed on the stream.
- This field is important to the XDR primitives,
- but should not affect a stream's implementation.
- That is, a stream's implementation should not depend
- on this value.
- The fields
- .LW x_private ,
- .LW x_base ,
- and
- .LW x_handy
- are private to the particular
- stream's implementation.
- The field
- .LW x_public
- is for the XDR client and should never be used by
- the XDR stream implementations or the XDR primitives.
- .LP
- Macros for accessing operations
- .LW x_getpostn() ,
- .LW x_setpostn() ,
- and
- .LW x_destroy()
- were defined in Section 3.6.
- The operation
- .LW x_inline()
- takes two parameters:
- an XDR *, and an unsigned integer, which is a byte count.
- The routine returns a pointer to a piece of
- the stream's internal buffer.
- The caller can then use the buffer segment for any purpose.
- From the stream's point of view, the bytes in the
- buffer segment have been consumed or put.
- The routine may return
- .LW NULL
- if it cannot return a buffer segment of the requested size.
- (The
- .LW x_inline
- routine is for cycle squeezers.
- Use of the resulting buffer is not data-portable.
- Users are encouraged not to use this feature.)
- .LP
- The operations
- .LW x_getbytes()
- and
- .LW x_putbytes()
- blindly get and put sequences of bytes
- from or to the underlying stream;
- they return
- .LW TRUE
- if they are successful, and
- .LW FALSE
- otherwise. The routines have identical parameters (replace
- .LW xxx ):
- .LS
- bool_t
- xxxbytes(xdrs, buf, bytecount)
- XDR *xdrs;
- char *buf;
- u_int bytecount;
- .Lf
- The operations
- .LW x_getlong()
- and
- .LW x_putlong()
- receive and put
- long numbers from and to the data stream.
- It is the responsibility of these routines
- to translate the numbers between the machine representation
- and the (standard) external representation.
- The
- .UX
- primitives
- .LW htonl()
- and
- .LW ntohl()
- can be helpful in accomplishing this.
- Section 6 defines the standard representation of numbers.
- The higher-level XDR implementation assumes that
- signed and unsigned long integers contain the same number of bits,
- and that nonnegative integers
- have the same bit representations as unsigned integers.
- The routines return
- .I TRUE
- if they succeed, and
- .LW FALSE
- otherwise. They have identical parameters:
- .LS
- bool_t
- xxxlong(xdrs, lp)
- XDR *xdrs;
- long *lp;
- .Lf
- Implementors of new XDR streams must make an XDR structure
- (with new operation routines) available to clients,
- using some kind of create routine.
- .bp
- .NH
- XDR Standard
- .LP
- This section defines the external data representation standard.
- The standard is independent of languages,
- operating systems and hardware architectures.
- Once data is shared among machines, it should not matter that the data
- was produced on a Sun, but is consumed by a VAX (or vice versa).
- Similarly the choice of operating systems should have no influence
- on how the data is represented externally.
- For programming languages,
- data produced by a C program should be readable
- by a Fortran or Pascal program.
- .LP
- The external data representation standard depends on the assumption that
- bytes (or octets) are portable.
- A byte is defined to be eight bits of data.
- It is assumed that hardware
- that encodes bytes onto various media
- will preserve the bytes' meanings
- across hardware boundaries.
- For example, the Ethernet standard suggests that bytes be
- encoded ``little endian'' style.
- Both Sun and VAX hardware implementations
- adhere to the standard.
- .LP
- The XDR standard also suggests a language used to describe data.
- The language is a bastardized C;
- it is a data description language, not a programming language.
- (The Xerox Courier Standard uses bastardized Mesa
- as its data description language.)
- .NH 2
- Basic Block Size
- .LP
- The representation of all items requires
- a multiple of four bytes (or 32 bits) of data.
- The bytes are numbered
- $0$ through $n-1$, where $(n ~ \fRmod\fP ~ 4) = 0$.
- The bytes are read or written to some byte stream
- such that byte $m$ always precedes byte $m+1$.
- .NH 2
- Integer
- .LP
- An XDR signed integer is a 32-bit datum
- that encodes an integer in the range
- .LW [-2147483648,2147483647] .
- The integer is represented in two's complement notation.
- The most and least significant bytes are 0 and 3, respectively.
- The data description of integers is
- .LW integer .
- .NH 2
- Unsigned Integer
- .LP
- An XDR unsigned integer is a 32-bit datum
- that encodes a nonnegative integer in the range
- .LW [0,4294967295] .
- It is represented by an unsigned binary number whose most
- and least significant bytes are 0 and 3, respectively.
- The data description of unsigned integers is
- .LW unsigned .
- .NH 2
- Enumerations
- .LP
- Enumerations have the same representation as integers.
- Enumerations are handy for describing subsets of the integers.
- The data description of enumerated data is as follows:
- .LS
- typedef enum { name = value, .... } type-name;
- .Lf
- For example the three colors red, yellow and blue
- could be described by an enumerated type:
- .LS
- typedef enum { RED = 2, YELLOW = 3, BLUE = 5 } colors;
- .Lf
- .NH 2
- Booleans
- .LP
- Booleans are important enough and occur frequently enough
- to warrant their own explicit type in the standard.
- Boolean is an enumeration with the
- following form:
- .LS
- typedef enum { FALSE = 0, TRUE = 1 } boolean;
- .Lf
- .NH 2
- Hyper Integer and Hyper Unsigned
- .LP
- The standard also defines 64-bit (8-byte) numbers called
- .LW "hyper integer"
- and
- .LW "hyper unsigned" .
- Their representations are the obvious extensions of
- the integer and unsigned defined above.
- The most and least significant bytes are 0 and 7, respectively.
- .NH 2
- Floating Point and Double Precision
- .LP
- The standard defines the encoding for the floating point data types
- .LW float
- (32 bits or 4 bytes) and
- .LW double
- (64 bits or 8 bytes).
- The encoding used is the IEEE standard for normalized
- single- and double-precision floating point numbers.
- See the IEEE floating point standard for more information.
- The standard encodes the following three fields,
- which describe the floating point number:
- .IP \fIS\fP
- The sign of the number.
- Values 0 and 1 represent
- positive and negative, respectively.
- .IP \fIE\fP
- The exponent of the number, base 2.
- Floats devote 8 bits to this field,
- while doubles devote 11 bits.
- The exponents for float and double are
- biased by 127 and 1023, respectively.
- .IP \fIF\fP
- The fractional part of the number's mantissa, base 2.
- Floats devote 23 bits to this field,
- while doubles devote 52 bits.
- .LP
- Therefore, the floating point number is described by:
- .EQ
- (-1) sup S * 2 sup { E - Bias } * 1.F
- .EN
- .LP
- Just as the most and least significant bytes of a number are 0 and 3,
- the most and least significant bits of
- a single-precision floating point number are 0 and 31.
- The beginning bit (and most significant bit) offsets
- of $S$, $E$, and $F$ are 0, 1, and 9, respectively.
- .LP
- Doubles have the analogous extensions.
- The beginning bit (and
- most significant bit) offsets of $S$, $E$, and $F$
- are 0, 1, and 12, respectively.
- .LP
- The IEEE specification should be consulted concerning the encoding for
- signed zero, signed infinity (overflow), and denormalized numbers (underflow).
- Under IEEE specifications, the ``NaN'' (not a number)
- is system dependent and should not be used.
- .NH 2
- Opaque Data
- .LP
- At times fixed-sized uninterpreted data
- needs to be passed among machines.
- This data is called
- .LW opaque
- and is described as:
- .LS
- typedef opaque type-name[n];
- opaque name[n];
- .Lf
- where
- .LW n
- is the (static) number of bytes necessary to contain the opaque data.
- If
- .LW n
- is not a multiple of four, then the
- .LW n
- bytes are followed by enough (up to 3) zero-valued bytes
- to make the total byte count of the opaque object a multiple of four.
- .NH 2
- Counted Byte Strings
- .LP
- The standard defines a string of $n$ (numbered $0$ through $n-1$)
- bytes to be the number $n$ encoded as
- .LW unsigned ,
- and followed by the $n$ bytes of the string.
- If $n$ is not a multiple of four,
- then the $n$ bytes are followed by
- enough (up to 3) zero-valued bytes
- to make the total byte count a multiple of four.
- The data description of strings is as follows:
- .LS
- typedef string type-name<N>;
- typedef string type-name<>;
- string name<N>;
- string name<>;
- .Lf
- Note that the data description language uses angle brackets (< and >)
- to denote anything that is varying-length
- (as opposed to square brackets to denote fixed-length sequences of data).
- .LP
- The constant
- .LW N
- denotes an upper bound of the number of bytes that a
- string may contain.
- If
- .LW N
- is not specified, it is assumed to be $2 sup 32 - 1$,
- the maximum length.
- The constant
- .LW N
- would normally be found in a protocol specification.
- For example, a filing protocol may state
- that a file name can be no longer than 255 bytes, such as:
- .LS
- string filename<255>;
- .Lf
- .LP
- The XDR specification does not say what the
- individual bytes of a string represent;
- this important information is left to higher-level specifications.
- A reasonable default is to assume
- that the bytes encode ASCII characters.
- .NH 2
- Fixed Arrays
- .LP
- The data description for fixed-size arrays of
- homogeneous elements is as follows:
- .LS
- typedef elementtype type-name[n];
- elementtype name[n];
- .Lf
- Fixed-size arrays of elements numbered $0$ through $n-1$
- are encoded by individually encoding the elements of the array
- in their natural order, $0$ through $n-1$.
- .NH 2
- Counted Arrays
- .LP
- Counted arrays provide the ability to encode varyiable-length arrays
- of homogeneous elements.
- The array is encoded as:
- the element count $n$ (an unsigned integer),
- followed by the encoding of each of the array's elements,
- starting with element $0$ and progressing through element $n-1$.
- The data description for counted arrays
- is similar to that of counted strings:
- .LS
- typedef elementtype type-name<N>;
- typedef elementtype type-name<>;
- elementtype name<N>;
- elementtype name<>;
- .Lf
- Again, the constant
- .LW N
- specifies the maximum acceptable
- element count of an array; if
- .LW N
- is not specified, it is assumed to be $2 sup 32 - 1$.
- .NH 2
- Structures
- .LP
- The data description for structures is very similar to
- that of standard C:
- .LS
- typedef struct {
- component-type component-name;
- ...
- } type-name;
- .Lf
- The components of the structure are encoded
- in the order of their declaration in the structure.
- .NH 2
- Discriminated Unions
- .LP
- A discriminated union is a type composed of a discriminant followed by a type
- selected from a set of prearranged types according to the value of the
- discriminant.
- The type of the discriminant is always an enumeration.
- The component types are called ``arms'' of the union.
- The discriminated union is encoded as its discriminant followed by
- the encoding of the implied arm.
- The data description for discriminated unions is as follows:
- .LS
- typedef union switch (discriminant-type) {
- discriminant-value: arm-type;
- ...
- default: default-arm-type;
- } type-name;
- .Lf
- The default arm is optional.
- If it is not specified, then a valid
- encoding of the union cannot take on unspecified discriminant values.
- Most specifications neither need nor use default arms.
- .NH 2
- Missing Specifications
- .LP
- The standard lacks representations for bit fields and bitmaps,
- since the standard is based on bytes.
- This is not to say that no specification should be attempted.
- .NH 2
- Library Primitive / XDR Standard Cross Reference
- .LP
- The following table describes the association between
- the C library primitives discussed in Section 3,
- and the standard data types defined in this section:
- .LP
- .TS
- box;
- cfBI s s
- cfI cfI cfI
- rfL | cfL | l.
- .sp.1
- Primitives and Data Types
- .sp.1
- _
- C Primitive XDR Type Sections
- _
- xdr_int
- xdr_long integer 3.1, 6.2
- xdr_short
- _
- xdr_u_int
- xdr_u_long unsigned 3.1, 6.3
- xdr_u_short
- _
- - hyper integer 6.6
- hyper unsigned
- _
- xdr_float float 3.2, 6.7
- _
- xdr_double double 3.2, 6.7
- _
- xdr_enum enum_t 3.3, 6.4
- _
- xdr_bool bool_t 3.3, 6.5
- _
- xdr_string string 3.5.1, 6.9
- xdr_bytes 3.5.2
- _
- xdr_array \fR(varying arrays)\fP 3.5.3, 6.11
- _
- - \fR(fixed arrays)\fP 3.5.5, 6.10
- _
- xdr_opaque opaque 3.5.4, 6.8
- _
- xdr_union union 3.5.6, 6.13
- _
- xdr_reference - 3.5.7
- _
- - struct 6.6
- .TE
- .TN "Primitives and Data Types"
- .bp
- .NH
- Advanced Topics
- .LP
- This section describes techniques for passing data structures
- that are not covered in the preceding sections.
- Such structures include linked lists (of arbitrary lengths).
- Unlike the simpler examples covered in the earlier sections,
- the following examples are written using both
- the XDR C library routines and the XDR data description language.
- Section 6 describes the XDR data definition language used below.
- .NH 2
- Linked Lists
- .LP
- The last example in Section 2 presented a C data structure and its
- associated XDR routines for a person's gross assets and liabilities.
- The example is duplicated below:
- .LS
- struct gnumbers {
- long g_assets;
- long g_liabilities;
- };
- .sp.5
- bool_t
- xdr_gnumbers(xdrs, gp)
- XDR *xdrs;
- struct gnumbers *gp;
- {
- if (xdr_long(xdrs, &(gp->g_assets)))
- return(xdr_long(xdrs, &(gp->g_liabilities)));
- return(FALSE);
- }
- .Lf
- Now assume that we wish to implement a linked list of such information.
- A data structure could be constructed as follows:
- .LS
- typedef struct gnnode {
- struct gnumbers gn_numbers;
- struct gnnode *nxt;
- };
- .sp.5
- typedef struct gnnode *gnumbers_list;
- .Lf
- The head of the linked list can be thought of as the data object;
- that is, the head is not merely a convenient shorthand for a structure.
- Similarly the
- .LW nxt
- field is used to indicate whether or not the object has terminated.
- Unfortunately, if the object continues, the
- .LW nxt
- field is also the address
- of where it continues.
- The link addresses carry no useful information when
- the object is serialized.
- .LP
- The XDR data description of this linked list is described by the
- recursive type declaration of gnumbers_list:
- .LS
- struct gnumbers {
- unsigned g_assets;
- unsigned g_liabilities;
- };
- .Lf
- .LS
- typedef union switch (boolean) {
- case TRUE: struct {
- struct gnumbers current_element;
- gnumbers_list rest_of_list;
- };
- case FALSE: struct {};
- } gnumbers_list;
- .Lf
- In this description,
- the boolean indicates whether there is more data following it.
- If the boolean is
- .LW FALSE ,
- then it is the last data field of the structure.
- If it is
- .LW TRUE ,
- then it is followed by a
- .LW gnumbers
- structure and (recursively) by a
- .LW gnumbers_list
- (the rest of the object).
- Note that the C declaration has no boolean explicitly declared in it
- (though the
- .LW nxt
- field implicitly carries the information), while
- the XDR data description has no pointer explicitly declared in it.
- .LP
- Hints for writing a set of XDR routines to successfully (de)serialize
- a linked list of entries can be taken
- from the XDR description of the pointer-less data.
- The set consists of the mutually recursive routines
- .LW xdr_gnumbers_list ,
- .LW xdr_wrap_list ,
- and
- .LW xdr_gnnode .
- .LS
- bool_t
- xdr_gnnode(xdrs, gp)
- XDR *xdrs;
- struct gnnode *gp;
- {
- return(xdr_gnumbers(xdrs, &(gp->gn_numbers)) &&
- xdr_gnumbers_list(xdrs, &(gp->nxt)) );
- }
- .Lf
-