home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / SRC / PCPKT.C < prev    next >
Text File  |  1994-11-28  |  8KB  |  279 lines

  1.  
  2. // FRAGSUPPORT enables support for packet reassembly of fragmented packets
  3. #define FRAGSUPPORT
  4.  
  5. #include <copyright.h>
  6. #include <wattcp.h>
  7. #include <elib.h>
  8. #include <dos.h>
  9. #ifdef __BORLANDC__
  10. #include <mem.h>
  11. #endif
  12. #include <string.h>
  13. #include <stdlib.h>
  14.  
  15.  
  16.  
  17. #define MAXBUFS     5    /* maximum number of Ethernet buffers */
  18. #define BUFSIZE     2100
  19.  
  20. #define DOS         0x21
  21. #define GETVECT     0x35
  22.  
  23. #define INT_FIRST 0x60
  24. #define INT_LAST  0x80
  25. #define PD_DRIVER_INFO    0x1ff
  26. #define PD_ACCESS     0x200
  27. #define PD_RELEASE    0x300
  28. #define PD_SEND        0x400
  29. #define PD_GET_ADDRESS    0x600
  30. #define  CARRY         1         /* carry bit in flags register */
  31.  
  32. word _pktipofs = 0;            /* offset from header to start of pkt */
  33. word pkt_interrupt;
  34. word pkt_ip_type = 0x0008;        /* these are intelled values */
  35. word pkt_arp_type = 0x608;
  36.  
  37. byte pktbuf[MAXBUFS][ BUFSIZE + 2 ];    /* first byte is busy flag, 2nd spare */
  38. word  pkt_ip_handle;
  39. word  pkt_arp_handle;
  40. //byte eth_addr[ 6 ] ;     // 94.11.19
  41. eth_address eth_addr;
  42. longword far *interrupts = 0L;
  43. char *pkt_line = "PKT DRVR";
  44.  
  45. // fragfix -- just a note this is intel
  46. #define IP_DF       0x0040      // Don't fragment bit set for FRAG Flags
  47.  
  48. // forwards and externs
  49. int pkt_init( void );
  50. extern void _pktentry();                          /* see asmpkt.asm */
  51. extern void _pktasminit( void far *, int, int );  /* see asmpkt.asm */
  52.  
  53. static int pkt_init( void )       /* 94.11.27 -- made static */
  54. {
  55.     struct REGPACK regs, regs2;
  56.     char far *temp;
  57.     int pd_type;    /* packet driver type */
  58.     int class;
  59.  
  60.     _pktasminit( pktbuf, MAXBUFS, BUFSIZE );
  61.     for (pkt_interrupt = INT_FIRST; pkt_interrupt <= INT_LAST; ++pkt_interrupt ) {
  62.  
  63.         temp = (char far *)getvect( pkt_interrupt );
  64.         if ( ! _fmemcmp( &(temp[3]), pkt_line, strlen( pkt_line )))
  65.             break;
  66.     }
  67.     if ( pkt_interrupt > INT_LAST ) {
  68.         outs("NO PACKET DRIVER FOUND\r\n");
  69.     return( 1 );
  70.     }
  71.  
  72.     /* lets find out about the driver */
  73.     regs.r_ax = PD_DRIVER_INFO;
  74.     intr( pkt_interrupt, ®s );
  75.  
  76.     /* handle old versions, assume a class and just keep trying */
  77.     if (regs.r_flags & CARRY ) {
  78.     for ( class = 0; class < 2; ++class ) {
  79.         _pktdevclass = (class) ? PD_SLIP : PD_ETHER;
  80.  
  81.         for (pd_type = 1; pd_type < 128; ++pd_type ) {
  82.         regs.r_ax = PD_ACCESS | _pktdevclass;  /* ETH, SLIP */
  83.         regs.r_bx = pd_type;        /* type */
  84.         regs.r_dx = 0;            /* if number */
  85.                 regs.r_cx = (_pktdevclass == PD_SLIP ) ? 0 : sizeof( pkt_ip_type);
  86.         regs.r_ds = FP_SEG( &pkt_ip_type );
  87.         regs.r_si = FP_OFF( &pkt_ip_type );
  88.                 regs.r_es = FP_SEG( _pktentry);
  89.         regs.r_di = FP_OFF( _pktentry);
  90.         intr( pkt_interrupt, ®s );
  91.         if ( ! (regs.r_flags & CARRY) ) break;
  92.         }
  93.  
  94.         if (pd_type == 128 ) {
  95.         outs("ERROR initializing packet driver\n\r");
  96.         return( 1 );
  97.         }
  98.         /* we have found a working type, so kill it */
  99.         regs.r_bx = regs.r_ax;    /* handle */
  100.         regs.r_ax = PD_RELEASE;
  101.         intr( pkt_interrupt, ®s );
  102.     }
  103.     } else {
  104.     pd_type = regs.r_dx;
  105.     switch ( _pktdevclass = (regs.r_cx >> 8)) {
  106.         case PD_ETHER : _pktipofs = 14;
  107.  
  108.             case PD_SLIP  : break;
  109.             default       : outs("ERROR: only Ethernet or SLIP packet drivers allowed\n\r");
  110.                 return( 1 );
  111.     }
  112.     }
  113.     regs.r_ax = PD_ACCESS | _pktdevclass;
  114.     regs.r_bx = 0xffff;                 /* any type */
  115.     regs.r_dx = 0;            /* if number */
  116.     regs.r_cx = (_pktdevclass == PD_SLIP) ? 0 : sizeof( pkt_ip_type );
  117.     regs.r_ds = FP_SEG( &pkt_ip_type );
  118.     regs.r_si = FP_OFF( &pkt_ip_type );
  119.     regs.r_es = FP_SEG( _pktentry);
  120.     regs.r_di = FP_OFF( _pktentry);
  121.     memcpy( ®s2, ®s, sizeof( regs ));
  122.     regs2.r_si = FP_OFF( &pkt_arp_type );
  123.     regs2.r_ds = FP_SEG( &pkt_arp_type );
  124.  
  125.     intr( pkt_interrupt, ®s );
  126.     if ( regs.r_flags & CARRY ) {
  127.     outs("ERROR # 0x");
  128.     outhex( regs.r_dx >> 8 );
  129.     outs(" accessing packet driver\n\r" );
  130.     return( 1 );
  131.     }
  132.     pkt_ip_handle = regs.r_ax;
  133.  
  134.     if (_pktdevclass != PD_SLIP) {
  135.     intr( pkt_interrupt, ®s2 );
  136.     if ( regs2.r_flags & CARRY ) {
  137.         regs.r_ax = PD_RELEASE;
  138.         regs.r_bx = pkt_ip_handle;
  139.         intr( pkt_interrupt, ®s );
  140.  
  141.         outs("ERROR # 0x");
  142.         outhex( regs2.r_dx >> 8 );
  143.         outs(" accessing packet driver\n\r" );
  144.         return( 1 );
  145.     }
  146.     pkt_arp_handle = regs2.r_ax;
  147.     }
  148.  
  149.     /* get ethernet address */
  150.     regs.r_ax = PD_GET_ADDRESS;
  151.     regs.r_bx = pkt_ip_handle;
  152.     regs.r_es = FP_SEG( ð_addr );
  153.     regs.r_di = FP_OFF( ð_addr );
  154.     regs.r_cx = sizeof( eth_addr );
  155.     intr( pkt_interrupt, ®s );
  156.     if ( regs.r_flags & CARRY ) {
  157.     outs("ERROR # reading ethernet address\n\r" );
  158.     return( 1 );
  159.     }
  160.  
  161.     return( 0 );
  162. }
  163.  
  164. void pkt_release( void )
  165. {
  166.     struct REGPACK regs;
  167.     int error;
  168.  
  169.     if ( _pktdevclass != PD_SLIP ) {
  170.     regs.r_ax = PD_RELEASE;
  171.     regs.r_bx = pkt_arp_handle;
  172.     intr( pkt_interrupt, ®s );
  173.     if (regs.r_flags & CARRY ) {
  174.         outs("ERROR releasing packet driver for ARP\n\r");
  175.     }
  176.     }
  177.  
  178.     regs.r_ax = PD_RELEASE;
  179.     regs.r_bx = pkt_ip_handle;
  180.     intr( pkt_interrupt, ®s );
  181.     if (regs.r_flags & CARRY )
  182.     outs("ERROR releasing packet driver for IP\n\r");
  183.  
  184.     return;
  185. }
  186.  
  187. int pkt_send( char *buffer, int length )
  188. {
  189.     struct REGPACK regs;
  190.     int retries;
  191.  
  192.     retries = 5;
  193.     while (retries--) {
  194.         regs.r_ax = PD_SEND;
  195.         regs.r_ds = FP_SEG( buffer );
  196.         regs.r_si = FP_OFF( buffer );
  197.         regs.r_cx = length;
  198.         intr( pkt_interrupt, ®s );
  199.         if ( regs.r_flags & CARRY )
  200.             continue;
  201.         return( 0 );
  202.     }
  203.     return( 1 );
  204. }
  205.  
  206. /* return a buffer to the pool */
  207. void pkt_buf_wipe( void )
  208. {
  209.     memset( pktbuf, 0, sizeof( byte ) * MAXBUFS * (BUFSIZE+ 2));
  210. }
  211.  
  212. void pkt_buf_release( char *ptr )
  213. {
  214.     *(ptr - (2 + _pktipofs)) = 0;
  215. }
  216.  
  217. void * pkt_received( void )
  218. {
  219.     word old;
  220.     int i;
  221.     word oldin, newin;    /* ip sequence numbers */
  222.     eth_Header  * temp_e = NULL;
  223.     in_Header   * temp;
  224.     byte        * t_buf;
  225.     extern int active_frags;
  226.  
  227.     /* check if there are any */
  228.     old = oldin = 0xffff;
  229.  
  230.     // Do frag timeout bit sad if we got the bit of one we're about to kill
  231. #ifdef FRAGSUPPORT
  232.     if ( active_frags ) timeout_frags();
  233. #endif // FRAGSUPPORT
  234.     for ( i = 0 ; i < MAXBUFS; ++i ) {
  235.     if ( *pktbuf[i] != 1 ) continue;
  236.  
  237.         // check if fragmented - SLIP supported
  238.     temp = (in_Header *) &pktbuf[ i ][ 2 ];
  239.         if ( _pktdevclass == PD_ETHER ) {
  240.             temp_e = (eth_Header *) temp;
  241. // fragfix -- next line did pointer arith so incorrectly added
  242. //               ... * sizeof(typeof(*temp)) instead of ... * 1
  243. //            temp += sizeof( eth_Header );
  244.             temp = (in_Header *)((byte*)temp + sizeof(eth_Header));
  245.         }
  246.  
  247. #ifdef FRAGSUPPORT
  248.         if ((( _pktdevclass == PD_SLIP ) || ( temp_e->type == IP_TYPE ))
  249. // fragfix -- next line, need ~ to clear DF bit, not all others
  250. //              ... check is either MF set or frag offset not 0
  251.                 && ( temp->frags & ~IP_DF )) {
  252. //            && ( temp->frags & IP_DF )) {
  253.  
  254.             if ( ( t_buf = fragment( temp )) == NULL )
  255.                 // pass pointer to ip section of buffer
  256.                 continue;
  257.             else
  258.                 return( t_buf );
  259.         }
  260. #endif // FRAGSUPPORT
  261.         newin = *(word *)( &pktbuf[i][ _pktipofs + 4 + 2 ]);
  262.         if ( newin <= oldin ) {
  263.             oldin = newin;
  264.             old = i;
  265.         }
  266.     }
  267.  
  268.     return( (old == 0xffff) ? NULL : &pktbuf[old][2] );
  269. }
  270.  
  271. eth_address *_pkt_eth_init( void )
  272. {
  273.     if ( pkt_init() ) {
  274.         outs("Program halted\r\n");
  275.     exit( 1 );
  276.     }
  277.     return( ð_addr );
  278. }
  279.