home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / msr313src.zip / msnpkt.c < prev    next >
C/C++ Source or Header  |  1993-07-12  |  6KB  |  219 lines

  1. /* File MSNPKT.C
  2.  * Packet Driver interface
  3.  *
  4.  * Copyright (C) 1985, 1993, Trustees of Columbia University in the 
  5.  * City of New York.  Permission is granted to any individual or institution
  6.  * to use this software as long as it is not sold for profit.  This copyright
  7.  * notice must be retained.  This software may not be included in commercial
  8.  * products without written permission of Columbia University.
  9.  *
  10.  * Written for MS-DOS Kermit by Joe R. Doupnik, 
  11.  *  Utah State University, jrd@cc.usu.edu, jrd@usu.Bitnet
  12.  *
  13.  * Last edit
  14.  * 27 August 1992 v3.13
  15.  */
  16. #include "msntcp.h"
  17. #include "msnlib.h"
  18.  
  19. #define BUFSIZE    6000             /* size of pkt receive buffer */
  20.  
  21. word    pkt_ip_type = 0x0008;        /* these are little endian values */
  22. word    pkt_arp_type = 0x0608;
  23. word    pkt_rarp_type = 0x3580;
  24.  
  25. int    pkt_ip_handle = -1;        /* -1 means invalid handle */
  26. int    pkt_arp_handle = -1;
  27. int    pkt_rarp_handle = -1;
  28.  
  29. int    pdversion, pdtype, pdnum, pdfunct;
  30. extern    word pktdevclass;
  31. extern    int pdinit(void *, void *);
  32. extern    int pdinfo(int *, int *, int *, int *, int *);
  33. extern    int pdaccess(int *, int, int *);
  34. extern    int pdclose(int);
  35. extern    eth_address eth_addr;            /* six byte array */
  36. extern    word MAC_len;
  37. extern    word mss;
  38.  
  39. byte pktbuf[BUFSIZE] = {0};    /* linked list packet receive buffer */
  40. static byte * pktbuf_read = pktbuf;
  41. byte pktwnum = 0;        /* seq number for packets written to buffer */
  42. byte pktrnum = 0;        /* seq number for packets read from buffer */
  43. byte * pktbuf_wrote = &pktbuf[BUFSIZE - 4];
  44.  
  45. int
  46. pkt_eth_init()
  47. {
  48.         pkt_buf_wipe();        /* clean out and init receiver buffer */
  49.     mss = ETH_MSS;        /* set default max seg size */
  50.     if (pdinit(pktbuf, eth_addr) == 0)
  51.             {
  52.         outs("\r\nCannot attach to an Ethernet Packet Driver");
  53.         outs(" or a Novell ODI driver.");
  54.         return( 0 );
  55.         }
  56.     pkt_buf_wipe();
  57.                     /* lets find out about the driver */
  58.     if ((pdinfo(&pdversion, &pktdevclass, &pdtype, &pdnum, &pdfunct))
  59.         == 0)
  60.         {
  61.         outs("\r\nCannot obtain Packet Driver or ODI information");
  62.         return (0);
  63.         }
  64.  
  65.     if (pdaccess(&pkt_ip_type, 
  66.     (pktdevclass == PD_SLIP)? 0: sizeof(pkt_ip_handle), &pkt_ip_handle)
  67.         == 0)
  68.          {
  69.         outs("\r\n\7Cannot access IP type packets");
  70.         return (0);
  71.         }
  72.  
  73.     if ((pdinfo(&pdversion, &pktdevclass, &pdtype, &pdnum, &pdfunct))
  74.         == 0)
  75.         {
  76.         outs("\r\nCannot obtain Packet Driver or ODI information");
  77.         return (0);
  78.         }
  79.  
  80. /* Check for real SLIP and for ODI with SLIP_PPP; neither uses ARP and RARP */
  81. /* ODI returns length of MAC header, such as 6 for real Ethernet and 0
  82.    for SLIP and PPP. We get Ethernet style frames for ODI material. */
  83.     if (pktdevclass == PD_SLIP ||
  84.         ( pktdevclass == PD_ETHER && MAC_len == 0))
  85.         return (1);
  86.  
  87.     if (pdaccess(&pkt_arp_type, sizeof(pkt_arp_handle), &pkt_arp_handle)
  88.         == 0)
  89.          {
  90.         outs("\r\n\7Cannot access ARP type packets");
  91.         return (0);
  92.         }
  93.     return (1);                /* say success */
  94. }
  95.  
  96. int
  97. pkt_rarp_init()                /* access PD or ODI for RARP */
  98. {
  99.     if (pkt_rarp_handle != -1)
  100.         return (1);        /* have handle already */
  101.  
  102.     if (pdaccess(&pkt_rarp_type, sizeof(pkt_rarp_handle), &pkt_rarp_handle)
  103.         == 0)
  104.          {
  105.         outs("\r\n\7Cannot access RARP type packets");
  106.         return (0);
  107.         }
  108.     return (1);                /* say success */
  109. }
  110.  
  111. int
  112. pkt_release()
  113. {
  114.     register int status = 1;            /* assume success */
  115.  
  116.     if (pkt_ip_handle != -1)
  117.         if (pdclose(pkt_ip_handle) == 0)
  118.             {
  119.             outs("\r\nERROR releasing Packet Driver for IP");
  120.             status = 0;
  121.             }
  122.         else pkt_ip_handle = -1;    /* handle is out of service */
  123.  
  124.         if (pkt_arp_handle != -1)
  125.         if (pdclose(pkt_arp_handle) == 0)
  126.             {
  127.             outs("\r\nERROR releasing Packet Driver for ARP");
  128.             status = 0;
  129.             }
  130.         else pkt_arp_handle = -1;    /* handle is out of service */
  131.  
  132.     if (pkt_rarp_handle != -1)
  133.         if (pdclose(pkt_rarp_handle) == 0)
  134.             {
  135.             outs("\r\nERROR releasing Packet Driver for RARP");
  136.             status = 0;
  137.             }
  138.         else pkt_rarp_handle = -1;    /* handle is out of service */
  139.     return (status);
  140. }
  141.  
  142. /* Deliver pointer to start of packet (destination address) or NULL.
  143.  * Simple linked list: byte flag, byte pkt seq number, int count, 
  144.  * byte data[count] and so on.
  145.  * Status on the link flag byte is
  146.  * 0 = end of buffer (count has size to point to start of buffer)
  147.  * 1 = this slot is free (unused)
  148.  * 2 = this slot has an unread packet
  149.  * 4 = this slot is allocated for a packet, packet not yet loaded into it
  150.  * 8 = this slot has a packet which has been read once
  151.  * A two byte count value follows the flag byte, for number of bytes in 
  152.  * this slot. Pktbuf_read remembers the pointer to the last-read slot.
  153. */
  154.  
  155. void * 
  156. pkt_received()
  157. {
  158.     register byte * p;
  159.     register int i;
  160.  
  161.     p = pktbuf_read;            /* start with last read */
  162.     for (i = 0; i < 2 * (BUFSIZE / (60 + 4)); i++)    /* 2 * max pkts */
  163.         {
  164.         if (*p == 2 && *(p+1) == pktrnum) /* 2 == ready to be read */
  165.             {            /* if this is the next pkt */
  166.              pktbuf_read = p;    /* where we have read */
  167.             *p = 8;            /* mark as have read */
  168.             pktrnum++;        /* next one to read */
  169.             return (p + 4);        /* return ptr to pkt*/
  170.             }
  171. /* if link is:      end of buf  free      ready      allocated  have read */
  172.         if (*p == 0 || *p == 1 || *p == 2 || *p == 4 || *p == 8)
  173.             {
  174.             p += 4 + *(word *)(p+2);   /* point at next link */
  175.             continue;
  176.             }
  177.         else                /* bad link information */
  178.             {
  179.             pkt_buf_wipe();        /* emergency treatment */
  180.             break;
  181.             }
  182.         if (p == pktbuf_read) break;    /* where we came in */
  183.         }
  184.     return (NULL);
  185. }
  186.     
  187. void
  188. pkt_buf_release(byte *p)    /* return a buffer to the pool */
  189. {
  190.     disable();        /* disallow interrupts here */
  191.     if (pktdevclass == PD_SLIP)
  192.         p -= 4;            /* just link info */
  193.     else
  194.         p -= 4 + 6 + 6 + 2;    /* link info and MAC header */
  195.  
  196.     if (*p == 8)        /* if packet has been read */
  197.         *p = 1;        /* mark link as freed */
  198.     enable();
  199. }
  200.  
  201. void
  202. pkt_buf_wipe()                    /* clear all buffers */
  203. {
  204.     disable();
  205.     pktbuf[0] = 1;                /* flag first link as free */
  206.     pktbuf[1] = 0;
  207.     *(word *)&pktbuf[2] = BUFSIZE - 8; /* free space, size - two links */
  208.  
  209.     pktbuf[BUFSIZE - 4] = 0;        /* flag as end of buffer */
  210.     pktbuf[BUFSIZE - 3] = 0;        /* pkt buffer seq number */
  211.                 /* count below points to start of buffer */
  212.     *(int *)&pktbuf[BUFSIZE - 2] = - BUFSIZE; 
  213.     pktbuf_read = pktbuf;                /* where last read */
  214.     pktbuf_wrote = &pktbuf[BUFSIZE - 4];        /* where last wrote*/
  215.     pktwnum = pktrnum = 0;        /* reset buffer sequence numbers */
  216.     enable();
  217. }
  218.  
  219.