home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / netlite / NET / c / ETHERNET < prev    next >
Text File  |  1993-04-26  |  4KB  |  166 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "werr.h"
  6. #include "kernel.h"
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "iface.h"
  11. #include "cmdparse.h"
  12. #include "misc.h"
  13. #include "ethernet.h"
  14. #include "arc.h"
  15. #include "os.h"
  16. #include "swis.h"
  17. #include "timer.h"
  18. #include "ip.h"
  19.  
  20. struct ether ether[ETHER_MAX];
  21. unsigned int nether;
  22. extern struct interface *ifaces;
  23.  
  24. extern char nospace[];
  25.  
  26. static int ether_init(int16, struct interface *, int);
  27. static int ether_stop(struct interface *);
  28. static int ether_send(struct mbuf *, struct interface *, int32, int, int, int, int);
  29. static int ether_recv(struct interface *);
  30.  
  31. /* Attach an ethernet interface to the system
  32.  * argv[0]: hardware type, must be "ether"
  33.  * argv[1]: port number
  34.  * argv[2]: interface label, e.g., "et0"
  35.  * argv[3]: maximum transmission unit, bytes
  36.  */
  37. int ether_attach(int argc, char **argv)
  38. {
  39.         struct interface *if_ether;
  40.         int16 dev;
  41.  
  42.         argc = argc;
  43.  
  44.         if (nether >= ETHER_MAX){
  45.                 werr(1,"Too many ethernet controllers\n");
  46.                 return -1;
  47.         }
  48.  
  49.         dev = nether++;
  50.  
  51.         /* Create interface structure and fill in details */
  52.         if_ether = (struct interface *)calloc(1,sizeof(struct interface));
  53.         if_ether->name = strdup(argv[2]);
  54.         if_ether->mtu  = atoi(argv[3]);
  55.         if_ether->dev  = dev;
  56.         if_ether->recv = ether_recv;
  57.         if_ether->stop = ether_stop;
  58.         if_ether->send = ether_send;
  59.         if_ether->raw  = NULLFP;
  60.         if_ether->put  = NULLFP;
  61.         if_ether->get  = NULLFP;
  62.  
  63.         if_ether->next = ifaces;
  64.         ifaces = if_ether;
  65.         ether_init(dev, if_ether, atoi(argv[1]));
  66.  
  67.         return 0;
  68. }
  69.  
  70. /* Initialize ether port "dev" */
  71. static int ether_init(int16 dev, struct interface *iface, int port)
  72. {
  73.         _kernel_swi_regs rin, rout;
  74.  
  75.         ether[dev].iface = iface;
  76.         ether[dev].port  = port;
  77.  
  78.         rin.r[0] = port;
  79.         rin.r[1] = ip_addr;
  80.         _kernel_swi(ETHER_OPEN, &rin, &rout);
  81.  
  82.         if (rout.r[1] != ETHER_ERR_OK)
  83.         {
  84.               werr(1, "Error %d when opening DCI driver", rout.r[1]);
  85.               return(0);
  86.         }
  87.  
  88.         return(1);
  89. }
  90.  
  91. static int ether_stop(struct interface *iface)
  92. {
  93.         _kernel_swi_regs rin, rout;
  94.  
  95.         rin.r[0] = ether[iface->dev].port;
  96.         _kernel_swi(ETHER_CLOSE, &rin, &rout);
  97.  
  98.         return(0);
  99. }
  100.  
  101. /* Send a packet to transmitter */
  102. static int ether_send(struct mbuf *bp, struct interface *iface, int32 gateway, int precedence, int delay, int throughput, int reliability)
  103. {
  104.         _kernel_swi_regs rin, rout;
  105.         char buffer[1500];
  106.         int size;
  107.  
  108.         precedence  = precedence;
  109.         delay       = delay;
  110.         throughput  = throughput;
  111.         reliability = reliability;
  112.  
  113.         size = dqdata(bp, buffer, 1500);
  114.  
  115.         rin.r[0] = ether[iface->dev].port;
  116.         rin.r[1] = (int)buffer;
  117.         rin.r[2] = size;
  118.         rin.r[3] = gateway;
  119.         _kernel_swi(ETHER_WRITE, &rin, &rout);
  120.  
  121.         if (rout.r[3] != ETHER_ERR_OK)
  122.         {
  123.                werr(0, "Error %d when writing to DCI driver", rout.r[3]);
  124.                return(0);
  125.         }
  126.  
  127.         return(0);
  128. }
  129.  
  130. /* Receive characters from line
  131.  * Returns count of characters read
  132.  */
  133. static int ether_recv(struct interface *iface)
  134. {
  135.         _kernel_swi_regs rin, rout;
  136.         struct mbuf *bp;
  137.  
  138.         while (1)
  139.         {
  140.                 if ((bp = alloc_mbuf(1500)) == NULLBUF) return(0);
  141.  
  142.                 rin.r[0] = ether[iface->dev].port;
  143.                 rin.r[1] = (int)bp->data;
  144.                 rin.r[2] = 1500;
  145.                 _kernel_swi(ETHER_READ, &rin, &rout);
  146.  
  147.                 if (rout.r[4] != ETHER_ERR_OK)
  148.                 {
  149.                        werr(0, "Error %d when reading from DCI driver", rout.r[4]);
  150.                        free_p(bp);
  151.                        return(0);
  152.                 }
  153.  
  154.                 if ((bp->cnt = rout.r[2]) == 0)
  155.                 {
  156.                        free_p(bp);
  157.                        return(0);
  158.                 }
  159.  
  160.                 ip_route(bp, rout.r[3]);
  161.         }
  162.  
  163.         return(0);
  164. }
  165.  
  166.