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

239 lines
5.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"
#include "zmmisc.h"
2004-11-23 21:40:30 +00:00
#include "zmrecv.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;
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)
int wcgetsec(size_t *, char *, unsigned int);
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)
*/
2004-11-23 20:46:21 +00:00
int wcrxpn(char *rpn)
{
register int c;
size_t Blklen = 0; /* record length of received packets */
purgeline(0);
2004-11-23 21:40:30 +00:00
Syslog('x', "%s: wcrxpn()", protname());
2004-11-23 20:46:21 +00:00
et_tu:
Firstsec = TRUE;
eof_seen = FALSE;
sendline(Crcflg?WANTCRC:NAK);
2004-11-23 21:40:30 +00:00
ioctl(1, TCFLSH, 0);
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);
2004-11-23 21:40:30 +00:00
ioctl(1, TCFLSH, 0);
2004-11-23 20:46:21 +00:00
purgeline(0); /* Do read next time ... */
GETCHAR(1);
goto et_tu;
}
return ERROR;
}
sendline(ACK);
2004-11-23 21:40:30 +00:00
ioctl(1, TCFLSH, 0);
2004-11-23 20:46:21 +00:00
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 = Crcflg ? WANTCRC:NAK;
Syslog('x', "%s: wcrx", protname());
for (;;) {
sendline(sendchar); /* send it now, we're ready! */
ioctl(1, TCFLSH, 0);
purgeline(0); /* Do read next time ... */
sectcurr = wcgetsec(&Blklen, secbuf, (unsigned int) ((sectnum & 0177) ? 5 : 13));
2004-11-23 22:35:34 +00:00
Syslog('x', "%s: got sector %d", protname(), sectcurr);
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)) {
Syslog('x', "Received dup Sector");
sendchar = ACK;
} else if (sectcurr == WCEOT) {
if (closeit(1))
return ERROR;
sendline(ACK);
ioctl(1, TCFLSH, 0);
purgeline(0); /* Do read next time ... */
return OK;
}
else if (sectcurr == ERROR)
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;
2004-11-23 21:40:30 +00:00
Syslog('x', "%s: wcgetsec()", protname());
2004-11-23 20:46:21 +00:00
for (Lastrx = errors = 0; errors < RETRYMAX; errors++) {
if ((firstch = GETCHAR(maxtime)) == STX) {
*Blklen = 1024; goto get2;
}
if (firstch == SOH) {
*Blklen=128;
get2:
2004-11-23 22:35:34 +00:00
Syslog('x', "%s: wcgetsec blklen %d", protname(), *Blklen);
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('x', "Sender Cancelled");
return ERROR;
} 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(Crcflg ? WANTCRC:NAK);
2004-11-23 21:40:30 +00:00
ioctl(1, TCFLSH, 0);
Syslog('x', "%s: send %s", protname(), Crcflg ? "WANTCRC":"NAK");
2004-11-23 20:46:21 +00:00
purgeline(0); /* Do read next time ... */
} else {
maxtime = 40;
sendline(NAK);
2004-11-23 21:40:30 +00:00
Syslog('x', "%s: send NAK", protname());
ioctl(1, TCFLSH, 0);
2004-11-23 20:46:21 +00:00
purgeline(0); /* Do read next time ... */
}
}
/* try to stop the bubble machine. */
canit(STDOUT_FILENO);
return ERROR;
}