xdr_rec.c 15 KB

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