home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / rpc3.9 / part13 / xdr.nts.ms
Encoding:
Text File  |  1988-02-27  |  48.5 KB  |  1,976 lines

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