home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ek / eksw / src / kermit.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  102KB  |  3,426 lines

  1. // todo:
  2. // 1. should we send an I packet before the R-00 packet?
  3. // 2. commands to exit the server and/or logout from the session
  4.  
  5. #define KERMIT_C
  6. /*
  7.  * EKSW: Embedded Kermit with true sliding windows 
  8.  * protocol module Version: 0.94
  9.  * Most Recent Update: March 30, 2010
  10.  * John Dunlap
  11.  * 
  12.  * Author: Frank da Cruz. Copyright (C) 1995, 2004, Trustees of Columbia
  13.  * University in the City of New York. All rights reserved.
  14.  * 
  15.  * No stdio or other runtime library calls, no system calls, no system
  16.  * includes, no static data, and no global variables in this module.
  17.  * 
  18.  * Warning: you cannot use debug() in any routine whose argument list does
  19.  * not include "struct k_data *k".  Thus most routines in this module
  20.  * include this arg, even if they don't use it. 
  21.  *
  22.  * Redistribution and use in source and binary forms, with or without
  23.  * modification, are permitted provided that the following conditions are met:
  24.  *
  25.  * - Redistributions of source code must retain the above copyright notice,
  26.  *   this list of conditions and the following disclaimer.
  27.  *
  28.  * - Redistributions in binary form must reproduce the above copyright notice,
  29.  *   this list of conditions and the following disclaimer in the documentation
  30.  *   and/or other materials provided with the distribution.
  31.  *
  32.  * - Neither the name of Columbia University nor the names of its contributors
  33.  *   may be used to endorse or promote products derived from this software
  34.  *   without specific prior written permission.
  35.  *  
  36.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  37.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  40.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  41.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  42.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  43.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  44.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  46.  * POSSIBILITY OF SUCH DAMAGE.
  47.  */
  48.  
  49. # if DEBUG
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <ctype.h>
  54. # ifndef __linux
  55. # include "../include/snprintf.h"
  56. # endif
  57. # endif
  58.  
  59. #include "cdefs.h"              /* C language defs for all modules */
  60. #include "debug.h"              /* Debugging */
  61. #include "kermit.h"             /* Kermit protocol definitions */
  62.  
  63. #define USE_ZGETC_MACRO
  64. #ifdef USE_ZGETC_MACRO
  65. #define zgetc() \
  66. ((--(k->zincnt))>=0)?((int)(*(k->zinptr)++)&0xff):(*(k->readf))(k)
  67. #else // USE_ZGETC_MACRO
  68. STATIC int
  69. zgetc (struct k_data *k)
  70. {
  71.    UCHAR *ptr;
  72.    int kar;
  73.  
  74.    k->zincnt--;
  75.    if (k->zincnt >= 0)
  76.    {
  77.       ptr = k->zinptr;
  78.       k->zinptr++;
  79.       kar = (*ptr) & 0xFF;
  80.    }
  81.    else
  82.    {
  83.       kar = (*(k->readf)) (k);
  84.    }
  85.    return (kar);
  86. }
  87. #endif // USE_ZGETC_MACRO
  88.  
  89. /*
  90.  * See cdefs.h for meaning of STATIC, ULONG, and UCHAR 
  91.  */
  92.  
  93. STATIC ULONG stringnum (UCHAR *, struct k_data *);
  94. STATIC UCHAR *numstring (ULONG, UCHAR *, int, struct k_data *);
  95. STATIC int spkt (char, short, int, UCHAR *, struct k_data *);
  96. STATIC int ack (struct k_data *, short, UCHAR * text);
  97. STATIC int nak (struct k_data *, short, short);
  98. STATIC int chk1 (UCHAR *, struct k_data *);
  99. STATIC USHORT chk2 (UCHAR *, struct k_data *);
  100. STATIC USHORT chk3 (UCHAR *, struct k_data *);
  101.  
  102. STATIC void spar (struct k_data *, UCHAR *, int);
  103. STATIC int rpar (struct k_data *, char);
  104. STATIC int decode (struct k_data *, struct k_response *, short, UCHAR *,
  105.                    int rslot);
  106.  
  107. STATIC int gattr (struct k_data *, UCHAR *, struct k_response *);
  108. STATIC int sattr (struct k_data *, struct k_response *);
  109.  
  110. STATIC int sdata (struct k_data *, struct k_response *);
  111.  
  112. STATIC void epkt (char *, struct k_data *);
  113. STATIC int getpkt (struct k_data *, struct k_response *);
  114. STATIC int encstr (UCHAR *, struct k_data *, struct k_response *);
  115. STATIC void encode (int, int, struct k_data *);
  116. STATIC short nxtpkt (struct k_data *);
  117. STATIC int resend (struct k_data *, short seq);
  118. STATIC int nused_sslots (struct k_data *);
  119. STATIC int nused_rslots (struct k_data *);
  120.  
  121. #if 0
  122. STATIC void decstr (UCHAR *, struct k_data *, struct k_response *);
  123. STATIC short prevpkt (struct k_data *);
  124. #endif
  125.  
  126. STATIC short earliest_sseq (struct k_data *k);
  127.  
  128. #ifdef DEBUG
  129. int xerror (void);
  130. STATIC void show_sslots (struct k_data *);
  131. STATIC void show_rslots (struct k_data *);
  132. #endif /* DEBUG */
  133.  
  134. STATIC int test_rslots (struct k_data *);
  135.  
  136. STATIC short
  137. earliest_rseq (struct k_data *k)
  138. {
  139.    short slot;
  140.    short seq;
  141.    short seq_ref = -1;
  142.    short seq_rot;
  143.    short seq_rot_oldest = -1;
  144.    short seq_oldest = -1;
  145.  
  146.    for (slot = 0; slot < k->wslots; slot++)
  147.    {
  148.       seq = k->ipktinfo[slot].seq;
  149.       if (seq >= 0 && seq < 64)
  150.       {
  151.          seq_ref = seq;
  152.          seq_oldest = seq;
  153.          seq_rot_oldest = 0;
  154.       }
  155.    }
  156.  
  157.    if (seq_ref != -1)
  158.    {
  159.       for (slot = 0; slot < k->wslots; slot++)
  160.       {
  161.          seq = k->ipktinfo[slot].seq;
  162.          if (seq >= 0 && seq < 64)
  163.          {
  164.             seq_rot = seq - seq_ref;
  165.             if (seq_rot < -31)
  166.                seq_rot += 64;
  167.             if (seq_rot > 31)
  168.                seq_rot -= 64;
  169.  
  170.             if (seq_rot < seq_rot_oldest)
  171.             {
  172.                seq_rot_oldest = seq_rot;
  173.                seq_oldest = seq;
  174.             }
  175.          }
  176.       }
  177.    }
  178.  
  179.    debug (DB_LOG, "EARLIEST_RSEQ", 0, seq_oldest);
  180.    return (seq_oldest);
  181. }
  182.  
  183. STATIC short
  184. latest_rseq (struct k_data *k)
  185. {
  186.    short slot;
  187.    short seq;
  188.    short seq_ref = -1;
  189.    short seq_rot;
  190.    short seq_rot_newest = -1;
  191.    short seq_newest = -1;
  192.  
  193.    for (slot = 0; slot < k->wslots; slot++)
  194.    {
  195.       seq = k->ipktinfo[slot].seq;
  196.       if (seq >= 0 && seq < 64)
  197.       {
  198.          seq_ref = seq;
  199.          seq_newest = seq;
  200.          seq_rot_newest = 0;
  201.       }
  202.    }
  203.  
  204.    if (seq_ref != -1)
  205.    {
  206.       for (slot = 0; slot < k->wslots; slot++)
  207.       {
  208.          seq = k->ipktinfo[slot].seq;
  209.          if (seq >= 0 && seq < 64)
  210.          {
  211.             seq_rot = seq - seq_ref;
  212.             if (seq_rot < -31)
  213.                seq_rot += 64;
  214.             if (seq_rot > 31)
  215.                seq_rot -= 64;
  216.  
  217.             if (seq_rot > seq_rot_newest)
  218.             {
  219.                seq_rot_newest = seq_rot;
  220.                seq_newest = seq;
  221.             }
  222.          }
  223.       }
  224.    }
  225.  
  226.    debug (DB_LOG, "LATEST_RSEQ", 0, seq_newest);
  227.    return (seq_newest);
  228. }
  229.  
  230. // nak_oldest_unacked() never finds anything because
  231. // we ACK as soon as we put each packet into its slot.
  232. // Also, rseq for short packets is unvetted and for long packets
  233. // the only test is a simple checksum so it's safer
  234. // to time out.  For Iridium, there are no bad bytes so
  235. // the point is moot.
  236. #undef USE_NAK_OLDEST_UNACKED
  237. #ifdef USE_NAK_OLDEST_UNACKED
  238. STATIC int
  239. nak_oldest_unacked (struct k_data *k, short rseq)
  240. {
  241.    short slot;
  242.    short seq;
  243.    short age;
  244.    short oldest_seq = -1;
  245.    short oldest_age = -99;
  246.    short oldest_rslot = -1;
  247.    int flg;
  248.  
  249.    for (slot = 0; slot < k->wslots; slot++)
  250.    {
  251.       seq = k->ipktinfo[slot].seq;
  252.       flg = k->ipktinfo[slot].flg;
  253.       if (seq >= 0 && seq < 64 && flg == 0)
  254.       {
  255.          age = k->s_seq - seq;
  256.          if (age < 0)
  257.             age += 64;
  258.          if (age > 63)
  259.             age -= 64;
  260.  
  261.          if (age > oldest_age)
  262.          {
  263.             oldest_rslot = slot;
  264.             oldest_age = age;
  265.             oldest_seq = seq;
  266.          }
  267.       }
  268.    }
  269.  
  270.    debug (DB_LOG, "NAK_OLDEST_UNACKED oldest_seq", 0, oldest_seq);
  271.    debug (DB_LOG, "NAK_OLDEST_UNACKED rseq", 0, rseq);
  272.  
  273.    if (oldest_seq != -1)
  274.    {
  275.       nak (k, oldest_seq, oldest_rslot);
  276. //    k->anseq = oldest_seq;
  277.       return (X_OK);
  278.    }
  279.    else
  280.    {
  281. //    nak (k, (k->anseq+1)&63, -1);
  282. //    nak (k, k->anseq, -1);
  283. //    nak (k, rseq, -1);
  284.       return (X_OK);
  285.    }
  286. }
  287. #endif /* USE_NAK_OLDEST_UNACKED */
  288.  
  289. STATIC int
  290. handle_good_rpkt (struct k_data *k, struct k_response *r,
  291.                   short rseq, UCHAR * pbuf)
  292. {
  293.    // true sliding windows for receive see p. 292
  294.    short wsize = k->wslots;     // negotiated window size
  295.    short high = -1;             // latest table entry (chronlogically)
  296.    short low = -1;              // earliest possible table entry
  297.    short eseq = -1;             // expected sequence number
  298. // short psn = rseq;            // just-arrived packet sequence number
  299.  
  300.    short rslot;
  301.    short nused;
  302.    int isnxt = 0;
  303.    int isgap = 0;
  304.    int isold = 0;
  305.    int rc;
  306.    int i;
  307.    short iseq;
  308.    short dseq;
  309.    short nnak;
  310.    int do_add_pkt;
  311.  
  312.    nused = nused_rslots (k);
  313.  
  314.    if (nused == 0)
  315.    {
  316.       eseq = rseq;              // for first time
  317.    }
  318.    else
  319.    {
  320.       high = latest_rseq (k);
  321.       low = high - wsize + 1;
  322.       if (low < 0)
  323.          low += 64;
  324.  
  325.       eseq = (high + 1) & 63;
  326.  
  327.       if (rseq != eseq)
  328.       {
  329.          for (i = 2; i <= wsize; i++)
  330.          {
  331.             iseq = (high + i) & 63;
  332.             if (rseq == iseq)
  333.             {
  334.                isgap = 1;
  335.                break;
  336.             }
  337.          }
  338.       }
  339.  
  340.       if (rseq != eseq && !isgap)
  341.       {
  342.          short hp = (high + 1) & 63;
  343.          for (i = low; i != hp; i++)
  344.          {
  345.             iseq = i & 63;
  346.             if (rseq == iseq)
  347.             {
  348.                isold = 1;
  349.                break;
  350.             }
  351.          }
  352.       }
  353.    }
  354.  
  355.    if (rseq == eseq)
  356.       isnxt = 1;
  357.    else
  358.       isnxt = 0;
  359.  
  360.    debug (DB_LOG, "HANDLE_RPKT rseq", 0, rseq);
  361.    debug (DB_LOG, "  high", 0, high);
  362.    debug (DB_LOG, "  low", 0, low);
  363.    debug (DB_LOG, "  eseq", 0, eseq);
  364.    debug (DB_LOG, "  nused", 0, nused);
  365.    debug (DB_LOG, "  isnxt", 0, isnxt);
  366.    debug (DB_LOG, "  isgap", 0, isgap);
  367.    debug (DB_LOG, "  isold", 0, isold);
  368.  
  369. #ifdef DEBUG
  370.    show_rslots (k);
  371. #endif
  372.  
  373.    // write old packets to file in order to keep
  374.    // our receive window aligned with sender's window
  375.    if (isnxt || isgap)
  376.    {
  377.       debug (DB_LOG, "HANDLE_RPKT align our recv window with sender rseq",
  378.              0, rseq);
  379.       for (i = 0; i < wsize; i++)
  380.       {
  381.          iseq = earliest_rseq (k);
  382.          debug (DB_LOG, "HANDLE_RPKT iseq", 0, iseq);
  383.          if (iseq < 0 || iseq >= 64)
  384.             break;
  385.  
  386.          dseq = rseq - iseq;
  387.          if (dseq < 0)
  388.             dseq += 64;
  389.          debug (DB_LOG, "HANDLE_RPKT dseq", 0, dseq);
  390.          if (dseq < wsize)
  391.             continue;
  392.  
  393.          rslot = k->r_pw[iseq];
  394.          debug (DB_LOG, "HANDLE_RPKT rslot", 0, rslot);
  395.          if (rslot < 0 || rslot >= wsize)
  396.          {
  397.             debug (DB_LOG, "HANDLE_RPKT error 1, rslot", 0, rslot);
  398.             debug (DB_LOG, "HANDLE_RPKT iseq", 0, iseq);
  399.             return (X_ERROR);
  400.          }
  401.  
  402.          if (k->ipktinfo[rslot].flg == 0)
  403.          {
  404.             debug (DB_MSG, "HANDLE_RPKT error 2, flg", 0,
  405.                    k->ipktinfo[rslot].flg);
  406.             debug (DB_LOG, "  rslot", 0, rslot);
  407.             return (X_ERROR);
  408.          }
  409.  
  410.          /* Decode pkt and write file */
  411.          rc = decode (k, r, 1, k->ipktinfo[rslot].buf, rslot);
  412.          debug (DB_LOG, "HANDLE_RPKT decode rc", 0, rc);
  413.          if (rc != X_OK)
  414.          {
  415.             debug (DB_LOG, "decode failed rc", 0, rc);
  416. #ifdef DEBUG
  417.             show_rslots (k);
  418. #endif
  419. //          exit (1);
  420.             epkt ("EKSW decode failed", k);
  421.             return (rc);
  422.          }
  423.          free_rslot (k, rslot);
  424. #ifdef DEBUG
  425.          show_rslots (k);
  426. #endif
  427.       }
  428.    }
  429.  
  430.    do_add_pkt = 0;
  431.  
  432.    if (isnxt)
  433.    {
  434.       // 1. PSN = SEQ = HIGH + 1, the usual case
  435.       debug (DB_LOG, "HANDLE_RPKT usual rseq", 0, rseq);
  436.  
  437.       // acknowledge incoming packet
  438.       ack (k, rseq, (UCHAR *) 0);
  439.  
  440.       do_add_pkt = 1;
  441.    }
  442.    else if (isgap)
  443.    {
  444.       // 2. PSN != SEQ, a new packet, but not the next sequential one
  445.       debug (DB_LOG, "HANDLE_RPKT isgap rseq", 0, rseq);
  446.  
  447.       nnak = rseq - high - 1;
  448.       if (nnak < 0)
  449.          nnak += 64;
  450.  
  451.       debug (DB_LOG, "  nnak", 0, nnak);
  452.  
  453.       // acknowledge incoming packet
  454.       ack (k, rseq, (UCHAR *) 0);
  455.  
  456.       // send NAKs for whole gap: HIGH+1 to PSN-1
  457.       for (i = 0; i < nnak; i++)
  458.       {
  459.          iseq = (high + 1 + i) & 63;
  460.          rslot = k->r_pw[iseq];
  461.          debug (DB_LOG, "HANDLE_RPKT isgap nak iseq", 0, iseq);
  462.          debug (DB_LOG, "  rslot", 0, rslot);
  463.          nak (k, iseq, rslot);
  464.       }
  465.  
  466.       do_add_pkt = 2;
  467.    }
  468.    else if (isold)
  469.    {
  470.       // 3. LOW <= PSN <= HIGH, old, possibly missing pkt arrived
  471.       debug (DB_LOG, "HANDLE_RPKT isold rseq", 0, rseq);
  472.  
  473.       ack (k, rseq, (UCHAR *) 0);
  474.  
  475.       // If packet was missing add it to window.
  476.       // There is supposed to be a slot available.
  477.       // Don't store a repeated packet again
  478.       rslot = k->r_pw[rseq];
  479.       if (rslot < 0 || rslot >= wsize)
  480.       {
  481.          do_add_pkt = 3;
  482.       }
  483.    }
  484.    else
  485.    {
  486.       // 4. PSN < LOW || PSN > HIGH -- unexpected, undesired, ignore
  487.       debug (DB_LOG, "HANDLE_RPKT unexpected so ignored rseq", 0, rseq);
  488.    }
  489.  
  490.    if (do_add_pkt > 0)
  491.    {
  492.       test_rslots (k);
  493.  
  494.       get_rslot (k, &rslot);
  495.       if (rslot < 0 || rslot >= wsize)
  496.       {
  497.          debug (DB_LOG, "HANDLE_RPKT error 3, rslot", 0, rslot);
  498.          debug (DB_LOG, "  do_add_pkt", 0, do_add_pkt);
  499.          return (X_ERROR);
  500.       }
  501.  
  502.       test_rslots (k);
  503.  
  504.       // put pbuf into slot in receive table
  505.       for (i = 0; i < P_BUFLEN; i++)
  506.       {
  507.          k->ipktinfo[rslot].buf[i] = pbuf[i];
  508.          if (pbuf[i] == 0)
  509.             break;
  510.       }
  511.       k->ipktinfo[rslot].len = i;
  512.       k->ipktinfo[rslot].flg = 1;
  513.       k->ipktinfo[rslot].seq = rseq;
  514.       k->ipktinfo[rslot].crc = chk3 (pbuf, k);
  515.       k->r_pw[rseq] = rslot;
  516.  
  517.       test_rslots (k);
  518.    }
  519.  
  520. #ifdef DEBUG
  521.    show_rslots (k);
  522. #endif
  523.  
  524.    return (X_OK);
  525. }
  526.  
  527. void
  528. handle_bad_rpkt ()
  529. {
  530. }
  531.  
  532.  
  533. int
  534. ok2rxd (struct k_data *k)
  535. {
  536.    int ok;
  537.    int sw_full;
  538.  
  539.    if (nused_sslots (k) == k->wslots)
  540.       sw_full = 1;
  541.    else
  542.       sw_full = 0;
  543.  
  544.    if ((k->what == W_SEND || k->what == W_GET) &&
  545.        k->state == S_DATA && sw_full == 0)
  546.       ok = 0;
  547.    else
  548.       ok = 1;
  549.  
  550.    debug (DB_LOG, "ok2rxd", 0, ok);
  551.  
  552.    k->do_rxd = ok;
  553.  
  554.    return (ok);
  555. }
  556.  
  557. STATIC void
  558. free_sslot_easca (struct k_data *k)
  559. // Remove earliest & subsequent contiguous ACK'd packets.
  560. // Packet numbers thus remain in order in the sliding window.
  561. // This way we don't send packets which won't fit in their window.
  562. {
  563.    int nfreed = 0;
  564.    int i;
  565.  
  566.    for (i = 0; i < k->wslots; i++)
  567.    {
  568.       short seq_earl, slot_earl;
  569.       seq_earl = earliest_sseq (k);
  570.       if (seq_earl < 0 || seq_earl > 63)
  571.          break;
  572.       slot_earl = k->s_pw[seq_earl];
  573.       if (slot_earl < 0 || slot_earl >= k->wslots)
  574.          break;
  575.       if (k->opktinfo[slot_earl].flg == 0)
  576.          break;
  577.       free_sslot (k, slot_earl);
  578.       nfreed++;
  579.    }
  580.    debug (DB_LOG, "FREE_SSLOT_EASCA nfreed", 0, nfreed);
  581. }
  582.  
  583. #ifdef DEBUG
  584. STATIC void
  585. chk_sseq_nos (struct k_data *k)
  586. // check that sequence numbers are still in order
  587. {
  588.    int i;
  589.    int dif, dif0;
  590.    int ok;
  591.    int nerr;
  592.  
  593.    nerr = 0;
  594.    ok = 0;
  595.    for (i = 0; i < k->wslots; i++)
  596.    {
  597.       short ss;
  598.       if (k->opktinfo[i].seq < 0)
  599.          continue;
  600.       ss = k->opktinfo[i].seq - k->r_seq;
  601.       if (ss < -32)
  602.          ss += 64;
  603.       if (ss > 31)
  604.          ss -= 64;
  605.       dif = ss - i;
  606.       if (ok == 0)
  607.       {
  608.          ok = 1;
  609.          dif0 = dif;
  610.       }
  611.       else if (dif != dif0 && (dif + k->wslots) != dif0)
  612.          nerr++;
  613.    }
  614.  
  615.    if (nerr)
  616.    {
  617.       debug (DB_LOG, "CHK_SEQ nerr", 0, nerr);
  618. #ifdef DEBUG
  619.       show_sslots (k);
  620. #endif
  621.    }
  622. }
  623. #endif
  624.  
  625. STATIC int
  626. flush_to_file (struct k_data *k, struct k_response *r)
  627. {
  628.    short wsize = k->wslots;     // negotiated window size
  629.    short rslot;
  630.    short iseq;
  631.    int rc;
  632.  
  633.    debug (DB_MSG, "FLUSH_TO_FILE begin", 0, 0);
  634.  
  635.    // flush receive window to file
  636.    while ((iseq = earliest_rseq (k)) >= 0)
  637.    {
  638.  
  639.       debug (DB_LOG, "FLUSH_TO_FILE iseq", 0, iseq);
  640.  
  641.       rslot = k->r_pw[iseq];
  642.  
  643.       debug (DB_LOG, "  rslot", 0, rslot);
  644.  
  645.       if (rslot < 0 || rslot >= wsize)
  646.       {
  647.          debug (DB_LOG, "FLUSH_TO_FILE error rslot", 0, rslot);
  648.          return (X_ERROR);
  649.       }
  650.  
  651.       if (k->ipktinfo[rslot].flg == 0)
  652.       {
  653.          debug (DB_MSG, "FLUSH_TO_FILE error flg", 0, k->ipktinfo[rslot].flg);
  654.          return (X_ERROR);
  655.       }
  656.  
  657.       /* Decode pkt and write file */
  658.       rc = decode (k, r, 1, k->ipktinfo[rslot].buf, rslot);
  659.       debug (DB_LOG, "FLUSH_TO_FILE decode rc", 0, rc);
  660.       if (rc != X_OK)
  661.       {
  662.          debug (DB_LOG, "decode failed rc", 0, rc);
  663. #ifdef DEBUG
  664.          show_rslots (k);
  665. #endif
  666. //       exit (1);
  667.          epkt ("EKSW flush to file failed", k);
  668.          return (rc);
  669.       }
  670.       free_rslot (k, rslot);
  671. #ifdef DEBUG
  672.       show_rslots (k);
  673. #endif
  674.    }
  675.  
  676.    debug (DB_LOG, "FLUSH_TO_FILE obufpos", 0, k->obufpos);
  677.  
  678.    if (k->obufpos > 0)
  679.    {                            /* Flush output buffer to file */
  680.       rc = (*(k->writef)) (k, k->obuf, k->obufpos);
  681.       debug (DB_LOG, "FLUSH_TO_FILE writef rc", 0, rc);
  682.       r->sofar += k->obufpos;
  683.       r->sofar_rumor += k->obufpos;
  684.       k->obufpos = 0;
  685.    }
  686.  
  687.    return (rc);
  688. }
  689.  
  690. /*
  691.  * Utility routines 
  692.  */
  693.  
  694. UCHAR *
  695. get_rslot (struct k_data * k, short *n)
  696. {                               /* Find a free packet buffer */
  697.    register int slot;
  698.    /*
  699.     * Note: We don't clear the retry count here. It is cleared only after 
  700.     * the NEXT packet arrives, which indicates that the other Kermit got
  701.     * our ACK for THIS packet. 
  702.     */
  703.    for (slot = 0; slot < k->wslots; slot++)
  704.    {                            /* Search */
  705.       if (k->ipktinfo[slot].len < 1)
  706.       {
  707.          *n = slot;             /* Slot number */
  708.          k->ipktinfo[slot].len = -1;    /* Mark it as allocated but not used */
  709.          k->ipktinfo[slot].seq = -1;
  710.          k->ipktinfo[slot].typ = SP;
  711.          k->ipktinfo[slot].crc = 0xFFFF;
  712.          /*
  713.           * k->ipktinfo[slot].rtr = 0; 
  714.           *//*
  715.           * (see comment above) 
  716.           */
  717.          k->ipktinfo[slot].dat = (UCHAR *) 0;
  718.          debug (DB_LOG, "GET_RSLOT slot", 0, slot);
  719.          return (k->ipktinfo[slot].buf);
  720.       }
  721.    }
  722.    *n = -1;
  723.    debug (DB_LOG, "GET_RSLOT slot", 0, -1);
  724.    return ((UCHAR *) 0);
  725. }
  726.  
  727. void                            /* Initialize a window slot */
  728. free_rslot (struct k_data *k, short slot)
  729. {
  730.    short seq;
  731.  
  732.    debug (DB_LOG, "FREE_RSLOT slot", 0, slot);
  733.  
  734.    if (slot < 0 || slot >= P_WSLOTS)
  735.    {
  736.       debug (DB_LOG, "  confused: slot out of bounds", 0, slot);
  737.       epkt ("EKSW free_rslot confused", k);
  738. //    exit (1);
  739.       return;
  740.    }
  741.  
  742.    seq = k->ipktinfo[slot].seq;
  743.    debug (DB_LOG, "  seq", 0, seq);
  744.  
  745.    if (seq >= 0 && seq < 64)
  746.       k->r_pw[seq] = -1;
  747.  
  748.    k->ipktinfo[slot].len = 0;   /* Packet length */
  749.    k->ipktinfo[slot].seq = -1;  /* Sequence number */
  750.    k->ipktinfo[slot].typ = (char) 0;    /* Type */
  751.    k->ipktinfo[slot].rtr = 0;   /* Retry count */
  752.    k->ipktinfo[slot].flg = 0;   /* Flags */
  753.    k->ipktinfo[slot].crc = 0xFFFF;
  754. }
  755.  
  756. UCHAR *
  757. get_sslot (struct k_data *k, short *n)
  758. {                               /* Find a free packet buffer */
  759.    register int slot;
  760.    for (slot = 0; slot < k->wslots; slot++)
  761.    {                            /* Search */
  762.       if (k->opktinfo[slot].len < 1)
  763.       {
  764.          *n = slot;             /* Slot number */
  765.          k->opktinfo[slot].len = -1;    /* Mark it as allocated but not used */
  766.          k->opktinfo[slot].seq = -1;
  767.          k->opktinfo[slot].typ = SP;
  768.          k->opktinfo[slot].rtr = 0;
  769.          k->opktinfo[slot].flg = 0;     // ACK'd bit
  770.          k->opktinfo[slot].dat = (UCHAR *) 0;
  771.          debug (DB_LOG, "GET_SSLOT slot", 0, slot);
  772.          return (k->opktinfo[slot].buf);
  773.       }
  774.    }
  775.    *n = -1;
  776.    debug (DB_LOG, "GET_SSLOT slot", 0, -1);
  777.    return ((UCHAR *) 0);
  778. }
  779.  
  780. STATIC int
  781. nused_sslots (struct k_data *k)
  782. {
  783.    int i;
  784.    int n = 0;
  785.  
  786.    for (i = 0; i < k->wslots; i++)
  787.       if (k->opktinfo[i].len > 0)
  788.          n++;
  789.    return (n);
  790. }
  791.  
  792. STATIC int
  793. nused_rslots (struct k_data *k)
  794. {
  795.    int i;
  796.    int n = 0;
  797.  
  798.    for (i = 0; i < k->wslots; i++)
  799.       if (k->ipktinfo[i].len > 0)
  800.          n++;
  801.    return (n);
  802. }
  803.  
  804. #ifdef DEBUG
  805. STATIC void
  806. show_sslots (struct k_data *k)
  807. {
  808.    int slot, j, n;
  809.    char tmp[80], *p;
  810.  
  811.    debug (DB_MSG, "SHOW_SSLOTS", 0, 0);
  812.    for (slot = 0; slot < k->wslots; slot++)
  813.    {
  814.       n = snprintf (tmp, sizeof (tmp) - 1, "  slot=%02d seq=%02d flg=%d "
  815.                     "len=%4d crc=%5d pwi=",
  816.                     slot, k->opktinfo[slot].seq, k->opktinfo[slot].flg,
  817.                     k->opktinfo[slot].len, k->opktinfo[slot].crc);
  818.  
  819.       p = tmp + n;
  820.       for (j = 0; j < 64; j++)
  821.          if (k->s_pw[j] == slot)
  822.          {
  823.             n = snprintf (p, sizeof (tmp) - 1 - n, " %02d", j);
  824.             p += n;
  825.          }
  826.       debug (DB_MSG, tmp, 0, 0);
  827.    }
  828. }
  829. #endif /* DEBUG */
  830. #ifdef DEBUG
  831. STATIC void
  832. show_rslots (struct k_data *k)
  833. {
  834.    int slot, j, n, m;
  835.    char tmp[256], *p;
  836.  
  837.  
  838.    debug (DB_MSG, "SHOW_RSLOTS", 0, 0);
  839.    for (slot = 0; slot < k->wslots; slot++)
  840.    {
  841.       int ok;
  842.       if (k->ipktinfo[slot].seq >= 0 &&
  843.           chk3 (k->ipktinfo[slot].buf, k) != k->ipktinfo[slot].crc)
  844.          ok = 0;
  845.       else
  846.          ok = 1;
  847.  
  848.       p = tmp;
  849.       m = sizeof (tmp) - 1;
  850.       n = snprintf (p, m, "  slot=%02d seq=%02d flg=%d "
  851.                     "len=%4d crc=%5d crcok=%d pwi=",
  852.                     slot, k->ipktinfo[slot].seq, k->ipktinfo[slot].flg,
  853.                     k->ipktinfo[slot].len, k->ipktinfo[slot].crc, ok);
  854.       if (n < 0)
  855.       {
  856.          debug (DB_LOG, "SHOW_RSLOTS 1 n", 0, n);
  857.          debug (DB_LOG, "SHOW_RSLOTS 1 m", 0, m);
  858.          debug (DB_LOG, "SHOW_RSLOTS 1 slot", 0, slot);
  859.          debug (DB_LOG, "SHOW_RSLOTS 1 seq", 0, k->ipktinfo[slot].seq);
  860.          debug (DB_LOG, "SHOW_RSLOTS 1 flg", 0, k->ipktinfo[slot].flg);
  861.          debug (DB_LOG, "SHOW_RSLOTS 1 len", 0, k->ipktinfo[slot].len);
  862.          debug (DB_LOG, "SHOW_RSLOTS 1 crc", 0, k->ipktinfo[slot].crc);
  863.          debug (DB_LOG, "SHOW_RSLOTS ok", 0, ok);
  864.          debug (DB_LOG, "SHOW_RSLOTS 1 p-tmp", 0, p - tmp);
  865.          debug (DB_LOG, "SHOW_RSLOTS 1 tmp", tmp, 0);
  866. //       exit (1);
  867.       }
  868.       p += n;
  869.       m -= n;
  870.       for (j = 0; j < 64; j++)
  871.       {
  872.          if (k->r_pw[j] == slot)
  873.          {
  874.             n = snprintf (p, m, " %02d", j);
  875.             if (n < 0)
  876.             {
  877.                debug (DB_LOG, "SHOW_RSLOTS 2 j", 0, j);
  878.                debug (DB_LOG, "SHOW_RSLOTS 2 n", 0, n);
  879.                debug (DB_LOG, "SHOW_RSLOTS 2 m", 0, m);
  880.                debug (DB_LOG, "SHOW_RSLOTS 2 slot", 0, slot);
  881.                debug (DB_LOG, "SHOW_RSLOTS 2 seq", 0, k->ipktinfo[slot].seq);
  882.                debug (DB_LOG, "SHOW_RSLOTS 2 flg", 0, k->ipktinfo[slot].flg);
  883.                debug (DB_LOG, "SHOW_RSLOTS 2 len", 0, k->ipktinfo[slot].len);
  884.                debug (DB_LOG, "SHOW_RSLOTS 2 crc", 0, k->ipktinfo[slot].crc);
  885.                debug (DB_LOG, "SHOW_RSLOTS 2 p-tmp", 0, p - tmp);
  886.                debug (DB_LOG, "SHOW_RSLOTS 2 tmp", tmp, 0);
  887. //             exit (1);
  888.             }
  889.             p += n;
  890.             m -= n;
  891.          }
  892.       }
  893.       debug (DB_MSG, tmp, 0, 0);
  894.    }
  895. }
  896. #endif
  897.  
  898. STATIC int
  899. test_rslots (struct k_data *k)
  900. {
  901.    int slot, nbad = 0;
  902.  
  903.    for (slot = 0; slot < k->wslots; slot++)
  904.    {
  905.       if (k->ipktinfo[slot].seq >= 0 &&
  906.           chk3 (k->ipktinfo[slot].buf, k) != k->ipktinfo[slot].crc)
  907.       {
  908.          nbad++;
  909.          debug (DB_HEX, "THEX", k->ipktinfo[slot].buf, k->ipktinfo[slot].len);
  910.       }
  911.    }
  912.    if (nbad > 0)
  913.    {
  914.       debug (DB_LOG, "TEST_RSLOTS nbad", 0, nbad);
  915. #ifdef DEBUG
  916.       show_rslots (k);
  917. # endif
  918.       epkt ("EKSW test rslots failed", k);
  919. //    exit (1);
  920.       return X_ERROR;
  921.    }
  922.    return X_OK;
  923. }
  924.  
  925. void                            /* Initialize a window slot */
  926. free_sslot (struct k_data *k, short slot)
  927. {
  928.    short seq;
  929.  
  930.    debug (DB_LOG, "FREE_SSLOT slot", 0, slot);
  931.    if (slot < 0 || slot >= P_WSLOTS)
  932.       return;
  933.  
  934.    seq = k->opktinfo[slot].seq;
  935.    debug (DB_LOG, "  seq", 0, seq);
  936.  
  937.    if (seq >= 0 && seq < 64)
  938.       k->s_pw[seq] = -1;
  939.  
  940.    k->opktinfo[slot].len = 0;   /* Packet length */
  941.    k->opktinfo[slot].seq = -1;  /* Sequence number */
  942.    k->opktinfo[slot].typ = (char) 0;    /* Type */
  943.    k->opktinfo[slot].rtr = 0;   /* Retry count */
  944.    k->opktinfo[slot].flg = 0;   /* ACK'd bit */
  945. }
  946.  
  947. STATIC short
  948. earliest_sseq (struct k_data *k)
  949. {
  950.    short slot;
  951.    short seq;
  952.    short age;
  953.    short oldest_seq = -1;
  954.    short oldest_age = -99;
  955.  
  956.    for (slot = 0; slot < k->wslots; slot++)
  957.    {
  958.       seq = k->opktinfo[slot].seq;
  959.       if (seq >= 0 && seq < 64)
  960.       {
  961.          age = k->s_seq - seq;
  962.          if (age < 0)
  963.             age += 64;
  964.          if (age > 63)
  965.             age -= 64;
  966.  
  967.          if (age > oldest_age)
  968.          {
  969.             oldest_age = age;
  970.             oldest_seq = seq;
  971.          }
  972.       }
  973.    }
  974.  
  975.    debug (DB_LOG, "EARLIEST_SSEQ", 0, oldest_seq);
  976.    return (oldest_seq);
  977. }
  978.  
  979. /*
  980.  * C H K 1 -- Compute a type-1 Kermit 6-bit checksum.  
  981.  */
  982.  
  983. STATIC int
  984. chk1 (UCHAR * pkt, struct k_data *k)
  985. {
  986.    register unsigned int chk;
  987.    chk = chk2 (pkt, k);
  988.    chk = (((chk & 0300) >> 6) + chk) & 077;
  989.    return ((int) chk);
  990. }
  991.  
  992. /*
  993.  * C H K 2 -- Numeric sum of all the bytes in the packet, 12 bits.  
  994.  */
  995.  
  996. STATIC USHORT
  997. chk2 (UCHAR * pkt, struct k_data *k)
  998. {
  999.    register USHORT chk;
  1000.    for (chk = 0; *pkt != '\0'; pkt++)
  1001.       chk += *pkt;
  1002.    return (chk);
  1003. }
  1004.  
  1005. /*
  1006.  * C H K 3 -- Compute a type-3 Kermit block check.  
  1007.  */
  1008. /*
  1009.  * Calculate the 16-bit CRC-CCITT of a null-terminated string using a
  1010.  * lookup table.  Assumes the argument string contains no embedded nulls. 
  1011.  */
  1012. STATIC USHORT
  1013. chk3 (UCHAR * pkt, struct k_data * k)
  1014. {
  1015.    register USHORT c, crc;
  1016.    for (crc = 0; *pkt != '\0'; pkt++)
  1017.    {
  1018.       c = crc ^ (*pkt);
  1019.       crc = (crc >> 8) ^ ((k->crcta[(c & 0xF0) >> 4]) ^ (k->crctb[c & 0x0F]));
  1020.    }
  1021.    return (crc);
  1022. }
  1023.  
  1024. /*
  1025.  * S P K T -- Send a packet.  
  1026.  */
  1027. /*
  1028.  * Call with packet type, sequence number, data length, data, Kermit
  1029.  * struct. Returns: X_OK on success X_ERROR on i/o error 
  1030.  */
  1031. STATIC int
  1032. spkt (char typ, short seq, int len, UCHAR * data, struct k_data *k)
  1033. {
  1034.    int retc;
  1035.  
  1036.    unsigned int crc;            /* For building CRC */
  1037.    int i, j, lenpos;            /* Workers */
  1038.    UCHAR *s, *buf;
  1039.    int buflen;
  1040.    short slot;
  1041.    UCHAR tmp[100];              // for packets we don't want to resend
  1042.  
  1043.    debug (DB_CHR, "SPKT typ", 0, typ);
  1044.    debug (DB_LOG, "  seq", 0, seq);
  1045.    debug (DB_LOG, "  len", 0, len);
  1046.  
  1047.    if (seq < 0 || seq > 63)
  1048.       return (X_ERROR);
  1049.  
  1050.    if (len < 0)
  1051.    {                            /* Calculate data length ourselves? */
  1052.       len = 0;
  1053.       s = data;
  1054.       while (*s++)
  1055.          len++;
  1056.       debug (DB_LOG, "SPKT calc len", 0, len);
  1057.    }
  1058.    if (typ == 'Y' || typ == 'N' || typ == 'E')
  1059.    {
  1060.       buf = tmp;
  1061.       buflen = sizeof (tmp);
  1062.    }
  1063.    else
  1064.    {
  1065.       debug (DB_LOG, "SPKT k->s_seq", 0, k->s_seq);
  1066.       debug (DB_LOG, "SPKT k->s_pw[seq]", 0, k->s_pw[seq]);
  1067.  
  1068.       get_sslot (k, &slot);     // get a new send slot
  1069.       if (slot < 0 || slot >= k->wslots)
  1070.          return (X_ERROR);
  1071.       k->s_pw[k->s_seq] = slot;
  1072.  
  1073.       // save these for use in resend()
  1074.       k->opktinfo[slot].typ = typ;
  1075.       k->opktinfo[slot].seq = seq;
  1076.       k->opktinfo[slot].len = len;
  1077.  
  1078.       buf = k->opktinfo[slot].buf;
  1079.       buflen = P_BUFLEN;
  1080.    }
  1081.  
  1082.    i = 0;                       /* Packet buffer position */
  1083.    buf[i++] = k->s_soh;         /* SOH */
  1084.    lenpos = i++;                /* Remember this place */
  1085.    buf[i++] = tochar (seq);     /* Sequence number */
  1086.    buf[i++] = typ;              /* Packet type */
  1087.    if (k->bcta3)
  1088.       k->bct = 3;
  1089.    j = len + k->bct;
  1090.    if ((len + k->bct + 2) > 94)
  1091.    {                            /* If long packet */
  1092.       buf[lenpos] = tochar (0); /* Put blank in LEN field */
  1093.       buf[i++] = tochar (j / 95);       /* Make extended header: Big part */
  1094.       buf[i++] = tochar (j % 95);       /* and small part of length. */
  1095.       buf[i] = NUL;             /* Terminate for header checksum */
  1096.       buf[i++] = tochar (chk1 (&buf[lenpos], k));       /* Insert header checksum */
  1097.    }
  1098.    else
  1099.    {                            /* Short packet */
  1100.       buf[lenpos] = tochar (j + 2);     /* Single-byte length in LEN field */
  1101.    }
  1102.    if (data)                    /* Copy data, if any */
  1103.       for (; len--; i++)
  1104.       {
  1105.          if (i < 0 || i >= buflen)
  1106.          {
  1107.             debug (DB_LOG, "confused copy data i", 0, i);
  1108.             epkt ("EKSW spkt confused", k);
  1109. //          exit (1);
  1110.             return (X_ERROR);
  1111.          }
  1112.          buf[i] = *data++;
  1113.       }
  1114.    buf[i] = '\0';
  1115.  
  1116.    switch (k->bct)
  1117.    {                            /* Add block check */
  1118.    case 1:                     /* 1 = 6-bit chksum */
  1119.       buf[i++] = tochar (chk1 (&buf[lenpos], k));
  1120.       break;
  1121.    case 2:                     /* 2 = 12-bit chksum */
  1122.       j = chk2 (&buf[lenpos], k);
  1123. #if 0
  1124.       buf[i++] = (unsigned) tochar ((j >> 6) & 077);
  1125.       buf[i++] = (unsigned) tochar (j & 077);
  1126. #else
  1127.       // HiTech's XAC compiler silently ruins the above code.
  1128.       // An intermediate variable provides a work-around.
  1129.       // 2004-06-29 -- JHD
  1130.       {
  1131.          USHORT jj;
  1132.          jj = (j >> 6) & 077;
  1133.          buf[i++] = tochar (jj);
  1134.          jj = j & 077;
  1135.          buf[i++] = tochar (jj);
  1136.       }
  1137. #endif
  1138.  
  1139.       break;
  1140.    case 3:                     /* 3 = 16-bit CRC */
  1141.       crc = chk3 (&buf[lenpos], k);
  1142. #if 0
  1143.       buf[i++] = (unsigned) tochar (((crc & 0170000)) >> 12);
  1144.       buf[i++] = (unsigned) tochar ((crc >> 6) & 077);
  1145.       buf[i++] = (unsigned) tochar (crc & 077);
  1146. #else
  1147.       // HiTech's XAC compiler silently ruins the above code.
  1148.       // An intermediate variable provides a work-around.
  1149.       // 2004-06-29 -- JHD
  1150.       {
  1151.          USHORT jj;
  1152.          jj = (crc >> 12) & 0x0f;
  1153.          buf[i++] = tochar (jj);
  1154.          jj = (crc >> 6) & 0x3f;
  1155.          buf[i++] = tochar (jj);
  1156.          jj = crc & 0x3f;
  1157.          buf[i++] = tochar (jj);
  1158.       }
  1159. #endif
  1160.       break;
  1161.    }
  1162.  
  1163.    buf[i++] = k->s_eom;         /* Packet terminator */
  1164.    buf[i] = '\0';               /* String terminator */
  1165.    // packet ready to go
  1166.  
  1167.    if (i < 0 || i >= buflen)
  1168.    {
  1169.       debug (DB_LOG, "SPKT i", 0, i);
  1170.       debug (DB_LOG, "SPKT buflen", 0, buflen);
  1171.       return (X_ERROR);
  1172.    }
  1173.  
  1174.    k->s_seq = seq;              /* Remember sequence number */
  1175.    k->opktlen = i;              /* Remember length for retransmit */
  1176.  
  1177.    if (typ != 'Y' && typ != 'N' && typ != 'E')
  1178.    {
  1179.       k->opktinfo[slot].len = i;        /* Remember length for retransmit */
  1180.    }
  1181.  
  1182.    debug (DB_LOG, "SPKT opktlen", 0, k->opktlen);
  1183.  
  1184. #ifdef DEBUG
  1185.    /*
  1186.     * CORRUPT THE PACKET SENT BUT NOT THE ONE WE SAVE 
  1187.     */
  1188.    if (xerror ())
  1189.    {
  1190.       UCHAR p[P_BUFLEN];
  1191.       int i;
  1192.       for (i = 0; i < buflen - 8; i++)
  1193.          if (!(p[i] = buf[i]))
  1194.             break;
  1195.       if (xerror ())
  1196.       {
  1197.          p[i - 2] = 'X';
  1198.          debug (DB_PKT, "XPKT", (char *) &p[1], 0);
  1199.       }
  1200.       else if (xerror ())
  1201.       {
  1202.          p[k->opktlen - 1] = 'N';
  1203.          debug (DB_PKT, "NPKT", (char *) &p[1], 0);
  1204.       }
  1205.       else
  1206.       {
  1207.          p[0] = 'A';
  1208.          debug (DB_PKT, "APKT", (char *) &p[1], 0);
  1209.       }
  1210.       return ((*(k->txd)) (k, p, k->opktlen));  /* Send it. */
  1211.    }
  1212. #endif /* DEBUG */
  1213.  
  1214. // debug (DB_PKT, "SPKT", &buf[1], k->opktlen);
  1215.    retc = ((*(k->txd)) (k, buf, k->opktlen));   /* Send buf == whole packet */
  1216.    debug (DB_LOG, "SPKT txd retc", 0, retc);
  1217.  
  1218.    return (retc);
  1219. }
  1220.  
  1221. /*
  1222.  * N A K -- Send a NAK (negative acknowledgement) 
  1223.  */
  1224.  
  1225. STATIC int
  1226. nak (struct k_data *k, short seq, short slot)
  1227. {
  1228.    int rc;
  1229.    k->nsnak++;
  1230.    debug (DB_LOG, "NAK seq", 0, seq);
  1231.    debug (DB_LOG, "  slot", 0, slot);
  1232. // k->anseq = seq;
  1233.    rc = spkt ('N', seq, 0, (UCHAR *) 0, k);
  1234.    if (slot >= 0 && slot < 64 && k->ipktinfo[slot].rtr++ > k->retry)
  1235.    {
  1236.       debug (DB_MSG, "X_ERROR returned from nak(): too many retries", 0, 0);
  1237.       debug (DB_MSG, "  seq", 0, seq);
  1238.       debug (DB_MSG, "  slot", 0, slot);
  1239.       rc = X_ERROR;
  1240.    }
  1241.    return (rc);
  1242. }
  1243.  
  1244. /*
  1245.  * A C K -- Send an ACK (positive acknowledgement) 
  1246.  */
  1247.  
  1248. STATIC int
  1249. ack (struct k_data *k, short seq, UCHAR * text)
  1250. {
  1251.    int len, rc;
  1252.    debug (DB_LOG, "ACK seq", 0, seq);
  1253. // k->anseq = (seq+1)&63;
  1254.    len = 0;
  1255.    if (text)
  1256.    {                            /* Get length of data */
  1257.       UCHAR *p;
  1258.       p = text;
  1259.       for (; *p++; len++);
  1260.    }
  1261.    debug (DB_LOG, "ACK len", 0, len);
  1262.    rc = spkt ('Y', seq, len, text, k);  /* Send the packet */
  1263.    debug (DB_LOG, "ACK spkt rc", 0, rc);
  1264.    if (rc == X_OK && seq == k->r_seq)   /* If OK */
  1265.       k->r_seq = (k->r_seq + 1) & 63;   /* bump the packet number */
  1266.    debug (DB_LOG, "ACK new k->r_seq", 0, k->r_seq);
  1267.    return (rc);
  1268. }
  1269.  
  1270. /*
  1271.  * S P A R -- Set parameters requested by other Kermit 
  1272.  */
  1273.  
  1274. STATIC void
  1275. spar (struct k_data *k, UCHAR * s, int datalen)
  1276. {
  1277.    int x;
  1278.    int y = 0;
  1279.  
  1280.    debug (DB_MSG, "SPAR", 0, 0);
  1281.  
  1282.    s--;                         /* Line up with field numbers. */
  1283.  
  1284.    if (datalen >= 1)            /* Max packet length to send */
  1285.       k->s_maxlen = xunchar (s[1]);
  1286.  
  1287.    if (datalen >= 2)            /* Timeout on inbound packets */
  1288.       k->r_timo = xunchar (s[2]);
  1289.  
  1290.    /*
  1291.     * No padding 
  1292.     */
  1293.  
  1294.    if (datalen >= 5)            /* Outbound Packet Terminator */
  1295.       k->s_eom = xunchar (s[5]);
  1296.  
  1297.    if (datalen >= 6)            /* Incoming control prefix */
  1298.       k->r_ctlq = s[6];
  1299.  
  1300.    if (datalen >= 7)
  1301.    {                            /* 8th bit prefix */
  1302.       k->ebq = s[7];
  1303.       if ((s[7] > 32 && s[7] < 63) || (s[7] > 95 && s[7] < 127))
  1304.       {
  1305.          if (!k->parity)        /* They want it */
  1306.             k->parity = 1;      /* Set parity to something nonzero */
  1307.          k->ebqflg = 1;
  1308.       }
  1309.       else if (s[7] == 'Y' && k->parity)
  1310.       {                         // they will do 8th bit prefixing if requested
  1311.          k->ebqflg = 1;
  1312.          k->ebq = '&';
  1313.       }
  1314.       else if (s[7] == 'N')
  1315.       {                         // they refuse to do 8th bit prefixing
  1316.          /*
  1317.           * WHAT? 
  1318.           */
  1319.       }
  1320.    }
  1321.    if (datalen >= 8)
  1322.    {                            /* Block check */
  1323.       k->bct = s[8] - '0';
  1324.       if ((k->bct < 1) || (k->bct > 3))
  1325.          k->bct = 1;
  1326.       if (k->bcta3)
  1327.          k->bct = 3;
  1328.    }
  1329.    if (datalen >= 9)
  1330.    {                            /* Repeat counts */
  1331.       if ((s[9] > 32 && s[9] < 63) || (s[9] > 95 && s[9] < 127))
  1332.       {
  1333.          k->rptq = s[9];
  1334.          k->rptflg = 1;
  1335.       }
  1336.    }
  1337.    if (datalen >= 10)
  1338.    {                            /* Capability bits */
  1339.       x = xunchar (s[10]);
  1340.  
  1341.       if (!(x & CAP_LP))
  1342.          k->capas &= ~CAP_LP;
  1343.  
  1344.       if (!(x & CAP_SW))
  1345.          k->capas &= ~CAP_SW;
  1346.  
  1347.       if (!(x & CAP_AT))
  1348.          k->capas &= ~CAP_AT;
  1349.  
  1350. #ifdef F_RS                     /* Recovery */
  1351.       if (!(x & CAP_RS))
  1352. #endif /* F_RS */
  1353.          k->capas &= ~CAP_RS;
  1354.  
  1355. #ifdef F_LS                     /* Locking shifts */
  1356.       if (!(x & CAP_LS))
  1357. #endif /* F_LS */
  1358.          k->capas &= ~CAP_LS;
  1359.  
  1360.       /* In case other Kermit sends addt'l capas fields ...  */
  1361.  
  1362.       for (y = 10; (xunchar (s[y]) & 1) && (datalen >= y); y++);
  1363.    }
  1364.    if (k->capas & CAP_LP)
  1365.    {
  1366.       if (datalen > y + 1)
  1367.       {
  1368.          x = xunchar (s[y + 2]) * 95 + xunchar (s[y + 3]);
  1369.          k->s_maxlen = (x > P_PKTLEN) ? P_PKTLEN : x;
  1370.          if (k->s_maxlen < 10)
  1371.             k->s_maxlen = 60;
  1372.       }
  1373.    }
  1374.  
  1375.    debug (DB_LOG, "  s_maxlen", 0, k->s_maxlen);
  1376.  
  1377.    if (k->capas & CAP_SW)
  1378.    {
  1379.       if (datalen > y)
  1380.       {
  1381.          x = xunchar (s[y + 1]);
  1382.          k->wslots = (x > k->wslots_max) ? k->wslots_max : x;
  1383.          if (k->wslots < 1)     /* Watch out for bad negotiation */
  1384.             k->wslots = 1;
  1385.          if (k->wslots > 1)
  1386.             if (k->wslots > k->retry)   /* Retry limit must be greater */
  1387.                k->retry = k->wslots + 1;        /* than window size. */
  1388.       }
  1389.    }
  1390.    else
  1391.    {
  1392.       k->wslots = 1;
  1393.    }
  1394.  
  1395.    debug (DB_LOG, "  k->capas & CAP_LP", 0, k->capas & CAP_LP);
  1396.    debug (DB_LOG, "  k->capas & CAP_SW", 0, k->capas & CAP_SW);
  1397.    debug (DB_LOG, "  k->capas & CAP_AT", 0, k->capas & CAP_AT);
  1398.    debug (DB_LOG, "  k->capas & CAP_RS", 0, k->capas & CAP_RS);
  1399.    debug (DB_LOG, "  k->capas & CAP_LS", 0, k->capas & CAP_LS);
  1400.    debug (DB_CHR, "  k->ebq           ", 0, k->ebq);
  1401.    debug (DB_LOG, "  k->ebqflg        ", 0, k->ebqflg);
  1402.    debug (DB_LOG, "  k->parity        ", 0, k->parity);
  1403.    debug (DB_LOG, "  k->s_eom         ", 0, k->s_eom);
  1404.    debug (DB_LOG, "  k->r_timo        ", 0, k->r_timo);
  1405.    debug (DB_LOG, "  k->s_timo        ", 0, k->s_timo);
  1406.    debug (DB_CHR, "  k->r_ctlq        ", 0, k->r_ctlq);
  1407.    debug (DB_CHR, "  k->s_ctlq        ", 0, k->s_ctlq);
  1408.    debug (DB_CHR, "  k->rptq          ", 0, k->rptq);
  1409.    debug (DB_LOG, "  k->rptflg        ", 0, k->rptflg);
  1410.    debug (DB_LOG, "  k->bct           ", 0, k->bct);
  1411.    debug (DB_LOG, "  k->bcta3         ", 0, k->bcta3);
  1412.    debug (DB_LOG, "  k->r_maxlen      ", 0, k->r_maxlen);
  1413.    debug (DB_LOG, "  k->s_maxlen      ", 0, k->s_maxlen);
  1414.    debug (DB_LOG, "  k->wslots        ", 0, k->wslots);
  1415.    debug (DB_LOG, "  k->binary        ", 0, k->binary);
  1416.    debug (DB_LOG, "  k->retry         ", 0, k->retry);
  1417. }
  1418.  
  1419. /*
  1420.  * R P A R -- Send my parameters to other Kermit 
  1421.  */
  1422.  
  1423. STATIC int
  1424. rpar (struct k_data *k, char type)
  1425. {
  1426.    UCHAR *d;
  1427.    int rc, len;
  1428.    short bctsv;
  1429.    UCHAR *buf;
  1430.    short s_slot;
  1431.  
  1432.    debug (DB_LOG, "RPAR capas", 0, k->capas);
  1433.    debug (DB_LOG, "  wslots", 0, k->wslots);
  1434.  
  1435.    d = k->ack_s;                /* Where to put it */
  1436.    d[0] = tochar (94);          /* Maximum short-packet length */
  1437.    d[1] = tochar (k->s_timo);   /* When I want to be timed out */
  1438.    d[2] = tochar (0);           /* How much padding I need */
  1439.    d[3] = ctl (0);              /* Padding character I want */
  1440.    d[4] = tochar (k->r_eom);    /* End-of message character I want */
  1441.    d[5] = k->s_ctlq;            /* Control prefix I send */
  1442.    if ((k->ebq == 'Y') && (k->parity))  /* 8th-bit prefix */
  1443.       d[6] = k->ebq = '&';      /* I need to request it */
  1444.    else                         /* else just agree with other Kermit */
  1445.       d[6] = k->ebq;
  1446.    if (k->bcta3)
  1447.       d[7] = '3';
  1448.    else
  1449.       d[7] = k->bct + '0';      /* Block check type */
  1450.    d[8] = k->rptq;              /* Repeat prefix */
  1451.    d[9] = tochar (k->capas);    /* Capability bits */
  1452.    d[10] = tochar (k->wslots);  /* Window size */
  1453.  
  1454.    d[11] = tochar (k->r_maxlen / 95);   /* Long packet size, big part */
  1455.    d[12] = tochar (k->r_maxlen % 95);   /* Long packet size, little part */
  1456.    d[13] = '\0';                /* Terminate the init string */
  1457.    len = 13;
  1458.  
  1459.    if (k->bcta3)
  1460.    {
  1461.       bctsv = 3;
  1462.       k->bct = 3;
  1463.    }
  1464.    else
  1465.    {
  1466.       bctsv = k->bct;
  1467.       k->bct = 1;               /* Always use block check type 1 */
  1468.    }
  1469.    switch (type)
  1470.    {
  1471.    case 'Y':                   /* This is an ACK for packet 0 */
  1472.       rc = ack (k, 0, d);
  1473.       break;
  1474.    case 'S':                   /* It's an S packet */
  1475.       buf = get_sslot (k, &s_slot);     // get a new send slot
  1476.       k->s_pw[k->s_seq] = s_slot;
  1477.       debug (DB_LOG, "sending S pkt s_seq", 0, k->s_seq);
  1478.       debug (DB_LOG, "  s_slot", 0, s_slot);
  1479.       rc = spkt ('S', 0, len, d, k);
  1480.       break;
  1481.    default:
  1482.       rc = -1;
  1483.    }
  1484.    k->bct = bctsv;
  1485.    return (rc);                 /* Pass along return code. */
  1486. }
  1487.  
  1488. /*
  1489.  * D E C O D E -- Decode data field of Kermit packet - binary mode only 
  1490.  */
  1491. /*
  1492. Call with:
  1493.     k = kermit data structure
  1494.     r = kermit response structure
  1495.     f = function code:
  1496.         0: decode filename
  1497.         1: decode file data
  1498.     inbuf = pointer to packet data to be decoded
  1499.     Returns: X_OK on success X_ERROR if output function fails 
  1500.  */
  1501. STATIC int
  1502. decode (struct k_data *k, struct k_response *r, short f, UCHAR * inbuf,
  1503.         int rslot)
  1504. {
  1505.  
  1506.    register unsigned int a, a7; /* Current character */
  1507.    unsigned int b8;             /* 8th bit */
  1508.    int rpt;                     /* Repeat count */
  1509.    int rc;                      /* Return code */
  1510.    UCHAR *ucp = 0;
  1511.    int i;
  1512.    int nobuf;
  1513.    USHORT crc;
  1514.  
  1515.    rc = X_OK;
  1516.    rpt = 0;                     /* Initialize repeat count. */
  1517.    if (f == 0)                  /* Output function... */
  1518.       ucp = r->filename;
  1519.  
  1520.    debug (DB_LOG, "DECODE rslot", 0, rslot);
  1521.    if (rslot >= 0)
  1522.    {
  1523.       k->anseq = k->ipktinfo[rslot].seq;
  1524.       debug (DB_LOG, "DECODE seq", 0, k->ipktinfo[rslot].seq);
  1525.       debug (DB_LOG, "DECODE len", 0, k->ipktinfo[rslot].len);
  1526.       debug (DB_LOG, "DECODE crc", 0, k->ipktinfo[rslot].crc);
  1527.       debug (DB_HEX, "IHEX", inbuf, k->ipktinfo[rslot].len);
  1528.  
  1529.       for (i = 0; i < k->ipktinfo[rslot].len; i++)
  1530.          if (inbuf[i] == 0)
  1531.             break;
  1532.  
  1533.       if (i != k->ipktinfo[rslot].len)
  1534.       {
  1535.          debug (DB_LOG, "DECODE error i", 0, i);
  1536. #ifdef DEBUG
  1537.          show_rslots (k);
  1538. #endif
  1539.          epkt ("EKSW decode len bad", k);
  1540. //       exit (1);
  1541.          return (X_ERROR);
  1542.       }
  1543.  
  1544.       crc = chk3 (inbuf, k);
  1545.       if (crc != k->ipktinfo[rslot].crc)
  1546.       {
  1547.          debug (DB_LOG, "DECODE error crc", 0, crc);
  1548. #ifdef DEBUG
  1549.          show_rslots (k);
  1550. #endif
  1551.          epkt ("EKSW decode crc bad", k);
  1552. //       exit (1);
  1553.          return (X_ERROR);
  1554.       }
  1555.    }
  1556.  
  1557.    nobuf = 0;
  1558.    while ((a = *inbuf++ & 0xFF) != '\0')
  1559.    {                            /* Character loop */
  1560.       if (k->rptflg && a == k->rptq)
  1561.       {                         /* Got a repeat prefix? */
  1562.          rpt = xunchar (*inbuf++ & 0xFF);       /* Yes, get the repeat count, */
  1563.          a = *inbuf++ & 0xFF;   /* and get the prefixed character. */
  1564.       }
  1565.       b8 = 0;                   /* 8th-bit value */
  1566.       if (k->parity && (a == k->ebq))
  1567.       {                         /* Have 8th-bit prefix? */
  1568.          b8 = 0200;             /* Yes, flag the 8th bit */
  1569.          a = *inbuf++ & 0x7F;   /* and get the prefixed character. */
  1570.       }
  1571.       if (a == k->r_ctlq)
  1572.       {                         /* If control prefix, */
  1573.          a = *inbuf++ & 0xFF;   /* get its operand */
  1574.          a7 = a & 0x7F;         /* and its low 7 bits. */
  1575.          if ((a7 >= 0100 && a7 <= 0137) || a7 == '?')   /* Controllify */
  1576.             a = ctl (a);        /* if in control range. */
  1577.       }
  1578.       a |= b8;                  /* OR in the 8th bit */
  1579.  
  1580. //    debug(DB_LOG,"DECODE rpt",0,rpt);
  1581.       if (rpt == 0)
  1582.          rpt = 1;               /* If no repeats, then one */
  1583.  
  1584.       for (; rpt > 0; rpt--)
  1585.       {                         /* Output the char 'rpt' times */
  1586.          if (f == 0)
  1587.          {
  1588.             *ucp++ = (UCHAR) a; /* to memory */
  1589.          }
  1590.          else
  1591.          {                      /* or to file */
  1592.             k->obuf[k->obufpos++] = (UCHAR) a;  /* Deposit the byte */
  1593.             nobuf++;
  1594.             if (k->obufpos == k->obuflen)
  1595.             {                   /* Buffer full? */
  1596.                rc = (*(k->writef)) (k, k->obuf, k->obuflen);    /* Dump it. */
  1597.                debug (DB_LOG, "DECODE writef rc", 0, rc);
  1598.                r->sofar += k->obuflen;
  1599.                r->sofar_rumor += k->obuflen;
  1600.                if (rc != X_OK)
  1601.                   break;
  1602.                k->obufpos = 0;
  1603.             }
  1604.          }
  1605.       }
  1606.    }
  1607.  
  1608.    debug (DB_LOG, "DECODE nobuf", 0, nobuf);
  1609.    debug (DB_LOG, "DECODE obufpos", 0, k->obufpos);
  1610.  
  1611.    if (f == 0)                  /* If writing to memory */
  1612.       *ucp = '\0';              /* terminate the string */
  1613.  
  1614.    debug (DB_LOG, "DECODE rc", 0, rc);
  1615.    return (rc);
  1616. }
  1617.  
  1618. STATIC ULONG                    /* Convert decimal string to number */
  1619. stringnum (UCHAR * s, struct k_data * k)
  1620. {
  1621.    long n;
  1622.    n = 0L;
  1623.    while (*s == SP)
  1624.       s++;
  1625.    while (*s >= '0' && *s <= '9')
  1626.       n = n * 10 + (*s++ - '0');
  1627.    return (n);
  1628. }
  1629.  
  1630. STATIC UCHAR *                  /* Convert number to string */
  1631. numstring (ULONG n, UCHAR * buf, int buflen, struct k_data * k)
  1632. {
  1633.    int i, x;
  1634.    buf[buflen - 1] = '\0';
  1635.    for (i = buflen - 2; i > 0; i--)
  1636.    {
  1637.       x = n % 10L;
  1638.       buf[i] = x + '0';
  1639.       n /= 10L;
  1640.       if (!n)
  1641.          break;
  1642.    }
  1643.    if (n)
  1644.    {
  1645.       return ((UCHAR *) 0);
  1646.    }
  1647.    if (i > 0)
  1648.    {
  1649.       UCHAR *p, *s;
  1650.       s = &buf[i];
  1651.       p = buf;
  1652.       while ((*p++ = *s++));
  1653.       *(p - 1) = '\0';
  1654.    }
  1655.    return ((UCHAR *) buf);
  1656. }
  1657.  
  1658. /*
  1659.  * G A T T R -- Read incoming attributes.
  1660.  * 
  1661.  * Returns: -1 if no transfer mode (text/binary) was announced. 0 if text
  1662.  * was announced. 1 if binary was announced. 
  1663.  */
  1664.  
  1665. #define SIZEBUFL 32             /* For number conversions */
  1666.  
  1667. STATIC int
  1668. gattr (struct k_data *k, UCHAR * s, struct k_response *r)
  1669. {
  1670.    long fsize = 0, fsizek = 0;  /* File size */
  1671.    UCHAR c;                     /* Workers */
  1672.    int aln, i, rc;
  1673.  
  1674.    UCHAR sizebuf[SIZEBUFL];
  1675.  
  1676.    rc = -1;
  1677.    while ((c = *s++))
  1678.    {                            /* Get attribute tag */
  1679.       aln = xunchar (*s++);     /* Length of attribute string */
  1680.       switch (c)
  1681.       {
  1682.       case '!':                /* File length in K */
  1683.       case '"':                /* File type */
  1684.          for (i = 0; (i < aln) && (i < SIZEBUFL); i++)  /* Copy it */
  1685.             sizebuf[i] = *s++;
  1686.          sizebuf[i] = '\0';     /* Terminate with null */
  1687.          if (i < aln)
  1688.             s += (aln - i);     /* If field was too long for buffer */
  1689.          if (c == '!')
  1690.          {                      /* Length */
  1691.             fsizek = stringnum (sizebuf, k);    /* Convert to number */
  1692.          }
  1693.          else
  1694.          {                      /* Type */
  1695.             if (sizebuf[0] == 'A')      /* Text */
  1696.                rc = 0;
  1697.             else if (sizebuf[0] == 'B') /* Binary */
  1698.                rc = 1;
  1699.             debug (DB_LOG, "GATTR rc", 0, rc);
  1700.             debug (DB_LOG, "GATTR sizebuf", sizebuf, 0);
  1701.          }
  1702.          break;
  1703.  
  1704.       case '#':                /* File creation date */
  1705.          for (i = 0; (i < aln) && (i < DATE_MAX); i++)
  1706.             r->filedate[i] = *s++;      /* save it to a string */
  1707.          if (i < aln)
  1708.             s += (aln - i);
  1709.          r->filedate[i] = '\0';
  1710.          break;
  1711.  
  1712.       case '1':                /* File length in bytes */
  1713.          for (i = 0; (i < aln) && (i < SIZEBUFL); i++)  /* Copy it */
  1714.             sizebuf[i] = *s++;
  1715.          sizebuf[i] = '\0';     /* Terminate with null */
  1716.          if (i < aln)
  1717.             s += (aln - i);
  1718.          fsize = stringnum (sizebuf, k);        /* Convert to number */
  1719.          break;
  1720.  
  1721.       default:                 /* Unknown attribute */
  1722.          s += aln;              /* Just skip past it */
  1723.          break;
  1724.       }
  1725.    }
  1726.    if (fsize > -1L)
  1727.    {                            /* Remember the file size */
  1728.       r->filesize = fsize;
  1729.    }
  1730.    else if (fsizek > -1L)
  1731.    {
  1732.       r->filesize = fsizek * 1024L;
  1733.    }
  1734.    debug (DB_LOG, "gattr r->filesize", 0, (r->filesize));
  1735.    debug (DB_LOG, "gattr r->filedate=", r->filedate, 0);
  1736.    return (rc);
  1737. }
  1738.  
  1739. #define ATTRLEN 48
  1740.  
  1741. STATIC int
  1742. sattr (struct k_data *k, struct k_response *r)
  1743. {                               /* Build and send A packet */
  1744.    // int i, x, aln;
  1745.    int i, x;
  1746.    short tmp;
  1747.    long filelength;
  1748.    UCHAR datebuf[DATE_MAX], *p;
  1749.    UCHAR *buf;
  1750.    short s_slot;
  1751.  
  1752.    debug (DB_PKT, "SATTR k->zincnt 0", 0, (k->zincnt));
  1753.  
  1754.    tmp = k->binary;
  1755.    filelength = (*(k->finfo))
  1756.       (k, k->filename, datebuf, DATE_MAX, &tmp, k->xfermode);
  1757.    k->binary = tmp;
  1758.  
  1759.    debug (DB_LOG, "  filename: ", k->filename, 0);
  1760.    debug (DB_LOG, "  filedate: ", datebuf, 0);
  1761.    debug (DB_LOG, "  filelength", 0, filelength);
  1762.    debug (DB_LOG, "  binary", 0, (k->binary));
  1763.  
  1764.    i = 0;
  1765.  
  1766.    k->xdata[i++] = '"';
  1767.    if (k->binary)
  1768.    {                            /* Binary */
  1769.       k->xdata[i++] = tochar (2);       /* Two characters */
  1770.       k->xdata[i++] = 'B';      /* B for Binary */
  1771.       k->xdata[i++] = '8';      /* 8-bit bytes (note assumption...) */
  1772.    }
  1773.    else
  1774.    {                            /* Text */
  1775.       k->xdata[i++] = tochar (3);       /* Three characters */
  1776.       k->xdata[i++] = 'A';      /* A = (extended) ASCII with CRLFs */
  1777.       k->xdata[i++] = 'M';      /* M for carriage return */
  1778.       k->xdata[i++] = 'J';      /* J for linefeed */
  1779.       k->xdata[i++] = '*';      /* Encoding */
  1780.       k->xdata[i++] = tochar (1);       /* Length of value is 1 */
  1781.       k->xdata[i++] = 'A';      /* A for ASCII */
  1782.    }
  1783.    if (filelength > -1L)
  1784.    {                            /* File length in bytes */
  1785.       UCHAR lenbuf[16];
  1786.       r->filesize = filelength;
  1787.       p = numstring (filelength, lenbuf, 16, k);
  1788.       if (p)
  1789.       {
  1790.          for (x = 0; p[x]; x++);        /* Get length of length string */
  1791.          if (i + x < ATTRLEN - 3)
  1792.          {                      /* Don't overflow buffer */
  1793.             k->xdata[i++] = '1';        /* Length-in-Bytes attribute */
  1794.             k->xdata[i++] = tochar (x);
  1795.             while (*p)
  1796.                k->xdata[i++] = *p++;
  1797.          }
  1798.       }
  1799.    }
  1800.    debug (DB_LOG, "SATTR datebuf: ", datebuf, 0);
  1801.  
  1802.    if (datebuf[0])
  1803.    {                            /* File modtime */
  1804.       p = datebuf;
  1805.       for (x = 0; p[x]; x++);   /* Length of modtime */
  1806.       if (i + x < ATTRLEN - 3)
  1807.       {                         /* If it will fit */
  1808.          k->xdata[i++] = '#';   /* Add modtime attribute */
  1809.          k->xdata[i++] = tochar (x);    /* Its length */
  1810.          while (*p)             /* And itself */
  1811.             k->xdata[i++] = *p++;
  1812.          /*
  1813.           * Also copy modtime to result struct 
  1814.           */
  1815.          for (x = 0; x < DATE_MAX - 1 && datebuf[x]; x++)
  1816.             r->filedate[x] = datebuf[x];
  1817.          r->filedate[x] = '\0';
  1818.       }
  1819.    }
  1820.    k->xdata[i++] = '@';         /* End of Attributes */
  1821.    k->xdata[i++] = ' ';
  1822.    k->xdata[i] = '\0';          /* Terminate attribute string */
  1823.    debug (DB_PKT, "SATTR k->xdata: ", k->xdata, 0);
  1824.    buf = get_sslot (k, &s_slot);        // get a new send slot
  1825.    k->s_pw[k->s_seq] = s_slot;
  1826.    debug (DB_LOG, "SATTR sending A pkt s_seq", 0, k->s_seq);
  1827.    debug (DB_LOG, "  s_slot", 0, s_slot);
  1828.    return (spkt ('A', k->s_seq, -1, k->xdata, k));
  1829. }
  1830.  
  1831. STATIC int
  1832. getpkt (struct k_data *k, struct k_response *r)
  1833. {                               /* Fill a packet from file */
  1834.    int i, next, rpt, maxlen;
  1835. // static int c;                /* PUT THIS IN STRUCT */
  1836.    int c = k->cgetpkt;
  1837.  
  1838.    debug (DB_LOG, "GETPKT k->s_first", 0, k->s_first);
  1839. // debug (DB_PKT, "  k->s_remain=", k->s_remain, 0);
  1840.  
  1841.    if (k->bcta3)
  1842.       k->bct = 3;
  1843.    maxlen = k->s_maxlen - k->bct - 3 - 6;       /* Maximum data length */
  1844.    if (k->s_first == 1)
  1845.    {                            /* If first time thru...  */
  1846.       k->s_first = 0;           /* don't do this next time, */
  1847.       k->s_remain[0] = '\0';    /* discard any old leftovers. */
  1848.       if (k->istring)
  1849.       {                         /* Get first byte. */
  1850.          c = *(k->istring)++;   /* Of memory string... */
  1851.          if (!c)
  1852.             c = -1;
  1853.       }
  1854.       else
  1855.       {                         /* or file... */
  1856. #ifdef USE_ZGETC_MACRO
  1857.          c = zgetc ();
  1858. #else
  1859.          c = zgetc (k);
  1860. #endif
  1861.       }
  1862.  
  1863.       k->cgetpkt = c;
  1864.  
  1865.       if (c < 0)
  1866.       {                         /* Watch out for empty file. */
  1867.          debug (DB_LOG, "GETPKT first c", 0, c);
  1868.          k->s_first = -1;
  1869.          return (k->size = 0);
  1870.       }
  1871.       // r->sofar++; // makes it too big
  1872.       if (k->state == S_DATA)
  1873.          r->sofar_rumor++;
  1874.       debug (DB_LOG, "GETPKT first state", 0, k->state);
  1875.       debug (DB_CHR, "  first c", 0, c);
  1876.    }
  1877.    else if (k->s_first == -1 && !k->s_remain[0])
  1878.    {                            /* EOF from last time? */
  1879.       return (k->size = 0);
  1880.    }
  1881.  
  1882.    // string copy s_remain to xdata
  1883.    for (k->size = 0;
  1884.         (k->xdata[k->size] = k->s_remain[k->size]) != '\0'; (k->size)++);
  1885.  
  1886.    // set s_remain to zero length
  1887.    k->s_remain[0] = '\0';
  1888.  
  1889.    if (k->s_first == -1)
  1890.       return (k->size);
  1891.  
  1892.    rpt = 0;                     /* Initialize repeat counter. */
  1893.    while (k->s_first > -1)
  1894.    {                            /* Until end of file or string... */
  1895.       if (k->istring)
  1896.       {
  1897.          next = *(k->istring)++;
  1898.          if (!next)
  1899.             next = -1;
  1900.       }
  1901.       else
  1902.       {
  1903. #ifdef USE_ZGETC_MACRO
  1904.          next = zgetc ();
  1905. #else
  1906.          next = zgetc (k);
  1907. #endif
  1908.       }
  1909.       if (next < 0)
  1910.       {                         /* If none, we're at EOF. */
  1911.          k->s_first = -1;
  1912.       }
  1913.       else
  1914.       {                         /* Otherwise */
  1915.          r->sofar_rumor++;      /* count this byte */
  1916.       }
  1917.       k->osize = k->size;       /* Remember current size. */
  1918.       encode (c, next, k);      /* Encode the character. */
  1919.       /*
  1920.        * k->xdata[k->size] = '\0'; 
  1921.        */
  1922.       c = next;                 /* Old next char is now current. */
  1923.  
  1924.       k->cgetpkt = c;
  1925.  
  1926.       if (k->size == maxlen)
  1927.       {                         /* Just at end, done. */
  1928.          debug (DB_LOG, "GETPKT size perfect c", 0, c);
  1929.          return (k->size);
  1930.       }
  1931.  
  1932.       if (k->size > maxlen)
  1933.       {                         /* Past end, must save some. */
  1934.          for (i = 0; (k->s_remain[i] = k->xdata[(k->osize) + i]) != '\0';
  1935.               i++);
  1936.          debug (DB_LOG, "GETPKT size past end i", 0, i);
  1937.          k->size = k->osize;
  1938.          k->xdata[k->size] = '\0';
  1939.          return (k->size);      /* Return size. */
  1940.       }
  1941.    }
  1942.    if (k->size > maxlen)
  1943.    {
  1944.       debug (DB_LOG, "GETPKT error confused: from getpkt() k->size",
  1945.              0, k->size);
  1946.       epkt ("EKSW getpkt confused", k);
  1947. //    exit (1);
  1948.       return (X_ERROR);
  1949.    }
  1950.    return (k->size);            /* EOF, return size. */
  1951. }
  1952.  
  1953. STATIC int
  1954. sdata (struct k_data *k, struct k_response *r)
  1955. {                               /* Send a data packet */
  1956.    int len, rc;
  1957.    if (k->cancel)
  1958.    {                            /* Interrupted */
  1959.       debug (DB_LOG, "SDATA interrupted k->cancel", 0, (k->cancel));
  1960.       return (0);
  1961.    }
  1962.    len = getpkt (k, r);         /* Fill data field from input file */
  1963.    debug (DB_LOG, "SDATA getpkt len", 0, len);
  1964.    if (len < 1)
  1965.    {
  1966.       debug (DB_LOG, "SDATA getpkt got eof s_seq", 0, k->s_seq);
  1967.       return (0);
  1968.    }
  1969.    debug (DB_LOG, "SDATA sending D pkt s_seq", 0, k->s_seq);
  1970.  
  1971.    rc = spkt ('D', k->s_seq, len, k->xdata, k); /* Send the packet */
  1972.  
  1973.    debug (DB_LOG, "SDATA spkt rc", 0, rc);
  1974.    return ((rc == X_ERROR) ? rc : len);
  1975. }
  1976.  
  1977. /*
  1978.  * E P K T -- Send a (fatal) Error packet with the given message 
  1979.  */
  1980.  
  1981. STATIC void
  1982. epkt (char *msg, struct k_data *k)
  1983. {
  1984.    int bctsv;
  1985.  
  1986.    if (k->bcta3)
  1987.    {
  1988.       bctsv = 3;
  1989.       k->bct = 3;
  1990.    }
  1991.    else
  1992.    {
  1993.       bctsv = k->bct;
  1994.       k->bct = 1;
  1995.    }
  1996.    debug (DB_LOG, "EPKT msg:", msg, 0);
  1997.    spkt ('E', 0, -1, (UCHAR *) msg, k);
  1998.    k->bct = bctsv;
  1999. }
  2000.  
  2001. STATIC int                      /* Fill a packet from string s. */
  2002. encstr (UCHAR * s, struct k_data *k, struct k_response *r)
  2003. {
  2004.    k->s_first = 1;              /* Start lookahead. */
  2005.    k->istring = s;              /* Set input string pointer */
  2006.    getpkt (k, r);               /* Fill a packet */
  2007.    k->istring = (UCHAR *) 0;    /* Reset input string pointer */
  2008.    k->s_first = 1;              /* "Rewind" */
  2009.    return (k->size);            /* Return data field length */
  2010. }
  2011.  
  2012. /*
  2013.  * Decode packet data into a string 
  2014.  */
  2015.  
  2016. #if 0                           // never used
  2017. STATIC void
  2018. decstr (UCHAR * s, struct k_data *k, struct k_response *r)
  2019. {
  2020.    k->ostring = s;              /* Set output string pointer */
  2021.    (void) decode (k, r, 0, s, -1);
  2022.    *(k->ostring) = '\0';        /* Terminate with null */
  2023.    k->ostring = (UCHAR *) 0;    /* Reset output string pointer */
  2024. }
  2025. #endif
  2026.  
  2027. STATIC void
  2028. encode (int a, int next, struct k_data *k)
  2029. {                               /* Encode character into packet == k->xdata */
  2030.    int a7, b8, maxlen;
  2031.  
  2032.    maxlen = k->s_maxlen - 4;
  2033.    if (k->rptflg)
  2034.    {                            /* Doing run-length encoding? */
  2035.       if (a == next)
  2036.       {                         /* Yes, got a run? */
  2037.          if (++(k->s_rpt) < 94)
  2038.          {                      /* Yes, count. */
  2039.             return;
  2040.          }
  2041.          else if (k->s_rpt == 94)
  2042.          {                      /* If at maximum */
  2043.             k->xdata[(k->size)++] = k->rptq;    /* Emit prefix, */
  2044.             k->xdata[(k->size)++] = tochar (k->s_rpt);  /* and count, */
  2045.             k->s_rpt = 0;       /* and reset counter. */
  2046.          }
  2047.       }
  2048.       else if (k->s_rpt == 1)
  2049.       {                         /* Run broken, only two? */
  2050.          k->s_rpt = 0;          /* Yes, do the character twice */
  2051.          encode (a, -1, k);     /* by calling self recursively. */
  2052.          if (k->size <= maxlen) /* Watch boundary. */
  2053.             k->osize = k->size;
  2054.          k->s_rpt = 0;          /* Call self second time. */
  2055.          encode (a, -1, k);
  2056.          return;
  2057.       }
  2058.       else if (k->s_rpt > 1)
  2059.       {                         /* Run broken, more than two? */
  2060.          k->xdata[(k->size)++] = k->rptq;       /* Yes, emit prefix and count */
  2061.          k->xdata[(k->size)++] = tochar (++(k->s_rpt));
  2062.          k->s_rpt = 0;          /* and reset counter. */
  2063.       }
  2064.    }
  2065.    a7 = a & 127;                /* Get low 7 bits of character */
  2066.    b8 = a & 128;                /* And "parity" bit */
  2067.  
  2068.    if (k->ebqflg && b8)
  2069.    {                            /* If doing 8th bit prefixing */
  2070.       k->xdata[(k->size)++] = k->ebq;   /* and 8th bit on, insert prefix */
  2071.       a = a7;                   /* and clear the 8th bit. */
  2072.    }
  2073.  
  2074. // if (a7 < 32 || a7 == 127)    /* If in control range -- conservative */
  2075. // if (a7==0 || a7==1 || a7==13)  // 2004-07-04 -- JHD -- need 127 for telnet
  2076.    // this is a bit more conservative than C-Kermit "set prefixing minimal" 
  2077.    if (a7 == 0 || a7 == 1 || a7 == 3 || a7 == 4 || a7 == 10 ||
  2078.        a7 == 13 || a7 == 21 || a7 == 127)
  2079.    {
  2080.       k->xdata[(k->size)++] = k->s_ctlq;        /* insert control prefix */
  2081.       a = ctl (a);              /* and make character printable. */
  2082.    }
  2083.    else if (a7 == k->s_ctlq)    /* If data is control prefix, */
  2084.       k->xdata[(k->size)++] = k->s_ctlq;        /* prefix it. */
  2085.    else if (k->ebqflg && a7 == k->ebq)  /* If doing 8th-bit prefixing, */
  2086.       k->xdata[(k->size)++] = k->s_ctlq;        /* ditto for 8th-bit prefix. */
  2087.    else if (k->rptflg && a7 == k->rptq) /* If doing run-length encoding, */
  2088.       k->xdata[(k->size)++] = k->s_ctlq;        /* ditto for repeat prefix. */
  2089.  
  2090.    k->xdata[(k->size)++] = a;   /* Finally, emit the character. */
  2091.    k->xdata[(k->size)] = '\0';  /* Terminate string with null. */
  2092.  
  2093.    if (k->size < 0 || k->size >= P_PKTLEN + 2)
  2094.    {
  2095.       debug (DB_LOG, "confused: from encode() k->size", 0, k->size);
  2096.       epkt ("EKSW encode confused", k);
  2097. //    exit (1);
  2098.       return;
  2099.    }
  2100. }
  2101.  
  2102. STATIC short
  2103. nxtpkt (struct k_data *k)
  2104. {                               /* Get next packet to send */
  2105.    k->s_seq = (k->s_seq + 1) & 63;      /* Next sequence number */
  2106.    k->s_cnt++;
  2107.    k->xdata = k->xdatabuf;
  2108.    return (k->s_seq);
  2109. }
  2110.  
  2111. STATIC int
  2112. resend (struct k_data *k, short seq)
  2113. {
  2114.    UCHAR *buf;
  2115.    int ret;
  2116.    short slot;
  2117.  
  2118.    debug (DB_LOG, "RESEND seq", 0, seq);
  2119.  
  2120.    if (seq < 0)
  2121.       seq = earliest_sseq (k);
  2122.    if (seq < 0)
  2123.    {
  2124.       debug (DB_LOG, "RESEND failed: no earliest seq", 0, seq);
  2125. #ifdef DEBUG
  2126.       show_sslots (k);
  2127. #endif
  2128.       return (X_OK);
  2129.    }
  2130.    slot = k->s_pw[seq];
  2131.    if (slot < 0)
  2132.    {
  2133.       debug (DB_LOG, "RESEND failed: no slot for seq", 0, seq);
  2134. #ifdef DEBUG
  2135.       show_sslots (k);
  2136. #endif
  2137.       seq = earliest_sseq (k);
  2138.       if (seq < 0)
  2139.       {
  2140.          debug (DB_LOG, "RESEND failed: still no earliest seq", 0, seq);
  2141. #ifdef DEBUG
  2142.          show_sslots (k);
  2143. #endif
  2144.          return (X_OK);
  2145.       }
  2146.       slot = k->s_pw[seq];
  2147.       debug (DB_LOG, "RESEND sending earliest seq", 0, seq);
  2148.       debug (DB_LOG, "  slot", 0, slot);
  2149.    }
  2150.    if (slot < 0)
  2151.    {
  2152.       debug (DB_LOG, "RESEND failed: still bad slot", 0, slot);
  2153. #ifdef DEBUG
  2154.       show_sslots (k);
  2155. #endif
  2156.       return (X_OK);
  2157.    }
  2158.  
  2159.    debug (DB_MSG, "RESEND opktinfo:", 0, 0);
  2160.    debug (DB_LOG, "  seq", 0, k->opktinfo[slot].seq);
  2161.    debug (DB_CHR, "  typ", 0, k->opktinfo[slot].typ);
  2162.    debug (DB_LOG, "  len", 0, k->opktinfo[slot].len);
  2163.    debug (DB_LOG, "  rtr", 0, k->opktinfo[slot].rtr);
  2164.    debug (DB_LOG, "  crc", 0, k->opktinfo[slot].crc);
  2165.    debug (DB_LOG, "  flg", 0, k->opktinfo[slot].flg);
  2166.  
  2167.    k->opktlen = k->opktinfo[slot].len;
  2168.  
  2169.    if (k->opktlen < 0 || k->opktlen >= P_BUFLEN)
  2170.    {
  2171.       debug (DB_LOG, "RESEND error opktlen", 0, k->opktlen);
  2172.       return (X_ERROR);
  2173.    }
  2174.  
  2175.    // what about .seq, .typ, .rtr, .flg ?
  2176.  
  2177.    if (!k->opktlen)             /* Nothing to resend */
  2178.       return (X_OK);
  2179.  
  2180.    k->nresend++;
  2181.  
  2182.    buf = k->opktinfo[slot].buf;
  2183.  
  2184.    k->opktinfo[slot].rtr++;
  2185.    if (k->opktinfo[slot].rtr > k->retry)
  2186.    {
  2187.       debug (DB_LOG, "RESEND error retries", 0, k->opktinfo[slot].rtr);
  2188.       return (X_ERROR);
  2189.    }
  2190.  
  2191. // debug (DB_PKT, ">PKT", &buf[1], k->opktlen);
  2192.    ret = ((*(k->txd)) (k, buf, k->opktlen));
  2193.    debug (DB_LOG, "RESEND txd ret", 0, ret);
  2194.    return (ret);
  2195. }
  2196.  
  2197. int                             /* The kermit() function */
  2198. kermit (short fc,               /* Function code */
  2199.         struct k_data *k,       /* The control struct */
  2200.         int len,                /* Length of packet in buf */
  2201.         char *msg,              /* Message for error packet */
  2202.         struct k_response *r)   /* Response struct */
  2203. {
  2204.    int did_a_pkt = 0;
  2205.    UCHAR *buf = 0;
  2206.    short s_slot;
  2207.  
  2208.    // int i, j, rc; /* Workers */
  2209.    int i, rc;                   /* Workers */
  2210.    int datalen = 0;             /* Length of packet data field */
  2211.    // int bctu; /* Block check type for this packet */
  2212.    UCHAR *pdf = 0;              /* Pointer to packet data field */
  2213.    UCHAR *qdf = 0;              /* Pointer to data to be checked */
  2214.    UCHAR *s = 0;                /* Worker string pointer */
  2215.    // UCHAR c, t; /* Worker chars */
  2216.    UCHAR rtyp = 0;              /* Worker chars */
  2217.    UCHAR c;                     /* Worker chars */
  2218.    UCHAR pbc[4];                /* Copy of packet block check */
  2219.    short rseq = 0;              // actual received packet number
  2220.    short rlen = 0;
  2221.    short chklen;                /* Length of packet block check */
  2222.    unsigned int crc;            /* 16-bit CRC */
  2223.    int ok;
  2224.  
  2225.    /* Mark each entry: */
  2226.    debug (DB_LOG, "KERMIT ---------------------- version", VERSION, 0);
  2227.    debug (DB_LOG, "  fc", 0, fc);
  2228.    debug (DB_LOG, "  state", 0, k->state);
  2229.    debug (DB_LOG, "  zincnt", 0, (k->zincnt));
  2230.    debug (DB_LOG, "  k->wslots", 0, k->wslots);
  2231.  
  2232.    test_rslots (k);
  2233.  
  2234. #ifdef DEBUG
  2235.    show_sslots (k);
  2236. #endif
  2237.  
  2238.    if (fc == K_INIT)
  2239.    {                            /* Initialize packet buffers etc */
  2240.  
  2241.       k->version = (UCHAR *) VERSION;   /* Version of this module */
  2242.       r->filename[0] = '\0';    /* No filename yet. */
  2243.       r->filedate[0] = '\0';    /* No filedate yet. */
  2244.       r->filesize = 0L;         /* No filesize yet. */
  2245.       r->sofar = 0L;            /* No bytes transferred yet */
  2246.       r->sofar_rumor = 0L;      /* No bytes transferred yet */
  2247.  
  2248.  
  2249.       for (i = 0; i < P_WSLOTS; i++)
  2250.       {                         /* Packet info for each window slot */
  2251.          free_rslot (k, i);
  2252.          free_sslot (k, i);
  2253.       }
  2254.       for (i = 0; i < 64; i++)
  2255.       {                         /* Packet finder array */
  2256.          k->r_pw[i] = -1;       /* initialized to "no packets yet" */
  2257.          k->s_pw[i] = -1;       /* initialized to "no packets yet" */
  2258.       }
  2259.  
  2260. /* Initialize the k_data structure */
  2261.  
  2262.       k->sw_full = 0;
  2263.       k->do_rxd = 1;
  2264.       k->s_cnt = 0;
  2265.  
  2266.       for (i = 0; i < 6; i++)
  2267.          k->s_remain[i] = '\0';
  2268.  
  2269.       k->state = R_WAIT;        /* Beginning protocol state */
  2270.       r->rstatus = R_WAIT;
  2271.       k->what = W_RECV;         /* Default action */
  2272.       k->s_first = 1;           /* Beginning of file */
  2273.       k->r_soh = k->s_soh = SOH;        /* Packet start */
  2274.       k->r_eom = k->s_eom = CR; /* Packet end */
  2275.       k->s_seq = k->r_seq = 0;  /* Packet sequence number */
  2276.       k->s_cnt = 0;             /* Packet count */
  2277.       k->s_type = k->r_type = 0;        /* Packet type */
  2278.       k->r_timo = P_R_TIMO;     /* Timeout interval for me to use */
  2279.       k->s_timo = P_S_TIMO;     /* Timeout for other Kermit to use */
  2280.       k->r_maxlen = k->p_maxlen;        /* Maximum packet length */
  2281.       k->s_maxlen = k->p_maxlen;        /* Maximum packet length */
  2282.       k->wslots = k->wslots_max;        /* Current window slots */
  2283.       k->zincnt = 0;
  2284.       k->filename = (UCHAR *) 0;
  2285.  
  2286.       /* Parity must be filled in by the caller */
  2287.  
  2288.       k->retry = P_RETRY;       /* Retransmission limit */
  2289.       k->s_ctlq = k->r_ctlq = '#';      /* Control prefix */
  2290.       k->ebq = 'Y';             /* 8th-bit prefix negotiation */
  2291.       k->ebqflg = 0;            /* 8th-bit prefixing flag */
  2292.       k->rptq = '~';            /* Send repeat prefix */
  2293.       k->rptflg = 0;            /* Repeat counts negotiated */
  2294.       k->s_rpt = 0;             /* Current repeat count */
  2295.       k->capas = 0              /* Capabilities */
  2296.          | CAP_LP               /* Long packets */
  2297.          | CAP_AT               /* Attribute packets */
  2298.          ;
  2299.       if (k->wslots > 1)
  2300.          k->capas |= CAP_SW;    /* Sliding windows */
  2301.  
  2302.       for (i = 0; i < P_WSLOTS; i++)
  2303.       {
  2304.          k->ipktinfo[i].buf = k->ipktbufs + i * P_BUFLEN;
  2305.          k->ipktinfo[i].buf[0] = '\0';
  2306.          k->ipktinfo[i].len = 0;
  2307.          k->ipktinfo[i].seq = -1;
  2308.          k->ipktinfo[i].typ = SP;
  2309.          k->ipktinfo[i].dat = (UCHAR *) (0);
  2310.          k->ipktinfo[i].crc = 0xFFFF;
  2311.       }
  2312.  
  2313.       for (i = 0; i < P_WSLOTS; i++)
  2314.       {
  2315.          k->opktinfo[i].buf = k->opktbuf + i * P_BUFLEN;
  2316.          k->opktinfo[i].buf[0] = '\0';
  2317.          k->opktinfo[i].len = 0;
  2318.          k->opktinfo[i].seq = -1;
  2319.          k->opktinfo[i].typ = SP;
  2320.          k->opktinfo[i].dat = (UCHAR *) (0);
  2321.       }
  2322.  
  2323.       k->opktlen = 0;
  2324.  
  2325.       /* This is the only way to initialize these tables -- no static data. */
  2326.  
  2327.       k->crcta[0] = 0;          /* CRC generation table A */
  2328.       k->crcta[1] = 010201;
  2329.       k->crcta[2] = 020402;
  2330.       k->crcta[3] = 030603;
  2331.       k->crcta[4] = 041004, k->crcta[5] = 051205;
  2332.       k->crcta[6] = 061406;
  2333.       k->crcta[7] = 071607;
  2334.       k->crcta[8] = 0102010;
  2335.       k->crcta[9] = 0112211;
  2336.       k->crcta[10] = 0122412;
  2337.       k->crcta[11] = 0132613;
  2338.       k->crcta[12] = 0143014, k->crcta[13] = 0153215;
  2339.       k->crcta[14] = 0163416;
  2340.       k->crcta[15] = 0173617;
  2341.  
  2342.       k->crctb[0] = 0;          /* CRC table B */
  2343.       k->crctb[1] = 010611;
  2344.       k->crctb[2] = 021422;
  2345.       k->crctb[3] = 031233;
  2346.       k->crctb[4] = 043044;
  2347.       k->crctb[5] = 053655;
  2348.       k->crctb[6] = 062466;
  2349.       k->crctb[7] = 072277;
  2350.       k->crctb[8] = 0106110;
  2351.       k->crctb[9] = 0116701;
  2352.       k->crctb[10] = 0127532;
  2353.       k->crctb[11] = 0137323;
  2354.       k->crctb[12] = 0145154;
  2355.       k->crctb[13] = 0155745;
  2356.       k->crctb[14] = 0164576;
  2357.       k->crctb[15] = 0174367;
  2358.  
  2359. //              k->nspkt = 0;
  2360. //    k->nsack = 0;
  2361.       k->nsnak = 0;
  2362.       k->nresend = 0;
  2363. //    k->nepkt = 0;
  2364. //    k->nbchk = 0;
  2365. //    k->nshort = 0;
  2366. //    k->nnaa = 0;
  2367. //    k->ndiscard1 = 0;
  2368. //    k->ndiscard2 = 0;
  2369. //    k->ndiscard3 = 0;
  2370.  
  2371.       return (X_OK);
  2372.  
  2373.    }
  2374.    else if (fc == K_GET)
  2375.    {
  2376.       /*
  2377.        * Send R packet with filenames we want.
  2378.        * Filenames cannot have spaces
  2379.        * since names are separated by spaces.
  2380.        */
  2381.       int i;
  2382.       char *p, *q;
  2383.  
  2384.       debug (DB_LOG, "function code == K_GET fc", 0, fc);
  2385.  
  2386.       p = (char *) (k->obuf);
  2387.       for (i = 0;; i++)
  2388.       {
  2389.          q = (char *) (k->filelist[i]);
  2390.          if (q == 0)
  2391.             break;
  2392.          if (i > 0)
  2393.             *p++ = ' ';
  2394.          while (*q)
  2395.             *p++ = *q++;
  2396.       }
  2397.       *p = 0;
  2398.  
  2399.       debug (DB_LOG, "before encstr k->obuf", k->obuf, 0);
  2400.       k->xdata = k->xdatabuf;
  2401.       encstr (k->obuf, k, r);   // Encode the name for transmission 
  2402.       debug (DB_LOG, "after encstr k->xdata", k->xdata, 0);
  2403.  
  2404.       buf = get_sslot (k, &s_slot);     // get a new send slot
  2405.       k->s_pw[k->s_seq] = s_slot;
  2406.       if (k->bcta3)
  2407.          k->bct = 3;
  2408.       else
  2409.          k->bct = 1;
  2410.       debug (DB_LOG, "K_GET sending R pkt s_seq", 0, k->s_seq);
  2411.       if (spkt ('R', k->s_seq, -1, k->xdata, k) != X_OK)
  2412.       {
  2413.          debug (DB_LOG, "K_GET error spkt(R) failed fc", 0, fc);
  2414.          return (X_ERROR);      /* I/O error, quit. */
  2415.       }
  2416.       k->state = R_WAIT;        /* All OK, switch states */
  2417.       r->rstatus = R_WAIT;
  2418.       k->what = W_GET;
  2419.       return (X_OK);
  2420.    }
  2421.    else if (fc == K_SEND)
  2422.    {
  2423.       if (rpar (k, 'S') != X_OK)        /* Send S packet with my parameters */
  2424.       {
  2425.          debug (DB_LOG, "K_SEND error rpar(S) failed fc", 0, fc);
  2426.          return (X_ERROR);      /* I/O error, quit. */
  2427.       }
  2428.       k->state = S_INIT;        /* All OK, switch states */
  2429.       r->rstatus = S_INIT;
  2430.       k->what = W_SEND;         /* Act like a sender */
  2431.       return (X_OK);
  2432.    }
  2433.    else if (fc == K_STATUS)
  2434.    {                            /* Status report requested. */
  2435.       debug (DB_LOG, "function code == K_STATUS fc", 0, fc);
  2436.       return (X_STATUS);        /* File name, date, size, if any. */
  2437.    }
  2438.    else if (fc == K_QUIT)
  2439.    {                            /* You told me to quit */
  2440.       debug (DB_LOG, "function code == K_QUIT fc", 0, fc);
  2441.       return (X_DONE);          /* so I quit. */
  2442.    }
  2443.    else if (fc == K_SYNC)
  2444.    {
  2445.       debug (DB_LOG, "function code == K_SYNC fc", 0, fc);
  2446.       epkt (msg, k);
  2447.       return (X_OK);
  2448.    }
  2449.    else if (fc == K_ERROR)
  2450.    {                            /* Send an error packet... */
  2451.       debug (DB_LOG, "K_ERROR error fc", 0, fc);
  2452.       epkt (msg, k);
  2453.       k->closef (k, 0, (k->state == S_DATA) ? 1 : 2);   /* Close file */
  2454.       return (X_DONE);          /* and quit. */
  2455.    }
  2456.    else if (fc != K_RUN)
  2457.    {                            /* Anything else is an error. */
  2458.       debug (DB_LOG, "not K_RUN error fc", 0, fc);
  2459.       return (X_ERROR);
  2460.    }
  2461.  
  2462.    if (k->state == R_NONE)      /* (probably unnecessary) */
  2463.       return (X_OK);
  2464.  
  2465.    /* If we're in the protocol, check to make sure we got a new packet */
  2466.    debug (DB_MSG, "In the protocol", 0, 0);
  2467.  
  2468.    if (k->do_rxd)
  2469.    {                            // we tried to read a packet in main loop
  2470.  
  2471.       debug (DB_LOG, "DO_RXD len", 0, len);     // in kermit call list
  2472.  
  2473.       if (len < 4)
  2474.       {                         /* Packet obviously no good? */
  2475.          int ret;
  2476.  
  2477.          if (k->what == W_RECV) /* If receiving */
  2478.          {
  2479.             debug (DB_MSG, "DO_RXD len<4", 0, 0);
  2480. #ifdef USE_NAK_OLDEST_UNACKED
  2481.             nak_oldest_unacked (k, -1);
  2482. #endif
  2483.             if (k->state == R_FILE)
  2484.             {
  2485.                debug (DB_MSG, "calling rpar again", 0, 0);
  2486.                rc = rpar (k, 'Y');      /* ACK again with my parameters */
  2487.             }
  2488.             return (X_OK);
  2489.          }
  2490.          else                   /* If W_SEND or W_GET */
  2491.          {
  2492.             debug (DB_MSG, "DO_RXD len<4: resending earliest", 0, 0);
  2493.             ret = resend (k, -1);       /* retransmit earliest packet in queue. */
  2494.             return (ret);
  2495.          }
  2496.       }
  2497.  
  2498.       /* Parse the packet */
  2499.  
  2500.       if (k->what == W_RECV)
  2501.       {                         /* If we're sending ACKs */
  2502.          switch (k->cancel)
  2503.          {                      /* Get cancellation code if any */
  2504.          case 0:
  2505.             s = (UCHAR *) 0;
  2506.             break;
  2507.          case 1:
  2508.             s = (UCHAR *) "X";
  2509.             break;
  2510.          case 2:
  2511.             s = (UCHAR *) "Z";
  2512.             break;
  2513.          }
  2514.       }
  2515.  
  2516.       pdf = k->ipktbuf;
  2517.  
  2518.       qdf = pdf;                /* Pointer to data to be checked */
  2519.       rlen = xunchar (*pdf++);  /* Length field */
  2520.       rseq = xunchar (*pdf++);  /* Received Sequence number */
  2521.       rtyp = *pdf++;            /* Type */
  2522.  
  2523.       if (k->state == S_EOT &&
  2524.           len == 4 && rseq == 0 && rtyp == 'N' && *pdf == 0x33)
  2525.       {
  2526.          debug (DB_MSG, "Got NAK for seq=0 after sending B-pkt", 0, 0);
  2527.          debug (DB_MSG, "  so assuming we are done.", 0, 0);
  2528.          return (X_DONE);       /* (or X_ERROR) */
  2529.       }
  2530.  
  2531.       // Really to use rseq the packet must be validated by CRC first.
  2532.       if (rseq < 0 || rseq > 63)
  2533.       {
  2534.          debug (DB_LOG, "WARNING: TOSSING BAD PACKET: rseq", 0, rseq);
  2535.          debug (DB_LOG, "  rlen", 0, rlen);
  2536.          debug (DB_CHR, "  rtyp", 0, rtyp);
  2537.          debug (DB_LOG, "  what", 0, k->what);
  2538.          debug (DB_PKT, "  k->ipktbuf", k->ipktbuf, 0);
  2539.          return (X_OK);
  2540.       }
  2541.  
  2542.       if ((k->what == W_RECV) && (rtyp == 'N' || rtyp == 'Y'))
  2543.       {
  2544.          /* Echo (it happens), ignore */
  2545.          return (X_OK);
  2546.       }
  2547.  
  2548.       if (rlen == 0)
  2549.       {                         /* Length 0 means long packet */
  2550.          c = pdf[2];            /* Get header checksum */
  2551.          pdf[2] = '\0';
  2552.          if (xunchar (c) != chk1 (pdf - 3, k))
  2553.          {                      /* Check it */
  2554.             int ret;
  2555.             debug (DB_MSG, "long pkt chk1 bad", 0, 0);
  2556.             if (k->what == W_RECV)
  2557.             {
  2558. #ifdef USE_NAK_OLDEST_UNACKED
  2559.                debug (DB_MSG, "sending NAK for oldest unacked", 0, 0);
  2560.                nak_oldest_unacked (k, -1);      /* Send NAK */
  2561. #endif
  2562.                return (X_OK);
  2563.             }
  2564.             else
  2565.             {
  2566.                debug (DB_MSG, "resending earliest", 0, 0);
  2567.                ret = resend (k, -1);
  2568.                debug (DB_LOG, "resend ret", 0, ret);
  2569.                return (ret);
  2570.             }
  2571.          }
  2572.          debug (DB_MSG, "HDR CHKSUM OK", 0, 0);
  2573.          pdf[2] = c;            /* Put checksum back */
  2574.          if (k->bcta3)
  2575.             k->bct = 3;
  2576.          datalen = xunchar (pdf[0]) * 95 + xunchar (pdf[1]) - k->bct;   /* Data length */
  2577.  
  2578.          debug (DB_LOG, "  long packet datalen", 0, datalen);
  2579.          debug (DB_LOG, "  rlen", 0, rlen);
  2580.          debug (DB_LOG, "  rseq", 0, rseq);
  2581.  
  2582.          pdf += 3;              /* Fix data pointer */
  2583.       }
  2584.       else
  2585.       {                         /* Regular packet */
  2586.          if (k->bcta3)
  2587.             k->bct = 3;
  2588.          datalen = rlen - k->bct - 2;   /* Data length */
  2589.          debug (DB_LOG, "regular packet datalen", 0, datalen);
  2590.       }
  2591.  
  2592.  
  2593.       if (rtyp == 'S' || k->state == S_INIT)
  2594.       {                         /* S-packet was
  2595.                                  * retransmitted? */
  2596.          if (k->bcta3)
  2597.          {
  2598.             chklen = 3;
  2599.             datalen = rlen - 5;
  2600.          }
  2601.          else
  2602.          {
  2603.             chklen = 1;         /* Block check is always type 1 */
  2604.             datalen = rlen - 3;
  2605.          }
  2606.          debug (DB_LOG, "S-packet datalen", 0, datalen);
  2607.       }
  2608.       else
  2609.       {
  2610.          if (k->bcta3)
  2611.             k->bct = 3;
  2612.          chklen = k->bct;
  2613.       }
  2614.  
  2615.       debug (DB_LOG, "DO_RXD state", 0, k->state);
  2616.       debug (DB_MSG, "  These are unverified: (before blk chk tested)", 0, 0);
  2617.       debug (DB_LOG, "  rlen", 0, rlen);
  2618.       debug (DB_LOG, "  rseq", 0, rseq);
  2619.       debug (DB_CHR, "  rtyp", 0, rtyp);
  2620.       debug (DB_LOG, "  bct", 0, k->bct);
  2621.       debug (DB_LOG, "  bcta3", 0, k->bcta3);
  2622.       debug (DB_LOG, "  datalen", 0, datalen);
  2623.       debug (DB_LOG, "  chklen", 0, chklen);
  2624.  
  2625.       if (datalen < 0 || datalen + chklen + 1 >= P_BUFLEN)
  2626.       {
  2627.          debug (DB_MSG, "DO_RXD datalen out of bounds", 0, 0);
  2628.          if (k->what == W_RECV)
  2629.          {
  2630. #ifdef USE_NAK_OLDEST_UNACKED
  2631.             debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2632.             nak_oldest_unacked (k, rseq);
  2633. #endif
  2634.          }
  2635.          else
  2636.          {
  2637.             debug (DB_MSG, "  resend earliest", 0, 0);
  2638.             resend (k, -1);
  2639.          }
  2640.          return (X_OK);
  2641.       }
  2642.  
  2643.       for (i = 0; i < chklen; i++)      /* Copy the block check */
  2644.          pbc[i] = pdf[datalen + i];
  2645.       pbc[i] = '\0';            /* Null-terminate block check string */
  2646.       pdf[datalen] = '\0';      /* and the packet DATA field. */
  2647.       switch (chklen)
  2648.       {                         /* Check the block check */
  2649.       case 1:                  /* Type 1, 6-bit checksum */
  2650.          ok = (xunchar (*pbc) == chk1 (qdf, k));
  2651. #ifdef DEBUG
  2652.          if (ok && xerror ())
  2653.             ok = 0;
  2654. #endif /* DEBUG */
  2655.          if (!ok)
  2656.          {
  2657.             debug (DB_CHR, "6-bit checksum ERROR rtyp", 0, rtyp);
  2658.             debug (DB_LOG, "  rseq", 0, rseq);
  2659.             debug (DB_PKT, "  k->ipktbuf", k->ipktbuf, 0);
  2660.             if (k->what == W_RECV)
  2661.             {
  2662. #ifdef USE_NAK_OLDEST_UNACKED
  2663.                debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2664.                nak_oldest_unacked (k, rseq);
  2665. #endif
  2666.             }
  2667.             else
  2668.             {
  2669.                debug (DB_MSG, "  resend earliest", 0, 0);
  2670.                resend (k, -1);
  2671.             }
  2672.             return (X_OK);
  2673.          }
  2674.          break;
  2675.  
  2676.       case 2:                  /* Type 2, 12-bit checksum */
  2677.          i = xunchar (*pbc) << 6 | xunchar (pbc[1]);
  2678.          ok = (i == chk2 (qdf, k));
  2679. #ifdef DEBUG
  2680.          if (ok && xerror ())
  2681.             ok = 0;
  2682. #endif /* DEBUG */
  2683.          if (!ok)
  2684.          {                      /* No match */
  2685.             debug (DB_CHR, "12-bit checksum ERROR rtyp", 0, rtyp);
  2686.             debug (DB_LOG, "  rseq", 0, rseq);
  2687.             debug (DB_PKT, "  k->ipktbuf", k->ipktbuf, 0);
  2688.             if (rtyp == 'E')
  2689.             {                   /* Allow E packets to have type 1 */
  2690.                int j;
  2691.                j = datalen;
  2692.                pdf[j++] = pbc[0];
  2693.                pdf[j] = '\0';
  2694.                if (xunchar (pbc[1]) == chk1 (qdf, k))
  2695.                   break;
  2696.                else
  2697.                   pdf[--j] = '\0';
  2698.             }
  2699.             if (k->what == W_RECV)
  2700.             {
  2701. #ifdef USE_NAK_OLDEST_UNACKED
  2702.                debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2703.                nak_oldest_unacked (k, rseq);
  2704. #endif
  2705.             }
  2706.             else
  2707.             {
  2708.                debug (DB_MSG, "  resend earliest", 0, 0);
  2709.                resend (k, -1);
  2710.             }
  2711.             return (X_OK);
  2712.          }
  2713.          break;
  2714.  
  2715.       case 3:                  /* Type 3, 16-bit CRC */
  2716.          crc = (xunchar (pbc[0]) << 12)
  2717.             | (xunchar (pbc[1]) << 6) | (xunchar (pbc[2]));
  2718.          ok = (crc == chk3 (qdf, k));
  2719. #ifdef DEBUG
  2720.          if (ok && xerror ())
  2721.          {
  2722.             ok = 0;
  2723.             debug (DB_MSG, "INPUT CRC ERROR INJECTED", 0, 0);
  2724.          }
  2725. #endif /* DEBUG */
  2726.          if (!ok)
  2727.          {
  2728.             debug (DB_MSG, "Packet blk chk3 bad", 0, 0);
  2729.             debug (DB_LOG, "  rseq", 0, rseq);
  2730.  
  2731.             if (rtyp == 'E')
  2732.             {                   /* Allow E packets to have type 1 */
  2733.                int j;
  2734.                j = datalen;
  2735.                pdf[j++] = pbc[0];
  2736.                pdf[j++] = pbc[1];
  2737.                pdf[j] = '\0';
  2738.                if (xunchar (pbc[2]) == chk1 (qdf, k))
  2739.                   break;
  2740.                else
  2741.                {
  2742.                   j -= 2;
  2743.                   pdf[j] = '\0';
  2744.                }
  2745.             }
  2746.             if (k->what == W_RECV)
  2747.             {
  2748. #ifdef USE_NAK_OLDEST_UNACKED
  2749.                debug (DB_LOG, "  NAK oldest or rseq", 0, rseq);
  2750.                nak_oldest_unacked (k, rseq);
  2751. #endif
  2752.             }
  2753.             else
  2754.             {
  2755.                debug (DB_MSG, "  resend earliest", 0, 0);
  2756.                resend (k, -1);
  2757.             }
  2758.             return (X_OK);
  2759.          }
  2760.       }
  2761.  
  2762.       // now the packet has a good block check
  2763.       debug (DB_MSG, "Packet blk chk good", 0, 0);
  2764.       debug (DB_CHR, "  rtyp", 0, rtyp);
  2765.       debug (DB_LOG, "  rseq", 0, rseq);
  2766.       debug (DB_LOG, "  k->r_seq", 0, k->r_seq);
  2767.       debug (DB_LOG, "  k->state", 0, k->state);
  2768.       debug (DB_LOG, "  chklen", 0, chklen);
  2769.       debug (DB_LOG, "  datalen", 0, datalen);
  2770.  
  2771.       if (rtyp == 'E')          /* (AND CLOSE FILES?) */
  2772.       {
  2773.          debug (DB_MSG, "error msg received from other kermit", 0, 0);
  2774.          debug (DB_PKT, "EPKT", pdf, 0);
  2775.          return (X_ERROR);
  2776.       }
  2777.  
  2778.       if (k->what == W_SEND)    /* Sending, check for ACK */
  2779.       {
  2780.          if (rtyp != 'Y')
  2781.          {
  2782.             int ret;
  2783.  
  2784.             debug (DB_CHR, "rtyp not Y, rtyp", 0, rtyp);
  2785.  
  2786.             if (k->state == S_DATA &&
  2787.                 rtyp == 'N' && rseq == ((k->r_seq + 1) & 63))
  2788.             {
  2789.                debug (DB_MSG,
  2790.                       "FYI got NAK for packet just after last one sent", 0,
  2791.                       0);
  2792. #ifdef DEBUG
  2793.                show_sslots (k);
  2794. #endif
  2795.  
  2796. # if 0
  2797.                debug (DB_MSG, "W_SEND Freeing all send slots", 0, 0);
  2798.                for (i = 0; i < k->wslots; i++)
  2799.                   free_sslot (k, i);
  2800.                k->sw_full = 0;
  2801. # endif
  2802.             }
  2803.  
  2804.             if (nused_sslots (k) == 0)
  2805.             {
  2806.                debug (DB_LOG, "sending requested rseq", 0, rseq);
  2807.  
  2808.                nxtpkt (k);
  2809.                if (k->s_seq != rseq)
  2810.                {
  2811.                   debug (DB_LOG, "error confused s_seq", 0, k->s_seq);
  2812.                   debug (DB_LOG, "  rseq", 0, rseq);
  2813.                   epkt ("EKSW W_SEND confused", k);
  2814. //                exit (1);
  2815.                   return (X_ERROR);
  2816.                }
  2817.                buf = get_sslot (k, &s_slot);    // get a new send slot
  2818.                k->s_pw[k->s_seq] = s_slot;
  2819.  
  2820.                rc = sdata (k, r);       /* Send next data packet */
  2821.                if (rc == X_ERROR)
  2822.                   return (rc);
  2823.                if (rc == 0)
  2824.                {                /* If there was no data to send */
  2825.                   k->closef (k, 0, 1);  /* Close input file */
  2826.                   k->state = S_EOF;     /* And wait for ACK */
  2827.                   r->rstatus = S_EOF;
  2828.                   k->s_seq--;
  2829.                   if (k->s_seq < 0)
  2830.                      k->s_seq += 64;
  2831.                   free_sslot (k, s_slot);
  2832.                }                /* Otherwise stay in data state */
  2833.                k->r_seq = k->s_seq;     /* Sequence number to wait for */
  2834.                return (X_OK);
  2835.             }
  2836.             else
  2837.             {
  2838.                debug (DB_LOG, "6: resending rseq", 0, rseq);
  2839.                // resend will send rseq if it's in the send table
  2840.                // otherwise it will resend earliest unacked packet
  2841.                ret = resend (k, rseq);
  2842.                debug (DB_LOG, "ret", 0, ret);
  2843.                return (ret);
  2844.             }
  2845.          }
  2846.  
  2847.          // we're sending, received an ACK and packet block check OK
  2848.  
  2849.          s_slot = k->s_pw[rseq];
  2850.          if (s_slot < 0 || s_slot >= k->wslots)
  2851.          {
  2852.             debug (DB_LOG, "ignoring ack -- not in table rseq", 0, rseq);
  2853. #ifdef DEBUG
  2854.             show_sslots (k);
  2855. #endif
  2856.             return (X_OK);
  2857.          }
  2858.  
  2859.          // set ACK'd flag for that send slot
  2860.          k->opktinfo[s_slot].flg = 1;
  2861.  
  2862.          // remove earliest and subsequent
  2863.          free_sslot_easca (k);
  2864.  
  2865. #ifdef DEBUG
  2866.          // check that send table sequence numbers are in order
  2867.          chk_sseq_nos (k);
  2868. #endif
  2869.  
  2870.          if (k->state == S_DATA)
  2871.          {                      /* ACK to Data packet? */
  2872.             if (k->cancel ||    /* Cancellation requested by caller? */
  2873.                 *pdf == 'X' || *pdf == 'Z')
  2874.             {                   /* Or by receiver? */
  2875.                k->closef (k, 0, 1);     /* Close input file */
  2876.                nxtpkt (k);      /* Next packet sequence number */
  2877.                buf = get_sslot (k, &s_slot);    // get a new send slot
  2878.                k->s_pw[k->s_seq] = s_slot;
  2879.  
  2880.                debug (DB_LOG, "Cancellation: sending Z pkt s_seq", 0,
  2881.                       k->s_seq);
  2882.                debug (DB_LOG, "  s_slot", 0, s_slot);
  2883.  
  2884.                rc = spkt ('Z', k->s_seq, 0, (UCHAR *) 0, k);
  2885.                debug (DB_LOG, "  rc", 0, rc);
  2886.                if (rc != X_OK)
  2887.                {
  2888.                   return (rc);
  2889.                }
  2890.                if (*pdf == 'Z' || k->cancel == I_GROUP)
  2891.                {                /* Cancel Group? */
  2892.                   debug (DB_MSG, "Group Cancel (Send)", 0, 0);
  2893.                   while (*(k->filelist))
  2894.                   {             /* Go to end of file list */
  2895.                      debug (DB_LOG, "Skip", *(k->filelist), 0);
  2896.                      (k->filelist)++;
  2897.                   }
  2898.                }
  2899.                k->state = S_EOF;        /* Wait for ACK to EOF */
  2900.                r->rstatus = S_EOF;
  2901.                k->r_seq = k->s_seq;     /* Sequence number of packet we want */
  2902.                return (X_OK);
  2903.             }
  2904.          }
  2905.          r->sofar = r->sofar_rumor;
  2906.  
  2907.          debug (DB_LOG, "end of W_SEND rseq", 0, rseq);
  2908.          debug (DB_LOG, "  k->r_seq", 0, k->r_seq);
  2909.          debug (DB_LOG, "  k->state", 0, k->state);
  2910.          debug (DB_CHR, "  rtyp", 0, rtyp);
  2911.       }                         // if (k->what == W_SEND)
  2912.    }                            // if(k->do_rxd)
  2913.  
  2914.    switch (k->state)
  2915.    {                            /* Kermit protocol state switcher */
  2916.  
  2917.    case S_INIT:                /* Got other Kermit's parameters */
  2918.    case S_EOF:                 /* got ack to Z packet */
  2919.       if (k->state == S_INIT)
  2920.       {                         /* Got ACK to S packet? */
  2921.          debug (DB_MSG, "S_INIT", 0, 0);
  2922.          spar (k, pdf, datalen);        /* Set negotiated parameters */
  2923.          debug (DB_CHR, "Parity", 0, k->parity);
  2924.          debug (DB_LOG, "Ebqflg", 0, k->ebqflg);
  2925.          debug (DB_CHR, "Ebq", 0, k->ebq);
  2926.       }
  2927.       else
  2928.       {
  2929.          debug (DB_LOG, "S_EOF rseq", 0, rseq);
  2930.  
  2931. # if 0                          // don't use k->r_seq to switch states -- use nused_sslots()
  2932.          debug (DB_LOG, "S_EOF k->r_seq", 0, k->r_seq);
  2933.          if (rseq != k->r_seq)  // keep reading until last packet ACKed
  2934.             return (X_OK);
  2935. # endif
  2936.  
  2937.          if ((i = nused_sslots (k)) > 0)
  2938.          {
  2939.             debug (DB_LOG, "keep reading until all sslots free.  nused", 0,
  2940.                    i);
  2941. #ifdef DEBUG
  2942.             show_sslots (k);
  2943. #endif
  2944.             return (X_OK);
  2945.          }
  2946.       }
  2947.  
  2948.       nxtpkt (k);               /* Get next packet number etc */
  2949.  
  2950.       k->filename = *(k->filelist);     /* Get next filename */
  2951.       if (k->filename)
  2952.       {                         /* If there is one */
  2953.          int i;
  2954.          for (i = 0; i < FN_MAX; i++)
  2955.          {                      /* Copy name to result struct 
  2956.                                  */
  2957.             r->filename[i] = k->filename[i];
  2958.             if (!(r->filename[i]))
  2959.                break;
  2960.          }
  2961.          (k->filelist)++;
  2962.          debug (DB_LOG, "Filename", k->filename, 0);
  2963.          if ((rc = (k->openf) (k, k->filename, 1)) != X_OK)     /* Try to open */
  2964.          {
  2965.             debug (DB_LOG, "k->openf failed rc", 0, rc);
  2966.             return (rc);
  2967.          }
  2968.  
  2969.          encstr (k->filename, k, r);    /* Encode the name for transmission */
  2970.  
  2971.          buf = get_sslot (k, &s_slot);  // get a new send slot
  2972.          k->s_pw[k->s_seq] = s_slot;
  2973.  
  2974.          debug (DB_LOG, "sending F pkt k->s_seq", 0, k->s_seq);
  2975.          debug (DB_LOG, "sending F pkt s_slot", 0, s_slot);
  2976.          k->sw_full = 0;
  2977.          if ((rc = spkt ('F', k->s_seq, -1, k->xdata, k)) != X_OK)
  2978.          {
  2979.             return (rc);        /* Send F packet */
  2980.          }
  2981.          r->sofar = 0L;
  2982.          r->sofar_rumor = 0L;
  2983.          k->state = S_FILE;     /* Wait for ACK */
  2984.          r->rstatus = S_FILE;
  2985.       }
  2986.       else
  2987.       {                         /* No more files - we're done */
  2988.  
  2989. # if 0                          // this was a hack but should not be needed
  2990.  
  2991.          int nused = nused_sslots (k);
  2992.          debug (DB_LOG, "want to send B pkt nused", 0, nused);
  2993.  
  2994.          if (nused == k->wslots)
  2995.          {
  2996.             short rm_sseq;
  2997.             short rm_sslot;
  2998.  
  2999.             rm_sseq = earliest_sseq (k);
  3000.             if (rm_sseq < 0 || rm_sseq >= k->wslots)
  3001.             {
  3002.                debug (DB_LOG, "want to send B pkt error rm_sseq", 0, rm_sseq);
  3003.                return (X_ERROR);
  3004.             }
  3005.  
  3006.             rm_sslot = k->s_pw[rm_sseq];
  3007.             if (rm_sslot < 0 || rm_sslot >= k->wslots)
  3008.             {
  3009.                debug (DB_LOG, "want to send B pkt error rm_sslot", 0,
  3010.                       rm_sslot);
  3011.                debug (DB_LOG, "  rm_sseq", 0, rm_sseq);
  3012.                return (X_ERROR);
  3013.             }
  3014.  
  3015.             free_sslot (k, rm_sslot);
  3016.          }
  3017. # endif
  3018.  
  3019.          // all sslots are supposed to be free at this point
  3020.  
  3021.          buf = get_sslot (k, &s_slot);  // get a new send slot
  3022.          if (s_slot < 0 || s_slot >= k->wslots)
  3023.          {
  3024.             debug (DB_LOG, "want to send B pkt error s_slot", 0, s_slot);
  3025.             debug (DB_LOG, "  k->s_seq", 0, k->s_seq);
  3026.             return (X_ERROR);
  3027.          }
  3028.          k->s_pw[k->s_seq] = s_slot;
  3029.  
  3030.          debug (DB_LOG, "sending B pkt k->s_seq", 0, k->s_seq);
  3031.          debug (DB_LOG, "  s_slot", 0, s_slot);
  3032.  
  3033.          if ((rc = spkt ('B', k->s_seq, 0, (UCHAR *) 0, k)) != X_OK)
  3034.          {
  3035.             return (rc);        /* Send EOT packet */
  3036.          }
  3037.          k->state = S_EOT;      /* Wait for ACK to B packet, end of transmission */
  3038.          r->rstatus = S_EOT;
  3039.       }
  3040.       k->r_seq = k->s_seq;      /* Sequence number of packet we want */
  3041.       return (X_OK);            /* Return to control program */
  3042.  
  3043.    case S_FILE:                /* Got ACK to F packet */
  3044.       nxtpkt (k);               /* Get next packet number etc */
  3045.       if (k->capas & CAP_AT)
  3046.       {                         /* A-packets negotiated? */
  3047.          if ((rc = sattr (k, r)) != X_OK)       /* Yes, send Attribute packet */
  3048.          {
  3049.             debug (DB_LOG, "SATTR failed rc", 0, rc);
  3050.             return (rc);
  3051.          }
  3052.          k->state = S_ATTR;     /* And wait for its ACK */
  3053.          r->rstatus = S_ATTR;
  3054.          did_a_pkt = 1;
  3055.       }
  3056.       else
  3057.       {
  3058.          did_a_pkt = 0;
  3059.       }
  3060.  
  3061.       if (did_a_pkt == 0)
  3062.       {
  3063.          /* No A packets - send first data */
  3064.  
  3065.          buf = get_sslot (k, &s_slot);  // get a new send slot
  3066.          k->s_pw[k->s_seq] = s_slot;
  3067.  
  3068.          debug (DB_LOG, "sending first D k->s_seq", 0, k->s_seq);
  3069.          debug (DB_LOG, "  s_slot", 0, s_slot);
  3070.  
  3071.          rc = sdata (k, r);     /* Send next data packet */
  3072.          if (rc == X_ERROR)
  3073.             return (rc);
  3074.          if (rc == 0)
  3075.          {
  3076.             /* File is empty so send EOF packet */
  3077.             buf = get_sslot (k, &s_slot);       // get a new send slot
  3078.             k->s_pw[k->s_seq] = s_slot;
  3079.             debug (DB_LOG, "empty file: sending Z pkt k->s_seq", 0, k->s_seq);
  3080.             debug (DB_LOG, "  s_slot", 0, s_slot);
  3081.             if ((rc = spkt ('Z', k->s_seq, 0, (UCHAR *) 0, k)) != X_OK)
  3082.             {
  3083.                return (rc);
  3084.             }
  3085.             k->closef (k, 0, 1);        /* Close input file */
  3086.             k->state = S_EOF;   /* Wait for ACK to EOF */
  3087.             r->rstatus = S_EOF;
  3088.          }
  3089.          else
  3090.          {                      /* Sent some data */
  3091.             k->state = S_DATA;  /* Wait for ACK to first data */
  3092.             r->rstatus = S_DATA;
  3093.          }
  3094.       }                         // if(did_a_pkt==0)
  3095.  
  3096.       k->r_seq = k->s_seq;      /* Sequence number to wait for */
  3097.       return (X_OK);
  3098.  
  3099.    case S_ATTR:                /* Got ACK to A packet */
  3100.    case S_DATA:                /* Got ACK to D packet */
  3101.       if (k->state == S_ATTR)
  3102.       {
  3103.          /*
  3104.           * CHECK ATTRIBUTE RESPONSE 
  3105.           */
  3106.          /*
  3107.           * IF REJECTED do the right thing...
  3108.           * Left as an exersise for the reader...
  3109.           */
  3110.          k->state = S_DATA;
  3111.          r->rstatus = S_DATA;
  3112.       }
  3113.  
  3114.       buf = get_sslot (k, &s_slot);     // get a new send slot
  3115.       if (s_slot < 0)
  3116.       {
  3117.          debug (DB_LOG, "window full k->state", 0, k->state);
  3118.          k->sw_full = 1;
  3119.          return (X_OK);
  3120.       }
  3121.       nxtpkt (k);               /* Get next packet number */
  3122.       k->s_pw[k->s_seq] = s_slot;
  3123.  
  3124.       k->sw_full = 0;
  3125.  
  3126.       debug (DB_LOG, "S_DATA sending k->s_seq", 0, k->s_seq);
  3127.       debug (DB_LOG, "  s_slot", 0, s_slot);
  3128.  
  3129.       rc = sdata (k, r);        /* Send first or next data packet */
  3130.  
  3131.       debug (DB_LOG, "S_DATA sdata rc", 0, rc);
  3132.       debug (DB_LOG, "  k->s_seq", 0, k->s_seq);
  3133.  
  3134.       if (rc == X_ERROR)
  3135.          return (rc);
  3136.  
  3137.       if (rc == 0)
  3138.       {                         /* If there was no data to send */
  3139.          k->closef (k, 0, 1);   /* Close input file */
  3140.  
  3141.          debug (DB_LOG, "NUSED_SSLOTS", 0, nused_sslots (k));
  3142.  
  3143.          k->state = S_EOF;      /* And wait for ACK to Z pkt */
  3144.          r->rstatus = S_EOF;
  3145.  
  3146.          // sdata() above did not send D pkt so send Z pkt in its place
  3147.          debug (DB_LOG, "EOF so sending Z pkt k->s_seq", 0, k->s_seq);
  3148.          debug (DB_LOG, "  s_slot", 0, s_slot);
  3149.  
  3150.          if ((rc = spkt ('Z', k->s_seq, 0, (UCHAR *) 0, k)) != X_OK)
  3151.             return (rc);
  3152.  
  3153.       }                         /* Otherwise stay in data state */
  3154.       k->r_seq = k->s_seq;      /* Sequence number to wait for */
  3155.       return (X_OK);
  3156.  
  3157.    case S_EOT:                 /* Get ACK to EOT packet */
  3158.       debug (DB_MSG, "S_EOT X_DONE", 0, 0);
  3159.       return (X_DONE);          /* (or X_ERROR) */
  3160.  
  3161.    case R_WAIT:                /* Waiting for the S packet */
  3162.       debug (DB_CHR, "R_WAIT rtyp", 0, rtyp);
  3163.       debug (DB_LOG, "  rseq", 0, rseq);
  3164.       debug (DB_LOG, "  what", 0, k->what);
  3165.       if (rtyp == 'S')
  3166.       {                         /* Got it */
  3167.          spar (k, pdf, datalen);        /* Set parameters from it */
  3168.  
  3169.          debug (DB_MSG, "R_WAIT rtyp==S after spar", 0, 0);
  3170.          debug (DB_CHR, "  Parity", 0, k->parity);
  3171.          debug (DB_LOG, "  Ebqflg", 0, k->ebqflg);
  3172.          debug (DB_CHR, "  Ebq", 0, k->ebq);
  3173.  
  3174.          rc = rpar (k, 'Y');    /* ACK with my parameters */
  3175.  
  3176.          debug (DB_LOG, "R_WAIT rtyp==S after rpar rc", 0, rc);
  3177.          debug (DB_LOG, "  k->capas & CAP_LP", 0, k->capas & CAP_LP);
  3178.          debug (DB_LOG, "  k->capas & CAP_SW", 0, k->capas & CAP_SW);
  3179.          debug (DB_LOG, "  k->capas & CAP_AT", 0, k->capas & CAP_AT);
  3180.          debug (DB_LOG, "  k->capas & CAP_RS", 0, k->capas & CAP_RS);
  3181.          debug (DB_LOG, "  k->capas & CAP_LS", 0, k->capas & CAP_LS);
  3182.          debug (DB_CHR, "  k->ebq           ", 0, k->ebq);
  3183.          debug (DB_LOG, "  k->ebqflg        ", 0, k->ebqflg);
  3184.          debug (DB_LOG, "  k->parity        ", 0, k->parity);
  3185.          debug (DB_LOG, "  k->s_eom         ", 0, k->s_eom);
  3186.          debug (DB_LOG, "  k->r_timo        ", 0, k->r_timo);
  3187.          debug (DB_LOG, "  k->s_timo        ", 0, k->s_timo);
  3188.          debug (DB_CHR, "  k->r_ctlq        ", 0, k->r_ctlq);
  3189.          debug (DB_CHR, "  k->s_ctlq        ", 0, k->s_ctlq);
  3190.          debug (DB_CHR, "  k->rptq          ", 0, k->rptq);
  3191.          debug (DB_LOG, "  k->rptflg        ", 0, k->rptflg);
  3192.          debug (DB_LOG, "  k->bct           ", 0, k->bct);
  3193.          debug (DB_LOG, "  k->bcta3           ", 0, k->bcta3);
  3194.          debug (DB_LOG, "  k->r_maxlen      ", 0, k->r_maxlen);
  3195.          debug (DB_LOG, "  k->s_maxlen      ", 0, k->s_maxlen);
  3196.          debug (DB_LOG, "  k->wslots        ", 0, k->wslots);
  3197.          debug (DB_LOG, "  k->binary        ", 0, k->binary);
  3198.          debug (DB_LOG, "  k->retry         ", 0, k->retry);
  3199.  
  3200.          if (rc != X_OK)
  3201.          {
  3202.             debug (DB_MSG, "R_WAIT error rpar(Y) failed", 0, 0);
  3203.             return (X_ERROR);   /* I/O error, quit. */
  3204.          }
  3205.          k->state = R_FILE;     /* All OK, switch states */
  3206.          r->rstatus = R_FILE;
  3207.          k->what = W_RECV;
  3208.       }
  3209.       else if (k->what == W_GET)
  3210.       {
  3211.          debug (DB_MSG, "  what==W_GET so resending R-packet", 0, 0);
  3212.          rc = resend (k, 0);
  3213.       }
  3214.       else
  3215.       {
  3216.          debug (DB_MSG, "  unexpected packet so send NAK for seq 0", 0, 0);
  3217.          rc = nak (k, 0, -1);
  3218.       }
  3219.       if (rc != X_OK)
  3220.          debug (DB_LOG, "R_WAIT rc not X_OK: rc", 0, rc);
  3221.       return (rc);
  3222.  
  3223.    case R_FILE:                /* Want an F or B packet, may get Z and S pkt */
  3224.       debug (DB_CHR, "R_FILE rtyp", 0, rtyp);
  3225.       if (rtyp == 'F')
  3226.       {                         /* File name */
  3227.          if ((rc = decode (k, r, 0, pdf, -1)) == X_OK)  /* Decode and save */
  3228.             k->state = R_ATTR;  /* Switch to next state */
  3229.          r->rstatus = k->state;
  3230.          debug (DB_LOG, "R_FILE decode rc", 0, rc);
  3231.          debug (DB_LOG, "R_FILE FILENAME", r->filename, 0);
  3232.          if (rc == X_OK)
  3233.          {                      /* All OK so far */
  3234.             r->filedate[0] = '\0';      /* No file date yet */
  3235.             r->filesize = 0L;   /* Or file size */
  3236.             r->sofar = 0L;      /* Or bytes transferred yet */
  3237.             r->sofar_rumor = 0L;        /* Or bytes transferred yet */
  3238.             rc = ack (k, rseq, r->filename);    /* so ACK the F packet */
  3239.          }
  3240.          else
  3241.          {
  3242.             epkt ("eksw Filename error", k);    /* Error decoding filename */
  3243.             return (rc);
  3244.          }
  3245.       }
  3246.       else if (rtyp == 'B')
  3247.       {                         /* Break, end of transaction */
  3248. //       Extra ACKs to insure they heard the ACK to their B-packet.
  3249. //       But they send extra NAKs in response.
  3250. //       ack (k, rseq, (UCHAR *) 0);
  3251. //       ack (k, rseq, (UCHAR *) 0);
  3252.          rc = (ack (k, rseq, (UCHAR *) 0) == X_OK) ? X_DONE : X_ERROR;
  3253.       }
  3254.       else if (rtyp == 'Z')
  3255.       {                         /* End of file again */
  3256.          rc = ack (k, rseq, (UCHAR *) 0);
  3257.       }
  3258.       else if (rtyp == 'S')
  3259.       {                         /* got S pkt again */
  3260.          spar (k, pdf, datalen);        /* Set parameters from it */
  3261.  
  3262.          debug (DB_MSG, "R_FILE rtyp==S again", 0, 0);
  3263.          debug (DB_CHR, "  Parity", 0, k->parity);
  3264.          debug (DB_LOG, "  Ebqflg", 0, k->ebqflg);
  3265.          debug (DB_CHR, "  Ebq", 0, k->ebq);
  3266.  
  3267.          rc = rpar (k, 'Y');    /* ACK with my parameters */
  3268.  
  3269.          debug (DB_LOG, "R_FILE after rpar rc", 0, rc);
  3270.          if (rc != X_OK)
  3271.          {
  3272.             debug (DB_MSG, "R_FILE error rpar(Y) failed", 0, 0);
  3273.             return (X_ERROR);   /* I/O error, quit. */
  3274.          }
  3275.          k->state = R_FILE;     /* All OK, switch states */
  3276.          r->rstatus = R_FILE;
  3277.          k->what = W_RECV;
  3278.       }
  3279.       else
  3280.       {
  3281.          rc = X_ERROR;
  3282.       }
  3283.       debug (DB_LOG, "rc", 0, rc);
  3284.       return (rc);
  3285.  
  3286.    case R_ATTR:                /* Want A, D, or Z packet */
  3287.       debug (DB_CHR, "R_ATTR rtyp", 0, rtyp);
  3288.       if (rtyp == 'A')
  3289.       {                         /* Attribute packet */
  3290.          int x;
  3291.          x = gattr (k, pdf, r); /* Read the attributes */
  3292.          if (x > -1)
  3293.             k->binary = x;
  3294.          ack (k, rseq, (UCHAR *) "Y");  /* Always accept the file */
  3295.          return (X_OK);
  3296.       }
  3297.       else if (rtyp == 'D')
  3298.       {                         /* First data packet */
  3299.          k->obufpos = 0;        /* Initialize output buffer */
  3300.          k->filename = r->filename;
  3301.          r->sofar = 0L;
  3302.          r->sofar_rumor = 0L;
  3303.          if ((rc = (*(k->openf)) (k, r->filename, 2)) == X_OK)
  3304.          {
  3305.             k->state = R_DATA;  /* Switch to Data state */
  3306.             r->rstatus = k->state;
  3307.             rc = handle_good_rpkt (k, r, rseq, pdf);
  3308.          }
  3309.          else
  3310.          {
  3311.             debug (DB_MSG, "eksw cannot open output file", 0, 0);
  3312.             epkt ("eksw cannot open output file", k);
  3313.             return (rc);
  3314.          }
  3315.  
  3316.          if (rc == X_OK)
  3317.          {
  3318.          }
  3319.          else
  3320.          {
  3321.             debug (DB_MSG, "eksw Error writing data to file", 0, 0);
  3322.             epkt ("eksw Error writing data to file", k);
  3323.          }
  3324.          return (rc);
  3325.       }
  3326.       else if (rtyp == 'Z')
  3327.       {                         /* Empty file */
  3328.          debug (DB_LOG, "R_ATTR empty file", r->filename, 0);
  3329.          k->obufpos = 0;        /* Initialize output buffer */
  3330.          k->filename = r->filename;
  3331.          r->sofar = 0L;         /* Open and close the file */
  3332.          r->sofar_rumor = 0L;   /* Open and close the file */
  3333.          if ((rc = (*(k->openf)) (k, r->filename, 2)) == X_OK)
  3334.          {
  3335.             if (((rc = (*(k->closef)) (k, *pdf, 2)) == X_OK))
  3336.             {
  3337.                k->state = R_FILE;
  3338.                rc = ack (k, rseq, s);
  3339.             }
  3340.             else
  3341.             {
  3342.                debug (DB_MSG, "eksw Error closing empty file", 0, 0);
  3343.                epkt ("eksw Error closing empty file", k);
  3344.                return (rc);
  3345.             }
  3346.          }
  3347.          else
  3348.          {
  3349.             debug (DB_LOG, "eksw File refused or cannot be opened rc", 0, rc);
  3350.             epkt ("eksw File refused or cannot be opened", k);
  3351.             return (rc);
  3352.          }
  3353.          r->rstatus = k->state;
  3354.          return (X_OK);
  3355.       }
  3356.       else if (rtyp == 'F')     // received F pkt again
  3357.       {
  3358.          ack (k, rseq, (UCHAR *) 0);
  3359.          return (X_OK);
  3360.       }
  3361.       else
  3362.       {
  3363.          debug (DB_CHR, "R_ATTR error 3 unexpected packet rtyp", 0, rtyp);
  3364.          epkt ("eksw R_ATTR error 3 unexpected packet type", k);
  3365. //       exit (1);
  3366.          return (X_ERROR);
  3367.       }
  3368. //    break;
  3369.  
  3370.    case R_DATA:                /* Want a D or Z packet */
  3371.       debug (DB_CHR, "R_DATA rtyp", 0, rtyp);
  3372.       debug (DB_LOG, "R_DATA rseq", 0, rseq);
  3373.       if (rtyp == 'D')
  3374.       {                         /* Data */
  3375.          rc = handle_good_rpkt (k, r, rseq, pdf);
  3376.          debug (DB_LOG, "R_DATA hgr rc", 0, rc);
  3377.       }
  3378.       else if (rtyp == 'Z')
  3379.       {                         /* End of file */
  3380.          debug (DB_LOG, "R_DATA Z pkt obufpos", 0, k->obufpos);
  3381.          flush_to_file (k, r);
  3382.          if (((rc = (*(k->closef)) (k, *pdf, 2)) == X_OK) && (rc == X_OK))
  3383.             k->state = R_FILE;
  3384.          debug (DB_LOG, "R_DATA closef rc", 0, rc);
  3385.          r->rstatus = k->state;
  3386.       }
  3387.       else
  3388.       {
  3389.          debug (DB_CHR, "R_DATA error 4 unexpected packet rtyp", 0, rtyp);
  3390.          epkt ("eksw error 4 unexpected packet type", k);
  3391. //       exit (1);
  3392.          return (X_ERROR);
  3393.       }
  3394.  
  3395.       if (rc == X_OK)
  3396.       {
  3397.          if (rtyp == 'Z')
  3398.          {
  3399.             rc = ack (k, rseq, s);      // usual ACK to D or Z packet
  3400.          }
  3401.       }
  3402.       else
  3403.       {
  3404.          debug (DB_CHR, "Error 12: rc != X_OK rtyp", 0, rtyp);
  3405.          epkt (rtyp == 'Z' ? "eksw Can't close file" : "eksw hgr failed", k);
  3406.       }
  3407.  
  3408.       return (rc);
  3409.  
  3410.    case R_ERROR:               /* Canceled from above */
  3411.       debug (DB_LOG, "R_ERROR error so sending E pkt k->state", 0, k->state);
  3412.       epkt (msg, k);
  3413.       return (X_ERROR);
  3414.  
  3415.    default:
  3416.       debug (DB_LOG, "default error so sending E pkt k->state", 0, k->state);
  3417.       epkt (msg, k);
  3418.       return (X_ERROR);
  3419.    }
  3420.  
  3421.    // not supposed to get here
  3422. // debug (DB_LOG, "Kermit logic error k->state", 0, k->state);
  3423. // exit (1);
  3424. // return (X_ERROR);            /* make compiler happy */
  3425. }
  3426.