home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / ARPHDR.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  2KB  |  72 lines

  1. /* ARP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    $Id: arphdr.c 1.2 93/07/16 11:41:58 ROOT_DOS Exp $
  7. *    14 Jun 93    1.2        GT    Fix warnings.                                    *
  8. ****************************************************************************/
  9.  
  10. #include "global.h"
  11. #include "mbuf.h"
  12. #include "arp.h"
  13. #include "ip.h"
  14.  
  15.  
  16. /* Copy a host format arp structure into mbuf for transmission */
  17. struct mbuf *
  18. htonarp(arp)
  19. register struct arp *arp;
  20. {
  21.     struct mbuf *bp;
  22.     register char *buf;
  23.  
  24.     if(arp == (struct arp *)NULL)
  25.         return NULLBUF;
  26.  
  27.     if((bp = alloc_mbuf(ARPLEN + 2 * uchar(arp->hwalen))) == NULLBUF)
  28.         return NULLBUF;
  29.  
  30.     buf = bp->data;
  31.  
  32.     buf = put16(buf,arp->hardware);
  33.     buf = put16(buf,arp->protocol);
  34.     *buf++ = arp->hwalen;
  35.     *buf++ = arp->pralen;
  36.     buf = put16(buf,arp->opcode);
  37.     memcpy(buf,arp->shwaddr,(int16)uchar(arp->hwalen));
  38.     buf += arp->hwalen;
  39.     buf = put32(buf,arp->sprotaddr);
  40.     memcpy(buf,arp->thwaddr,(int16)uchar(arp->hwalen));
  41.     buf += arp->hwalen;
  42.     buf = put32(buf,arp->tprotaddr);
  43.  
  44.     bp->cnt = buf - bp->data;
  45.     return bp;
  46. }
  47. /* Convert an incoming ARP packet into a host-format structure */
  48. int
  49. ntoharp(arp,bpp)
  50. register struct arp *arp;
  51. struct mbuf **bpp;
  52. {
  53.     if(arp == (struct arp *)NULL || bpp == NULLBUFP)
  54.         return -1;
  55.  
  56.     arp->hardware = pull16(bpp);
  57.     arp->protocol = pull16(bpp);
  58.     arp->hwalen = PULLCHAR(bpp);
  59.     arp->pralen = PULLCHAR(bpp);
  60.     arp->opcode = pull16(bpp);
  61.     pullup(bpp,arp->shwaddr,(int16)uchar(arp->hwalen));
  62.     arp->sprotaddr = pull32(bpp);
  63.     pullup(bpp,arp->thwaddr,(int16)uchar(arp->hwalen));
  64.     arp->tprotaddr = pull32(bpp);
  65.  
  66.     /* Get rid of anything left over */
  67.     free_p(*bpp);
  68.     *bpp = NULLBUF;
  69.     return 0;
  70. }
  71.  
  72.