This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbsebbs/ymsend.c

472 lines
11 KiB
C
Raw Normal View History

2004-11-22 15:12:55 +00:00
/*****************************************************************************
*
* $Id$
* Purpose ...............: Ymodem sender
*
*****************************************************************************
* Copyright (C) 1997-2004
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS 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, or (at your option) any
* later version.
*
* MBSE BBS 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 MBSE BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************/
#include "../config.h"
#include "../lib/mbselib.h"
#include "../lib/mbse.h"
2004-11-22 16:45:24 +00:00
#include "ttyio.h"
#include "zmmisc.h"
#include "transfer.h"
#include "openport.h"
#include "timeout.h"
#include "term.h"
2004-11-22 22:05:57 +00:00
#include "ymsend.h"
2004-11-22 16:45:24 +00:00
#define MAX_BLOCK 8192
#define sendline(c) PUTCHAR((c & 0377))
2004-11-22 16:45:24 +00:00
2004-11-22 22:05:57 +00:00
FILE *input_f;
char Crcflg;
char Lastrx;
int Fullname = 0; /* transmit full pathname */
int Filesleft;
long Totalleft;
long bytes_sent;
int firstsec;
int Optiong; /* Let it rip no wait for sector ACK's */
int Totsecs; /* total number of sectors this file */
char *txbuf;
size_t blklen = 128; /* length of transmitted records */
2004-11-23 20:46:21 +00:00
int zmodem_requested = FALSE;
2004-11-22 22:05:57 +00:00
static int no_unixmode;
struct timeval starttime, endtime;
struct timezone tz;
long skipsize;
2004-11-22 16:45:24 +00:00
extern int Rxtimeout;
2004-11-22 22:05:57 +00:00
static int wctxpn(char *);
2004-11-22 16:45:24 +00:00
static int getnak(void);
2004-11-22 22:05:57 +00:00
static int wctx(long);
static int wcputsec(char *, int, size_t);
2004-11-22 16:45:24 +00:00
static size_t filbuf(char *, size_t);
2004-11-22 22:05:57 +00:00
int ymsndfiles(down_list *lst, int use1k)
{
int maxrc = 0;
down_list *tmpf;
Syslog('+', "%s: start send files", protname());
txbuf = malloc(MAX_BLOCK);
/*
* Count files to transmit
*/
Totalleft = Filesleft = 0;
for (tmpf = lst; tmpf; tmpf = tmpf->next) {
if (tmpf->remote) {
Filesleft++;
Totalleft += tmpf->size;
}
}
Syslog('x', "%s: %d files, size %d bytes", protname(), Filesleft, Totalleft);
/*
* This is not documented as auto start sequence, but a lot of terminal
* programs seem to react on this.
*/
if (protocol == ZM_YMODEM)
PUTSTR((char *)"rb\r");
2004-11-22 22:05:57 +00:00
for (tmpf = lst; tmpf && (maxrc < 2); tmpf = tmpf->next) {
if (tmpf->remote) {
bytes_sent = 0;
skipsize = 0L;
Totsecs = 0;
switch (wctxpn(tmpf->remote)) {
case TERROR: Syslog('x', "wctxpn returns error");
tmpf->failed = TRUE;
maxrc = 2;
break;
case ZSKIP: Syslog('x', "wctxpn returns skip");
tmpf->failed = TRUE;
break;
case OK: gettimeofday(&starttime, &tz);
if ((blklen == 128) && use1k) {
Syslog('x', "%s: will use 1K blocks", protname());
blklen = 1024;
}
2004-11-27 12:32:27 +00:00
if (wctx(tmpf->size) == TERROR) {
Syslog('+', "%s: wctx returned error", protname());
2004-11-22 22:05:57 +00:00
tmpf->failed = TRUE;
} else {
tmpf->sent = TRUE;
gettimeofday(&endtime, &tz);
Syslog('+', "%s: OK %s", protname(),
transfertime(starttime, endtime, (unsigned long)tmpf->size - skipsize, TRUE));
}
}
} else if (maxrc == 0) {
tmpf->failed = TRUE;
}
if (unlink(tmpf->remote))
Syslog('+', "%s: could not unlink %s", protname(), tmpf->remote);
}
2004-11-27 12:32:27 +00:00
if (maxrc == 2) {
canit(1);
purgeline(50);
} else {
if (protocol == ZM_YMODEM) {
/*
* Send empty filename to signal end of batch
*/
wctxpn((char *)"");
}
2004-11-22 22:05:57 +00:00
}
if (txbuf)
free(txbuf);
txbuf = NULL;
io_mode(0, 1);
2004-11-27 12:32:27 +00:00
Syslog('+', "%s: end send files rc=%d", protname(), maxrc);
2004-11-22 22:05:57 +00:00
return (maxrc < 2)?0:maxrc;
}
2004-11-22 16:45:24 +00:00
/*
* generate and transmit pathname block consisting of
* pathname (null terminated),
* file length, mode time and file mode in octal
* as provided by the Unix fstat call.
* N.B.: modifies the passed name, may extend it!
*/
static int wctxpn(char *fname)
{
register char *p, *q;
char *name2;
struct stat f;
name2 = alloca(PATH_MAX+1);
2004-11-22 22:05:57 +00:00
if ((input_f = fopen(fname, "r"))) {
fstat(fileno(input_f), &f);
Syslog('+', "%s: send \"%s\"", protname(), MBSE_SS(fname));
Syslog('+', "%s: size %lu bytes, dated %s", protname(), (unsigned long)f.st_size, rfcdate(f.st_mtime));
if (protocol == ZM_XMODEM) {
if (*fname) {
sprintf(name2, "Sending %s, %ld blocks: ", fname, (long) (f.st_size >> 7));
PUTSTR(name2);
Enter(1);
}
PUTSTR((char *)"Give your local XMODEM receive command now.");
Enter(1);
return OK;
2004-11-22 16:45:24 +00:00
}
2004-11-22 22:05:57 +00:00
} else {
/*
* Reset, this normally happens for the ymodem end of batch block.
*/
f.st_size = 0;
f.st_mtime = 0;
f.st_mode = 0;
2004-11-22 16:45:24 +00:00
}
2004-11-23 20:46:21 +00:00
if (!zmodem_requested)
2004-11-22 16:45:24 +00:00
if (getnak()) {
2004-11-27 12:32:27 +00:00
Syslog('+', "%s: timeout sending filename %s", protname(), MBSE_SS(fname));
2004-11-22 22:05:57 +00:00
return TERROR;
2004-11-22 16:45:24 +00:00
}
q = (char *) 0;
for (p = fname, q = txbuf ; *p; )
if ((*q++ = *p++) == '/' && !Fullname)
q = txbuf;
*q++ = 0;
p = q;
while (q < (txbuf + MAX_BLOCK))
*q++ = 0;
2004-11-22 22:05:57 +00:00
2004-11-22 16:45:24 +00:00
/*
* note that we may lose some information here in case mode_t is wider than an
* int. But i believe sending %lo instead of %o _could_ break compatability
*/
2004-11-22 22:05:57 +00:00
if ((input_f != stdin) && *fname)
2004-11-22 16:45:24 +00:00
sprintf(p, "%lu %lo %o 0 %d %ld", (long) f.st_size, f.st_mtime,
(unsigned int)((no_unixmode) ? 0 : f.st_mode), Filesleft, Totalleft);
// sprintf(p, "%lu %lo 0 0 0 0", (long) f.st_size, f.st_mtime);
2004-11-22 22:05:57 +00:00
2004-11-22 16:45:24 +00:00
Totalleft -= f.st_size;
if (--Filesleft <= 0)
Totalleft = 0;
if (Totalleft < 0)
Totalleft = 0;
purgeline(1);
2004-11-27 12:32:27 +00:00
if (wcputsec(txbuf, 0, 128) == TERROR) {
Syslog('+', "%s: failed to send filename", protname());
2004-11-22 22:05:57 +00:00
return TERROR;
2004-11-22 16:45:24 +00:00
}
return OK;
}
/*
* Get NAK, C or G
*/
2004-11-22 16:45:24 +00:00
static int getnak(void)
{
int firstch;
int tries = 0;
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak()");
2004-11-22 16:45:24 +00:00
Lastrx = 0;
2004-11-22 22:05:57 +00:00
2004-11-22 16:45:24 +00:00
for (;;) {
tries++;
switch (firstch = GETCHAR(10)) {
// case ZPAD:
// if (getzrxinit())
// return ERROR;
// Ascii = 0; /* Receiver does the conversion */
// return FALSE;
case TIMEOUT:
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak: timeout try %d", tries);
/* 50 seconds are enough (was 30, 26-11-2004 MB) */
2004-11-27 12:32:27 +00:00
if (tries == 5) {
2004-11-22 22:05:57 +00:00
Syslog('x', "Timeout on pathname");
2004-11-22 16:45:24 +00:00
return TRUE;
}
/* don't send a second ZRQINIT _directly_ after the
* first one. Never send more then 4 ZRQINIT, because
* omen rz stops if it saw 5 of them */
// if ((zrqinits_sent>1 || tries>1) && zrqinits_sent<4) {
/* if we already sent a ZRQINIT we are using zmodem
* protocol and may send further ZRQINITs
*/
// stohdr(0L);
// zshhdr(ZRQINIT, Txhdr);
// zrqinits_sent++;
// }
continue;
case WANTG:
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak: got WANTG");
2004-11-22 16:45:24 +00:00
io_mode(0, 2); /* Set cbreak, XON/XOFF, etc. */
2004-11-27 23:36:54 +00:00
if (!Optiong)
Syslog('+', "%s: using Ymodem-G", protname());
2004-11-22 16:45:24 +00:00
Optiong = TRUE;
blklen=1024;
Crcflg = TRUE;
return FALSE;
2004-11-22 16:45:24 +00:00
case WANTCRC:
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak: got WANTCRC");
2004-11-22 16:45:24 +00:00
Crcflg = TRUE;
return FALSE;
2004-11-22 16:45:24 +00:00
case NAK:
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak: got NAK");
2004-11-22 16:45:24 +00:00
return FALSE;
case CAN:
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak: got CAN");
if ((firstch = GETCHAR(2)) == CAN && Lastrx == CAN) {
Syslog('+', "%s: Receiver cancelled", protname());
2004-11-22 16:45:24 +00:00
return TRUE;
}
2004-11-22 16:45:24 +00:00
default:
2004-11-27 12:32:27 +00:00
Syslog('x', "got %d", firstch);
2004-11-22 16:45:24 +00:00
break;
}
Lastrx = firstch;
}
2004-11-22 22:05:57 +00:00
Syslog('x', "getnak: done");
2004-11-22 16:45:24 +00:00
}
static int wctx(long bytes_total)
{
register size_t thisblklen;
register int sectnum, attempts, firstch;
2004-11-25 21:58:26 +00:00
firstsec=TRUE;
thisblklen = blklen;
2004-11-22 22:05:57 +00:00
Syslog('x', "wctx: file length=%ld, blklen=%d", bytes_total, blklen);
2004-11-22 16:45:24 +00:00
2004-11-25 21:58:26 +00:00
while ((firstch = GETCHAR(Rxtimeout)) != NAK && firstch != WANTCRC
2004-11-22 16:45:24 +00:00
&& firstch != WANTG && firstch != TIMEOUT && firstch != CAN);
if (firstch == CAN) {
Syslog('+', "%s: receiver Cancelled during transfer", protname());
2004-11-22 22:05:57 +00:00
return TERROR;
2004-11-22 16:45:24 +00:00
}
if (firstch == WANTCRC)
Crcflg = TRUE;
if (firstch == WANTG)
Crcflg = TRUE;
sectnum = 0;
for (;;) {
if (bytes_total <= (bytes_sent + 896L))
thisblklen = 128;
if ( !filbuf(txbuf, thisblklen))
break;
if (wcputsec(txbuf, ++sectnum, thisblklen) == TERROR)
2004-11-22 22:05:57 +00:00
return TERROR;
2004-11-22 16:45:24 +00:00
bytes_sent += thisblklen;
2004-11-25 21:00:04 +00:00
/*
* Keep connections alive
*/
Nopper();
alarm_on();
2004-11-22 16:45:24 +00:00
}
fclose(input_f);
attempts = 0;
do {
2004-11-22 22:05:57 +00:00
purgeline(5);
2004-11-22 16:45:24 +00:00
PUTCHAR(EOT);
++attempts;
} while ((firstch = (GETCHAR(Rxtimeout)) != ACK) && attempts < RETRYMAX);
if (attempts == RETRYMAX) {
2004-11-22 22:05:57 +00:00
Syslog('x', "No ACK on EOT");
return TERROR;
2004-11-22 16:45:24 +00:00
} else
return OK;
}
2004-11-22 22:05:57 +00:00
static int wcputsec(char *buf, int sectnum, size_t cseclen)
2004-11-22 16:45:24 +00:00
{
int Checksum, wcj;
char *cp;
unsigned oldcrc;
int firstch;
int attempts;
firstch = 0; /* part of logic to detect CAN CAN */
2004-11-22 22:05:57 +00:00
2004-11-27 12:32:27 +00:00
Syslog('x', "%s: wcputsec: sectnum %d, len %d, %s", protname(), sectnum, cseclen, Crcflg ? "crc":"checksum");
2004-11-22 16:45:24 +00:00
for (attempts = 0; attempts <= RETRYMAX; attempts++) {
Lastrx = firstch;
sendline(cseclen == 1024 ? STX:SOH);
sendline(sectnum);
sendline((-sectnum -1));
2004-11-22 16:45:24 +00:00
oldcrc = Checksum = 0;
for (wcj = cseclen, cp = buf; --wcj >= 0; ) {
sendline(*cp);
oldcrc = updcrc16((0377& *cp), oldcrc);
Checksum += *cp++;
}
if (Crcflg) {
oldcrc = updcrc16(0, updcrc16(0, oldcrc));
sendline((int)oldcrc >> 8);
sendline((int)oldcrc);
} else {
2004-11-22 16:45:24 +00:00
sendline(Checksum);
}
2004-11-22 16:45:24 +00:00
if (Optiong) {
firstsec = FALSE;
return OK;
}
firstch = GETCHAR(Rxtimeout);
gotnak:
switch (firstch) {
case CAN:
2004-11-22 22:05:57 +00:00
Syslog('x', "got CAN");
2004-11-22 16:45:24 +00:00
if(Lastrx == CAN) {
cancan:
2004-11-22 22:05:57 +00:00
Syslog('x', "Cancelled");
2004-11-27 12:32:27 +00:00
return TERROR;
2004-11-22 16:45:24 +00:00
}
break;
case TIMEOUT:
2004-11-22 22:05:57 +00:00
Syslog('x', "Timeout on sector ACK");
2004-11-22 16:45:24 +00:00
continue;
case WANTCRC:
if (firstsec)
Crcflg = TRUE;
case NAK:
2004-11-27 12:32:27 +00:00
Syslog('+', "%s: got NAK on sector %d", protname(), sectnum);
purgeline(20);
2004-11-22 16:45:24 +00:00
continue;
2004-11-22 22:05:57 +00:00
case ACK:
2004-11-22 16:45:24 +00:00
firstsec=FALSE;
Totsecs += (cseclen>>7);
return OK;
case TERROR:
2004-11-22 22:05:57 +00:00
Syslog('x', "Got burst for sector ACK");
2004-11-22 16:45:24 +00:00
break;
default:
2004-11-22 22:05:57 +00:00
Syslog('x', "Got %02x for sector ACK", firstch);
2004-11-22 16:45:24 +00:00
break;
}
for (;;) {
Lastrx = firstch;
if ((firstch = GETCHAR(Rxtimeout)) == TIMEOUT)
break;
if (firstch == NAK || firstch == WANTCRC)
goto gotnak;
if (firstch == CAN && Lastrx == CAN)
goto cancan;
}
}
2004-11-22 22:05:57 +00:00
Syslog('x', "Retry Count Exceeded");
return TERROR;
2004-11-22 16:45:24 +00:00
}
2004-11-22 22:05:57 +00:00
/*
* fill buf with count chars padding with ^Z for CPM
*/
2004-11-22 16:45:24 +00:00
static size_t filbuf(char *buf, size_t count)
{
size_t m;
2004-11-22 22:05:57 +00:00
m = read(fileno(input_f), buf, count);
if (m <= 0)
return 0;
while (m < count)
buf[m++] = 032;
2004-11-22 16:45:24 +00:00
return count;
}
2004-11-22 15:12:55 +00:00