home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / LITECOMM / QUICKB.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  25KB  |  1,121 lines

  1. /*
  2. **  QUICKB.C - Quick B Protocol Support routines
  3. **
  4. **    converted to C by Paul M. Resch
  5. **  adapted to LiteComm(tm) ToolBox by Information Technology, Ltd.
  6. */
  7.  
  8. /*
  9. ** This module implements the B-Protocol Functions.
  10. **
  11. ** bp_DLE should be invoked whenever a <DLE> is received.
  12. ** bp_ENQ should be called whenever an <ENQ> is received.
  13. ** bp_ESC_I should be called when the sequence <ESC><I> is received.
  14. **
  15. ** This source was originally derived from QUICKB.INC, written by
  16. ** Russ Ranshaw, CompuServe Incorporated.
  17. **
  18. */
  19.  
  20. #include "litecomm.h"
  21. #include "litexm.h"
  22. #include <vcstdio.h>
  23.  
  24. #include <dos.h>
  25.  
  26. #ifdef M_I86
  27. #include <stdlib.h>
  28. #include <fcntl.h>
  29. #include <io.h>
  30. #include <sys\types.h>
  31. #include <sys\stat.h>
  32. #endif
  33.  
  34. #ifdef __TURBOC__
  35. #include <conio.h>
  36. #include <fcntl.h>
  37. #include <io.h>
  38. #include <stat.h>
  39. #endif
  40.  
  41. extern unsigned int port;                /* defined in TTL main */
  42.  
  43. #define    TRUE    1
  44. #define    FALSE    0
  45.  
  46. #undef DLE
  47. #undef NAK
  48.  
  49. #define    DLE        16
  50. #define    ETX        03
  51. #define    NAK        21
  52. #define    ENQ        05
  53. #define    CR        0x0D
  54. #define    LF        0x0A
  55. #define    MAX_BUF_SIZE    1032            /* Largest data block we can handle */
  56. #define    MAX_SA        2                    /* Maximum number of waiting packets */
  57.  
  58. #define    DEF_BUF_SIZE    511                /* Default data block */
  59. #define    DEF_WS        1                    /* I can send 2 packets ahead */
  60. #define    DEF_WR        1                    /* I can receive single send-ahead */
  61. #define    DEF_BS        8                    /* I can handle 1024 bytes */
  62. #define    DEF_CM        1                    /* I can handle CRC */
  63. #define    DEF_DQ        1                    /* I can handle non-quoted NUL */
  64.  
  65. #define    MAX_ERRORS    10
  66.  
  67. #define incr_seq(v)    (v == 9 ? 0 : v+1)    /* macro to incr seq number */
  68. #define incr_SA(v)  (v == MAX_SA ? 0 : v + 1 )
  69. #define    send_enq() (lc_put(port, ENQ))
  70.  
  71.  
  72. /*
  73. ** Receive States
  74. */
  75.  
  76. #define    R_GET_DLE        0
  77. #define    R_GET_B            1
  78. #define    R_GET_SEQ        2
  79. #define    R_GET_DATA        3
  80. #define    R_GET_CHECKSUM    4
  81. #define    R_SEND_ACK        5
  82. #define    R_TIMED_OUT        6
  83. #define    R_SUCCESS        7
  84.  
  85. /*
  86. ** Send States
  87. */
  88.  
  89. #define    S_GET_DLE    1
  90. #define    S_GET_NUM    2
  91. #define    S_HAVE_ACK    3
  92. #define    S_GET_PACKET    4
  93. #define    S_TIMED_OUT    5
  94. #define    S_SEND_NAK    6
  95. #define    S_SEND_ENQ    7
  96. #define    S_SEND_DATA    8
  97.  
  98. typedef    struct    PACKETB
  99. {
  100.     int        seq;                        /* Packet's sequence number */
  101.     int        num;                        /* Number of bytes in packet */
  102.     unsigned char buf[MAX_BUF_SIZE];     /* Actual packet data */
  103. } PACKET;
  104.  
  105. static    PACKET    SA_Buf[MAX_SA+1];          /* Send-ahead buffers */
  106.  
  107. /*
  108. ** Table of control characters that need to be masked
  109. */
  110.  
  111. static    char mask_table[] =
  112. {
  113.     0, 0, 0, 1, 0, 1, 0, 0,               /* NUL SOH SOB ETX EOT ENQ SYN BEL */
  114.     0, 0, 0, 0, 0, 0, 0, 0,                /* BS  HT  LF  VT  FF  CR  SO  SI  */
  115.     1, 1, 0, 1, 0, 1, 0, 0,                /* DLE DC1 DC2 DC3 DC4 NAK ^V  ^W  */
  116.     0, 0, 0, 0, 0, 0, 0, 0                /* CAN ^Y  ^Z  ESC ?   ?   ?   ?   */
  117. };
  118.  
  119. static    char    hex_digit[] = "0123456789ABCDEF";
  120.  
  121. static    int    seq_num;                    /* Current Sequence Number */
  122. static    int    lchecksm;                    /* May hold CRC */
  123. static    int    r_size;                        /* size of receiver buffer */
  124. static    unsigned int s_counter,
  125.                      r_counter;
  126. static    int    timed_out;                    /* we timed out before receiving */
  127. static    int    cchar;                        /* current character */
  128. static    int    masked;                        /* TRUE if ctrl character 'masked' */
  129. static    int    packet_received;            /* True if a packet was received */
  130. static    unsigned char r_buffer[MAX_BUF_SIZE];
  131.  
  132. /*
  133. ** Other End's Parameters
  134. */
  135.  
  136. static    char    His_WS;                    /* Sender's Window Send */
  137. static    char    His_WR;                    /* Sender's Window Receive */
  138. static    char    His_BS;                    /* Sender's Block Size */
  139. static    char    His_CM;                    /* Sender's Check Method */
  140.  
  141. /*
  142. ** Negotiated Parameters
  143. */
  144.  
  145. static    char    Our_WS;                    /* Negotiated Window Send */
  146. static    char    Our_WR;                    /* Negotiated Window Receive */
  147. static    char    Our_BS;                    /* Negotiated Block Size */
  148. static    char    Our_CM;                    /* Negotiated Check Method */
  149.  
  150. static    int    Quick_B;                    /* True if Quick B in effect */
  151. static    int    Use_CRC;                    /* True if CRC in effect */
  152. static    int    buffer_size;                /* Our_BS * 4 */
  153. static    int    SA_Max;                        /* 1 if SA not enabled, else MAX_SA */
  154. static    int    SA_Enabled;                    /* True if Send-Ahead is permitted  */
  155. static    int    ack_SA;                        /* Which SA_Buf is waiting for ACK */
  156. static    int    fill_SA;                    /* Which SA_Buf is ready, new data */
  157. static    int    SA_Waiting;                    /* Num of SA_Buf's waiting for ACK */
  158. static  int blkct;                      /* block counter for display */
  159. extern  char strbuf[];                  /* defined in qbttl */
  160.  
  161. static    void do_transport_parameters(void);
  162.  
  163. /*
  164. ** crc
  165. **
  166. ** Calculates XMODEM-style CRC (uses the CCITT V.41 polynomial but
  167. ** completely backwards from the normal bit ordering).
  168. */
  169.  
  170.  
  171. static    unsigned    crc_table[] =
  172. {
  173.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  174.     0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  175.     0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  176.     0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  177.     0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  178.     0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  179.     0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  180.     0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  181.     0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  182.     0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  183.     0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  184.     0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  185.     0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  186.     0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  187.     0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  188.     0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  189.     0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  190.     0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  191.     0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  192.     0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  193.     0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  194.     0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  195.     0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  196.     0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  197.     0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  198.     0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  199.     0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  200.     0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  201.     0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  202.     0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  203.     0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  204.     0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
  205. };
  206.  
  207. static    unsigned int    crc_16;
  208.  
  209. /*
  210. ** Upd_CRC updates crc_16 and returns the updated value.
  211. */
  212.  
  213. static    unsigned int    upd_CRC (value)
  214. unsigned int    value;
  215. {
  216.     crc_16 = crc_table [((crc_16 >> 8) ^ (value)) & 0xff] ^    (crc_16 << 8);
  217.     return( crc_16 );
  218. }
  219.  
  220. /*
  221. ** Update the checksum/CRC
  222. */
  223.  
  224. static    void    do_checksum(c)
  225. int    c;
  226. {
  227.     if (Quick_B && Use_CRC)
  228.         lchecksm = upd_CRC (c);
  229.     else
  230.     {
  231.         lchecksm = lchecksm << 1;
  232.  
  233.         if (lchecksm > 255)
  234.             lchecksm = (lchecksm & 0xFF) + 1;
  235.  
  236.         lchecksm = lchecksm + c;
  237.  
  238.         if (lchecksm > 255)
  239.             lchecksm = (lchecksm & 0xFF) + 1;
  240.     }
  241. }
  242.  
  243. static    void    send_failure( code )
  244. int    code;
  245. {
  246.     register PACKET    *p;
  247.  
  248.     ack_SA = 0;
  249.     fill_SA = 0;
  250.     SA_Waiting = 0;
  251.  
  252.     p = &SA_Buf [0];
  253.     p->buf[0] = 'F';
  254.     p->buf[1] = code;
  255.  
  256.     if ( send_packet (1))
  257.         SA_Flush();   /* Gotta wait for the host to ACK it */
  258. }
  259.  
  260. /*
  261. ** bp_ENQ is called when the terminal emulator receives the character <ENQ>
  262. ** from the host.  Its purpose is to initialize for B Protocol and tell the
  263. ** host that we support Quick B.
  264. */
  265.  
  266. void    bp_ENQ()
  267. {
  268.     seq_num = 0;
  269.     buffer_size = 511;               /* Set up defaults */
  270.     Quick_B     = FALSE;             /* Not Quick B Protocol */
  271.     Use_CRC     = FALSE;             /* Not CRC_16      */
  272.     SA_Enabled  = FALSE;             /* No Send-Ahead by us */
  273.     SA_Max      = 1;                 /* = single packet sent */
  274.  
  275.     lc_put (port, DLE);
  276.     lc_put (port, '+');
  277.  
  278.     lc_put (port, DLE);
  279.     lc_put (port, '0');
  280. }
  281.  
  282. /*
  283. ** bp_ESC_I is called when <ESC><I> is received by the terminal emulator.
  284. ** Note that Quick B allows +XX to be added to the end of the response, where
  285. ** XX is the two hex digits of the standard B Protocol checksum of the
  286. ** preceeding characters in the response.  The Qbttl program also supports
  287. ** standard VIDTEX cursor control.
  288. */
  289. static    char    esc_I_response[] = "#VCO,CC,PB,DT,+";
  290.  
  291. void    bp_ESC_I()
  292. {
  293.     int    save_Use_CRC;
  294.     register int    i;
  295.  
  296.     save_Use_CRC = Use_CRC;
  297.     Use_CRC = FALSE;
  298.     lchecksm = 0;
  299.  
  300.     i = 0;
  301.     while( esc_I_response[i] )
  302.     {
  303.         lc_put(port, esc_I_response [i]);
  304.         do_checksum (esc_I_response [i]);
  305.         i++;
  306.     }
  307.  
  308. /*
  309. ** Append two hex digits of checksum to response
  310. */
  311.  
  312.     lc_put(port,  hex_digit[ (lchecksm >> 4) & 0x0F ]);
  313.     lc_put(port,  hex_digit[ lchecksm & 0x0F ] );
  314.     lc_put(port, CR);
  315.  
  316.     Use_CRC = save_Use_CRC;
  317. }
  318.  
  319.  
  320. static    void    send_masked_byte(c)
  321. int    c;
  322. {
  323.     c = c & 0xFF;
  324.  
  325.     if (c < 32)
  326.     {
  327.         if (mask_table [c] != 0)
  328.         {
  329.             lc_put(port, DLE);
  330.             lc_put(port, c + '@');
  331.         }
  332.         else
  333.             lc_put(port, c);
  334.     }
  335.     else
  336.         lc_put(port, c);
  337.  
  338.     s_counter = (s_counter + 1) % 512;
  339. }
  340.  
  341. static    void    send_ack()
  342. {
  343.     lc_put(port, DLE);
  344.     lc_put(port, seq_num + '0');
  345. }
  346.  
  347.  
  348. static    int    read_byte()
  349. {
  350.     timed_out = FALSE;
  351.  
  352.     cchar = wait(port, 10);
  353.  
  354.     if (cchar < 0 )
  355.         return( FALSE );
  356.  
  357.     r_counter = (r_counter + 1) % 512;
  358.     return( TRUE );
  359. }
  360.  
  361.  
  362. static    int    read_masked_byte()
  363. {
  364.     masked = FALSE;
  365.  
  366.     if (read_byte() == FALSE)
  367.         return( FALSE );
  368.  
  369.     if (cchar == DLE)
  370.     {
  371.         if (read_byte() == FALSE)
  372.             return( FALSE );
  373.         cchar &= 0x1F;
  374.         masked = TRUE;
  375.     }
  376.  
  377.     return( TRUE );
  378. }
  379.  
  380. static    int    read_packet (lead_in_seen, from_send_packet)
  381. int    lead_in_seen, from_send_packet;
  382. /*
  383. ** Lead_in_Seen is TRUE if the <DLE><B> has been seen already.
  384. ** from_send_packet is TRUE if called from Send_Packet
  385. **    (causes exit on first error detected)
  386. **
  387. ** Returns True if packet is available from host.
  388. */
  389. {
  390.     int    State, next_seq, block_num, errors, new_cks;
  391.     int    i;
  392.  
  393.     packet_received = FALSE;
  394.     for(i=0; i<buffer_size; i++ )
  395.         r_buffer[i] = 0;
  396.     next_seq = (seq_num +  1) % 10;
  397.     errors = 0;
  398.  
  399.     if (lead_in_seen)        /* Start off on the correct foot */
  400.         State = R_GET_SEQ;
  401.     else
  402.         State = R_GET_DLE;
  403.  
  404.     while (TRUE)
  405.     {
  406.         switch  (State)
  407.         {
  408.             case R_GET_DLE :
  409.                 if (_abort_flag)
  410.                 {
  411.                     send_failure ('A');
  412.                     return( FALSE );
  413.                 }
  414.  
  415.                 if (!read_byte())
  416.                     State = R_TIMED_OUT;
  417.                 else
  418.                     if ((cchar & 0x7F) == DLE)
  419.                         State = R_GET_B;
  420.                     else
  421.                         if ((cchar & 0x7F) == ENQ)
  422.                             State = R_SEND_ACK;
  423.                 break;
  424.  
  425.             case R_GET_B :
  426.                 if (!read_byte())
  427.                     State = R_TIMED_OUT;
  428.                 else
  429.                     if ((cchar & 0x7F) == 'B')
  430.                         State = R_GET_SEQ;
  431.                     else
  432.                         if (cchar == ENQ)
  433.                             State = R_SEND_ACK;
  434.                         else
  435.                             State = R_GET_DLE;
  436.                 break;
  437.  
  438.             case R_GET_SEQ :
  439.                 if (!read_byte())
  440.                     State = R_TIMED_OUT;
  441.                 else
  442.                     if (cchar == ENQ)
  443.                         State = R_SEND_ACK;
  444.                     else
  445.                     {
  446.                         if (Quick_B && Use_CRC)
  447.                             lchecksm = crc_16 = -1;
  448.                         else
  449.                             lchecksm = 0;
  450.  
  451.                         block_num = cchar - '0';
  452.  
  453.                         do_checksum(cchar);
  454.  
  455.                         i = 0;
  456.                         State = R_GET_DATA;
  457.                     }
  458.                 break;
  459.  
  460.             case R_GET_DATA :
  461.                 r_counter = 0;
  462.                 if (!read_masked_byte())
  463.                     State = R_TIMED_OUT;
  464.                 else
  465.                     if ((cchar == ETX) && !masked)
  466.                     {
  467.                         do_checksum(ETX);
  468.                         State = R_GET_CHECKSUM;
  469.                     }
  470.                     else
  471.                     {
  472.                         r_buffer[i] = cchar;
  473.                         i = i + 1;
  474.                         do_checksum(cchar);
  475.                     }
  476.                 break;
  477.  
  478.             case R_GET_CHECKSUM :
  479.                 if (!read_masked_byte())
  480.                     State = R_TIMED_OUT;
  481.                 else
  482.                 {
  483.                     if (Quick_B && Use_CRC)
  484.                     {
  485.                         lchecksm = upd_CRC (cchar);
  486.  
  487.                         if (!read_masked_byte())
  488.                             new_cks = lchecksm ^ 0xFF;
  489.                         else
  490.                         {
  491.                             lchecksm = upd_CRC (cchar);
  492.                             new_cks = 0;
  493.                         }
  494.                     }
  495.                     else
  496.                         new_cks = cchar;
  497.  
  498.                     if (new_cks != lchecksm)
  499.                         State = R_TIMED_OUT;
  500.                     else
  501.                         if (r_buffer[0] == 'F') /* Watch for Failure Packet */
  502.                             State = R_SUCCESS;  /* which is always accepted */
  503.                         else
  504.                             if (block_num == seq_num) /* Watch for dup block */
  505.                                 State = R_SEND_ACK;
  506.                             else
  507.                                 if (block_num != next_seq)
  508.                                     State = R_TIMED_OUT; /* Bad sequence number */
  509.                                 else
  510.                                     State = R_SUCCESS;
  511.                 }
  512.                 break;
  513.  
  514.             case R_TIMED_OUT :
  515.                 errors = errors + 1;
  516.  
  517.                 if ((errors > MAX_ERRORS) || (from_send_packet))
  518.                     return( FALSE );
  519.  
  520.                 lc_put(port, NAK);
  521.  
  522.                 if (from_send_packet)
  523.                     return( FALSE );
  524.  
  525.                 State = R_GET_DLE;
  526.                 break;
  527.  
  528.             case R_SEND_ACK :
  529.                 send_ack();
  530.                 State = R_GET_DLE;        /* wait for the next block */
  531.                 break;
  532.  
  533.             case R_SUCCESS :
  534.                 seq_num = block_num;
  535.                 r_size = i;
  536.                 packet_received = TRUE;
  537.                 return( TRUE );
  538.         }
  539.     }
  540. }
  541.  
  542. static    void    send_data (Buffer_Number)
  543. int    Buffer_Number;
  544. {
  545.     int    i;
  546.     register PACKET    *p;
  547.  
  548.     s_counter = 0;
  549.     p = &SA_Buf [Buffer_Number];
  550.     if (Quick_B && Use_CRC)
  551.         lchecksm = crc_16 = -1;
  552.     else
  553.         lchecksm = 0;
  554.  
  555.     lc_put(port, DLE);
  556.     lc_put(port, 'B');
  557.  
  558.     lc_put(port, p->seq + '0');
  559.     do_checksum(p->seq + '0');
  560.  
  561.     for (i = 0; i<=p->num; i++ )
  562.     {
  563.         send_masked_byte(p->buf[i]);
  564.         do_checksum(p->buf[i]);
  565.     }
  566.  
  567.     lc_put(port, ETX);
  568.     do_checksum (ETX);
  569.  
  570.     if (Quick_B && Use_CRC)
  571.         send_masked_byte (lchecksm >> 8);
  572.  
  573.     send_masked_byte(lchecksm);
  574. }
  575.  
  576. /*
  577. ** ReSync is called to restablish syncronism with the remote.  This is
  578. ** accomplished by sending <ENQ><ENQ> and waiting for the sequence
  579. ** <DLE><d><DLE><d> to be received, ignoring everything else.
  580. **
  581. ** Return is -1 on time out, else the digit <d>.
  582. */
  583. #define    GET_FIRST_DLE        1
  584. #define    GET_FIRST_DIGIT        2
  585. #define    GET_SECOND_DLE        3
  586. #define    GET_SECOND_DIGIT    4
  587.  
  588. static    int    ReSync()
  589. {
  590.     int    State, Digit_1;
  591.  
  592.     lc_put(port, ENQ);     /* Send <ENQ><ENQ> */
  593.     lc_put(port, ENQ);
  594.     State = GET_FIRST_DLE;
  595.  
  596.     while(1)
  597.     {
  598.         switch (State)
  599.         {
  600.             case GET_FIRST_DLE :
  601.                 if( !read_byte() )
  602.                     return( -1 );
  603.                 if( cchar == DLE )
  604.                     State = GET_FIRST_DIGIT;
  605.                 break;
  606.             case GET_FIRST_DIGIT :
  607.                 if( !read_byte() )
  608.                     return( -1 );
  609.                 if( (cchar >= '0') && (cchar <= '9') )
  610.                 {
  611.                     Digit_1 = cchar;
  612.                     State = GET_SECOND_DLE;
  613.                 }
  614.                 break;
  615.             case GET_SECOND_DLE :
  616.                 if( !read_byte() )
  617.                     return( -1 );
  618.                 if( cchar == DLE )
  619.                     State = GET_SECOND_DIGIT;
  620.                 break;
  621.             case GET_SECOND_DIGIT :
  622.                 if( !read_byte() )
  623.                     return( -1 );
  624.                 if( (cchar >= '0') && (cchar <= '9') )
  625.                 {
  626.                     if( Digit_1 == cchar )
  627.                         return( cchar );
  628.                     else
  629.                     {
  630.                         Digit_1 = cchar;
  631.                         State = GET_SECOND_DLE;
  632.                     }
  633.                 }
  634.                 else
  635.                     State = GET_SECOND_DLE;
  636.                 break;
  637.         }
  638.     }
  639. }
  640.  
  641. /*
  642. ** get_ACK is called to wait until the SA_Buf indicated by ack_SA
  643. ** has been ACKed by the host.
  644. */
  645. static    int    get_ACK()
  646. {
  647.     int    State, errors, block_num, i;
  648.     int    Sent_ENQ;
  649.     int    SA_Index;
  650.  
  651.     packet_received = FALSE;
  652.     errors = 0;
  653.     Sent_ENQ = FALSE;
  654.     State = S_GET_DLE;
  655.  
  656.     while( TRUE )
  657.     {
  658.         switch (State) {
  659.         case S_GET_DLE :
  660.             if (_abort_flag)
  661.             {
  662.                 send_failure ('A');
  663.                 return( FALSE );
  664.             }
  665.  
  666.             if (!read_byte())
  667.                 State = S_TIMED_OUT;
  668.             else if (cchar == DLE)
  669.                 State = S_GET_NUM;
  670.             else if (cchar == NAK)
  671.             {
  672.                 if (++errors > MAX_ERRORS)
  673.                     return( FALSE );
  674.                 State = S_SEND_ENQ;
  675.             }
  676.             else if (cchar == ETX)
  677.                 State = S_SEND_NAK;
  678.             break;
  679.  
  680.         case S_GET_NUM :
  681.             if (!read_byte())
  682.                 State = S_TIMED_OUT;
  683.             else if ((cchar >= '0') && (cchar <= '9'))
  684.                 State = S_HAVE_ACK;    /* Received ACK */
  685.             else if (cchar == 'B')
  686.                 State = S_GET_PACKET; /* Try to get packet */
  687.             else if (cchar == NAK)
  688.             {
  689.                 if (++errors > MAX_ERRORS)
  690.                     return( FALSE );
  691.                 State = S_SEND_ENQ;
  692.             }
  693.             else
  694.                 State = S_TIMED_OUT;
  695.             break;
  696.  
  697.         case S_GET_PACKET :
  698.             if (read_packet (TRUE, TRUE))
  699.             {
  700.                 if (r_buffer [0] == 'F')
  701.                 {
  702.                     send_ack();
  703.                     return( FALSE );
  704.                 }
  705.                 else
  706.                     return( TRUE );
  707.             }
  708.  
  709.             State = S_TIMED_OUT; /* On a bad receive, try again */
  710.             break;
  711.         case S_HAVE_ACK:
  712.             block_num = cchar - '0';
  713.             if (SA_Buf [ack_SA].seq == block_num)
  714.             {    /* This is the one we're waiting for */
  715.                 ack_SA = incr_SA(ack_SA);
  716.                 SA_Waiting--;
  717.                 return( TRUE );
  718.             }
  719.             else
  720.                 if (SA_Buf [incr_SA (ack_SA)].seq == block_num)
  721.                 {    /* Must have missed an ACK */
  722.                     ack_SA = incr_SA (ack_SA);
  723.                     ack_SA = incr_SA (ack_SA);
  724.                     SA_Waiting -= 2;
  725.                     return( TRUE );
  726.                 }
  727.                 else 
  728.                     if (SA_Buf [ack_SA].seq == incr_seq (block_num))
  729.                     {
  730.                         if( Sent_ENQ )
  731.                             State = S_SEND_DATA;
  732.                         else
  733.                             State = S_GET_DLE;
  734.                     }
  735.                     else
  736.                         State = S_TIMED_OUT;
  737.             Sent_ENQ = FALSE;
  738.             break;
  739.         case S_TIMED_OUT :
  740.             if (++errors > MAX_ERRORS)
  741.                 return( FALSE );
  742.  
  743.             State = S_SEND_ENQ;
  744.             break;
  745.  
  746.         case S_SEND_NAK :
  747.             if (++errors > MAX_ERRORS)
  748.                 return( FALSE );
  749.  
  750.             lc_put(port, NAK);
  751.  
  752.             State = S_GET_DLE;
  753.             break;
  754.  
  755.         case S_SEND_ENQ :
  756.             if (++errors > MAX_ERRORS)
  757.                 return( FALSE );
  758.             cchar = ReSync();
  759.             if( cchar == -1 )
  760.                 State = S_SEND_ENQ;
  761.             else
  762.                 State = S_HAVE_ACK;
  763.             Sent_ENQ = TRUE;
  764.             break;
  765.  
  766.         case S_SEND_DATA :
  767.             SA_Index = ack_SA;
  768.  
  769.             for (i = 1; i<=SA_Waiting; i++ )
  770.             {
  771.                 send_data (SA_Index);
  772.                 SA_Index = incr_SA (SA_Index);
  773.             }
  774.  
  775.             State = S_GET_DLE;
  776.             Sent_ENQ = FALSE;
  777.             break;
  778.         }
  779.     }
  780. } /* get_ACK */
  781.  
  782. static    int    send_packet (size)
  783. int    size;
  784. {
  785.     if (SA_Waiting == SA_Max)
  786.         if (!get_ACK())
  787.             return( FALSE );
  788.  
  789.     seq_num = incr_seq (seq_num);
  790.     SA_Buf [fill_SA].seq = seq_num;
  791.     SA_Buf [fill_SA].num = size;
  792.     send_data (fill_SA);
  793.     fill_SA = incr_SA (fill_SA);
  794.     SA_Waiting = SA_Waiting + 1;
  795.     return( TRUE );
  796. }
  797. /*
  798. ** SA_Flush is called after sending the last packet to get host's
  799. ** ACKs on outstanding packets.
  800. */
  801. static    int    SA_Flush()
  802. {
  803.     while( SA_Waiting != 0 )
  804.         if (!get_ACK())
  805.             return( FALSE );
  806.     return( TRUE );
  807. }
  808.  
  809. /* Send_File is called to send a file to the host */
  810. static    int    send_file(name)
  811. char    name[];
  812. {
  813.     int    fd;
  814.     int    n;
  815.     register PACKET    *p;
  816.  
  817.     fd = open(name, (O_BINARY | O_RDONLY));
  818.  
  819.     if (fd < 0)
  820.       {
  821.         send_failure('E');
  822.         urgentmsg ("ERROR","** Cannot find that file **");
  823.         return( FALSE );
  824.     }
  825.  
  826.     do
  827.     {
  828.         p = &SA_Buf [fill_SA];
  829.         p->buf[0] = 'N';
  830.         n = read(fd, &p->buf[1], buffer_size);
  831.  
  832.         if (n > 0)
  833.         {
  834.             if (send_packet (n) == FALSE)
  835.                 return( FALSE );
  836.             sprintf(strbuf, "Sent Block: %d", blkct++);
  837.             atsay(4,1,strbuf);
  838.         }
  839.     } while( n == buffer_size );
  840.  
  841.     close (fd);
  842.  
  843.     if (n < 0)
  844.     {
  845.         send_failure ('E');
  846.         urgentmsg ("ERROR", "** Read failure...aborting **");
  847.         return(FALSE);
  848.     }
  849.  
  850. /* Inform host that the file was sent */
  851.     p = &SA_Buf [fill_SA];
  852.     p->buf[0] = 'T';
  853.     p->buf[1] = 'C';
  854.  
  855.     if (send_packet(2) == FALSE)
  856.         return( FALSE );
  857.     else
  858.     {
  859.         say( "Waiting for host..." );
  860.         if (!SA_Flush())
  861.             return( FALSE );
  862.         return( TRUE );
  863.     }
  864. }
  865.  
  866. /*
  867. ** do_transport_parameters is called when a Packet type of + is received.
  868. ** It sends a packet of our local Quick B parameters and sets the Our_xx
  869. ** parameters to the minimum of the sender's and our own parameters.
  870. */
  871. static    void do_transport_parameters()
  872. {
  873.     register PACKET    *p;
  874.  
  875.     His_WS = r_buffer [1];     /* Pick out Sender's parameters */
  876.     His_WR = r_buffer [2];
  877.     His_BS = r_buffer [3];
  878.     His_CM = r_buffer [4];
  879.  
  880.     p = &SA_Buf [fill_SA];
  881.     p->buf [0] = '+';  /* Prepare to return our own parameters */
  882.     p->buf [1] = DEF_WS;
  883.     p->buf [2] = DEF_WR;
  884.     p->buf [3] = DEF_BS;
  885.     p->buf [4] = DEF_CM;
  886.     p->buf [5] = DEF_DQ;
  887.  
  888.     if (!send_packet (5))
  889.         return;
  890.  
  891.     if (SA_Flush())                 /* Wait for host's ACK on our packet */
  892.     {
  893. /* Take minimal subset of Transport Params. */
  894. /* If he can send ahead, we can receive it. */
  895.         Our_WR = (His_WS < DEF_WR) ? His_WS : DEF_WR;
  896.  
  897. /* If he can receive send ahead, we can send it. */
  898.         Our_WS = (His_WR < DEF_WS) ? His_WR : DEF_WS;
  899.  
  900.         Our_BS = His_BS < DEF_BS ? His_BS : DEF_BS;
  901.  
  902.         Our_CM = His_CM < DEF_CM ? His_CM : DEF_CM;
  903.  
  904.         if (Our_BS == 0)
  905.             Our_BS = 4;    /* Default */
  906.  
  907.         buffer_size = Our_BS * 128;
  908.  
  909.         Quick_B = TRUE;
  910.  
  911.         if (Our_CM == 1)
  912.             Use_CRC = TRUE;
  913.  
  914.         if (Our_WS != 0)
  915.         {
  916.             SA_Enabled = TRUE;
  917.             SA_Max     = MAX_SA;
  918.         }
  919.     }
  920. }
  921.  
  922. /*
  923.   do_application_parameters is called when a ? packet is received.
  924.   This version ignores the host's packet and returns a ? packet
  925.   saying that normal B Protocol File Transfer is supported.
  926.   (Well, actually it says that no extended application packets are
  927.    supported.  The T packet is assumed to be standard.) */
  928.  
  929. static    void    do_application_parameters()
  930. {
  931.     register PACKET    *p;
  932.  
  933.     p = &SA_Buf [fill_SA];
  934.     p->buf[0] = '?';     /* Build the ? packet */
  935.     p->buf[1] = 1;             /* The T packet flag  */
  936.  
  937.     if (send_packet (1))            /* Send the packet    */
  938.         SA_Flush();
  939. }
  940.  
  941. /* Receive_File is called to receive a file from the host */
  942. static    int    receive_file (name)
  943. char    name[];
  944. {
  945.     int    fd;
  946.     unsigned  bytes;
  947.  
  948.     _fmode = O_BINARY;
  949.     fd = creat(name,(S_IREAD | S_IWRITE) );
  950.  
  951.     if (fd < 0)
  952.     {
  953.         urgentmsg ("ERROR", "** Cannot open file...aborting **");
  954.         send_failure('E');
  955.         return( FALSE );
  956.     }
  957.  
  958.     send_ack();
  959.  
  960. /* Process each incoming packet until 'TC' packet received or failure */
  961.  
  962.     while( TRUE )
  963.     {
  964.         if (read_packet (FALSE, FALSE))
  965.         {
  966.             switch (r_buffer[0]) {
  967.             case 'N' :
  968.                 bytes = r_size - 1;
  969.  
  970.                 if (write(fd, &r_buffer[1], bytes) != bytes )
  971.                 {
  972.                     urgentmsg ("ERROR", "** Write failure...aborting **");
  973.                     close (fd);
  974.                     send_failure ('E');
  975.                     return( FALSE );
  976.                 }
  977.                 send_ack();
  978.                 sprintf(strbuf, "Received Block: %d", blkct++);
  979.                 atsay(4,1,strbuf);
  980.                 break;
  981.  
  982.             case 'T' :
  983.                 if (r_buffer[1] == 'C')
  984.                 {
  985.                     close(fd);
  986.  
  987.                     send_ack();
  988.                     return( TRUE );
  989.                 }
  990.                 else
  991.                 {
  992.                     urgentmsg ("ERROR", "** Invalid termination packet...aborting **");
  993.                     close (fd);
  994.                     send_failure ('N');
  995.                     return( FALSE );
  996.                 }
  997.  
  998.             case 'F' :
  999.                 send_ack();
  1000.                 urgentmsg ("ERROR","** Failure packet received...aborting **");
  1001.                 close (fd);
  1002.                 return( FALSE );
  1003.             }
  1004.         }
  1005.          else
  1006.         {
  1007.             urgentmsg ("ERROR", "** Failed to receive packet...aborting **");
  1008.             close (fd);
  1009.             return( FALSE );
  1010.         }
  1011.     }
  1012. }
  1013.  
  1014. /*
  1015. ** bp_DLE is called from the main program when the character <DLE> is
  1016. ** received from the host.
  1017. **
  1018. ** This routine calls read_packet and dispatches to the appropriate
  1019. ** handler for the incoming packet.
  1020. */
  1021. void    bp_DLE()
  1022. {
  1023.     int    i;
  1024.     char    filename[255];
  1025.     char    str[2];
  1026. /*
  1027. ** Begin by getting the next character.  If it is <B> then enter the
  1028. ** B_Protocol state.  Otherwise simply return.
  1029. */
  1030.  
  1031.     if (wait (port, 10) != 'B')
  1032.         return;
  1033.  
  1034.     strcpy( str, " " );
  1035.  
  1036.     ack_SA  = 0;    /* Initialize Send-ahead variables */
  1037.     fill_SA = 0;
  1038.     SA_Waiting      = 0;
  1039.     blkct = 0;
  1040.     erase();                    /* clear the window */
  1041.  
  1042. /*  <DLE><B> received; begin B Protocol */
  1043.  
  1044.     r_counter   = 0;
  1045.     s_counter   = 0;
  1046.  
  1047.     if (Quick_B)
  1048.     {
  1049.         say ("*** Quick B is in effect ***\r");
  1050.  
  1051.         if (Use_CRC)
  1052.             say ("*** Using CRC ***\r");
  1053.  
  1054.         if (Our_WS != 0) /* Allow send-ahead if other end agrees */
  1055.             say ("*** Send-Ahead enabled ***\r");
  1056.     }
  1057.  
  1058.     if (read_packet (TRUE, FALSE))
  1059.     {
  1060. /* Dispatch on the type of packet just received */
  1061.  
  1062.             switch (r_buffer[0]) {
  1063.         case 'T':     /* File Transfer Application */
  1064.             switch (r_buffer[1]) {
  1065.             case 'D' :    /* downloading */
  1066.                 break;
  1067.             case 'U' :    /* uploading */
  1068.                 break;
  1069.             default :
  1070.                 send_failure('N');
  1071.                 return;
  1072.             }
  1073.  
  1074.             switch (r_buffer[2]) {
  1075.             case 'A':    /* ascii file */
  1076.                 break;
  1077.             case 'B':    /* binary file */
  1078.                 break;
  1079.             default :
  1080.                 send_failure('N');        /* not implemented */
  1081.                 return;
  1082.             }
  1083.  
  1084.             i = 2;
  1085.             strcpy( filename, "" );
  1086.  
  1087.             while( (r_buffer[i] != 0) && (i < r_size) )
  1088.             {
  1089.                 i = i + 1;
  1090.                 str[0] = r_buffer[i];
  1091.                 strcat( filename, str );
  1092.             }
  1093.  
  1094.             if (r_buffer[1] == 'U')
  1095.             {
  1096.                 if( send_file(filename) )
  1097.                     urgentmsg("SUCCESS", "Transfer completed!" );
  1098.             }
  1099.             else
  1100.             {
  1101.                 if( receive_file(filename) )
  1102.                     urgentmsg("SUCCESS", "Transfer completed!" );
  1103.             }
  1104.             break;
  1105.  
  1106.         case '+':          /* Received Transport Parameters Packet */
  1107.             do_transport_parameters();
  1108.             break;
  1109.  
  1110.         case '?':          /* Received Application Parameters Packet */
  1111.             do_application_parameters();
  1112.             break;
  1113.  
  1114.         default:    /* Unknown packet; tell host we don't know */
  1115.             send_failure ('N');
  1116.                     break;
  1117.  
  1118.         }  /* of case */
  1119.     }     /* of if read_packet the */
  1120. }
  1121.