forked from quinot/taylor-uucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protz.c
2541 lines (2252 loc) · 66.3 KB
/
protz.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
/* protz.c Version 1.5, 92Apr24 */
/* Modified by Ian Lance Taylor for Taylor UUCP 1.04 92Aug4. */
/*
* Doug Evans, [email protected] or [email protected]
*
* This file provides the Zmodem protocol (by Chuck Forsberg) for
* Ian Taylor's UUCP package.
*
* It was originally developed to establish a uucp link between myself and my
* employer: Ivation Datasystems, Inc. of Ottawa.
*
* My thanks to Ivation for letting me release this to the public. Given that
* Zmodem is in the public domain, no additional copyrights have been added.
*
*****************************************************************************
*
* It's been difficult fitting Zmodem into the UUCP world. I have been guided
* mostly by trying to plug it into Taylor UUCP. Where "the Zmodem way of doing
* things" conflicted with "the UUCP way of doing things", I have err'd on the
* side of UUCP. At the end of it all, I have achieved something that will plug
* into Taylor UUCP very easily, but some might argue that I have corrupted Z
* too much. At any rate, compatibility with sz/rz was sacrificed to achieve a
* clean UUCP protocol. Given that, I took the opportunity to start from
* scratch when defining protocol constants (EG: ZBIN).
*
* 1) I wasn't quite sure how to enhance Zmodem to handle send+receive in one
* session, so I added a 'g' protocol like initialization sequence. This
* also gets this stuff out of the way, in case we ever try to support
* full-duplex.
*
* Caller Callee
* ------ ------
* ZINIT --> <-- ZINIT
* ZDATA (ZCRCF) --> <-- ZDATA (ZCRCF)
* ZACK --> <-- ZACK
* ZINITEND --> <-- ZINITEND
*
* ZINIT is a combination of ZRINIT and ZSINIT and is intended to exchange
* simple protocol information (flags) and the protocol version number.
* ZDATA is intended to include window size information as well as the
* "Myattn" string (although at the moment it doesn't contain anything).
* ZDATA may contain at most 1k bytes of data and is sent out as one ZCRCF
* packet. Two ack's (ZACK + ZINITEND) are needed to ensure both sides have
* received ZDATA.
*
* 2) I've hardcoded several protocol parameters, like 32 bit CRC's for data.
* Others are not supported (we don't need them).
*
* 3) ZHEX headers use 32 bit CRC's.
*
* 4) Zmodem sends the ZFILE message "in one chunk". If there are errors, the
* entire string is resent. I have continued this practice. All UUCP
* commands are sent "in one chunk". This can be changed down the road if
* necessary.
*
* 5) The ZEOF message has been replaced with a new ZCRCx value: ZCRCF. ZCRCF
* is identical to ZCRCW except that it indicates the end of the message.
* The protocol here is *not* a file transfer protocol. It is an end to end
* transport protocol (that preserves message boundaries).
*
* 6) Zmodem handles restarting a file transfer, but as best as I can tell UUCP
* does not. At least Taylor UUCP doesn't. And if UUCP does start handling
* file restart, can it be plugged into the existing Zmodem way with zero
* changes? Beats me. Therefore I have removed this part of the code. One
* can always put it back in if and when UUCP handles it. Ditto for other
* pieces of removed code: there's no point in overly complicating this code
* when supporting all the bells and whistles requires enhancements to UUCP
* itself.
*
* *** It is easier to put code back in in an upward compatible manner ***
* *** than it is to correct for misunderstood code or poorly merged ***
* *** (Zmodem vs UUCP) code. ***
*
* 7) For the character in the initial "protocol selection" sequence, I have
* chosen 'a'. I'm told 'z' is already in use for something that isn't
* Zmodem. It's entirely reasonable to believe that if Zmodem ever becomes a
* standard UUCP protocol, this won't be it (so I'll leave z/Z for them).
* Publicly, this is the 'a' protocol. Internally, it is refered to as 'z'.
* A little confusing, I know. Maybe in time I'll refer to it internally as
* 'a', or maybe in time this will be *the* 'z' protocol.
*
* 8) Since we are writing a transport protocol, which isn't supposed to know
* anything about what is being transferred or where it is coming from, the
* header data value has changed meaning. It no longer means "file position"
* but instead means "window position". It is a running counter of the bytes
* transferred. Each "message" begins on a 1k boundary so the count isn't a
* precise byte count. The counter wraps every 4 gigabytes, although this
* wrapping isn't supported yet.
*
* FIXME: At present the max data transferred per session is 4 gigabytes.
*
****************************************************************************
*
* A typical message sequence is (master sending file to slave):
*
* Master Slave
* ------ -----
* ZDATA (S, ZCRCF) -->
* <-- ZACK
* <-- ZDATA (SY, ZCRCF)
* ZACK -->
* ZDATA -->
* ... <-- ZACK/ZRPOS
* ZDATA (ZCRCF) -->
* <-- ZACK
* <-- ZDATA (CY, ZCRCF)
* ZACK -->
*
* A typical message sequence is (master receiving file from slave):
*
* Master Slave
* ------ -----
* ZDATA (R, ZCRCF) -->
* <-- ZACK
* <-- ZDATA (RY, ZCRCF)
* ZACK -->
* <-- ZDATA
* ZACK/ZRPOS ... -->
* <-- ZDATA (ZCRCF)
* ZACK -->
* ZDATA (CY, ZCRCF) -->
* <-- ZACK
*
*****************************************************************************
*
* Notes:
* 1) For future bidirectional concerns, keep packet types "unidirectional".
* Sender always uses: ZDATA, ZNAK
* Receiver always uses: ZRPOS, ZACK
* There is no intersection.
*
* I'm not sure if this is necessary or even useful, but it seems to be.
*
* 2) I use to store the byte count / 32 in the data header. This left 5 bits
* unused for future concerns. I removed this because of the following
* situation when sending a file:
*
* ZDATA (ZCRCG, xx bytes) - received ok
* ZDATA (ZCRCF, 0 bytes) - corrupted
*
* At this point the receiver would like to send back a ZRPOS with a value
* of the size of the file. However, it can't because the value is divided
* by 32, and it would have to round up to the next multiple of 32. This
* seemed a little ugly, so I went with using the entire header to store
* the byte count.
*
*****************************************************************************
*
* Source version:
*
* 1.1,2,3
* Protocol version 0
* Early attempts, completely rewritten later.
*
* 1.4 Protocol version 1
* Beta test sent to Ian for analysis 92Apr18.
*
* 1.5 Protocol version 1
* Released 92Apr24.
*
*****************************************************************************
*
* Protocol version:
*
* A version number is exchanged in the ZINIT message, so it is possible to
* correct or enhance the protocol, without breaking existing versions.
* The purpose of this section is to document these versions as they come out.
* Remember, this is the protocol version, not the source version.
*
* 0 Initial version.
* Zmodem controlled file transfer. This was more of a "plug Z
* into UUCP as is" port.
*
* 1 Complete rewrite.
* Made Z more of a transport protocol. UUCP now controls transfer and Z
* is on the same footing as the other UUCP protocols.
* Theoretically, there will be little pain when UUCP goes bidirectional.
*/
#include "uucp.h"
#if USE_RCS_ID
const char protz_rcsid[] = "$Id$";
#endif
#include <errno.h>
#include "uudefs.h"
#include "uuconf.h"
#include "conn.h"
#include "trans.h"
#include "system.h"
#include "prot.h"
#define ZPROTOCOL_VERSION 1
/*
* Control message characters ...
*/
#define ZPAD '*' /* Padding character begins frames */
#define ZDLE 030 /* Ctrl-X Zmodem escape - `ala BISYNC DLE */
#define ZBIN 'A' /* Binary frame indicator */
#define ZHEX 'B' /* HEX frame indicator */
/*
* Frame types (see array "frametypes" in zm.c) ...
*
* Note that the numbers here have been reorganized, as we don't support
* all of them (nor do we need to).
*
* WARNING: The init sequence assumes ZINIT < ZDATA < ZACK < ZINITEND.
*/
#define ZINIT 0 /* Init (contains protocol version, flags) */
#define ZDATA 1 /* Data packet(s) follow */
#define ZRPOS 2 /* Resume data trans at this position */
#define ZACK 3 /* ACK to above */
#define ZNAK 4 /* Last packet was garbled */
#define Zreserved 5 /* reserved (for future concerns) */
#define ZINITEND 6 /* end of init sequence */
#define ZFIN 7 /* Finish session */
/*
* ZDLE sequences ...
*
* Note addition of ZCRCF: "end of message".
*/
#define ZCRCE 'h' /* CRC next, frame ends, header packet follows */
#define ZCRCG 'i' /* CRC next, frame continues nonstop */
#define ZCRCQ 'j' /* CRC next, frame continues, ZACK expected */
#define ZCRCW 'k' /* CRC next, ZACK expected, end of frame */
#define ZCRCF 'l' /* CRC next, ZACK expected, end of message */
#define ZRUB0 'm' /* Translate to rubout 0177 */
#define ZRUB1 'n' /* Translate to rubout 0377 */
/*
* zdlread return values (internal) ...
* Other values are ZM_ERROR, ZM_TIMEOUT, ZM_RCDO.
*/
#define GOTOR 0400
#define GOTCRCE (ZCRCE | GOTOR) /* ZDLE-ZCRCE received */
#define GOTCRCG (ZCRCG | GOTOR) /* ZDLE-ZCRCG received */
#define GOTCRCQ (ZCRCQ | GOTOR) /* ZDLE-ZCRCQ received */
#define GOTCRCW (ZCRCW | GOTOR) /* ZDLE-ZCRCW received */
#define GOTCRCF (ZCRCF | GOTOR) /* ZDLE-ZCRCF received */
/*
* Byte positions within header array ...
*/
#define ZF0 3 /* First flags byte */
#define ZF1 2
#define ZF2 1
#define ZF3 0
#define ZP0 0 /* Low order 8 bits of position */
#define ZP1 1
#define ZP2 2
#define ZP3 3 /* High order 8 bits of position */
/*
* Bit Masks for ZRQINIT flags byte ZF0 ...
*/
#define TX_ESCCTL 1 /* Tx will escape control chars */
/*
* Possible errors when running ZMODEM ...
*/
#define ZM_ERROR (-1) /* crc error, etc. */
#define ZM_TIMEOUT (-2)
#define ZM_RCDO (-3) /* Carrier Lost */
/*
* ASCII characters ...
*/
#define LF 012
#define CR 015
#define XON 021
#define XOFF 023
#define XON_WAIT 10 /* seconds */
/*
* Packet sizes ...
*
* FIXME: CPACKETSIZE is hardcoded in a lot of places.
* It's not clear to me whether changing it's value would be a
* "good thing" or not. But of course that doesn't excuse the hardcoding.
*/
#define CPACKETSIZE 1024 /* max packet size (data only) */
#define CFRAMELEN 12 /* header size */
#define CSUFFIXLEN 10 /* suffix at end of data packets */
#define CEXCHANGE_INIT_RETRIES 4
/* The header CRC value. */
#if ANSI_C
#define IHDRCRC 0xDEBB20E3UL
#else
#define IHDRCRC ((unsigned long) 0xDEBB20E3L)
#endif
/* packet buffer size */
#define CPACKBUFSIZE (CFRAMELEN + 2 * CPACKETSIZE + CSUFFIXLEN + 42 /*slop*/)
/*
* Data types ...
*/
typedef unsigned char achdrval_t[4];
typedef unsigned long hdrval_t;
typedef unsigned long winpos_t;
/*
* Configurable parms ...
*
* FIXME: <cZrx_buf_len> isn't used yet. It may not be needed.
*/
#define CTIMEOUT 10
#define CRETRIES 10
#define CSTARTUP_RETRIES 4
#define CGARBAGE 2400
#define CSEND_WINDOW 16384
#define FESCAPE_CONTROL FALSE
static int cZtimeout = CTIMEOUT; /* (seconds) */
static int cZretries = CRETRIES;
static int cZstartup_retries = CSTARTUP_RETRIES;
static int cZmax_garbage = CGARBAGE; /* max garbage before header */
static int cZtx_window = CSEND_WINDOW; /* our transmission window */
static int cZrx_buf_len = 0; /* our reception buffer size */
static boolean fZesc_ctl = FESCAPE_CONTROL; /* escape control chars */
struct uuconf_cmdtab asZproto_params[] =
{
{"timeout", UUCONF_CMDTABTYPE_INT, (pointer) & cZtimeout, NULL},
{"retries", UUCONF_CMDTABTYPE_INT, (pointer) & cZretries, NULL},
{"startup-retries", UUCONF_CMDTABTYPE_INT,
(pointer) & cZstartup_retries, NULL},
{"garbage", UUCONF_CMDTABTYPE_INT, (pointer) & cZmax_garbage, NULL},
{"send-window", UUCONF_CMDTABTYPE_INT, (pointer) & cZtx_window, NULL},
{"escape-control", UUCONF_CMDTABTYPE_BOOLEAN, (pointer) & fZesc_ctl,
NULL},
{NULL, 0, NULL, NULL}
};
/*
* Variables for statistic gathering ...
*
* We use <wpZtxpos, wpZrxbytes> to record the number of "packets"
* sent/received. Packets is in double quotes because some of them aren't full.
*/
static unsigned long cZheaders_sent;
static unsigned long cZheaders_received;
static unsigned long cZbytes_resent;
static unsigned long cZtimeouts;
static unsigned long cZerrors;
/*
* Data buffers ...
*/
static char *zZtx_buf; /* transmit buffer */
static char *zZtx_packet_buf; /* raw outgoing packet data */
static char *zZrx_packet_buf; /* raw incoming packet data */
/*
* Transmitter state variables ...
*/
static unsigned cZblklen; /* data length in sent/received packets */
static unsigned cZtxwspac; /* spacing between ZCRCQ requests */
/*static unsigned cZblklen_override;*//* override value for <cZblklen> */
static unsigned cZtxwcnt; /* counter used to space ack requests */
static unsigned cZrxwcnt; /* counter used to watch receiver's buf size */
static winpos_t wpZtxstart; /* <wpZtxpos> when message started */
static winpos_t wpZtxpos; /* transmitter position */
static winpos_t wpZlastsync; /* last offset to which we got a ZRPOS */
static winpos_t wpZlrxpos; /* receiver's last reported offset */
static winpos_t wpZrxpos; /* receiver file position */
static int iZlast_tx_data_packet; /* type of last ZDATA packet (ZCRCx) */
static int iZjunk_count; /* amount of garbage characters received */
static int iZtleft; /* for dynamic packet resizing */
static int iZbeenhereb4; /* times we've been ZRPOS'd to same place */
/*
* Receiver state variables ...
*/
static winpos_t wpZrxbytes; /* receiver byte count */
static int iZlast_rx_data_packet; /* last successfully received ZCRCx packet */
/*
* Misc. globals ...
*/
static char xon = XON;
#ifdef DJE_TESTING
int uucptest = -1;
int uucptest2;
int uucptestseed;
#endif
/*
* Kludge!!!
* See fzfinish_tx(). Basically the next two globals are used to record the
* fact that we got a ZDATA, but aren't quite ready to process it.
*/
static int iZpkt_rcvd_kludge; /* -1 if not valid */
static hdrval_t hvZpkt_hdrval_kludge;
/*
* Packet types ...
*/
static const char *azZframe_types[] = {
"Carrier Lost", /* -3 */
"Timeout", /* -2 */
"Error", /* -1 */
#define FTOFFSET 3
"ZINIT",
"ZDATA",
"ZRPOS",
"ZACK",
"ZNAK",
"Zreserved",
"ZINITEND",
"ZFIN",
"UNKNOWN!!!"
};
#define FTNUMBER (sizeof(azZframe_types) / sizeof(char *))
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#define ZZHEADER_NAME(itype) \
azZframe_types[min((size_t) ((itype) + FTOFFSET), FTNUMBER - 1)]
/*
* Local functions ...
*/
static boolean fzsend_data P((struct sdaemon *qdaemon, char *zdata,
size_t cdata, boolean fendofmessage));
static boolean fzprocess P((struct sdaemon *qdaemon));
static boolean fzstart_proto P((struct sdaemon *qdaemon));
static int izexchange_init P((struct sdaemon *qdaemon, int send_type,
achdrval_t send_val, achdrval_t recv_val));
static boolean fzshutdown_proto P((struct sdaemon *qdaemon));
static boolean fzstart_tx P((void));
static boolean fzfinish_tx P((struct sdaemon *qdaemon, long *plredo));
static boolean fzstart_rx P((void));
static boolean fzfinish_rx P((struct sdaemon *qdaemon));
static boolean fzsend_hdr P((struct sdaemon *qdaemon, int ipkttype,
int ihdrtype, hdrval_t hdrval,
boolean fcheckreceive));
static boolean fzsend_data_packet P((struct sdaemon *qdaemon, char *zdata,
size_t cdata, int frameend,
boolean fcheckreceive));
static int czbuild_header P((char *zresult, int ipkttype, int ihdrtype,
hdrval_t hdrval));
static int czbuild_data_packet P((char *zresult, const char *zdata,
size_t cdata, int frameend));
/*
* The rest of the functions do not follow Ian's naming style. I have left
* the names the same as the original zm source. Over time, they may change.
*/
static int izrecv_hdr P((struct sdaemon *qdaemon, achdrval_t hdr));
static int zrbhdr32 P((struct sdaemon *qdaemon, achdrval_t hdr));
static int zrhhdr P((struct sdaemon *qdaemon, achdrval_t hdr));
static int zrdat32 P((struct sdaemon *qdaemon, char *buf, int length,
int *iprxcount));
static int getinsync P((struct sdaemon *qdaemon, boolean flag));
static char *zputhex P((char *p, int ch));
static char *zputchar P((char *p, int ch));
static int zgethex P((struct sdaemon *qdaemon));
static int zdlread P((struct sdaemon *qdaemon));
static int noxrd7 P((struct sdaemon *qdaemon));
static int realreadchar P((struct sdaemon *qdaemon, int timeout));
static boolean fzreceive_ready P((void));
static void stohdr P((hdrval_t pos, achdrval_t hdr));
static hdrval_t rclhdr P((achdrval_t hdr));
static hdrval_t hvzencode_data_hdr P((winpos_t cbytes));
static void zdecode_data_hdr P((hdrval_t hdrval, winpos_t *pcbytes));
static winpos_t lzupdate_rxpos P((achdrval_t rx_hdr, winpos_t rxpos,
winpos_t lrxpos, winpos_t txpos));
/*
* This macro replaces readchar() because it achieves a noticable speed up. The
* readchar() function has been renamed realreadchar(). Thanks to Ian for
* running this stuff through a profiler to find this out. Ian suggests further
* speed ups may be obtained by doing a similar thing in zrdat32().
*/
/* Assign the next character to b. */
#define READCHAR(qdaemon, b, i) \
(iPrecstart != iPrecend \
? ((b) = BUCHAR (abPrecbuf[iPrecstart]), \
iPrecstart = (iPrecstart + 1) % CRECBUFLEN) \
: ((b) = realreadchar ((qdaemon), (i))))
/************************************************************************/
/*
* Start the protocol ...
*/
boolean
fzstart(struct sdaemon *qdaemon, char **pzlog)
{
*pzlog = zbufalc (sizeof "protocol 'a' starting: , , , , , " + 100);
sprintf (*pzlog, "protocol 'a' starting: %d, %d, %d, %d, %d, %d",
cZtimeout, cZretries, cZstartup_retries,
cZmax_garbage, cZtx_window, fZesc_ctl);
if (! fconn_set (qdaemon->qconn, PARITYSETTING_NONE,
STRIPSETTING_EIGHTBITS, XONXOFF_OFF))
return FALSE;
/*
* For now, we place tight restrictions on the size of the transmit
* window. This might be relaxed in the future. If it is relaxed,
* some of these tests will stay, some will go. That is why it is
* coded like it is.
*/
if (cZtx_window % 1024 != 0 ||
cZtx_window < 4096 || cZtx_window > 65536 ||
65536 % cZtx_window != 0
) {
ulog (LOG_ERROR,
"fzstart: cZtx_window not one of 4096, 8192, 16384, 32768, 65536");
return FALSE;
}
zZtx_buf = (char *) xmalloc (CPACKETSIZE);
zZtx_packet_buf = (char *) xmalloc (CPACKBUFSIZE);
zZrx_packet_buf = (char *) xmalloc (CPACKBUFSIZE);
iZlast_tx_data_packet = -1;
iZlast_rx_data_packet = -1;
wpZtxpos = wpZlrxpos = wpZrxpos = wpZrxbytes = 0;
cZtxwspac = cZtx_window / 4;
cZheaders_sent = cZheaders_received = cZbytes_resent = 0;
cZtimeouts = cZerrors = 0;
iZpkt_rcvd_kludge = -1;
#if 0
/*
* We ensure <cZtx_window> is at least 4k, so the following is
* unnecessary. It can be put back in later if needed.
*/
if (cZblklen_override > cZtxwspac
|| (!cZblklen_override && cZtxwspac < 1024))
cZblklen_override = cZtxwspac;
#endif
#ifdef DJE_TESTING
{
extern int uucptest,uucptest2,uucptestseed;
FILE *f;
if (uucptest == -1) {
f = fopen ("/usr/local/src/bin/uucp/uucptest", "r");
if (f != NULL) {
fscanf (f, "%d %d %d",
&uucptestseed, &uucptest, &uucptest2);
fclose (f);
}
srand (uucptestseed);
}
}
#endif
/*
* Fire up the protocol (exchange init messages) ...
*/
if (!fzstart_proto (qdaemon))
return FALSE;
return TRUE;
}
/*
* Stop the protocol ...
*/
boolean
fzshutdown(struct sdaemon *qdaemon)
{
(void) fzshutdown_proto (qdaemon);
xfree ((pointer) zZtx_buf);
xfree ((pointer) zZtx_packet_buf);
xfree ((pointer) zZrx_packet_buf);
zZtx_buf = NULL;
zZtx_packet_buf = NULL;
zZrx_packet_buf = NULL;
/*
* Print some informative statistics ...
*
* I use the word "messages" here instead of "headers" because the
* latter is jargonese.
*/
ulog (LOG_NORMAL,
"Protocol 'a' messages: sent %lu, received %lu",
cZheaders_sent, cZheaders_received);
ulog (LOG_NORMAL,
"Protocol 'a' packets: sent %lu, received %lu",
wpZtxpos / 1024, wpZrxbytes / 1024);
if (cZbytes_resent != 0 || cZtimeouts != 0 || cZerrors != 0)
ulog (LOG_NORMAL,
"Protocol 'a' errors: bytes resent %lu, timeouts %lu, errors %lu",
cZbytes_resent, cZtimeouts, cZerrors);
/*
* Reset all the parameters to their default values, so that the
* protocol parameters used for this connection do not affect the
* next one.
*/
cZtimeout = CTIMEOUT;
cZretries = CRETRIES;
cZstartup_retries = CSTARTUP_RETRIES;
cZmax_garbage = CGARBAGE;
cZtx_window = CSEND_WINDOW;
fZesc_ctl = FESCAPE_CONTROL;
cZheaders_sent = cZheaders_received = cZbytes_resent = 0;
cZtimeouts = cZerrors = 0;
return TRUE;
}
/*
* Send a command string ...
* We send everything up to and including the null byte.
*
* We assume the command will fit in the outgoing data buffer.
* FIXME: A valid assumption?
*/
/*ARGSUSED*/
boolean
fzsendcmd(struct sdaemon *qdaemon, const char *z, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED)
{
size_t n,clen;
long lredo;
char *zbuf;
clen = strlen (z) + 1;
DEBUG_MESSAGE1 (DEBUG_PROTO, "fzsendcmd: sending command %s", z);
if (!fzstart_tx ()) /* must be called before zzgetspace() */
return FALSE;
if ((zbuf = zzgetspace (qdaemon, &n)) == NULL)
return FALSE;
#if DEBUG > 0
if (clen > n)
ulog (LOG_FATAL, "fzsendcmd: clen > n");
#endif
strcpy (zbuf, z);
/*
* Send it out ...
*/
do {
if (!fzsend_data (qdaemon, zbuf, clen, TRUE))
return FALSE;
if (!fzfinish_tx (qdaemon, &lredo))
return FALSE;
} while (lredo >= 0);
return fzprocess (qdaemon);
}
/*
* Allocate a packet to send out ...
*
* Note that 'z' has dynamic packet resizing and that <cZblklen> will range
* from 32 to 1024, in multiples of 2.
*/
/*ARGSUSED*/
char *
zzgetspace(struct sdaemon *qdaemon ATTRIBUTE_UNUSED, size_t *pclen)
{
*pclen = cZblklen;
return zZtx_buf;
}
/*
* Send a block of data ...
*
* If (cdata == 0) then the end of the file has been reached.
*/
/*ARGSUSED*/
boolean
fzsenddata(struct sdaemon *qdaemon, char *zdata, size_t cdata, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED, long int ipos ATTRIBUTE_UNUSED)
{
DEBUG_MESSAGE1 (DEBUG_PROTO, "fzsenddata: %d bytes", (int) cdata);
if (! fzsend_data (qdaemon, zdata, cdata, cdata == 0))
return FALSE;
return fzprocess (qdaemon);
}
/*
* Send a block of data (command or file) ...
*/
/* This should buffer the data internally. Until it does, it needs to
be able to reset the file position when it is called. This is
really ugly. */
extern struct stransfer *qTsend;
static boolean
fzsend_data(struct sdaemon *qdaemon, char *zdata, size_t cdata, boolean fendofmessage)
{
size_t n;
if (iZlast_tx_data_packet == -1 || iZlast_tx_data_packet == ZCRCW) {
cZtxwcnt = cZrxwcnt = 0;
iZjunk_count = 0;
if (!fzsend_hdr (qdaemon, ZBIN, ZDATA,
hvzencode_data_hdr (wpZtxpos), TRUE))
return FALSE;
}
n = cdata;
if (fendofmessage)
iZlast_tx_data_packet = ZCRCF;
else if (iZjunk_count > 3)
iZlast_tx_data_packet = ZCRCW;
else if (wpZtxpos == wpZlastsync)
iZlast_tx_data_packet = ZCRCW;
else if (cZrx_buf_len && (cZrxwcnt += n) >= (size_t) cZrx_buf_len)
iZlast_tx_data_packet = ZCRCW;
else if ((cZtxwcnt += n) >= cZtxwspac) {
iZlast_tx_data_packet = ZCRCQ;
cZtxwcnt = 0;
} else
iZlast_tx_data_packet = ZCRCG;
if (++iZtleft > 3) {
iZtleft = 0;
if (cZblklen < 1024)
cZblklen *= 2;
#if 0 /* <cZblklen_override> is currently unnecessary */
if (cZblklen_override && cZblklen > cZblklen_override)
cZblklen = cZblklen_override;
#endif
if (cZblklen > 1024)
cZblklen = 1024;
if (cZrx_buf_len && cZblklen > (size_t) cZrx_buf_len)
cZblklen = cZrx_buf_len;
}
#if DEBUG > 1
if (FDEBUGGING(DEBUG_PROTO)) {
const char *type;
switch (iZlast_tx_data_packet) {
case ZCRCW: type = "ZCRCW"; break;
case ZCRCG: type = "ZCRCG"; break;
case ZCRCQ: type = "ZCRCQ"; break;
case ZCRCE: type = "ZCRCE"; break;
case ZCRCF: type = "ZCRCF"; break;
default : type = "UNKNOWN!!!"; break;
}
DEBUG_MESSAGE3 (DEBUG_PROTO,
"fzsend_data: %s, pos 0x%lx, %d bytes",
type, wpZtxpos, (int) n);
}
#endif
if (!fzsend_data_packet (qdaemon, zdata, n, iZlast_tx_data_packet,
TRUE))
return FALSE;
wpZtxpos += n;
if (iZlast_tx_data_packet == ZCRCW) {
/*
* FIXME: Ideally this would be done in fzprocess. However, it
* is only called if there is data pending which there
* may not be yet. I could have patched fploop() a bit but
* for now, I've done it like this.
*/
switch (getinsync (qdaemon, FALSE)) {
case ZACK:
break;
case ZRPOS:
if (qTsend == NULL
|| ! ffileisopen (qTsend->e)) {
ulog (LOG_ERROR, "Can't reset non-file");
return FALSE;
}
iZlast_tx_data_packet = -1; /* trigger ZDATA */
DEBUG_MESSAGE1 (DEBUG_PROTO,
"fzsend_data: Seeking to %ld",
(long) (wpZrxpos - wpZtxstart));
if (!ffileseek (qTsend->e, wpZrxpos - wpZtxstart)) {
ulog (LOG_ERROR, "seek: %s", strerror (errno));
return FALSE;
}
break;
default:
return FALSE;
}
return TRUE;
}
/*
* If we've reached the maximum transmit window size, let the
* receiver catch up ...
*
* I use (cZtx_window - 2048) to play it safe.
*/
while (wpZtxpos - wpZlrxpos >= (size_t) cZtx_window - 2048) {
if (iZlast_tx_data_packet != ZCRCQ) {
if (!fzsend_data_packet (qdaemon, zdata, (size_t) 0,
iZlast_tx_data_packet = ZCRCQ,
TRUE))
return FALSE;
}
/*
* FIXME: I'd rather not call ffileseek() in this file. When we
* start buffering the outgoing data, the following
* ffileseek() will disappear.
*/
switch (getinsync (qdaemon, TRUE)) {
case ZACK:
break;
case ZRPOS:
if (qTsend == NULL
|| ! ffileisopen (qTsend->e)) {
ulog (LOG_ERROR, "Can't reset non-file");
return FALSE;
}
iZlast_tx_data_packet = -1; /* trigger ZDATA */
DEBUG_MESSAGE1 (DEBUG_PROTO,
"fzsend_data: Seeking to %ld",
(long) (wpZrxpos - wpZtxstart));
if (!ffileseek (qTsend->e, wpZrxpos - wpZtxstart)) {
ulog (LOG_ERROR, "seek: %s", strerror (errno));
return FALSE;
}
break;
default:
return FALSE;
}
}
return TRUE;
}
/*
* Process existing data ...
*/
static boolean
fzprocess(struct sdaemon *qdaemon)
{
int c,ch;
while (fzreceive_ready ()) {
READCHAR (qdaemon, ch, 1);
switch (ch) {
case ZPAD:
/* see if we're detecting ZRPOS packets quickly */
DEBUG_MESSAGE0 (DEBUG_PROTO,
"fzprocess: possible ZRPOS packet");
/* We just ate the ZPAD char that getinsync
expects, so put it back. */
iPrecstart = ((iPrecstart + CRECBUFLEN - 1)
% CRECBUFLEN);
c = getinsync (qdaemon, TRUE);
if (c == ZACK)
break;
/* FIXME: sz does a TCFLSH here */
#if 0 /* FIXME: Not sure if this is needed, or where to put it. */
/* ZCRCE - dinna wanna starta ping-pong game */
if (!fzsend_data_packet (qdaemon, zZtx_packet_buf,
0, ZCRCE, TRUE))
return FALSE;
#endif
if (c == ZRPOS) {
if (qTsend == NULL
|| ! ffileisopen (qTsend->e)) {
ulog (LOG_ERROR,
"Attempt to back up non-file");
return FALSE;
}
if (! ffileseek (qTsend->e,
wpZrxpos - wpZtxstart)) {
ulog (LOG_ERROR,
"seek: %s", strerror (errno));
return FALSE;
}
iZlast_tx_data_packet = -1; /* trigger ZDATA */
break; /* not returning is intentional */
}
return FALSE;
case XOFF:
case XOFF | 0200:
READCHAR (qdaemon, ch, XON_WAIT);
break;
case CR:
break;
default:
iZjunk_count++;
break;
}
}
return TRUE;
}
/*
* Wait for data to come in.
*
* This continues processing until a complete file or command has been
* received.
*/
boolean
fzwait(struct sdaemon *qdaemon)
{
int c,cerr,rxcount;
boolean fexit;
achdrval_t rx_hdr;
if (!fzstart_rx ())
return FALSE;
cerr = cZretries;
goto nxthdr;
for (;;) {
if (!fzsend_hdr (qdaemon, ZHEX, ZRPOS,
hvzencode_data_hdr (wpZrxbytes), FALSE))
return FALSE;
nxthdr:
c = izrecv_hdr (qdaemon, rx_hdr);
switch (c) {
case ZM_TIMEOUT:
case ZNAK:
if (--cerr < 0) {
ulog (LOG_ERROR, "fzwait: retries exhausted");
return FALSE;
}
continue;
case ZM_ERROR:
if (--cerr < 0) {
ulog (LOG_ERROR, "fzwait: retries exhausted");
return FALSE;
}
/*fport_break ();*/
continue;
case ZM_RCDO:
case ZFIN:
return FALSE;
case ZRPOS:
case ZACK:
goto nxthdr; /* ignore, partner is out of sync */