netjunk.c #1

  • //
  • guest/
  • sandy_currier/
  • p4filter/
  • netjunk.c
  • View
  • Commits
  • Open Download .zip Download (2 KB)
/*
 * netjunk.c: Useful functions for dealing with the outside world.
 * $Header$
 */

#include <config.h>
#include "netjunk.h"

#include <errno.h>
#include <unistd.h>

ssize_t doread(int fd, void *buf, size_t count)
{
  char *cbuf = buf;
  int qread = 0, retval;
  while (count > 0)
  {
    retval = read(fd, cbuf, count);
    /* What'd read give back?  An error? */
    if (retval < 0)
    {
      /* Interrupted read; restart. */
      if (errno == EINTR)
	continue;
      /* Some other read error. */
      return retval;
    }
    /* End of file? */
    else if (retval == 0)
    {
      /* Just return the number of bytes read; we'll get the EOF later. */
      return qread;
    }
    /* Valid data. */
    else
    {
      qread += retval;
      count -= retval;
      cbuf += retval;
    }
  }
  /* All done.  Return the number of bytes we actually read. */
  return qread;
}

ssize_t dowrite(int fd, const void *buf, size_t count)
{
  const char *cbuf = buf;
  int qwrote = 0, retval;
  while (count > 0)
  {
    retval = write(fd, cbuf, count);
    /* What'd write give back?  An error? */
    if (retval < 0)
    {
      /* Interrupted write; restart. */
      if (errno == EINTR)
	continue;
      /* Some other write error. */
      return retval;
    }
    /* Valid data. */
    else
    {
      qwrote += retval;
      count -= retval;
      cbuf += retval;
    }
  }
  /* All done.  Return the number of bytes we actually wrote. */
  return qwrote;
}

unsigned read_p4_long(const void *buf)
{
  const unsigned char *cbuf = buf;
  unsigned count;
  unsigned shift = 0;
  unsigned val = 0;

  /* Little-endian byte order means the least-significant byte is first. */
  for (count = 0; count < 4; count++)
  {
    val += cbuf[count] << shift;
    shift += 8;
  }
  
  return val;
}

void write_p4_long(void *buf, unsigned val)
{
  unsigned char *cbuf = buf;
  unsigned shift = 0;
  unsigned count;
  
  for (count = 0; count < 4; count++)
  {
    cbuf[count] = (val >> shift) & 0xFF;
    shift += 8;
  }
}

char p4_checksum(const void *buf)
{
  const char *cbuf = buf;
  unsigned count;
  unsigned char checksum = 0;
  
  for (count = 0; count < 4; count++)
  {
    checksum ^= cbuf[count];
  }
  
  return checksum;
}
# Change User Description Committed
#1 450 sandy_currier Initial import of p4filter code.
 This contains a solaris2.6 binary but
no others.