home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku200.zip / ckcfn2.c < prev    next >
C/C++ Source or Header  |  2001-12-02  |  101KB  |  3,305 lines

  1. /*  C K C F N 2  --  System-independent Kermit protocol support functions... */
  2.  
  3. /*  ...Part 2 (continued from ckcfns.c)  */
  4.  
  5. /*
  6.   Author: Frank da Cruz <fdc@columbia.edu>,
  7.   Columbia University Academic Information Systems, New York City.
  8.  
  9.   Copyright (C) 1985, 2001,
  10.     Trustees of Columbia University in the City of New York.
  11.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  12.     copyright text in the ckcmai.c module for disclaimer and permissions.
  13. */
  14. /*
  15.  Note -- if you change this file, please amend the version number and date at
  16.  the top of ckcfns.c accordingly.
  17. */
  18.  
  19. #include "ckcsym.h"            /* Compilation options */
  20. #include "ckcdeb.h"            /* Debugging and other symbols */
  21. #include "ckcasc.h"            /* ASCII symbols */
  22. #include "ckcker.h"            /* Kermit symbols */
  23. #include "ckcxla.h"            /* Translation */
  24. #include "ckcnet.h"            /* IKS and VMS #define TCPSOCKET */
  25. #ifdef TCPSOCKET            /* For TELNET business in spack() */
  26. extern int tn_nlm, ttnproto, tn_b_nlm;
  27. #endif /* TCPSOCKET */
  28.  
  29. extern int parity, network, local, interrupted, fatalio, wasclosed;
  30.  
  31. int kstartactive = 0;            /* Flag for kstart() in a packet */
  32.  
  33. static CHAR p_tbl[] = {            /* Even parity table for dopar(). */
  34.     (CHAR) '\000',            /* ANSI C casts '\ooo' constants  */
  35.     (CHAR) '\201',            /* to signed char, so we have to  */
  36.     (CHAR) '\202',            /* cast back to unsigned char...  */
  37.     (CHAR) '\003',
  38.     (CHAR) '\204',
  39.     (CHAR) '\005',
  40.     (CHAR) '\006',
  41.     (CHAR) '\207',
  42.     (CHAR) '\210',
  43.     (CHAR) '\011',
  44.     (CHAR) '\012',
  45.     (CHAR) '\213',
  46.     (CHAR) '\014',
  47.     (CHAR) '\215',
  48.     (CHAR) '\216',
  49.     (CHAR) '\017',
  50.     (CHAR) '\220',
  51.     (CHAR) '\021',
  52.     (CHAR) '\022',
  53.     (CHAR) '\223',
  54.     (CHAR) '\024',
  55.     (CHAR) '\225',
  56.     (CHAR) '\226',
  57.     (CHAR) '\027',
  58.     (CHAR) '\030',
  59.     (CHAR) '\231',
  60.     (CHAR) '\232',
  61.     (CHAR) '\033',
  62.     (CHAR) '\234',
  63.     (CHAR) '\035',
  64.     (CHAR) '\036',
  65.     (CHAR) '\237',
  66.     (CHAR) '\240',
  67.     (CHAR) '\041',
  68.     (CHAR) '\042',
  69.     (CHAR) '\243',
  70.     (CHAR) '\044',
  71.     (CHAR) '\245',
  72.     (CHAR) '\246',
  73.     (CHAR) '\047',
  74.     (CHAR) '\050',
  75.     (CHAR) '\251',
  76.     (CHAR) '\252',
  77.     (CHAR) '\053',
  78.     (CHAR) '\254',
  79.     (CHAR) '\055',
  80.     (CHAR) '\056',
  81.     (CHAR) '\257',
  82.     (CHAR) '\060',
  83.     (CHAR) '\261',
  84.     (CHAR) '\262',
  85.     (CHAR) '\063',
  86.     (CHAR) '\264',
  87.     (CHAR) '\065',
  88.     (CHAR) '\066',
  89.     (CHAR) '\267',
  90.     (CHAR) '\270',
  91.     (CHAR) '\071',
  92.     (CHAR) '\072',
  93.     (CHAR) '\273',
  94.     (CHAR) '\074',
  95.     (CHAR) '\275',
  96.     (CHAR) '\276',
  97.     (CHAR) '\077',
  98.     (CHAR) '\300',
  99.     (CHAR) '\101',
  100.     (CHAR) '\102',
  101.     (CHAR) '\303',
  102.     (CHAR) '\104',
  103.     (CHAR) '\305',
  104.     (CHAR) '\306',
  105.     (CHAR) '\107',
  106.     (CHAR) '\110',
  107.     (CHAR) '\311',
  108.     (CHAR) '\312',
  109.     (CHAR) '\113',
  110.     (CHAR) '\314',
  111.     (CHAR) '\115',
  112.     (CHAR) '\116',
  113.     (CHAR) '\317',
  114.     (CHAR) '\120',
  115.     (CHAR) '\321',
  116.     (CHAR) '\322',
  117.     (CHAR) '\123',
  118.     (CHAR) '\324',
  119.     (CHAR) '\125',
  120.     (CHAR) '\126',
  121.     (CHAR) '\327',
  122.     (CHAR) '\330',
  123.     (CHAR) '\131',
  124.     (CHAR) '\132',
  125.     (CHAR) '\333',
  126.     (CHAR) '\134',
  127.     (CHAR) '\335',
  128.     (CHAR) '\336',
  129.     (CHAR) '\137',
  130.     (CHAR) '\140',
  131.     (CHAR) '\341',
  132.     (CHAR) '\342',
  133.     (CHAR) '\143',
  134.     (CHAR) '\344',
  135.     (CHAR) '\145',
  136.     (CHAR) '\146',
  137.     (CHAR) '\347',
  138.     (CHAR) '\350',
  139.     (CHAR) '\151',
  140.     (CHAR) '\152',
  141.     (CHAR) '\353',
  142.     (CHAR) '\154',
  143.     (CHAR) '\355',
  144.     (CHAR) '\356',
  145.     (CHAR) '\157',
  146.     (CHAR) '\360',
  147.     (CHAR) '\161',
  148.     (CHAR) '\162',
  149.     (CHAR) '\363',
  150.     (CHAR) '\164',
  151.     (CHAR) '\365',
  152.     (CHAR) '\366',
  153.     (CHAR) '\167',
  154.     (CHAR) '\170',
  155.     (CHAR) '\371',
  156.     (CHAR) '\372',
  157.     (CHAR) '\173',
  158.     (CHAR) '\374',
  159.     (CHAR) '\175',
  160.     (CHAR) '\176',
  161.     (CHAR) '\377'
  162. };
  163.  
  164. /*  D O P A R  --  Add an appropriate parity bit to a character  */
  165.  
  166. CHAR
  167. #ifdef CK_ANSIC
  168. dopar(register CHAR ch)
  169. #else
  170. dopar(ch) register CHAR ch;
  171. #endif /* CK_ANSIC */
  172.     {
  173.     register unsigned int a;
  174.     if (!parity
  175. #ifdef TCPSOCKET
  176.         || (network && (ttnproto == NP_TELNET) && (TELOPT_ME(TELOPT_BINARY)))
  177. #ifndef NOXFER
  178.     || (!local && sstelnet)        /* TELNET BINARY MODE */
  179. #endif /* NOXFER */
  180. #endif /* TCPSOCKET */
  181.         ) return((CHAR) (ch & 255)); else a = ch & 127;
  182.     switch (parity) {
  183.     case 'e':  return(p_tbl[a]);                 /* Even */
  184.     case 'm':  return((CHAR) (a | 128));         /* Mark */
  185.     case 'o':  return((CHAR) (p_tbl[a] ^ 128));  /* Odd */
  186.     case 's':  return((CHAR) a);                 /* Space */
  187.     default:   return((CHAR) a);                 /* Something illegal */
  188.     }
  189. }
  190.  
  191. #ifndef NOXFER                /* Rest of this file... */
  192.  
  193. #define NEWDPL                /* New dynamic packet length method */
  194.  
  195. #ifdef VMS
  196. extern int batch;
  197. #else
  198. extern int backgrd;
  199. #endif /* VMS */
  200.  
  201. #ifdef DYNAMIC
  202. extern struct pktinfo *s_pkt;        /* array of pktinfo structures */
  203. extern struct pktinfo *r_pkt;        /* array of pktinfo structures */
  204. #else
  205. extern struct pktinfo s_pkt[];        /* array of pktinfo structures */
  206. extern struct pktinfo r_pkt[];        /* array of pktinfo structures */
  207. #endif /* DYNAMIC */
  208.  
  209. extern int sseqtbl[], rseqtbl[], sbufuse[], sacktbl[], wslots, winlo, wslotn,
  210.   sbufnum, rbufnum, pktpaus, reliable;
  211.  
  212. #ifdef STREAMING
  213. static int dontsend = 0;
  214. extern int streaming;
  215. #endif /* STREAMING */
  216.  
  217. extern int ttprty;            /* from ck*tio.c */
  218. extern int autopar;
  219.  
  220. extern int spsiz, spmax, rpsiz, timint, timef, npad, bestlen, maxsend;
  221. extern int rpt, rptq, rptflg, capas, spsizf, en_fin, tsecs, flow;
  222. extern int pktnum, sndtyp, rcvtyp, bctr, bctu, bctl, rsn, rln, maxtry, size;
  223. extern int osize, maxsize, spktl, rpktl, nfils, stdouf, fsecs;
  224. extern int turn, turnch, displa, pktlog, seslog, xflg, mypadn;
  225. extern int hcflg, server, cxseen, czseen, discard, slostart;
  226. extern int nakstate, quiet, success, xitsta, what, filestatus;
  227. extern int spackets, rpackets, timeouts, retrans, crunched, urpsiz;
  228. extern int carrier, fdispla, srvidl;
  229.  
  230. #ifdef GFTIMER
  231. extern CKFLOAT fptsecs, fpfsecs, fpxfsecs;
  232. #endif /* GFTIMER */
  233.  
  234. extern long filcnt, filrej, ffc, flci, flco, tlci, tlco, tfc, speed;
  235. extern long filcps, tfcps;
  236.  
  237. extern char *cmarg, filnam[];
  238.  
  239. extern CHAR padch, mypadc, eol, seol, ctlq, sstate;
  240. extern CHAR *recpkt, *data, myinit[];
  241. extern CHAR *srvptr, stchr, mystch, *rdatap;
  242. extern CHAR padbuf[];
  243. extern CHAR * epktmsg;
  244. extern int epktrcvd, epktsent;
  245.  
  246. #ifdef OS2                /* AUTODOWNLOAD parameters */
  247. extern int adl_kmode, adl_zmode;    /* Match Packet to signal download */
  248. extern char * adl_kstr;            /* KERMIT Download String */
  249. extern char * adl_zstr;            /* ZMODEM Download String */
  250. #endif /* OS2 */
  251.  
  252. #ifdef CK_AUTODL
  253. CHAR ksbuf[96] = { NUL, NUL };        /* Autodownload "Kermit Start" buf */
  254. #endif /* CK_AUTODL */
  255.  
  256. int numerrs = 0;            /* Number of packet errors so far */
  257. int rcvtimo = 0;            /* Timeout for receiving a packet */
  258. int idletmo = 0;            /* Flag for idle timeout */
  259.  
  260. long filcps = 0L;            /* CPS most recent file transferred */
  261. long tfcps  = 0L;            /* CPS most recent transaction */
  262. long xfsecs = 0L;            /* Elapsed time for most recent file */
  263. #ifdef GFTIMER
  264. CKFLOAT fpxfsecs = 0.0;            /* Ditto, but floating point */
  265. #endif /* GFTIMER */
  266.  
  267. #ifdef CK_TIMERS
  268. int rrttbl[64], srttbl[64];        /* Packet timestamp tables */
  269. extern int rttflg;
  270. #define RTT_SCALE 1000
  271. long
  272.   rttsamples,                /* Round trip time samples */
  273.   rttdelay,                /* RTT delay */
  274.   pktintvl,                /* Interpacket arrival time */
  275.   rttvariance,                /* RTT variance */
  276.   rttstddev;                /* RTT standard deviation */
  277. #endif /* CK_TIMERS */
  278.  
  279. /* CRC generation tables */
  280.  
  281. long crcta[16] = { 0L, 010201L, 020402L, 030603L, 041004L,
  282.   051205L, 061406L, 071607L, 0102010L, 0112211L, 0122412L, 0132613L, 0143014L,
  283.   0153215L, 0163416L, 0173617L
  284. };
  285.  
  286. long crctb[16] = { 0L, 010611L, 021422L, 031233L, 043044L,
  287.   053655L, 062466L, 072277L, 0106110L, 0116701L, 0127532L, 0137323L, 0145154L,
  288.   0155745L, 0164576L, 0174367L
  289. };
  290.  
  291. #ifdef CK_TIMERS
  292. /*
  293.   Round-trip timer calculations adapted from Tim Kientzle's article,
  294.   "Improving Kermit Performance", Dr Dobb's Journal, February 1996.
  295. */
  296.  
  297.  
  298. /*  R T T I N I T  --  Initialize timers at start of transaction  */
  299.  
  300. VOID
  301. rttinit() {                /* Initialize round-trip timing */
  302.     int i;
  303.  
  304.     if (timint == 0)
  305.       return;
  306.  
  307.     rttsamples  = 0L;            /* Samples (packets) */
  308.     rttvariance = 0L;            /* Variance in delay */
  309.     rttdelay    = (long) timint * RTT_SCALE; /* Delay */
  310.     pktintvl    = (long) timint * RTT_SCALE; /* Delay */
  311.     rttstddev   = (long) timint * RTT_SCALE; /* Standard deviation of delay */
  312.  
  313.     /* Tables of timestamps indexed by packet sequence number */
  314.  
  315.     for (i = 0; i < 64; i++) {
  316.     rrttbl[i] = -1;            /* Time each packet was received */
  317.     srttbl[i] = -1;            /* Time each packet was sent */
  318.     }
  319.     rcvtimo = timint;            /* Initial timeout is what user said */
  320. }
  321.  
  322. /*  G E T R T T  --  Get packet round trip time  */
  323. /*
  324.   Call with nakstate == 0 if file sender, nonzero if receiver,
  325.   and n == packet sequence number of the packet we just received.
  326.  
  327.   Returns:
  328.   -1 on failure with rcvtimo set to timint (what the user said), or:
  329.    0 on success with rcvtimo set to dynamically calculated value:
  330.      1 <= rcvtimo <= timint * 3.
  331. */
  332. int
  333. getrtt(nakstate, n) int nakstate, n; {
  334.     extern int mintime, maxtime;
  335.     static int prevz = 0, prevr = 0;
  336.     int x, y, yy, z = 0, zz = 0;    /* How long did it take to get here? */
  337.  
  338.     rcvtimo = timint;            /* Default timeout is what user said */
  339.  
  340.     if (timint == 0)            /* We're not timing out. */
  341.       return(0);
  342.  
  343.     if (!rttflg)            /* Not supposed to be doing this? */
  344.       return(-1);            /*  So don't */
  345.  
  346.     if (!RTT_SCALE)            /* Paranoia... */
  347.       return(-1);
  348.  
  349.     /* rtimer() (reset timer) is not called until 1st data packet */
  350. #ifdef GFTIMER
  351.     /* rftimer(); */
  352. #endif /* GFTIMER */
  353.     /* S (F [ A ] D* Z)* B */
  354.  
  355.     /* NOTE: we calculate both the round-trip time AND the packet */
  356.     /* arrival rate.  We don't use the RTT for anything, we just display it. */
  357.     /* Timeouts are based on the packet arrival rate. */
  358.  
  359.     if (spackets > 3) {            /* Don't start till 4th packet */
  360.     if (nakstate) {            /* File receiver */
  361.         x = rrttbl[n];             /* Time when I got packet n */
  362.         y = rrttbl[n > 0 ? n - 1 : 63];  /* Time when I got packet n-1 */
  363.         yy = srttbl[n > 0 ? n - 1 : 63]; /* Time when I sent ACK(n-1) */
  364.         if (x > -1 && y > -1) {    /* Be careful */
  365.         z = x - y;        /* Packet rate */
  366.         zz = x - yy;        /* Round trip time */
  367.         z++;            /* So sender & receiver differ */
  368.         debug(F101,"RTT RECV","",z);
  369.         } else {            /* This shouldn't happen */
  370.         debug(F101,"RTT RECV ERROR spackets","",spackets);
  371.         debug(F101,"RTT RECV ERROR sequence","",n);
  372.         return(-1);
  373.         }
  374.     } else {            /* File sender */
  375.         x = rrttbl[n];        /* Time when I got ACK(n) */
  376.         y = rrttbl[n > 0 ? n - 1 : 63]; /* Time when I got packet n-1 */
  377.         yy = srttbl[n];        /* Time when I sent n */
  378.         if (x > -1 && y > -1) {
  379.         z = x - y;        /* Packet rate */
  380.         zz = x - yy;        /* Round trip time */
  381.         debug(F101,"RTT SEND","",z);
  382.         } else {
  383.         debug(F100,"RTT SEND ERROR","",0);
  384.         return(-1);
  385.         }
  386.     }
  387.     if (z < 1)            /* For fast connections */
  388.       z = RTT_SCALE / 2;        /* Convert to scale... */
  389.     else
  390.       z *= RTT_SCALE;
  391.     debug(F101,"RTT z scaled","",z);
  392.  
  393.     if (zz < 1)            /* For fast connections */
  394.       zz = RTT_SCALE / 2;        /* Convert to scale... */
  395.     else
  396.       zz *= RTT_SCALE;
  397.  
  398.     rttdelay = zz;            /* Round trip time of this packet */
  399. #ifdef COMMENT
  400. /*
  401.   This was used in C-Kermit 7.0 (and 6.0?) but not only is it overkill,
  402.   it also can produce ridiculously long timeouts under certain conditions.
  403.   Replaced in 8.0 by a far simpler and more aggressive strategy.
  404. */
  405.     if (rttsamples++ == 0L) {    /* First sample */
  406.         pktintvl = z;
  407.     } else {            /* Subsequent samples */
  408.         long oldavg = pktintvl;
  409.         long rttdiffsq;
  410.  
  411.         if (rttsamples > 30)    /* Use real average for first 30 */
  412.           rttsamples = 30;        /*  then decaying average. */
  413.  
  414.         /* Average delay, difference squared, variance, std deviation */
  415.  
  416.         pktintvl += (z - pktintvl) / rttsamples;
  417.         rttdiffsq = (z - oldavg) * (z - oldavg);
  418.         rttvariance += (rttdiffsq - rttvariance) / rttsamples;
  419.         debug(F101,"RTT stddev1","",rttstddev);
  420.         if (rttstddev < 1L)        /* It can be zero, in which case */
  421.           rttstddev = RTT_SCALE / 3; /* set it to something small... */
  422.         rttstddev = (rttstddev + rttvariance / rttstddev) / 2;
  423.     }
  424.     debug(F101,"RTT stddev2","",rttstddev);
  425.     debug(F101,"RTT delay  ","",pktintvl);
  426.     rcvtimo = (pktintvl + (3L * rttstddev)) / RTT_SCALE + 1;
  427.     if (rpackets < 32)        /* Allow for slow start */
  428.       rcvtimo += rcvtimo + 2;
  429.     else if (rpackets < 64)
  430.       rcvtimo += rcvtimo / 2 + 1;
  431.         /* On a reliable link, don't try too hard to time out. */
  432.     /* Especially on fast local network connections. */
  433.         if (server && what == W_NOTHING) /* Server command wait */
  434.       rcvtimo = rcvtimo;        /* == srvtim */
  435.         else if (reliable == SET_ON && rcvtimo > 0) /* Reliable */
  436.       rcvtimo = rcvtimo +15;    /* and not server command wait */
  437.         else                            /* Not reliable or server cmd wait */
  438.       rcvtimo = rcvtimo;
  439.     if (rcvtimo < mintime)        /* Lower bound */
  440.       rcvtimo = mintime;
  441.     if (maxtime > 0) {        /* User specified an upper bound */
  442.         if (rcvtimo > maxtime)
  443.           rcvtimo = maxtime;
  444.     } else if (maxtime == 0) {    /* User didn't specify */
  445.         if (rcvtimo > timint * 6)
  446.           rcvtimo = timint * 6;
  447.     }
  448. #else  /* COMMENT */
  449. #ifdef CKFLOAT
  450.     {
  451.         CKFLOAT x;
  452.         x = (CKFLOAT)(prevz + z + z) / 3.0;
  453.         rcvtimo = (int)((((CKFLOAT)x * 2.66) / RTT_SCALE) + 0.5);
  454.         debug(F101,"RTT rcvtimo (float)","",rcvtimo);
  455.     }
  456. #else
  457.     rcvtimo = (prevz + z + z) / RTT_SCALE;
  458.     debug(F101,"RTT rcvtimo (int)","",rcvtimo);
  459. #endif /* CKFLOAT */
  460. #endif /* COMMENT */
  461.  
  462.     zz = (rttdelay + 500) / 1000;
  463.     if (rcvtimo > (zz * 3))
  464.       rcvtimo = zz * 3;
  465.  
  466.     if (rcvtimo < 1)
  467.       rcvtimo = 1;
  468.     if (mintime > 0) {
  469.         if (rcvtimo < mintime)    /* Lower bound */
  470.           rcvtimo = mintime;
  471.     }
  472.     if (maxtime > 0) {        /* Upper bound */
  473.         if (rcvtimo > maxtime)
  474.           rcvtimo = maxtime;
  475.     }
  476.     if (rcvtimo == (prevr - 1))
  477.       rcvtimo++;
  478.  
  479.     debug(F101,"RTT final rcvtimo","",rcvtimo);
  480.     }
  481.     prevz = z;
  482.     prevr = rcvtimo;
  483.     return(0);
  484. }
  485. #endif /* CK_TIMERS */
  486.  
  487. /*  I N P U T  --  Attempt to read packet number 'pktnum'.  */
  488.  
  489. /*
  490.  This is the function that feeds input to Kermit's finite state machine,
  491.  in the form of a character in the range 32-126, normally a packet type
  492.  (uppercase letter) or pseudo-packet-type (lowercase letter).
  493.  
  494.  If a special start state is in effect, that state is returned as if it were
  495.  the type of an incoming packet.
  496. */
  497. int
  498. input() {
  499.     int type = 0, acktype;        /* Received packet type */
  500.     int x, y, k;            /* Workers */
  501.     int z, pi, nf;            /* Worker, packet index, NAK flag */
  502.     int nak2ack = 0;
  503.  
  504.     debug(F000,"input sstate","",sstate);
  505.     debug(F101,"input nakstate","",nakstate);
  506.     debug(F000,"input sndtyp","",sndtyp);
  507.     debug(F101,"input xitsta","",xitsta);
  508.     debug(F101,"input what","",what);
  509.  
  510.     while (1) {                /* Big loop... */
  511. /*
  512.   It is ttchk()'s responsibility to tell us if the connection is broken,
  513.   and to do so instantly and nondestructively -- no blocking, etc, that would
  514.   slow down file transfer.
  515. */
  516.     if (ttchk() < 0) {
  517.         debug(F100,"input CONNECTION BROKEN","",0);
  518.         fatalio = 1;
  519.         return('q');
  520.     }
  521.     if (sstate != 0) {        /* If a start state is in effect, */
  522.         type = sstate;        /* return it like a packet type, */
  523.         sstate = 0;            /* and then nullify it. */
  524.         numerrs = 0;        /* (PWP) no errors so far */
  525.         return(type);
  526.     }
  527.     if (nakstate) {            /* This section for file receiver. */
  528.         if (wslots > 1) {        /* If we're doing windows, */
  529.         x = rseqtbl[winlo];    /* see if desired packet already in. */
  530.         debug(F101,"input winlo","",winlo);
  531.         debug(F101,"input rseqtbl[winlo]","",rseqtbl[winlo]);
  532.         if (x > -1) {        /* Already there? */
  533.             if (r_pkt[x].pk_seq == winlo) { /* (double check) */
  534.             rsn = winlo;                /* Yes, return its info */
  535.             debug(F101,"input return pre-stashed packet","",rsn);
  536.             dumprbuf();
  537.             rdatap = r_pkt[x].pk_adr;   /* like rpack would do. */
  538.             rln = (int)strlen((char *) rdatap);
  539.             type = r_pkt[x].pk_typ;
  540.             break;
  541.             }
  542.         }
  543.         }
  544.         type = rpack();            /* Try to read a packet. */
  545.         debug(F101,"input rpack","",type);
  546.  
  547.         while (type == 'e') {    /* Handle echoes */
  548.         debug(F101,"input echo discarded","",type);
  549.         type = rpack();
  550.         }
  551. #ifdef DEBUG
  552.         if (deblog) {
  553.         if (type == 'D')
  554.           debug(F011,"input type D=",(char *)rdatap,39);
  555.         else
  556.           debug(F000,"input type",(char *)rdatap,type);
  557.         }
  558. #endif /* DEBUG */
  559. #ifndef OLDCHKINT
  560.         if (type == 'z') {
  561.         epktrcvd = 1;
  562.         errpkt((CHAR *)"User cancelled.");
  563.         type = 'E';
  564.         break;
  565.         }
  566. #endif /* OLDCHKINT */
  567.         if (type < -1) {
  568.         char * s;
  569.         s = (type == -2) ?
  570.           "FAILED - Interrupted" :
  571.             "FAILED - Connection lost";
  572.  
  573.         xxscreen(SCR_PT,'q',0L,s);
  574. #ifdef CKLOGDIAL
  575.         dologend();
  576. #endif /* CKLOGDIAL */
  577.         return('q');        /* Ctrl-C or connection lost */
  578.         }
  579.         if (type < 0) {        /* Receive window full */
  580.         /* Another thing to do here would be to delete */
  581.         /* the highest packet and NAK winlo.  But that */
  582.         /* shouldn't be necessary since the other Kermit */
  583.         /* should not have sent a packet outside the window. */
  584. #ifdef COMMENT
  585.                 char foo[256];
  586.                 ckmakxmsg(foo,256,"Receive window full (rpack): wslots=",
  587.                           ckitoa(wslots)," winlo=",ckitoa(winlo)," pktnum=",
  588.                           ckitoa(pktnum), NULL,NULL,NULL,NULL,NULL,NULL);
  589.         errpkt((CHAR *)foo);
  590.                 debug(F100,foo,"",0);
  591. #else
  592.         errpkt((CHAR *)"Receive window full");
  593.         debug(F101,"rpack receive window full","",0);
  594.                 debug(F101," wslots","",wslots);
  595.                 debug(F101," winlo","",winlo);
  596.         debug(F101," pktnum","",pktnum);
  597. #endif
  598.         dumprbuf();
  599.         type = 'E';
  600.         break;
  601.         }
  602.         dumprbuf();
  603.  
  604. #ifdef OLDCHKINT
  605.         if (chkint() < 0) {        /* Check for console interrupts. */
  606.         errpkt((CHAR *)"User cancelled."); /* (old way) */
  607.         type = 'E';
  608.         break;
  609.         }
  610. #endif /* OLDCHKINT */
  611.  
  612. #ifdef STREAMING
  613.         if (streaming) {        /* Streaming */
  614.         if (type == 'Q' || type == 'T') { /* Errors are fatal. */
  615.             crunched++;        /* For statistics */
  616.             errpkt((CHAR *)"Transmission error on reliable link.");
  617.             type = 'E';
  618.         }
  619.         }
  620. #endif /* STREAMING */
  621.         if (type == 'E') {
  622.         debug(F101,"input got E, nakstate","",nakstate);
  623.         break;            /* Error packet */
  624.         }
  625.         if (type == 'Q') {        /* Crunched packet. */
  626.         crunched++;
  627.         numerrs++;
  628. /*
  629.   Packet arrived damaged.  It was most likely the packet we were expecting
  630.   next, so we send a NAK for that packet.  Prior to 5A(189), we always
  631.   NAK'd winlo here, but that was bad because if two (or more) different
  632.   packets were damaged, we would keep NAKing the first one and never NAK the
  633.   other ones, which could result in a lengthy series of timeouts.  Now we
  634.   NAK the oldest as-yet-unNAK'd missing packet.
  635. */
  636. #ifdef CK_TIMERS
  637.         rcvtimo++;        /* Stretch the timeout a little */
  638. #endif /* CK_TIMERS */
  639.         z = (winlo + wslots) % 64;  /* Search from winlo to z */
  640.         debug(F101,"ZZZ crunched z","",z);
  641.         nf = 0;                /* NAK flag not set yet */
  642.         for (x = winlo; x != z; x = (x + 1) % 64) {
  643.             debug(F101,"ZZZ x","",x);
  644.             if (rseqtbl[x] > -1) /* Have I received packet x? */
  645.               continue;         /* Yes, go on. */
  646.             debug(F101,"ZZZ x not recd yet","",x);
  647.             pi = sseqtbl[x];     /* No, have I NAK'd it yet? */
  648.             if (pi < 0 || s_pkt[pi].pk_rtr == 0) {
  649.             debug(F101,"ZZZ x not NAK'd yet","",x);
  650.             nack(x);     /* No, NAK it now. */
  651.             nf = 1;         /* Flag that I did. */
  652.             break;
  653.             }
  654.         }
  655.         if (!nf) {        /* If we didn't NAK anything above, */
  656.             debug(F101,"ZZZ NAKing winlo","",winlo);
  657.             if (nack(winlo) < 0) { /* we have to NAK winlo (again) */
  658.             errpkt((CHAR *)"Too many retries."); /* Too many */
  659.             type = 'E';
  660.             break;
  661.             }
  662.         }
  663.         continue;
  664.         }
  665.  
  666.         if (type == 'T') {        /* Timeout */
  667. #ifndef OS2
  668.         /* K95 does this its own way */
  669.         if (server && srvidl) {
  670.             idletmo = 1;
  671.             debug(F101,"SERVER IDLE TIMEOUT","",srvidl);
  672.             return('q');
  673.         }
  674. #endif /* OS2 */
  675. #ifdef CK_TIMERS
  676.         rcvtimo++;        /* Stretch the timeout a little */
  677. #endif /* CK_TIMERS */
  678.         timeouts++;
  679.         debug(F101,"input receive-state timeout, winlo","",winlo);
  680.         /* NAK only the packet at window-low */
  681.         debug(F101,"input sending NAK for winlo","",winlo);
  682.         x = ttchk();
  683.         if (x > 0)        /* Don't give up if there is still */
  684.           continue;        /* something to read. */
  685.         else if (x < 0) {
  686. #ifdef CKLOGDIAL
  687.             dologend();
  688. #endif /* CKLOGDIAL */
  689.             fatalio = 1;
  690.             return('q');    /* Connection Lost */
  691.         }
  692.         if (nack(winlo) < 0) {
  693.             debug(F101,"input sent too many naks","",winlo);
  694.             errpkt((CHAR *)"Too many retries.");
  695.             type = 'E';
  696.             break;
  697.         } else continue;
  698.         }
  699.         if (rsn == winlo) {        /* Got the packet we want, done. */
  700. #ifdef CK_TIMERS
  701.         if (rttflg && timint)    /* Dynamic round trip timers? */
  702.           getrtt(nakstate, rsn); /* yes, do it. */
  703. #endif /* CK_TIMERS */
  704.         debug(F101,"input rsn=winlo","",rsn);
  705.         break;
  706.         }
  707.  
  708.         /* Got a packet out of order. */
  709.  
  710.         debug(F101,"input out of sequence, rsn","",rsn);
  711.         k = rseqtbl[rsn];        /* Get window slot of this packet. */
  712.         debug(F101,"input rseqtbl[rsn]","",k);
  713.         if (k < 0) {
  714.         debug(F101,"input recv can't find index for rcvd pkt","",rsn);
  715.         /* Was "Internal error 21" */
  716.         /* This should not happen  */
  717.         errpkt((CHAR *)"Sliding windows protocol error.");
  718.         type = 'E';
  719.         break;
  720.         }
  721.         y = chkwin(rsn,winlo,wslots); /* See what window it's in. */
  722.         debug(F101,"input recv chkwin","",y);
  723.         if (y == 1) {        /* From previous window. */
  724. #ifdef STREAMING
  725.         if (!streaming)        /* NO RESEND IF STREAMING! */
  726. #endif /* STREAMING */
  727.           resend(rsn);        /* Resend the ACK (might have data) */
  728.         freerpkt(rsn);        /* Get rid of received packet */
  729.         continue;        /* Back to wait for another packet */
  730.         } else {            /* In this window or out of range */
  731.         if (y < 0)        /* If out of range entirely, */
  732.           freerpkt(rsn);    /* release its buffer */
  733.  
  734. #ifdef STREAMING
  735.         if (streaming) {    /* Streaming (this shouldn't happen) */
  736.             errpkt((CHAR *)"Sequence error on reliable link.");
  737.             type = 'E';
  738.             break;
  739.         }
  740. #endif /* STREAMING */
  741.  
  742. /* If our receive window is full, NAK window-low */
  743.  
  744.         if (rbufnum < 1) {    /* Receive window full? */
  745.             if (nack(winlo) < 0) {    /* No choice, must NAK winlo. */
  746.             errpkt((CHAR *)"Too many retries."); /* Too many */
  747.             type = 'E';
  748.             break;
  749.             } else continue;
  750.         }
  751. /*
  752.   Receive window not full.  This is a packet in the current window but it is
  753.   not the desired packet at winlo.  So therefore there are gaps before this
  754.   packet.  So we find the "lowest" unNAK'd missing packet, if any, between
  755.   winlo and this one, and NAK it.  If there are no as-yet-unNAK'd missing
  756.   packets in the window, then we send nothing and go wait for another packet.
  757.   In theory, this could result in a timeout, but in practice it is likely that
  758.   the already-NAK'd missing packets are already on their way.  Note, we do not
  759.   NAK ahead of ourselves, as that only creates unnecessary retransmissions.
  760. */
  761.         for (x = winlo; x != rsn; x = (x + 1) % 64) {
  762.             if (rseqtbl[x] > -1) /* Have I received packet x? */
  763.               continue;         /* Yes, check next sequence number. */
  764.             pi = sseqtbl[x];     /* No, have I NAK'd it yet? */
  765.             if (pi < 0 || s_pkt[pi].pk_rtr == 0) {
  766.             nack(x);     /* No, NAK it now. */
  767.             break;
  768.             }
  769.         }
  770.         }
  771. /*!!!*/
  772.     } else {            /* Otherwise file sender... */
  773.  
  774. #ifdef STREAMING
  775.         if (streaming && sndtyp == 'D') {
  776.         debug(F101,"STREAMING input streaming","",streaming);
  777.         debug(F000,"STREAMING input sndtyp","",sndtyp);
  778.         rsn = winlo;
  779.         type = 'Y';        /* Pretend we got an ACK */
  780.         }
  781. #endif /* STREAMING */
  782.         if (!nak2ack) {        /* NAK(n+1) = ACK(n) */
  783.         if (wslots > 1) {    /* Packet at winlo already ACK'd? */
  784.             if (sacktbl[winlo]) { /* If so,  */
  785.             sacktbl[winlo] = 0; /* Turn off the ACK'd flag */
  786.             winlo = (winlo + 1) % 64; /* Rotate the window */
  787.             type = 'Y';    /* And return ACK */
  788.             debug(F101,
  789.                   "input send returning pre-stashed ACK","",
  790.                   winlo-1);
  791.             break;
  792.             }
  793.         }
  794. #ifdef STREAMING
  795.         if (!(streaming && sndtyp == 'D')) { /* Not streaming | data */
  796.             type = rpack();    /* Try to read an acknowledgement */
  797.         } else {        /* Streaming and in Data phase */
  798.             type = 'Y';        /* Assume all is normal */
  799.             if (chkint() < 0)    /* Check for console interrupts. */
  800.               type = 'z';
  801.             else if (ttchk() > 4 + bctu) /* Check for return traffic */
  802.               type = rpack();
  803.             debug(F000,"input streaming type","",type);
  804.         }
  805. #endif /* STREAMING */
  806.         debug(F111,"input send",(char *) rdatap,(int) type);
  807.         while (type == 'e') {    /* Handle echoes */
  808.             debug(F000,"echo discarded","",type);
  809.             type = rpack();
  810.         }
  811. #ifndef OLDCHKINT
  812.         if (type == 'z') {
  813.             epktrcvd = 1;
  814.             errpkt((CHAR *)"User cancelled.");
  815.             type = 'E';
  816.             break;
  817.         }
  818. #endif /* OLDCHKINT */
  819.         if (type < -1) {
  820.             xxscreen(SCR_PT,'q',0L,
  821.                ((char *)((type == -2) ?
  822.                "Interrupted" :
  823.                "Connection lost"))
  824.                );
  825. #ifdef CKLOGDIAL
  826.             if (type != -2)
  827.               dologend();
  828. #endif /* CKLOGDIAL */
  829.             return('q');    /* Ctrl-C or connection lost */
  830.         }
  831.         if (type == -1) {
  832. #ifdef COMMENT
  833.                     char foo[256];
  834.                     ckmakxmsg(foo,256,
  835.                   "Receive window full (error 18): wslots=",
  836.                               ckitoa(wslots),
  837.                   " winlo=",ckitoa(winlo)," pktnum=",
  838.                               ckitoa(pktnum), NULL,NULL,NULL,NULL,NULL,NULL);
  839.             errpkt((CHAR *)foo);
  840.                     debug(F100,foo,"",0);
  841. #else
  842.             errpkt((CHAR *)"Receive window full"); /* was "internal */
  843.                     debug(F101," wslots","",wslots); /* error 18" */
  844.                     debug(F101," winlo","",winlo);
  845.             debug(F101," pktnum","",pktnum);
  846. #endif /* COMMENT */
  847.             dumprbuf();
  848.             type = 'E';
  849.             break;
  850.         }
  851.         dumprbuf();        /* Debugging */
  852.  
  853. #ifdef OLDCHKINT
  854.         if (chkint() < 0) {    /* Check for console interrupts. */
  855.             errpkt((CHAR *)"User cancelled.");
  856.             return(type = 'E');
  857.         }
  858. #endif /* OLDCHKINT */
  859.  
  860.         /* Got a packet */
  861.  
  862. #ifdef STREAMING
  863.         if (streaming) {        /* Streaming */
  864.             if (type == 'Q' || type == 'T') { /* Errors are fatal. */
  865.             crunched++;    /* For statistics */
  866.             errpkt((CHAR *)"Transmission error on reliable link.");
  867.             type = 'E';
  868.             }
  869.         }
  870. #endif /* STREAMING */
  871.         if (type == 'E') {
  872.             debug(F101,"input send got E, nakstate","",nakstate);
  873.             break;        /* Error packet */
  874.         }
  875.         if (type == 'Q') {    /* Crunched packet */
  876.             crunched++;        /* For statistics */
  877.             numerrs++;        /* For packet resizing */
  878.             x = resend(winlo);    /* Resend window-low */
  879.             if (x < 0) {
  880.             type = 'E';
  881.             errpkt((CHAR *)"Too many retries");
  882.             break;
  883.             }
  884.             continue;
  885.         }
  886.         if (type == 'T') {    /* Timeout waiting for ACKs. */
  887.             timeouts++;        /* Count it */
  888.             numerrs++;        /* Count an error too */
  889.             debug(F101,"input send state timeout, winlo","",winlo);
  890.  
  891.             /* Retransmit the oldest un-ACK'd packet. */
  892.  
  893.             debug(F101,"input send resending winlo","",winlo);
  894.             if (resend(winlo) < 0) { /* Check retries */
  895.             debug(F101,"input send too many resends","",maxtry);
  896.             errpkt((CHAR *)"Too many retries");
  897.             return(type = 'E');
  898.             }
  899. #ifdef NEWDPL
  900.             /* Reduce prevailing packet length */
  901.             x = sseqtbl[winlo];    /* Get length of packet we want ACKd */
  902.             if (x > -1) {    /* Only if we have a valid index */
  903.             if (s_pkt[x].pk_typ == 'D') { /* only for D packets */
  904.                 spsiz = (s_pkt[x].pk_len + 8) >> 1; /* halve it */
  905.                 if (spsiz < 20) spsiz = 20; /* within reason */
  906.                 debug(F101,"input T cut packet length","",spsiz);
  907.             }
  908.             }
  909. #endif /* NEWDPL */
  910.             continue;
  911.         }
  912.         }
  913.         /* Got an actual normal packet */
  914.  
  915.         nak2ack = 0;        /* Unset this flag. */
  916.         y = chkwin(rsn,winlo,wslots); /* Is it in the window? */
  917.         debug(F101,"input send rsn","",rsn);
  918.         debug(F101,"input send winlo","",winlo);
  919.         debug(F101,"input send chkwin","",y);
  920.  
  921.         if (type == 'Y') {        /* Got an ACK */
  922.         if (y == 0) {        /* In current window */
  923.             if (spackets < 4)    /* Error counter doesn't count */
  924.               numerrs = 0;    /* until data phase. */
  925.             sacktbl[rsn]++;    /* Mark the packet as ACK'd */
  926.             x = sseqtbl[rsn];    /* Get ACK'd packet's buffer index */
  927.             debug(F101,"bestlen ack x","",x);
  928. #ifdef NEWDPL
  929.             if (x > -1) {
  930.             acktype = s_pkt[x].pk_typ; /* Get type */
  931.             debug(F000,"bestlen ack type","",acktype);
  932.  
  933.             if (acktype == 'D') { /* Adjust data packet length */
  934.                 if (spsiz > bestlen) {
  935.                 bestlen = spsiz;
  936.                 debug(F101,"bestlen B","",bestlen);
  937.                 }
  938. #ifdef DEBUG
  939.                 if (deblog) {
  940.                 debug(F101,"bestlen retry","",s_pkt[x].pk_rtr);
  941.                 debug(F101,"bestlen len","",s_pkt[x].pk_len);
  942.                 debug(F101,"bestlen spackets","",spackets);
  943.                 }
  944. #endif /* DEBUG */
  945.                 /* Set new best length */
  946.                 if (s_pkt[x].pk_rtr == 0 &&
  947.                 s_pkt[x].pk_len + 8 > bestlen) {
  948.                 bestlen = s_pkt[x].pk_len + 8;
  949.                 if (bestlen > spmax)
  950.                   bestlen = spmax;
  951.                 debug(F101,"bestlen A","",bestlen);
  952.                 }
  953. #ifdef DEBUG
  954.                 if (deblog) {
  955.                 debug(F101,"bestlen wslots","",wslots);
  956.                 debug(F101,"bestlen maxsend","",maxsend);
  957.                 }
  958. #endif /* DEBUG */
  959.                 /* Slow start */
  960.                 if (slostart &&
  961.                 (maxsend <= spmax) &&
  962.                 (rpackets < 11) &&
  963.                 (numerrs == 0)) {
  964.                 spsiz = spsiz << 1;
  965.                 debug(F101,"bestlen spsiz A","",spsiz);
  966.  
  967.                 /* Creep up to best length */
  968.                 } else if ((spackets > 5) &&
  969.                        (spsiz < bestlen - 8)) {
  970.                 spsiz += (bestlen - spsiz) / 3;
  971.                 debug(F101,"bestlen spsiz B","",spsiz);
  972.  
  973.                 /* Push the envelope */
  974.                 } else if ((spackets % (wslots + 1) == 0) &&
  975.                        (spackets > 6) &&
  976.                        (bestlen < spmax - 8) &&
  977.                        (spsiz < spmax)) {
  978.                 spsiz += (spmax - bestlen) / 3;
  979.                 debug(F101,"bestlen spsiz C","",spsiz);
  980.                 }
  981.                 /* But not too far */
  982.                 if (spsiz > spmax) {
  983.                 spsiz = spmax;
  984.                 debug(F101,"bestlen spsiz D","",spsiz);
  985.                 }
  986.             }
  987.             }
  988. #endif /* NEWDPL */
  989.  
  990. #ifdef CK_TIMERS
  991.             if (rttflg && timint) /* If doing dynamic timers */
  992.               getrtt(nakstate, rsn); /* call routine to set it. */
  993. #endif /* CK_TIMERS */
  994. /*
  995.   NOTE: The following statement frees the buffer of the ACK we just got.
  996.   But the upper layers still need the data, like if it's the ACK to an I,
  997.   S, F, D, Z, or just about any kind of packet.  So for now, freerbuf()
  998.   deallocates the buffer, but does not erase the data or destroy the pointer
  999.   to it.  There's no other single place where these receive buffers can be
  1000.   correctly freed (?) ...
  1001. */
  1002.             freerpkt(rsn);    /* Free the ACK's buffer */
  1003.             freesbuf(rsn);    /* *** Free the sent packet's buffer */
  1004.             if (rsn == winlo) {    /* Got the one we want */
  1005.             sacktbl[winlo] = 0;
  1006.             winlo = (winlo + 1) % 64;
  1007.             debug(F101,"input send rotated send window","",winlo);
  1008.             break;        /* Return the ACK */
  1009.             } else {
  1010.             debug(F101,"input send mark pkt","",rsn);
  1011.             continue;    /* Otherwise go read another packet */
  1012.             }
  1013.         } else if (y == 1 && wslots < 2) { /* (190) ACK for previous */
  1014.             numerrs++;        /* == NAK for current, count error */
  1015.             debug(F101,"input send ACK for previous","",rsn);
  1016.             freerpkt(rsn);    /* Free NAK's buffer */
  1017.             x = resend(winlo);    /* Resend current packet */
  1018.             if (x < 0) {
  1019.             type = 'E';
  1020.             errpkt((CHAR *)"Too many retries");
  1021.             break;
  1022.             } else continue;    /* Resend ok, go read another packet */
  1023.         } else {        /* Other cases, just ignore */
  1024.             debug(F101,"input send ACK out of window","",rsn);
  1025.             freerpkt(rsn);
  1026.             continue;
  1027.         }
  1028.         }
  1029.         if (type == 'N') {        /* NAK */
  1030.         numerrs++;        /* Count an error */
  1031. #ifdef STREAMING
  1032.         if (streaming) {        /* Streaming */
  1033.             errpkt((CHAR *)"NAK received on reliable link.");
  1034.             type = 'E';
  1035.             break;
  1036.         }
  1037. #endif /* STREAMING */
  1038.  
  1039.         debug(F101,"input send NAK","",rsn);
  1040. #ifdef NEWDPL
  1041.         /* Reduce prevailing packet length */
  1042.         x = sseqtbl[rsn];    /* Length of packet that was NAK'd */
  1043.         if (x > -1) {        /* If it's a Data packet we've sent */
  1044.             if (s_pkt[x].pk_typ == 'D') {
  1045.             spsiz = (s_pkt[x].pk_len + 8) >> 1; /* Halve length */
  1046. #ifdef COMMENT
  1047.             /* This might be a good idea -- haven't tried it ... */
  1048.             if (bestlen > 0 && spsiz > bestlen)
  1049.               spsiz = bestlen;
  1050. #endif /* COMMENT */
  1051.             if (spsiz < 20) spsiz = 20;
  1052.             debug(F101,"input N cut packet length","",spsiz);
  1053.             }
  1054.         }
  1055. #endif /* NEWDPL */
  1056.         freerpkt(rsn);        /* Free buffer where NAK lies. */
  1057.         if (y == 0) {        /* In current window */
  1058.             debug(F100," in window","",0);
  1059.             k = sseqtbl[rsn];    /* Get pointer to NAK'd packet. */
  1060.             if (k < 0 || (k > -1 && s_pkt[k].pk_typ == ' ')) {
  1061.             x = resend(winlo); /* Packet we haven't sent yet. */
  1062.             } else {
  1063.             x = resend(rsn); /* Resend requested packet. */
  1064.             }
  1065.             if (x < 0) {    /* Resend error is fatal.  */
  1066.             type = 'E';
  1067.             errpkt((CHAR *)"Too many retries");
  1068.             break;
  1069.             } else continue;    /* Resend ok, go read another packet */
  1070.         } else if ((rsn == (pktnum + 1) % 64)) { /* NAK for next pkt */
  1071.             if (wslots > 1) {
  1072.             debug( F101,"NAK for next packet, windowing","",rsn);
  1073.             x = resend(winlo); /* Resend window-low */
  1074.             if (x < 0) {
  1075.                 type = 'E';
  1076.                 errpkt((CHAR *)"Too many retries");
  1077.                 break;
  1078.             }
  1079.             continue;    /* Go back and read another pkt */
  1080.             }
  1081.             debug(F101,"NAK for next packet, no windowing","",rsn);
  1082.             x = (rsn == 0) ? 63 : rsn - 1;
  1083.             if (x == 0 && (sndtyp == 'S' || sndtyp == 'I')) {
  1084.             resend(0);    /* ACK for S or I packet missing */
  1085.             continue;    /* so resend the S or I */
  1086.             }
  1087.             rsn = x;        /* Else, treat NAK(n+1) as ACK(n) */
  1088.             nak2ack = 1;    /* Go back and process the ACK */
  1089.             continue;
  1090.         } else if (y > 0) {    /* NAK for pkt we can't resend */
  1091.             debug(F101," NAK out of window","",rsn); /* bad... */
  1092.             type = 'E';
  1093.             errpkt((CHAR *)"NAK out of window");
  1094.             break;
  1095.         } else continue;    /* Ignore other NAKs */
  1096.         }                /* End of file-sender NAK handler */
  1097.  
  1098.             if (rsn == winlo) {        /* Not ACK, NAK, timeout, etc. */
  1099.         debug(F000,"input send unexpected type","",type);
  1100.         break;
  1101.         }
  1102.     }                /* End of file-sender section */
  1103.     }                    /* End of input() while() loop */
  1104. /*
  1105.   When the window size is 1 and we have the packet we want, there can not
  1106.   possibly be anything waiting for us on the connection that is useful to us.
  1107.   However, there might be redundant copies of a packet we already got, which
  1108.   would cause needless cycles of repeated packets.  Therefore we flush the
  1109.   communications input buffer now to try to get rid of undesired and unneeded
  1110.   packets that we have not read yet.
  1111. */
  1112.     if (wslotn == 1            /* (not wslots!) */
  1113. #ifdef STREAMING
  1114.     && !streaming            /* But not when streaming */
  1115. #endif /* STREAMING */
  1116.     ) {
  1117.     debug(F100,"input about to flush","",0);
  1118.     ttflui();        /* Got what we want, clear input buffer. */
  1119.     }
  1120. #ifndef NEWDPL
  1121.     if (!nakstate)            /* When sending */
  1122.       rcalcpsz();            /* recalculate size every packet */
  1123. #endif /* NEWDPL */
  1124.     if (type == 'E')
  1125.       xitsta |= (what ? what : 1);    /* Remember what failed. */
  1126.     debug(F101,"input winlo","",winlo);
  1127.     debug(F101,"input rsn","",rsn);
  1128.     debug(F000,"input returning type","",type);
  1129.     return(rcvtyp = type);        /* Success, return packet type. */
  1130. }
  1131.  
  1132. #ifdef PARSENSE
  1133. /*  P A R C H K  --  Check if Kermit packet has parity  */
  1134.  
  1135. /*
  1136.   Call with s = pointer to packet, start = packet start character, n = length.
  1137.   Returns 0 if packet has no parity, -1 on error, or, if packet has parity:
  1138.     'e' for even, 'o' for odd, 'm' for mark.  Space parity cannot be sensed.
  1139.   So a return value of 0 really means either space or none.
  1140.   Returns -2 if parity has already been checked during this protocol operation.
  1141. */
  1142. int
  1143. #ifdef CK_ANSIC
  1144. parchk(CHAR *s, CHAR start, int n)
  1145. #else
  1146. parchk(s,start,n) CHAR *s, start; int n;
  1147. #endif /* CK_ANSIC */
  1148. /* parchk */ {
  1149.     CHAR s0, s1, s2, s3;
  1150.  
  1151.     debug(F101,"parchk n","",n);
  1152.     debug(F101,"parchk start","",start);
  1153.  
  1154.     s0 = s[0] & 0x7f;            /* Mark field (usually Ctrl-A) */
  1155.  
  1156.     if (s0 != start || n < 5) return(-1); /* Not a valid packet */
  1157.  
  1158. /* Look at packet control fields, which never have 8th bit set */
  1159. /* First check for no parity, most common case. */
  1160.  
  1161.     if (((s[0] | s[1] | s[2] | s[3]) & 0x80) == 0)
  1162.       return(0);            /* No parity or space parity */
  1163.  
  1164. /* Check for mark parity */
  1165.  
  1166.     if (((s[0] & s[1] & s[2] & s[3]) & 0x80) == 0x80)
  1167.       return('m');            /* Mark parity */
  1168.  
  1169. /* Packet has some kind of parity */
  1170. /* Make 7-bit copies of control fields */
  1171.  
  1172.     s1 = s[1] & 0x7f;            /* LEN */
  1173.     s2 = s[2] & 0x7f;            /* SEQ */
  1174.     s3 = s[3] & 0x7f;            /* TYPE */
  1175.  
  1176. /* Check for even parity */
  1177.  
  1178.     if ((s[0] == p_tbl[s0]) &&
  1179.         (s[1] == p_tbl[s1]) &&
  1180.         (s[2] == p_tbl[s2]) &&
  1181.     (s[3] == p_tbl[s3]))
  1182.       return('e');
  1183.  
  1184. /* Check for odd parity */
  1185.  
  1186.     if ((s[0] != p_tbl[s0]) &&
  1187.         (s[1] != p_tbl[s1]) &&
  1188.         (s[2] != p_tbl[s2]) &&
  1189.     (s[3] != p_tbl[s3]))
  1190.       return('o');
  1191.  
  1192. /* Otherwise it's probably line noise.  Let checksum calculation catch it. */
  1193.  
  1194.     return(-1);
  1195. }
  1196. #endif /* PARSENSE */
  1197.  
  1198. /*
  1199.   Check to make sure timeout intervals are long enough to allow maximum
  1200.   length packets to get through before the timer goes off.  If not, the
  1201.   timeout interval is adjusted upwards.
  1202.  
  1203.   This routine is called at the beginning of a transaction, before we
  1204.   know anything about the delay characteristics of the line.  It works
  1205.   only for serial communication devices; it trusts the speed reported by
  1206.   the operating system.
  1207.  
  1208.   Call with a timout interval.  Returns it, adjusted if necessary.
  1209. */
  1210. int
  1211. chktimo(timo,flag) int timo, flag; {
  1212.     long cps, z; int x, y;
  1213. #ifdef STREAMING
  1214.     debug(F101,"chktimo streaming","",streaming);
  1215.     if (streaming)
  1216.       return(0);
  1217. #endif /* STREAMING */
  1218.  
  1219.     debug(F101,"chktimo timo","",timo); /* Timeout before adjustment */
  1220.     debug(F101,"chktimo flag","",flag);
  1221.  
  1222.     if (flag)                /* Don't change timeout if user */
  1223.       return(timo);            /* gave SET SEND TIMEOUT command. */
  1224.     debug(F101,"chktimo spmax","",spmax);
  1225.     debug(F101,"chktimo urpsiz","",urpsiz);
  1226.  
  1227.     speed = ttgspd();            /* Get current speed. */
  1228.     if (speed > 0L && !network) {
  1229.     cps = speed / 10L;        /* Convert to chars per second */
  1230.     if (cps > 0L) {
  1231.         long plen;            /* Maximum of send and rcv pkt size */
  1232.         z = cps * (long) timo;    /* Chars per timeout interval */
  1233.         z -= z / 10L;        /* Less 10 percent */
  1234.         plen = spmax;
  1235.         if (urpsiz > spmax) plen = urpsiz;
  1236.         debug(F101,"chktimo plen","",plen);
  1237.         if (z < plen) {        /* Compare with packet size */
  1238.         x = (int) ((long) plen / cps); /* Adjust if necessary */
  1239.         y = x / 10;        /* Add 10 percent for safety */
  1240.         if (y < 2) y = 2;    /* Or 2 seconds, whichever is more */
  1241.         x += y;
  1242.         if (x > timo)        /* If this is greater than current */
  1243.           timo = x;        /* timeout, change the timeout */
  1244.         debug(F101,"chktimo new timo","",timo);
  1245.         }
  1246.     }
  1247.     }
  1248.     return(timo);
  1249. }
  1250.  
  1251. /*  S P A C K  --  Construct and send a packet  */
  1252.  
  1253. /*
  1254.   spack() sends a packet of the given type, sequence number n, with len data
  1255.   characters pointed to by d, in either a regular or extended- length packet,
  1256.   depending on len.  Returns the number of bytes actually sent, or else -1
  1257.   upon failure.  Uses global npad, padch, mystch, bctu, data.  Leaves packet
  1258.   fully built and null-terminated for later retransmission by resend().
  1259.   Updates global sndpktl (send-packet length).
  1260.  
  1261.   NOTE: The global pointer "data" is assumed to point into the 7th position
  1262.   of a character array (presumably in packet buffer for the current packet).
  1263.   It was used by getpkt() to build the packet data field.  spack() fills in
  1264.   the header to the left of the data pointer (the data pointer is defined
  1265.   in getsbuf() in ckcfn3.c).  If the address "d" is the same as "data", then
  1266.   the packet's data field has been built "in place" and need not be copied.
  1267. */
  1268. int
  1269. #ifdef CK_ANSIC
  1270. spack(char pkttyp, int n, int len, CHAR *d)
  1271. #else
  1272. spack(pkttyp,n,len,d) char pkttyp; int n, len; CHAR *d;
  1273. #endif /* CK_ANSIC */
  1274. /* spack */ {
  1275.     register int i;
  1276.     int ix, j, k, x, lp, longpkt, copy, loglen;
  1277.  
  1278. #ifdef GFTIMER
  1279.     CKFLOAT t1 = 0.0, t2 = 0.0;
  1280. #endif /* GFTIMER */
  1281.  
  1282.     register CHAR *cp, *mydata;
  1283.     unsigned crc;
  1284.  
  1285.     copy = (d != data);            /* Flag whether data must be copied  */
  1286.  
  1287. #ifdef DEBUG
  1288.     if (deblog) {            /* Save lots of function calls! */
  1289.     debug(F101,"spack n","",n);
  1290.     if (pkttyp != 'D') {        /* Data packets would be too long */
  1291.         debug(F111,"spack data",data,data);
  1292.         debug(F111,"spack d",d,d);
  1293.     }
  1294.     debug(F101,"spack len","",len);
  1295.     debug(F101,"spack copy","",copy);
  1296.     }
  1297. #endif /* DEBUG */
  1298.  
  1299.     longpkt = (len + bctl + 2) > 94;    /* Decide whether it's a long packet */
  1300.     mydata = data - 7 + (longpkt ? 0 : 3); /* Starting position of header */
  1301.     k = sseqtbl[n];            /* Packet structure info for pkt n */
  1302. #ifdef DEBUG
  1303.     if (deblog) {            /* Save 2 more function calls... */
  1304.     debug(F101,"spack mydata","",mydata);
  1305.     debug(F101,"spack sseqtbl[n]","",k);
  1306.     if (k < 0) {
  1307. #ifdef STREAMING
  1308.         if (!streaming)
  1309. #endif /* STREAMING */
  1310.           debug(F101,"spack sending packet out of window","",n);
  1311.     }
  1312.     }
  1313. #endif /* DEBUG */
  1314.     if (k > -1) {
  1315.     s_pkt[k].pk_adr = mydata;    /* Remember address of packet. */
  1316.     s_pkt[k].pk_seq = n;        /* Record sequence number */
  1317.     s_pkt[k].pk_typ = pkttyp;    /* Record packet type */
  1318.     }
  1319.     spktl = 0;                /* Initialize length of this packet */
  1320.     i = 0;                /* and position in packet. */
  1321.  
  1322. /* Now fill the packet */
  1323.  
  1324.     mydata[i++] = mystch;        /* MARK */
  1325.     lp = i++;                /* Position of LEN, fill in later */
  1326.  
  1327.     mydata[i++] = tochar(n);        /* SEQ field */
  1328.     mydata[i++] = pkttyp;        /* TYPE field */
  1329.     j = len + bctl;            /* Length of data + block check */
  1330.     if (longpkt) {            /* Long packet? */
  1331.     int x;                /* Yes, work around SCO Xenix/286 */
  1332. #ifdef CKTUNING
  1333.     unsigned int chk;
  1334. #endif /* CKTUNING */
  1335.     x = j / 95;            /* compiler bug... */
  1336.         mydata[lp] = tochar(0);        /* Set LEN to zero */
  1337.         mydata[i++] = tochar(x);    /* Extended length, high byte */
  1338.         mydata[i++] = tochar(j % 95);    /* Extended length, low byte */
  1339. #ifdef CKTUNING
  1340.         /* Header checksum - skip the function calls and loops */
  1341.       chk = (unsigned) mydata[lp]   +
  1342.             (unsigned) mydata[lp+1] +
  1343.             (unsigned) mydata[lp+2] +
  1344.         (unsigned) mydata[lp+3] +
  1345.         (unsigned) mydata[lp+4] ;
  1346.     mydata[i++] = tochar((CHAR) ((((chk & 0300) >> 6) + chk) & 077));
  1347. #else
  1348.         mydata[i] = '\0';        /* Terminate for header checksum */
  1349.         mydata[i++] = tochar(chk1(mydata+lp,5));
  1350. #endif /* CKTUNING */
  1351.     } else mydata[lp] = tochar(j+2);    /* Normal LEN */
  1352. /*
  1353.   When sending a file, the data is already in the right place.  If it weren't,
  1354.   it might make sense to optimize this section by using memcpy or bcopy
  1355.   (neither of which are portable), but only if our packets were rather long.
  1356.   When receiving, we're only sending ACKs so it doesn't matter.  So count the
  1357.   following loop as a sleeping dog.
  1358. */
  1359.     if (copy)                /* Data field built in place? */
  1360.       for ( ; len--; i++) mydata[i] = *d++; /* No, must copy. */
  1361.     else                /* Otherwise, */
  1362.       i += len;                /* Just skip past data field. */
  1363.     mydata[i] = '\0';            /* Null-terminate for checksum calc. */
  1364.  
  1365.     switch (bctu) {            /* Block check */
  1366.         case 1:                /* 1 = 6-bit chksum */
  1367.         ix = i - lp;        /* Avoid "order of operation" error */
  1368.         mydata[i++] = tochar(chk1(mydata+lp,ix));
  1369.         break;
  1370.     case 2:                /* 2 = 12-bit chksum */
  1371.         j = chk2(mydata+lp,i-lp);
  1372.         mydata[i++] = (unsigned)tochar((j >> 6) & 077);
  1373.            mydata[i++] = (unsigned)tochar(j & 077);
  1374.         break;
  1375.         case 3:                /* 3 = 16-bit CRC */
  1376.         crc = chk3(mydata+lp,i-lp);
  1377.         mydata[i++] = (unsigned)tochar(((crc & 0170000)) >> 12);
  1378.         mydata[i++] = (unsigned)tochar((crc >> 6) & 077);
  1379.         mydata[i++] = (unsigned)tochar(crc & 077);
  1380.         break;
  1381.     case 4:                /* 2 = 12-bit chksum, blank-free */
  1382.         j = chk2(mydata+lp,i-lp);
  1383.         mydata[i++] =
  1384.           (unsigned)(tochar((unsigned)(((j >> 6) & 077) + 1)));
  1385.            mydata[i++] = (unsigned)(tochar((unsigned)((j & 077) + 1)));
  1386.         break;
  1387.     }
  1388.     loglen = i;
  1389.     mydata[i++] = seol;            /* End of line (packet terminator) */
  1390. #ifdef TCPSOCKET
  1391. /*
  1392.   If TELNET connection and packet terminator is carriage return,
  1393.   we must stuff either LF or NUL, according to SET TELNET NEWLINE-MODE
  1394.   (tn_nlm), to meet the TELNET NVT specification, unless user said RAW.
  1395.  
  1396.   If NEWLINE-MODE is set to LF instead of CR, we still send CR-NUL
  1397.   on a NVT connection and CR on a binary connection.
  1398. */
  1399.     if (
  1400. #ifdef STREAMING
  1401.     !dontsend &&
  1402. #endif /* STREAMING */
  1403.     ((network && ttnproto == NP_TELNET) || (!local && sstelnet))
  1404.     && seol == CR) {
  1405.         switch (TELOPT_ME(TELOPT_BINARY) ? tn_b_nlm : tn_nlm) {
  1406.       case TNL_CR:            /* NVT or BINARY */
  1407.         break;
  1408.       case TNL_CRNUL:
  1409.         mydata[i++] = NUL;
  1410.         break;
  1411.       case TNL_CRLF:
  1412.         mydata[i++] = LF;
  1413.         break;
  1414.     }
  1415.     }
  1416. #endif /* TCPSOCKET */
  1417.     mydata[i] = '\0';            /* Terminate string */
  1418.     if (
  1419. #ifdef STREAMING
  1420.     !dontsend &&
  1421. #endif /* STREAMING */
  1422.     pktlog
  1423.     )                /* Save a function call! */
  1424.       logpkt('s',n,mydata,loglen);    /* Log the packet */
  1425.  
  1426.     /* (PWP) add the parity quickly at the end */
  1427.     if (parity) {
  1428.     switch (parity) {
  1429.       case 'e':            /* Even */
  1430.         for (cp = &mydata[i-1]; cp >= mydata; cp--)
  1431.           *cp = p_tbl[*cp];
  1432.         break;
  1433.       case 'm':            /* Mark */
  1434.         for (cp = &mydata[i-1]; cp >= mydata; cp--)
  1435.           *cp |= 128;
  1436.         break;
  1437.       case 'o':            /* Odd */
  1438.         for (cp = &mydata[i-1]; cp >= mydata; cp--)
  1439.           *cp = p_tbl[*cp] ^ 128;
  1440.         break;
  1441.       case 's':            /* Space */
  1442.         for (cp = &mydata[i-1]; cp >= mydata; cp--)
  1443.           *cp &= 127;
  1444.         break;
  1445.     }
  1446.     }
  1447.     if (pktpaus) msleep(pktpaus);    /* Pause if requested */
  1448.     x = 0;
  1449.  
  1450.     if (npad) {
  1451. #ifdef STREAMING
  1452.     if (dontsend)
  1453.       x = 0;
  1454.     else
  1455. #endif /* STREAMING */
  1456.       x = ttol(padbuf,npad);    /* Send any padding */
  1457.     }
  1458.     if (x > -1) {
  1459. #ifdef CK_TIMERS
  1460.     if (timint > 0) {
  1461.         if (pkttyp == 'N')
  1462.           srttbl[n > 0 ? n-1 : 63] = gtimer();
  1463.         else
  1464.           srttbl[n] = gtimer();
  1465.     }
  1466. #endif /* CK_TIMERS */
  1467.     spktl = i;            /* Remember packet length */
  1468.     if (k > -1)
  1469.       s_pkt[k].pk_len = spktl;    /* also in packet info structure */
  1470.  
  1471. #ifdef DEBUG
  1472. #ifdef GFTIMER
  1473. /*
  1474.   This code shows (in the debug log) how long it takes write() to execute.
  1475.   Sometimes on a congested TCP connection, it can surprise you -- 90 seconds
  1476.   or more...
  1477. */
  1478.     if (
  1479. #ifdef STREAMING
  1480.         !dontsend &&
  1481. #endif /* STREAMING */
  1482.         deblog
  1483.         )
  1484.       t1 = gftimer();
  1485. #endif /* GFTIMER */
  1486. #endif /* DEBUG */
  1487.  
  1488. #ifdef STREAMING
  1489.     if (dontsend) {
  1490.         debug(F000,"STREAMING spack skipping","",pkttyp);
  1491.         x = 0;
  1492.     } else
  1493. #endif /* STREAMING */
  1494.     x = ttol(mydata,spktl);        /* Send the packet */
  1495.     }
  1496. #ifdef STREAMING
  1497.     if (!dontsend) {
  1498. #endif /* STREAMING */
  1499.     debug(F101,"spack spktl","",spktl);
  1500.     debug(F101,"spack ttol returns","",x);
  1501.     if (x < 0) {            /* Failed. */
  1502.         if (local && x < -1) {
  1503.         xxscreen(SCR_ST,ST_ERR,0L,"FAILED: Connection lost");
  1504.         /* We can't send an E packet because the connection is lost. */
  1505.         epktsent = 1;        /* So pretend we sent one. */
  1506.         fatalio = 1;        /* Remember we got a fatal i/o error */
  1507. #ifdef CKLOGDIAL
  1508.         dologend();
  1509. #endif /* CKLOGDIAL */
  1510.         ckstrncpy((char *)epktmsg,"Connection lost",PKTMSGLEN);
  1511.         }
  1512.         return(x);
  1513.     }
  1514.     if (spktl > maxsend)        /* Keep track of longest packet sent */
  1515.       maxsend = spktl;
  1516. #ifdef DEBUG
  1517. #ifdef GFTIMER
  1518.     if (deblog)  {            /* Log elapsed time for write() */
  1519.         t2 = gftimer();
  1520.         debug(F101,"spack ttol msec","",(long)((t2-t1)*1000.0));
  1521.     }
  1522. #endif /* GFTIMER */
  1523. #endif /* DEBUG */
  1524. #ifdef STREAMING
  1525.     }
  1526. #endif /* STREAMING */
  1527.  
  1528.     sndtyp = pkttyp;            /* Remember packet type for echos */
  1529. #ifdef STREAMING
  1530.     if (!dontsend) {            /* If really sent, */
  1531.     spackets++;            /* count it. */
  1532.     flco += spktl;            /* Count the characters */
  1533.     tlco += spktl;            /* for statistics... */
  1534. #ifdef DEBUG
  1535.     if (deblog) {            /* Save two function calls! */
  1536.         dumpsbuf();            /* Dump send buffers to debug log */
  1537.         debug(F111,"spack calling screen, mydata=",mydata,n);
  1538.     }
  1539. #endif /* DEBUG */
  1540.     }
  1541. #endif /* STREAMING */
  1542.     if (local) {
  1543.     int x = 0;
  1544.     if (fdispla != XYFD_N) x = 1;
  1545.     if ((fdispla == XYFD_B) && (pkttyp == 'D' || pkttyp == 'Y')) x = 0;
  1546.     if (x)
  1547.       xxscreen(SCR_PT,pkttyp,(long)n,(char *)mydata); /* Update screen */
  1548.     }
  1549.     return(spktl);            /* Return length */
  1550. }
  1551.  
  1552. /*  C H K 1  --  Compute a type-1 Kermit 6-bit checksum.  */
  1553.  
  1554. int
  1555. chk1(pkt,len) register CHAR *pkt; register int len; {
  1556.     register unsigned int chk;
  1557. #ifdef CKTUNING
  1558. #ifdef COMMENT
  1559.     register unsigned int m;        /* Avoid function call */
  1560.     m = (parity) ? 0177 : 0377;
  1561.     for (chk = 0; len-- > 0; pkt++)
  1562.       chk += *pkt & m;
  1563. #else
  1564.     chk = 0;
  1565.     while (len-- > 0) chk += (unsigned) *pkt++;
  1566. #endif /* COMMENT */
  1567. #else
  1568.     chk = chk2(pkt,len);
  1569. #endif /* CKTUNING */
  1570.     chk = (((chk & 0300) >> 6) + chk) & 077;
  1571.     debug(F101,"chk1","",chk);
  1572.     return((int) chk);
  1573. }
  1574.  
  1575. /*  C H K 2  --  Compute the numeric sum of all the bytes in the packet.  */
  1576.  
  1577. unsigned int
  1578. chk2(pkt,len) register CHAR *pkt; register int len; {
  1579.     register long chk;
  1580. #ifdef COMMENT
  1581.     register unsigned int m;
  1582.     m = (parity) ? 0177 : 0377;
  1583.     for (chk = 0; len-- > 0; pkt++)
  1584.       chk += *pkt & m;
  1585. #else
  1586.     /* Parity has already been stripped */
  1587.     chk = 0L;
  1588.     while (len-- > 0) chk += (unsigned) *pkt++;
  1589. #endif /* COMMENT */
  1590.     debug(F101,"chk2","",(unsigned int) (chk & 07777));
  1591.     return((unsigned int) (chk & 07777));
  1592. }
  1593.  
  1594. /*  C H K 3  --  Compute a type-3 Kermit block check.  */
  1595. /*
  1596.  Calculate the 16-bit CRC-CCITT of a null-terminated string using a lookup
  1597.  table.  Assumes the argument string contains no embedded nulls.
  1598. */
  1599. #ifdef COMMENT
  1600. unsigned int
  1601. chk3(pkt,parity,len) register CHAR *pkt; int parity; register int len; {
  1602.     register long c, crc;
  1603.     register unsigned int m;
  1604.     m = (parity) ? 0177 : 0377;
  1605.     for (crc = 0; len-- > 0; pkt++) {
  1606.     c = crc ^ (long)(*pkt & m);
  1607.     crc = (crc >> 8) ^ (crcta[(c & 0xF0) >> 4] ^ crctb[c & 0x0F]);
  1608.     }
  1609.     return((unsigned int) (crc & 0xFFFF));
  1610. }
  1611. #else
  1612. unsigned int
  1613. chk3(pkt,len) register CHAR *pkt; register int len; {
  1614.     register long c, crc;
  1615.     for (crc = 0; len-- > 0; pkt++) {
  1616.     c = crc ^ (long)(*pkt);
  1617.     crc = (crc >> 8) ^ (crcta[(c & 0xF0) >> 4] ^ crctb[c & 0x0F]);
  1618.     }
  1619.     debug(F101,"chk3","",(unsigned int) (crc & 0xFFFF));
  1620.     return((unsigned int) (crc & 0xFFFF));
  1621. }
  1622. #endif /* COMMENT */
  1623.  
  1624. /*  N X T P K T  --  Next Packet  */
  1625. /*
  1626.   Get packet number of next packet to send and allocate a buffer for it.
  1627.   Returns:
  1628.     0 on success, with global pktnum set to the packet number;
  1629.    -1 on failure to allocate buffer (fatal);
  1630.    -2 if resulting packet number is outside the current window.
  1631. */
  1632. int
  1633. nxtpkt() {                /* Called by file sender */
  1634.     int j, n, x;
  1635.  
  1636.     debug(F101,"nxtpkt pktnum","",pktnum);
  1637.     debug(F101,"nxtpkt winlo ","",winlo);
  1638.     n = (pktnum + 1) % 64;        /* Increment packet number mod 64 */
  1639.     debug(F101,"nxtpkt n","",n);
  1640. #ifdef STREAMING
  1641.     if (!streaming) {
  1642.     x = chkwin(n,winlo,wslots);    /* Don't exceed window boundary */
  1643.     debug(F101,"nxtpkt chkwin","",x);
  1644.     if (x)
  1645.       return(-2);
  1646.     j = getsbuf(n);            /* Get a buffer for packet n */
  1647.     if (j < 0) {
  1648.         debug(F101,"nxtpkt getsbuf failure","",j);
  1649.         return(-1);
  1650.     }
  1651.     }
  1652. #endif /* STREAMING */
  1653.     pktnum = n;
  1654.     return(0);
  1655. }
  1656.  
  1657. /* Functions for sending ACKs and NAKs */
  1658.  
  1659. /* Note, we should only ACK the packet at window-low (winlo) */
  1660. /* However, if an old packet arrives again (e.g. because the ACK we sent */
  1661. /* earlier was lost), we ACK it again. */
  1662.  
  1663. int
  1664. ack() {                    /* Acknowledge the current packet. */
  1665.     return(ackns(winlo,(CHAR *)""));
  1666. }
  1667.  
  1668. #ifdef STREAMING
  1669. int
  1670. fastack() {                /* Acknowledge packet n */
  1671.     int j, k, n, x;
  1672.     n = winlo;
  1673.  
  1674.     k = rseqtbl[n];            /* First find received packet n. */
  1675.     debug(F101,"STREAMING fastack k","",k);
  1676.     freesbuf(n);            /* Free current send-buffer, if any */
  1677.     if ((j = getsbuf(n)) < 0) {
  1678.     /* This can happen if we have to re-ACK an old packet that has */
  1679.         /* already left the window.  It does no harm. */
  1680.     debug(F101,"STREAMING fastack can't getsbuf","",n);
  1681.     }
  1682.     dontsend = 1;
  1683.     x = spack('Y',n,0,(CHAR *)"");    /* Now send it (but not really) */
  1684.     dontsend = 0;
  1685.     if (x < 0) return(x);
  1686.     debug(F101,"STREAMING fastack x","",x);
  1687.     if (k > -1)
  1688.       freerbuf(k);            /* don't need it any more */
  1689.     if (j > -1)
  1690.       freesbuf(j);            /* and don't need to keep ACK either */
  1691.     winlo = (winlo + 1) % 64;
  1692.     return(0);
  1693. }
  1694. #endif /* STREAMING */
  1695.  
  1696. int
  1697. ackns(n,s) int n; CHAR *s; {        /* Acknowledge packet n */
  1698.     int j, k, x;
  1699.     debug(F111,"ackns",s,n);
  1700.  
  1701.     k = rseqtbl[n];            /* First find received packet n. */
  1702.     debug(F101,"ackns k","",k);
  1703.     freesbuf(n);            /* Free current send-buffer, if any */
  1704.     if ((j = getsbuf(n)) < 0) {
  1705.     /* This can happen if we have to re-ACK an old packet that has */
  1706.         /* already left the window.  It does no harm. */
  1707.     debug(F101,"ackns can't getsbuf","",n);
  1708.     }
  1709.     x = spack('Y',n,(int)strlen((char *)s),s); /* Now send it. */
  1710.     if (x < 0) return(x);
  1711.     debug(F101,"ackns winlo","",winlo);
  1712.     debug(F101,"ackns n","",n);
  1713.     if (n == winlo) {            /* If we're acking winlo */
  1714.     if (k > -1)
  1715.       freerbuf(k);            /* don't need it any more */
  1716.     if (j > -1)
  1717.       freesbuf(j);            /* and don't need to keep ACK either */
  1718.     winlo = (winlo + 1) % 64;
  1719.     }
  1720.     return(0);
  1721. }
  1722.  
  1723. int
  1724. ackn(n) int n; {            /* Send ACK for packet number n */
  1725.     return(ackns(n,(CHAR *)""));
  1726. }
  1727.  
  1728. int
  1729. ack1(s) CHAR *s; {            /* Send an ACK with data. */
  1730.     if (!s) s = (CHAR *)"";
  1731.     debug(F110,"ack1",(char *)s,0);
  1732.     return(ackns(winlo,s));
  1733. }
  1734.  
  1735. /* N A C K  --   Send a Negative ACKnowledgment. */
  1736. /*
  1737.  Call with the packet number, n, to be NAK'd.
  1738.  Returns -1 if that packet has been NAK'd too many times, otherwise 0.
  1739.  Btw, it is not right to return 0 under error conditions.  This is
  1740.  done because the -1 code is used for cancelling the file transfer.
  1741.  More work is needed here.
  1742. */
  1743. int
  1744. nack(n) int n; {
  1745.     int i, x;
  1746.  
  1747.     if (n < 0 || n > 63) {
  1748.     debug(F101,"nack bad pkt num","",n);
  1749.     return(0);
  1750.     } else debug(F101,"nack","",n);
  1751.     if ((i = sseqtbl[n]) < 0) {        /* If necessary */
  1752.     if (getsbuf(n) < 0) {        /* get a buffer for this NAK */
  1753.         debug(F101,"nack can't getsbuf","",n);
  1754.         return(0);
  1755.     } else i = sseqtbl[n];        /* New slot number */
  1756.     }
  1757.     if (maxtry > 0 && s_pkt[i].pk_rtr++ > maxtry) /* How many? */
  1758.       return(-1);            /* Too many... */
  1759.  
  1760. /* Note, don't free this buffer.  Eventually an ACK will come, and that */
  1761. /* will set it free.  If not, well, it's back to ground zero anyway...  */
  1762.  
  1763.     x = spack('N',n,0,(CHAR *) "");    /* NAKs never have data. */
  1764.     return(x);
  1765. }
  1766.  
  1767. #ifndef NEWDPL                /* This routine no longer used */
  1768. /*
  1769.  * (PWP) recalculate the optimal packet length in the face of errors.
  1770.  * This is a modified version of the algorithm by John Chandler in Kermit/370,
  1771.  * see "Dynamic Packet Size Control", Kermit News, V2 #1, June 1988.
  1772.  *
  1773.  * This implementation minimizes the total overhead equation, which is
  1774.  *
  1775.  *   Total chars = file_chars + (header_len * num_packs)
  1776.  *                            + (errors * (header_len + packet_len))
  1777.  *
  1778.  * Differentiate with respect to number of chars, solve for packet_len, get:
  1779.  *
  1780.  *   packet_len = sqrt (file_chars * header_len / errors)
  1781.  */
  1782.  
  1783. /*
  1784.  (FDC) New super-simple algorithm.  If there was an error in the most recent
  1785.  packet exchange, cut the send-packet size in half, down to a minimum of 20.
  1786.  If there was no error, increase the size by 5/4, up to the maximum negotiated
  1787.  length.  Seems to be much more responsive than previous algorithm, which took
  1788.  forever to recover the original packet length, and it also went crazy under
  1789.  certain conditions.
  1790.  
  1791.  Here's another idea for packet length resizing that keeps a history of the
  1792.  last n packets.  Push a 1 into the left end of an n-bit shift register if the
  1793.  current packet is good, otherwise push a zero.  The current n-bit value, w, of
  1794.  this register is a weighted sum of the noise hits for the last n packets, with
  1795.  the most recent weighing the most.  The current packet length is some function
  1796.  of w and the negotiated packet length, like:
  1797.  
  1798.    (2^n - w) / (2^n) * (negotiated length)
  1799.  
  1800.  If the present resizing method causes problems, think about this one a little
  1801.  more.
  1802. */
  1803. VOID
  1804. rcalcpsz() {
  1805.  
  1806. #ifdef COMMENT
  1807. /* Old way */
  1808.     register long x, q;
  1809.     if (numerrs == 0) return;        /* bounds check just in case */
  1810.  
  1811.     /* overhead on a data packet is npad+5+bctr, plus 3 if extended packet */
  1812.     /* an ACK is 5+bctr */
  1813.  
  1814.     /* first set x = per packet overhead */
  1815.     if (wslots > 1)            /* Sliding windows */
  1816.       x = (long) (npad+5+bctr);        /* packet only, don't count ack */
  1817.     else                /* Stop-n-wait */
  1818.       x = (long) (npad+5+3+bctr+5+bctr); /* count packet and ack. */
  1819.  
  1820.     /* then set x = packet length ** 2 */
  1821.     x = x * ( ffc / (long) numerrs);    /* careful of overflow */
  1822.  
  1823.     /* calculate the long integer sqrt(x) quickly */
  1824.     q = 500;
  1825.     q = (q + x/q) >> 1;
  1826.     q = (q + x/q) >> 1;
  1827.     q = (q + x/q) >> 1;
  1828.     q = (q + x/q) >> 1;        /* should converge in about 4 steps */
  1829.     if ((q > 94) && (q < 130))    /* break-even point for long packets */
  1830.       q = 94;
  1831.     if (q > spmax) q = spmax;    /* maximum bounds */
  1832.     if (q < 10) q = 10;        /* minimum bounds */
  1833.     spsiz = q;            /* set new send packet size */
  1834.     debug(F101,"rcalcpsiz","",q);
  1835. #else
  1836. /* New way */
  1837.     debug(F101,"rcalcpsiz numerrs","",numerrs);
  1838.     debug(F101,"rcalcpsiz spsiz","",spsiz);
  1839.     if (spackets < 3) {
  1840.     numerrs = 0;
  1841.     return;
  1842.     }
  1843.     if (numerrs)
  1844.       spsiz = spsiz / 2;
  1845.     else
  1846.       spsiz = (spsiz / 4) * 5;
  1847.     if (spsiz < 20) spsiz = 20;
  1848.     if (spsiz > spmax) spsiz = spmax;
  1849.     debug(F101,"rcalcpsiz new spsiz","",spsiz);
  1850.     numerrs = 0;
  1851. #endif /* COMMENT */
  1852. }
  1853. #endif /* NEWDPL */
  1854.  
  1855. /*  R E S E N D  --  Retransmit packet n.  */
  1856.  
  1857. /*
  1858.   Returns 0 or positive on success (the number of retries for packet n).
  1859.   On failure, returns a negative number, and an error message is placed
  1860.   in recpkt.
  1861. */
  1862. int
  1863. resend(n) int n; {            /* Send packet n again. */
  1864.     int j, k, x;
  1865. #ifdef GFTIMER
  1866.     CKFLOAT t1 = 0.0, t2 = 0.0;
  1867. #endif /* GFTIMER */
  1868.  
  1869.     debug(F101,"resend seq","",n);
  1870.  
  1871.     k = chkwin(n,winlo,wslots);        /* See if packet in current window */
  1872.     j = -1;                /* Assume it's lost */
  1873.     if (k == 0) j = sseqtbl[n];        /* See if we still have a copy of it */
  1874.     if (k != 0 || j < 0) {        /* If not.... */
  1875.     if (nakstate && k == 1) {
  1876. /*
  1877.   Packet n is in the previous window and we are the file receiver.
  1878.   We already sent the ACK and deallocated its buffer so we can't just
  1879.   retransmit the ACK.  Rather than give up, we try some tricks...
  1880. */
  1881.         if (n == 0 && spackets < 63 && myinit[0]) { /* ACK to Send-Init */
  1882. /*
  1883.   If the packet number is 0, and we're at the beginning of a protocol
  1884.   operation (spackets < 63), then we have to resend the ACK to an I or S
  1885.   packet, complete with parameters in the data field.  So we take a chance and
  1886.   send a copy of the parameters in an ACK packet with block check type 1.
  1887. */
  1888.         int bctlsav;        /* Temporary storage */
  1889.         int bctusav;
  1890.         bctlsav = bctl;        /* Save current block check length */
  1891.         bctusav = bctu;        /* and type */
  1892.         bctu = bctl = 1;    /* Set block check to 1 */
  1893.         x = spack('Y',0,(int)strlen((char *)myinit),(CHAR *)myinit);
  1894.         if (x < 0) return(x);
  1895.         logpkt('#',n,(CHAR *)"<reconstructed>",0); /* Log it */
  1896.         bctu = bctusav;        /* Restore block check type */
  1897.         bctl = bctlsav;        /* and length */
  1898.  
  1899.         } else {            /* Not the first packet */
  1900. /*
  1901.   It's not the first packet of the protocol operation.  It's some other packet
  1902.   that we have already ACK'd and forgotten about.  So we take a chance and
  1903.   send an empty ACK using the current block-check type.  Usually this will
  1904.   work out OK (like when acking Data packets), and no great harm will be done
  1905.   if it was some other kind of packet (F, etc).  If we are requesting an
  1906.   interruption of the file transfer, the flags are still set, so we'll catch
  1907.   up on the next packet.
  1908. */
  1909.         x = spack('Y',n,0,(CHAR *) "");
  1910.         if (x < 0) return(x);
  1911.         }
  1912.         retrans++;
  1913.         xxscreen(SCR_PT,'%',(long)pktnum,"Retransmission");
  1914.         return(0);
  1915.     } else {
  1916. /*
  1917.   Packet number is not in current or previous window.  We seem to hit this
  1918.   code occasionally at the beginning of a transaction, for apparently no good
  1919.   reason.  Let's just log it for debugging, send nothing, and try to proceed
  1920.   with the protocol rather than killing it.
  1921. */
  1922.         debug(F101,"resend PKT NOT IN WINDOW","",n);
  1923.         debug(F101,"resend k","",k);
  1924.         return(0);
  1925.     }
  1926.     }
  1927.  
  1928. /* OK, it's in the window and it's not lost. */
  1929.  
  1930.     debug(F101,"resend pktinfo index","",k);
  1931.  
  1932.     if (maxtry > 0 && s_pkt[j].pk_rtr++ > maxtry) { /* Over retry limit */
  1933.     xitsta |= what;
  1934.     return(-1);
  1935.     }
  1936.     debug(F101,"resend retry","",s_pkt[j].pk_rtr); /* OK so far */
  1937.     dumpsbuf();                /* (debugging) */
  1938.     if (s_pkt[j].pk_typ == ' ') {    /* Incompletely formed packet */
  1939.     if (nakstate) {            /* (This shouldn't happen any more) */
  1940.         nack(n);
  1941.         retrans++;
  1942.         xxscreen(SCR_PT,'%',(long)pktnum,"(resend)");
  1943.         return(s_pkt[j].pk_rtr);
  1944.     } else {            /* No packet to resend! */
  1945. #ifdef COMMENT
  1946. /*
  1947.   This happened (once) while sending a file with 2 window slots and typing
  1948.   X to the sender to cancel the file.  But since we're cancelling anyway,
  1949.   there's no need to give a scary message.
  1950. */
  1951.         sprintf((char *)epktmsg,
  1952.             "resend logic error: NPS, n=%d, j=%d.",n,j);
  1953.         return(-2);
  1954. #else
  1955. /* Just ignore it. */
  1956.         return(0);
  1957. #endif /* COMMENT */
  1958.     }
  1959.     }
  1960. #ifdef DEBUG
  1961. #ifdef GFTIMER
  1962.     if (deblog) t1 = gftimer();
  1963. #endif /* GFTIMER */
  1964. #endif /* DEBUG */
  1965.  
  1966.     /* Everything ok, send the packet */
  1967. #ifdef CK_TIMERS
  1968.     if (timint > 0)
  1969.       srttbl[n] = gtimer();        /* Update the timer */
  1970. #endif /* CK_TIMERS */
  1971.     x = ttol(s_pkt[j].pk_adr,s_pkt[j].pk_len);
  1972.  
  1973. #ifdef DEBUG
  1974. #ifdef GFTIMER
  1975.     if (deblog)  {
  1976.     t2 = gftimer();
  1977.     debug(F101,"resend ttol msec","",(long)((t2-t1)*1000.0));
  1978.     }
  1979. #endif /* GFTIMER */
  1980. #endif /* DEBUG */
  1981.     debug(F101,"resend ttol returns","",x);
  1982.  
  1983.     retrans++;                /* Count a retransmission */
  1984.     xxscreen(SCR_PT,'%',(long)pktnum,"(resend)"); /* Tell user about resend */
  1985.     logpkt('S',n,s_pkt[j].pk_adr, s_pkt[j].pk_len); /* Log the resent packet */
  1986.     return(s_pkt[j].pk_rtr);        /* Return the number of retries. */
  1987. }
  1988.  
  1989. /*  E R R P K T  --  Send an Error Packet  */
  1990.  
  1991. int
  1992. errpkt(reason) CHAR *reason; {        /* ...containing the reason given */
  1993.     extern int rtimo, state, justone;
  1994.     int x, y;
  1995.     czseen = 1;                /* Also cancels batch */
  1996.     state = 0;                /* Reset protocol state */
  1997.     debug(F110,"errpkt",reason,0);
  1998.     tlog(F110,"Protocol Error:",(char *)reason,0L);
  1999.     xxscreen(SCR_EM,0,0L,reason);
  2000.     encstr(reason);
  2001.     x = spack('E',pktnum,size,data);
  2002.     ckstrncpy((char *)epktmsg,(char *)reason,PKTMSGLEN);
  2003.     y = quiet; quiet = 1; epktsent = 1;    /* Close files silently. */
  2004.     clsif(); clsof(1);
  2005.     quiet = y;
  2006. /*
  2007.   I just sent an E-packet.  I'm in local mode, I was receiving a file,
  2008.   I'm not a server, and sliding windows are in use.  Therefore, there are
  2009.   likely to be a bunch of packets already "in the pipe" on their way to me
  2010.   by the time the remote sender gets the E-packet.  So the next time I
  2011.   CONNECT or try to start another protocol operation, I am likely to become
  2012.   terribly confused by torrents of incoming material.  To prevent this,
  2013.   the following code soaks up packets from the connection until there is an
  2014.   error or timeout, without wasting too much time waiting.
  2015.  
  2016.   Exactly the same problem occurs when I am in remote mode or if I am
  2017.   in server mode with the justone flag set.  In remote mode not only
  2018.   does the packet data potentially get echo'd back to the sender which
  2019.   is confusing to the user in CONNECT mode, but it also may result in the
  2020.   host performing bizarre actions such as suspending the process if ^Z is
  2021.   unprefixed, etc.
  2022.  
  2023.   Furthermore, thousands of packets bytes in the data stream prevent the
  2024.   client from being able to process Telnet Kermit Option negotiations
  2025.   properly.
  2026. */
  2027. #ifdef STREAMING
  2028.     /* Because streaming sets the timeout to 0... */
  2029.     if (streaming) {
  2030.     timint = rcvtimo = rtimo;
  2031.     streaming = 0;
  2032.     }
  2033. #endif /* STREAMING */
  2034.     if (what & W_RECV &&
  2035.         (!server || (server && justone)) &&
  2036.         (wslots > 1
  2037. #ifdef STREAMING
  2038.      || streaming
  2039. #endif /* STREAMING */
  2040.      )) {
  2041. #ifdef GFTIMER
  2042.     CKFLOAT oldsec, sec = (CKFLOAT) 0.0;
  2043. #else
  2044.     int oldsec, sec = 0;
  2045. #endif /* GFTIMER */
  2046.     debug(F101,"errpkt draining","",wslots);
  2047.     xxscreen(SCR_ST,ST_MSG,0l,"Draining incoming packets, wait...");
  2048.     while (x > -1) {        /* Don't bother if no connection */
  2049.         oldsec = sec;
  2050. #ifdef GFTIMER
  2051.         sec = gftimer();
  2052.         if (oldsec != (CKFLOAT) 0.0)
  2053.           timint = rcvtimo = (int) (sec - oldsec + 0.5);
  2054. #else
  2055.         sec = gtimer();
  2056.         if (oldsec != 0)
  2057.           timint = rcvtimo = sec - oldsec + 1;
  2058. #endif /* GFTIMER */
  2059.         if (timint < 1)
  2060.           timint = rcvtimo = 1;
  2061.         msleep(50);            /* Allow a bit of slop */
  2062.         x = rpack();        /* Read a packet */
  2063.         if (x == 'T' || x == 'z')    /* Timed out means we're done */
  2064.           break;
  2065.         xxscreen(SCR_PT,x,rsn,"");    /* Let user know */
  2066.     }
  2067.     xxscreen(SCR_ST,ST_MSG,0l,"Drain complete.");
  2068.     }
  2069.     if ((x = (what & W_KERMIT)))
  2070.       xitsta |= x;            /* Remember what failed. */
  2071.     success = 0;
  2072.     return(y);
  2073. }
  2074.  
  2075. /* scmd()  --  Send a packet of the given type */
  2076.  
  2077. int
  2078. #ifdef CK_ANSIC
  2079. scmd(char t, CHAR *dat)
  2080. #else
  2081. scmd(t,dat) char t; CHAR *dat;
  2082. #endif /* CK_ANSIC */
  2083. /* scmd */ {
  2084.     int x;
  2085.     extern char * srimsg;
  2086.     debug(F000,"scmd",dat,t);
  2087.     if (encstr(dat) < 0) {        /* Encode the command string */
  2088.     srimsg = "String too long";
  2089.     return(-1);
  2090.     }
  2091.     x = spack(t,pktnum,size,data);
  2092.     debug(F101,"scmd spack","",x);
  2093.     return(x);
  2094. }
  2095.  
  2096. /* Compose and Send GET packet */
  2097.  
  2098. struct opktparm {            /* O-Packet item list */
  2099.     CHAR * opktitem;
  2100.     struct opktparm * opktnext;
  2101. };
  2102.  
  2103. struct opktparm * opkthead = NULL;    /* Linked list of O-packet fields */
  2104. int opktcnt = 0;            /* O-Packet counter */
  2105. char * srimsg = NULL;            /* GET-Packet error message */
  2106.  
  2107. /* S O P K T  --  Send O-Packet */
  2108. /*
  2109.   Sends one O-Packet each time called, using first-fit method of filling
  2110.   the packet from linked list of parameters pointed to by opkthead.
  2111.   To be called repeatedly until list is empty or there is an error.
  2112.   Returns:
  2113.    -1 on failure.
  2114.     0 on success and no more fields left to send.
  2115.     1 on success but with more fields left to be sent.
  2116. */
  2117.  
  2118. int
  2119. sopkt() {
  2120.     int n = 0;                /* Field number in this packet */
  2121.     int rc = 0;                /* Return code */
  2122.     int len = 0;            /* Data field length */
  2123.     char c = NUL;
  2124.     struct opktparm * o = NULL;
  2125.     struct opktparm * t = NULL;
  2126.     struct opktparm * prev = NULL;
  2127.     CHAR * dsave = data;
  2128.     int x, ssave = spsiz;
  2129.  
  2130.     srimsg = NULL;            /* Error message */
  2131.     o = opkthead;            /* Point to head of list */
  2132.     if (!o) {                /* Oops, no list... */
  2133.     srimsg = "GET Packet Internal Error 1";
  2134.     debug(F100,"sopkt NULL list","",0);
  2135.     return(-1);
  2136.     }
  2137.     while (o) {                /* Go thru linked list... */
  2138.     c = *(o->opktitem);        /* Parameter code */
  2139.     debug(F000,"sopkt",o->opktitem,c);
  2140.     x = encstr((CHAR *)o->opktitem);
  2141.     debug(F111,"sopkt encstr",dsave,x);
  2142.     if (x < 0) {            /* Encode this item */
  2143.         if (n == 0) {        /* Failure, first field in packet */
  2144.         debug(F100,"sopkt overflow","",0);
  2145.         spsiz = ssave;        /* Restore these */
  2146.         data = dsave;
  2147.         o = opkthead;        /* Free linked list */
  2148.         while (o) {
  2149.             if (o->opktitem) free(o->opktitem);
  2150.             t = o->opktnext;
  2151.             free((char *)o);
  2152.             o = t;
  2153.         }
  2154.         opkthead = NULL;
  2155.         srimsg = "GET Packet Too Long for Server";
  2156.         return(-1);        /* Fail */
  2157.         } else {            /* Not first field in packet */
  2158.         debug(F110,"sopkt leftover",o->opktitem,0);
  2159.         prev = o;        /* Make this one the new previous */
  2160.         o = o->opktnext;    /* Get next */
  2161.         c = NUL;        /* So we know we're not done */
  2162.         *data = NUL;        /* Erase any partial encoding */
  2163.         continue;        /* We can try this one again later */
  2164.         }
  2165.     }
  2166.     n++;                /* Encoding was successful */
  2167.     debug(F111,"sopkt field",data,x);
  2168.     len += x;            /* Total data field length */
  2169.     data += x;            /* Set up for next field... */
  2170.     spsiz -= x;
  2171.     free(o->opktitem);        /* Free item just encoded */
  2172.     if (o == opkthead) {        /* If head */
  2173.         opkthead = o->opktnext;    /* Move head to next */
  2174.         free((char *)o);        /* Free this list node */
  2175.         o = opkthead;
  2176.     } else {            /* If not head */
  2177.         o = o->opktnext;        /* Get next */
  2178.         prev->opktnext = o;        /* Link previous to next */
  2179.     }
  2180.     if (c == '@')            /* Loop exit */
  2181.       break;
  2182.     if (!o && !opkthead) {        /* Set up End Of Parameters Field */
  2183.         o = (struct opktparm *)malloc(sizeof(struct opktparm));
  2184.         if (o) {
  2185.         opkthead = o;
  2186.         if (!(o->opktitem = (CHAR *)malloc(3))) {
  2187.             free((char *)o);
  2188.             srimsg = "GET Packet Internal Error 8";
  2189.             return(-1);
  2190.         }
  2191.         ckstrncpy((char *)(o->opktitem), "@ ", 3);
  2192.         debug(F111,"sopkt o->opktitem",o->opktitem,
  2193.               strlen((char *)(o->opktitem)));
  2194.         o->opktnext = NULL;
  2195.         }
  2196.     }
  2197.     }
  2198.     data = dsave;            /* Restore globals */
  2199.     spsiz = ssave;
  2200.     debug(F110,"sopkt data",data,0);
  2201.     debug(F101,"sopkt opktcnt","",opktcnt);
  2202.     if (opktcnt++ > 0) {
  2203.     if (nxtpkt() < 0) {        /* Get next packet number and buffer */
  2204.         srimsg = "GET Packet Internal Error 9";
  2205.         return(-1);
  2206.     }
  2207.     }
  2208.     debug(F101,"sopkt pktnum","",pktnum);
  2209.     rc = spack((char)'O',pktnum,len,data); /* Send O-Packet */
  2210.     debug(F101,"sopkt spack","",rc);
  2211.     if (rc < 0)                /* Failed */
  2212.       srimsg = "Send Packet Failure";    /* Set message */
  2213.     else                /* Succeeded */
  2214.       rc = (c == '@') ? 0 : 1;        /* 1 = come back for more, 0 = done */
  2215.     debug(F101,"sopkt rc","",rc);
  2216.     return(rc);
  2217. }
  2218.  
  2219. /* S R I N I T  --  Send GET packet  */
  2220. /*
  2221.   Sends the appropriate GET-Class packet.
  2222.   Returns:
  2223.   -1 on error
  2224.    0 if packet sent successfully and we can move on to the next state
  2225.    1 if an O-packet was sent OK but more O packets still need to be sent.
  2226. */
  2227. int
  2228. srinit(reget, retrieve, opkt) int reget, retrieve, opkt; {
  2229.     int x = 0, left = 0;
  2230.     extern int oopts, omode;
  2231.     CHAR * p = NULL;
  2232. #ifdef RECURSIVE
  2233.     extern int recursive;
  2234.     debug(F101,"srinit recursive","",recursive);
  2235. #endif /* RECURSIVE */
  2236.     debug(F101,"srinit reget","",reget);
  2237.     debug(F101,"srinit retrieve","",retrieve);
  2238.     debug(F101,"srinit opkt","",opkt);
  2239.     debug(F101,"srinit oopts","",oopts);
  2240.     debug(F101,"srinit omode","",omode);
  2241.     debug(F110,"srinit cmarg",cmarg,0);
  2242.     srimsg = NULL;
  2243.  
  2244.     opktcnt = 0;
  2245.     if (!cmarg) cmarg = "";
  2246.     if (!*cmarg) {
  2247.     srimsg = "GET with no filename";
  2248.     debug(F100,"srinit null cmarg","",0);
  2249.     return(-1);
  2250.     }
  2251.     if (opkt) {                /* Extended GET is totally different */
  2252.     char buf[16];
  2253.     struct opktparm * o = NULL;
  2254.     struct opktparm * prev = NULL;
  2255.  
  2256.         buf[0] = NUL;
  2257.  
  2258.     /* Build O-Packet fields and send (perhaps first) O-Packet */
  2259.  
  2260.     if (oopts > -1) {        /* Write Option flags */
  2261.         o = (struct opktparm *)malloc(sizeof(struct opktparm));
  2262.         if (!o) {
  2263.         srimsg = "GET Packet Internal Error 2";
  2264.         debug(F100,"srinit malloc fail O1","",0);
  2265.         return(-1);
  2266.         }
  2267.         sprintf(buf,"Ox%d",oopts);    /* safe */
  2268.         x = (int) strlen(buf+2);
  2269.         buf[1] = tochar(x);
  2270.         o->opktitem = (CHAR *)malloc(x + 3);
  2271.         if (!o->opktitem) {
  2272.         srimsg = "GET Packet Internal Error 3";
  2273.         debug(F100,"srinit malloc fail O2","",0);
  2274.         return(-1);
  2275.         }
  2276.         ckstrncpy((char *)(o->opktitem),buf,x+3);
  2277.         o->opktnext = NULL;
  2278.         if (!opkthead)
  2279.           opkthead = o;
  2280.         prev = o;
  2281.     }
  2282.     if (omode > -1) {        /* If Xfer Mode specified, write it */
  2283.         o = (struct opktparm *)malloc(sizeof(struct opktparm));
  2284.         if (!o) {
  2285.         srimsg = "GET Packet Internal Error 4";
  2286.         debug(F100,"srinit malloc fail M1","",0);
  2287.         return(-1);
  2288.         }
  2289.         sprintf(buf,"Mx%d",omode);    /* safe */
  2290.         x = (int) strlen(buf+2);
  2291.         buf[1] = tochar(x);
  2292.         o->opktitem = (CHAR *)malloc(x + 3);
  2293.         if (!o->opktitem) {
  2294.         srimsg = "GET Packet Internal Error 5";
  2295.         debug(F100,"srinit malloc fail O2","",0);
  2296.         return(-1);
  2297.         }
  2298.         ckstrncpy((char *)(o->opktitem),buf,x+3);
  2299.         o->opktnext = NULL;
  2300.         if (!opkthead)
  2301.           opkthead = o;
  2302.         else
  2303.           prev->opktnext = o;
  2304.         prev = o;
  2305.     }
  2306.  
  2307.     /* Same deal for oname and opath eventually but not needed now... */
  2308.  
  2309.     x = strlen(cmarg);        /* Now do filename */
  2310.     if (x > spsiz - 4) {
  2311.         srimsg = "GET Packet Too Long for Server";
  2312.         return(-1);
  2313.     }
  2314.     o = (struct opktparm *)malloc(sizeof(struct opktparm));
  2315.     if (!o) {
  2316.         srimsg = "GET Packet Internal Error 6";
  2317.         debug(F100,"srinit malloc fail F1","",0);
  2318.         return(-1);
  2319.     }
  2320.     left = x + 6;
  2321.     o->opktitem = (CHAR *)malloc(left + 1);
  2322.     if (!o->opktitem) {
  2323.         srimsg = "GET Packet Internal Error 7";
  2324.         debug(F100,"srinit malloc fail F2","",0);
  2325.         return(-1);
  2326.     }
  2327.     p = o->opktitem;
  2328.     *p++ = 'F';
  2329.     left--;
  2330.     if (x > 94) {            /* Too long for normal length */
  2331.         *p++ = SYN;            /* Escape length with Ctrl-V */
  2332.         *p++ = tochar(x / 95);
  2333.         *p++ = tochar(x % 95);
  2334.         left -= 3;
  2335.     } else {            /* Normal encoding for 94 or less */
  2336.         *p++ = tochar(x);
  2337.         left--;
  2338.     }
  2339.     ckstrncpy((char *)p,cmarg,left); /* Copy the filename */
  2340.     o->opktnext = NULL;
  2341.     if (!opkthead)
  2342.       opkthead = o;
  2343.     else
  2344.       prev->opktnext = o;
  2345.     prev = o;
  2346.  
  2347.     /* End of Parameters */
  2348.  
  2349.     prev->opktnext = NULL;        /* End of list. */
  2350.     return(sopkt());
  2351.     }
  2352.  
  2353.     /* Not Extended GET */
  2354.  
  2355.     if (encstr((CHAR *)cmarg) < 0) {    /* Encode the filename. */
  2356.     srimsg = "GET Packet Too Long for Server";
  2357.     return(-1);
  2358.     }
  2359.     if (retrieve) {            /* Send the packet. */
  2360. #ifdef RECURSIVE
  2361.     if (recursive)
  2362.       x = spack((char)'W',pktnum,size,data); /* GET /DELETE /RECURSIVE */
  2363.     else
  2364. #endif /* RECURSIVE */
  2365.       x = spack((char)'H',pktnum,size,data); /* GET /DELETE */
  2366.     }
  2367. #ifdef RECURSIVE
  2368.     else if (recursive)
  2369.       x = spack((char)'V',pktnum,size,data); /* GET /RECURSIVE */
  2370. #endif /* RECURSIVE */
  2371.     else
  2372.       x = spack((char)(reget ? 'J' : 'R'),pktnum,size,data); /* GET */
  2373.     if (x < 0)
  2374.       srimsg = "Send Packet Failure";
  2375.     return(x < 0 ? x : 0);
  2376. }
  2377.  
  2378.  
  2379. /*  K S T A R T  --  Checks for a Kermit packet while in terminal mode.  */
  2380.  
  2381. /*  (or command mode...)  */
  2382.  
  2383. #ifdef CK_AUTODL
  2384. int
  2385. #ifdef CK_ANSIC
  2386. kstart(CHAR ch)
  2387. #else
  2388. kstart(ch) CHAR ch;
  2389. #endif /* CK_ANSIC */
  2390. /* kstart */ {
  2391.     static CHAR * p = NULL;
  2392.  
  2393. #ifdef OS2
  2394.     static CHAR * pk = NULL;
  2395. #endif /* OS2 */
  2396.     ch &= 0177;                /* Strip 8th bit */
  2397.  
  2398.     /* Because we're in cooked mode at the command prompt... */
  2399.  
  2400.     if (ch == LF) {
  2401.     debug(F110,"kstart","ch == LF",0);
  2402.     if ((what == W_COMMAND || what == W_INIT || what == W_NOTHING)) {
  2403.         if (eol == CR) {
  2404.         ch = eol;
  2405.         debug(F110,"kstart","ch = CR",0);
  2406.         }
  2407.     }
  2408.     }
  2409.  
  2410. #ifdef OS2
  2411.     if (adl_kmode == ADL_STR) {
  2412.     if (!ch)
  2413.       return(0);
  2414.     if (!pk)
  2415.       pk = adl_kstr;
  2416.  
  2417.     if (ch == *pk) {
  2418.         pk++;
  2419.         if (*pk == '\0') {
  2420.         pk = adl_kstr;
  2421.         debug(F100, "kstart Kermit Start String","",0);
  2422.         return(PROTO_K + 1);
  2423.         }
  2424.     } else
  2425.       pk = adl_kstr;
  2426.     }
  2427. #endif /* OS2 */
  2428.  
  2429.     if (ch == stchr) {            /* Start of packet */
  2430.         if (what == W_COMMAND)
  2431.       kstartactive = 1;
  2432.     p = ksbuf;
  2433.     *p = ch;
  2434.     debug(F101,"kstart SOP","",ch);
  2435.     } else if (ch == eol) {        /* End of packet */
  2436.     kstartactive = 0;
  2437.     if (p) {
  2438.         debug(F101,"kstart EOL","",ch);
  2439.         p++;
  2440.         if (p - ksbuf < 94 ) {
  2441.         int rc = 0;
  2442.         *p++ = ch;
  2443.         *p = NUL;
  2444.         rc = chkspkt((char *)ksbuf);
  2445.         debug(F111,"kstart EOP chkspkt", ksbuf, rc);
  2446.         p = NULL;
  2447.         if (!rc) return(0);
  2448.         if (rc == 2) rc = -1;
  2449.         debug(F111,"kstart ksbuf",ksbuf,rc);
  2450.         return(rc);
  2451.         } else {
  2452.         debug(F110,"kstart","p - ksbuf >= 94",0);
  2453.         p = NULL;
  2454.         }
  2455.     }
  2456.     } else if (p) {
  2457.     if (ch < SP)
  2458.       kstartactive = 0;
  2459.     p++;
  2460.     if (p - ksbuf < 94) {
  2461.         *p = ch;
  2462.     } else {
  2463.         p = NULL;
  2464.         debug(F110,"kstart","p - ksbuf >= 94",0);
  2465.     }
  2466.     }
  2467.     return(0);
  2468. }
  2469.  
  2470. #ifdef CK_XYZ
  2471.  
  2472. /*  Z S T A R T  --  Checks for a ZMODEM packet while in terminal mode.  */
  2473.  
  2474. int
  2475. #ifdef CK_ANSIC
  2476. zstart(CHAR ch)
  2477. #else
  2478. zstart(ch) CHAR ch;
  2479. #endif /* CK_ANSIC */
  2480. /* zstart */ {
  2481.     static CHAR * matchstr = (CHAR *) "\030B00";
  2482.     /* "rz\r**\030B00000000000000\r\033J\021"; */
  2483.     static CHAR * p = NULL;
  2484.     extern int inserver;
  2485.  
  2486.     if (inserver)
  2487.       return(0);
  2488.  
  2489.     if (!ch)
  2490.       return(0);
  2491.     if (!p) {
  2492. #ifdef OS2
  2493.     p = adl_zmode == ADL_PACK ? matchstr : adl_zstr;
  2494. #else
  2495.     p = matchstr;
  2496. #endif /* OS2 */
  2497.     }
  2498.     if (ch == *p) {
  2499.     p++;
  2500.     if (*p == '\0') {
  2501. #ifdef OS2
  2502.         if (adl_zmode == ADL_PACK) {
  2503.         p = matchstr;
  2504.         debug(F100, "zstart Zmodem SOP","",0);
  2505.         } else {
  2506.         p = adl_zstr;
  2507.         debug(F100, "zstart Zmodem Start String","",0);
  2508.         }
  2509. #else
  2510.         p = matchstr;
  2511.         debug(F100, "zstart Zmodem SOP","",0);
  2512. #endif /* OS2 */
  2513.         return(PROTO_Z + 1);
  2514.     }
  2515.     } else {
  2516. #ifdef OS2
  2517.     p = adl_zmode == ADL_PACK ? matchstr : adl_zstr;
  2518. #else
  2519.     p = matchstr;
  2520. #endif /* OS2 */
  2521.     }
  2522.     return(0);
  2523. }
  2524. #endif /* CK_XYZ */
  2525.  
  2526. #ifndef NOICP
  2527. #ifdef CK_APC
  2528. /*  A U T O D O W N  */
  2529.  
  2530. #ifdef CK_ANSIC
  2531. VOID
  2532. autodown(int ch)
  2533. #else
  2534. VOID
  2535. autodown(ch) int ch;
  2536. #endif /* CK_ANSIC */
  2537. /* autodown */ {
  2538.  
  2539. /* The Kermit and Zmodem Auto-download calls go here */
  2540.  
  2541.     extern int justone;            /* From protocol module */
  2542.     extern int debses, protocol, apcactive, autodl, inautodl;
  2543. #ifdef DCMDBUF
  2544.     extern char *apcbuf;
  2545. #else
  2546.     extern char apcbuf[];
  2547. #endif /* DCMDBUF */
  2548. #ifdef OS2
  2549.     extern int apclength, term_io;
  2550. #endif /* OS2 */
  2551.     int k = 0;
  2552.  
  2553.     if ((autodl || inautodl
  2554. #ifdef IKS_OPTION
  2555.      || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  2556. #endif /* IKS_OPTION */
  2557.      ) && !debses) {
  2558. #ifdef CK_XYZ
  2559. #ifdef XYZ_INTERNAL
  2560.     extern int p_avail;
  2561. #else
  2562.     int p_avail = 1;
  2563. #endif /* XYZ_INTERNAL */
  2564.     if (p_avail && zstart((CHAR) ch)) {
  2565.         debug(F100, "Zmodem download","",0);
  2566. #ifdef OS2
  2567. #ifndef NOTERM
  2568.             apc_command(APC_LOCAL,"receive /protocol:zmodem");
  2569. #endif /* NOTERM */
  2570. #else /* OS2 */
  2571.             ckstrncpy(apcbuf,"receive /protocol:zmodem",APCBUFLEN);
  2572.         apcactive = APC_LOCAL;
  2573. #endif /* OS2 */
  2574.         return;
  2575.     }
  2576. #endif /* CK_XYZ */
  2577.  
  2578.     /* First try... */
  2579.     k = kstart((CHAR) ch);
  2580.     if (
  2581. #ifdef NOSERVER
  2582.         k > 0
  2583. #else /* NOSERVER */
  2584.         k
  2585. #endif /* NOSERVER */
  2586.         ) {                /* We saw a valid S or I packet */
  2587.         if (k < 0) {        /* Stuff RECEIVE into APC buffer */
  2588.         justone = 1;
  2589.         switch (protocol) {
  2590. #ifdef CK_XYZ
  2591.           case PROTO_G:
  2592.             ckstrncpy(apcbuf,
  2593.                   "set proto kermit, server, set protocol g",
  2594.                   APCBUFLEN
  2595.                   );
  2596.             break;
  2597.           case PROTO_X:
  2598.             ckstrncpy(apcbuf,
  2599.                   "set proto kermit,server,set proto xmodem",
  2600.                   APCBUFLEN
  2601.                   );
  2602.             break;
  2603.                   case PROTO_XC:
  2604.             ckstrncpy(apcbuf,
  2605.                "set proto kermit,server,set proto xmodem-crc",
  2606.                   APCBUFLEN
  2607.                   );
  2608.                       break;
  2609.           case PROTO_Y:
  2610.             ckstrncpy(apcbuf,
  2611.                   "set proto kermit,server, set protocol y",
  2612.                   APCBUFLEN
  2613.                   );
  2614.             break;
  2615.           case PROTO_Z:
  2616.             ckstrncpy(apcbuf,
  2617.                   "set proto kermit,server,set proto zmodem",
  2618.                   APCBUFLEN
  2619.                   );
  2620.             break;
  2621. #endif /* CK_XYZ */
  2622.           case PROTO_K:
  2623.             ckstrncpy(apcbuf,"server",APCBUFLEN);
  2624.             break;
  2625.         }
  2626.         } else {
  2627.         justone = 0;
  2628.                 ckstrncpy(apcbuf,"receive /protocol:kermit",APCBUFLEN);
  2629.         }
  2630. #ifdef OS2
  2631. #ifndef NOTERM
  2632.             apc_command(APC_LOCAL,apcbuf);
  2633. #endif /* NOTERM */
  2634. #else /* OS2 */
  2635.             ckstrncpy(apcbuf,"receive /protocol:zmodem",APCBUFLEN);
  2636.         apcactive = APC_LOCAL;
  2637. #endif /* OS2 */
  2638.         return;
  2639.     }
  2640.     }
  2641. }
  2642. #endif /* CK_APC */
  2643. #endif /* NOICP */
  2644.  
  2645. /*  C H K S P K T  --  Check if buf contains a valid S or I packet  */
  2646.  
  2647. int
  2648. chkspkt(buf) char *buf; {
  2649.     int buflen;
  2650.     int len = -1;
  2651.     CHAR chk;
  2652.     char type = 0;
  2653.     char *s = buf;
  2654.  
  2655.     if (!buf) return(0);
  2656.     buflen = strlen(buf);
  2657.     if (buflen < 5) return(0);        /* Too short */
  2658.     if (*s++ != stchr) return(0);    /* SOH */
  2659.     len = xunchar(*s++);        /* Length */
  2660.     if (len < 0) return(0);
  2661.     if (*s++ != SP) return(0);        /* Sequence number */
  2662.     type = *s++;            /* Type */
  2663.     if (type != 'S' && type != 'I')
  2664.       return(0);
  2665.     if (buflen < len + 2) return(0);
  2666.     s += (len - 3);            /* Position of checksum */
  2667.     chk = (CHAR) (*s);            /* Checksum */
  2668.     *s = NUL;
  2669.     if (xunchar(chk) != chk1((CHAR *)(buf+1),buflen-2)) /* Check it */
  2670.       return(0);
  2671.     *s = chk;
  2672.     return(type == 'S' ? 1 : 2);
  2673. }
  2674. #endif /* CK_AUTODL */
  2675.  
  2676. /* R P A C K  --  Read a Packet */
  2677.  
  2678. /*
  2679.   rpack reads a packet and returns the packet type, or else Q if the
  2680.   packet was invalid, or T if a timeout occurred.  Upon successful return,
  2681.   sets the values of global rsn (received sequence number),  rln (received
  2682.   data length), and rdatap (pointer to null-terminated data field), and
  2683.   returns the packet type.  NOTE: This is an inner-loop function so must be
  2684.   efficient.  Protect function calls by if-tests where possible, e.g.
  2685.   "if (pktlog) logpkt(...);".
  2686. */
  2687. int
  2688. rpack() {
  2689.     register int i, j, x, lp;        /* Local variables */
  2690. #ifdef CKTUNING
  2691.     unsigned int chk;
  2692. #endif /* CKTUNING */
  2693.     int k, type, chklen;
  2694.     unsigned crc;
  2695.     CHAR pbc[5];            /* Packet block check */
  2696.     CHAR *sohp;                /* Pointer to SOH */
  2697.     CHAR e;                /* Packet end character */
  2698.  
  2699. #ifdef GFTIMER
  2700.     CKFLOAT t1 = 0.0, t2 = 0.0;
  2701. #endif /* GFTIMER */
  2702.  
  2703.     debug(F101,"rpack pktnum","",pktnum);
  2704.  
  2705. #ifndef OLDCHKINT
  2706.     if (chkint() < 0)            /* Check for console interrupts. */
  2707.       return('z');
  2708. #endif /* OLDCHKINT */
  2709.  
  2710.     k = getrbuf();            /* Get a new packet input buffer. */
  2711.     debug(F101,"rpack getrbuf","",k);
  2712.     if (k < 0) {            /* Return like this if none free. */
  2713.     return(-1);
  2714.     }
  2715.     recpkt = r_pkt[k].bf_adr;
  2716.     *recpkt = '\0';            /* Clear receive buffer. */
  2717.     sohp = recpkt;            /* Initialize pointers to it. */
  2718.     rdatap = recpkt;
  2719.     rsn = rln = -1;            /* In case of failure. */
  2720.     e = (turn) ? turnch : eol;        /* Use any handshake char for eol */
  2721.  
  2722. /* Try to get a "line". */
  2723.  
  2724. #ifdef CK_AUTODL
  2725.     debug(F110,"rpack ksbuf",ksbuf,0);
  2726.     if (ksbuf[0]) {            /* Kermit packet already */
  2727.     int x;                /* collected for us in CONNECT mode */
  2728.     CHAR *s1 = recpkt, *s2 = ksbuf;
  2729.     j = 0;
  2730.     while (*s2) {            /* Copy and get length */
  2731.         *s1++ = *s2++;        /* No point optimizing this since */
  2732.         j++;            /* it's never more than ~20 chars */
  2733.     }
  2734.     *s1 = NUL;
  2735. #ifdef PARSENSE
  2736.     x = parchk(recpkt, stchr, j);    /* Check parity */
  2737.     debug(F000,"autodownload parity","",parity);
  2738.     debug(F000,"autodownload parchk","",x);
  2739.     if (x > -1 && parity != x) {
  2740.         autopar = 1;
  2741.         parity = x;
  2742.     }
  2743. #endif /* PARSENSE */
  2744.     ksbuf[0] = NUL;            /* Don't do this next time! */
  2745.  
  2746.     } else {                /* Normally go read a packet */
  2747. #endif /* CK_AUTODL */
  2748.  
  2749. #ifdef DEBUG
  2750.     if (deblog) {
  2751.         debug(F101,"rpack timint","",timint);
  2752.         debug(F101,"rpack rcvtimo","",rcvtimo);
  2753. #ifdef STREAMING
  2754.         debug(F101,"rpack streaming","",streaming);
  2755. #endif /* STREAMING */
  2756. #ifdef GFTIMER
  2757.         /* Measure how long it takes to read a packet */
  2758.         t1 = gftimer();
  2759. #endif /* GFTIMER */
  2760.     }
  2761. #endif /* DEBUG */
  2762.  
  2763. /* JUST IN CASE (otherwise this could clobber streaming) */
  2764.  
  2765.     if ((timint == 0
  2766. #ifdef STREAMING
  2767.          || streaming
  2768. #endif /* STREAMING */
  2769.          ) && (rcvtimo != 0)) {
  2770.         debug(F101,"rpack timint 0 || streaming but rcvtimo","",rcvtimo);
  2771.         rcvtimo = 0;
  2772.     }
  2773.  
  2774. #ifdef PARSENSE
  2775. #ifdef UNIX
  2776. /*
  2777.   So far the final turn argument is only for ck[uvdl]tio.c.  Should be added
  2778.   to the others too.  (turn == handshake character.)
  2779. */
  2780.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr,turn);
  2781. #else
  2782. #ifdef VMS
  2783.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr,turn);
  2784. #else
  2785. #ifdef datageneral
  2786.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr,turn);
  2787. #else
  2788. #ifdef STRATUS
  2789.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr,turn);
  2790. #else
  2791. #ifdef OS2
  2792.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr,turn);
  2793. #else
  2794. #ifdef OSK
  2795.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr,turn);
  2796. #else
  2797.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e,stchr);
  2798. #endif /* OSK */
  2799. #endif /* OS2 */
  2800. #endif /* STRATUS */
  2801. #endif /* datageneral */
  2802. #endif /* VMS */
  2803. #endif /* UNIX */
  2804.     if (parity != 0 && parity != 's' && ttprty != 0) {
  2805.         if (parity != ttprty) autopar = 1;
  2806.         parity = ttprty;
  2807.     }
  2808. #else /* !PARSENSE */
  2809.     j = ttinl(recpkt,r_pkt[k].bf_len - 1,rcvtimo,e);
  2810. #endif /* PARSENSE */
  2811.  
  2812. #ifdef DEBUG
  2813.     if (deblog)  {
  2814.         debug(F101,"rpack ttinl len","",j);
  2815. #ifdef GFTIMER
  2816.         t2 = gftimer();
  2817.         debug(F101,"rpack ttinl msec","",(long)((t2-t1)*1000.0));
  2818. #endif /* GFTIMER */
  2819.     }
  2820. #endif /* DEBUG */
  2821.  
  2822. #ifdef STREAMING
  2823.     if (streaming && sndtyp == 'D' && j == 0)
  2824.         return('Y');
  2825. #endif /* STREAMING */
  2826.  
  2827.     if (j < 0) {
  2828.         /* -1 == timeout, -2 == ^C, -3 == connection lost or fatal i/o */
  2829.         debug(F101,"rpack: ttinl fails","",j); /* Otherwise, */
  2830.         freerbuf(k);        /* Free this buffer */
  2831.         if (j < -1) {        /* Bail out if ^C^C typed. */
  2832.         if (j == -2) {
  2833.             interrupted = 1;
  2834.             debug(F101,"rpack ^C server","",server);
  2835.             debug(F101,"rpack ^C en_fin","",en_fin);
  2836.         } else if (j == -3) {
  2837.             fatalio = 1;
  2838.             debug(F101,"rpack fatalio","",en_fin);
  2839.         }
  2840.         return(j);
  2841.         }
  2842.         if (nakstate)        /* j == -1 is a read timeout */
  2843.           xxscreen(SCR_PT,'T',(long)winlo,"");
  2844.         else
  2845.           xxscreen(SCR_PT,'T',(long)pktnum,"");
  2846.         logpkt('r',-1,(CHAR *)"<timeout>",0);
  2847.         if (flow == 1) ttoc(XON);    /* In case of Xoff blockage. */
  2848.         return('T');
  2849.     }
  2850. #ifdef CK_AUTODL
  2851.     }
  2852. #endif /* CK_AUTODL */
  2853.  
  2854.     rpktl = j;
  2855.     tlci += j;                /* All OK, Count the characters. */
  2856.     flci += j;
  2857.  
  2858. /* Find start of packet */
  2859.  
  2860. #ifndef PARSENSE
  2861.     for (i = 0; (recpkt[i] != stchr) && (i < j); i++)
  2862.       sohp++;                /* Find mark */
  2863.     if (i++ >= j) {            /* Didn't find it. */
  2864.     logpkt('r',-1,"<timeout>",0);
  2865.     freerbuf(k);
  2866.     return('T');
  2867.     }
  2868. #else
  2869.     i = 1;                /* ttinl does this for us */
  2870. #endif /* PARSENSE */
  2871.  
  2872.     rpackets++;                /* Count received packet. */
  2873.     lp = i;                /* Remember LEN position. */
  2874.     if ((j = xunchar(recpkt[i++])) == 0) { /* Get packet length.  */
  2875.         if ((j = lp+5) > MAXRP) {    /* Long packet */
  2876.         return('Q');        /* Too long */
  2877.     }
  2878.  
  2879. #ifdef CKTUNING
  2880.     /* Save some function-call and loop overhead... */
  2881. #ifdef COMMENT
  2882.     /* ttinl() already removed parity */
  2883.     if (parity)
  2884. #endif /* COMMENT */
  2885.       chk = (unsigned) ((unsigned) recpkt[i-1] +
  2886.                 (unsigned) recpkt[i]   +
  2887.                 (unsigned) recpkt[i+1] +
  2888.                 (unsigned) recpkt[i+2] +
  2889.                 (unsigned) recpkt[i+3]
  2890.                 );
  2891. #ifdef COMMENT
  2892.     else
  2893.       chk = (unsigned) ((unsigned) (recpkt[i-1] & 077) +
  2894.                 (unsigned) (recpkt[i]   & 077) +
  2895.                 (unsigned) (recpkt[i+1] & 077) +
  2896.                 (unsigned) (recpkt[i+2] & 077) +
  2897.                 (unsigned) (recpkt[i+3] & 077)
  2898.                 );
  2899. #endif /* COMMENT */
  2900.     if (xunchar(recpkt[j]) != ((((chk & 0300) >> 6) + chk) & 077))
  2901. #else
  2902.     x = recpkt[j];            /* Header checksum. */
  2903.     recpkt[j] = '\0';        /* Calculate & compare. */
  2904.     if (xunchar(x) != chk1(recpkt+lp,5))
  2905. #endif /* CKTUNING */
  2906.       {
  2907.           freerbuf(k);
  2908.           logpkt('r',-1,(CHAR *)"<crunched:hdr>",0);
  2909.           xxscreen(SCR_PT,'%',(long)pktnum,"Bad packet header");
  2910.           return('Q');
  2911.       }
  2912. #ifndef CKTUNING
  2913.     recpkt[j] = x;            /* Checksum ok, put it back. */
  2914. #endif /* CKTUNING */
  2915.     rln = xunchar(recpkt[j-2]) * 95 + xunchar(recpkt[j-1]) - bctl;
  2916.     j = 3;                /* Data offset. */
  2917.     } else if (j < 3) {
  2918.     debug(F101,"rpack packet length less than 3","",j);
  2919.     freerbuf(k);
  2920.     logpkt('r',-1,(CHAR *)"<crunched:len>",0);
  2921.     xxscreen(SCR_PT,'%',(long)pktnum,"Bad packet length");
  2922.     return('Q');
  2923.     } else {
  2924.     rln = j - bctl - 2;        /* Regular packet */
  2925.     j = 0;                /* No extended header */
  2926.     }
  2927.     rsn = xunchar(recpkt[i++]);        /* Sequence number */
  2928.     if (pktlog)                /* Save a function call! */
  2929.       logpkt('r',rsn,sohp,rln+bctl+j+4);
  2930.     if (rsn < 0 || rsn > 63) {
  2931.     debug(F101,"rpack bad sequence number","",rsn);
  2932.     freerbuf(k);
  2933.     if (pktlog)
  2934.       logpkt('r',rsn,(CHAR *)"<crunched:seq>",0);
  2935.     xxscreen(SCR_PT,'%',(long)pktnum,"Bad sequence number");
  2936.     return('Q');
  2937.     }
  2938. /*
  2939.   If this packet has the same type as the packet just sent, assume it is
  2940.   an echo and ignore it.  Don't even bother with the block check calculation:
  2941.   even if the packet is corrupted, we don't want to NAK an echoed packet.
  2942.   Nor must we NAK an ACK or NAK.
  2943. */
  2944.     type = recpkt[i++];            /* Get packet's TYPE field */
  2945.     if (type == sndtyp || (nakstate && (type == 'N' /* || type == 'Y' */ ))) {
  2946.     debug(F000,"rpack echo","",type); /* If it's an echo */
  2947.     freerbuf(k);            /* Free this buffer */
  2948.     logpkt('#',rsn,(CHAR *)"<echo:ignored>",0);
  2949.     return('e');            /* Return special (lowercase) code */
  2950.     }
  2951. /*
  2952.   Separate the data from the block check, accounting for the case where
  2953.   a packet was retransmitted after the block check switched.
  2954. */
  2955.     if (type == 'I' || type == 'S') {    /* I & S packets always have type 1 */
  2956.     chklen = 1;
  2957.     rln = rln + bctl - 1;
  2958.     } else if (type == 'N') {        /* A NAK packet never has data */
  2959.     chklen = xunchar(recpkt[lp]) - 2;
  2960.     rln = rln + bctl - chklen;
  2961.     } else chklen = bctl;
  2962. #ifdef DEBUG
  2963.     if (deblog) {            /* Save 2 function calls */
  2964.     debug(F101,"rpack bctl","",bctl);
  2965.     debug(F101,"rpack chklen","",chklen);
  2966.     }
  2967. #endif /* DEBUG */
  2968.     i += j;                /* Buffer index of DATA field */
  2969.     rdatap = recpkt+i;            /* Pointer to DATA field */
  2970.     if ((j = rln + i) > r_pkt[k].bf_len) { /* Make sure it fits */
  2971.     debug(F101,"packet too long","",j);
  2972.     freerbuf(k);
  2973.     logpkt('r',rsn,(CHAR *)"<overflow>",0);
  2974.     return('Q');
  2975.     }
  2976.     for (x = 0; x < chklen; x++)    /* Copy the block check */
  2977.       pbc[x] = recpkt[j+x];        /* 3 bytes at most. */
  2978.     pbc[x] = '\0';            /* Null-terminate block check string */
  2979.     recpkt[j] = '\0';            /* and the packet Data field. */
  2980.  
  2981.     if (chklen == 2 && bctu == 4) {    /* Adjust for Blank-Free-2 */
  2982.     chklen = 4;            /* (chklen is now a misnomer...) */
  2983.     debug(F100,"rpack block check B","",0);
  2984.     }
  2985.     switch (chklen) {            /* Check the block check */
  2986.       case 1:                /* Type 1, 6-bit checksum */
  2987.     if (xunchar(*pbc) != chk1(recpkt+lp,j-lp)) {
  2988. #ifdef DEBUG
  2989.         if (deblog) {
  2990.         debug(F110,"checked chars",recpkt+lp,0);
  2991.         debug(F101,"block check (1)","",(int) xunchar(*pbc));
  2992.         debug(F101,"should be (1)","",chk1(recpkt+lp,j-lp));
  2993.         }
  2994. #endif /* DEBUG */
  2995.         freerbuf(k);
  2996.         logpkt('r',-1,(CHAR *)"<crunched:chk1>",0);
  2997.         xxscreen(SCR_PT,'%',(long)pktnum,"Checksum error");
  2998.         return('Q');
  2999.     }
  3000.     break;
  3001.       case 2:                /* Type 2, 12-bit checksum */
  3002.     x = xunchar(*pbc) << 6 | xunchar(pbc[1]);
  3003.     if (x != chk2(recpkt+lp,j-lp)) { /* No match */
  3004.         if (type == 'E') {        /* Allow E packets to have type 1 */
  3005.         recpkt[j++] = pbc[0];
  3006.         recpkt[j] = '\0';
  3007.         if (xunchar(pbc[1]) == chk1(recpkt+lp,j-lp))
  3008.           break;
  3009.         else
  3010.           recpkt[--j] = '\0';
  3011.         }
  3012. #ifdef DEBUG
  3013.         if (deblog) {
  3014.         debug(F110,"checked chars",recpkt+lp,0);
  3015.         debug(F101,"block check (2)","", x);
  3016.         debug(F101,"should be (2)","", (int) chk2(recpkt+lp,j-lp));
  3017.         }
  3018. #endif /* DEBUG */
  3019.         freerbuf(k);
  3020.         logpkt('r',-1,(CHAR *)"<crunched:chk2>",0);
  3021.         xxscreen(SCR_PT,'%',(long)pktnum,"Checksum error");
  3022.         return('Q');
  3023.     }
  3024.     break;
  3025.       case 3:                /* Type 3, 16-bit CRC */
  3026.     crc = (xunchar(pbc[0]) << 12)
  3027.         | (xunchar(pbc[1]) << 6)
  3028.         | (xunchar(pbc[2]));
  3029.     if (crc != chk3(recpkt+lp,j-lp)) {
  3030.         if (type == 'E') {        /* Allow E packets to have type 1 */
  3031.         recpkt[j++] = pbc[0];
  3032.         recpkt[j++] = pbc[1];
  3033.         recpkt[j] = '\0';
  3034.         if (xunchar(pbc[2]) == chk1(recpkt+lp,j-lp))
  3035.           break;
  3036.         else { j -=2; recpkt[j] = '\0'; }
  3037.         }
  3038. #ifdef DEBUG
  3039.         if (deblog) {
  3040.         debug(F110,"checked chars",recpkt+lp,0);
  3041.         debug(F101,"block check (3)","",crc);
  3042.         debug(F101,"should be (3)","",(int) chk3(recpkt+lp,j-lp));
  3043.         }
  3044. #endif /* DEBUG */
  3045.         freerbuf(k);
  3046.         logpkt('r',-1,(CHAR *)"<crunched:chk3>",0);
  3047.         xxscreen(SCR_PT,'%',(long)pktnum,"CRC error");
  3048.         return('Q');
  3049.     }
  3050.     break;
  3051.       case 4:                /* Type 4 = Type 2, no blanks. */
  3052.     x = (unsigned)((xunchar(*pbc) - 1) << 6) |
  3053.       (unsigned)(xunchar(pbc[1]) - 1);
  3054.     if (x != chk2(recpkt+lp,j-lp)) {
  3055.         if (type == 'E') {    /* Allow E packets to have type 1 */
  3056.         recpkt[j++] = pbc[0];
  3057.         recpkt[j] = '\0';
  3058.         if (xunchar(pbc[1]) == chk1(recpkt+lp,j-lp))
  3059.           break;
  3060.         else
  3061.           recpkt[--j] = '\0';
  3062.         }
  3063.         debug(F101,"bad type B block check","",x);
  3064.         freerbuf(k);
  3065.         logpkt('r',-1,(CHAR *)"<crunched:chkb>",0);
  3066.         xxscreen(SCR_PT,'%',(long)pktnum,"Checksum error");
  3067.         return('Q');
  3068.     }
  3069.     break;
  3070.       default:            /* Shouldn't happen... */
  3071.     freerbuf(k);
  3072.     logpkt('r',-1,(CHAR *)"<crunched:chkx>",0);
  3073.     xxscreen(SCR_PT,'%',(long)pktnum,"(crunched)");
  3074.     return('Q');
  3075.     }
  3076.     debug(F101,"rpack block check OK","",rsn);
  3077.  
  3078. /* Now we can believe the sequence number, and other fields. */
  3079. /* Here we violate strict principles of layering, etc, and look at the  */
  3080. /* packet sequence number.  If there's already a packet with the same   */
  3081. /* number in the window, we remove this one so that the window will not */
  3082. /* fill up. */
  3083.  
  3084.     if ((x = rseqtbl[rsn]) != -1) {    /* Already a packet with this number */
  3085.     retrans++;            /* Count it for statistics */
  3086.     debug(F101,"rpack got dup","",rsn);
  3087.     logpkt('r',rsn,(CHAR *)"<duplicate>",0);
  3088.     freerbuf(x);            /* Free old buffer, keep new packet. */
  3089.     r_pkt[k].pk_rtr++;        /* Count this as a retransmission. */
  3090.     }
  3091.  
  3092. /* New packet, not seen before, enter it into the receive window. */
  3093.  
  3094. #ifdef CK_TIMERS
  3095.     if (timint > 0)
  3096.       rrttbl[rsn] = gtimer();        /* Timestamp */
  3097. #endif /* CK_TIMERS */
  3098.  
  3099.     rseqtbl[rsn] = k;            /* Make back pointer */
  3100.     r_pkt[k].pk_seq = rsn;        /* Record in packet info structure */
  3101.     r_pkt[k].pk_typ = type;        /* Sequence, type,... */
  3102.     r_pkt[k].pk_adr = rdatap;        /* pointer to data buffer */
  3103.     if (local) {            /* Save a function call! */
  3104.     int x = 0;
  3105.     if (fdispla != XYFD_N) x = 1;
  3106.     if (fdispla == XYFD_B && (type == 'D' || sndtyp == 'D')) x = 0;
  3107.     if (x)                /* Update screen */
  3108.       xxscreen(SCR_PT,(char)type,(long)rsn,(char *)sohp);
  3109.     }
  3110.     return(type);            /* Return packet type */
  3111. }
  3112.  
  3113. /*  L O G P K T  --  Log packet number n, pointed to by s.  */
  3114.  
  3115. /* c = 's' (send) or 'r' (receive) */
  3116.  
  3117. VOID
  3118. #ifdef CK_ANSIC
  3119. logpkt(char c,int n, CHAR *s, int len)
  3120. #else
  3121. logpkt(c,n,s,len) char c; int n; CHAR *s; int len;
  3122. #endif /* CK_ANSIC */
  3123. /* logpkt */ {
  3124.     char plog[20];
  3125.     if (!s) s = (CHAR *)"";
  3126.     if (pktlog) if (chkfn(ZPFILE) > 0) {
  3127.     if (n < 0)            /* Construct entry header */
  3128.       sprintf(plog,"%c-xx-%02d-",c,(gtimer()%60)); /* safe */
  3129.     else
  3130.       sprintf(plog,"%c-%02d-%02d-",c,n,(gtimer()%60)); /* safe */
  3131.     if (zsoutx(ZPFILE,plog,(int)strlen(plog)) < 0) {
  3132.         pktlog = 0;
  3133.         return;
  3134.     } else {
  3135.         if (len == 0)
  3136.           len = strlen((char *)s);
  3137.         if (len > 0) {
  3138.         char * p;        /* Make SOP printable */
  3139.         int x;            /* so we can look at logs without */
  3140.         p = dbchr(*s);        /* triggering autodownload. */
  3141.         x = strlen(dbchr(*s));
  3142.         if (*s < 32 || (*s > 127 && *s < 160)) {
  3143.             if (zsoutx(ZPFILE,p,x) < 0) {
  3144.             pktlog = 0;
  3145.             return;
  3146.             } else {
  3147.             len--;
  3148.             s++;
  3149.             }
  3150.         }
  3151.         }
  3152.         if (zsoutx(ZPFILE,(char *)s,len) < 0) {
  3153.         pktlog = 0;
  3154.         return;
  3155.         } else if (zsoutx(ZPFILE,
  3156. #ifdef UNIX
  3157.                   "\n", 1
  3158. #else
  3159. #ifdef datageneral
  3160.                   "\n", 1
  3161. #else
  3162. #ifdef OSK
  3163.                   "\r", 1
  3164. #else
  3165. #ifdef MAC
  3166.                   "\r", 1
  3167. #else
  3168.                   "\015\012", 2
  3169. #endif /* MAC */
  3170. #endif /* OSK */
  3171. #endif /* datageneral */
  3172. #endif /* UNIX */
  3173.                   ) < 0) {
  3174.         pktlog = 0;
  3175.         }
  3176.     }
  3177.     }
  3178. }
  3179.  
  3180. /*  T S T A T S  --  Record statistics in transaction log  */
  3181.  
  3182. VOID
  3183. tstats() {
  3184.     char *tp = NULL;
  3185. #ifdef GFTIMER
  3186.     CKFLOAT xx;                /* Elapsed time divisor */
  3187. #endif /* GFTIMER */
  3188.  
  3189.     debug(F101,"tstats xfsecs","",xfsecs);
  3190.     debug(F101,"tstats filcnt","",filcnt);
  3191.     if (filcnt == 1) {            /* Get timing for statistics */
  3192.     tsecs = xfsecs;            /* Single file, we already have it */
  3193. #ifdef GFTIMER
  3194.     debug(F101,"tstats fpxfsecs","",(int)fpxfsecs);
  3195.     fptsecs = fpxfsecs;
  3196. #endif /* GFTIMER */
  3197.     } else {                /* Multiple files */
  3198.     tsecs = gtimer();        /* Get current time */
  3199. #ifdef GFTIMER
  3200.     fptsecs = gftimer();
  3201. #endif /* GFTIMER */
  3202.     }
  3203. #ifdef GFTIMER
  3204.     if (fptsecs <= GFMINTIME)        /* Calculate CPS */
  3205.       fptsecs = (CKFLOAT) GFMINTIME;
  3206.     debug(F101,"tstats fptsecs","",(int)fptsecs);
  3207.     xx = (CKFLOAT) tfc / fptsecs;
  3208.     if (sizeof(long) <= 4) {        /* doesn't account for 16-bit longs */
  3209.     if (xx  > 2147483647.0)
  3210.       tfcps = 2147483647L;            /* 31 bits */
  3211.     else
  3212.       tfcps = (long) xx;
  3213.     } else
  3214.       tfcps = (long) xx;
  3215. #else
  3216.     if (tsecs < 2L)
  3217.       tsecs = 1L;
  3218.     debug(F101,"tstats tsecs","",tsecs);
  3219.     tfcps = tfc / tsecs;
  3220. #endif /* GFTIMER */
  3221.  
  3222.     ztime(&tp);                /* Get time stamp */
  3223.     tlog(F100,"","",0L);        /* Leave a blank line */
  3224.     tlog(F110,"Transaction complete",tp,0L);  /* Record it */
  3225.  
  3226.     if (filcnt < 1) return;        /* If no files, done. */
  3227.  
  3228. /* If multiple files, record character totals for all files */
  3229.  
  3230.     if (filcnt > 1) {
  3231.     tlog(F101," files transferred       ","",filcnt - filrej);
  3232.     tlog(F101," total file characters   ","",tfc);
  3233.     tlog(F101," communication line in   ","",tlci);
  3234.     tlog(F101," communication line out  ","",tlco);
  3235.     }
  3236.  
  3237. /* Record timing info for one or more files */
  3238.  
  3239. #ifdef GFTIMER
  3240.     if (filcnt - filrej == 1) {
  3241.     tlog(F101," elapsed time (seconds)  ","",(long) fpxfsecs);
  3242.     tlog(F101," effective data rate     ","",filcps);
  3243.     } else {
  3244.     tlog(F101," elapsed time (seconds)  ","",(long) fptsecs);
  3245.     tlog(F101," effective data rate     ","",(long) xx);
  3246.     }
  3247. #else
  3248.     tlog(F101," elapsed time (seconds)  ","",(long) tsecs);
  3249.     if (tsecs > 0) {
  3250.     long lx;
  3251.     lx = (tfc * 10L) / (long) tsecs;
  3252.     tlog(F101," effective data rate     ","",lx/10L);
  3253.     }
  3254. #endif /* GFTIMER */
  3255.     tlog(F100,"","",0L);        /* Leave a blank line */
  3256. }
  3257.  
  3258. /*  F S T A T S  --  Record file statistics in transaction log  */
  3259.  
  3260. VOID
  3261. fcps() {
  3262. #ifdef GFTIMER
  3263.     double xx;
  3264.     fpxfsecs = gftimer() - fpfsecs;
  3265.     if (fpxfsecs <= GFMINTIME)
  3266.       fpxfsecs = (CKFLOAT) GFMINTIME;
  3267.     xx = (CKFLOAT) ffc / fpxfsecs;
  3268.     if (sizeof(long) <= 4) {
  3269.     if (xx  > 2147483647.0)
  3270.       tfcps = 2147483647L;        /* 31 bits */
  3271.     else
  3272.       filcps = (long) xx;
  3273.     } else
  3274.       filcps = (long) xx;
  3275.     if (sizeof(int) >= 4)
  3276.       xfsecs = (int) fpxfsecs;
  3277.     else if (fpxfsecs < 32768.0)
  3278.       xfsecs = (int) fpxfsecs;
  3279.     else
  3280.       xfsecs = 32767;
  3281. #else /* GFTIMER */
  3282.     xfsecs = gtimer() - fsecs;
  3283.     if (xfsecs < 1L) xfsecs = 1L;
  3284.     filcps = ffc / xfsecs;
  3285. #endif /* GFTIMER */
  3286. }
  3287.  
  3288. VOID
  3289. fstats() {
  3290.     tfc += ffc;
  3291. #ifdef DEBUG
  3292.     if (deblog) {
  3293.     debug(F101,"fstats tfc","",tfc);
  3294.     debug(F101,"fstats what","",what);
  3295.     debug(F110,"fstats epktmsg",epktmsg,0);
  3296.     }
  3297. #endif /* DEBUG */
  3298. #ifdef TLOG
  3299.     if (!discard && !cxseen && !czseen && what != W_NOTHING && !*epktmsg)
  3300.       tlog(F101," complete, size","",ffc);
  3301. #endif /* TLOG */
  3302. }
  3303.  
  3304. #endif /* NOXFER */
  3305.