forked from quinot/taylor-uucp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proty.c
619 lines (492 loc) · 16.6 KB
/
proty.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
/* proty.c
The 'y' protocol.
Copyright (C) 1994, 1995, 2002, 2003 Jorge Cwik and 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.
*/
#include "uucp.h"
#if USE_RCS_ID
const char proty_id[] = "$Id$";
#endif
#include "uudefs.h"
#include "uuconf.h"
#include "conn.h"
#include "trans.h"
#include "system.h"
#include "prot.h"
/* The 'y' protocol, and this implementation, was written and designed
by Jorge Cwik <[email protected]>. Some of the routines, and the
coding style in general, were taken verbatim or adapted from other
Taylor UUCP modules. Mark Delany made the initial testings and
helped in portability issues.
This protocol does not perform any kind of error correction or flow
control. It does do error checking. It does not require an end to
end reliable link. It is recommended for error-free (also called
semi-reliable) connections as provided by error correcting modems.
It needs an eight bit clean channel and some kind of flow control
at the lower layers, typically RTS/CTS hardware flow control.
The flow of the file transmission is completely unidirectional.
There are no ACKs or NAKs outside file boundaries. This makes it
very suitable for half duplex modulations (like PEP) and
connections with very long delays, like multihop satellite links. */
/* This protocol uses 16 bit little-endian ints in the packet header. */
#define FROMLITTLE(p) (((p)[0] & 0xff) + (((p)[1] & 0xff) << 8))
#define TOLITTLE(p, i) ((p)[0] = (i) & 0xff, (p)[1] = ((i) >> 8) & 0xff)
/* The buffer and packet size we use. */
#define CYBUFSIZE (1024)
#define IYPACKSIZE (1024)
/* The offset in the buffer to the data. */
#define CYFRAMELEN (6)
/* Offsets in a packet header. */
#define YFRAME_SEQ_OFF (0)
#define YFRAME_LEN_OFF (2)
#define YFRAME_CTL_OFF (2)
#define YFRAME_CHK_OFF (4)
/* Offsets in a packet header viewed as an array of shorts. */
#define YFRAME_SEQ (0)
#define YFRAME_LEN (1)
#define YFRAME_CTL (1)
#define YFRAME_CHK (2)
/* The default timeout. */
#define CYTIMEOUT (60)
/* Control packet types. */
#define YPKT_ACK (0xFFFE)
#define YPKT_ERR (0xFFFD)
#define YPKT_BAD (0xFFFC)
/* The protocol version number. */
#define Y_VERSION (1)
/* When the protocol starts up, it transmit the following information:
1 byte version
1 byte packet size
2 byte flags (none currently defined)
Future revision may expand the structure as long as these members
keep their current offset. */
#define Y_INIT_HDR_LEN (4)
#define Y_INIT_HDR_VERSION_OFF (0)
#define Y_INIT_HDR_PKTSIZE_OFF (1)
#define Y_INIT_HDR_FLAGS_OFF (2)
/* The initialization length of the lowest accepted version. */
#define MIN_Y_SYNC (4)
/* Not strictly needed, but I would not want to accept a 32k sync pkt. */
#define MAX_Y_SYNC IYPACKSIZE
/* Local and remote packet sizes (we actually use the same value for
both). */
static size_t iYlocal_packsize = IYPACKSIZE;
static size_t iYremote_packsize = IYPACKSIZE;
/* Local and remote packet sequence numbers. */
static unsigned short iYlocal_pktnum;
static unsigned short iYremote_pktnum;
/* The timeout. */
static int cYtimeout = CYTIMEOUT;
/* Transmitter buffer. */
static char *zYbuf;
/* Protocol parameters. */
struct uuconf_cmdtab asYproto_params[] =
{
{ "timeout", UUCONF_CMDTABTYPE_INT, (pointer) &cYtimeout, NULL },
{ "packet-size", UUCONF_CMDTABTYPE_INT, (pointer) &iYlocal_packsize, NULL },
{ NULL, 0, NULL, NULL }
};
/* Local functions. */
static boolean fyxchg_syncs P((struct sdaemon *qdaemon));
static boolean fywait_for_packet P((struct sdaemon *qdaemon,
boolean *pfexit));
static unsigned short iychecksum P((const char *z, size_t c));
static unsigned short iychecksum2 P((const char *zfirst, size_t cfirst,
const char *zsecond, size_t csecond));
static boolean fywait_for_header P((struct sdaemon *qdaemon,
unsigned short header[3], int timeout));
static boolean fysend_pkt P((struct sdaemon *qdaemon,
const void *zdata, size_t cdata));
static boolean fysend_control P((struct sdaemon *qdaemon,
int itype));
static boolean fyread_data P((struct sdaemon *qdaemon, size_t clen,
int timeout));
/* Exchange sync packets at protocol startup. */
static boolean
fyxchg_syncs (struct sdaemon *qdaemon)
{
char inithdr[Y_INIT_HDR_LEN];
unsigned short header[3];
unsigned short ichk;
size_t clen, cfirst;
int rpktsize;
/* Send our configuration. We could use only one array (for local
and remote). But this is safer in case the code changes and
depend on separate ones. */
inithdr[Y_INIT_HDR_VERSION_OFF] = Y_VERSION;
inithdr[Y_INIT_HDR_PKTSIZE_OFF] = iYlocal_packsize >> 8;
TOLITTLE (inithdr + Y_INIT_HDR_FLAGS_OFF, 0);
if (! fysend_pkt (qdaemon, inithdr, Y_INIT_HDR_LEN))
return FALSE;
if (! fywait_for_header (qdaemon, header, cYtimeout))
return FALSE;
DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO, "fyxchg_syncs: Got sync header");
clen = header[YFRAME_LEN];
if (clen < MIN_Y_SYNC || clen > MAX_Y_SYNC)
{
ulog (LOG_ERROR, "Bad 'y' protocol sync packet length");
return FALSE;
}
/* It may be better to integrate this code with fywait_for_packet. */
if (! fyread_data (qdaemon, clen, cYtimeout))
return FALSE;
cfirst = CRECBUFLEN - iPrecstart;
ichk = iychecksum2 (abPrecbuf + iPrecstart, cfirst,
abPrecbuf, clen - cfirst);
rpktsize = BUCHAR (abPrecbuf[(iPrecstart + 1) % CRECBUFLEN]);
/* Future versions of the protocol may need to check and react
according to the version number. */
if (rpktsize == 0 || header[YFRAME_CHK] != ichk)
{
ulog (LOG_ERROR, "Bad 'y' protocol sync packet");
return FALSE;
}
iYremote_packsize = rpktsize << 8;
/* Some may want to do this different and in effect the protocol
support this. But I like the idea that the packet size would be
the same in both directions. This allows the caller to select
both packet sizes without changing the configuration at the
server. */
if (iYremote_packsize > iYlocal_packsize)
iYremote_packsize = iYlocal_packsize;
iPrecstart = (iPrecstart + clen) % CRECBUFLEN;
return TRUE;
}
/* Start the protocol. */
boolean
fystart (struct sdaemon *qdaemon, char **pzlog)
{
*pzlog = NULL;
/* This should force, or at least enable if available, RTS/CTS
hardware flow control !! */
/* The 'y' protocol requires an eight bit clean link */
if (! fconn_set (qdaemon->qconn, PARITYSETTING_NONE,
STRIPSETTING_EIGHTBITS, XONXOFF_OFF))
return FALSE;
iYlocal_pktnum = iYremote_pktnum = 0;
/* Only multiple of 256 sizes are allowed */
iYlocal_packsize &= ~0xff;
if (iYlocal_packsize < 256 || iYlocal_packsize > (16*1024))
iYlocal_packsize = IYPACKSIZE;
/* Exhange SYNC packets */
if (! fyxchg_syncs (qdaemon))
{
/* Restore defaults */
cYtimeout = CYTIMEOUT;
iYlocal_packsize = IYPACKSIZE;
return FALSE;
}
zYbuf = (char *) xmalloc (CYBUFSIZE + CYFRAMELEN);
return TRUE;
}
/* Shutdown the protocol. */
boolean
fyshutdown (struct sdaemon *qdaemon ATTRIBUTE_UNUSED)
{
xfree ((pointer) zYbuf);
zYbuf = NULL;
cYtimeout = CYTIMEOUT;
iYlocal_packsize = IYPACKSIZE;
return TRUE;
}
/* Send a command string. We send packets containing the string until
the entire string has been sent, including the zero terminator. */
/*ARGSUSED*/
boolean
fysendcmd (struct sdaemon *qdaemon, const char *z, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED)
{
size_t clen;
DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "fysendcmd: Sending command \"%s\"", z);
clen = strlen (z) + 1;
while (clen > 0)
{
size_t csize;
csize = clen;
if (csize > iYremote_packsize)
csize = iYremote_packsize;
if (! fysend_pkt (qdaemon, z, csize))
return FALSE;
z += csize;
clen -= csize;
}
return TRUE;
}
/* Get space to be filled with data. We always use zYbuf, which was
allocated from the heap. */
char *
zygetspace (struct sdaemon *qdaemon ATTRIBUTE_UNUSED, size_t *pclen)
{
*pclen = iYremote_packsize;
return zYbuf + CYFRAMELEN;
}
/* Send out a data packet. */
boolean
fysenddata (struct sdaemon *qdaemon, char *zdata, size_t cdata, int ilocal ATTRIBUTE_UNUSED, int iremote ATTRIBUTE_UNUSED, long int ipos ATTRIBUTE_UNUSED)
{
#if DEBUG > 0
if (cdata > iYremote_packsize)
ulog (LOG_FATAL, "fysend_packet: Packet size too large");
#endif
TOLITTLE (zYbuf + YFRAME_SEQ_OFF, iYlocal_pktnum);
++iYlocal_pktnum;
TOLITTLE (zYbuf + YFRAME_LEN_OFF, cdata);
TOLITTLE (zYbuf + YFRAME_CHK_OFF, iychecksum (zdata, cdata));
/* We pass FALSE to fsend_data since we don't expect the other side
to be sending us anything just now. */
return fsend_data (qdaemon->qconn, zYbuf, cdata + CYFRAMELEN, FALSE);
}
/* Wait for data to come in and process it until we've finished a
command or a file. */
boolean
fywait (struct sdaemon *qdaemon)
{
boolean fexit = FALSE;
while (! fexit)
{
if (! fywait_for_packet (qdaemon, &fexit))
return FALSE;
}
return TRUE;
}
/* File level routines
We could handle this inside the other public routines,
but this is cleaner and better for future expansions */
boolean
fyfile (struct sdaemon *qdaemon, struct stransfer *qtrans ATTRIBUTE_UNUSED, boolean fstart, boolean fsend, long int cbytes ATTRIBUTE_UNUSED, boolean *pfhandled)
{
unsigned short header[3];
*pfhandled = FALSE;
if (! fstart)
{
if (fsend)
{
/* It is critical that the timeout here would be long
enough. We have just sent a full file without any kind
of flow control at the protocol level. The traffic may
be buffered in many places of the link, and the remote
may take a while until cathing up. */
if (! fywait_for_header (qdaemon, header, cYtimeout * 2))
return FALSE;
if (header[YFRAME_CTL] != (unsigned short) YPKT_ACK)
{
DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
"fyfile: Error from remote: 0x%04X", header[1]);
ulog (LOG_ERROR, "Received 'y' protocol error from remote");
return FALSE;
}
}
else
{
/* This is technically not requireed. But I've put this in
the protocol to allow easier expansions. */
return fysend_control (qdaemon, YPKT_ACK);
}
}
return TRUE;
}
/* Send a control packet, not used during the normal file
transmission. */
static boolean
fysend_control (struct sdaemon *qdaemon, int itype)
{
char header[CYFRAMELEN];
TOLITTLE (header + YFRAME_SEQ_OFF, iYlocal_pktnum);
iYlocal_pktnum++;
TOLITTLE (header + YFRAME_CTL_OFF, itype);
TOLITTLE (header + YFRAME_CHK_OFF, 0);
return fsend_data (qdaemon->qconn, header, CYFRAMELEN, FALSE);
}
/* Private function to send a packet. This one doesn't need the data
to be in the buffer provided by zygetspace. I've found it worth
for avoiding memory copies. Somebody may want to do it otherwise */
static boolean
fysend_pkt (struct sdaemon *qdaemon, const void *zdata, size_t cdata)
{
char header[CYFRAMELEN];
TOLITTLE (header + YFRAME_SEQ_OFF, iYlocal_pktnum);
iYlocal_pktnum++;
TOLITTLE (header + YFRAME_LEN_OFF, cdata);
TOLITTLE (header + YFRAME_CHK_OFF, iychecksum (zdata, cdata));
if (! fsend_data (qdaemon->qconn, header, CYFRAMELEN, FALSE))
return FALSE;
return fsend_data (qdaemon->qconn, zdata, cdata, FALSE);
}
/* Wait until enough data arrived from the comm line. This protocol
doesn't need to perform any kind of action while waiting. */
static boolean
fyread_data (struct sdaemon *qdaemon, size_t clen, int timeout)
{
int cinbuf;
size_t crec;
cinbuf = iPrecend - iPrecstart;
if (cinbuf < 0)
cinbuf += CRECBUFLEN;
if ((size_t) cinbuf < clen)
{
if (! freceive_data (qdaemon->qconn, clen - cinbuf, &crec,
timeout, TRUE))
return FALSE;
cinbuf += crec;
if ((size_t) cinbuf < clen)
{
if (! freceive_data (qdaemon->qconn, clen - cinbuf, &crec,
timeout, TRUE))
return FALSE;
}
cinbuf += crec;
if ((size_t) cinbuf < clen)
{
ulog (LOG_ERROR, "Timed out waiting for data");
return FALSE;
}
}
return TRUE;
}
/* Receive a remote packet header, check for correct sequence number. */
static boolean
fywait_for_header (struct sdaemon *qdaemon, short unsigned int *header, int timeout)
{
if (! fyread_data (qdaemon, CYFRAMELEN, timeout))
return FALSE;
/* Somebody may want to optimize this in a portable way. I'm not
sure it's worth, but the output by gcc for the portable construct
is so bad (even with optimization), that I couldn't resist. */
if (iPrecstart <= (CRECBUFLEN - CYFRAMELEN))
{
header[0] = FROMLITTLE (abPrecbuf + iPrecstart);
header[1] = FROMLITTLE (abPrecbuf + iPrecstart + 2);
header[2] = FROMLITTLE (abPrecbuf + iPrecstart + 4);
}
else
{
register int i, j;
for (i = j = 0; j < CYFRAMELEN; i++, j += 2)
{
header[i] =
(((abPrecbuf[(iPrecstart + j + 1) % CRECBUFLEN] & 0xff) << 8)
+ (abPrecbuf[(iPrecstart + j) % CRECBUFLEN] & 0xff));
}
}
iPrecstart = (iPrecstart + CYFRAMELEN) % CRECBUFLEN;
DEBUG_MESSAGE3 (DEBUG_UUCP_PROTO,
"fywait_for_header: Got header: 0x%04X, 0x%04X, 0x%04X",
header[0], header[1], header[2]);
if (header[YFRAME_SEQ] != iYremote_pktnum++)
{
ulog (LOG_ERROR, "Incorrect 'y' packet sequence");
fysend_control (qdaemon, YPKT_BAD);
return FALSE;
}
return TRUE;
}
/* Receive a remote data packet */
static boolean
fywait_for_packet (struct sdaemon *qdaemon, boolean *pfexit)
{
unsigned short header[3], ichk;
size_t clen, cfirst;
if (! fywait_for_header (qdaemon, header, cYtimeout))
return FALSE;
clen = header[YFRAME_LEN];
if (clen == 0 && pfexit != NULL)
{
/* I Suppose the pointers could be NULL ??? */
return fgot_data (qdaemon, abPrecbuf, 0, abPrecbuf, 0,
-1, -1, (long) -1, TRUE, pfexit);
}
if (clen & 0x8000)
{
DEBUG_MESSAGE1 (DEBUG_PROTO | DEBUG_ABNORMAL,
"fywait_for_packet: Error from remote: 0x%04X",
header[YFRAME_CTL]);
ulog (LOG_ERROR, "Remote error packet");
return FALSE;
}
/* This is really not neccessary. But if this check is removed,
take in mind that the packet may be up to 32k long. */
if (clen > iYlocal_packsize)
{
ulog (LOG_ERROR, "Packet too large");
return FALSE;
}
if (! fyread_data (qdaemon, clen, cYtimeout))
return FALSE;
cfirst = CRECBUFLEN - iPrecstart;
if (cfirst > clen)
cfirst = clen;
if (cfirst == clen)
ichk = iychecksum (abPrecbuf + iPrecstart, clen);
else
ichk = iychecksum2 (abPrecbuf + iPrecstart, cfirst,
abPrecbuf, clen - cfirst);
if (header[YFRAME_CHK] != ichk)
{
DEBUG_MESSAGE2 (DEBUG_PROTO | DEBUG_ABNORMAL,
"fywait_for_packet: Bad checksum 0x%x != 0x%x",
header[YFRAME_CHK], ichk);
fysend_control (qdaemon, YPKT_ERR);
ulog (LOG_ERROR, "Checksum error");
return FALSE;
}
if (pfexit != NULL
&& ! fgot_data (qdaemon, abPrecbuf + iPrecstart, cfirst,
abPrecbuf, clen - cfirst,
-1, -1, (long) -1, TRUE, pfexit))
return FALSE;
iPrecstart = (iPrecstart + clen) % CRECBUFLEN;
return TRUE;
}
/* Compute 16 bit checksum */
#ifdef __GNUC__
#ifdef __i386__
#define I386_ASM
#endif
#endif
#ifdef I386_ASM
#define ROTATE(i) \
asm ("rolw $1,%0" : "=g" (i) : "g" (i))
#else
#define ROTATE(i) i += i + ((i & 0x8000) >> 15)
#endif
static unsigned short
iychecksum (register const char *z, register size_t c)
{
register unsigned short ichk;
ichk = 0xffff;
while (c-- > 0)
{
ROTATE (ichk);
ichk += BUCHAR (*z++);
}
return ichk;
}
static unsigned short
iychecksum2 (const char *zfirst, size_t cfirst, const char *zsecond, size_t csecond)
{
register unsigned short ichk;
register const char *z;
register size_t c;
z = zfirst;
c = cfirst + csecond;
ichk = 0xffff;
while (c-- > 0)
{
ROTATE (ichk);
ichk += BUCHAR (*z++);
/* If the first buffer has been finished, switch to the second. */
if (--cfirst == 0)
z = zsecond;
}
return ichk;
}