xdr_rec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /* @(#)xdr_rec.c 2.2 88/08/01 4.0 RPCSRC */
  2. /*
  3. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4. * unrestricted use provided that this legend is included on all tape
  5. * media and as a part of the software program in whole or part. Users
  6. * may copy or modify Sun RPC without charge, but are not authorized
  7. * to license or distribute it to anyone else except as part of a product or
  8. * program developed by the user.
  9. *
  10. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13. *
  14. * Sun RPC is provided with no support and without any obligation on the
  15. * part of Sun Microsystems, Inc. to assist in its use, correction,
  16. * modification or enhancement.
  17. *
  18. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20. * OR ANY PART THEREOF.
  21. *
  22. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23. * or profits or other special, indirect and consequential damages, even if
  24. * Sun has been advised of the possibility of such damages.
  25. *
  26. * Sun Microsystems, Inc.
  27. * 2550 Garcia Avenue
  28. * Mountain View, California 94043
  29. */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * xdr_rec.c, Implements TCP/IP based XDR streams with a "record marking"
  35. * layer above tcp (for rpc's use).
  36. *
  37. * Copyright (C) 1984, Sun Microsystems, Inc.
  38. *
  39. * These routines interface XDRSTREAMS to a tcp/ip connection.
  40. * There is a record marking layer between the xdr stream
  41. * and the tcp transport level. A record is composed on one or more
  42. * record fragments. A record fragment is a thirty-two bit header followed
  43. * by n bytes of data, where n is contained in the header. The header
  44. * is represented as a htonl(u_long). Thegh order bit encodes
  45. * whether or not the fragment is the last fragment of the record
  46. * (1 => fragment is last, 0 => more fragments to follow.
  47. * The other 31 bits encode the byte length of the fragment.
  48. */
  49. #include <stdio.h>
  50. #include <rpc/types.h>
  51. #include <rpc/xdr.h>
  52. #include <netinet/in.h>
  53. extern long lseek();
  54. static u_int fix_buf_size();
  55. static bool_t xdrrec_getlong();
  56. static bool_t xdrrec_putlong();
  57. static bool_t xdrrec_getbytes();
  58. static bool_t xdrrec_putbytes();
  59. static u_int xdrrec_getpos();
  60. static bool_t xdrrec_setpos();
  61. static long *xdrrec_inline();
  62. static void xdrrec_destroy();
  63. static struct xdr_ops xdrrec_ops = {
  64. xdrrec_getlong,
  65. xdrrec_putlong,
  66. xdrrec_getbytes,
  67. xdrrec_putbytes,
  68. xdrrec_getpos,
  69. xdrrec_setpos,
  70. xdrrec_inline,
  71. xdrrec_destroy
  72. };
  73. /*
  74. * A record is composed of one or more record fragments.
  75. * A record fragment is a two-byte header followed by zero to
  76. * 2**32-1 bytes. The header is treated as a long unsigned and is
  77. * encode/decoded to the network via htonl/ntohl. The low order 31 bits
  78. * are a byte count of the fragment. The highest order bit is a boolean:
  79. * 1 => this fragment is the last fragment of the record,
  80. * 0 => this fragment is followed by more fragment(s).
  81. *
  82. * The fragment/record machinery is not general; it is constructed to
  83. * meet the needs of xdr and rpc based on tcp.
  84. */
  85. #define LAST_FRAG ((u_long)(1 << 31))
  86. typedef struct rec_strm {
  87. caddr_t tcp_handle;
  88. caddr_t the_buffer;
  89. /*
  90. * out-goung bits
  91. */
  92. int (*writeit) ();
  93. caddr_t out_base; /* output buffer (points to frag header) */
  94. caddr_t out_finger; /* next output position */
  95. caddr_t out_boundry; /* data cannot up to this address */
  96. u_long *frag_header; /* beginning of curren fragment */
  97. bool_t frag_sent; /* true if buffer sent in middle of record */
  98. /*
  99. * in-coming bits
  100. */
  101. int (*readit) ();
  102. u_long in_size; /* fixed size of the input buffer */
  103. caddr_t in_base;
  104. caddr_t in_finger; /* location of next byte to be had */
  105. caddr_t in_boundry; /* can read up to this location */
  106. long fbtbc; /* fragment bytes to be consumed */
  107. bool_t last_frag;
  108. u_int sendsize;
  109. u_int recvsize;
  110. } RECSTREAM;
  111. /*
  112. * Create an xdr handle for xdrrec
  113. * xdrrec_create fills in xdrs. Sendsize and recvsize are
  114. * send and recv buffer sizes (0 => use default).
  115. * tcp_handle is an opaque handle that is passed as the first parameter to
  116. * the procedures readit and writeit. Readit and writeit are read and
  117. * write respectively. They are like the system
  118. * calls expect that they take an opaque handle rather than an fd.
  119. */
  120. void xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
  121. register XDR *xdrs;
  122. register u_int sendsize;
  123. register u_int recvsize;
  124. caddr_t tcp_handle;
  125. int (*readit) (); /* like read, but pass it a tcp_handle, not sock */
  126. int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */
  127. {
  128. register RECSTREAM *rstrm = (RECSTREAM *) mem_alloc(sizeof(RECSTREAM));
  129. if (rstrm == NULL) {
  130. (void) fprintf(stderr, "xdrrec_create: out of memory\n");
  131. /*
  132. * This is bad. Should rework xdrrec_create to
  133. * return a handle, and in this case return NULL
  134. */
  135. return;
  136. }
  137. /*
  138. * adjust sizes and allocate buffer quad byte aligned
  139. */
  140. rstrm->sendsize = sendsize = fix_buf_size(sendsize);
  141. rstrm->recvsize = recvsize = fix_buf_size(recvsize);
  142. rstrm->the_buffer =
  143. mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
  144. if (rstrm->the_buffer == NULL) {
  145. (void) fprintf(stderr, "xdrrec_create: out of memory\n");
  146. return;
  147. }
  148. for (rstrm->out_base = rstrm->the_buffer;
  149. (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
  150. rstrm->out_base++);
  151. rstrm->in_base = rstrm->out_base + sendsize;
  152. /*
  153. * now the rest ...
  154. */
  155. xdrs->x_ops = &xdrrec_ops;
  156. xdrs->x_private = (caddr_t) rstrm;
  157. rstrm->tcp_handle = tcp_handle;
  158. rstrm->readit = readit;
  159. rstrm->writeit = writeit;
  160. rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
  161. rstrm->frag_header = (u_long *) rstrm->out_base;
  162. rstrm->out_finger += sizeof(u_long);
  163. rstrm->out_boundry += sendsize;
  164. rstrm->frag_sent = FALSE;
  165. rstrm->in_size = recvsize;
  166. rstrm->in_boundry = rstrm->in_base;
  167. rstrm->in_finger = (rstrm->in_boundry += recvsize);
  168. rstrm->fbtbc = 0;
  169. rstrm->last_frag = TRUE;
  170. }
  171. /*
  172. * The reoutines defined below are the xdr ops which will go into the
  173. * xdr handle filled in by xdrrec_create.
  174. */
  175. static bool_t xdrrec_getlong(xdrs, lp)
  176. XDR *xdrs;
  177. long *lp;
  178. {
  179. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  180. register long *buflp = (long *) (rstrm->in_finger);
  181. long mylong;
  182. /* first try the inline, fast case */
  183. if ((rstrm->fbtbc >= sizeof(long)) &&
  184. (((int) rstrm->in_boundry - (int) buflp) >= sizeof(long))) {
  185. *lp = (long) ntohl((u_long) (*buflp));
  186. rstrm->fbtbc -= sizeof(long);
  187. rstrm->in_finger += sizeof(long);
  188. } else {
  189. if (!xdrrec_getbytes(xdrs, (caddr_t) & mylong, sizeof(long)))
  190. return (FALSE);
  191. *lp = (long) ntohl((u_long) mylong);
  192. }
  193. return (TRUE);
  194. }
  195. static bool_t xdrrec_putlong(xdrs, lp)
  196. XDR *xdrs;
  197. long *lp;
  198. {
  199. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  200. register long *dest_lp = ((long *) (rstrm->out_finger));
  201. if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
  202. /*
  203. * this case should almost never happen so the code is
  204. * inefficient
  205. */
  206. rstrm->out_finger -= sizeof(long);
  207. rstrm->frag_sent = TRUE;
  208. if (!flush_out(rstrm, FALSE))
  209. return (FALSE);
  210. dest_lp = ((long *) (rstrm->out_finger));
  211. rstrm->out_finger += sizeof(long);
  212. }
  213. *dest_lp = (long) htonl((u_long) (*lp));
  214. return (TRUE);
  215. }
  216. static bool_t
  217. /* must manage buffers, fragments, and records */
  218. xdrrec_getbytes(xdrs, addr, len)
  219. XDR *xdrs;
  220. register caddr_t addr;
  221. register u_int len;
  222. {
  223. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  224. register int current;
  225. while (len > 0) {
  226. current = rstrm->fbtbc;
  227. if (current == 0) {
  228. if (rstrm->last_frag)
  229. return (FALSE);
  230. if (!set_input_fragment(rstrm))
  231. return (FALSE);
  232. continue;
  233. }
  234. current = (len < current) ? len : current;
  235. if (!get_input_bytes(rstrm, addr, current))
  236. return (FALSE);
  237. addr += current;
  238. rstrm->fbtbc -= current;
  239. len -= current;
  240. }
  241. return (TRUE);
  242. }
  243. static bool_t xdrrec_putbytes(xdrs, addr, len)
  244. XDR *xdrs;
  245. register caddr_t addr;
  246. register u_int len;
  247. {
  248. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  249. register int current;
  250. while (len > 0) {
  251. current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger;
  252. current = (len < current) ? len : current;
  253. bcopy(addr, rstrm->out_finger, current);
  254. rstrm->out_finger += current;
  255. addr += current;
  256. len -= current;
  257. if (rstrm->out_finger == rstrm->out_boundry) {
  258. rstrm->frag_sent = TRUE;
  259. if (!flush_out(rstrm, FALSE))
  260. return (FALSE);
  261. }
  262. }
  263. return (TRUE);
  264. }
  265. static u_int xdrrec_getpos(xdrs)
  266. register XDR *xdrs;
  267. {
  268. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  269. register long pos;
  270. pos = lseek((int) rstrm->tcp_handle, (long) 0, 1);
  271. if (pos != -1)
  272. switch (xdrs->x_op) {
  273. case XDR_ENCODE:
  274. pos += rstrm->out_finger - rstrm->out_base;
  275. break;
  276. case XDR_DECODE:
  277. pos -= rstrm->in_boundry - rstrm->in_finger;
  278. break;
  279. default:
  280. pos = (u_int) - 1;
  281. break;
  282. }
  283. return ((u_int) pos);
  284. }
  285. static bool_t xdrrec_setpos(xdrs, pos)
  286. register XDR *xdrs;
  287. u_int pos;
  288. {
  289. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  290. u_int currpos = xdrrec_getpos(xdrs);
  291. int delta = currpos - pos;
  292. caddr_t newpos;
  293. if ((int) currpos != -1)
  294. switch (xdrs->x_op) {
  295. case XDR_ENCODE:
  296. newpos = rstrm->out_finger - delta;
  297. if ((newpos > (caddr_t) (rstrm->frag_header)) &&
  298. (newpos < rstrm->out_boundry)) {
  299. rstrm->out_finger = newpos;
  300. return (TRUE);
  301. }
  302. break;
  303. case XDR_DECODE:
  304. newpos = rstrm->in_finger - delta;
  305. if ((delta < (int) (rstrm->fbtbc)) &&
  306. (newpos <= rstrm->in_boundry) &&
  307. (newpos >= rstrm->in_base)) {
  308. rstrm->in_finger = newpos;
  309. rstrm->fbtbc -= delta;
  310. return (TRUE);
  311. }
  312. break;
  313. }
  314. return (FALSE);
  315. }
  316. static long *xdrrec_inline(xdrs, len)
  317. register XDR *xdrs;
  318. int len;
  319. {
  320. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  321. long *buf = NULL;
  322. switch (xdrs->x_op) {
  323. case XDR_ENCODE:
  324. if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
  325. buf = (long *) rstrm->out_finger;
  326. rstrm->out_finger += len;
  327. }
  328. break;
  329. case XDR_DECODE:
  330. if ((len <= rstrm->fbtbc) &&
  331. ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
  332. buf = (long *) rstrm->in_finger;
  333. rstrm->fbtbc -= len;
  334. rstrm->in_finger += len;
  335. }
  336. break;
  337. }
  338. return (buf);
  339. }
  340. static void xdrrec_destroy(xdrs)
  341. register XDR *xdrs;
  342. {
  343. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  344. mem_free(rstrm->the_buffer,
  345. rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
  346. mem_free((caddr_t) rstrm, sizeof(RECSTREAM));
  347. }
  348. /*
  349. * Exported routines to manage xdr records
  350. */
  351. /*
  352. * Before reading (deserializing from the stream, one should always call
  353. * this procedure to guarantee proper record alignment.
  354. */
  355. bool_t xdrrec_skiprecord(xdrs)
  356. XDR *xdrs;
  357. {
  358. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  359. while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
  360. if (!skip_input_bytes(rstrm, rstrm->fbtbc))
  361. return (FALSE);
  362. rstrm->fbtbc = 0;
  363. if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
  364. return (FALSE);
  365. }
  366. rstrm->last_frag = FALSE;
  367. return (TRUE);
  368. }
  369. /*
  370. * Look ahead fuction.
  371. * Returns TRUE iff there is no more input in the buffer
  372. * after consuming the rest of the current record.
  373. */
  374. bool_t xdrrec_eof(xdrs)
  375. XDR *xdrs;
  376. {
  377. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  378. while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
  379. if (!skip_input_bytes(rstrm, rstrm->fbtbc))
  380. return (TRUE);
  381. rstrm->fbtbc = 0;
  382. if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
  383. return (TRUE);
  384. }
  385. if (rstrm->in_finger == rstrm->in_boundry)
  386. return (TRUE);
  387. return (FALSE);
  388. }
  389. /*
  390. * The client must tell the package when an end-of-record has occurred.
  391. * The second paraemters tells whether the record should be flushed to the
  392. * (output) tcp stream. (This let's the package support batched or
  393. * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
  394. */
  395. bool_t xdrrec_endofrecord(xdrs, sendnow)
  396. XDR *xdrs;
  397. bool_t sendnow;
  398. {
  399. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  400. register u_long len; /* fragment length */
  401. if (sendnow || rstrm->frag_sent ||
  402. ((u_long) rstrm->out_finger + sizeof(u_long) >=
  403. (u_long) rstrm->out_boundry)) {
  404. rstrm->frag_sent = FALSE;
  405. return (flush_out(rstrm, TRUE));
  406. }
  407. len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) -
  408. sizeof(u_long);
  409. *(rstrm->frag_header) = htonl((u_long) len | LAST_FRAG);
  410. rstrm->frag_header = (u_long *) rstrm->out_finger;
  411. rstrm->out_finger += sizeof(u_long);
  412. return (TRUE);
  413. }
  414. /*
  415. * Internal useful routines
  416. */
  417. static bool_t flush_out(rstrm, eor)
  418. register RECSTREAM *rstrm;
  419. bool_t eor;
  420. {
  421. register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
  422. register u_long len = (u_long) (rstrm->out_finger) -
  423. (u_long) (rstrm->frag_header) - sizeof(u_long);
  424. *(rstrm->frag_header) = htonl(len | eormask);
  425. len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base);
  426. if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
  427. != (int) len)
  428. return (FALSE);
  429. rstrm->frag_header = (u_long *) rstrm->out_base;
  430. rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(u_long);
  431. return (TRUE);
  432. }
  433. static bool_t
  434. /* knows nothing about records! Only about input buffers */
  435. fill_input_buf(rstrm)
  436. register RECSTREAM *rstrm;
  437. {
  438. register caddr_t where;
  439. u_int i;
  440. register int len;
  441. where = rstrm->in_base;
  442. i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
  443. where += i;
  444. len = rstrm->in_size - i;
  445. if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
  446. return (FALSE);
  447. rstrm->in_finger = where;
  448. where += len;
  449. rstrm->in_boundry = where;
  450. return (TRUE);
  451. }
  452. static bool_t
  453. /* knows nothing about records! Only about input buffers */
  454. get_input_bytes(rstrm, addr, len)
  455. register RECSTREAM *rstrm;
  456. register caddr_t addr;
  457. register int len;
  458. {
  459. register int current;
  460. while (len > 0) {
  461. current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
  462. if (current == 0) {
  463. if (!fill_input_buf(rstrm))
  464. return (FALSE);
  465. continue;
  466. }
  467. current = (len < current) ? len : current;
  468. bcopy(rstrm->in_finger, addr, current);
  469. rstrm->in_finger += current;
  470. addr += current;
  471. len -= current;
  472. }
  473. return (TRUE);
  474. }
  475. static bool_t
  476. /* next two bytes of the input stream are treated as a header */
  477. set_input_fragment(rstrm)
  478. register RECSTREAM *rstrm;
  479. {
  480. u_long header;
  481. if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
  482. return (FALSE);
  483. header = (long) ntohl(header);
  484. rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
  485. rstrm->fbtbc = header & (~LAST_FRAG);
  486. return (TRUE);
  487. }
  488. static bool_t
  489. /* consumes input bytes; knows nothing about records! */
  490. skip_input_bytes(rstrm, cnt)
  491. register RECSTREAM *rstrm;
  492. long cnt;
  493. {
  494. register int current;
  495. while (cnt > 0) {
  496. current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
  497. if (current == 0) {
  498. if (!fill_input_buf(rstrm))
  499. return (FALSE);
  500. continue;
  501. }
  502. current = (cnt < current) ? cnt : current;
  503. rstrm->in_finger += current;
  504. cnt -= current;
  505. }
  506. return (TRUE);
  507. }
  508. static u_int fix_buf_size(s)
  509. register u_int s;
  510. {
  511. if (s < 100)
  512. s = 4000;
  513. return (RNDUP(s));
  514. }