home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / etherlib / part01 / src / nit3read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-24  |  1.1 KB  |  46 lines

  1. /* $Id: nit3read.c,v 2.1 89/10/23 15:42:53 dupuy Exp $ */
  2.  
  3. #include "libether.h"
  4.  
  5. /*
  6.  * Reads and returns a single packet, filling in all fields.  If pktbuf is
  7.  * NULL, a buffer is allocated for it.    If pktbuf is not NULL, the function
  8.  * assumes that pktbuf is large enough to hold pktlen bytes.  Since read() may
  9.  * return several packets at a time, we have to buffer them as well.  Since
  10.  * socket based nit doesn't handle multicast addresses, we have to check them
  11.  * at the user level.
  12.  *
  13.  * NOTE - the buffering code is not re-entrant, although different fd's will
  14.  * not conflict.
  15.  */
  16.  
  17. int
  18. ether_read (fd, packet)
  19. int fd;
  20. ether_packet *packet;
  21. {
  22.     char *pbuf;
  23.     int bytes;
  24.  
  25.     if ((bytes = _ether_next_packet (fd, &pbuf)) < 0)
  26.     return (-1);
  27.  
  28.     bcopy (pbuf, (char *) packet, ETHER_PKT);
  29.     pbuf += ETHER_PKT;
  30.  
  31.     if (packet->pktbuf == 0)
  32.     {
  33.     packet->pktlen = bytes;
  34.     if ((packet->pktbuf = (char *) malloc ((unsigned) bytes)) == 0)
  35.         return (-1);
  36.     }
  37.  
  38.     bcopy (pbuf, packet->pktbuf,
  39.        (int) ((packet->pktlen > bytes) ? bytes : packet->pktlen));
  40.  
  41.     if (packet->pktlen > bytes)
  42.     packet->pktlen = bytes;
  43.  
  44.     return (bytes);
  45. }
  46.