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/ymrecv.c

292 lines
6.9 KiB
C
Raw Normal View History

2004-11-22 15:12:55 +00:00
/*****************************************************************************
*
* $Id$
* Purpose ...............: Ymodem receiver
*
*****************************************************************************
* 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-23 20:46:21 +00:00
#include "ttyio.h"
2004-11-25 21:58:26 +00:00
#include "timeout.h"
#include "transfer.h"
2004-11-23 20:46:21 +00:00
#include "zmmisc.h"
2004-11-23 21:40:30 +00:00
#include "zmrecv.h"
#include "ymsend.h"
2004-11-22 15:12:55 +00:00
#include "ymrecv.h"
2004-11-23 20:46:21 +00:00
static int Firstsec;
static int eof_seen;
static int errors;
static int Gflg = FALSE;
static char NAKchar;
2004-11-23 20:46:21 +00:00
2004-11-23 21:40:30 +00:00
extern long Bytesleft;
2004-11-23 22:35:34 +00:00
extern off_t rxbytes;
2004-11-23 20:46:21 +00:00
extern int Crcflg;
extern char Lastrx;
2004-11-23 21:40:30 +00:00
extern char *secbuf;
2004-11-23 20:46:21 +00:00
#define sendline(c) PUTCHAR((c & 0377))
2004-11-23 20:46:21 +00:00
int wcgetsec(size_t *, char *, unsigned int);
int wcrxpn(char *);
2004-11-27 12:32:27 +00:00
int wcrx(void);
int ymrcvfiles(int want1k, int wantG)
2004-11-27 12:32:27 +00:00
{
int rc = 0;
NAKchar = NAK;
Crcflg = Gflg = FALSE;
if (protocol == ZM_YMODEM) {
NAKchar = WANTCRC;
Crcflg = TRUE;
}
if (want1k) {
Crcflg = TRUE;
NAKchar = WANTCRC;
}
if (wantG) {
Crcflg = Gflg = TRUE;
NAKchar = WANTG;
}
Syslog('x', "%s: ymrcvfiles(%s, %s)", protname(), want1k ? "TRUE":"FALSE", wantG ? "TRUE":"FALSE");
Syslog('x', "%s: NAK character will be %s", protname(), printablec(NAKchar));
2004-11-27 12:32:27 +00:00
for (;;) {
rxbytes = 0l;
if (wcrxpn(secbuf) == TERROR) {
2004-11-27 12:32:27 +00:00
rc = 2;
break;
}
if (secbuf[0] == 0) {
Syslog('+', "%s: end of batch", protname());
break;
}
if (procheader(secbuf) == ZFERR) {
canit(1);
rc = 2;
break;
}
if (wcrx() == TERROR) {
rc = 2;
break;
}
}
Syslog('x', "%s: ymrcvfiles rc=%d", protname(), rc);
return rc;
}
2004-11-23 20:46:21 +00:00
2004-11-23 22:35:34 +00:00
/*
* Fetch a pathname from the other end as a C ctyle ASCIZ string.
* Length is indeterminate as long as less than Blklen
* A null string represents no more files (YMODEM)
*/
int wcrxpn(char *rpn)
2004-11-23 20:46:21 +00:00
{
register int c;
size_t Blklen = 0; /* record length of received packets */
purgeline(0);
2004-11-25 21:00:32 +00:00
Syslog('x', "%s: wcrxpn() crc=%s", protname(), Crcflg ? "true":"false");
2004-11-23 20:46:21 +00:00
et_tu:
Firstsec = TRUE;
eof_seen = FALSE;
Syslog('x', "Send %s", printablec(NAKchar));
sendline(NAKchar);
2004-11-23 20:46:21 +00:00
purgeline(0); /* Do read next time ... */
2004-11-23 21:40:30 +00:00
while ((c = wcgetsec(&Blklen, rpn, 10)) != 0) {
2004-11-23 20:46:21 +00:00
if (c == WCEOT) {
Syslog('x', "Pathname fetch returned EOT");
sendline(ACK);
purgeline(0); /* Do read next time ... */
goto et_tu;
}
2004-11-27 12:32:27 +00:00
return TERROR;
2004-11-23 20:46:21 +00:00
}
sendline(ACK);
return OK;
}
int wcrx(void)
{
2004-11-23 21:40:30 +00:00
register int sectnum, sectcurr;
register char sendchar;
size_t Blklen;
long bytes_received = 0;
Firstsec = TRUE; sectnum = 0;
eof_seen = FALSE;
sendchar = NAKchar;
2004-11-23 21:40:30 +00:00
Syslog('x', "%s: wcrx, request type %s", protname(), printablec(sendchar));
2004-11-23 21:40:30 +00:00
for (;;) {
sendline(sendchar); /* send it now, we're ready! */
purgeline(0); /* Do read next time ... */
2004-11-25 21:00:04 +00:00
/*
* Keep connections alive
*/
Nopper();
alarm_on();
2004-11-23 21:40:30 +00:00
sectcurr = wcgetsec(&Blklen, secbuf, (unsigned int) ((sectnum & 0177) ? 5 : 13));
2004-11-25 22:43:14 +00:00
Syslog('x', "%s: got sector %d size %d", protname(), sectcurr, Blklen);
2004-11-23 21:40:30 +00:00
if (sectcurr == ((sectnum+1) &0377)) {
sectnum++;
/* if using xmodem we don't know how long a file is */
if (Bytesleft && (Bytesleft - bytes_received) < Blklen)
Blklen = Bytesleft - bytes_received;
bytes_received += Blklen;
2004-11-23 22:35:34 +00:00
rxbytes += Blklen;
2004-11-23 21:40:30 +00:00
if (putsec(secbuf, Blklen) == ERROR)
return ERROR;
sendchar = ACK;
} else if (sectcurr == (sectnum & 0377)) {
2004-11-23 21:40:30 +00:00
Syslog('x', "Received dup Sector");
sendchar = ACK;
} else if (sectcurr == WCEOT) {
if (closeit(1))
return ERROR;
sendline(ACK);
purgeline(0); /* Do read next time ... */
return OK;
}
else if (sectcurr == TERROR)
2004-11-23 21:40:30 +00:00
return ERROR;
else {
Syslog('x', "Sync Error");
return ERROR;
}
}
2004-11-23 20:46:21 +00:00
}
2004-11-23 21:40:30 +00:00
2004-11-23 20:46:21 +00:00
int wcgetsec(size_t *Blklen, char *rxbuf, unsigned int maxtime)
{
register int Checksum, wcj, firstch;
register unsigned short oldcrc;
register char *p;
int sectcurr;
for (Lastrx = errors = 0; errors < RETRYMAX; errors++) {
if ((firstch = GETCHAR(maxtime)) == STX) {
*Blklen = 1024; goto get2;
}
if (firstch == SOH) {
*Blklen=128;
get2:
Syslog('x', "wcgetsec: firstch=%s, maxtime=%d, check=%s", printablec(firstch), maxtime, Crcflg?"CRC":"Checksum");
2004-11-23 20:46:21 +00:00
sectcurr = GETCHAR(1);
if ((sectcurr + (oldcrc = GETCHAR(1))) == 0377) {
oldcrc = Checksum = 0;
for (p = rxbuf, wcj = *Blklen; --wcj >= 0; ) {
if ((firstch = GETCHAR(1)) < 0)
goto bilge;
oldcrc = updcrc16(firstch, oldcrc);
Checksum += (*p++ = firstch);
}
if ((firstch = GETCHAR(1)) < 0)
goto bilge;
if (Crcflg) {
oldcrc = updcrc16(firstch, oldcrc);
if ((firstch = GETCHAR(1)) < 0)
goto bilge;
oldcrc = updcrc16(firstch, oldcrc);
if (oldcrc & 0xFFFF)
Syslog('x', "CRC");
else {
Firstsec=FALSE;
return sectcurr;
}
} else if (((Checksum - firstch) & 0377) == 0) {
Firstsec = FALSE;
return sectcurr;
} else
Syslog('x', "Checksum");
} else
Syslog('x', "Sector number garbled");
}
/* make sure eot really is eot and not just mixmash */
else if (firstch == EOT && GETCHAR(1) == TIMEOUT)
return WCEOT;
else if (firstch == CAN) {
if (Lastrx == CAN) {
Syslog('+', "%s: sender Cancelled during transfer", protname());
return TERROR;
2004-11-23 20:46:21 +00:00
} else {
Lastrx=CAN;
continue;
}
} else if (firstch == TIMEOUT) {
if (Firstsec)
goto humbug;
bilge:
Syslog('x', "TIMEOUT");
} else
Syslog('x', "Got 0%o sector header", firstch);
humbug:
Lastrx = 0;
{
int cnt = 1000;
while (cnt-- && GETCHAR(1) != TIMEOUT);
}
if (Firstsec) {
sendline(NAKchar);
Syslog('x', "%s: send %s", protname(), printablec(NAKchar));
2004-11-23 20:46:21 +00:00
purgeline(0); /* Do read next time ... */
} else {
maxtime = 5;
2004-11-23 20:46:21 +00:00
sendline(NAK);
2004-11-23 21:40:30 +00:00
Syslog('x', "%s: send NAK", protname());
2004-11-23 20:46:21 +00:00
purgeline(0); /* Do read next time ... */
}
}
/* try to stop the bubble machine. */
canit(STDOUT_FILENO);
return TERROR;
2004-11-23 20:46:21 +00:00
}