forked from quinot/taylor-uucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproti.c
1665 lines (1381 loc) · 47.2 KB
/
proti.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
/* proti.c
The 'i' protocol.
Copyright (C) 1992, 1993, 1995, 2002 Ian Lance Taylor
This file is part of the Taylor UUCP package.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
The author of the program may be contacted at [email protected].
*/
#include "uucp.h"
#if USE_RCS_ID
const char proti_rcsid[] = "$Id$";
#endif
#include <ctype.h>
#include <errno.h>
#include "uudefs.h"
#include "uuconf.h"
#include "conn.h"
#include "trans.h"
#include "system.h"
#include "prot.h"
/* The 'i' protocol is a simple sliding window protocol, created by
me. It is in many ways similar to the 'g' protocol. Several ideas
are also taken from the paper ``A High-Throughput Message Transport
System'' by P. Lauder. I don't know where the paper was published,
but the author's e-mail address is [email protected]. However, I
haven't adopted his main idea, which is to dispense with windows
entirely. This is because some links still do require flow control
and, more importantly, because I want to have a limit to the amount
of data I must be able to resend upon request. To reduce the costs
of window acknowledgements, I use a large window and only require
an ack at the halfway point.
Each packet starts with a header containing the following
information:
Intro byte 8 bits byte 1
Packet number 5 bits byte 2
Local channel 3 bits
Packet ack 5 bits byte 3
Remote channel 3 bits
Packet type 3 bits bytes 4-5
Direction 1 bit
Data length 12 bits
Header check 8 bits byte 6
If the data length is not 0, this is followed by the data and a 32
bit CRC checksum.
The following packet types are defined:
SYNC Initialize the connection
DATA Data packet
ACK Simple acknowledgement with no data
NAK Negative acknowledgement; requests resend of single packet
SPOS Set file position
CLOSE Close the connection
*/
/* The offsets of the bytes in the packet header. */
#define IHDR_INTRO (0)
#define IHDR_LOCAL (1)
#define IHDR_REMOTE (2)
#define IHDR_CONTENTS1 (3)
#define IHDR_CONTENTS2 (4)
#define IHDR_CHECK (5)
/* Macros to set and extract values of IHDR_LOCAL and IHDR_REMOTE. */
#define IHDRWIN_SET(iseq, ichan) (((iseq) << 3) | (ichan))
#define IHDRWIN_GETSEQ(ival) (((ival) >> 3) & 0x1f)
#define IHDRWIN_GETCHAN(ival) ((ival) & 0x07)
/* Macros to set and extract values of IHDR_CONTENTS fields. */
#define IHDRCON_SET1(ttype, fcaller, cbytes) \
(((ttype) << 5) | ((fcaller) ? (1 << 4) : 0) | (((cbytes) >> 8) & 0x0f))
#define IHDRCON_SET2(ttype, fcaller, cbytes) ((cbytes) & 0xff)
#define THDRCON_GETTYPE(i1, i2) (((i1) >> 5) & 0x07)
#define FHDRCON_GETCALLER(i1, i2) (((i1) & (1 << 4)) != 0)
#define CHDRCON_GETBYTES(i1, i2) ((((i1) & 0x0f) << 8) | ((i2) & 0xff))
/* Macros for the IHDR_CHECK field. */
#define IHDRCHECK_VAL(qdaemon, zhdr) \
(((zhdr[IHDR_LOCAL] \
^ zhdr[IHDR_REMOTE] \
^ zhdr[IHDR_CONTENTS1] \
^ zhdr[IHDR_CONTENTS2]) \
& 0xff) \
^ ((qdaemon->ifeatures & FEATURE_ICOMPL) \
? 0xff \
: 0))
/* Length of the packet header. */
#define CHDRLEN (6)
/* Amount of space to skip between start of packet and actual data.
This is used to make the actual data longword aligned, to encourage
good performance when copying data into the buffer. */
#define CHDRSKIPLEN (CHDRLEN + (sizeof (long) - CHDRLEN % sizeof (long)))
/* Amount of space to skip between memory buffer and header. */
#define CHDROFFSET (CHDRSKIPLEN - CHDRLEN)
/* Length of the trailing checksum. */
#define CCKSUMLEN (4)
/* Macros to set and get the checksum. These multiply evaluate their
arguments. */
#define ICKSUM_GET(z) \
((((((((unsigned long) ((z)[0] & 0xff)) << 8) \
| (unsigned long) ((z)[1] & 0xff)) << 8) \
| (unsigned long) ((z)[2] & 0xff)) << 8) \
| (unsigned long) ((z)[3] & 0xff))
#define UCKSUM_SET(z, i) \
(void) ((z)[0] = (((i) >> 24) & 0xff), \
(z)[1] = (((i) >> 16) & 0xff), \
(z)[2] = (((i) >> 8) & 0xff), \
(z)[3] = ((i) & 0xff))
/* The header introduction character. */
#define IINTRO ('\007')
/* The packet types. */
#define DATA (0)
#define SYNC (1)
#define ACK (2)
#define NAK (3)
#define SPOS (4)
#define CLOSE (5)
/* Largest possible packet size. */
#define IMAXPACKSIZE ((1 << 12) - 1)
/* Largest possible sequence number (plus 1). */
#define IMAXSEQ 32
/* Get the next sequence number given a sequence number. */
#define INEXTSEQ(i) (((i) + 1) & (IMAXSEQ - 1))
/* Get the previous sequence number given a sequence number. */
#define IPREVSEQ(i) (((i) + IMAXSEQ - 1) & (IMAXSEQ - 1))
/* Compute i1 - i2 in sequence space (i.e., the number of packets from
i2 to i1). */
#define CSEQDIFF(i1, i2) (((i1) + IMAXSEQ - (i2)) & (IMAXSEQ - 1))
/* Largest possible channel number (plus 1). */
#define IMAXICHAN (8)
/* Default packet size to request (protocol parameter
``packet-size''). */
#define IREQUEST_PACKSIZE (1024)
/* Default window size to request (protocol parameter ``window''). */
#define IREQUEST_WINSIZE (16)
/* Default timeout to use when sending the SYNC packet (protocol
parameter ``sync-timeout''). */
#define CSYNC_TIMEOUT (10)
/* Default number of times to retry sending the SYNC packet (protocol
parameter ``sync-retries''). */
#define CSYNC_RETRIES (6)
/* Default timeout to use when waiting for a packet (protocol
parameter ``timeout''). */
#define CTIMEOUT (10)
/* Default number of times to retry sending a packet before giving up
(protocol parameter ``retries''). */
#define CRETRIES (6)
/* Default maximum level of errors to accept before giving up
(protocol parameter ``errors''). */
#define CERRORS (100)
/* Default decay rate. Each time we receive this many packets
succesfully, we decrement the error level by one (protocol
parameter ``error-decay''). */
#define CERROR_DECAY (10)
/* The default list of characters to avoid: XON and XOFF. This string
is processed as an escape sequence. This is 'j' protocol parameter
``avoid''; it is defined in this file because the 'i' and 'j'
protocols share protocol parameters. */
#define ZAVOID "\\021\\023"
/* Local variables. */
/* Packet size to request (protocol parameter ``packet-size''). */
static int iIrequest_packsize = IREQUEST_PACKSIZE;
/* Window size to request (protocol parameter ``window''). */
static int iIrequest_winsize = IREQUEST_WINSIZE;
/* Remote packet size (set from SYNC packet or from
iIforced_remote_packsize). */
static int iIremote_packsize;
/* Size which buffers were allocated for. */
static int iIalc_packsize;
/* Forced remote packet size, used if non-zero (protocol parameter
``remote-packet-size''). There is no forced remote window size
because the ACK strategy requires that both sides agree on the
window size. */
static int iIforced_remote_packsize = 0;
/* Remote window size (set from SYNC packet). */
static int iIremote_winsize;
/* Timeout to use when sending the SYNC packet (protocol
parameter ``sync-timeout''). */
int cIsync_timeout = CSYNC_TIMEOUT;
/* Number of times to retry sending the SYNC packet (protocol
parameter ``sync-retries''). */
static int cIsync_retries = CSYNC_RETRIES;
/* Timeout to use when waiting for a packet (protocol parameter
``timeout''). */
static int cItimeout = CTIMEOUT;
/* Timeout to use when waiting for an acknowledgement to open up space
in the window. This is computed based on the window size and the
connection speed. */
static int cIwindow_timeout = CTIMEOUT;
/* Number of times to retry sending a packet before giving up
(protocol parameter ``retries''). */
static int cIretries = CRETRIES;
/* Maximum level of errors to accept before giving up (protocol
parameter ``errors''). */
static int cIerrors = CERRORS;
/* Each time we receive this many packets succesfully, we decrement
the error level by one (protocol parameter ``error-decay''). */
static int cIerror_decay = CERROR_DECAY;
/* The number of packets we should wait to receive before sending an
ACK; this is set by default to half the window size we have
requested (protocol parameter ``ack-frequency''). */
static int cIack_frequency = 0;
/* The set of characters to avoid (protocol parameter ``avoid'').
This is actually part of the 'j' protocol; it is defined in this
file because the 'i' and 'j' protocols use the same protocol
parameters. */
const char *zJavoid_parameter = ZAVOID;
/* Routine to use when sending data. This is a hook for the 'j'
protocol. */
static boolean (*pfIsend) P((struct sconnection *qconn, const char *zsend,
size_t csend, boolean fdoread));
/* Routine to use to use when reading data. This is a hook for the
'j' protocol. */
static boolean (*pfIreceive) P((struct sconnection *qconn, size_t cneed,
size_t *pcrec, int ctimeout,
boolean freport));
/* Next sequence number to send. */
static int iIsendseq;
/* Last sequence number received. */
static int iIrecseq;
/* Last sequence number we have acknowledged. */
static int iIlocal_ack;
/* Last sequence number remote system has acknowledged. */
static int iIremote_ack;
/* File position we are sending from. */
static long iIsendpos;
/* File position we are receiving to. */
static long iIrecpos;
/* TRUE if closing the connection. */
static boolean fIclosing;
/* Array of sent packets indexed by packet number. */
static char *azIsendbuffers[IMAXSEQ];
/* Array of received packets that we aren't ready to process yet,
indexed by packet number. */
static char *azIrecbuffers[IMAXSEQ];
/* For each packet sequence number, record whether we sent a NAK for
the packet. */
static boolean afInaked[IMAXSEQ];
/* Number of SYNC packets received (used only to detect whether one
was received). */
static int cIsyncs;
/* Number of packets sent. */
static long cIsent_packets;
/* Number of packets received. */
static long cIreceived_packets;
/* Number of packets resent. */
static long cIresent_packets;
/* Number of bad packet headers received. */
static long cIbad_hdr;
/* Number of out of order packets received. */
static long cIbad_order;
/* Number of bad checksums received. */
static long cIbad_cksum;
/* Number of packets rejected by remote system. */
static long cIremote_rejects;
/* Protocol parameter commands. */
struct uuconf_cmdtab asIproto_params[] =
{
{ "packet-size", UUCONF_CMDTABTYPE_INT, (pointer) &iIrequest_packsize,
NULL },
{ "window", UUCONF_CMDTABTYPE_INT, (pointer) &iIrequest_winsize, NULL },
{ "remote-packet-size", UUCONF_CMDTABTYPE_INT,
(pointer) &iIforced_remote_packsize, NULL },
{ "sync-timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cIsync_timeout,
NULL },
{ "sync-retries", UUCONF_CMDTABTYPE_INT, (pointer) &cIsync_retries,
NULL },
{ "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cItimeout, NULL },
{ "retries", UUCONF_CMDTABTYPE_INT, (pointer) &cIretries, NULL },
{ "errors", UUCONF_CMDTABTYPE_INT, (pointer) &cIerrors, NULL },
{ "error-decay", UUCONF_CMDTABTYPE_INT, (pointer) &cIerror_decay, NULL },
{ "ack-frequency", UUCONF_CMDTABTYPE_INT, (pointer) &cIack_frequency, NULL },
/* The ``avoid'' protocol parameter is part of the 'j' protocol, but
it is convenient for the 'i' and 'j' protocols to share the same
protocol parameter table. */
{ "avoid", UUCONF_CMDTABTYPE_STRING, (pointer) &zJavoid_parameter, NULL },
{ NULL, 0, NULL, NULL }
};
/* Local functions. */
static boolean finak P((struct sdaemon *qdaemon, int iseq));
static boolean firesend P((struct sdaemon *qdaemon));
static boolean fiwindow_wait P((struct sdaemon *qdaemon));
static boolean fiwait_for_packet P((struct sdaemon *qdaemon,
int ctimeout, int cretries,
boolean fone, boolean *ftimedout));
static boolean ficheck_errors P((struct sdaemon *qdaemon));
static boolean fiprocess_data P((struct sdaemon *qdaemon,
boolean *pfexit, boolean *pffound,
size_t *pcneed));
static boolean fiprocess_packet P((struct sdaemon *qdaemon,
const char *zhdr,
const char *zfirst, int cfirst,
const char *zsecond, int csecond,
boolean *pfexit));
/* The 'i' protocol start routine. The work is done in a routine
which is also called by the 'j' protocol start routine. */
boolean
fistart (struct sdaemon *qdaemon, char **pzlog)
{
return fijstart (qdaemon, pzlog, IMAXPACKSIZE, fsend_data, freceive_data);
}
/* Start the protocol. This routine is called by both the 'i' and 'j'
protocol start routines. We keep transmitting a SYNC packet
containing the window and packet size we would like to receive
until we receive a SYNC packet from the remote system. The first
two bytes of the data contents of a SYNC packet are the maximum
packet size we want to receive (high byte, low byte), and the next
byte is the maximum window size we want to use. */
boolean
fijstart (struct sdaemon *qdaemon, char **pzlog, int imaxpacksize, boolean (*pfsend) (struct sconnection *, const char *, size_t, boolean), boolean (*pfreceive) (struct sconnection *, size_t, size_t *, int, boolean))
{
char ab[CHDRLEN + 4 + CCKSUMLEN];
unsigned long icksum;
int ctries;
int csyncs;
long ibaud;
*pzlog = NULL;
pfIsend = pfsend;
pfIreceive = pfreceive;
if (iIforced_remote_packsize <= 0
|| iIforced_remote_packsize > imaxpacksize)
iIforced_remote_packsize = 0;
else
iIremote_packsize = iIforced_remote_packsize;
iIalc_packsize = 0;
iIsendseq = 1;
iIrecseq = 0;
iIlocal_ack = 0;
iIremote_ack = 0;
iIsendpos = 0;
iIrecpos = 0;
fIclosing = FALSE;
cIsent_packets = 0;
cIreceived_packets = 0;
cIresent_packets = 0;
cIbad_hdr = 0;
cIbad_order = 0;
cIbad_cksum = 0;
cIremote_rejects = 0;
if (iIrequest_packsize < 0 || iIrequest_packsize > imaxpacksize)
{
ulog (LOG_ERROR, "Illegal protocol '%c' packet size; using %d",
qdaemon->qproto->bname, imaxpacksize);
iIrequest_packsize = imaxpacksize;
}
/* The maximum permissible window size is 16. Otherwise the
protocol can get confused because a duplicated packet may arrive
out of order. If the window size is large in such a case, the
duplicate packet may be treated as a packet in the upcoming
window, causing the protocol to assume that all intermediate
packets have been lost, leading to immense confusion. */
if (iIrequest_winsize < 0 || iIrequest_winsize > IMAXSEQ / 2)
{
ulog (LOG_ERROR, "Illegal protocol '%c' window size; using %d",
qdaemon->qproto->bname, IREQUEST_WINSIZE);
iIrequest_winsize = IREQUEST_WINSIZE;
}
/* The default for the ACK frequency is half the window size. */
if (cIack_frequency <= 0 || cIack_frequency >= iIrequest_winsize)
cIack_frequency = iIrequest_winsize / 2;
ab[IHDR_INTRO] = IINTRO;
ab[IHDR_LOCAL] = ab[IHDR_REMOTE] = IHDRWIN_SET (0, 0);
ab[IHDR_CONTENTS1] = IHDRCON_SET1 (SYNC, qdaemon->fcaller, 4);
ab[IHDR_CONTENTS2] = IHDRCON_SET2 (SYNC, qdaemon->fcaller, 4);
ab[IHDR_CHECK] = IHDRCHECK_VAL (qdaemon, ab);
ab[CHDRLEN + 0] = (iIrequest_packsize >> 8) & 0xff;
ab[CHDRLEN + 1] = iIrequest_packsize & 0xff;
ab[CHDRLEN + 2] = iIrequest_winsize;
ab[CHDRLEN + 3] = qdaemon->cchans;
icksum = icrc (ab + CHDRLEN, 4, ICRCINIT);
UCKSUM_SET (ab + CHDRLEN + 4, icksum);
/* The static cIsyncs is incremented each time a SYNC packet is
received. */
csyncs = cIsyncs;
ctries = 0;
while (TRUE)
{
boolean ftimedout;
DEBUG_MESSAGE3 (DEBUG_PROTO,
"fistart: Sending SYNC packsize %d winsize %d channels %d",
iIrequest_packsize, iIrequest_winsize, qdaemon->cchans);
if (! (*pfIsend) (qdaemon->qconn, ab, CHDRLEN + 4 + CCKSUMLEN,
TRUE))
return FALSE;
if (fiwait_for_packet (qdaemon, cIsync_timeout, 0, FALSE,
&ftimedout))
{
if (csyncs != cIsyncs)
break;
}
else
{
if (! ftimedout)
return FALSE;
++ctries;
if (ctries > cIsync_retries)
{
ulog (LOG_ERROR, "Protocol startup failed");
return FALSE;
}
}
}
/* Calculate the window timeout. */
ibaud = iconn_baud (qdaemon->qconn);
if (ibaud == 0)
cIwindow_timeout = cItimeout;
else
{
/* We expect to receive an ACK about halfway through each
window. In principle, an entire window might be sitting in a
modem buffer while we are waiting for an ACK. Therefore, the
minimum time we should wait for an ACK is
(1/2 window size) * (seconds / byte) + (roundtrip time) ==
(1/2 window size) * (1 / (baud / 10)) + (roundtrip time) ==
(1/2 window size) * (10 / baud) + (roundtrip time) ==
(5 * (window size)) / baud + (roundtrip time)
The window size is iIremote_packsize * iIremote_winsize. For
typical settings of packsize == 1024, winsize == 16, baud ==
9600, this equation works out to
(5 * 1024 * 16) / 9600 == 8 seconds
We then take cItimeout as the round trip time, which gives us
some flexibility. We get more flexibility because it is
quite likely that by the time we have finished sending out
the last packet in a window, the first one has already been
received by the remote system. */
cIwindow_timeout = ((5 * iIremote_packsize * iIremote_winsize) / ibaud
+ cItimeout);
}
/* If we are the callee, bump both timeouts by one, to make it less
likely that both systems will timeout simultaneously. */
if (! qdaemon->fcaller)
{
++cItimeout;
++cIwindow_timeout;
}
/* We got a SYNC packet; set up packet buffers to use. */
if (iIremote_packsize > imaxpacksize)
iIremote_packsize = imaxpacksize;
do
{
int iseq;
for (iseq = 0; iseq < IMAXSEQ; iseq++)
{
azIrecbuffers[iseq] = NULL;
afInaked[iseq] = FALSE;
azIsendbuffers[iseq] = (char *) malloc (iIremote_packsize
+ CHDRSKIPLEN
+ CCKSUMLEN);
if (azIsendbuffers[iseq] == NULL)
{
int ifree;
for (ifree = 0; ifree < iseq; ifree++)
free ((pointer) azIsendbuffers[ifree]);
break;
}
}
if (iseq >= IMAXSEQ)
{
*pzlog =
zbufalc (sizeof "protocol '' sending packet/window / receiving /"
+ 64);
sprintf (*pzlog,
"protocol '%c' sending packet/window %d/%d receiving %d/%d",
qdaemon->qproto->bname, (int) iIremote_packsize,
(int) iIremote_winsize, (int) iIrequest_packsize,
(int) iIrequest_winsize);
iIalc_packsize = iIremote_packsize;
return TRUE;
}
iIremote_packsize >>= 1;
}
while (iIremote_packsize > 200);
ulog (LOG_ERROR,
"'%c' protocol startup failed; insufficient memory for packets",
qdaemon->qproto->bname);
return FALSE;
}
/* Shut down the protocol. We can be fairly informal about this,
since we know that the upper level protocol has already exchanged
hangup messages. If we didn't know that, we would have to make
sure that all packets before the CLOSE had been received. */
boolean
fishutdown (struct sdaemon *qdaemon)
{
char *z;
size_t clen;
fIclosing = TRUE;
z = zigetspace (qdaemon, &clen) - CHDRLEN;
z[IHDR_INTRO] = IINTRO;
z[IHDR_LOCAL] = IHDRWIN_SET (iIsendseq, 0);
z[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
iIlocal_ack = iIrecseq;
z[IHDR_CONTENTS1] = IHDRCON_SET1 (CLOSE, qdaemon->fcaller, 0);
z[IHDR_CONTENTS2] = IHDRCON_SET2 (CLOSE, qdaemon->fcaller, 0);
z[IHDR_CHECK] = IHDRCHECK_VAL (qdaemon, z);
DEBUG_MESSAGE0 (DEBUG_PROTO, "fishutdown: Sending CLOSE");
if (! (*pfIsend) (qdaemon->qconn, z, CHDRLEN, FALSE))
return FALSE;
ulog (LOG_NORMAL,
"Protocol '%c' packets: sent %ld, resent %ld, received %ld",
qdaemon->qproto->bname, cIsent_packets, cIresent_packets,
cIreceived_packets);
if (cIbad_hdr != 0
|| cIbad_cksum != 0
|| cIbad_order != 0
|| cIremote_rejects != 0)
ulog (LOG_NORMAL,
"Errors: header %ld, checksum %ld, order %ld, remote rejects %ld",
cIbad_hdr, cIbad_cksum, cIbad_order, cIremote_rejects);
/* Reset the protocol parameters to their default values. */
iIrequest_packsize = IREQUEST_PACKSIZE;
iIrequest_winsize = IREQUEST_WINSIZE;
iIforced_remote_packsize = 0;
cIsync_timeout = CSYNC_TIMEOUT;
cIsync_retries = CSYNC_RETRIES;
cItimeout = CTIMEOUT;
cIwindow_timeout = CTIMEOUT;
cIretries = CRETRIES;
cIerrors = CERRORS;
cIerror_decay = CERROR_DECAY;
cIack_frequency = 0;
zJavoid_parameter = ZAVOID;
return TRUE;
}
/* Send a command string. These are just sent as normal packets,
ending in a packet containing a null byte. */
boolean
fisendcmd (struct sdaemon *qdaemon, const char *z, int ilocal, int iremote)
{
size_t clen;
DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "fisendcmd: Sending command \"%s\"", z);
clen = strlen (z);
while (TRUE)
{
char *zpacket;
size_t csize;
zpacket = zigetspace (qdaemon, &csize);
if (clen < csize)
{
memcpy (zpacket, z, clen + 1);
return fisenddata (qdaemon, zpacket, clen + 1, ilocal, iremote,
(long) -1);
}
memcpy (zpacket, z, csize);
z += csize;
clen -= csize;
if (! fisenddata (qdaemon, zpacket, csize, ilocal, iremote, (long) -1))
return FALSE;
}
/*NOTREACHED*/
}
/* Send a NAK. */
static boolean
finak (struct sdaemon *qdaemon, int iseq)
{
char abnak[CHDRLEN];
abnak[IHDR_INTRO] = IINTRO;
abnak[IHDR_LOCAL] = IHDRWIN_SET (iseq, 0);
abnak[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
iIlocal_ack = iIrecseq;
abnak[IHDR_CONTENTS1] = IHDRCON_SET1 (NAK, qdaemon->fcaller, 0);
abnak[IHDR_CONTENTS2] = IHDRCON_SET2 (NAK, qdaemon->fcaller, 0);
abnak[IHDR_CHECK] = IHDRCHECK_VAL (qdaemon, abnak);
afInaked[iseq] = TRUE;
DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
"finak: Sending NAK %d", iseq);
return (*pfIsend) (qdaemon->qconn, abnak, CHDRLEN, TRUE);
}
/* Resend the latest packet the remote has not acknowledged. */
static boolean
firesend (struct sdaemon *qdaemon)
{
int iseq;
char *zhdr;
size_t clen;
iseq = INEXTSEQ (iIremote_ack);
if (iseq == iIsendseq)
{
/* Everything has been ACKed and there is nothing to resend. */
return TRUE;
}
DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
"firesend: Resending packet %d", iseq);
/* Update the received sequence number. */
zhdr = azIsendbuffers[iseq] + CHDROFFSET;
if (IHDRWIN_GETSEQ (zhdr[IHDR_REMOTE]) != iIrecseq)
{
int iremote;
iremote = IHDRWIN_GETCHAN (zhdr[IHDR_REMOTE]);
zhdr[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, iremote);
zhdr[IHDR_CHECK] = IHDRCHECK_VAL (qdaemon, zhdr);
iIlocal_ack = iIrecseq;
}
++cIresent_packets;
clen = CHDRCON_GETBYTES (zhdr[IHDR_CONTENTS1],
zhdr[IHDR_CONTENTS2]);
return (*pfIsend) (qdaemon->qconn, zhdr,
CHDRLEN + clen + (clen > 0 ? CCKSUMLEN : 0),
TRUE);
}
/* Wait until there is an opening in the receive window of the remote
system. */
static boolean
fiwindow_wait (struct sdaemon *qdaemon)
{
/* iIsendseq is the sequence number we are sending, and iIremote_ack
is the last sequence number acknowledged by the remote. */
while (CSEQDIFF (iIsendseq, iIremote_ack) > iIremote_winsize)
{
/* If a NAK is lost, it is possible for the other side to be
sending a stream of packets while we are waiting for an ACK.
This should be caught in fiprocess_data; if it is about to
send an ACK, but it has an unacknowledged packet to send, it
sends the entire packet. Hopefully that will trigger an ACK
or a NAK and get us going again. */
DEBUG_MESSAGE0 (DEBUG_PROTO, "fiwindow_wait: Waiting for ACK");
if (! fiwait_for_packet (qdaemon, cIwindow_timeout, cIretries,
TRUE, (boolean *) NULL))
return FALSE;
}
return TRUE;
}
/* Get buffer space to use for packet data. We return a pointer to
the space to be used for the actual data, leaving room for the
header. */
/*ARGSUSED*/
char *
zigetspace (struct sdaemon *qdaemon ATTRIBUTE_UNUSED, size_t *pclen)
{
*pclen = iIremote_packsize;
return azIsendbuffers[iIsendseq] + CHDRSKIPLEN;
}
/* Send a data packet. The zdata argument will always point to value
returned by zigetspace, so we know that we have room before it for
the header information. */
boolean
fisenddata (struct sdaemon *qdaemon, char *zdata, size_t cdata, int ilocal, int iremote, long int ipos)
{
char *zhdr;
unsigned long icksum;
boolean fret;
#if DEBUG > 0
if (ilocal < 0 || ilocal >= IMAXICHAN
|| iremote < 0 || iremote >= IMAXICHAN)
ulog (LOG_FATAL, "fisenddata: ilocal %d iremote %d", ilocal, iremote);
#endif
/* If we are changing the file position, we must send an SPOS
packet. */
if (ipos != iIsendpos && ipos != (long) -1)
{
int inext;
char *zspos;
/* We need to get a buffer to hold the SPOS packet, and it needs
to be next sequence number. However, the data we have been
given is currently in the next sequence number buffer. So we
shuffle the buffers around. */
inext = INEXTSEQ (iIsendseq);
zspos = azIsendbuffers[inext];
azIsendbuffers[inext] = zdata - CHDRSKIPLEN;
azIsendbuffers[iIsendseq] = zspos;
zspos += CHDROFFSET;
zspos[IHDR_INTRO] = IINTRO;
zspos[IHDR_LOCAL] = IHDRWIN_SET (iIsendseq, 0);
zspos[IHDR_CONTENTS1] = IHDRCON_SET1 (SPOS, qdaemon->fcaller,
CCKSUMLEN);
zspos[IHDR_CONTENTS2] = IHDRCON_SET2 (SPOS, qdaemon->fcaller,
CCKSUMLEN);
UCKSUM_SET (zspos + CHDRLEN, (unsigned long) ipos);
icksum = icrc (zspos + CHDRLEN, CCKSUMLEN, ICRCINIT);
UCKSUM_SET (zspos + CHDRLEN + CCKSUMLEN, icksum);
/* Wait for an opening in the window. */
if (iIremote_winsize > 0
&& CSEQDIFF (iIsendseq, iIremote_ack) > iIremote_winsize)
{
if (! fiwindow_wait (qdaemon))
return FALSE;
}
/* Fill in IHDR_REMOTE with the correct value of iIrecseq. */
zspos[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, 0);
iIlocal_ack = iIrecseq;
zspos[IHDR_CHECK] = IHDRCHECK_VAL (qdaemon, zspos);
DEBUG_MESSAGE1 (DEBUG_PROTO, "fisenddata: Sending SPOS %ld",
ipos);
if (! (*pfIsend) (qdaemon->qconn, zspos,
CHDRLEN + CCKSUMLEN + CCKSUMLEN, TRUE))
return FALSE;
iIsendseq = INEXTSEQ (iIsendseq);
iIsendpos = ipos;
}
zhdr = zdata - CHDRLEN;
zhdr[IHDR_INTRO] = IINTRO;
zhdr[IHDR_LOCAL] = IHDRWIN_SET (iIsendseq, ilocal);
zhdr[IHDR_CONTENTS1] = IHDRCON_SET1 (DATA, qdaemon->fcaller, cdata);
zhdr[IHDR_CONTENTS2] = IHDRCON_SET2 (DATA, qdaemon->fcaller, cdata);
/* Compute and set the checksum. */
if (cdata > 0)
{
icksum = icrc (zdata, cdata, ICRCINIT);
UCKSUM_SET (zdata + cdata, icksum);
}
/* Wait until there is an opening in the window (we hope to not have
to wait here at all, actually; ideally the window should be large
enough to avoid a wait). */
if (iIremote_winsize > 0
&& CSEQDIFF (iIsendseq, iIremote_ack) > iIremote_winsize)
{
if (! fiwindow_wait (qdaemon))
return FALSE;
}
/* We only fill in IHDR_REMOTE now, since only now do know the
correct value of iIrecseq. */
zhdr[IHDR_REMOTE] = IHDRWIN_SET (iIrecseq, iremote);
iIlocal_ack = iIrecseq;
zhdr[IHDR_CHECK] = IHDRCHECK_VAL (qdaemon, zhdr);
DEBUG_MESSAGE4 (DEBUG_PROTO,
"fisenddata: Sending packet %d size %d local %d remote %d",
iIsendseq, (int) cdata, ilocal, iremote);
iIsendseq = INEXTSEQ (iIsendseq);
++cIsent_packets;
fret = (*pfIsend) (qdaemon->qconn, zhdr,
cdata + CHDRLEN + (cdata > 0 ? CCKSUMLEN : 0),
TRUE);
iIsendpos += cdata;
if (fret && iPrecstart != iPrecend)
{
boolean fexit;
fret = fiprocess_data (qdaemon, &fexit, (boolean *) NULL,
(size_t *) NULL);
}
return fret;
}
/* Wait for data to come in. */
boolean
fiwait (struct sdaemon *qdaemon)
{
return fiwait_for_packet (qdaemon, cItimeout, cIretries,
FALSE, (boolean *) NULL);
}
/* Wait for a packet. Either there is no data to send, or the remote
window is full. */
static boolean
fiwait_for_packet (struct sdaemon *qdaemon, int ctimeout, int cretries, boolean fone, boolean *pftimedout)
{
int cshort;
int ctimeouts;
if (pftimedout != NULL)
*pftimedout = FALSE;
cshort = 0;
ctimeouts = 0;
while (TRUE)
{
boolean fexit, ffound;
size_t cneed;
size_t crec;
if (! fiprocess_data (qdaemon, &fexit, &ffound, &cneed))
return FALSE;
if (fexit || (fone && ffound))
return TRUE;
if (cneed == 0)
continue;
DEBUG_MESSAGE1 (DEBUG_PROTO, "fiwait_for_packet: Need %d bytes",
(int) cneed);
if (! (*pfIreceive) (qdaemon->qconn, cneed, &crec, ctimeout, TRUE))
return FALSE;
if (crec != 0)
{
/* If we didn't get enough data twice in a row, we may have
dropped some data and be waiting for the end of a large
packet. Incrementing iPrecstart will force
fiprocess_data to skip the current packet and try to find
the next one. */
if (crec >= cneed)
cshort = 0;
else
{
++cshort;
if (cshort > 1)
{
iPrecstart = (iPrecstart + 1) % CRECBUFLEN;
cshort = 0;
}
}
}
else
{
int i;
/* We timed out on the read. */
++ctimeouts;
if (ctimeouts > cretries)
{
if (cretries > 0)
ulog (LOG_ERROR, "Timed out waiting for packet");
if (pftimedout != NULL)
*pftimedout = TRUE;
return FALSE;
}
/* Clear out the list of packets we have sent NAKs for. We
should have seen some sort of response by now. */
for (i = 0; i < IMAXSEQ; i++)
afInaked[i] = FALSE;
/* Send a NAK for the packet we want, and, if we have an
unacknowledged packet, send it again. */
if (! finak (qdaemon, INEXTSEQ (iIrecseq))
|| ! firesend (qdaemon))
return FALSE;