-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckcfn3.c
2606 lines (2384 loc) · 75 KB
/
ckcfn3.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 3 -- Packet buffer management for C-Kermit */
/* (plus assorted functions tacked on at the end) */
/*
Author: Frank da Cruz <fdc@columbia.edu>,
Columbia University Academic Information Systems, New York City.
Copyright (C) 1985, 2010,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
*/
/*
Note -- if you change this file, please amend the version number and date at
the top of ckcfns.c accordingly.
*/
#include "ckcsym.h"
#include "ckcdeb.h"
#include "ckcasc.h"
#include "ckcker.h"
#include "ckcxla.h"
/* C K M K D I R -- Create a directory */
/*
Call with:
int fc = 0 to create, nonzero to remove, a directory.
char * s = pointer to name of directory to create or remove.
char ** r = address of pointer to return name or message.
int m = 1 to print error messages, 0 to be silent.
int cvt = 1 means convert s from standard format to local format;
0 means use s as is.
Returns:
0 on success (directory was created or removed).
-1 when attempt to create the directory failed.
-2 on internal error (e.g. no code for creating directories).
On success, the name is pointed to by p.
On failure, the reason is pointed to by p.
*/
#ifdef CK_MKDIR
static char ckmkdbuf[CKMAXPATH+1];
#else
#ifdef datageneral
static char ckmkdbuf[CKMAXPATH+1];
#endif /* datageneral */
#endif /* CK_MKDIR */
#ifdef CK_MKDIR
int
ckmkdir(fc,s,r,m,cvt) int fc; char * s; char ** r; int m; int cvt; {
int x, rc = -2;
char tmpbuf[CKMAXPATH+1];
char buf2[CKMAXPATH+1];
if (!s) s = "";
debug(F110,"ckmkdir 1 fc",s,fc);
if (!*s) {
ckmakmsg(ckmkdbuf,
CKMAXPATH+1,
(fc == 0) ? "mkdir" : "rmdir",
": no name given",
NULL,
NULL
);
*r = ckmkdbuf;
return(-2);
}
#ifdef datageneral
/* Come back and make this nicer later if anybody notices */
if (fc == 0) { /* mkdir */
rc = createdir(s,0);
} else { /* rmdir */
/* AOS/VS rmdir() is a no-op. */
ckmakmsg(tmpbuf,CKMAXPATH+1,"delete ",s,NULL,NULL);
debug(F110,"ckmkdir 2",tmpbuf,0);
rc = system(tmpbuf);
}
*r = NULL;
#else /* not datageneral */
/* First make sure the name has an acceptable directory-name format */
#ifdef VMS
{
char *p = s;
int lb = 0, rb = 0, sl = 0;
while (*p) {
if (*p == '[' || *p == '<') lb++; /* Count brackets */
else if (*p == ']' || *p == '>') rb++;
else if (*p == '/') sl++; /* and slashes */
p++;
}
if (lb != 1 && rb != 1 && sl == 0 && p > s && *(p-1) != ':') {
/* Probably just a word - convert to VMS format */
ckmakmsg(buf2,
CKMAXPATH+1,
"[",
(*s == '.') ? "" : ".",
s,
"]"
);
s = buf2;
} else if (lb == 0 && rb == 0 && sl != 0 && p > s && *(p-1) != ':') {
int flag = 0;
/* Seems to be in UNIX format */
x = strlen(s);
if (x > 0 && s[x-1] != '/')
flag = 1;
ckmakmsg(buf2,CKMAXPATH+1,s,flag ? "/" : "",NULL,NULL);
s = buf2;
}
if (s == buf2) {
ckstrncpy(tmpbuf,s,CKMAXPATH+1);
s = tmpbuf;
}
debug(F110,"ckmkdir 2+VMS",s,0);
}
#else
#ifdef UNIXOROSK
#ifdef DTILDE
s = tilde_expand(s);
#endif /* DTILDE */
ckstrncpy(tmpbuf,s,CKMAXPATH+1);
s = tmpbuf;
x = strlen(s);
if (x > 0 && s[x-1] != '/') { /* Must end in "/" for zmkdir() */
s[x] = '/';
s[x+1] = NUL;
debug(F110,"ckmkdir 2+UNIXOROSK",s,0);
}
#else /* UNIXOROSK */
#ifdef OS2
ckstrncpy(tmpbuf,s,CKMAXPATH+1);
s = tmpbuf;
x = strlen(s);
if (fc == 0 && x > 0 && s[x-1] != '/') { /* Must end in "/" for zmkdir() */
s[x] = '/';
s[x+1] = NUL;
debug(F110,"ckmkdir 2+OS2",s,0);
}
#endif /* OS2 */
#endif /* UNIXOROSK */
#endif /* VMS */
#ifdef NZLTOR
/* Server is calling us, so convert to local format if necessary */
if (cvt) {
nzrtol(s,(char *)buf2,1,PATH_ABS,CKMAXPATH);
s = buf2;
debug(F110,"ckmkdir 3",s,0);
}
#endif /* NZLTOR */
debug(F110,"ckmkdir 4",s,0);
if (fc == 0) { /* Making */
#ifdef CK_MKDIR
rc = zmkdir(s);
#else
#ifdef NT
rc = _mkdir(s);
#else
rc = mkdir(s,0777);
#endif /* NT */
#endif /* CK_MKDIR */
} else { /* Removing */
#ifdef ZRMDIR
rc = zrmdir(s);
#else
#ifdef NT
rc = _rmdir(s);
#else
#ifdef OSK
rc = -2;
#else
rc = rmdir(s);
#endif /* OSK */
#endif /* NT */
#endif /* ZRMDIR */
}
#endif /* datageneral */
debug(F101,"ckmkdir rc","",rc);
if (rc == -2) {
ckmakmsg(ckmkdbuf,
CKMAXPATH,
"Directory ",
(fc == 0) ? "creation" : "removal",
"not implemented in this version of C-Kermit",
NULL
);
*r = ckmkdbuf;
if (m) printf("%s\n",*r);
} else if (rc < 0) {
if (m) perror(s);
ckmakmsg(ckmkdbuf,CKMAXPATH,s,": ",ck_errstr(),NULL);
*r = ckmkdbuf;
} else if (fc == 0 && zfnqfp(s,CKMAXPATH,ckmkdbuf)) {
*r = ckmkdbuf;
} else if (fc != 0) {
ckmakmsg(ckmkdbuf,CKMAXPATH,s,": removed",NULL,NULL);
*r = ckmkdbuf;
}
return(rc);
}
#endif /* CK_MKDIR */
#ifndef NOXFER /* Rest of this file... */
#ifndef NODISPO
#ifdef pdp11
#define NODISPO
#endif /* pdpd11 */
#endif /* NODISPO */
extern int pipesend;
#ifdef PIPESEND
extern char ** sndfilter;
#endif /* PIPESEND */
extern int unkcs, wmax, wcur, discard, bctu, bctl, local, fdispla, what,
sendmode, opnerr, dest, epktrcvd, epktsent, filestatus, eofmethod, dispos,
fncnv, fnrpath;
extern char * ofn2;
extern char * rfspec, * sfspec, * prfspec, * psfspec, * rrfspec, * prrfspec;
extern char ofn1[];
extern int ofn1x;
extern char * ofperms;
#ifdef VMS
extern int batch;
#else
extern int backgrd;
#endif /* VMS */
extern int xflg, remfile, remappd;
extern CHAR *data;
extern char filnam[];
#ifndef NOFRILLS
extern int rprintf, rmailf; /* REMOTE MAIL, PRINT */
char optbuf[OPTBUFLEN]; /* Options for MAIL or REMOTE PRINT */
#endif /* NOFRILLS */
extern int wslots;
extern int fblksiz, frecl, forg, frecfm, fncact, fncsav, fcctrl, lf_opts;
extern CHAR * srvcmd;
extern int srvcmdlen;
extern int binary, spsiz;
extern int pktnum, cxseen, czseen, nfils, stdinf;
extern int memstr, stdouf, keep, sndsrc, hcflg;
extern int server, en_cwd, en_mai, en_pri;
/* Attributes in/out enabled flags */
extern int
atenci, atenco, atdati, atdato, atleni, atleno, atblki, atblko,
attypi, attypo, atsidi, atsido, atsysi, atsyso, atdisi, atdiso;
#ifdef CK_PERMS
extern int atlpri, atlpro, atgpri, atgpro;
#endif /* CK_PERMS */
#ifdef STRATUS
extern int atfrmi, atfrmo, atcrei, atcreo, atacti, atacto;
#endif /* STRATUS */
#ifdef datageneral
extern int quiet;
#endif /* datageneral */
extern long filcnt;
extern CK_OFF_T fsize, ffc, tfc, sendstart, calibrate;
CK_OFF_T rs_len;
#ifndef NOCSETS
_PROTOTYP (VOID setxlate, (void));
extern int tcharset, fcharset;
extern int ntcsets, xlatype, xfrxla;
extern struct csinfo tcsinfo[], fcsinfo[];
#endif /* NOCSETS */
/* Variables global to Kermit that are defined in this module */
#ifdef CKXXCHAR /* DOUBLE / IGNORE char table */
int dblflag = 0;
int ignflag = 0;
short dblt[256] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
#endif /* CKXXCHAR */
int winlo; /* packet number at low window edge */
int sbufnum; /* number of free buffers */
int dum001 = 1234; /* protection... */
int sbufuse[MAXWS]; /* buffer in-use flag */
int dum003 = 1111;
int rbufnum; /* number of free buffers */
int dum002 = 4321; /* more protection */
int rbufuse[MAXWS]; /* buffer in-use flag */
int sseqtbl[64]; /* sequence # to buffer # table */
int rseqtbl[64]; /* sequence # to buffer # table */
int sacktbl[64]; /* sequence # ack table */
int o_isopen = 0, i_isopen = 0; /* Input & output files are open */
#ifdef DYNAMIC
struct pktinfo *s_pkt = NULL; /* array of pktinfo structures */
struct pktinfo *r_pkt = NULL; /* array of pktinfo structures */
#else
struct pktinfo s_pkt[MAXWS]; /* array of pktinfo structures */
struct pktinfo r_pkt[MAXWS]; /* array of pktinfo structures */
#endif /* DYNAMIC */
#ifdef DEBUG
char xbuf[200]; /* For debug logging */
#endif /* DEBUG */
#ifdef DYNAMIC
CHAR *bigsbuf = NULL, *bigrbuf = NULL;
#else
char bigsbt[8]; /* Protection (shouldn't need this). */
/* BUT DON'T REMOVE IT! */
CHAR bigsbuf[SBSIZ + 5]; /* Send-packet buffer area */
char bigrbt[8]; /* Safety padding */
CHAR bigrbuf[RBSIZ + 5]; /* Receive-packet area */
#endif
int bigsbsiz = SBSIZ; /* Sizes of big send & rcv buffers. */
int bigrbsiz = RBSIZ;
#ifdef VMS
int zchkpath(char *s);
#endif /* VMS */
/* FUNCTIONS */
VOID
dofast() {
long maxbufsiz = RBSIZ; /* Configuration parameters */
int maxpktsiz = MAXSP;
extern int spsizf, /* For bug in IRIX Telnet server */
rpsiz, urpsiz, spsizr, spmax, wslotr;
extern struct ck_p ptab[];
if (maxpktsiz < 40) /* Long packet length */
maxpktsiz = 40;
else if (maxpktsiz > 4000)
maxpktsiz = 4000;
wslotr = maxbufsiz / maxpktsiz;
if (wslotr > MAXWS) /* Window slots */
wslotr = MAXWS;
if (wslotr > 30)
wslotr = 30;
else if (wslotr < 1)
wslotr = 1;
urpsiz = adjpkl(maxpktsiz,wslotr,maxbufsiz);
ptab[PROTO_K].rpktlen = urpsiz;
rpsiz = (urpsiz > 94) ? 94 : urpsiz; /* Max non-long packet length */
debug(F111,"dofast","uprsiz",urpsiz);
#ifdef IRIX
#ifndef IRIX65
/* IRIX Telnet server chops off writes longer than 4K */
spsiz = spmax = spsizr = urpsiz;
debug(F101,"doarg Q IRIX spsiz","",spsiz);
spsizf = 1;
#endif /* IRIX65 */
#endif /* IRIX */
#ifdef CK_SPEED
setprefix(PX_CAU); /* Cautious unprefixing */
#endif /* CK_SPEED */
}
/* For sanity, use "i" for buffer slots, "n" for packet numbers. */
/* I N I B U F S */
/*
Allocates the big send and receive buffers.
Call with size for big send buffer (s) and receive buffer (r).
These sizes can be different.
Attempts to allocate buffers of the requested size, but if it can't,
it will allocate smaller ones.
Sets global variables bigsbsiz and bigrbsiz to the actual sizes,
and bigsbuf and bigrbuf pointing to the actual buffers.
Designed to be called more than once.
Returns 0 on success, -1 on failure.
*/
CHAR *bigbufp = NULL;
int
inibufs(s,r) int s, r; {
#ifdef DYNAMIC
unsigned
int size;
#ifdef OS2
unsigned /* Don't you wish everybody had unsigned long... */
#endif /* OS2 */
long z;
int x;
debug(F101,"inibufs s","",s);
debug(F101,"inibufs r","",r);
if (s < 80 || r < 80) return(-1); /* Validate arguments. */
if (!s_pkt) { /* Allocate packet info structures */
if (!(s_pkt = (struct pktinfo *) malloc(sizeof(struct pktinfo)*MAXWS)))
fatal("ini_pkts: no memory for s_pkt");
}
for (x = 0; x < MAXWS; x++)
s_pkt[x].pk_adr = NULL; /* Initialize addresses */
if (!r_pkt) {
if (!(r_pkt = (struct pktinfo *) malloc(sizeof(struct pktinfo)*MAXWS)))
fatal("ini_pkts: no memory for s_pkt");
}
for (x = 0; x < MAXWS; x++)
r_pkt[x].pk_adr = NULL; /* Initialize addresses */
if (!srvcmd) { /* Allocate srvcmd buffer */
srvcmd = (CHAR *) malloc(r + 100);
if (!srvcmd) return(-1);
srvcmdlen = r + 99;
*srvcmd = NUL;
}
if (bigbufp) { /* Free previous buffers, if any. */
free(bigbufp);
bigbufp = NULL;
}
size = s + r + 40; /* Combined requested size + padding */
z = (unsigned) s + (unsigned) r + 40;
debug(F101,"inibufs size 1","",size);
debug(F101,"inibufs size z","",z);
if ((long) size != z) {
debug(F100,"inibufs overflow","",0);
size = 65535;
}
/* Try to get the space. If malloc fails, try to get a little less. */
/* (Obviously, this algorithm can be refined.) */
while (!(bigbufp = (CHAR *) malloc(size))) {
debug(F101,"inibufs bigbuf malloc failed","",size);
size = (size * 2) / 3; /* Failed, cut size by 1/3. */
if (size < 200) /* Try again until too small. */
return(-1);
}
debug(F101,"inibufs size 2","",size); /* OK, we got some space. */
/*
Now divide the allocated space between the send and receive buffers in the
requested proportion. The natural formula would be (s / (s + r)) * size
(for the send buffer), but that doesn't work with integer arithmetic and we
can't use floating point because some machines don't have it. This can be
rearranged as (s * size) / (s + r). But (s * size) can be VERY large, too
large for 32 bits. So let's do it this way. This arithmetic works for
buffer sizes up to about 5,000,000.
*/
#define FACTOR 20L
z = ( (long) s * FACTOR ) / ( (long) s + (long) r );
x = ( z * ( (long) size / FACTOR ) );
if (x < 0) return(-1); /* Catch overflow */
bigsbsiz = x - 5; /* Size of send buffer */
bigsbuf = bigbufp; /* Address of send buffer */
debug(F101,"inibufs bigsbsiz","",bigsbsiz);
bigrbsiz = size - x - 5; /* Size of receive buffer */
bigrbuf = bigbufp + x; /* Addresss of receive buffer */
debug(F101,"inibufs bigrbsiz","",bigrbsiz);
return(0); /* Success */
#else /* No dynamic allocation */
bigsbsiz = SBSIZ; /* Just use the symbols */
bigrbsiz = RBSIZ; /* ... */
return(0); /* Success. */
#endif /* DYNAMIC */
}
/* M A K E B U F -- Makes and clears a new buffers. */
/* Call with: */
/* slots: number of buffer slots to make, 1 to 32 */
/* bufsiz: size of the big buffer */
/* buf: address of the big buffer */
/* xx: pointer to array of pktinfo structures for these buffers */
/* Subdivides the big buffer into "slots" buffers. */
/* Returns: */
/* -1 if too many or too few slots requested, */
/* -2 if slots would be too small. */
/* n (positive) on success = size of one buffer. */
/* with pktinfo structure initialized for this set of buffers. */
int
makebuf(slots,bufsiz,buf,xx)
/* makebuf */ int slots, bufsiz; CHAR buf[]; struct pktinfo *xx; {
CHAR *a;
int i, size;
debug(F101,"makebuf","",slots);
debug(F101,"makebuf bufsiz","",bufsiz);
debug(F101,"makebuf MAXWS","",MAXWS);
if (slots > MAXWS || slots < 1) return(-1);
if (bufsiz < slots * 10 ) return(-2);
size = bufsiz / slots; /* Divide up the big buffer. */
a = buf; /* Address of first piece. */
for (i = 0; i < slots; i++) {
struct pktinfo *x = &xx[i];
x->bf_adr = a; /* Address of this buffer */
x->bf_len = size; /* Length of this buffer */
x->pk_len = 0; /* Length of data field */
x->pk_typ = ' '; /* packet type */
x->pk_seq = -1; /* packet sequence number */
x->pk_rtr = 0; /* retransmissions */
*a = '\0'; /* Clear the buffer */
a += size; /* Position to next buffer slot */
}
return(size);
}
/* M A K S B U F -- Makes the send-packet buffer */
int
mksbuf(slots) int slots; {
int i, x;
sbufnum = 0;
if ((x = makebuf(slots,bigsbsiz,bigsbuf,s_pkt)) < 0) {
debug(F101,"mksbuf makebuf return","",x);
return(x);
}
debug(F101,"mksbuf makebuf return","",x);
for (i = 0; i < 64; i++) { /* Initialize sequence-number- */
sseqtbl[i] = -1; /* to-buffer-number table. */
sacktbl[i] = 0;
}
for (i = 0; i < MAXWS; i++)
sbufuse[i] = 0; /* Mark each buffer as free */
sbufnum = slots;
wcur = 0;
return(x);
}
/* M A K R B U F -- Makes the receive-packet buffer */
int
mkrbuf(slots) int slots; {
int i, x;
rbufnum = 0;
if ((x = makebuf(slots,bigrbsiz,bigrbuf,r_pkt)) < 0) {
debug(F101,"mkrbuf makebuf return","",x);
return(x);
}
debug(F101,"mkrbuf makebuf return","",x);
for (i = 0; i < 64; i++) { /* Initialize sequence-number- */
rseqtbl[i] = -1; /* to-buffer-number table. */
}
for (i = 0; i < MAXWS; i++)
rbufuse[i] = 0; /* Mark each buffer as free */
rbufnum = slots;
wcur = 0;
return(x);
}
/* W I N D O W -- Resize the window to n */
int
window(n) int n; {
debug(F101,"window","",n);
if (n < 1 || n > MAXWS) return(-1);
if (mksbuf(n) < 0) return(-1);
if (mkrbuf(n) < 0) return(-1);
wslots = n;
#ifdef DEBUG
if (deblog) dumpsbuf();
if (deblog) dumprbuf();
#endif /* DEBUG */
return(0);
}
/* G E T S B U F -- Allocate a send-buffer. */
/* Call with packet sequence number to allocate buffer for. */
/* Returns: */
/* -4 if argument is invalid (negative, or greater than 63) */
/* -3 if buffers were thought to be available but really weren't (bug!) */
/* -2 if the number of free buffers is negative (bug!) */
/* -1 if no free buffers. */
/* 0 or positive, packet sequence number, with buffer allocated for it. */
int
getsbuf(n) int n; { /* Allocate a send-buffer */
int i;
CHAR * p = NULL;
if (n < 0 || n > 63) {
debug(F101,"getsbuf bad arg","",n);
return(-4); /* Bad argument */
}
debug(F101,"getsbuf packet","",n);
/* debug(F101,"getsbuf, sbufnum","",sbufnum); */
if (sbufnum == 0) return(-1); /* No free buffers. */
if (sbufnum < 0) return(-2); /* Shouldn't happen. */
for (i = 0; i < wslots; i++) /* Find the first one not in use. */
if (sbufuse[i] == 0) { /* Got one? */
sbufuse[i] = 1; /* Mark it as in use. */
sbufnum--; /* One less free buffer. */
*s_pkt[i].bf_adr = '\0'; /* Zero the buffer data field */
s_pkt[i].pk_seq = n; /* Put in the sequence number */
sseqtbl[n] = i; /* Back pointer from sequence number */
sacktbl[n] = 0; /* ACK flag */
s_pkt[i].pk_len = 0; /* Data field length now zero. */
s_pkt[i].pk_typ = ' '; /* Blank the packet type too. */
s_pkt[i].pk_rtr = 0; /* Zero the retransmission count */
p = s_pkt[i].bf_adr + 7; /* Set global "data" address. */
debug(F101,"getsbuf p","",0);
data = p;
if (!data) {
debug(F100,"getsbuf data == NULL","",0);
return(-3);
}
if ((what & (W_SEND|W_REMO)) && (++wcur > wmax))
wmax = wcur; /* For statistics. */
/* debug(F101,"getsbuf wcur","",wcur); */
return(n); /* Return its index. */
}
sbufnum = 0; /* Didn't find one. */
return(-3); /* Shouldn't happen! */
}
int
getrbuf() { /* Allocate a receive buffer */
int i;
#ifdef COMMENT
/* This code is pretty stable by now... */
/* Looks like we might need this after all */
debug(F101,"getrbuf rbufnum","",rbufnum);
debug(F101,"getrbuf wslots","",wslots);
debug(F101,"getrbuf dum002","",dum002);
debug(F101,"getrbuf dum003","",dum003);
#endif /* COMMENT */
if (rbufnum == 0) return(-1); /* No free buffers. */
if (rbufnum < 0) return(-2); /* Shouldn't happen. */
for (i = 0; i < wslots; i++) /* Find the first one not in use. */
if (rbufuse[i] == 0) { /* Got one? */
rbufuse[i] = 1; /* Mark it as in use. */
*r_pkt[i].bf_adr = '\0'; /* Zero the buffer data field */
rbufnum--; /* One less free buffer. */
debug(F101,"getrbuf new rbufnum","",rbufnum);
if ((what & W_RECV) && (++wcur > wmax))
wmax = wcur; /* For statistics. */
/* debug(F101,"getrbuf wcur","",wcur); */
return(i); /* Return its index. */
}
/* debug(F101,"getrbuf foulup","",i); */
rbufnum = 0; /* Didn't find one. */
return(-3); /* Shouldn't happen! */
}
/* F R E E S B U F -- Free send-buffer for given packet sequence number */
/* Returns: */
/* 1 upon success */
/* -1 if specified buffer does not exist */
int
freesbuf(n) int n; { /* Release send-buffer for packet n. */
int i;
debug(F101,"freesbuf","",n);
if (n < 0 || n > 63) /* No such packet. */
return(-1);
i = sseqtbl[n]; /* Get the window slot number. */
if (i > -1 && i <= wslots) {
sseqtbl[n] = -1; /* If valid, remove from seqtbl */
sbufnum++; /* and count one more free buffer */
sbufuse[i] = 0; /* and mark it as free, */
if (what & (W_SEND|W_REMO)) /* decrement active slots */
wcur--; /* for statistics and display. */
} else {
debug(F101," sseqtbl[n]","",sseqtbl[n]);
return(-1);
}
/* The following is done only so dumped buffers will look right. */
if (1) {
*s_pkt[i].bf_adr = '\0'; /* Zero the buffer data field */
s_pkt[i].pk_seq = -1; /* Invalidate the sequence number */
s_pkt[i].pk_len = 0; /* Data field length now zero. */
s_pkt[i].pk_typ = ' '; /* Blank the packet type too. */
s_pkt[i].pk_rtr = 0; /* And the retries field. */
}
return(1);
}
int
freerbuf(i) int i; { /* Release receive-buffer slot "i". */
int n;
/* NOTE !! Currently, this function frees the indicated buffer, but */
/* does NOT erase the data. The program counts on this. Will find a */
/* better way later.... */
/* debug(F101,"freerbuf, slot","",i); */
if (i < 0 || i >= wslots) { /* No such slot. */
debug(F101,"freerbuf no such slot","",i);
return(-1);
}
n = r_pkt[i].pk_seq; /* Get the packet sequence number */
debug(F101,"freerbuf packet","",n);
if (n > -1 && n < 64) /* If valid, remove from seqtbl */
rseqtbl[n] = -1;
if (rbufuse[i] != 0) { /* If really allocated, */
rbufuse[i] = 0; /* mark it as free, */
rbufnum++; /* and count one more free buffer. */
if (what & W_RECV) /* Keep track of current slots */
wcur--; /* for statistics and display */
debug(F101,"freerbuf rbufnum","",rbufnum);
}
/* The following is done only so dumped buffers will look right. */
if (1) {
/* *r_pkt[i].bf_adr = '\0'; */ /* Zero the buffer data field */
r_pkt[i].pk_seq = -1; /* And from packet list */
r_pkt[i].pk_len = 0; /* Data field length now zero. */
r_pkt[i].pk_typ = ' '; /* Blank the packet type too. */
r_pkt[i].pk_rtr = 0; /* And the retries field. */
}
return(1);
}
/* This is like freerbuf, except it's called with a packet sequence number */
/* rather than a packet buffer index. */
VOID
freerpkt(seq) int seq; {
int k;
debug(F101,"freerpkt seq","",seq);
k = rseqtbl[seq];
/* debug(F101,"freerpkt k","",k); */
if (k > -1) {
k = freerbuf(k);
/* debug(F101,"freerpkt freerbuf","",k); */
}
}
/* C H K W I N -- Check if packet n is in window. */
/* Returns: */
/* 0 if it is in the current window, */
/* +1 if it would have been in previous window (e.g. if ack was lost), */
/* -1 if it is outside any window (protocol error), */
/* -2 if either of the argument packet numbers is out of range. */
/* Call with packet number to check (n), lowest packet number in window */
/* (bottom), and number of slots in window (slots). */
int
chkwin(n,bottom,slots) int n, bottom, slots; {
int top, prev;
debug(F101,"chkwin packet","",n);
debug(F101,"chkwin winlo","",bottom);
debug(F101,"chkwin slots","",slots);
/* First do the easy and common cases, where the windows are not split. */
if (n < 0 || n > 63 || bottom < 0 || bottom > 63)
return(-2);
if (n == bottom) return(0); /* In a perfect world... */
top = bottom + slots; /* Calculate window top. */
if (top < 64 && n < top && n >= bottom)
return(0); /* In current window. */
prev = bottom - slots; /* Bottom of previous window. */
if (prev > -1 && n < bottom && n > prev)
return(1); /* In previous. */
/* Now consider the case where the current window is split. */
if (top > 63) { /* Wraparound... */
top -= 64; /* Get modulo-64 sequence number */
if (n < top || n >= bottom) {
return(0); /* In current window. */
} else { /* Not in current window. */
if (n < bottom && n >= prev) /* Previous window can't be split. */
return(1); /* In previous window. */
else
return(-1); /* Not in previous window. */
}
}
/* Now the case where current window not split, but previous window is. */
if (prev < 0) { /* Is previous window split? */
prev += 64; /* Yes. */
if (n < bottom || n >= prev)
return(1); /* In previous window. */
} else { /* Previous window not split. */
if (n < bottom && n >= prev)
return(1); /* In previous window. */
}
/* It's not in the current window, and not in the previous window... */
return(-1); /* So it's not in any window. */
}
int
dumpsbuf() { /* Dump send-buffers */
#ifdef DEBUG
int j, x, z; /* to debug log. */
if (! deblog) return(0);
x = zsoutl(ZDFILE,"SEND BUFFERS:");
if (x < 0) {
deblog = 0;
return(0);
}
x=zsoutl(ZDFILE,"buffer inuse address length data type seq flag retries");
if (x < 0) {
deblog = 0;
return(0);
}
for (j = 0; j < wslots; j++) {
if (!sbufuse[j])
continue;
z = ((unsigned long)(s_pkt[j].bf_adr)) & 0xffff;
sprintf(xbuf, /* safe (200) */
"%4d%6d%10d%5d%6d%4c%5d%6d\n",
j,
sbufuse[j],
/* Avoid warnings when addresses are bigger than ints */
z,
s_pkt[j].bf_len,
s_pkt[j].pk_len,
s_pkt[j].pk_typ,
s_pkt[j].pk_seq,
s_pkt[j].pk_rtr
);
if (zsout(ZDFILE,xbuf) < 0) {
deblog = 0;
return(0);
}
if (s_pkt[j].pk_adr) {
x = (int)strlen((char *) s_pkt[j].pk_adr);
if (x)
sprintf(xbuf, /* safe (checked) */
"[%.72s%s]\n",s_pkt[j].pk_adr, x > 72 ? "..." : "");
else
sprintf(xbuf,"[(empty string)]\n"); /* safe (200) */
} else {
sprintf(xbuf,"[(null pointer)]\n"); /* safe (200) */
}
if (zsout(ZDFILE,xbuf) < 0) {
deblog = 0;
return(0);
}
}
sprintf(xbuf,"free: %d, winlo: %d\n", sbufnum, winlo); /* safe (200) */
if (zsout(ZDFILE,xbuf) < 0) {
deblog = 0;
return(0);
}
#endif /* DEBUG */
return(0);
}
int
dumprbuf() { /* Dump receive-buffers */
#ifdef DEBUG
int j, x, z;
if (! deblog) return(0);
if (zsoutl(ZDFILE,"RECEIVE BUFFERS:") < 0) {
deblog = 0;
return(0);
}
x=zsoutl(ZDFILE,"buffer inuse address length data type seq flag retries");
if (x < 0) {
deblog = 0;
return(0);
}
for ( j = 0; j < wslots; j++ ) {
if (!rbufuse[j])
continue;
z = ((unsigned long)(r_pkt[j].bf_adr)) & 0xffff;
sprintf(xbuf, /* 200, safe */
"%4d%6d%10d%5d%6d%4c%5d%6d\n",
j,
rbufuse[j],
/* Avoid warnings when addresses are bigger than ints */
z,
r_pkt[j].bf_len,
r_pkt[j].pk_len,
r_pkt[j].pk_typ,
r_pkt[j].pk_seq,
r_pkt[j].pk_rtr
);
if (zsout(ZDFILE,xbuf) < 0) {
deblog = 0;
return(0);
}
x = (int)strlen((char *)r_pkt[j].bf_adr);
sprintf(xbuf, /* safe (checked) */
"[%.72s%s]\n",r_pkt[j].bf_adr, x > 72 ? "..." : "");
if (zsout(ZDFILE,xbuf) < 0) {
deblog = 0;
return(0);
}
}
sprintf(xbuf,"free: %d, winlo: %d\n", rbufnum, winlo); /* safe (200) */
if (zsout(ZDFILE,xbuf) < 0) {
deblog = 0;
return(0);
}
#endif /* DEBUG */
return(0);
}
/* S A T T R -- Send an Attribute Packet */
/*
Sends attribute packet(s) for the current file. If the info will not
fit into one packet, it can be called repeatedly until all the fields
that will fit are sent.
Call with:
xp == 0 if we're sending a real file (F packet), or:
xp != 0 for screen data (X packet).
And:
flag == 1 for first A packet
flag == 0 for subsequent A packets.
Returns:
1 or greater if an A packet was sent, or:
0 if an S-packet was not sent because there was no data to send or
there was no data left that was short enough to send, or:
-1 on any kind of error.
*/
/* (don't) #define TSOFORMAT */
/* which was only for making C-Kermit send TSO-Kermit-like A packets */
/* to try to track down a problem somebody reported... */
int
sattr(xp, flag) int xp, flag; { /* Send Attributes */
static int max; /* Maximum length for Attributes */
static short done[95]; /* Field-complete array */
static struct zattr x; /* File attribute struct */
static char xdate[24];
extern char * cksysid;
/* Some extra flags are used because the "done" array is sparse */
int i, j, rc, aln, left = 0, numset = 0, xbin = 0; /* Workers */
int notafile = 0;
char *tp, c;
notafile = sndarray || pipesend ||
#ifdef PIPESEND
sndfilter ||
#endif /* PIPESEND */
calibrate;
debug(F101,"sattr flag","",flag);
if (!flag) /* No more attributes to send */
if (done[xunchar('@')])
return(0);
/* Initialize Attribute mechanism */
if (flag) { /* First time here for this file? */
initattr(&x); /* Blank out all the fields. */
for (j = 0; j < 95; j++) /* Init array of completed fields */
done[j] = 0;
max = maxdata(); /* Get maximum data field length */
if (notafile || xp == 1) { /* Is it not a real file? */
extern char * zzndate();
char * p;
int i;
#ifdef CALIBRATE
if (calibrate) { /* Calibration run... */
x.lengthk = calibrate / 1024L; /* We know the length */