home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume1 / rpc / part04 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  54.7 KB

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