ckcfn2.c 98 KB

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