-
Notifications
You must be signed in to change notification settings - Fork 4
/
ckcfn2.c
1199 lines (1041 loc) · 32.2 KB
/
ckcfn2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* C K C F N 2 -- System-independent protocol support functions */
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2021, 2022, 2023 Jeffrey H. Johnson <[email protected]>
*
* Copyright (c) 1981-2011,
* Trustees of Columbia University in the City of New York.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* - Neither the name of Columbia University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ckcdeb.h"
#include "ckcker.h"
#ifdef __linux__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif /* ifdef __linux__ */
extern int spsiz, spmax, rpsiz, timint;
extern int npad, ebq, ebqflg, rpt;
extern int rptq, rptflg, capas, spsizf;
extern int pktnum, prvpkt, sndtyp, bctr;
extern int bctu, rsn, rln, maxtry;
extern int size, maxsize, spktl, nfils;
extern int stdouf, warn, timef, parity;
extern int speed, turn, turnch, delay;
extern int displa, pktlog, tralog, seslog;
extern int xflg, mypadn, deblog, hcflg;
extern int binary, fncnv, local;
#ifndef NOSERVER
extern int server;
#endif /* ifndef NOSERVER */
extern int cxseen, czseen, nakstate, quiet;
extern long filcnt, ffc, flci, flco;
extern long tlci, tlco, tfc, fsize;
extern char *cmarg, *cmarg2, **cmlist;
extern CHAR padch, mypadc, eol, seol;
extern CHAR ctlq, myctlq, sstate, *hlptxt;
extern CHAR filnam[], sndpkt[], recpkt[], data[];
extern CHAR srvcmd[], *srvptr, stchr, mystch;
extern CHAR *rdatap;
void rcalcpsz();
void nack();
void nxtpkt();
void nack();
void resend();
int numerrs = 0; /* Total number of packet errors so far */
char *strcpy(); /* Forward declarations */
unsigned int chk2(); /* of non-int functions */
unsigned int chk3();
CHAR dopar();
static CHAR partab[] = { /* Even parity table for dopar() */
'\000', '\201', '\202', '\003', '\204', '\005', '\006',
'\207', '\210', '\011', '\012', '\213', '\014', '\215',
'\216', '\017', '\220', '\021', '\022', '\223', '\024',
'\225', '\226', '\027', '\030', '\231', '\232', '\033',
'\234', '\035', '\036', '\237', '\240', '\041', '\042',
'\243', '\044', '\245', '\246', '\047', '\050', '\251',
'\252', '\053', '\254', '\055', '\056', '\257', '\060',
'\261', '\262', '\063', '\264', '\065', '\066', '\267',
'\270', '\071', '\072', '\273', '\074', '\275', '\276',
'\077', '\300', '\101', '\102', '\303', '\104', '\305',
'\306', '\107', '\110', '\311', '\312', '\113', '\314',
'\115', '\116', '\317', '\120', '\321', '\322', '\123',
'\324', '\125', '\126', '\327', '\330', '\131', '\132',
'\333', '\134', '\335', '\336', '\137', '\140', '\341',
'\342', '\143', '\344', '\145', '\146', '\347', '\350',
'\151', '\152', '\353', '\154', '\355', '\356', '\157',
'\360', '\161', '\162', '\363', '\164', '\365', '\366',
'\167', '\170', '\371', '\372', '\173', '\374', '\175',
'\176', '\377'
};
/*
* CRC generation
* tables
*/
static unsigned int crcta[16] = {
0, 010201, 020402, 030603, 041004, 051205, 061406, 071607,
0102010, 0112211, 0122412, 0132613, 0143014, 0153215, 0163416, 0173617
};
static unsigned int crctb[16] = {
0, 010611, 021422, 031233, 043044, 053655, 062466, 072277,
0106110, 0116701, 0127532, 0137323, 0145154, 0155745, 0164576, 0174367
};
/* I N P U T -- Attempt to read packet number 'pktnum' */
/*
* This is the function that feeds input to Kermit's finite state machine.
* If a special start state is in effect, that state is returned as if
* it were the type of an incoming packet.
*
* Otherwise:
*
* - If the desired packet arrives within MAXTRY tries, return its type,
* with its data stored in the global 'data' array.
*
* - If the previous packet arrives again, resend the last packet and
* wait for another to come in.
*
* - If the desired packet does not arrive within MAXTRY tries, return
* indicating that an error packet should be sent.
*/
int
kinput()
{
int type, numtry;
debug(F101, "input sstate", "", sstate);
if (sstate != 0) /* If a start state is in effect, */
{
type = sstate; /* return it like a packet type, */
sstate = 0; /* and then nullify it. */
numerrs = 0; /* (PWP) no errors so far */
return ( type );
}
else
{
type = rpack(); /* Else, try to read a packet. */
}
debug(F111, "input", rdatap, type);
/*
* If it's the same packet we
* just sent, it's an echo.
* Read another.
*/
if (type == sndtyp)
{
type = rpack();
}
#ifndef NOICP
chkint(); /* Check for console interrupts. */
#endif /* ifndef NOICP */
debug(F101, " nakstate", "", nakstate);
debug(F000, " type", "=", type);
debug(F101, " rsn", "", rsn);
debug(F101, " prvpkt", "", prvpkt);
/*
* If previous packet again,
* a timeout pseudopacket,
* or a bad packet, try again.
*/
for (numtry = 0;
( rsn == prvpkt || type == 'T' || type == 'Q' || type == 'N' );
numtry++)
{
debug(F101, "input() try", "", numtry);
debug(F101, " nakstate", "", nakstate);
debug(F000, " type", "=", type);
debug(F101, " rsn", "", rsn);
debug(F101, " prvpkt", "", prvpkt);
if (numtry > maxtry) /* If too many tries, give up */
{
strcpy(recpkt, "Timed out."); /* and send a timeout error packet, */
rdatap = recpkt; /* and pretend we read one. */
return ( 'E' );
}
if (type == 'E') /* Got error packet. */
{
break; /* Don't even bother about seq no */
}
else if (( type == 'N' ) && ( rsn == (( pktnum + 1 ) & 63 )))
{
type = 'Y'; /* NAK for next packet */
break; /* is ACK for current. */
}
else if (( rsn == prvpkt ) && ( type == 'Y' ))
{
; /* (Nothing) Another ACK for previous */
}
else if (( nakstate != 0 ) && ( type == 'Q' || type == 'T' ))
{
nack(); /* Send NAK if trashed or timed out */
}
else
{
resend(); /* Else, just send last packet again */
numerrs++; /* and count another error */
if (spktl && !spsizf)
{
rcalcpsz(); /* recalc optimal packet size */
}
}
if (sstate != 0) /* If an interrupt routine has set */
{
type = sstate; /* sstate behind our back, return */
sstate = 0; /* that. */
*data = '\0';
return ( type );
}
else
{
type = rpack(); /* Else try to read a packet. */
}
#ifndef NOICP
chkint(); /* Look again */
#endif /* ifndef NOICP */
if (type == sndtyp) /* for interruptions */
{
type = rpack(); /* and for echoes. */
}
}
ttflui(); /* Got it, now, clear buffer */
if (spktl && !spsizf && !( rsn & 007 )) /* should we recalc pack len? */
{
rcalcpsz(); /* (PWP) recalc every 8 packets */
}
return ( type ); /* Success, return packet type. */
}
/* D O P A R -- Add an appropriate parity bit to a character */
CHAR
dopar(ch)
register CHAR ch;
{
register unsigned int a;
if (!parity)
{
return ( ch & 255 );
}
else
{
a = ch & 127;
}
switch (parity)
{
case 'e':
return ( partab[a] ); /* Even */
case 'm':
return ( a | 128 ); /* Mark */
case 'o':
return ( partab[a] ^ 128 ); /* Odd */
case 's':
return ( a ); /* Space */
default:
return ( a );
}
}
/* S P A C K -- Construct and send a packet */
/*
* spack() sends a packet of the given type, sequence number n, with
* len data characters pointed to by d, in either a regular or extended-
* length packet, depending on length. Returns the number of bytes
* actually sent, or else -1 upon failure. Uses global npad, padch,
* mystch, bctu. Leaves packet in null-terminated global sndpkt[] array
* for later retransmission. Updates global sndpktl (send-packet length).
*/
int
spack(type, n, len, d)
char type;
int n;
register int len;
register char *d;
{
register int i;
int j, lp;
register CHAR *sohp = sndpkt;
register CHAR *cp;
unsigned crc;
spktl = 0;
for (i = 0; i < npad; sndpkt[i++] = padch)
{
sohp++; /* Do any requested padding */
}
sndpkt[i++] = mystch; /* MARK */
lp = i++; /* Position of LEN, fill in later */
sndpkt[i++] = tochar(n); /* SEQ field */
sndpkt[i++] = sndtyp = type; /* TYPE field */
j = len + bctu; /* Length of data + block check */
if (j + 2 > MAXPACK) /* Long packet? */
{
sndpkt[lp] = tochar(0); /* Yes, set LEN to zero */
sndpkt[i++] = tochar(j / 95); /* High part */
sndpkt[i++] = tochar(j % 95); /* Low part */
sndpkt[i] = '\0'; /* Header checksum */
sndpkt[i++] = \
tochar(chk1(sndpkt + lp));
}
else
{
sndpkt[lp] = tochar(j + 2); /* Normal LEN */
}
for (cp = &sndpkt[i];
len-- > 0;
i++)
{
*cp++ = *d++; /* Packet data */
}
sndpkt[i] = '\0'; /* Null-terminate */
switch (bctu) /* Block check */
{
case 1: /* 1 = 6-bit chksum */
sndpkt[i++] = \
tochar(chk1(sndpkt + lp));
break;
case 2: /* 2 = 12-bit chksum */
j = chk2(sndpkt + lp);
sndpkt[i++] = \
(unsigned)tochar(( j >> 6 ) & 077);
sndpkt[i++] = \
(unsigned)tochar(j & 077);
break;
case 3: /* 3 = 16-bit CRC */
crc = chk3(sndpkt + lp);
sndpkt[i++] = \
(unsigned)tochar((( crc & 0170000 )) >> 12);
sndpkt[i++] = \
(unsigned)tochar(( crc >> 6 ) & 077);
sndpkt[i++] = \
(unsigned)tochar(crc & 077);
break;
}
sndpkt[i++] = seol; /* End of line (packet terminator) */
sndpkt[i] = '\0'; /* Terminate string */
/*
* Add the parity
* quickly at the end
*/
switch (parity)
{
case 'e': /* Even */
for (cp = &sndpkt[i - 1];
cp >= sndpkt;
cp--)
{
*cp = partab[(int)*cp];
}
break;
case 'm': /* Mark */
for (
cp = &sndpkt[i - 1];
cp >= sndpkt;
cp--)
{
*cp = *cp | 128;
}
break;
case 'o': /* Odd */
for (cp = &sndpkt[i - 1];
cp >= sndpkt;
cp--)
{
*cp = \
partab[(int)*cp] ^ 128;
}
break;
}
if (ttol(sndpkt, i) < 0)
{
return ( -1 ); /* Send the packet */
}
spktl = i; /* Remember packet length */
flco += spktl; /* Count the characters */
tlco += spktl;
#ifndef NOLOGS
if (pktlog) /* If logging packets, log it */
{
zsout(ZPFILE, "s-");
if (*sndpkt)
{
zsoutl(ZPFILE, sndpkt);
}
else
{
zsoutl(ZPFILE, sohp);
}
}
#endif /* ifndef NOLOGS */
screen(SCR_PT, type, (long)n, sohp); /* Update screen */
return ( i ); /* Return length */
}
/* C H K 1 -- Compute a type-1 Kermit 6-bit checksum. */
int
chk1(pkt)
register CHAR *pkt;
{
register unsigned int chk;
chk = chk2(pkt);
chk = \
((( chk & 0300 ) >> 6 ) + chk ) & 077;
return ( chk );
}
/* C H K 2 -- Compute the numeric sum of all the bytes in the packet. */
unsigned int
chk2(pkt)
register CHAR *pkt;
{
register long chk;
register unsigned int m;
m = ( parity ) ? 0177 : 0377;
for (chk = 0;
*pkt != '\0';
pkt++)
{
chk += *pkt & m;
}
return ( chk & 07777 );
}
/* C H K 3 -- Compute a type-3 Kermit block check. */
/*
* Calculate the 16-bit CRC-CCITT of a null-terminated
* string using a lookup table. Assumes the argument
* string contains no embedded nulls.
*/
unsigned int
chk3(pkt)
register CHAR *pkt;
{
register LONG c, crc;
register unsigned int m;
m = ( parity ) ? 0177 : 0377;
for (crc = 0;
*pkt != '\0';
pkt++)
{
c = ( *pkt & m ) ^ crc;
crc = \
( crc >> 8 ) ^ \
( crcta[( c & 0xF0 ) >> 4] ^ \
crctb[c & 0x0F] );
}
return ( crc & 0xFFFF );
}
/*
* Functions for sending
* various kinds of packets
*/
void
ack() /* Send an ordinary acknowledgment. */
{
spack('Y', pktnum, 0, ""); /* No data. */
nxtpkt(&pktnum); /* Increment the packet number. */
} /* Note, only call this once! */
void
ack1(s)
char *s;
{ /* Send an ACK with data. */
spack('Y', pktnum, strlen(s), s); /* Send the packet. */
nxtpkt(&pktnum); /* Increment the packet number. */
} /* Only call this once! */
void
nack()
{ /* Negative acknowledgment. */
spack('N', pktnum, 0, ""); /* NAK's never have data. */
}
/*
* Recalculate the optimal packet length in the face of errors. This is a
* modified version of the algorithm by John Chandler in Kermit/370; see
* "Dynamic Packet Size Control", Kermit News, V2 #1, June 1988.
*
* Total chars = file_chars + (header_len * num_packs)
* + (errors * (header_len + packet_len))
*
* Differentiate with respect to number of chars, solve for packet_len:
* packet_len = sqrt (file_chars * header_len / errors)
*/
void
rcalcpsz()
{
register long x, q;
if (numerrs == 0)
{
return; /* Bounds check just in case */
}
/*
* Overhead on a data packet is npad+5+bctr,
* plus 3 if extended packet; an ACK is 5+bctr.
*
* First set x = per packet overhead
*/
#ifdef COMMENT /* Hook for windowing code */
if (window) /* only the packet, */
{
x = (long)( npad + 5 + bctr ); /* don't count the ack */
}
else
#endif /* ifdef COMMENT */
x = (long)( npad + 5 + 3 + bctr + 5 + bctr );
/*
* Then,
* set x = packet length ** 2
*/
x = x * ((long)ffc / (long)numerrs ); /* careful of overflow */
/*
* Calculate the long
* integer sqrt(x) quickly
*/
q = 500;
q = ( q + x / q ) >> 1;
q = ( q + x / q ) >> 1;
q = ( q + x / q ) >> 1;
q = ( q + x / q ) >> 1; /* should converge in ~4 steps */
if (( q > 94 ) && ( q < 130 )) /* breakeven point for long pkts */
{
q = 94;
}
if (q > spmax)
{
q = spmax; /* maximum bounds */
}
if (q < 10)
{
q = 10; /* minimum bounds */
}
spsiz = q; /* set new send packet size */
}
void
resend()
{ /* Send the old packet again. */
if (spktl) /* If buffer has something, */
{
if (ttol(sndpkt, spktl) < 1) /* resend it, */
{
nack();
}
}
else
{
nack(); /* otherwise send a NAK. */
}
#ifdef DEBUG
debug(F111, "resend", sndpkt, spktl);
#endif /* ifdef DEBUG */
screen(SCR_PT, '%',
(long)pktnum, "(resend)"); /* Say resend occurred */
#ifndef NOLOGS
if (pktlog)
{
zsout(ZPFILE, "s-");
zsoutl(ZPFILE, "(resend)"); /* Log packet if desired */
}
#endif /* ifndef NOLOGS */
}
void
errpkt(reason)
char *reason;
{ /* Send an error packet. */
int x;
encstr(reason);
spack('E', pktnum, size, data);
x = quiet;
quiet = 1; /* Close files silently. */
clsif();
clsof(1);
quiet = x;
screen(SCR_TC, 0, 0l, "");
}
void
scmd(t, dat)
char t, *dat;
{ /* Send packet of the given type */
encstr(dat); /* Encode the command string */
spack(t, pktnum, size, data);
}
void
srinit()
{ /* Send R (GET) packet */
encstr(cmarg); /* Encode the filename. */
spack('R', pktnum, size, data); /* Send the packet. */
}
void
nxtpkt(num)
int *num;
{
prvpkt = *num; /* Save previous */
*num = ( *num + 1 ) % 64; /* Increment packet number mod 64 */
}
int
sigint(
#ifdef DEBUG
sig,
code
#endif /* ifdef DEBUG */
)
#ifdef DEBUG
int sig, code;
#endif /* ifdef DEBUG */
{ /* Terminal interrupt handler */
if (local)
{
errpkt("User typed ^C");
}
#ifdef DEBUG
debug(F101, "sigint() caught signal", "", sig);
debug(F101, " code", "", code);
#endif /* ifdef DEBUG */
doexit(GOOD_EXIT); /* Exit program */
return ( GOOD_EXIT );
}
/* R P A C K -- Read a Packet */
/*
* rpack reads a packet and returns the packet type, or else Q if
* the packet was invalid, or T if a timeout occurred. Upon successful
* return, sets the values of global rsn (received sequence number),
* rln (received data length), and rdatap (pointer to null-terminated
* data field).
*/
int
rpack()
{
register int i, j, x, try, type, lp; /* Local variables */
unsigned crc;
CHAR pbc[4]; /* Packet block check */
CHAR *sohp = recpkt; /* Pointer to SOH */
CHAR e; /* Packet end character */
rsn = rln = -1; /* In case of failure */
*recpkt = '\0'; /* Clear receive buffer */
rdatap = recpkt; /* Initialize this */
e = ( turn ) ? turnch : eol; /* Use any handshake char for eol */
/*
* Try several times to get a "line".
* This allows for hosts that echo our
* normal CR packet terminator as CRLF.
* Don't diagnose CRLF as an invalid packet.
*/
#define TTITRY 5
for (try = 0; try < TTITRY; try++) /* Try to get a "line" */
{
j = ttinl(recpkt, MAXRP + 50, timint, e); /* Add a bit of extra */
if (j < 0)
{
if (j < -1)
{
doexit(BAD_EXIT); /* Bail out if ^C^C typed */
}
debug(F101, "rpack: ttinl fails", "", j);
screen(SCR_PT, 'T', (long)pktnum, "");
return ( 'T' ); /* Otherwise, a timeout */
}
tlci += j; /* All OK */
flci += j; /* Count the characters */
for (i = 0; ( recpkt[i] != stchr ) && ( i < j ); i++)
{
sohp++; /* Find mark */
}
if (i++ < j)
{
break; /* Found it. */
}
}
if (try >= TTITRY)
{
return ( 'Q' ); /* Diagnose bad packet. */
}
#ifdef DEBUG
debug(F111, "ttinl", sohp, j); /* Log packet infofmation */
#endif /* ifdef DEBUG */
#ifndef NOLOGS
if (pktlog)
{
zsout(ZPFILE, "r-");
zsoutl(ZPFILE, sohp);
}
#endif /* ifndef NOLOGS */
lp = i; /* Remember LEN position */
if (( j = xunchar(recpkt[i++])) == 0)
{
if (( j = lp + 5 ) > MAXRP)
{
return ( 'Q' ); /* Long packet */
}
x = recpkt[j]; /* Header checksum. */
recpkt[j] = '\0'; /* Calculate & compare. */
if (xunchar(x) != chk1(recpkt + lp))
{
return ( 'Q' );
}
recpkt[j] = x; /* Checksum ok. */
rln = xunchar(recpkt[j - 2]) * 95 + \
xunchar(recpkt[j - 1]) - bctu;
j = 3; /* Data offset. */
}
else if (j < 3)
{
debug(F101,
"rpack packet length less than 3", "", j);
return ( 'Q' );
}
else
{
rln = j - bctu - 2; /* Regular packet */
j = 0; /* No extended header */
}
rsn = xunchar(recpkt[i++]); /* Sequence number */
type = recpkt[i++]; /* Packet type */
i += j; /* Where data begins */
rdatap = recpkt + i; /* The data itself */
if (( j = rln + i ) > MAXRP)
{
debug(F101,
"packet sticks out too far", "", j);
return ( 'Q' ); /* Find block check */
}
for (x = 0; x < bctu; x++) /* Copy it */
{
pbc[x] = recpkt[j + x];
}
pbc[x] = '\0';
recpkt[j] = '\0'; /* Null-terminate data */
switch (bctu) /* Check the block check */
{
case 1:
if (xunchar(*pbc) != chk1(recpkt + lp))
{
debug(F110,
"checked chars", recpkt + lp, 0);
debug(F101,
"block check", "", xunchar(*pbc));
debug(F101,
"should be", "", chk1(recpkt + lp));
return ( 'Q' );
}
break;
case 2:
x = xunchar(*pbc) << 6 | \
xunchar(pbc[1]);
if ((unsigned int)x != \
(unsigned int)chk2(recpkt + lp))
{
debug(F110,
"checked chars", recpkt + lp, 0);
debug(F101,
"block check", "", x);
debug(F101,
"should be", "", chk2(recpkt + lp));
return ( 'Q' );
}
break;
case 3:
crc = \
( xunchar(pbc[0]) << 12 ) | \
( xunchar(pbc[1]) << 6 ) | \
( xunchar(pbc[2]));
if (crc != chk3(recpkt + lp))
{
debug(F110,
"checked chars", recpkt + lp, 0);
debug(F101,
"block check", "", xunchar(*pbc));
debug(F101,
"should be", "", chk3(recpkt + lp));
return ( 'Q' );
}
break;
default:
return ( 'Q' );
}
screen(SCR_PT, type, (long)rsn, sohp); /* Update screen */
return ( type ); /* Return packet type */
}
/*
* Attribute
* Packets
*/
/*
* Call with xp == 0 if we're
* sending a real file (F packet),
* or xp != 0 for screen data (X packet).
* Returns 0 on success, -1 on failure.
*/
int
sattr(xp)
int xp;
{ /* Send Attributes */
int i, j, aln;
struct zattr x;
if (zsattr(&x) < 0)
{
return ( -1 ); /* Get attributes or die */
}
i = 0;
data[i++] = '.'; /* System type */
data[i++] = tochar(x.systemid.len); /* Copy attrib structure */
for (j = 0; j < x.systemid.len; j++)
{
data[i++] = x.systemid.val[j];
}
data[i++] = '"'; /* File type */
if (binary) /* Binary file type */
{
data[i++] = tochar(2); /* Two characters */
data[i++] = 'B'; /* B for Binary */
data[i++] = '8'; /* Assume 8-bit bytes */
}
else /* Text file type */
{
data[i++] = tochar(3); /* Three characters */
data[i++] = 'A'; /* A for ASCII with CRLFs */
data[i++] = 'M'; /* M for carriage return */
data[i++] = 'J'; /* J for linefeed */
}
if (( xp == 0 ) && \
( x.length > -1L )) /* If it's a real file */
{
if (( aln = x.date.len ) > 0) /* Creation date, if any */
{
data[i++] = '#';
data[i++] = tochar(aln);
for (j = 0; j < aln; j++)
{
data[i++] = x.date.val[j];
}
}
data[i] = '!'; /* File length in K */
sprintf(&data[i + 2], "%ld", x.lengthk);
aln = strlen(&data[i + 2]);
data[i + 1] = tochar(aln);
i += aln + 2;
data[i] = '1'; /* File length in bytes */
sprintf(&data[i + 2], "%ld", fsize);
aln = strlen(&data[i + 2]);
data[i + 1] = tochar(aln);
i += aln + 2;
}
data[i] = '\0'; /* Ensure null terminated */
nxtpkt(&pktnum); /* Increment pkt number */
aln = strlen(data);
spack('A', pktnum, aln, data); /* Send Attribute packet */
debug(F111, "sattr", data, aln);
return ( 0 );
}
int
rsattr(s)
char *s;
{ /* Read response to attribute packet */
debug(F111, "rsattr: ", s, *s); /* If it's 'N' followed by anything */
if (*s == 'N')
{
return ( -1 ); /* means other Kermit is refusing. */
}
return ( 0 );
}
int
gattr(s, yy)
char *s;
struct zattr *yy;
{ /* Read incoming attribute packet */
char c;
int aln, i, x;
long l;
#define ABUFL 100 /* Temporary buffer for conversions */
char abuf[ABUFL];
#define FTBUFL 10 /* File type buffer */