forked from quinot/taylor-uucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protg.c
1928 lines (1621 loc) · 56.1 KB
/
protg.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
/* protg.c
The 'g' protocol.
Copyright (C) 1991, 1992, 1993, 1994, 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 protg_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"
/* Each 'g' protocol packet begins with six bytes. They are:
<DLE><k><c0><c1><C><x>
<DLE> is the ASCII DLE character (^P or '\020').
if 1 <= <k> <= 8, the packet is followed by 2 ** (k + 4) bytes of data;
if <k> == 9, these six bytes are a complete control packet;
other value of <k> are illegal.
<c0> is the low byte of a checksum.
<c1> is the high byte of a checksum.
<C> is a control byte (see below).
<x> is <k> ^ <c0> ^ <c1> ^ <C>.
The control byte <C> is divided into three bitfields:
t t x x x y y y
The two bit field tt is the packet type.
The three bit field xxx is the control type for a control packet, or
the sequence number for a data packet.
The three bit field yyy is a value for a control packet, or the
sequence number of the last packet received for a data packet.
For all successfully recieved packets, the control byte is stored
into iGpacket_control. */
/* Names for the bytes in the frame header. */
#define IFRAME_DLE (0)
#define IFRAME_K (1)
#define IFRAME_CHECKLOW (2)
#define IFRAME_CHECKHIGH (3)
#define IFRAME_CONTROL (4)
#define IFRAME_XOR (5)
/* Length of the frame header. */
#define CFRAMELEN (6)
/* Macros to break apart the control bytes. */
#define CONTROL_TT(b) ((int)(((b) >> 6) & 03))
#define CONTROL_XXX(b) ((int)(((b) >> 3) & 07))
#define CONTROL_YYY(b) ((int)((b) & 07))
/* DLE value. */
#define DLE ('\020')
/* Get the length of a packet given a pointer to the header. */
#define CPACKLEN(z) ((size_t) (1 << ((z)[IFRAME_K] + 4)))
/* <k> field value for a control message. */
#define KCONTROL (9)
/* Get the next sequence number given a sequence number. */
#define INEXTSEQ(i) ((i + 1) & 07)
/* Compute i1 - i2 modulo 8. */
#define CSEQDIFF(i1, i2) (((i1) + 8 - (i2)) & 07)
/* Packet types. These are from the tt field.
CONTROL -- control packet
ALTCHAN -- alternate channel; not used by UUCP
DATA -- full data segment
SHORTDATA -- less than full data segment (all the bytes specified by
the packet length <k> are always transferred). Let <u> be the number
of bytes in the data segment not to be used. If <u> <= 0x7f, the first
byte of the data segment is <u> and the data follows. If <u> > 0x7f,
the first byte of the data segment is 0x80 | (<u> & 0x7f), the second
byte of the data segment is <u> >> 7, and the data follows. The
maximum possible data segment size is 2**12, so this handles all
possible cases. */
#define CONTROL (0)
#define ALTCHAN (1)
#define DATA (2)
#define SHORTDATA (3)
/* Control types. These are from the xxx field if the type (tt field)
is CONTROL.
CLOSE -- close the connection
RJ -- reject; packet yyy last to be received correctly
SRJ -- selective reject; reject only packet yyy (not used by UUCP)
RR -- receiver ready; packet yyy received correctly
INITC -- third step of initialization; yyy holds window size
INITB -- second step of initialization; yyy holds maximum <k> value - 1
INITA -- first step of initialization; yyy holds window size.
The yyy value for RR is the same as the yyy value for an ordinary
data packet. */
#define CLOSE (1)
#define RJ (2)
#define SRJ (3)
#define RR (4)
#define INITC (5)
#define INITB (6)
#define INITA (7)
/* Maximum amount of data in a single packet. This is set by the <k>
field in the header; the amount of data in a packet is
2 ** (<k> + 4). <k> ranges from 1 to 8. */
#define CMAXDATAINDEX (8)
#define CMAXDATA (1 << (CMAXDATAINDEX + 4))
/* Maximum window size. */
#define CMAXWINDOW (7)
/* Defaults for the protocol parameters. These may all be changed by
using the ``protocol-parameter g'' command, so there is no
particular reason to change the values given here. */
/* The desired window size. This is what we tell the other system to
use. It must be between 1 and 7, and there's no reason to use less
than 7. Protocol parameter ``window''. */
#define IWINDOW (7)
/* The desired packet size. Many implementations only support 64 byte
packets. Protocol parameter ``packet-size''. */
#define IPACKSIZE (64)
/* The number of times to retry the exchange of INIT packets when
starting the protocol. Protocol parameter ``startup-retries''. */
#define CSTARTUP_RETRIES (8)
/* The timeout to use when waiting for an INIT packet when starting up
the protocol. Protocol parameter ``init-timeout''. */
#define CEXCHANGE_INIT_TIMEOUT (10)
/* The number of times to retry sending and waiting for a single INIT
packet when starting the protocol. This controls a single INIT
packet, while CSTARTUP_RETRIES controls how many times to try the
entire INIT sequence. Protocol parameter ``init-retries''. */
#define CEXCHANGE_INIT_RETRIES (4)
/* The timeout to use when waiting for a packet. Protocol parameter
``timeout''. */
#define CTIMEOUT (10)
/* The number of times to retry waiting for a packet. Each time the
timeout fails we send a copy of our last data packet or a reject
message for the packet we expect from the other side, depending on
whether we are waiting for an acknowledgement or a data packet.
This is the number of times we try doing that and then waiting
again. Protocol parameter ``retries''. */
#define CRETRIES (6)
/* If we see more than this much unrecognized data, we drop the
connection. This must be larger than a single packet size, which
means it must be larger than 4096 (the largest possible packet
size). Protocol parameter ``garbage''. */
#define CGARBAGE (10000)
/* If we see more than this many protocol errors, we drop the
connection. Protocol parameter ``errors''. */
#define CERRORS (100)
/* Default decay rate. Each time we send or receive this many packets
succesfully, we decrement the error level by one (protocol
parameter ``error-decay''). */
#define CERROR_DECAY (10)
/* If this value is non-zero, it will be used as the remote window
size regardless of what the other side requested. This can be
useful for dealing with some particularly flawed packages. This
default value should always be 0, and protocol parameter
``remote-window'' should be used for the affected systems. */
#define IREMOTE_WINDOW (0)
/* If this value is non-zero, it will be used as the packet size to
send to the remote system regardless of what it requested. It's
difficult to imagine any circumstances where you would want to set
this. Protocol parameter ``remote-packet-size''. */
#define IREMOTE_PACKSIZE (0)
/* Local variables. */
/* Next sequence number to send. */
static int iGsendseq;
/* Last sequence number that has been acked. */
static int iGremote_ack;
/* Last sequence number to be retransmitted. */
static int iGretransmit_seq;
/* Last sequence number we have received. */
static int iGrecseq;
/* Last sequence number we have acked. */
static int iGlocal_ack;
/* Window size to request (protocol parameter ``window''). */
static int iGrequest_winsize = IWINDOW;
/* Packet size to request (protocol parameter ``packet-size''). */
static int iGrequest_packsize = IPACKSIZE;
/* Remote window size (set during handshake). */
static int iGremote_winsize;
/* Forced remote window size (protocol parameter ``remote-window''). */
static int iGforced_remote_winsize = IREMOTE_WINDOW;
/* Remote segment size (set during handshake). This is one less than
the value in a packet header. */
static int iGremote_segsize;
/* Remote packet size (set based on iGremote_segsize). */
static size_t iGremote_packsize;
/* Forced remote packet size (protocol parameter
``remote-packet-size''). */
static int iGforced_remote_packsize = IREMOTE_PACKSIZE;
/* Recieved control byte. */
static int iGpacket_control;
/* Number of times to retry the initial handshake. Protocol parameter
``startup-retries''. */
static int cGstartup_retries = CSTARTUP_RETRIES;
/* Number of times to retry sending an initial control packet.
Protocol parameter ``init-retries''. */
static int cGexchange_init_retries = CEXCHANGE_INIT_RETRIES;
/* Timeout (seconds) for receiving an initial control packet.
Protocol parameter ``init-timeout''. */
static int cGexchange_init_timeout = CEXCHANGE_INIT_TIMEOUT;
/* Timeout (seconds) for receiving a data packet. Protocol parameter
``timeout''. */
static int cGtimeout = CTIMEOUT;
/* Maximum number of timeouts when receiving a data packet or
acknowledgement. Protocol parameter ``retries''. */
static int cGretries = CRETRIES;
/* Amount of garbage data we are prepared to see before giving up.
Protocol parameter ``garbage''. */
static int cGgarbage_data = CGARBAGE;
/* Maximum number of errors we are prepared to see before giving up.
Protocol parameter ``errors''. */
static int cGmax_errors = CERRORS;
/* Each time we receive this many packets succesfully, we decrement
the error level by one (protocol parameter ``error-decay''). */
static int cGerror_decay = CERROR_DECAY;
/* Whether to use shorter packets when possible. Protocol parameter
``short-packets''. */
static boolean fGshort_packets = TRUE;
/* Protocol parameter commands. */
struct uuconf_cmdtab asGproto_params[] =
{
{ "window", UUCONF_CMDTABTYPE_INT, (pointer) &iGrequest_winsize, NULL },
{ "packet-size", UUCONF_CMDTABTYPE_INT, (pointer) &iGrequest_packsize,
NULL },
{ "startup-retries", UUCONF_CMDTABTYPE_INT, (pointer) &cGstartup_retries,
NULL },
{ "init-timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cGexchange_init_timeout,
NULL },
{ "init-retries", UUCONF_CMDTABTYPE_INT, (pointer) &cGexchange_init_retries,
NULL },
{ "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cGtimeout, NULL },
{ "retries", UUCONF_CMDTABTYPE_INT, (pointer) &cGretries, NULL },
{ "garbage", UUCONF_CMDTABTYPE_INT, (pointer) &cGgarbage_data, NULL },
{ "errors", UUCONF_CMDTABTYPE_INT, (pointer) &cGmax_errors, NULL },
{ "error-decay", UUCONF_CMDTABTYPE_INT, (pointer) &cGerror_decay, NULL },
{ "remote-window", UUCONF_CMDTABTYPE_INT,
(pointer) &iGforced_remote_winsize, NULL },
{ "remote-packet-size", UUCONF_CMDTABTYPE_INT,
(pointer) &iGforced_remote_packsize, NULL },
{ "short-packets", UUCONF_CMDTABTYPE_BOOLEAN, (pointer) &fGshort_packets,
NULL },
{ NULL, 0, NULL, NULL }
};
/* Statistics. */
/* Number of packets we have sent. */
static long cGsent_packets;
/* Number of packets we have resent (these are not included in
cGsent_packets). */
static long cGresent_packets;
/* Number of packets we have delayed sending (these should not be
counted in cGresent_packets). */
static long cGdelayed_packets;
/* Number of packets we have received. */
static long cGrec_packets;
/* Number of packets rejected because the header was bad. */
static long cGbad_hdr;
/* Number of packets rejected because the checksum was bad. */
static long cGbad_checksum;
/* Number of packets received out of order. */
static long cGbad_order;
/* Number of packets rejected by receiver (number of RJ packets
received). */
static long cGremote_rejects;
/* Number of duplicate RR packets treated as RJ packets. Some UUCP
packages appear to never send RJ packets, but only RR packets. If
no RJ has been seen, fgprocess_data treats a duplicate RR as an RJ
and increments this variable. */
static long cGremote_duprrs;
/* The error level. This is the total number of errors as adjusted by
cGerror_decay. */
static long cGerror_level;
/* Each time we send an RJ, we can expect several out of order of
packets, because the other side will probably have sent a full
window by the time it sees the RJ. This variable keeps track of
the number of out of order packets we expect to see. We don't
count expected out of order packets against the error level. This
is reset to 0 when an in order packet is received. */
static int cGexpect_bad_order;
#if DEBUG > 1
/* Control packet names used for debugging. */
static const char * const azGcontrol[] =
{"?0?", "CLOSE", "RJ", "SRJ", "RR", "INITC", "INITB", "INITA"};
#endif
/* Local functions. */
static boolean fgexchange_init P((struct sdaemon *qdaemon, int ictl,
int ival, int *piset));
static boolean fgsend_control P((struct sdaemon *qdaemon, int ictl,
int ival));
static char *zgadjust_ack P((int iseq));
static boolean fgwait_for_packet P((struct sdaemon *qdaemon,
boolean freturncontrol, int ctimeout,
int cretries));
static boolean fgsend_acks P((struct sdaemon *qdaemon));
static boolean fggot_ack P((struct sdaemon *qdaemon, int iack));
static boolean fgprocess_data P((struct sdaemon *qdaemon, boolean fdoacks,
boolean freturncontrol,
boolean *pfexit, size_t *pcneed,
boolean *pffound));
static boolean fginit_sendbuffers P((boolean fallocate));
static boolean fgcheck_errors P((struct sdaemon *qdaemon));
static int igchecksum P((const char *zdata, size_t clen));
static int igchecksum2 P((const char *zfirst, size_t cfirst,
const char *zsecond, size_t csecond));
/* Start the protocol. This requires a three way handshake. Both sides
must send and receive an INITA packet, an INITB packet, and an INITC
packet. The INITA and INITC packets contain the window size, and the
INITB packet contains the packet size. */
boolean
fgstart (struct sdaemon *qdaemon, char **pzlog)
{
int iseg;
int i;
boolean fgota, fgotb;
*pzlog = NULL;
/* The 'g' protocol requires a full eight bit interface. */
if (! fconn_set (qdaemon->qconn, PARITYSETTING_NONE,
STRIPSETTING_EIGHTBITS, XONXOFF_OFF))
return FALSE;
iGsendseq = 1;
iGremote_ack = 0;
iGretransmit_seq = -1;
iGrecseq = 0;
iGlocal_ack = 0;
cGsent_packets = 0;
cGresent_packets = 0;
cGdelayed_packets = 0;
cGrec_packets = 0;
cGbad_hdr = 0;
cGbad_checksum = 0;
cGbad_order = 0;
cGremote_rejects = 0;
cGremote_duprrs = 0;
cGerror_level = 0;
cGexpect_bad_order = 0;
/* We must determine the segment size based on the packet size
which may have been modified by a protocol parameter command.
A segment size of 2^n is passed as n - 5. */
i = iGrequest_packsize;
iseg = -1;
while (i > 0)
{
++iseg;
i >>= 1;
}
iseg -= 5;
if (iseg < 0 || iseg > 7)
{
ulog (LOG_ERROR, "Illegal packet size %d for '%c' protocol",
iGrequest_packsize, qdaemon->qproto->bname);
iseg = 1;
}
if (iGrequest_winsize <= 0 || iGrequest_winsize > 7)
{
ulog (LOG_ERROR, "Illegal window size %d for '%c' protocol",
iGrequest_winsize, qdaemon->qproto->bname);
iGrequest_winsize = IWINDOW;
}
fgota = FALSE;
fgotb = FALSE;
for (i = 0; i < cGstartup_retries; i++)
{
if (fgota)
{
if (! fgsend_control (qdaemon, INITA, iGrequest_winsize))
return FALSE;
}
else
{
if (! fgexchange_init (qdaemon, INITA, iGrequest_winsize,
&iGremote_winsize))
continue;
}
fgota = TRUE;
if (fgotb)
{
if (! fgsend_control (qdaemon, INITB, iseg))
return FALSE;
}
else
{
if (! fgexchange_init (qdaemon, INITB, iseg, &iGremote_segsize))
continue;
}
fgotb = TRUE;
if (! fgexchange_init (qdaemon, INITC, iGrequest_winsize,
&iGremote_winsize))
continue;
/* We have succesfully connected. Determine the remote packet
size. */
iGremote_packsize = 1 << (iGremote_segsize + 5);
/* If the user requested us to force specific remote window and
packet sizes, do so now. */
if (iGforced_remote_winsize > 0
&& iGforced_remote_winsize <= CMAXWINDOW)
iGremote_winsize = iGforced_remote_winsize;
if (iGforced_remote_packsize >= 32
&& iGforced_remote_packsize <= 4096)
{
/* Force the value to a power of two. */
i = iGforced_remote_packsize;
iseg = -1;
while (i > 0)
{
++iseg;
i >>= 1;
}
iGremote_packsize = 1 << iseg;
iGremote_segsize = iseg - 5;
}
/* Set up packet buffers to use. We don't do this until we know
the maximum packet size we are going to send. */
if (! fginit_sendbuffers (TRUE))
return FALSE;
*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) iGremote_packsize,
(int) iGremote_winsize, (int) iGrequest_packsize,
(int) iGrequest_winsize);
return TRUE;
}
DEBUG_MESSAGE0 (DEBUG_PROTO | DEBUG_ABNORMAL,
"fgstart: Protocol startup failed");
return FALSE;
}
/* The 'G' protocol is identical to the 'g' protocol, except that
short packets are never supported. */
boolean
fbiggstart (struct sdaemon *qdaemon, char **pzlog)
{
fGshort_packets = FALSE;
return fgstart (qdaemon, pzlog);
}
/* The 'v' protocol is identical to the 'g' protocol, except that the
packet size defaults to 512 bytes. Rather than really get it
right, we automatically switch from the usual default of 64 to 512.
This won't work correctly if somebody does protocol-parameter v
packet-size 64. */
boolean
fvstart (struct sdaemon *qdaemon, char **pzlog)
{
if (iGrequest_packsize == IPACKSIZE)
iGrequest_packsize = 1024;
return fgstart (qdaemon, pzlog);
}
/* Exchange initialization messages with the other system.
A problem:
We send INITA; it gets received
We receive INITA
We send INITB; it gets garbled
We receive INITB
We have seen and sent INITB, so we start to send INITC. The other
side as sent INITB but not seen it, so it times out and resends
INITB. We will continue sending INITC and the other side will
continue sending INITB until both sides give up and start again
with INITA.
It might seem as though if we are sending INITC and receive INITB,
we should resend our INITB, but this could cause infinite echoing
of INITB on a long-latency line. Rather than risk that, I have
implemented a fast drop-back procedure. If we are sending INITB and
receive INITC, the other side has gotten ahead of us. We immediately
fail and begin again with INITA. For the other side, if we are
sending INITC and see INITA, we also immediately fail back to INITA.
Unfortunately, this doesn't work for the other case, in which we
are sending INITB but the other side has not yet seen INITA. As
far as I can see, if this happens we just have to wait until we
time out and resend INITA. */
static boolean
fgexchange_init (struct sdaemon *qdaemon, int ictl, int ival, int *piset)
{
int i;
/* The three-way handshake should be independent of who initializes
it, but it seems that some versions of uucico assume that the
caller sends first and the callee responds. This only matters if
we are the callee and the first packet is garbled. If we send a
packet, the other side will assume that we must have seen the
packet they sent and will never time out and send it again.
Therefore, if we are the callee we don't send a packet the first
time through the loop. This can still fail, but should usually
work, and, after all, if the initialization packets are received
correctly there will be no problem no matter what we do. */
for (i = 0; i < cGexchange_init_retries; i++)
{
long itime;
int ctimeout;
if (qdaemon->fcaller || i > 0)
{
if (! fgsend_control (qdaemon, ictl, ival))
return FALSE;
}
itime = ixsysdep_time ((long *) NULL);
ctimeout = cGexchange_init_timeout;
do
{
long inewtime;
/* We pass 0 as the retry count to fgwait_for_packet because
we want to handle retries here and because if it retried
it would send a packet, which would be bad. */
if (! fgwait_for_packet (qdaemon, TRUE, ctimeout, 0))
break;
if (CONTROL_TT (iGpacket_control) == CONTROL)
{
if (CONTROL_XXX (iGpacket_control) == ictl)
{
*piset = CONTROL_YYY (iGpacket_control);
/* If we didn't already send our initialization
packet, send it now. */
if (! qdaemon->fcaller && i == 0)
{
if (! fgsend_control (qdaemon, ictl, ival))
return FALSE;
}
return TRUE;
}
/* If the other side is farther along than we are,
we have lost a packet. Fail immediately back to
INITA (but don't fail if we are already doing INITA,
since that would count against cStart_retries more
than it should). */
if (CONTROL_XXX (iGpacket_control) < ictl && ictl != INITA)
return FALSE;
/* If we are sending INITC and we receive an INITA, the other
side has failed back (we know this because we have
seen an INITB from them). Fail back ourselves to
start the whole handshake over again. */
if (CONTROL_XXX (iGpacket_control) == INITA && ictl == INITC)
return FALSE;
/* As a special hack, if we are sending INITC and we
receive INITB, we update the segment size from the
packet. This permits a second INITB to override the
first one. It would be nice to do this in a cleaner
way. */
if (CONTROL_XXX (iGpacket_control) == INITB && ictl == INITC)
iGremote_segsize = CONTROL_YYY (iGpacket_control);
}
inewtime = ixsysdep_time ((long *) NULL);
ctimeout -= inewtime - itime;
}
while (ctimeout > 0);
}
return FALSE;
}
/* Shut down the protocol. */
boolean
fgshutdown (struct sdaemon *qdaemon)
{
(void) fgsend_control (qdaemon, CLOSE, 0);
(void) fgsend_control (qdaemon, CLOSE, 0);
(void) fginit_sendbuffers (FALSE);
/* The count of sent packets may not be accurate, because some of
them may have not been sent yet if the connection failed in the
middle (the ones that counted for cGdelayed_packets). I don't
think it's worth being precise. */
ulog (LOG_NORMAL,
"Protocol '%c' packets: sent %ld, resent %ld, received %ld",
qdaemon->qproto->bname, cGsent_packets,
cGresent_packets - cGdelayed_packets, cGrec_packets);
if (cGbad_hdr != 0
|| cGbad_checksum != 0
|| cGbad_order != 0
|| cGremote_rejects != 0
|| cGremote_duprrs != 0)
ulog (LOG_NORMAL,
"Errors: header %ld, checksum %ld, order %ld, remote rejects %ld",
cGbad_hdr, cGbad_checksum, cGbad_order,
cGremote_duprrs + cGremote_rejects);
/* Reset all the parameters to their default values, so that the
protocol parameters used for this connection do not affect the
next one. */
iGrequest_winsize = IWINDOW;
iGrequest_packsize = IPACKSIZE;
cGstartup_retries = CSTARTUP_RETRIES;
cGexchange_init_timeout = CEXCHANGE_INIT_TIMEOUT;
cGexchange_init_retries = CEXCHANGE_INIT_RETRIES;
cGtimeout = CTIMEOUT;
cGretries = CRETRIES;
cGgarbage_data = CGARBAGE;
cGmax_errors = CERRORS;
cGerror_decay = CERROR_DECAY;
iGforced_remote_winsize = IREMOTE_WINDOW;
iGforced_remote_packsize = IREMOTE_PACKSIZE;
fGshort_packets = TRUE;
return TRUE;
}
/* Send a command string. We send packets containing the string until
the entire string has been sent. Each packet is full. */
/*ARGSUSED*/
boolean
fgsendcmd (struct sdaemon *qdaemon, const char *z, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED)
{
size_t clen;
boolean fagain;
DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "fgsendcmd: Sending command \"%s\"", z);
clen = strlen (z);
do
{
char *zpacket;
size_t cdummy;
zpacket = zggetspace (qdaemon, &cdummy);
if (clen < iGremote_packsize)
{
size_t csize;
/* If the remote packet size is larger than 64 (the default,
which may indicate an older UUCP package), try to fit
this command into a smaller packet. We still always send
a complete packet, though. */
if (iGremote_packsize <= 64 || ! fGshort_packets)
csize = iGremote_packsize;
else
{
csize = 32;
while (csize <= clen)
csize <<= 1;
}
memcpy (zpacket, z, clen);
if (csize > clen)
bzero (zpacket + clen, csize - clen);
fagain = FALSE;
if (! fgsenddata (qdaemon, zpacket, csize, 0, 0, (long) 0))
return FALSE;
}
else
{
memcpy (zpacket, z, iGremote_packsize);
z += iGremote_packsize;
clen -= iGremote_packsize;
fagain = TRUE;
if (! fgsenddata (qdaemon, zpacket, iGremote_packsize,
0, 0, (long) 0))
return FALSE;
}
}
while (fagain);
return TRUE;
}
/* We keep an array of buffers to retransmit as necessary. Rather
than waste static space on large buffer sizes, we allocate the
buffers once we know how large the other system expects them to be.
The sequence numbers used in the 'g' protocol are only three bits
long, so we allocate eight buffers and maintain a correspondence
between buffer index and sequence number. This always wastes some
buffer space, but it's easy to implement.
We leave room at the front of the buffer for the frame header and
two additional bytes. The two extra bytes are used for short
packets, which essentially use a longer header and shorter data.
We do this to avoid moving the data. We zero out any unused bytes
before the frame, so we can locate the real header given a buffer
by finding the first non-zero byte (which will be one of the first
three bytes in the buffer). */
#define CSENDBUFFERS (CMAXWINDOW + 1)
static char *azGsendbuffers[CSENDBUFFERS];
static boolean
fginit_sendbuffers (boolean fallocate)
{
int i;
/* Free up any remaining old buffers. */
for (i = 0; i < CSENDBUFFERS; i++)
{
xfree ((pointer) azGsendbuffers[i]);
if (fallocate)
{
azGsendbuffers[i] = (char *) malloc (CFRAMELEN + 2
+ iGremote_packsize);
if (azGsendbuffers[i] == NULL)
return FALSE;
/* This bzero might not seem necessary, since before we send
out each packet we zero out any non-data bytes. However,
if we receive an SRJ at the start of the conversation, we
will send out the packet before it has been set to
anything, thus sending the contents of our heap. We
avoid this by using bzero. */
bzero (azGsendbuffers[i], CFRAMELEN + 2 + iGremote_packsize);
}
else
azGsendbuffers[i] = NULL;
}
return TRUE;
}
/* Allocate a packet to send out. The return value of this function
must be filled in and passed to fgsenddata, or discarded. This
will ensure that the buffers and iGsendseq stay in synch. Set
*pclen to the amount of data to place in the buffer. */
/*ARGSUSED*/
char *
zggetspace (struct sdaemon *qdaemon ATTRIBUTE_UNUSED, size_t *pclen)
{
*pclen = iGremote_packsize;
return azGsendbuffers[iGsendseq] + CFRAMELEN + 2;
}
/* Send out a data packet. This computes the checksum, sets up the
header, and sends the packet out. The argument zdata should point
to the return value of zggetspace. */
/*ARGSIGNORED*/
boolean
fgsenddata (struct sdaemon *qdaemon, char *zdata, size_t cdata, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED, long int ipos ATTRIBUTE_UNUSED)
{
char *z;
int itt, iseg;
size_t csize;
int iclr1, iclr2;
unsigned short icheck;
/* Set the initial length bytes. See the description at the definition
of SHORTDATA, above. */
itt = DATA;
csize = iGremote_packsize;
iseg = iGremote_segsize + 1;
#if DEBUG > 0
if (cdata > csize)
ulog (LOG_FATAL, "fgsend_packet: Packet size too large");
#endif
iclr1 = -1;
iclr2 = -2;
if (cdata < csize)
{
/* If the remote packet size is larger than 64, the default, we
can assume they can handle a smaller packet as well, which
will be more efficient to send. */
if (iGremote_packsize > 64 && fGshort_packets)
{
/* The packet size is 1 << (iseg + 4). */
iseg = 1;
csize = 32;
while (csize < cdata)
{
csize <<= 1;
++iseg;
}
}
if (csize != cdata)
{
size_t cshort;
/* We have to add bytes which indicate how short the packet
is. We do this by pushing the header backward, which we
can do because we allocated two extra bytes for this
purpose. */
iclr2 = 0;
itt = SHORTDATA;
cshort = csize - cdata;
if (cshort <= 127)
{
--zdata;
zdata[0] = (char) cshort;
zdata[-1] = '\0';
if (cshort > 1)
bzero (zdata + cdata + 1, cshort - 1);
}
else
{
zdata -= 2;
zdata[0] = (char) (0x80 | (cshort & 0x7f));
zdata[1] = (char) (cshort >> 7);
bzero (zdata + cdata + 2, cshort - 2);
iclr1 = 0;
}
}
}
z = zdata - CFRAMELEN;
/* Zero out the preceding bytes, in case the last time this buffer
was used those bytes were used. We need to zero out the initial
bytes so that we can find the true start of the packet in
zgadjust_ack. */
z[iclr1] = '\0';
z[iclr2] = '\0';
z[IFRAME_DLE] = DLE;
z[IFRAME_K] = (char) iseg;
icheck = (unsigned short) igchecksum (zdata, csize);
/* We're just about ready to go. Wait until there is room in the
receiver's window for us to send the packet. We do this now so
that we send the correct value for the last packet received.
Note that if iGsendseq == iGremote_ack, this means that the
sequence numbers are actually 8 apart, since the packet could not
have been acknowledged before it was sent; this can happen when
the window size is 7. */
while (iGsendseq == iGremote_ack
|| CSEQDIFF (iGsendseq, iGremote_ack) > iGremote_winsize)
{
if (! fgwait_for_packet (qdaemon, TRUE, cGtimeout, cGretries))
return FALSE;
}
/* Ack all packets up to the next one, since the UUCP protocol
requires that all packets be acked in order. */
while (CSEQDIFF (iGrecseq, iGlocal_ack) > 1)
{
iGlocal_ack = INEXTSEQ (iGlocal_ack);
if (! fgsend_control (qdaemon, RR, iGlocal_ack))
return FALSE;
}
iGlocal_ack = iGrecseq;
z[IFRAME_CONTROL] = (char) ((itt << 6) | (iGsendseq << 3) | iGrecseq);
iGsendseq = INEXTSEQ (iGsendseq);
icheck = ((unsigned short)
((0xaaaa - (icheck ^ (z[IFRAME_CONTROL] & 0xff))) & 0xffff));
z[IFRAME_CHECKLOW] = (char) (icheck & 0xff);
z[IFRAME_CHECKHIGH] = (char) (icheck >> 8);
z[IFRAME_XOR] = (char) (z[IFRAME_K] ^ z[IFRAME_CHECKLOW]
^ z[IFRAME_CHECKHIGH] ^ z[IFRAME_CONTROL]);
/* If we're waiting for acks of retransmitted packets, then don't
send this packet yet. The other side may not be ready for it
yet. Instead, code in fggot_ack will send the outstanding
packets when an ack is received. */
++cGsent_packets;
if (iGretransmit_seq != -1)
{
++cGdelayed_packets;
return TRUE;
}
DEBUG_MESSAGE2 (DEBUG_PROTO,
"fgsenddata: Sending packet %d (%d bytes)",
CONTROL_XXX (z[IFRAME_CONTROL]), (int) cdata);
return fsend_data (qdaemon->qconn, z, CFRAMELEN + csize, TRUE);
}
/* Recompute the control byte and checksum of a packet so that it
includes the correct packet acknowledgement. This is called when a
packet is retransmitted to make sure the retransmission does not
confuse the other side. It returns a pointer to the start of the
packet, skipping the bytes that may be unused at the start of
azGsendbuffers[iseq]. */
static char *
zgadjust_ack (int iseq)