home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / ARPHDR.C < prev    next >
C/C++ Source or Header  |  1991-01-27  |  2KB  |  64 lines

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