Copyright (C) 1991, 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA.
The author of the program may be contacted at ian@airs.com or
c/o AIRS, P.O. Box 520, Waltham, MA 02254.
Revision 1.15 1992/03/30 04:49:10 ian
Niels Baggesen: added debugging types abnormal and uucp-proto
Revision 1.14 1992/03/17 01:03:03 ian
Revision 1.13 1992/03/13 22:59:25 ian
Have breceive_char go through freceive_data
Revision 1.12 1992/03/12 19:56:10 ian
Debugging based on types rather than number
Revision 1.11 1992/02/08 03:54:18 ian
Include <string.h> only in <uucp.h>, added 1992 copyright
Revision 1.10 1992/01/16 18:16:58 ian
Niels Baggesen: add some debugging messages
Revision 1.9 1992/01/14 04:35:23 ian
Chip Salzenberg: implement this patch correctly
Revision 1.8 1992/01/14 04:21:59 ian
Chip Salzenberg: avoid use before set warning
Revision 1.7 1991/12/31 19:34:19 ian
Added number of bytes to pffile protocol entry point
Revision 1.6 1991/12/20 03:02:01 ian
Oleg Tabarovsky: added statistical messages to 'g' and 'f' protocols
Revision 1.5 1991/12/20 00:01:54 ian
Franc,ois Pinard: don't crash 'f' protocol because of an illegal byte
Revision 1.4 1991/11/16 00:31:01 ian
Increased default 't' and 'f' protocol timeouts
Revision 1.3 1991/11/15 23:32:15 ian
Don't use 1 second timeouts--loses data on System V
Revision 1.2 1991/11/15 21:00:59 ian
Efficiency hacks for 'f' and 't' protocols
Revision 1.1 1991/11/11 04:21:16 ian
char protf_rcsid
[] = "$Id: protf.c,v 1.15 1992/03/30 04:49:10 ian Rel $";
/* This implementation is based on code by Piet Beertema, CWI,
This code implements the 'f' protocol, which requires a
flow-controlled error-free seven-bit data path. It does check for
errors, but only at the end of each file transmission, so a noisy
line without error correcting modems will be unusable.
The conversion to seven bit data is done as follows, where b
represents the character to convert:
0 <= b <= 037: 0172, b + 0100 (0100 to 0137)
040 <= b <= 0171: b ( 040 to 0171)
0172 <= b <= 0177: 0173, b - 0100 ( 072 to 077)
0200 <= b <= 0237: 0174, b - 0100 (0100 to 0137)
0240 <= b <= 0371: 0175, b - 0200 ( 040 to 0171)
0372 <= b <= 0377: 0176, b - 0300 ( 072 to 077)
This causes all output bytes to be in the range 040 to 0176; these
are the printable ASCII characters. */
/* Internal functions. */
static boolean ffprocess_data
P((boolean
*pfexit
, int *pcneed
));
/* The size of the buffer we allocate to store outgoing data in. */
/* The timeout to wait for data to arrive before giving up. */
static int cFtimeout
= 120;
/* The maximum number of retries. */
static int cFmaxretries
= 2;
/* The buffer we allocate for outgoing data. */
/* TRUE if we are receiving a file rather than a command. */
/* The checksum so far. */
static unsigned int iFcheck
;
/* The last special byte (0172 to 0176) or 0 if none. */
/* The number of times we have retried this file. */
struct scmdtab asFproto_params
[] =
{ "timeout", CMDTABTYPE_INT
, (pointer
) &cFtimeout
, NULL
},
{ "retries", CMDTABTYPE_INT
, (pointer
) &cFmaxretries
, NULL
},
/* The number of data bytes sent in files. */
/* The number of actual bytes sent in files. */
static long cFsent_bytes
;
/* The number of data bytes received in files. */
/* The number of actual bytes received in files. */
/* The number of file retries when sending. */
static long cFsend_retries
;
/* The number of file retries when receiving. */
static long cFrec_retries
;
/* Start the protocol. */
/* Allow XON/XOFF to work. */
if (! fport_set (PORTSETTING_SEVEN
))
/* We sleep to allow the other side to reset the terminal; this is
what Mr. Beertema's code does. */
/* Shutdown the protocol. */
"Protocol 'f': sent %ld bytes for %ld, received %ld bytes for %ld",
cFsent_bytes
, cFsent_data
, cFrec_bytes
, cFrec_data
);
if (cFsend_retries
!= 0 || cFrec_retries
!= 0)
ulog (LOG_NORMAL
, "Protocol 'f' file retries: %ld sending, %ld receiving",
cFsend_retries
, cFrec_retries
);
/* Send a command string. We just send the string followed by a carriage
DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO
, "ffsendcmd: Sending command \"%s\"", z
);
zalc
= (char *) alloca (clen
+ 2);
sprintf (zalc
, "%s\r", z
);
return fsend_data (zalc
, clen
+ 1, TRUE
);
/* Get space to be filled with data. We allocate the space from the
zFbuf
= (char *) xmalloc (CFBUFSIZE
);
/* Send out a data packet. We have to encode the data into seven bits
and accumulate a checksum. */
ffsenddata (zdata
, cdata
)
register unsigned int itmpchk
;
/* Rotate the checksum left. */
if ((itmpchk
& 0x8000) == 0)
/* Add the next byte into the checksum. */
*ze
++ = (char) (b
+ 0100);
*ze
++ = (char) (b
- 0100);
*ze
++ = (char) (b
- 0100);
*ze
++ = (char) (b
- 0200);
*ze
++ = (char) (b
- 0300);
/* Passing FALSE tells fsend_data not to bother looking for incoming
information, since we really don't expect any. */
return fsend_data (ab
, ze
- ab
, FALSE
);
/* Process any data in the receive buffer. */
return ffprocess_data (pfexit
, (int *) NULL
);
/* Process data and return the amount of data we are looking for in
*pcneed. The 'f' protocol doesn't really reveal this, but when
transferring file we know that we need at least seven characters
ffprocess_data (pfexit
, pcneed
)
register unsigned int itmpchk
;
/* A command continues until a '\r' character, which we turn
into '\0' before calling fgot_data. */
while (iPrecstart
!= iPrecend
)
for (i
= iPrecstart
; i
< CRECBUFLEN
&& i
!= iPrecend
; i
++)
if (abPrecbuf
[i
] == '\r')
iPrecstart
= (i
+ 1) % CRECBUFLEN
;
return fgot_data (abPrecbuf
+ istart
, i
- istart
+ 1,
if (! fgot_data (abPrecbuf
+ iPrecstart
, i
- iPrecstart
,
iPrecstart
= i
% CRECBUFLEN
;
/* Here the data is destined for a file, and we must decode it. */
while (iPrecstart
!= iPrecend
)
char *zstart
, *zto
, *zfrom
;
zto
= zfrom
= zstart
= abPrecbuf
+ iPrecstart
;
c
= iPrecend
- iPrecstart
;
c
= CRECBUFLEN
- iPrecstart
;
ulog (LOG_ERROR
, "Illegal byte %d", b
);
/* Characters >= 0172 are always special characters. The
only legal pair of consecutive special characters
are 0176 0176 which immediately precede the four
if (bFspecial
!= 0176 || b
!= 0176)
ulog (LOG_ERROR
, "Illegal bytes %d %d",
/* Pass any initial data. */
/* Don't count the checksum in the received bytes. */
cFrec_bytes
+= zfrom
- zstart
- 2;
cFrec_data
+= zto
- zstart
;
if (! fgot_data (zstart
, zto
- zstart
, FALSE
,
/* The next characters we want to read are the
checksum, so skip the second 0176. */
iPrecstart
= (iPrecstart
+ zfrom
- zstart
) % CRECBUFLEN
;
/* Tell fgot_data that we've read the entire file by
passing 0 length data. This will set *pfexit to
TRUE and call fffile to verify the checksum. */
return fgot_data ((char *) NULL
, 0, FALSE
, TRUE
, pfexit
);
/* Here we have encountered a special character that
does not follow another special character. */
/* Here we have encountered a nonspecial character. */
/* Rotate the checksum left. */
if ((itmpchk
& 0x8000) == 0)
/* Add the next byte into the checksum. */
DEBUG_MESSAGE1 (DEBUG_PROTO
,
"ffprocess: Calling fgot_data with %d bytes",
cFrec_data
+= zto
- zstart
;
if (! fgot_data (zstart
, zto
- zstart
, FALSE
, TRUE
, pfexit
))
cFrec_bytes
+= zfrom
- zstart
;
iPrecstart
= (iPrecstart
+ zfrom
- zstart
) % CRECBUFLEN
;
/* At this point we may have seen the first 0176 in the checksum
but not the second. The checksum is at least seven
characters long (0176 0176 a b c d \r). This won't help
much, but reading seven characters is a lot better than
reading two, which is what I saw in a 2400 baud log file. */
/* Wait for data to come in and process it until we've finished a
if (! ffprocess_data (&fexit
, &cneed
))
/* We only ask for one character at a time. This could wind up
being quite inefficient, since we might only get one
character back from each read. We really want to do
something like get all available characters, then sleep for
half a second and get all available characters again, and
keep this up until we don't get anything after sleeping. */
if (! freceive_data (cneed
, &crec
, cFtimeout
, TRUE
))
ulog (LOG_ERROR
, "Timed out waiting for data");
/* File level operations. Reset the checksums when starting to send
or receive a file, and output the checksum when we've finished
fffile (fstart
, fsend
, pfredo
, cbytes
)
/* Send the final checksum. */
sprintf (ab
, "\176\176%04x\r", iFcheck
& 0xffff);
if (! fsend_data (ab
, 7, TRUE
))
/* Now look for the acknowledgement. */
/* An R means to retry sending the file. */
if (cFretries
> cFmaxretries
)
ulog (LOG_ERROR
, "Too many retries");
DEBUG_MESSAGE1 (DEBUG_PROTO
, "fffile: Got \"%s\"", z
);
ulog (LOG_ERROR
, "File send failed");
/* We next expect to receive a command. */
ulog (LOG_ERROR
, "Bad checksum format");
icheck
= strtol (z
, (char **) NULL
, 16);
if (icheck
!= (iFcheck
& 0xffff))
DEBUG_MESSAGE2 (DEBUG_PROTO
| DEBUG_ABNORMAL
,
"Checksum failed; calculated 0x%x, got 0x%x",
iFcheck
& 0xffff, icheck
);
if (cFretries
> cFmaxretries
)
ulog (LOG_ERROR
, "Too many retries");
/* Send an R to tell the other side to resend the file. */
/* Send a G to tell the other side the file was received