summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/librpc/xdr_rec.c
diff options
context:
space:
mode:
Diffstat (limited to 'mdk-stage1/dietlibc/librpc/xdr_rec.c')
-rw-r--r--mdk-stage1/dietlibc/librpc/xdr_rec.c233
1 files changed, 116 insertions, 117 deletions
diff --git a/mdk-stage1/dietlibc/librpc/xdr_rec.c b/mdk-stage1/dietlibc/librpc/xdr_rec.c
index 83e800f5e..ab0b6fd8d 100644
--- a/mdk-stage1/dietlibc/librpc/xdr_rec.c
+++ b/mdk-stage1/dietlibc/librpc/xdr_rec.c
@@ -42,7 +42,7 @@ static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
* and the tcp transport level. A record is composed on one or more
* record fragments. A record fragment is a thirty-two bit header followed
* by n bytes of data, where n is contained in the header. The header
- * is represented as a htonl(u_long). Thegh order bit encodes
+ * is represented as a htonl(unsigned long). Thegh order bit encodes
* whether or not the fragment is the last fragment of the record
* (1 => fragment is last, 0 => more fragments to follow.
* The other 31 bits encode the byte length of the fragment.
@@ -53,19 +53,18 @@ static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro";
#include <rpc/xdr.h>
#include <netinet/in.h>
#include <string.h>
+#include <unistd.h>
-extern long lseek();
+static unsigned int fix_buf_size (unsigned int);
-static u_int fix_buf_size();
-
-static bool_t xdrrec_getlong();
-static bool_t xdrrec_putlong();
-static bool_t xdrrec_getbytes();
-static bool_t xdrrec_putbytes();
-static u_int xdrrec_getpos();
-static bool_t xdrrec_setpos();
-static int32_t *xdrrec_inline();
-static void xdrrec_destroy();
+static bool_t xdrrec_getlong (XDR *, long *);
+static bool_t xdrrec_putlong (XDR *, const long *);
+static bool_t xdrrec_getbytes (XDR *, char *, unsigned int);
+static bool_t xdrrec_putbytes (XDR *, const char *, unsigned int);
+static unsigned int xdrrec_getpos (const XDR *);
+static bool_t xdrrec_setpos (XDR *, unsigned int);
+static int32_t *xdrrec_inline (XDR *, unsigned int);
+static void xdrrec_destroy (XDR *);
static struct xdr_ops xdrrec_ops = {
xdrrec_getlong,
@@ -78,6 +77,7 @@ static struct xdr_ops xdrrec_ops = {
xdrrec_destroy
};
+
/*
* A record is composed of one or more record fragments.
* A record fragment is a two-byte header followed by zero to
@@ -91,32 +91,32 @@ static struct xdr_ops xdrrec_ops = {
* meet the needs of xdr and rpc based on tcp.
*/
-#define LAST_FRAG ((u_long)(1 << 31))
+#define LAST_FRAG ((unsigned long)(1 << 31))
typedef struct rec_strm {
- caddr_t tcp_handle;
- caddr_t the_buffer;
+ char* tcp_handle;
+ char* the_buffer;
/*
* out-goung bits
*/
- int (*writeit) ();
- caddr_t out_base; /* output buffer (points to frag header) */
- caddr_t out_finger; /* next output position */
- caddr_t out_boundry; /* data cannot up to this address */
- u_long *frag_header; /* beginning of curren fragment */
+ int (*writeit) (char *, char *, int);
+ char* out_base; /* output buffer (points to frag header) */
+ char* out_finger; /* next output position */
+ char* out_boundry; /* data cannot up to this address */
+ uint32_t *frag_header; /* beginning of curren fragment */
bool_t frag_sent; /* true if buffer sent in middle of record */
/*
* in-coming bits
*/
- int (*readit) ();
- u_long in_size; /* fixed size of the input buffer */
- caddr_t in_base;
- caddr_t in_finger; /* location of next byte to be had */
- caddr_t in_boundry; /* can read up to this location */
+ int (*readit) (char *, char *, int);
+ unsigned long in_size; /* fixed size of the input buffer */
+ char* in_base;
+ char* in_finger; /* location of next byte to be had */
+ char* in_boundry; /* can read up to this location */
long fbtbc; /* fragment bytes to be consumed */
bool_t last_frag;
- u_int sendsize;
- u_int recvsize;
+ unsigned int sendsize;
+ unsigned int recvsize;
} RECSTREAM;
@@ -129,13 +129,11 @@ typedef struct rec_strm {
* write respectively. They are like the system
* calls expect that they take an opaque handle rather than an fd.
*/
-void xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit)
-register XDR *xdrs;
-register u_int sendsize;
-register u_int recvsize;
-caddr_t tcp_handle;
-int (*readit) (); /* like read, but pass it a tcp_handle, not sock */
-int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */
+void
+xdrrec_create (XDR *xdrs, unsigned int sendsize,
+ unsigned int recvsize, char *tcp_handle,
+ int (*readit) (char *, char *, int),
+ int (*writeit) (char *, char *, int))
{
register RECSTREAM *rstrm = (RECSTREAM *) mem_alloc(sizeof(RECSTREAM));
@@ -159,20 +157,20 @@ int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */
return;
}
for (rstrm->out_base = rstrm->the_buffer;
- (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
+ (unsigned long) rstrm->out_base % BYTES_PER_XDR_UNIT != 0;
rstrm->out_base++);
rstrm->in_base = rstrm->out_base + sendsize;
/*
* now the rest ...
*/
xdrs->x_ops = &xdrrec_ops;
- xdrs->x_private = (caddr_t) rstrm;
+ xdrs->x_private = (char*) rstrm;
rstrm->tcp_handle = tcp_handle;
rstrm->readit = readit;
rstrm->writeit = writeit;
rstrm->out_finger = rstrm->out_boundry = rstrm->out_base;
- rstrm->frag_header = (u_long *) rstrm->out_base;
- rstrm->out_finger += sizeof(u_long);
+ rstrm->frag_header = (uint32_t *) rstrm->out_base;
+ rstrm->out_finger += 4;
rstrm->out_boundry += sendsize;
rstrm->frag_sent = FALSE;
rstrm->in_size = recvsize;
@@ -188,25 +186,24 @@ int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */
* xdr handle filled in by xdrrec_create.
*/
-static bool_t xdrrec_getlong(xdrs, lp)
-XDR *xdrs;
-long *lp;
+static bool_t
+xdrrec_getlong (XDR *xdrs, long *lp)
{
register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
- register long *buflp = (long *) (rstrm->in_finger);
- long mylong;
+ register int32_t *buflp = (int32_t *) (rstrm->in_finger);
+ int32_t mylong;
/* first try the inline, fast case */
- if ((rstrm->fbtbc >= sizeof(long)) &&
- (((int) rstrm->in_boundry - (int) buflp) >= sizeof(long))) {
- *lp = (long) ntohl((u_long) (*buflp));
- rstrm->fbtbc -= sizeof(long);
- rstrm->in_finger += sizeof(long);
+ if ((rstrm->fbtbc >= BYTES_PER_XDR_UNIT) &&
+ ((rstrm->in_boundry - (char *) buflp) >= BYTES_PER_XDR_UNIT)) {
+ *lp = (int32_t) ntohl(*buflp);
+ rstrm->fbtbc -= BYTES_PER_XDR_UNIT;
+ rstrm->in_finger += BYTES_PER_XDR_UNIT;
} else {
- if (!xdrrec_getbytes(xdrs, (caddr_t) & mylong, sizeof(long)))
+ if (!xdrrec_getbytes(xdrs, (char*) & mylong, BYTES_PER_XDR_UNIT))
return (FALSE);
- *lp = (long) ntohl((u_long) mylong);
+ *lp = (int32_t) ntohl(mylong);
}
return (TRUE);
}
@@ -216,17 +213,18 @@ long *lp;
*/
static bool_t flush_out(RECSTREAM* rstrm, bool_t eor)
{
- register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0;
- register u_long len = (u_long) (rstrm->out_finger) -
- (u_long) (rstrm->frag_header) - sizeof(u_long);
+ register unsigned long eormask = (eor == TRUE) ? LAST_FRAG : 0;
+ register unsigned long len = (rstrm->out_finger
+ - (char *) rstrm->frag_header
+ - BYTES_PER_XDR_UNIT);
*(rstrm->frag_header) = htonl(len | eormask);
- len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base);
+ len = rstrm->out_finger - rstrm->out_base;
if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len)
!= (int) len)
return (FALSE);
- rstrm->frag_header = (u_long *) rstrm->out_base;
- rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(u_long);
+ rstrm->frag_header = (uint32_t *) rstrm->out_base;
+ rstrm->out_finger = (char*) rstrm->out_base + BYTES_PER_XDR_UNIT;
return (TRUE);
}
@@ -235,12 +233,12 @@ static bool_t
fill_input_buf(rstrm)
register RECSTREAM *rstrm;
{
- register caddr_t where;
- u_int i;
+ register char* where;
+ unsigned int i;
register int len;
where = rstrm->in_base;
- i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
+ i = (unsigned long) rstrm->in_boundry % BYTES_PER_XDR_UNIT;
where += i;
len = rstrm->in_size - i;
if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1)
@@ -255,20 +253,20 @@ static bool_t
/* knows nothing about records! Only about input buffers */
get_input_bytes(rstrm, addr, len)
register RECSTREAM *rstrm;
-register caddr_t addr;
+register char* addr;
register int len;
{
register int current;
while (len > 0) {
- current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
+ current = rstrm->in_boundry - rstrm->in_finger;
if (current == 0) {
if (!fill_input_buf(rstrm))
return (FALSE);
continue;
}
current = (len < current) ? len : current;
- bcopy(rstrm->in_finger, addr, current);
+ memmove(addr, rstrm->in_finger, current);
rstrm->in_finger += current;
addr += current;
len -= current;
@@ -281,12 +279,24 @@ static bool_t
set_input_fragment(rstrm)
register RECSTREAM *rstrm;
{
- u_long header;
+ uint32_t header;
- if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header)))
+ if (!get_input_bytes(rstrm, (char*) & header, sizeof(header)))
return (FALSE);
- header = (long) ntohl(header);
+ header = ntohl(header);
rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE;
+ /*
+ * Sanity check. Try not to accept wildly incorrect fragment
+ * sizes. Unfortunately, only a size of zero can be identified as
+ * 'wildely incorrect', and this only, if it is not the last
+ * fragment of a message. Ridiculously large fragment sizes may look
+ * wrong, but we don't have any way to be certain that they aren't
+ * what the client actually intended to send us. Many existing RPC
+ * implementations may sent a fragment of size zero as the last
+ * fragment of a message.
+ */
+ if (header == 0)
+ return FALSE;
rstrm->fbtbc = header & (~LAST_FRAG);
return (TRUE);
}
@@ -300,7 +310,7 @@ long cnt;
register int current;
while (cnt > 0) {
- current = (int) rstrm->in_boundry - (int) rstrm->in_finger;
+ current = rstrm->in_boundry - rstrm->in_finger;
if (current == 0) {
if (!fill_input_buf(rstrm))
return (FALSE);
@@ -313,47 +323,43 @@ long cnt;
return (TRUE);
}
-static u_int fix_buf_size(s)
-register u_int s;
+static unsigned int
+fix_buf_size (unsigned int s)
{
if (s < 100)
s = 4000;
return (RNDUP(s));
}
-static bool_t xdrrec_putlong(xdrs, lp)
-XDR *xdrs;
-long *lp;
+
+static bool_t
+xdrrec_putlong (XDR *xdrs, const long *lp)
{
register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
- register long *dest_lp = ((long *) (rstrm->out_finger));
+ register int32_t *dest_lp = (int32_t *) rstrm->out_finger;
- if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) {
+ if ((rstrm->out_finger += BYTES_PER_XDR_UNIT) > rstrm->out_boundry) {
/*
* this case should almost never happen so the code is
* inefficient
*/
- rstrm->out_finger -= sizeof(long);
+ rstrm->out_finger -= BYTES_PER_XDR_UNIT;
rstrm->frag_sent = TRUE;
if (!flush_out(rstrm, FALSE))
return (FALSE);
- dest_lp = ((long *) (rstrm->out_finger));
- rstrm->out_finger += sizeof(long);
+ dest_lp = ((int32_t *) (rstrm->out_finger));
+ rstrm->out_finger += BYTES_PER_XDR_UNIT;
}
- *dest_lp = (long) htonl((u_long) (*lp));
+ *dest_lp = htonl(*lp);
return (TRUE);
}
-static bool_t
-/* must manage buffers, fragments, and records */
-xdrrec_getbytes(xdrs, addr, len)
-XDR *xdrs;
-register caddr_t addr;
-register u_int len;
+static bool_t /* must manage buffers, fragments, and records */
+xdrrec_getbytes (XDR *xdrs, char *addr, unsigned int len)
{
register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
- register int current;
+ register unsigned int current;
while (len > 0) {
current = rstrm->fbtbc;
@@ -374,18 +380,16 @@ register u_int len;
return (TRUE);
}
-static bool_t xdrrec_putbytes(xdrs, addr, len)
-XDR *xdrs;
-register caddr_t addr;
-register u_int len;
+static bool_t
+xdrrec_putbytes (XDR *xdrs, const char *addr, unsigned int len)
{
register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
- register int current;
+ register unsigned int current;
while (len > 0) {
- current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger;
+ current = rstrm->out_boundry - rstrm->out_finger;
current = (len < current) ? len : current;
- bcopy(addr, rstrm->out_finger, current);
+ memmove(rstrm->out_finger, addr, current);
rstrm->out_finger += current;
addr += current;
len -= current;
@@ -398,13 +402,13 @@ register u_int len;
return (TRUE);
}
-static u_int xdrrec_getpos(xdrs)
-register XDR *xdrs;
+static unsigned int
+xdrrec_getpos (const XDR *xdrs)
{
register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
register long pos;
- pos = lseek((int) rstrm->tcp_handle, (long) 0, 1);
+ pos = lseek((int)((long) rstrm->tcp_handle), (long) 0, 1);
if (pos != -1)
switch (xdrs->x_op) {
@@ -417,27 +421,26 @@ register XDR *xdrs;
break;
default:
- pos = (u_int) - 1;
+ pos = (unsigned int) - 1;
break;
}
- return ((u_int) pos);
+ return ((unsigned int) pos);
}
-static bool_t xdrrec_setpos(xdrs, pos)
-register XDR *xdrs;
-u_int pos;
+static bool_t
+xdrrec_setpos (XDR *xdrs, unsigned int pos)
{
register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
- u_int currpos = xdrrec_getpos(xdrs);
+ unsigned int currpos = xdrrec_getpos(xdrs);
int delta = currpos - pos;
- caddr_t newpos;
+ char* newpos;
if ((int) currpos != -1)
switch (xdrs->x_op) {
case XDR_ENCODE:
newpos = rstrm->out_finger - delta;
- if ((newpos > (caddr_t) (rstrm->frag_header)) &&
+ if ((newpos > (char*) (rstrm->frag_header)) &&
(newpos < rstrm->out_boundry)) {
rstrm->out_finger = newpos;
return (TRUE);
@@ -458,9 +461,7 @@ u_int pos;
return (FALSE);
}
-static int32_t *xdrrec_inline(xdrs, len)
-register XDR *xdrs;
-int len;
+static int32_t *xdrrec_inline(XDR* xdrs, unsigned int len)
{
register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
int32_t *buf = NULL;
@@ -486,14 +487,14 @@ int len;
return (buf);
}
-static void xdrrec_destroy(xdrs)
-register XDR *xdrs;
+static void
+xdrrec_destroy (XDR *xdrs)
{
register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private;
mem_free(rstrm->the_buffer,
rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT);
- mem_free((caddr_t) rstrm, sizeof(RECSTREAM));
+ mem_free((char*) rstrm, sizeof(RECSTREAM));
}
@@ -554,20 +555,18 @@ XDR *xdrs;
bool_t sendnow;
{
register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private);
- register u_long len; /* fragment length */
+ register unsigned long len; /* fragment length */
if (sendnow || rstrm->frag_sent ||
- ((u_long) rstrm->out_finger + sizeof(u_long) >=
- (u_long) rstrm->out_boundry)) {
+ (rstrm->out_finger + BYTES_PER_XDR_UNIT >= rstrm->out_boundry)) {
rstrm->frag_sent = FALSE;
return (flush_out(rstrm, TRUE));
}
- len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) -
- sizeof(u_long);
- *(rstrm->frag_header) = htonl((u_long) len | LAST_FRAG);
- rstrm->frag_header = (u_long *) rstrm->out_finger;
- rstrm->out_finger += sizeof(u_long);
+ len = rstrm->out_finger - (char *)rstrm->frag_header -
+ BYTES_PER_XDR_UNIT;
+ *(rstrm->frag_header) = htonl((unsigned long) len | LAST_FRAG);
+ rstrm->frag_header = (uint32_t *) rstrm->out_finger;
+ rstrm->out_finger += BYTES_PER_XDR_UNIT;
return (TRUE);
}
-