home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / rpc2 / part03 / rpc / doc / xdr.spec.p1
Encoding:
Text File  |  1986-11-30  |  48.6 KB  |  1,995 lines

  1. .\" @(#)c3.xdr.spec 1.1 86/02/25 SMI; for 3.0 FCS
  2. .PL RIGHT
  3. .EQ
  4. delim $$
  5. .EN
  6. .TL
  7. External Data Representation
  8. .br
  9. Protocol Specification
  10. .bp
  11. .NH
  12. Introduction
  13. .LP
  14. This manual describes library routines that allow a C programmer to 
  15. describe arbitrary data structures in a machine-independent fashion.
  16. The eXternal Data Representation (XDR) standard
  17. is the backbone of Sun's Remote Procedure Call package,
  18. in the sense that data for remote procedure calls
  19. is transmitted using the standard.
  20. XDR library routines should be used to transmit data
  21. that is accessed (read or written) by more than one type of machine.
  22. .LP
  23. This manual contains a description of XDR library routines,
  24. a guide to accessing currently available XDR streams,
  25. information on defining new streams and data types,
  26. and a formal definition of the XDR standard.
  27. XDR was designed to work across different languages,
  28. operating systems, and machine architectures.
  29. Most users (particularly RPC users)
  30. only need the information in sections 2 and 3 of this document.
  31. Programmers wishing to implement RPC and XDR on new machines
  32. will need the information in sections 4 through 6.
  33. Advanced topics, not necessary for all implementations,
  34. are covered in section 7.
  35. .LP
  36. On Sun systems,
  37. C programs that want to use XDR routines
  38. must include the file
  39. .LW <rpc/rpc.h> ,
  40. which contains all the necessary interfaces to the XDR system.
  41. Since the C library
  42. .LW libc.a
  43. contains all the XDR routines,
  44. compile as normal.
  45. .LS
  46. % \f(LBcc\0\fIprogram\fP.c\fL
  47. .Lf
  48. .NH 2
  49. Justification
  50. .LP
  51. Consider the following two programs,
  52. .LW writer :
  53. .LS
  54. #include <stdio.h>
  55. .sp.5
  56. main()            /* writer.c */
  57. {
  58.     long i;
  59. .sp.5
  60.     for (i = 0; i < 8; i++) {
  61.         if (fwrite((char *)&i, sizeof(i), 1, stdout) != 1) {
  62.             fprintf(stderr, "failed!\en");
  63.             exit(1);
  64.         }
  65.     }
  66. }
  67. .Lf
  68. and
  69. .LW reader :
  70. .LS
  71. #include <stdio.h>
  72. .sp.5
  73. main()            /* reader.c */
  74. {
  75.     long i, j;
  76. .sp.5
  77.     for (j = 0; j < 8; j++) {
  78.         if (fread((char *)&i, sizeof (i), 1, stdin) != 1) {
  79.             fprintf(stderr, "failed!\en");
  80.             exit(1);
  81.         }
  82.         printf("%ld ", i);
  83.     }
  84.     printf("\en");
  85. }
  86. .Lf
  87. The two programs appear to be portable, because
  88. (a) they pass
  89. .LW lint
  90. checking, and
  91. (b) they exhibit the same behavior when executed
  92. on two different hardware architectures, a Sun and a VAX.
  93. .LP
  94. Piping the output of the
  95. .LW writer
  96. program to the
  97. .LW reader
  98. program gives identical results on a Sun or a VAX.\(dd
  99. .FS \(dd
  100. VAX is a trademark of Digital Equipment Corporation.
  101. .FE
  102. .LS
  103. sun% writer | reader
  104. 0 1 2 3 4 5 6 7
  105. sun%
  106. .Lf
  107. .LS
  108. vax% writer | reader
  109. 0 1 2 3 4 5 6 7
  110. vax%
  111. .Lf
  112. With the advent of local area networks and Berkeley's 4.2 BSD
  113. .UX
  114. came the concept of ``network pipes'' \(em
  115. a process produces data on one machine,
  116. and a second process consumes data on another machine.
  117. A network pipe can be constructed with
  118. .LW writer
  119. and
  120. .LW reader .
  121. Here are the results if the first produces data on a Sun,
  122. and the second consumes data on a VAX.
  123. .LS
  124. sun% writer | rsh vax reader
  125. 0 16777216 33554432 50331648 67108864 83886080 100663296
  126. 117440512
  127. sun%
  128. .Lf
  129. Identical results can be obtained by executing
  130. .LW writer
  131. on the VAX and
  132. .LW reader
  133. on the Sun.
  134. These results occur because the byte ordering
  135. of long integers differs between the VAX and the Sun,
  136. even though word size is the same.
  137. Note that $16777216$ is $2 sup 24$ \(em
  138. when four bytes are reversed, the 1 winds up in the 24th bit.
  139. .LP
  140. Whenever data is shared by two or more machine types,
  141. there is a need for portable data.
  142. Programs can be made data-portable by replacing the
  143. .LW read()
  144. and
  145. .LW write()
  146. calls with calls to an XDR library routine
  147. .LW xdr_long() ,
  148. a filter that knows the standard representation
  149. of a long integer in its external form.
  150. Here are the revised versions of
  151. .LW writer :
  152. .LS
  153. #include <stdio.h>
  154. #include <rpc/rpc.h>    /* xdr is a sub-library of rpc */
  155. .sp.5
  156. main()        /* writer.c */
  157. {
  158.     XDR xdrs;
  159.     long i;
  160. .sp.5
  161.     xdrstdio_create(&xdrs, stdout, XDR_ENCODE);
  162.     for (i = 0; i < 8; i++) {
  163.         if (!xdr_long(&xdrs, &i)) {
  164.             fprintf(stderr, "failed!\en");
  165.             exit(1);
  166.         }
  167.     }
  168. }
  169. .Lf
  170. and
  171. .LW reader :
  172. .LS
  173. #include <stdio.h>
  174. #include <rpc/rpc.h>    /* xdr is a sub-library of rpc */
  175. .sp.5
  176. main()        /* reader.c */
  177. {
  178.     XDR xdrs;
  179.     long i, j;
  180. .sp.5
  181.     xdrstdio_create(&xdrs, stdin, XDR_DECODE);
  182.     for (j = 0; j < 8; j++) {
  183.         if (!xdr_long(&xdrs, &i)) {
  184.             fprintf(stderr, "failed!\en");
  185.             exit(1);
  186.         }
  187.         printf("%ld ", i);
  188.     }
  189.     printf("\en");
  190. }
  191. .Lf
  192. The new programs were executed on a Sun,
  193. on a VAX, and from a Sun to a VAX;
  194. the results are shown below.
  195. .LS
  196. sun% writer | reader
  197. 0 1 2 3 4 5 6 7
  198. sun%
  199. .Lf
  200. .LS
  201. vax% writer | reader
  202. 0 1 2 3 4 5 6 7
  203. vax%
  204. .Lf
  205. .LS
  206. sun% writer | rsh vax reader
  207. 0 1 2 3 4 5 6 7
  208. sun%
  209. .Lf
  210. Dealing with integers is just the tip of the portable-data iceberg.
  211. Arbitrary data structures present portability problems,
  212. particularly with respect to alignment and pointers.
  213. Alignment on word boundaries may cause the
  214. size of a structure to vary from machine to machine.
  215. Pointers are convenient to use,
  216. but have no meaning outside the machine where they are defined.
  217. .NH 2
  218. The XDR Library
  219. .LP
  220. The XDR library solves data portability problems.
  221. It allows you to write and read arbitrary C constructs
  222. in a consistent, specified, well-documented manner.
  223. Thus, it makes sense to use the library even when the data
  224. is not shared among machines on a network.
  225. .LP
  226. The XDR library has filter routines for
  227. strings (null-terminated arrays of bytes),
  228. structures, unions, and arrays, to name a few.
  229. Using more primitive routines,
  230. you can write your own specific XDR routines
  231. to describe arbitrary data structures,
  232. including elements of arrays, arms of unions,
  233. or objects pointed at from other structures.
  234. The structures themselves may contain arrays of arbitrary elements,
  235. or pointers to other structures.
  236. .LP
  237. Let's examine the two programs more closely.
  238. There is a family of XDR stream creation routines
  239. in which each member treats the stream of bits differently.
  240. In our example, data is manipulated using standard I/O routines,
  241. so we use
  242. .LW xdrstdio_create() .
  243. The parameters to XDR stream creation routines
  244. vary according to their function.
  245. In our example,
  246. .LW xdrstdio_create()
  247. takes a pointer to an XDR structure that it initializes,
  248. a pointer to a
  249. .LW FILE
  250. that the input or output is performed on, and the operation.
  251. The operation may be
  252. .LW XDR_ENCODE
  253. for serializing in the
  254. .LW writer
  255. program, or
  256. .LW XDR_DECODE
  257. for deserializing in the
  258. .LW reader
  259. program.
  260. .LP
  261. Note: RPC clients never need to create XDR streams;
  262. the RPC system itself creates these streams,
  263. which are then passed to the clients.
  264. .LP
  265. The
  266. .LW xdr_long()
  267. primitive is characteristic of most XDR library 
  268. primitives and all client XDR routines.
  269. First, the routine returns
  270. .LW FALSE
  271. (0) if it fails, and
  272. .LW TRUE
  273. (1) if it succeeds.
  274. Second, for each data type,
  275. .LW xxx ,
  276. there is an associated XDR routine of the form:
  277. .LS
  278. xdr_xxx(xdrs, fp)
  279.     XDR *xdrs;
  280.     xxx *fp;
  281. {
  282. }
  283. .Lf
  284. In our case,
  285. .LW xxx
  286. is long, and the corresponding XDR routine is
  287. a primitive,
  288. .LW xdr_long . 
  289. The client could also define an arbitrary structure
  290. .LW xxx
  291. in which case the client would also supply the routine
  292. .LW xdr_xxx ,
  293. describing each field by calling XDR routines
  294. of the appropriate type.
  295. In all cases the first parameter,
  296. .LW xdrs
  297. can be treated as an opaque handle,
  298. and passed to the primitive routines.
  299. .LP
  300. XDR routines are direction independent;
  301. that is, the same routines are called to serialize or deserialize data.
  302. This feature is critical to software engineering of portable data.
  303. The idea is to call the same routine for either operation \(em
  304. this almost guarantees that serialized data can also be deserialized.
  305. One routine is used by both producer and consumer of networked data.
  306. This is implemented by always passing the address
  307. of an object rather than the object itself \(em
  308. only in the case of deserialization is the object modified.
  309. This feature is not shown in our trivial example,
  310. but its value becomes obvious when nontrivial data structures
  311. are passed among machines.
  312. If needed, you can obtain the direction of the XDR operation.
  313. See section 3.7 for details.
  314. .LP
  315. Let's look at a slightly more complicated example.
  316. Assume that a person's gross assets and liabilities
  317. are to be exchanged among processes.
  318. Also assume that these values are important enough
  319. to warrant their own data type:
  320. .LS
  321. struct gnumbers {
  322.     long g_assets;
  323.     long g_liabilities;
  324. };
  325. .Lf
  326. The corresponding XDR routine describing this structure would be:
  327. .LS
  328. bool_t          /* TRUE is success, FALSE is failure */
  329. xdr_gnumbers(xdrs, gp)
  330.     XDR *xdrs;
  331.     struct gnumbers *gp;
  332. {
  333.     if (xdr_long(xdrs, &gp->g_assets) &&
  334.         xdr_long(xdrs, &gp->g_liabilities))
  335.         return(TRUE);
  336.     return(FALSE);
  337. }
  338. .Lf
  339. Note that the parameter
  340. .LW xdrs
  341. is never inspected or modified;
  342. it is only passed on to the subcomponent routines.
  343. It is imperative to inspect the return value of each XDR routine call,
  344. and to give up immediately and return
  345. .LW FALSE
  346. if the subroutine fails.
  347. .LP
  348. This example also shows that the type
  349. .LW bool_t
  350. is declared as an integer whose only values are
  351. .LW TRUE
  352. (1) and
  353. .LW FALSE
  354. (0).  This document uses the following definitions:
  355. .LS
  356. #define bool_t    int
  357. #define TRUE    1
  358. #define FALSE    0
  359. .sp.5
  360. #define enum_t int    /* enum_t used for generic enums */
  361. .Lf
  362. .LP
  363. Keeping these conventions in mind,
  364. .LW xdr_gnumbers()
  365. can be rewritten as follows:
  366. .LS
  367. xdr_gnumbers(xdrs, gp)
  368.     XDR *xdrs;
  369.     struct gnumbers *gp;
  370. {
  371.     return(xdr_long(xdrs, &gp->g_assets) &&
  372.         xdr_long(xdrs, &gp->g_liabilities));
  373. }
  374. .Lf
  375. This document uses both coding styles.
  376. .bp
  377. .NH
  378. XDR Library Primitives
  379. .LP
  380. This section gives a synopsis of each XDR primitive.
  381. It starts with basic data types and moves on to constructed data types.
  382. Finally, XDR utilities are discussed.
  383. The interface to these primitives
  384. and utilities is defined in the include file
  385. .LW <rpc/xdr.h> ,
  386. automatically included by
  387. .LW <rpc/rpc.h> .
  388. .NH 2
  389. Number Filters
  390. .LP
  391. The XDR library provides primitives to translate between numbers
  392. and their corresponding external representations.
  393. Primitives cover the set of numbers in:
  394. .EQ
  395. [signed, unsigned] * [short, int, long]
  396. .EN
  397. Specifically, the six primitives are:
  398. .LS
  399. bool_t xdr_int(xdrs, ip)
  400.     XDR *xdrs;
  401.     int *ip;
  402. .sp.5
  403. bool_t xdr_u_int(xdrs, up)
  404.     XDR *xdrs;
  405.     unsigned *up;
  406. .sp.5
  407. bool_t xdr_long(xdrs, lip)
  408.     XDR *xdrs;
  409.     long *lip;
  410. .sp.5
  411. bool_t xdr_u_long(xdrs, lup)
  412.     XDR *xdrs;
  413.     u_long *lup;
  414. .sp.5
  415. bool_t xdr_short(xdrs, sip)
  416.     XDR *xdrs;
  417.     short *sip;
  418. .sp.5
  419. bool_t xdr_u_short(xdrs, sup)
  420.     XDR *xdrs;
  421.     u_short *sup;
  422. .Lf
  423. The first parameter,
  424. .LW xdrs ,
  425. is an XDR stream handle.
  426. The second parameter is the address of the number
  427. that provides data to the stream or receives data from it.
  428. All routines return
  429. .LW TRUE
  430. if they complete successfully, and
  431. .LW FALSE
  432. otherwise.
  433. .NH 2
  434. Floating Point Filters
  435. .LP
  436. The XDR library also provides primitive routines
  437. for C's floating point types:
  438. .LS
  439. bool_t xdr_float(xdrs, fp)
  440.     XDR *xdrs;
  441.     float *fp;
  442. .sp.5
  443. bool_t xdr_double(xdrs, dp)
  444.     XDR *xdrs;
  445.     double *dp;
  446. .Lf
  447. The first parameter,
  448. .LW xdrs
  449. is an XDR stream handle.
  450. The second parameter is the address
  451. of the floating point number that provides data to the stream
  452. or receives data from it.
  453. All routines return
  454. .LW TRUE
  455. if they complete successfully, and
  456. .LW FALSE
  457. otherwise.
  458. .LP
  459. Note: Since the numbers are represented in IEEE floating point,
  460. routines may fail when decoding a valid IEEE representation
  461. into a machine-specific representation, or vice-versa.
  462. .NH 2
  463. Enumeration Filters
  464. .LP
  465. The XDR library provides a primitive for generic enumerations.
  466. The primitive assumes that a C
  467. .LW enum
  468. has the same representation inside the machine as a C integer.
  469. The boolean type is an important instance of the
  470. .LW enum .
  471. The external representation of a boolean is always one
  472. .LW TRUE ) (
  473. or zero
  474. .LW FALSE ). (
  475. .LS
  476. #define bool_t    int
  477. #define FALSE    0
  478. #define TRUE    1
  479. .sp.5
  480. #define enum_t int
  481. .sp.5
  482. bool_t xdr_enum(xdrs, ep)
  483.     XDR *xdrs;
  484.     enum_t *ep;
  485. .sp.5
  486. bool_t xdr_bool(xdrs, bp)
  487.     XDR *xdrs;
  488.     bool_t *bp;
  489. .Lf
  490. The second parameters
  491. .LW ep
  492. and
  493. .LW bp
  494. are addresses of the associated type
  495. that provides data to, or receives data from, the stream
  496. .LW xdrs .
  497. The routines return
  498. .LW TRUE
  499. if they complete successfully, and
  500. .LW FALSE
  501. otherwise.
  502. .NH 2
  503. No Data
  504. .LP
  505. Occasionally, an XDR routine must be supplied to the RPC system,
  506. even when no data is passed or required.
  507. The library provides such a routine:
  508. .LS
  509. bool_t xdr_void();  /* always returns TRUE */
  510. .Lf
  511. .NH 2
  512. Constructed Data Type Filters
  513. .LP
  514. Constructed or compound data type primitives
  515. require more parameters and perform more complicated functions
  516. then the primitives discussed above.
  517. This section includes primitives for
  518. strings, arrays, unions, and pointers to structures.
  519. .LP
  520. Constructed data type primitives may use memory management.
  521. In many cases, memory is allocated when deserializing data with
  522. .LW XDR_DECODE .
  523. Therefore, the XDR package must provide means to deallocate memory.
  524. This is done by an XDR operation,
  525. .LW XDR_FREE .
  526. To review, the three XDR directional operations are
  527. .LW XDR_ENCODE ,
  528. .LW XDR_DECODE ,
  529. and
  530. .LW XDR_FREE .
  531. .NH 3
  532. Strings
  533. .LP
  534. In C, a string is defined as a sequence of bytes
  535. terminated by a null byte,
  536. which is not considered when calculating string length.
  537. However, when a string is passed or manipulated,
  538. a pointer to it is employed.
  539. Therefore, the XDR library defines a string to be a
  540. .LW "char *" ,
  541. and not a sequence of characters.
  542. The external representation of a string is drastically different
  543. from its internal representation.
  544. Externally, strings are represented as
  545. sequences of ASCII characters,
  546. while internally, they are represented with character pointers.
  547. Conversion between the two representations
  548. is accomplished with the routine
  549. .LW xdr_string() :
  550. .LS
  551. bool_t xdr_string(xdrs, sp, maxlength)
  552.     XDR *xdrs;
  553.     char **sp;
  554.     u_int maxlength;
  555. .Lf
  556. The first parameter
  557. .LW xdrs
  558. is the XDR stream handle.
  559. The second parameter
  560. .LW sp
  561. is a pointer to a string (type
  562. .LW "char **" ).
  563. The third parameter
  564. .LW maxlength
  565. specifies the maximum number of bytes allowed during encoding or decoding;
  566. its value is usually specified by a protocol.
  567. For example, a protocol specification may say
  568. that a file name may be no longer than 255 characters.
  569. The routine returns
  570. .LW FALSE
  571. if the number of characters exceeds
  572. .LW maxlength ,
  573. and
  574. .LW TRUE
  575. if it doesn't.
  576. .LP
  577. The behavior of
  578. .LW xdr_string()
  579. is similar to the behavior of other routines
  580. discussed in this section.  The direction
  581. .LW XDR_ENCODE
  582. is easiest to understand.  The parameter
  583. .LW sp
  584. points to a string of a certain length;
  585. if it does not exceed
  586. .LW maxlength ,
  587. the bytes are serialized.
  588. .LP
  589. The effect of deserializing a string is subtle.
  590. First the length of the incoming string is determined;
  591. it must not exceed
  592. .LW maxlength .
  593. Next
  594. .LW sp
  595. is dereferenced; if the the value is
  596. .LW NULL ,
  597. then a string of the appropriate length is allocated and
  598. .LW *sp
  599. is set to this string.
  600. If the original value of
  601. .LW *sp
  602. is non-null, then the XDR package assumes
  603. that a target area has been allocated,
  604. which can hold strings no longer than
  605. .LW maxlength .
  606. In either case, the string is decoded into the target area.
  607. The routine then appends a null character to the string.
  608. .LP
  609. In the
  610. .LW XDR_FREE
  611. operation, the string is obtained by dereferencing
  612. .LW sp .
  613. If the string is not
  614. .LW NULL ,
  615. it is freed and
  616. .LW *sp
  617. is set to
  618. .LW NULL .
  619. In this operation,
  620. .LW xdr_string
  621. ignores the
  622. .LW maxlength
  623. parameter.
  624. .NH 3
  625. Byte Arrays
  626. .LP
  627. Often variable-length arrays of bytes are preferable to strings.
  628. Byte arrays differ from strings in the following three ways: 
  629. 1) the length of the array (the byte count) is explicitly
  630. located in an unsigned integer,
  631. 2) the byte sequence is not terminated by a null character, and
  632. 3) the external representation of the bytes is the same as their
  633. internal representation.
  634. The primitive
  635. .LW xdr_bytes()
  636. converts between the internal and external
  637. representations of byte arrays:
  638. .LS
  639. bool_t xdr_bytes(xdrs, bpp, lp, maxlength)
  640.     XDR *xdrs;
  641.     char **bpp;
  642.     u_int *lp;
  643.     u_int maxlength;
  644. .Lf
  645. The usage of the first, second and fourth parameters
  646. are identical to the first, second and third parameters of
  647. .LW xdr_string() ,
  648. respectively.
  649. The length of the byte area is obtained by dereferencing
  650. .LW lp
  651. when serializing;
  652. .LW *lp
  653. is set to the byte length when deserializing.
  654. .NH 3
  655. Arrays
  656. .LP
  657. The XDR library package provides a primitive
  658. for handling arrays of arbitrary elements.
  659. The
  660. .LW xdr_bytes()
  661. routine treats a subset of generic arrays,
  662. in which the size of array elements is known to be 1,
  663. and the external description of each element is built-in.
  664. The generic array primitive,
  665. .LW xdr_array()
  666. requires parameters identical to those of
  667. .LW xdr_bytes()
  668. plus two more:
  669. the size of array elements,
  670. and an XDR routine to handle each of the elements.
  671. This routine is called to encode or decode
  672. each element of the array.
  673. .LS
  674. bool_t
  675. xdr_array(xdrs, ap, lp, maxlength, elementsiz, xdr_element)
  676.     XDR *xdrs;
  677.     char **ap;
  678.     u_int *lp;
  679.     u_int maxlength;
  680.     u_int elementsiz;
  681.     bool_t (*xdr_element)();
  682. .Lf
  683. The parameter
  684. .LW ap
  685. is the address of the pointer to the array.
  686. If
  687. .LW *ap
  688. is
  689. .LW NULL
  690. when the array is being deserialized,
  691. XDR allocates an array of the appropriate size and sets
  692. .LW *ap
  693. to that array.
  694. The element count of the array is obtained from
  695. .LW *lp
  696. when the array is serialized;
  697. .LW *lp
  698. is set to the array length when the array is deserialized. 
  699. The parameter
  700. .LW maxlength
  701. is the maximum number of elements that the array is allowed to have;
  702. .LW elementsiz
  703. is the byte size of each element of the array
  704. (the C function
  705. .LW sizeof()
  706. can be used to obtain this value).
  707. The routine
  708. .LW xdr_element
  709. is called to serialize, deserialize, or free
  710. each element of the array.
  711. .NH 4
  712. Examples
  713. .LP
  714. Before defining more constructed data types,
  715. it is appropriate to present three examples.
  716. .LP
  717. .I "Example A"
  718. .LP
  719. A user on a networked machine can be identified by 
  720. (a) the machine name, such as
  721. .LW krypton :
  722. see
  723. .I gethostname (3);
  724. (b) the user's UID: see
  725. .I geteuid (2);
  726. and (c) the group numbers to which the user belongs: see
  727. .I getgroups (2).
  728. A structure with this information and its associated XDR routine
  729. could be coded like this:
  730. .LS
  731. struct netuser {
  732.     char    *nu_machinename;
  733.     int     nu_uid;
  734.     u_int    nu_glen;
  735.     int     *nu_gids;
  736. };
  737. #define NLEN 255    /* machine names < 256 chars */
  738. #define NGRPS 20    /* user can't be in > 20 groups */
  739. .sp.5
  740. bool_t
  741. xdr_netuser(xdrs, nup)
  742.     XDR *xdrs;
  743.     struct netuser *nup;
  744. {
  745.     return(xdr_string(xdrs, &nup->nu_machinename, NLEN) &&
  746.         xdr_int(xdrs, &nup->nu_uid) &&
  747.         xdr_array(xdrs, &nup->nu_gids, &nup->nu_glen, NGRPS,
  748.         sizeof (int), xdr_int));
  749. }
  750. .Lf
  751. .LP
  752. .I "Example B"
  753. .LP
  754. A party of network users could be implemented
  755. as an array of
  756. .LW netuser
  757. structure.
  758. The declaration and its associated XDR routines
  759. are as follows:
  760. .LS
  761. struct party {
  762.     u_int p_len;
  763.     struct netuser *p_nusers;
  764. };
  765. #define PLEN 500 /* max number of users in a party */
  766. .sp.5
  767. bool_t
  768. xdr_party(xdrs, pp)
  769.     XDR *xdrs;
  770.     struct party *pp;
  771. {
  772.     return(xdr_array(xdrs, &pp->p_nusers, &pp->p_len, PLEN,
  773.         sizeof (struct netuser), xdr_netuser));
  774. }
  775. .Lf
  776. .LP
  777. .I "Example C"
  778. .LP
  779. The well-known parameters to
  780. .LW main() ,
  781. .LW argc
  782. and
  783. .LW argv
  784. can be combined into a structure.
  785. An array of these structures can make up a history of commands.
  786. The declarations and XDR routines might look like:
  787. .LS no
  788. struct cmd {
  789.     u_int c_argc;
  790.     char **c_argv;
  791. };
  792. #define ALEN 1000    /* args cannot be > 1000 chars */
  793. #define NARGC 100    /* commands cannot have > 100 args */
  794. .sp.5
  795. struct history {
  796.     u_int h_len;
  797.     struct cmd *h_cmds;
  798. };
  799. #define NCMDS 75  /* history is no more than 75 commands */
  800. .sp.5
  801. bool_t
  802. xdr_wrap_string(xdrs, sp)
  803.     XDR *xdrs;
  804.     char **sp;
  805. {
  806.     return(xdr_string(xdrs, sp, ALEN));
  807. }
  808. .sp.5
  809. bool_t
  810. xdr_cmd(xdrs, cp)
  811.     XDR *xdrs;
  812.     struct cmd *cp;
  813. {
  814.     return(xdr_array(xdrs, &cp->c_argv, &cp->c_argc, NARGC,
  815.         sizeof (char *), xdr_wrap_string));
  816. }
  817. .sp.5
  818. bool_t
  819. xdr_history(xdrs, hp)
  820.     XDR *xdrs;
  821.     struct history *hp;
  822. {
  823.     return(xdr_array(xdrs, &hp->h_cmds, &hp->h_len, NCMDS,
  824.         sizeof (struct cmd), xdr_cmd));
  825. }
  826. .Lf
  827. The most confusing part of this example is that the routine
  828. .LW xdr_wrap_string()
  829. is needed to package the
  830. .LW xdr_string()
  831. routine, because the implementation of
  832. .LW xdr_array()
  833. only passes two parameters to the array element description routine;
  834. .LW xdr_wrap_string()
  835. supplies the third parameter to
  836. .LW xdr_string() .
  837. .LP
  838. By now the recursive nature of the XDR library should be obvious.
  839. Let's continue with more constructed data types.
  840. .NH 3
  841. Opaque Data
  842. .LP
  843. In some protocols, handles are passed from a server to client.
  844. The client passes the handle back to the server at some later time.
  845. Handles are never inspected by clients;
  846. they are obtained and submitted.
  847. That is to say, handles are opaque.
  848. The primitive
  849. .LW xdr_opaque()
  850. is used for describing fixed sized, opaque bytes.
  851. .LS
  852. bool_t xdr_opaque(xdrs, p, len)
  853.     XDR *xdrs;
  854.     char *p;
  855.     u_int len;
  856. .Lf
  857. The parameter
  858. .LW p
  859. is the location of the bytes;
  860. .LW len
  861. is the number of bytes in the opaque object.
  862. By definition, the actual data
  863. contained in the opaque object are not machine portable.
  864. .NH 3
  865. Fixed Sized Arrays
  866. .LP
  867. The XDR library does not provide a primitive for fixed-length arrays
  868. (the primitive
  869. .LW xdr_array()
  870. is for varying-length arrays).
  871. Example A could be rewritten to use fixed-sized arrays
  872. in the following fashion:
  873. .LS no
  874. #define NLEN 255    /* machine names must be < 256 chars */
  875. #define NGRPS 20    /* user can't belong to > 20 groups */
  876. .sp.5
  877. struct netuser {
  878.     char *nu_machinename;
  879.     int nu_uid;
  880.     int nu_gids[NGRPS];
  881. };
  882. .sp.5
  883. bool_t
  884. xdr_netuser(xdrs, nup)
  885.     XDR *xdrs;
  886.     struct netuser *nup;
  887. {
  888.     int i;
  889. .sp.5
  890.     if (!xdr_string(xdrs, &nup->nu_machinename, NLEN))
  891.         return(FALSE);
  892.     if (!xdr_int(xdrs, &nup->nu_uid))
  893.         return(FALSE);
  894.     for (i = 0; i < NGRPS; i++) {
  895.         if (!xdr_int(xdrs, &nup->nu_gids[i]))
  896.             return(FALSE);
  897.     }
  898.     return(TRUE);
  899. }
  900. .Lf
  901. .LP
  902. Exercise:
  903. Rewrite example A so that it uses varying-length arrays and so that the
  904. .LW netuser
  905. structure contains the actual
  906. .LW nu_gids
  907. array body as in the example above.
  908. .NH 3
  909. Discriminated Unions
  910. .LP
  911. The XDR library supports discriminated unions.
  912. A discriminated union is a C union and an
  913. .LW enum_t
  914. value that selects an ``arm'' of the union.
  915. .LS
  916. struct xdr_discrim {
  917.     enum_t value;
  918.     bool_t (*proc)();
  919. };
  920. .sp.5
  921. bool_t xdr_union(xdrs, dscmp, unp, arms, defaultarm)
  922.     XDR *xdrs;
  923.     enum_t *dscmp;
  924.     char *unp;
  925.     struct xdr_discrim *arms;
  926.     bool_t (*defaultarm)();  /* may equal NULL */
  927. .Lf
  928. First the routine translates the discriminant of the union located at 
  929. .LW *dscmp .
  930. The discriminant is always an
  931. .LW enum_t .
  932. Next the union located at
  933. .LW *unp
  934. is translated.
  935. The parameter
  936. .LW arms
  937. is a pointer to an array of
  938. .LW xdr_discrim
  939. structures. 
  940. Each structure contains an order pair of
  941. .LW [value,proc] .
  942. If the union's discriminant is equal to the associated
  943. .LW value ,
  944. then the
  945. .LW proc
  946. is called to translate the union.
  947. The end of the
  948. .LW xdr_discrim
  949. structure array is denoted by a routine of value
  950. .LW NULL
  951. (0).  If the discriminant is not found in the
  952. .LW arms
  953. array, then the
  954. .LW defaultarm
  955. procedure is called if it is non-null;
  956. otherwise the routine returns
  957. .LW FALSE .
  958. .LP
  959. .I "Example D"
  960. .LP
  961. Suppose the type of a union may be integer,
  962. character pointer (a string), or a
  963. .LW gnumbers
  964. structure.
  965. Also, assume the union and its current type
  966. are declared in a structure.
  967. The declaration is:
  968. .LS
  969. enum utype { INTEGER=1, STRING=2, GNUMBERS=3 };
  970. .sp.5
  971. struct u_tag {
  972.     enum utype utype;    /* the union's discriminant */
  973.     union {
  974.         int ival;
  975.         char *pval;
  976.         struct gnumbers gn;
  977.     } uval;
  978. };
  979. .Lf
  980. The following constructs and XDR procedure (de)serialize
  981. the discriminated union:
  982. .LS
  983. struct xdr_discrim u_tag_arms[4] = {
  984.     { INTEGER, xdr_int },
  985.     { GNUMBERS, xdr_gnumbers }
  986.     { STRING, xdr_wrap_string },
  987.     { __dontcare__, NULL }
  988.     /* always terminate arms with a NULL xdr_proc */
  989. }
  990. .sp.5
  991. bool_t
  992. xdr_u_tag(xdrs, utp)
  993.     XDR *xdrs;
  994.     struct u_tag *utp;
  995. {
  996.     return(xdr_union(xdrs, &utp->utype, &utp->uval,
  997.         u_tag_arms, NULL));
  998. }
  999. .Lf
  1000. The routine
  1001. .LW xdr_gnumbers()
  1002. was presented in Section 2;
  1003. .LW xdr_wrap_string()
  1004. was presented in example C.
  1005. The default arm parameter to
  1006. .LW xdr_union()
  1007. (the last parameter) is
  1008. .LW NULL
  1009. in this example.  Therefore the value of the union's discriminant
  1010. may legally take on only values listed in the
  1011. .LW u_tag_arms
  1012. array.  This example also demonstrates that
  1013. the elements of the arm's array do not need to be sorted.
  1014. .LP
  1015. It is worth pointing out that the values of the discriminant
  1016. may be sparse, though in this example they are not.
  1017. It is always good
  1018. practice to assign explicitly integer values to each element of the
  1019. discriminant's type.
  1020. This practice both documents the external
  1021. representation of the discriminant and guarantees that different
  1022. C compilers emit identical discriminant values.
  1023. .LP
  1024. Exercise: Implement
  1025. .LW xdr_union()
  1026. using the other primitives in this section.
  1027. .NH 3
  1028. Pointers
  1029. .LP
  1030. In C it is often convenient to put pointers
  1031. to another structure within a structure.
  1032. The primitive
  1033. .LW xdr_reference()
  1034. makes it easy to serialize, deserialize, and free
  1035. these referenced structures.
  1036. .LS
  1037. bool_t xdr_reference(xdrs, pp, size, proc)
  1038.     XDR *xdrs;
  1039.     char **pp;
  1040.     u_int ssize;
  1041.     bool_t (*proc)();
  1042. .Lf
  1043. .LP
  1044. Parameter
  1045. .LW pp
  1046. is the address of
  1047. the pointer to the structure;
  1048. parameter
  1049. .LW ssize
  1050. is the size in bytes of the structure
  1051. (use the C function
  1052. .LW sizeof()
  1053. to obtain this value); and
  1054. .LW proc
  1055. is the XDR routine that describes the structure.
  1056. When decoding data, storage is allocated if
  1057. .LW *pp
  1058. is
  1059. .LW NULL .
  1060. .LP
  1061. There is no need for a primitive
  1062. .LW xdr_struct()
  1063. to describe structures within structures,
  1064. because pointers are always sufficient.
  1065. .LP
  1066. Exercise: Implement
  1067. .LW xdr_reference()
  1068. using
  1069. .LW xdr_array() .
  1070. Warning:
  1071. .LW xdr_reference()
  1072. and
  1073. .LW xdr_array()
  1074. are NOT interchangeable external representations of data.
  1075. .LP
  1076. .I "Example E"
  1077. .LP
  1078. Suppose there is a structure containing a person's name
  1079. and a pointer to a
  1080. .LW gnumbers
  1081. structure containing the person's gross assets and liabilities.
  1082. The construct is:
  1083. .LS
  1084. struct pgn {
  1085.     char *name;
  1086.     struct gnumbers *gnp;
  1087. };
  1088. .Lf
  1089. The corresponding XDR routine for this structure is:
  1090. .LS
  1091. bool_t
  1092. xdr_pgn(xdrs, pp)
  1093.     XDR *xdrs;
  1094.     struct pgn *pp;
  1095. {
  1096.     if (xdr_string(xdrs, &pp->name, NLEN) &&
  1097.       xdr_reference(xdrs, &pp->gnp,
  1098.       sizeof(struct gnumbers), xdr_gnumbers))
  1099.         return(TRUE);
  1100.     return(FALSE);
  1101. }
  1102. .Lf
  1103. .NH 4
  1104. Pointer Semantics and XDR
  1105. .LP
  1106. In many applications,
  1107. C programmers attach double meaning to the values of a pointer.
  1108. Typically the value
  1109. .LW NULL
  1110. (or zero) means data is not needed,
  1111. yet some application-specific interpretation applies.
  1112. In essence, the C programmer is encoding
  1113. a discriminated union efficiently
  1114. by overloading the interpretation of the value of a pointer.
  1115. For instance, in example E a
  1116. .LW NULL
  1117. pointer value for
  1118. .LW gnp
  1119. could indicate that
  1120. the person's assets and liabilities are unknown.
  1121. That is, the pointer value encodes two things:
  1122. whether or not the data is known;
  1123. and if it is known, where it is located in memory.
  1124. Linked lists are an extreme example of the use
  1125. of application-specific pointer interpretation.
  1126. .LP
  1127. The primitive
  1128. .LW xdr_reference()
  1129. cannot and does not attach any special
  1130. meaning to a null-value pointer during serialization.
  1131. That is, passing an address of a pointer whose value is
  1132. .LW NULL
  1133. to
  1134. .LW xdr_reference()
  1135. when serialing data will most likely cause a memory fault and, on
  1136. .UX ,
  1137. a core dump for debugging.
  1138. .LP
  1139. It is the explicit responsibility of the programmer
  1140. to expand non-dereferenceable pointers into their specific semantics.
  1141. This usually involves describing data with a two-armed discriminated union.
  1142. One arm is used when the pointer is valid;
  1143. the other is used when the pointer is invalid
  1144. .LW NULL ). (
  1145. Section 7 has an example (linked lists encoding) that deals
  1146. with invalid pointer interpretation.
  1147. .LP
  1148. Exercise:
  1149. After reading Section 7, return here and extend example E so that
  1150. it can correctly deal with null pointer values.
  1151. .LP
  1152. Exercise:
  1153. Using the
  1154. .LW xdr_union() ,
  1155. .LW xdr_reference()
  1156. and
  1157. .LW xdr_void()
  1158. primitives, implement a generic pointer handling primitive
  1159. that implicitly deals with
  1160. .LW NULL
  1161. pointers.  The XDR library does not provide such a primitive
  1162. because it does not want to give the illusion
  1163. that pointers have meaning in the external world.
  1164. .NH 2
  1165. Non-filter Primitives
  1166. .LP
  1167. XDR streams can be manipulated with
  1168. the primitives discussed in this section.
  1169. .LS
  1170. u_int xdr_getpos(xdrs)
  1171.     XDR *xdrs;
  1172. .sp.5
  1173. bool_t xdr_setpos(xdrs, pos)
  1174.     XDR *xdrs;
  1175.     u_int pos;
  1176. .sp.5
  1177. xdr_destroy(xdrs)
  1178.     XDR *xdrs;
  1179. .Lf
  1180. The routine
  1181. .LW xdr_getpos()
  1182. returns an unsigned integer
  1183. that describes the current position in the data stream.
  1184. Warning: In some XDR streams, the returned value of
  1185. .LW xdr_getpos()
  1186. is meaningless;
  1187. the routine returns a \-1 in this case
  1188. (though \-1 should be a legitimate value).
  1189. .LP
  1190. The routine
  1191. .LW xdr_setpos()
  1192. sets a stream position to
  1193. .LW pos .
  1194. Warning: In some XDR streams, setting a position is impossible;
  1195. in such cases,
  1196. .LW xdr_setpos()
  1197. will return
  1198. .LW FALSE .
  1199. This routine will also fail if the requested position is out-of-bounds.
  1200. The definition of bounds varies from stream to stream.
  1201. .LP
  1202. The
  1203. .LW xdr_destroy()
  1204. primitive destroys the XDR stream.
  1205. Usage of the stream
  1206. after calling this routine is undefined.
  1207. .NH 2
  1208. XDR Operation Directions
  1209. .LP
  1210. At times you may wish to optimize XDR routines by taking
  1211. advantage of the direction of the operation \(em
  1212. .LW XDR_ENCODE ,
  1213. .LW XDR_DECODE ,
  1214. or
  1215. .LW XDR_FREE .
  1216. The value
  1217. .LW xdrs->x_op
  1218. always contains the
  1219. direction of the XDR operation.
  1220. Programmers are not encouraged to take advantage of this information.
  1221. Therefore, no example is presented here.
  1222. However, an example in Section 7
  1223. demonstrates the usefulness of the
  1224. .LW xdrs->x_op
  1225. field.
  1226. .bp
  1227. .NH
  1228. XDR Stream Access
  1229. .LP
  1230. An XDR stream is obtained by calling the appropriate creation routine.
  1231. These creation routines take arguments that are tailored to the
  1232. specific properties of the stream.
  1233. .LP
  1234. Streams currently exist for (de)serialization of data to or from
  1235. standard I/O
  1236. .LW FILE
  1237. streams, TCP/IP connections and
  1238. .UX
  1239. files, and memory.
  1240. Section 5 documents the XDR object and how to make
  1241. new XDR streams when they are required.
  1242. .NH 2
  1243. Standard I/O Streams
  1244. .LP
  1245. XDR streams can be interfaced to standard I/O using the
  1246. .LW xdrstdio_create()
  1247. routine as follows:
  1248. .LS
  1249. #include <stdio.h>
  1250. #include <rpc/rpc.h>    /* xdr streams part of rpc */
  1251. .sp.5
  1252. void
  1253. xdrstdio_create(xdrs, fp, x_op)
  1254.     XDR *xdrs;
  1255.     FILE *fp;
  1256.     enum xdr_op x_op;
  1257. .Lf
  1258. The routine
  1259. .LW xdrstdio_create()
  1260. initializes an XDR stream pointed to by
  1261. .LW xdrs .
  1262. The XDR stream interfaces to the standard I/O library.
  1263. Parameter
  1264. .LW fp
  1265. is an open file, and
  1266. .LW x_op
  1267. is an XDR direction.
  1268. .NH 2
  1269. Memory Streams
  1270. .LP
  1271. Memory streams allow the streaming of data into or out of
  1272. a specified area of memory:
  1273. .LS
  1274. #include <rpc/rpc.h>
  1275. .sp.5
  1276. void
  1277. xdrmem_create(xdrs, addr, len, x_op)
  1278.     XDR *xdrs;
  1279.     char *addr;
  1280.     u_int len;
  1281.     enum xdr_op x_op;
  1282. .Lf
  1283. The routine
  1284. .LW xdrmem_create()
  1285. initializes an XDR stream in local memory.
  1286. The memory is pointed to by parameter
  1287. .LW addr ;
  1288. parameter
  1289. .LW len
  1290. is the length in bytes of the memory.
  1291. The parameters
  1292. .LW xdrs
  1293. and
  1294. .LW x_op
  1295. are identical to the corresponding parameters of
  1296. .LW xdrstdio_create() .
  1297. Currently, the UDP/IP implementation of RPC uses
  1298. .LW xdrmem_create() .
  1299. Complete call or result messages are built in memory before calling the
  1300. .LW sendto()
  1301. system routine.
  1302. .NH 2
  1303. Record (TCP/IP) Streams
  1304. .LP
  1305. A record stream is an XDR stream built on top of
  1306. a record marking standard that is built on top of the
  1307. .UX
  1308. file or 4.2 BSD connection interface.
  1309. .LS
  1310. #include <rpc/rpc.h>    /* xdr streams part of rpc */
  1311. .sp.5
  1312. xdrrec_create(xdrs,
  1313.   sendsize, recvsize, iohandle, readproc, writeproc)
  1314.     XDR *xdrs;
  1315.     u_int sendsize, recvsize;
  1316.     char *iohandle;
  1317.     int (*readproc)(), (*writeproc)();
  1318. .Lf
  1319. The routine
  1320. .LW xdrrec_create()
  1321. provides an XDR stream interface that allows for a bidirectional,
  1322. arbitrarily long sequence of records.
  1323. The contents of the records are meant to be data in XDR form.
  1324. The stream's primary use is for interfacing RPC to TCP connections.
  1325. However, it can be used to stream data into or out of normal
  1326. .UX
  1327. files.
  1328. .LP
  1329. The parameter
  1330. .LW xdrs
  1331. is similar to the corresponding parameter described above.
  1332. The stream does its own data buffering similar to that of standard I/O.
  1333. The parameters
  1334. .LW sendsize
  1335. and
  1336. .LW recvsize
  1337. determine the size in bytes of the output and input buffers, respectively;
  1338. if their values are zero (0), then predetermined defaults are used.
  1339. When a buffer needs to be filled or flushed, the routine
  1340. .LW readproc
  1341. or
  1342. .LW writeproc
  1343. is called, respectively.
  1344. The usage and behavior of these
  1345. routines are similar to the
  1346. .UX
  1347. system calls
  1348. .LW read()
  1349. and
  1350. .LW write() .
  1351. However,
  1352. the first parameter to each of these routines is the opaque parameter
  1353. .LW iohandle .
  1354. The other two parameters
  1355. .LW buf "" (
  1356. and
  1357. .LW nbytes )
  1358. and the results
  1359. (byte count) are identical to the system routines.
  1360. If
  1361. .LW xxx
  1362. is
  1363. .LW readproc
  1364. or
  1365. .LW writeproc ,
  1366. then it has the following form:
  1367. .LS
  1368. /*
  1369.  * returns the actual number of bytes transferred.
  1370.  * -1 is an error
  1371.  */
  1372. int
  1373. xxx(iohandle, buf, len)
  1374.     char *iohandle;
  1375.     char *buf;
  1376.     int nbytes;
  1377. .Lf
  1378. The XDR stream provides means for delimiting records in the byte stream.
  1379. The implementation details of delimiting records in a stream
  1380. are discussed in appendix 1.
  1381. The primitives that are specific to record streams are as follows:
  1382. .LS
  1383. bool_t
  1384. xdrrec_endofrecord(xdrs, flushnow)
  1385.     XDR *xdrs;
  1386.     bool_t flushnow;
  1387. .sp.5
  1388. bool_t
  1389. xdrrec_skiprecord(xdrs)
  1390.     XDR *xdrs;
  1391. .sp.5
  1392. bool_t
  1393. xdrrec_eof(xdrs)
  1394.     XDR *xdrs;
  1395. .Lf
  1396. The routine
  1397. .LW xdrrec_endofrecord()
  1398. causes the current outgoing data to be marked as a record.
  1399. If the parameter
  1400. .LW flushnow
  1401. is
  1402. .LW TRUE ,
  1403. then the stream's
  1404. .LW writeproc()
  1405. will be called; otherwise,
  1406. .LW writeproc()
  1407. will be called when the output buffer has been filled.
  1408. .LP
  1409. The routine
  1410. .LW xdrrec_skiprecord()
  1411. causes an input stream's position to be moved past
  1412. the current record boundary and onto the
  1413. beginning of the next record in the stream.
  1414. .LP
  1415. If there is no more data in the stream's input buffer,
  1416. then the routine
  1417. .LW xdrrec_eof()
  1418. returns
  1419. .LW TRUE .
  1420. That is not to say that there is no more data
  1421. in the underlying file descriptor.
  1422. .bp
  1423. .NH
  1424. XDR Stream Implementation
  1425. .LP
  1426. This section provides the abstract data types needed
  1427. to implement new instances of XDR streams.
  1428. .NH 2
  1429. The XDR Object
  1430. .LP
  1431. The following structure defines the interface to an XDR stream:
  1432. .LS
  1433. enum xdr_op { XDR_ENCODE=0, XDR_DECODE=1, XDR_FREE=2 };
  1434. .sp.5
  1435. typedef struct {
  1436.     enum xdr_op x_op;    /* operation; fast added param */
  1437.     struct xdr_ops {
  1438.         bool_t  (*x_getlong)();  /* get long from stream */
  1439.         bool_t  (*x_putlong)();  /* put long to stream */
  1440.         bool_t  (*x_getbytes)(); /* get bytes from stream */
  1441.         bool_t  (*x_putbytes)(); /* put bytes to stream */
  1442.         u_int   (*x_getpostn)(); /* return stream offset */
  1443.         bool_t  (*x_setpostn)(); /* reposition offset */
  1444.         caddr_t (*x_inline)();   /* ptr to buffered data */
  1445.         VOID    (*x_destroy)();  /* free private area */
  1446.     } *x_ops;
  1447.     caddr_t    x_public;    /* users' data */
  1448.     caddr_t    x_private;    /* pointer to private data */
  1449.     caddr_t    x_base;        /* private for position info */
  1450.     int        x_handy;    /* extra private word */
  1451. } XDR;
  1452. .Lf
  1453. The
  1454. .LW x_op
  1455. field is the current operation being performed on the stream.
  1456. This field is important to the XDR primitives,
  1457. but should not affect a stream's implementation.
  1458. That is, a stream's implementation should not depend
  1459. on this value.
  1460. The fields
  1461. .LW x_private ,
  1462. .LW x_base ,
  1463. and
  1464. .LW x_handy
  1465. are private to the particular
  1466. stream's implementation.
  1467. The field
  1468. .LW x_public
  1469. is for the XDR client and should never be used by
  1470. the XDR stream implementations or the XDR primitives.
  1471. .LP
  1472. Macros for accessing  operations
  1473. .LW x_getpostn() ,
  1474. .LW x_setpostn() ,
  1475. and
  1476. .LW x_destroy()
  1477. were defined in Section 3.6.
  1478. The operation
  1479. .LW x_inline()
  1480. takes two parameters:
  1481. an XDR *, and an unsigned integer, which is a byte count.
  1482. The routine returns a pointer to a piece of
  1483. the stream's internal buffer.
  1484. The caller can then use the buffer segment for any purpose.
  1485. From the stream's point of view, the bytes in the
  1486. buffer segment have been consumed or put.
  1487. The routine may return
  1488. .LW NULL
  1489. if it cannot return a buffer segment of the requested size.
  1490. (The
  1491. .LW x_inline
  1492. routine is for cycle squeezers.
  1493. Use of the resulting buffer is not data-portable.
  1494. Users are encouraged not to use this feature.) 
  1495. .LP
  1496. The operations
  1497. .LW x_getbytes()
  1498. and
  1499. .LW x_putbytes()
  1500. blindly get and put sequences of bytes
  1501. from or to the underlying stream;
  1502. they return
  1503. .LW TRUE
  1504. if they are successful, and
  1505. .LW FALSE
  1506. otherwise.  The routines have identical parameters (replace
  1507. .LW xxx ):
  1508. .LS
  1509. bool_t
  1510. xxxbytes(xdrs, buf, bytecount)
  1511.     XDR *xdrs;
  1512.     char *buf;
  1513.     u_int bytecount;
  1514. .Lf
  1515. The operations
  1516. .LW x_getlong()
  1517. and
  1518. .LW x_putlong()
  1519. receive and put
  1520. long numbers from and to the data stream.
  1521. It is the responsibility of these routines
  1522. to translate the numbers between the machine representation
  1523. and the (standard) external representation.
  1524. The
  1525. .UX
  1526. primitives
  1527. .LW htonl()
  1528. and
  1529. .LW ntohl()
  1530. can be helpful in accomplishing this.
  1531. Section 6 defines the standard representation of numbers.
  1532. The higher-level XDR implementation assumes that
  1533. signed and unsigned long integers contain the same number of bits,
  1534. and that nonnegative integers
  1535. have the same bit representations as unsigned integers.
  1536. The routines return
  1537. .I TRUE
  1538. if they succeed, and
  1539. .LW FALSE
  1540. otherwise.  They have identical parameters:
  1541. .LS
  1542. bool_t
  1543. xxxlong(xdrs, lp)
  1544.     XDR *xdrs;
  1545.     long *lp;
  1546. .Lf
  1547. Implementors of new XDR streams must make an XDR structure
  1548. (with new operation routines) available to clients,
  1549. using some kind of create routine.
  1550. .bp
  1551. .NH
  1552. XDR Standard
  1553. .LP
  1554. This section defines the external data representation standard.
  1555. The standard is independent of languages,
  1556. operating systems and hardware architectures.
  1557. Once data is shared among machines, it should not matter that the data
  1558. was produced on a Sun, but is consumed by a VAX (or vice versa).
  1559. Similarly the choice of operating systems should have no influence
  1560. on how the data is represented externally.
  1561. For programming languages,
  1562. data produced by a C program should be readable
  1563. by a Fortran or Pascal program.
  1564. .LP
  1565. The external data representation standard depends on the assumption that
  1566. bytes (or octets) are portable.
  1567. A byte is defined to be eight bits of data.
  1568. It is assumed that hardware
  1569. that encodes bytes onto various media
  1570. will preserve the bytes' meanings
  1571. across hardware boundaries.
  1572. For example, the Ethernet standard suggests that bytes be
  1573. encoded ``little endian'' style.
  1574. Both Sun and VAX hardware implementations
  1575. adhere to the standard.
  1576. .LP
  1577. The XDR standard also suggests a language used to describe data.
  1578. The language is a bastardized C;
  1579. it is a data description language, not a programming language.
  1580. (The Xerox Courier Standard uses bastardized Mesa
  1581. as its data description language.)
  1582. .NH 2
  1583. Basic Block Size
  1584. .LP
  1585. The representation of all items requires
  1586. a multiple of four bytes (or 32 bits) of data.
  1587. The bytes are numbered
  1588. $0$ through $n-1$, where $(n ~ \fRmod\fP ~ 4) = 0$.
  1589. The bytes are read or written to some byte stream
  1590. such that byte $m$ always precedes byte $m+1$.
  1591. .NH 2
  1592. Integer
  1593. .LP
  1594. An XDR signed integer is a 32-bit datum
  1595. that encodes an integer in the range
  1596. .LW [-2147483648,2147483647] . 
  1597. The integer is represented in two's complement notation. 
  1598. The most and least significant bytes are 0 and 3, respectively.
  1599. The data description of integers is
  1600. .LW integer .
  1601. .NH 2
  1602. Unsigned Integer
  1603. .LP
  1604. An XDR unsigned integer is a 32-bit datum
  1605. that encodes a nonnegative integer in the range
  1606. .LW [0,4294967295] .
  1607. It is represented by an unsigned binary number whose most
  1608. and least significant bytes are 0 and 3, respectively.
  1609. The data description of unsigned integers is
  1610. .LW unsigned .
  1611. .NH 2
  1612. Enumerations
  1613. .LP
  1614. Enumerations have the same representation as integers.
  1615. Enumerations are handy for describing subsets of the integers.
  1616. The data description of enumerated data is as follows:
  1617. .LS
  1618. typedef enum { name = value, .... } type-name;
  1619. .Lf
  1620. For example the three colors red, yellow and blue
  1621. could be described by an enumerated type:
  1622. .LS
  1623. typedef enum { RED = 2, YELLOW = 3, BLUE = 5 } colors;
  1624. .Lf
  1625. .NH 2
  1626. Booleans
  1627. .LP
  1628. Booleans are important enough and occur frequently enough
  1629. to warrant their own explicit type in the standard.
  1630. Boolean is an enumeration with the
  1631. following form:
  1632. .LS
  1633. typedef enum { FALSE = 0, TRUE = 1 } boolean;
  1634. .Lf
  1635. .NH 2
  1636. Hyper Integer and Hyper Unsigned
  1637. .LP
  1638. The standard also defines 64-bit (8-byte) numbers called 
  1639. .LW "hyper integer"
  1640. and
  1641. .LW "hyper unsigned" .
  1642. Their representations are the obvious extensions of 
  1643. the integer and unsigned defined above.
  1644. The most and least significant bytes are 0 and 7, respectively.
  1645. .NH 2
  1646. Floating Point and Double Precision
  1647. .LP
  1648. The standard defines the encoding for the floating point data types
  1649. .LW float
  1650. (32 bits or 4 bytes) and
  1651. .LW double
  1652. (64 bits or 8 bytes).
  1653. The encoding used is the IEEE standard for normalized
  1654. single- and double-precision floating point numbers.
  1655. See the IEEE floating point standard for more information.
  1656. The standard encodes the following three fields,
  1657. which describe the floating point number:
  1658. .IP \fIS\fP
  1659. The sign of the number.
  1660. Values 0 and 1 represent 
  1661. positive and negative, respectively.
  1662. .IP \fIE\fP
  1663. The exponent of the number, base 2.
  1664. Floats devote 8 bits to this field,
  1665. while doubles devote 11 bits.
  1666. The exponents for float and double are
  1667. biased by 127 and 1023, respectively.
  1668. .IP \fIF\fP
  1669. The fractional part of the number's mantissa, base 2.
  1670. Floats devote 23 bits to this field,
  1671. while doubles devote 52 bits.
  1672. .LP
  1673. Therefore, the floating point number is described by:
  1674. .EQ
  1675. (-1) sup S * 2 sup { E - Bias } * 1.F
  1676. .EN
  1677. .LP
  1678. Just as the most and least significant bytes of a number are 0 and 3,
  1679. the most and least significant bits of
  1680. a single-precision floating point number are 0 and 31.
  1681. The beginning bit (and most significant bit) offsets
  1682. of $S$, $E$, and $F$ are 0, 1, and 9, respectively.
  1683. .LP
  1684. Doubles have the analogous extensions.
  1685. The beginning bit (and
  1686. most significant bit) offsets of $S$, $E$, and $F$
  1687. are 0, 1, and 12, respectively.
  1688. .LP
  1689. The IEEE specification should be consulted concerning the encoding for
  1690. signed zero, signed infinity (overflow), and denormalized numbers (underflow).
  1691. Under IEEE specifications, the ``NaN'' (not a number)
  1692. is system dependent and should not be used.
  1693. .NH 2
  1694. Opaque Data
  1695. .LP
  1696. At times fixed-sized uninterpreted data
  1697. needs to be passed among machines.
  1698. This data is called
  1699. .LW opaque
  1700. and is described as:
  1701. .LS
  1702. typedef opaque type-name[n];
  1703. opaque name[n];
  1704. .Lf
  1705. where
  1706. .LW n
  1707. is the (static) number of bytes necessary to contain the opaque data.
  1708. If
  1709. .LW n
  1710. is not a multiple of four, then the
  1711. .LW n
  1712. bytes are followed by enough (up to 3) zero-valued bytes
  1713. to make the total byte count of the opaque object a multiple of four.
  1714. .NH 2
  1715. Counted Byte Strings
  1716. .LP
  1717. The standard defines a string of $n$ (numbered $0$ through $n-1$)
  1718. bytes to be the number $n$ encoded as
  1719. .LW unsigned ,
  1720. and followed by the $n$ bytes of the string.
  1721. If $n$ is not a multiple of four,
  1722. then the $n$ bytes are followed by
  1723. enough (up to 3) zero-valued bytes
  1724. to make the total byte count a multiple of four.
  1725. The data description of strings is as follows:
  1726. .LS
  1727. typedef string type-name<N>;
  1728. typedef string type-name<>;
  1729. string name<N>;
  1730. string name<>;
  1731. .Lf
  1732. Note that the data description language uses angle brackets (< and >)
  1733. to denote anything that is varying-length
  1734. (as opposed to square brackets to denote fixed-length sequences of data).
  1735. .LP
  1736. The constant
  1737. .LW N
  1738. denotes an upper bound of the number of bytes that a
  1739. string may contain.
  1740. If
  1741. .LW N
  1742. is not specified, it is assumed to be $2 sup 32 - 1$,
  1743. the maximum length.
  1744. The constant
  1745. .LW N
  1746. would normally be found in a protocol specification.
  1747. For example, a filing protocol may state
  1748. that a file name can be no longer than 255 bytes, such as:
  1749. .LS
  1750. string filename<255>;
  1751. .Lf
  1752. .LP
  1753. The XDR specification does not say what the
  1754. individual bytes of a string represent;
  1755. this important information is left to higher-level specifications.
  1756. A reasonable default is to assume
  1757. that the bytes encode ASCII characters.
  1758. .NH 2
  1759. Fixed Arrays
  1760. .LP
  1761. The data description for fixed-size arrays of
  1762. homogeneous elements is as follows:
  1763. .LS
  1764. typedef elementtype type-name[n];
  1765. elementtype name[n];
  1766. .Lf
  1767. Fixed-size arrays of elements numbered $0$ through $n-1$
  1768. are encoded by individually encoding the elements of the array
  1769. in their natural order, $0$ through $n-1$.
  1770. .NH 2
  1771. Counted Arrays
  1772. .LP
  1773. Counted arrays provide the ability to encode varyiable-length arrays
  1774. of homogeneous elements.
  1775. The array is encoded as:
  1776. the element count $n$ (an unsigned integer),
  1777. followed by the encoding of each of the array's elements,
  1778. starting with element $0$ and progressing through element $n-1$.
  1779. The data description for counted arrays
  1780. is similar to that of counted strings:
  1781. .LS
  1782. typedef elementtype type-name<N>;
  1783. typedef elementtype type-name<>;
  1784. elementtype name<N>;
  1785. elementtype name<>;
  1786. .Lf
  1787. Again, the constant
  1788. .LW N
  1789. specifies the maximum acceptable
  1790. element count of an array; if
  1791. .LW N
  1792. is  not specified, it is assumed to be $2 sup 32 - 1$.
  1793. .NH 2
  1794. Structures
  1795. .LP
  1796. The data description for structures is very similar to
  1797. that of standard C:
  1798. .LS
  1799. typedef struct {
  1800.     component-type component-name;
  1801.     ...
  1802. } type-name;
  1803. .Lf
  1804. The components of the structure are encoded 
  1805. in the order of their declaration in the structure.
  1806. .NH 2
  1807. Discriminated Unions
  1808. .LP
  1809. A discriminated union is a type composed of a discriminant followed by a type
  1810. selected from a set of prearranged types according to the value of the
  1811. discriminant.
  1812. The type of the discriminant is always an enumeration.
  1813. The component types are called ``arms'' of the union.
  1814. The discriminated union is encoded as its discriminant followed by
  1815. the encoding of the implied arm.
  1816. The data description for discriminated unions is as follows:
  1817. .LS
  1818. typedef union switch (discriminant-type) {
  1819.     discriminant-value: arm-type;
  1820.     ...
  1821.     default: default-arm-type;
  1822. } type-name;
  1823. .Lf
  1824. The default arm is optional.
  1825. If it is not specified, then a valid
  1826. encoding of the union cannot take on unspecified discriminant values.
  1827. Most specifications neither need nor use default arms.
  1828. .NH 2
  1829. Missing Specifications
  1830. .LP
  1831. The standard lacks representations for bit fields and bitmaps,
  1832. since the standard is based on bytes.
  1833. This is not to say that no specification should be attempted.
  1834. .NH 2
  1835. Library Primitive / XDR Standard Cross Reference
  1836. .LP
  1837. The following table describes the association between
  1838. the C library primitives discussed in Section 3,
  1839. and the standard data types defined in this section:
  1840. .LP
  1841. .TS
  1842. box;
  1843. cfBI s s
  1844. cfI cfI cfI
  1845. rfL | cfL | l.
  1846. .sp.1
  1847. Primitives and Data Types
  1848. .sp.1
  1849. _
  1850. C Primitive    XDR Type    Sections
  1851. _
  1852. xdr_int
  1853. xdr_long    integer    3.1, 6.2
  1854. xdr_short
  1855. _
  1856. xdr_u_int
  1857. xdr_u_long    unsigned    3.1, 6.3
  1858. xdr_u_short
  1859. _
  1860. -    hyper integer    6.6
  1861.     hyper unsigned
  1862. _
  1863. xdr_float    float    3.2, 6.7
  1864. _
  1865. xdr_double    double    3.2, 6.7
  1866. _
  1867. xdr_enum    enum_t    3.3, 6.4
  1868. _
  1869. xdr_bool    bool_t    3.3, 6.5
  1870. _
  1871. xdr_string    string    3.5.1, 6.9
  1872. xdr_bytes        3.5.2
  1873. _
  1874. xdr_array    \fR(varying arrays)\fP    3.5.3, 6.11
  1875. _
  1876. -    \fR(fixed arrays)\fP    3.5.5, 6.10
  1877. _
  1878. xdr_opaque    opaque    3.5.4, 6.8
  1879. _
  1880. xdr_union    union    3.5.6, 6.13
  1881. _
  1882. xdr_reference    -    3.5.7
  1883. _
  1884. -    struct    6.6
  1885. .TE
  1886. .TN "Primitives and Data Types"
  1887. .bp
  1888. .NH
  1889. Advanced Topics
  1890. .LP
  1891. This section describes techniques for passing data structures
  1892. that are not covered in the preceding sections.
  1893. Such structures include linked lists (of arbitrary lengths).
  1894. Unlike the simpler examples covered in the earlier sections,
  1895. the following examples are written using both
  1896. the XDR C library routines and the XDR data description language.
  1897. Section 6 describes the XDR data definition language used below.
  1898. .NH 2
  1899. Linked Lists
  1900. .LP
  1901. The last example in Section 2 presented a C data structure and its
  1902. associated XDR routines for a person's gross assets and liabilities.
  1903. The example is duplicated below:
  1904. .LS
  1905. struct gnumbers {
  1906.     long g_assets;
  1907.     long g_liabilities;
  1908. };
  1909. .sp.5
  1910. bool_t
  1911. xdr_gnumbers(xdrs, gp)
  1912.     XDR *xdrs;
  1913.     struct gnumbers *gp;
  1914. {
  1915.     if (xdr_long(xdrs, &(gp->g_assets)))
  1916.         return(xdr_long(xdrs, &(gp->g_liabilities)));
  1917.     return(FALSE);
  1918. }
  1919. .Lf
  1920. Now assume that we wish to implement a linked list of such information.
  1921. A data structure could be constructed as follows:
  1922. .LS
  1923. typedef struct gnnode {
  1924.     struct gnumbers gn_numbers;
  1925.     struct gnnode *nxt;
  1926. };
  1927. .sp.5
  1928. typedef struct gnnode *gnumbers_list;
  1929. .Lf
  1930. The head of the linked list can be thought of as the data object;
  1931. that is, the head is not merely a convenient shorthand for a structure.
  1932. Similarly the
  1933. .LW nxt
  1934. field is used to indicate whether or not the object has terminated.
  1935. Unfortunately, if the object continues, the
  1936. .LW nxt
  1937. field is also the address
  1938. of where it continues.
  1939. The link addresses carry no useful information when
  1940. the object is serialized.
  1941. .LP
  1942. The XDR data description of this linked list is described by the
  1943. recursive type declaration of gnumbers_list:
  1944. .LS
  1945. struct gnumbers {
  1946.     unsigned g_assets;
  1947.     unsigned g_liabilities;
  1948. };
  1949. .Lf
  1950. .LS
  1951. typedef union switch (boolean) {
  1952.     case TRUE: struct {
  1953.         struct gnumbers current_element;
  1954.         gnumbers_list rest_of_list;
  1955.     };
  1956.     case FALSE: struct {};
  1957. } gnumbers_list;
  1958. .Lf
  1959. In this description,
  1960. the boolean indicates whether there is more data following it.
  1961. If the boolean is
  1962. .LW FALSE ,
  1963. then it is the last data field of the structure.
  1964. If it is
  1965. .LW TRUE ,
  1966. then it is followed by a
  1967. .LW gnumbers
  1968. structure and (recursively) by a
  1969. .LW gnumbers_list
  1970. (the rest of the object).
  1971. Note that the C declaration has no boolean explicitly declared in it
  1972. (though the
  1973. .LW nxt
  1974. field implicitly carries the information), while
  1975. the XDR data description has no pointer explicitly declared in it.
  1976. .LP
  1977. Hints for writing a set of XDR routines to successfully (de)serialize
  1978. a linked list of entries can be taken
  1979. from the XDR description of the pointer-less data.
  1980. The set consists of the mutually recursive routines
  1981. .LW xdr_gnumbers_list ,
  1982. .LW xdr_wrap_list ,
  1983. and
  1984. .LW xdr_gnnode .
  1985. .LS
  1986. bool_t
  1987. xdr_gnnode(xdrs, gp)
  1988.     XDR *xdrs;
  1989.     struct gnnode *gp;
  1990. {
  1991.     return(xdr_gnumbers(xdrs, &(gp->gn_numbers)) &&
  1992.         xdr_gnumbers_list(xdrs, &(gp->nxt)) );
  1993. }
  1994. .Lf
  1995.