xdr_rec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 <string.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 int32_t *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. static bool_t flush_out(register RECSTREAM *rstrm, bool_t eor);
  112. static bool_t set_input_fragment(register RECSTREAM *rstrm);
  113. static bool_t get_input_bytes(register RECSTREAM *rstrm,
  114. register caddr_t addr, register int len);
  115. static bool_t skip_input_bytes(register RECSTREAM *rstrm, long cnt);
  116. /*
  117. * Create an xdr handle for xdrrec
  118. * xdrrec_create fills in xdrs. Sendsize and recvsize are
  119. * send and recv buffer sizes (0 => use default).
  120. * tcp_handle is an opaque handle that is passed as the first parameter to
  121. * the procedures readit and writeit. Readit and writeit are read and
  122. * write respectively. They are like the system
  123. * calls expect that they take an opaque handle rather than an fd.
  124. */
  125. void xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
  126. register XDR *xdrs;
  127. register u_int sendsize;
  128. register u_int recvsize;
  129. caddr_t tcp_handle;
  130. int (*readit) (); /* like read, but pass it a tcp_handle, not sock */
  131. int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */
  132. {
  133. register RECSTREAM *rstrm = (RECSTREAM *) mem_alloc(sizeof(RECSTREAM));
  134. if (rstrm == NULL) {
  135. (void) fprintf(stderr, "xdrrec_create: out of memory\n");
  136. /*
  137. * This is bad. Should rework xdrrec_create to
  138. * return a handle, and in this case return NULL
  139. */
  140. return;
  141. }
  142. /*
  143. * adjust sizes and allocate buffer quad byte aligned
  144. */
  145. rstrm->sendsize = sendsize = fix_buf_size(sendsize);
  146. rstrm->recvsize = recvsize = fix_buf_size(recvsize);
  147. rstrm->the_buffer =
  148. mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT);
  149. if (rstrm->the_buffer == NULL) {
  150. (void) fprintf(stderr, "xdrrec_create: out of memory\n");
  151. return;
  152. }
  153. for (rstrm->out_base = rstrm->the_buffer;
  154. (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
  155. rstrm->out_base++);
  156. rstrm->in_base = rstrm->out_base + sendsize;
  157. /*
  158. * now the rest ...
  159. */
  160. xdrs->x_ops = &xdrrec_ops;
  161. xdrs->x_private = (caddr_t) rstrm;
  162. rstrm->tcp_handle = tcp_handle;
  163. rstrm->readit = readit;
  164. rstrm->writeit = writeit;
  165. rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
  166. rstrm->frag_header = (u_long *) rstrm->out_base;
  167. rstrm->out_finger += sizeof(u_long);
  168. rstrm->out_boundry += sendsize;
  169. rstrm->frag_sent = FALSE;
  170. rstrm->in_size = recvsize;
  171. rstrm->in_boundry = rstrm->in_base;
  172. rstrm->in_finger = (rstrm->in_boundry += recvsize);
  173. rstrm->fbtbc = 0;
  174. rstrm->last_frag = TRUE;
  175. }
  176. /*
  177. * The reoutines defined below are the xdr ops which will go into the
  178. * xdr handle filled in by xdrrec_create.
  179. */
  180. static bool_t xdrrec_getlong(xdrs, lp)
  181. XDR *xdrs;
  182. long *lp;
  183. {
  184. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  185. register long *buflp = (long *) (rstrm->in_finger);
  186. long mylong;
  187. /* first try the inline, fast case */
  188. if ((rstrm->fbtbc >= sizeof(long)) &&
  189. (((int) rstrm->in_boundry - (int) buflp) >= sizeof(long))) {
  190. *lp = (long) ntohl((u_long) (*buflp));
  191. rstrm->fbtbc -= sizeof(long);
  192. rstrm->in_finger += sizeof(long);
  193. } else {
  194. if (!xdrrec_getbytes(xdrs, (caddr_t) & mylong, sizeof(long)))
  195. return (FALSE);
  196. *lp = (long) ntohl((u_long) mylong);
  197. }
  198. return (TRUE);
  199. }
  200. static bool_t xdrrec_putlong(xdrs, lp)
  201. XDR *xdrs;
  202. long *lp;
  203. {
  204. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  205. register long *dest_lp = ((long *) (rstrm->out_finger));
  206. if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
  207. /*
  208. * this case should almost never happen so the code is
  209. * inefficient
  210. */
  211. rstrm->out_finger -= sizeof(long);
  212. rstrm->frag_sent = TRUE;
  213. if (!flush_out(rstrm, FALSE))
  214. return (FALSE);
  215. dest_lp = ((long *) (rstrm->out_finger));
  216. rstrm->out_finger += sizeof(long);
  217. }
  218. *dest_lp = (long) htonl((u_long) (*lp));
  219. return (TRUE);
  220. }
  221. static bool_t
  222. /* must manage buffers, fragments, and records */
  223. xdrrec_getbytes(xdrs, addr, len)
  224. XDR *xdrs;
  225. register caddr_t addr;
  226. register u_int len;
  227. {
  228. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  229. register int current;
  230. while (len > 0) {
  231. current = rstrm->fbtbc;
  232. if (current == 0) {
  233. if (rstrm->last_frag)
  234. return (FALSE);
  235. if (!set_input_fragment(rstrm))
  236. return (FALSE);
  237. continue;
  238. }
  239. current = (len < current) ? len : current;
  240. if (!get_input_bytes(rstrm, addr, current))
  241. return (FALSE);
  242. addr += current;
  243. rstrm->fbtbc -= current;
  244. len -= current;
  245. }
  246. return (TRUE);
  247. }
  248. static bool_t xdrrec_putbytes(xdrs, addr, len)
  249. XDR *xdrs;
  250. register caddr_t addr;
  251. register u_int len;
  252. {
  253. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  254. register int current;
  255. while (len > 0) {
  256. current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger;
  257. current = (len < current) ? len : current;
  258. bcopy(addr, rstrm->out_finger, current);
  259. rstrm->out_finger += current;
  260. addr += current;
  261. len -= current;
  262. if (rstrm->out_finger == rstrm->out_boundry) {
  263. rstrm->frag_sent = TRUE;
  264. if (!flush_out(rstrm, FALSE))
  265. return (FALSE);
  266. }
  267. }
  268. return (TRUE);
  269. }
  270. static u_int xdrrec_getpos(xdrs)
  271. register XDR *xdrs;
  272. {
  273. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  274. register long pos;
  275. pos = lseek((int) rstrm->tcp_handle, (long) 0, 1);
  276. if (pos != -1)
  277. switch (xdrs->x_op) {
  278. case XDR_ENCODE:
  279. pos += rstrm->out_finger - rstrm->out_base;
  280. break;
  281. case XDR_DECODE:
  282. pos -= rstrm->in_boundry - rstrm->in_finger;
  283. break;
  284. default:
  285. pos = (u_int) - 1;
  286. break;
  287. }
  288. return ((u_int) pos);
  289. }
  290. static bool_t xdrrec_setpos(xdrs, pos)
  291. register XDR *xdrs;
  292. u_int pos;
  293. {
  294. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  295. u_int currpos = xdrrec_getpos(xdrs);
  296. int delta = currpos - pos;
  297. caddr_t newpos;
  298. if ((int) currpos != -1)
  299. switch (xdrs->x_op) {
  300. case XDR_ENCODE:
  301. newpos = rstrm->out_finger - delta;
  302. if ((newpos > (caddr_t) (rstrm->frag_header)) &&
  303. (newpos < rstrm->out_boundry)) {
  304. rstrm->out_finger = newpos;
  305. return (TRUE);
  306. }
  307. break;
  308. case XDR_DECODE:
  309. newpos = rstrm->in_finger - delta;
  310. if ((delta < (int) (rstrm->fbtbc)) &&
  311. (newpos <= rstrm->in_boundry) &&
  312. (newpos >= rstrm->in_base)) {
  313. rstrm->in_finger = newpos;
  314. rstrm->fbtbc -= delta;
  315. return (TRUE);
  316. }
  317. break;
  318. default: /* silence the warnings */
  319. }
  320. return (FALSE);
  321. }
  322. static int32_t *xdrrec_inline(xdrs, len)
  323. register XDR *xdrs;
  324. int len;
  325. {
  326. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  327. int32_t *buf = NULL;
  328. switch (xdrs->x_op) {
  329. case XDR_ENCODE:
  330. if ((rstrm->out_finger + len) <= rstrm->out_boundry) {
  331. buf = (int32_t *) rstrm->out_finger;
  332. rstrm->out_finger += len;
  333. }
  334. break;
  335. case XDR_DECODE:
  336. if ((len <= rstrm->fbtbc) &&
  337. ((rstrm->in_finger + len) <= rstrm->in_boundry)) {
  338. buf = (int32_t *) rstrm->in_finger;
  339. rstrm->fbtbc -= len;
  340. rstrm->in_finger += len;
  341. }
  342. break;
  343. default: /* silence the warnings */
  344. }
  345. return (buf);
  346. }
  347. static void xdrrec_destroy(xdrs)
  348. register XDR *xdrs;
  349. {
  350. register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
  351. mem_free(rstrm->the_buffer,
  352. rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
  353. mem_free((caddr_t) rstrm, sizeof(RECSTREAM));
  354. }
  355. /*
  356. * Exported routines to manage xdr records
  357. */
  358. /*
  359. * Before reading (deserializing from the stream, one should always call
  360. * this procedure to guarantee proper record alignment.
  361. */
  362. bool_t xdrrec_skiprecord(xdrs)
  363. XDR *xdrs;
  364. {
  365. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  366. while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
  367. if (!skip_input_bytes(rstrm, rstrm->fbtbc))
  368. return (FALSE);
  369. rstrm->fbtbc = 0;
  370. if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
  371. return (FALSE);
  372. }
  373. rstrm->last_frag = FALSE;
  374. return (TRUE);
  375. }
  376. /*
  377. * Look ahead fuction.
  378. * Returns TRUE iff there is no more input in the buffer
  379. * after consuming the rest of the current record.
  380. */
  381. bool_t xdrrec_eof(xdrs)
  382. XDR *xdrs;
  383. {
  384. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  385. while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) {
  386. if (!skip_input_bytes(rstrm, rstrm->fbtbc))
  387. return (TRUE);
  388. rstrm->fbtbc = 0;
  389. if ((!rstrm->last_frag) && (!set_input_fragment(rstrm)))
  390. return (TRUE);
  391. }
  392. if (rstrm->in_finger == rstrm->in_boundry)
  393. return (TRUE);
  394. return (FALSE);
  395. }
  396. /*
  397. * The client must tell the package when an end-of-record has occurred.
  398. * The second paraemters tells whether the record should be flushed to the
  399. * (output) tcp stream. (This let's the package support batched or
  400. * pipelined procedure calls.) TRUE => immmediate flush to tcp connection.
  401. */
  402. bool_t xdrrec_endofrecord(xdrs, sendnow)
  403. XDR *xdrs;
  404. bool_t sendnow;
  405. {
  406. register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
  407. register u_long len; /* fragment length */
  408. if (sendnow || rstrm->frag_sent ||
  409. ((u_long) rstrm->out_finger + sizeof(u_long) >=
  410. (u_long) rstrm->out_boundry)) {
  411. rstrm->frag_sent = FALSE;
  412. return (flush_out(rstrm, TRUE));
  413. }
  414. len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) -
  415. sizeof(u_long);
  416. *(rstrm->frag_header) = htonl((u_long) len | LAST_FRAG);
  417. rstrm->frag_header = (u_long *) rstrm->out_finger;
  418. rstrm->out_finger += sizeof(u_long);
  419. return (TRUE);
  420. }
  421. /*
  422. * Internal useful routines
  423. */
  424. static bool_t flush_out(rstrm, eor)
  425. register RECSTREAM *rstrm;
  426. bool_t eor;
  427. {
  428. register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
  429. register u_long len = (u_long) (rstrm->out_finger) -
  430. (u_long) (rstrm->frag_header) - sizeof(u_long);
  431. *(rstrm->frag_header) = htonl(len | eormask);
  432. len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base);
  433. if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
  434. != (int) len)
  435. return (FALSE);
  436. rstrm->frag_header = (u_long *) rstrm->out_base;
  437. rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(u_long);
  438. return (TRUE);
  439. }
  440. static bool_t
  441. /* knows nothing about records! Only about input buffers */
  442. fill_input_buf(rstrm)
  443. register RECSTREAM *rstrm;
  444. {
  445. register caddr_t where;
  446. u_int i;
  447. register int len;
  448. where = rstrm->in_base;
  449. i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
  450. where += i;
  451. len = rstrm->in_size - i;
  452. if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
  453. return (FALSE);
  454. rstrm->in_finger = where;
  455. where += len;
  456. rstrm->in_boundry = where;
  457. return (TRUE);
  458. }
  459. static bool_t
  460. /* knows nothing about records! Only about input buffers */
  461. get_input_bytes(rstrm, addr, len)
  462. register RECSTREAM *rstrm;
  463. register caddr_t addr;
  464. register int len;
  465. {
  466. register int current;
  467. while (len > 0) {
  468. current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
  469. if (current == 0) {
  470. if (!fill_input_buf(rstrm))
  471. return (FALSE);
  472. continue;
  473. }
  474. current = (len < current) ? len : current;
  475. bcopy(rstrm->in_finger, addr, current);
  476. rstrm->in_finger += current;
  477. addr += current;
  478. len -= current;
  479. }
  480. return (TRUE);
  481. }
  482. static bool_t
  483. /* next two bytes of the input stream are treated as a header */
  484. set_input_fragment(rstrm)
  485. register RECSTREAM *rstrm;
  486. {
  487. u_long header;
  488. if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
  489. return (FALSE);
  490. header = (long) ntohl(header);
  491. rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
  492. rstrm->fbtbc = header & (~LAST_FRAG);
  493. return (TRUE);
  494. }
  495. static bool_t
  496. /* consumes input bytes; knows nothing about records! */
  497. skip_input_bytes(rstrm, cnt)
  498. register RECSTREAM *rstrm;
  499. long cnt;
  500. {
  501. register int current;
  502. while (cnt > 0) {
  503. current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
  504. if (current == 0) {
  505. if (!fill_input_buf(rstrm))
  506. return (FALSE);
  507. continue;
  508. }
  509. current = (cnt < current) ? cnt : current;
  510. rstrm->in_finger += current;
  511. cnt -= current;
  512. }
  513. return (TRUE);
  514. }
  515. static u_int fix_buf_size(s)
  516. register u_int s;
  517. {
  518. if (s < 100)
  519. s = 4000;
  520. return (RNDUP(s));
  521. }