xdr_rec.c 15 KB

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