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