home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / ENET.C < prev    next >
C/C++ Source or Header  |  1992-04-08  |  3KB  |  135 lines

  1. /* Stuff generic to all Ethernet controllers
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "iface.h"
  8. #include "arp.h"
  9. #include "ip.h"
  10. #include "enet.h"
  11.  
  12. char Ether_bdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  13.  
  14. /* Convert Ethernet header in host form to network mbuf */
  15. struct mbuf *
  16. htonether(ether,bp)
  17. struct ether *ether;
  18. struct mbuf *bp;
  19. {
  20.     register char *cp;
  21.  
  22.     bp = pushdown(bp,ETHERLEN);
  23.  
  24.     cp = bp->data;
  25.  
  26.     memcpy(cp,ether->dest,EADDR_LEN);
  27.     cp += EADDR_LEN;
  28.     memcpy(cp,ether->source,EADDR_LEN);
  29.     cp += EADDR_LEN;
  30.     put16(cp,ether->type);
  31.  
  32.     return bp;
  33. }
  34. /* Extract Ethernet header */
  35. int
  36. ntohether(ether,bpp)
  37. struct ether *ether;
  38. struct mbuf **bpp;
  39. {
  40.     pullup(bpp,ether->dest,EADDR_LEN);
  41.     pullup(bpp,ether->source,EADDR_LEN);
  42.     ether->type = pull16(bpp);
  43.     return ETHERLEN;
  44. }
  45.  
  46. /* Format an Ethernet address into a printable ascii string */
  47. char *
  48. pether(out,addr)
  49. char *out,*addr;
  50. {
  51.     sprintf(out,"%02x:%02x:%02x:%02x:%02x:%02x",
  52.      uchar(addr[0]),uchar(addr[1]),
  53.      uchar(addr[2]),uchar(addr[3]),
  54.      uchar(addr[4]),uchar(addr[5]));
  55.     return out;
  56. }
  57.  
  58. /* Convert an Ethernet address from Hex/ASCII to binary */
  59. int
  60. gether(out,cp)
  61. register char *out;
  62. register char *cp;
  63. {
  64.     register int i;
  65.  
  66.     for(i=6; i!=0; i--){
  67.         *out++ = htoi(cp);
  68.         if((cp = strchr(cp,':')) == NULLCHAR)    /* Find delimiter */
  69.             break;
  70.         cp++;            /* and skip over it */
  71.     }
  72.     return i;
  73. }
  74. /* Send an IP datagram on Ethernet */
  75. int
  76. enet_send(bp,iface,gateway,tos)
  77. struct mbuf *bp;    /* Buffer to send */
  78. struct iface *iface;    /* Pointer to interface control block */
  79. int32 gateway;        /* IP address of next hop */
  80. int tos;        /* Type of service, not used */
  81. {
  82.     char *egate;
  83.  
  84.     egate = res_arp(iface,ARP_ETHER,gateway,bp);
  85.     if(egate != NULLCHAR)
  86.         return (*iface->output)(iface,egate,iface->hwaddr,IP_TYPE,bp);
  87.     return 0;
  88. }
  89. /* Send a packet with Ethernet header */
  90. int
  91. enet_output(iface,dest,source,type,data)
  92. struct iface *iface;    /* Pointer to interface control block */
  93. char *dest;        /* Destination Ethernet address */
  94. char *source;        /* Source Ethernet address */
  95. int16 type;        /* Type field */
  96. struct mbuf *data;    /* Data field */
  97. {
  98.     struct ether ep;
  99.     struct mbuf *bp;
  100.  
  101.     memcpy(ep.dest,dest,EADDR_LEN);
  102.     memcpy(ep.source,source,EADDR_LEN);
  103.     ep.type = type;
  104.     if((bp = htonether(&ep,data)) == NULLBUF){
  105.         free_p(data);
  106.         return -1;
  107.     }
  108.     return (*iface->raw)(iface,bp);
  109. }
  110. /* Process incoming Ethernet packets. Shared by all ethernet drivers. */
  111. void
  112. eproc(iface,bp)
  113. struct iface *iface;
  114. struct mbuf *bp;
  115. {
  116.     struct ether hdr;
  117.  
  118.     /* Remove Ethernet header and kick packet upstairs */
  119.     ntohether(&hdr,&bp);
  120.  
  121.     switch(hdr.type){
  122.     case REVARP_TYPE:
  123.     case ARP_TYPE:
  124.         arp_input(iface,bp);
  125.         break;
  126.     case IP_TYPE:
  127.         ip_route(iface,bp,hdr.dest[0] & 1);
  128.         break;
  129.     default:
  130.         free_p(bp);
  131.         break;
  132.     }
  133. }
  134.  
  135.