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/mbcico/binkp.c

3001 lines
74 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-12-02 14:57:46 +00:00
* $Id$
2003-05-12 18:31:51 +00:00
* Purpose .................: Fidonet binkp protocol
2001-08-17 05:46:24 +00:00
* Binkp protocol copyright : Dima Maloff.
*
*****************************************************************************
2004-01-14 21:06:49 +00:00
* Copyright (C) 1997-2004
2001-08-17 05:46:24 +00:00
*
* 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
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-08-17 05:46:24 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
#include "../lib/nodelist.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbsedb.h"
2001-08-17 05:46:24 +00:00
#include "ttyio.h"
#include "session.h"
#include "statetbl.h"
#include "config.h"
#include "emsi.h"
#include "openfile.h"
#include "respfreq.h"
#include "filelist.h"
#include "opentcp.h"
#include "rdoptions.h"
#include "lutil.h"
#include "binkp.h"
#include "config.h"
2003-07-06 20:37:58 +00:00
#include "md5b.h"
2003-08-23 14:57:51 +00:00
#include "inbound.h"
2001-08-17 05:46:24 +00:00
/*
* Safe characters for binkp filenames, the rest will be escaped.
*/
2003-05-10 21:52:52 +00:00
#define BNKCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@&=+%$-_.!()#|"
2003-09-07 20:25:33 +00:00
static char *bstate[] = {
2004-01-14 21:06:49 +00:00
(char *)"M_NUL", (char *)"M_ADR", (char *)"M_PWD", (char *)"M_FILE", (char *)"M_OK",
(char *)"M_EOB", (char *)"M_GOT", (char *)"M_ERR", (char *)"M_BSY", (char *)"M_GET",
(char *)"M_SKIP"
2003-09-07 20:25:33 +00:00
};
2001-08-17 05:46:24 +00:00
2003-05-12 18:31:51 +00:00
2003-09-13 21:07:06 +00:00
extern char *tempinbound;
2001-08-17 05:46:24 +00:00
extern char *ttystat[];
extern int Loaded;
extern pid_t mypid;
2003-07-06 20:37:58 +00:00
extern struct sockaddr_in peeraddr;
2003-09-07 20:25:33 +00:00
extern int most_debug;
2001-08-17 05:46:24 +00:00
extern unsigned long sentbytes;
extern unsigned long rcvdbytes;
2004-01-14 21:06:49 +00:00
typedef enum {RxWaitF, RxAccF, RxReceD, RxWriteD, RxEOB, RxDone} RxType;
typedef enum {TxGNF, TxTryR, TxReadS, TxWLA, TxDone} TxType;
2001-08-17 05:46:24 +00:00
typedef enum {Ok, Failure, Continue} TrType;
2003-09-09 20:49:38 +00:00
typedef enum {No, WeCan, WeWant, TheyWant, Active} OptionState;
2004-01-14 21:06:49 +00:00
typedef enum {NoState, Sending, IsSent, Got, Skipped, Get} FileState;
typedef enum {InitTransfer, Switch, Receive, Transmit, DeinitTransfer} FtType;
typedef enum {CompNone, CompGZ, CompBZ2, CompPLZ} CompType;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
static char *rxstate[] = { (char *)"RxWaitF", (char *)"RxAccF", (char *)"RxReceD",
(char *)"RxWriteD", (char *)"RxEOB", (char *)"RxDone" };
static char *opstate[] = { (char *)"No", (char *)"WeCan", (char *)"WeWant", (char *)"TheyWant", (char *)"Active" };
static char *cpstate[] = { (char *)"No", (char *)"GZ", (char *)"BZ2", (char *)"PLZ" };
2001-08-17 05:46:24 +00:00
static time_t Timer;
2003-07-06 20:37:58 +00:00
int ext_rand = 0;
2001-08-17 05:46:24 +00:00
2003-12-24 19:52:23 +00:00
2004-01-14 21:06:49 +00:00
the_queue *tql = NULL; /* The Queue List */
file_list *tosend = NULL; /* Files to send */
file_list *respond = NULL; /* Requests honored */
binkp_list *bll = NULL; /* Files to send with status */
static binkp_list *cursend = NULL; /* Current file being transmitted */
struct timeval txtvstart; /* Transmitter start time */
struct timeval txtvend; /* Transmitter end time */
struct timeval rxtvstart; /* Receiver start time */
struct timeval rxtvend; /* Receiver end time */
2003-12-24 19:52:23 +00:00
#ifdef USE_BINKDZLIB
#if defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB_H)
int compress_init(int type, void **data);
int do_compress(int type, char *dst, int *dst_len, char *src, int *src_len, int finish, void *data);
void compress_deinit(int type, void *data);
void compress_abort(int type, void *data);
int decompress_init(int type, void **data);
int do_decompress(int type, char *dst, int *dst_len, char *src, int *src_len, void *data);
int decompress_deinit(int type, void *data);
int decompress_abort(int type, void *data);
#define ZBLKSIZE 1024 /* read/write file buffer size */
#endif
#endif
2003-09-12 20:38:00 +00:00
struct binkprec {
2004-01-14 21:06:49 +00:00
int Role; /* 1=orig, 0=answer */
2003-09-12 20:38:00 +00:00
int RxState; /* Receiver state */
int TxState; /* Transmitter state */
2004-01-14 21:06:49 +00:00
int FtState; /* File transfer state */
/* Option flags */
int CRAMflag; /* CRAM authentication */
/* Session state */
int Secure; /* Secure session */
int Major; /* Major protocol version */
int Minor; /* Minor protocol version */
unsigned char *MD_Challenge; /* Received challenge data */
2004-01-25 13:42:05 +00:00
int PLZflag; /* Zlib packet compression */
2004-01-26 12:12:08 +00:00
int cmpblksize; /* Ideal next blocksize */
2004-01-25 13:42:05 +00:00
2004-01-14 21:06:49 +00:00
/* Receiver buffer */
char *rxbuf; /* Receiver buffer */
int GotFrame; /* Frame complete flag */
int rxlen; /* Frame length */
int cmd; /* Frame command flag */
int blklen; /* Frame blocklength */
unsigned short header; /* Frame header */
int rc; /* General return code */
2003-09-12 20:38:00 +00:00
long rsize; /* Receiver filesize */
long roffs; /* Receiver offset */
char *rname; /* Receiver filename */
time_t rtime; /* Receiver filetime */
2004-01-14 21:06:49 +00:00
FILE *rxfp; /* Receiver file */
2003-12-24 19:52:23 +00:00
off_t rxbytes; /* Receiver bytecount */
2004-01-14 21:06:49 +00:00
int rxpos; /* Receiver position */
2004-01-25 14:51:44 +00:00
int rxcompressed; /* Receiver compressed bytes */
char *ropts; /* Receiver M_FILE optional args */
int rmode; /* Receiver compression mode */
2003-12-24 19:52:23 +00:00
struct timezone tz; /* Timezone */
2004-01-14 21:06:49 +00:00
int DidSendGET; /* Receiver send GET status */
char *txbuf; /* Transmitter buffer */
int txlen; /* Transmitter file length */
FILE *txfp; /* Transmitter file */
int txpos; /* Transmitter position */
int stxpos; /* Transmitter start position */
2004-01-25 14:51:44 +00:00
int txcompressed; /* Transmitter compressed bytes */
int tmode; /* Transmitter compression mode */
long tfsize; /* Transmitter filesize */
2004-01-14 21:06:49 +00:00
int local_EOB; /* Local EOB sent */
int remote_EOB; /* Got EOB from remote */
int messages; /* Messages sent + rcvd */
2003-12-24 19:52:23 +00:00
unsigned long nethold; /* Netmail on hold */
2004-01-14 21:06:49 +00:00
unsigned long mailhold; /* Packed mail on hold */
int batchnr;
int msgs_on_queue; /* Messages on the queue */
2005-04-07 19:28:52 +00:00
int buggyIrex; /* Buggy Irex detected */
#ifdef USE_BINKDZLIB
void *z_idata; /* Data for zstream */
void *z_odata; /* Data for zstream */
char *z_obuf; /* Compression buffer */
int txcpos; /* Transmitter compressed position */
#if defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB2_H)
int extcmd; /* EXTCMD flag */
#endif
#ifdef HAVE_ZLIB_H
int GZflag; /* GZ compression flag */
#endif
#ifdef HAVE_BZLIB_H
int BZ2flag; /* BZ2 compression flag */
#endif
#endif
2003-09-12 20:38:00 +00:00
};
2004-01-14 21:06:49 +00:00
2003-09-12 20:38:00 +00:00
struct binkprec bp; /* Global structure */
2004-01-14 21:06:49 +00:00
/*
* Prototypes
*/
TrType binkp_receiver(void); /* Receiver routine */
TrType binkp_transmitter(void); /* Transmitter routine */
int binkp_send_frame(int, char *, int); /* Send cmd/data frame */
int binkp_send_command(int, ...); /* Send command frame */
void binkp_settimer(int); /* Set timeout timer */
int binkp_expired(void); /* Timer expired? */
int binkp_banner(void); /* Send system banner */
2004-01-25 13:42:05 +00:00
int binkp_recv_command(char *, unsigned long *, int *); /* Receive command frame */
2004-01-14 21:06:49 +00:00
void parse_m_nul(char *); /* Parse M_NUL message */
int binkp_poll_frame(void); /* Poll for a frame */
void binkp_add_message(char *frame); /* Add cmd frame to queue */
int binkp_process_messages(void); /* Process the queue */
int binkp_resync(off_t); /* File resync */
char *unix2binkp(char *); /* Binkp -> Unix escape */
char *binkp2unix(char *); /* Unix -> Binkp escape */
void fill_binkp_list(binkp_list **, file_list *, off_t); /* Build pending files */
int binkp_pendingfiles(void); /* Count pending files */
2004-07-16 14:51:07 +00:00
void binkp_clear_filelist(int); /* Clear current filelist */
2004-01-14 21:06:49 +00:00
static int orgbinkp(void); /* Originate session state */
static int ansbinkp(void); /* Answer session state */
static int file_transfer(void); /* File transfer state */
/************************************************************************************/
/*
* General entry point
*/
2004-01-14 21:06:49 +00:00
int binkp(int role)
2003-09-12 20:38:00 +00:00
{
2004-01-14 21:06:49 +00:00
int rc = 0;
Syslog('+', "Binkp: start session");
memset(&bp, 0, sizeof(bp));
bp.Role = role;
bp.CRAMflag = FALSE;
bp.Secure = FALSE;
2003-09-12 20:38:00 +00:00
bp.Major = 1;
bp.Minor = 0;
2004-01-14 21:06:49 +00:00
bp.MD_Challenge = NULL;
2003-12-24 19:52:23 +00:00
bp.rxbuf = calloc(MAX_BLKSIZE + 3, sizeof(unsigned char));
2004-01-14 21:06:49 +00:00
bp.txbuf = calloc(MAX_BLKSIZE + 3, sizeof(unsigned char));
bp.rname = calloc(512, sizeof(char));
bp.ropts = calloc(512, sizeof(char));
2003-12-24 19:52:23 +00:00
bp.rxfp = NULL;
2004-01-14 21:06:49 +00:00
bp.DidSendGET = FALSE;
bp.local_EOB = FALSE;
bp.remote_EOB = FALSE;
bp.msgs_on_queue = 0;
2004-01-26 12:12:08 +00:00
bp.cmpblksize = SND_BLKSIZE;
2004-01-25 13:42:05 +00:00
#ifdef HAVE_ZLIB_H
bp.PLZflag = WeCan;
#ifdef USE_BINKDZLIB
bp.z_obuf = calloc(MAX_BLKSIZE + 3, sizeof(unsigned char));
bp.GZflag = WeCan;
#endif
2004-01-25 13:42:05 +00:00
#else
bp.PLZflag = No;
#endif
#ifdef HAVE_BZLIB_H
#ifdef USE_BINKDZLIB
bp.BZ2flag = WeCan;
#endif
2004-01-25 13:42:05 +00:00
#endif
2005-04-07 19:28:52 +00:00
bp.buggyIrex = FALSE;
2002-10-20 20:58:55 +00:00
if (role == 1) {
if (orgbinkp()) {
2002-10-21 18:29:29 +00:00
rc = MBERR_SESSION_ERROR;
2001-08-17 05:46:24 +00:00
}
2002-10-20 20:58:55 +00:00
} else {
if (ansbinkp()) {
2002-10-21 18:29:29 +00:00
rc = MBERR_SESSION_ERROR;
2001-08-17 05:46:24 +00:00
}
2002-10-20 20:58:55 +00:00
}
2004-01-14 21:06:49 +00:00
2002-10-20 20:58:55 +00:00
if (rc) {
2004-01-14 21:06:49 +00:00
Syslog('!', "Binkp: session handshake failed");
goto binkpend;
2002-10-20 20:58:55 +00:00
}
2001-08-17 05:46:24 +00:00
2005-04-07 19:28:52 +00:00
if (((Loaded && nodes.NoBinkp11) || bp.buggyIrex) && (bp.Major == 1) && (bp.Minor != 0)) {
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: forcing downgrade to binkp/1.0 protocol");
bp.Major = 1;
bp.Minor = 0;
}
if (localoptions & NOFREQS)
session_flags &= ~SESSION_WAZOO;
else
2003-08-10 13:56:26 +00:00
session_flags |= SESSION_WAZOO;
2003-08-10 14:20:36 +00:00
2004-01-14 21:06:49 +00:00
bp.FtState = InitTransfer;
rc = file_transfer();
2003-08-10 12:53:08 +00:00
2004-01-14 21:06:49 +00:00
binkpend:
2002-10-20 20:58:55 +00:00
/*
2004-01-14 21:06:49 +00:00
* Deinit
2002-10-20 20:58:55 +00:00
*/
2004-01-14 21:06:49 +00:00
if (bp.rname)
free(bp.rname);
if (bp.MD_Challenge)
free(bp.MD_Challenge);
if (bp.rxbuf)
free(bp.rxbuf);
if (bp.txbuf)
free(bp.txbuf);
if (bp.ropts)
free(bp.ropts);
#ifdef USE_BINKDZLIB
#if defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB_H)
if ((bp.rmode != CompNone) && bp.z_idata)
decompress_deinit(bp.rmode, bp.z_idata);
if ((bp.tmode != CompNone) && bp.z_odata)
compress_deinit(bp.tmode, bp.z_odata);
if (bp.z_obuf)
free(bp.z_obuf);
#endif
#endif
2002-10-20 20:58:55 +00:00
rc = abs(rc);
2003-05-10 21:52:52 +00:00
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: session finished, rc=%d", rc);
return rc;
2003-05-10 21:52:52 +00:00
}
2004-01-14 21:06:49 +00:00
/************************************************************************************/
2003-05-12 18:31:51 +00:00
/*
2004-01-14 21:06:49 +00:00
* Originate Session Setup
2003-05-12 18:31:51 +00:00
*/
2004-01-14 21:06:49 +00:00
SM_DECL(orgbinkp, (char *)"orgbinkp")
SM_STATES
ConnInit,
WaitConn,
SendPasswd,
WaitAddr,
AuthRemote,
IfSecure,
WaitOk,
Opts
SM_NAMES
(char *)"ConnInit",
(char *)"WaitConn",
(char *)"SendPasswd",
(char *)"WaitAddr",
(char *)"AuthRemote",
(char *)"IfSecure",
(char *)"WaitOk",
(char *)"Opts"
SM_EDECL
2004-01-25 13:42:05 +00:00
faddr *primary;
char *p, *q, *pwd;
int i, rc = 0, cmd, dupe, SendPass = FALSE, akas = 0;
unsigned long bufl;
fa_list **tmp, *tmpa;
faddr *fa, ra;
2003-05-12 18:31:51 +00:00
2004-01-14 21:06:49 +00:00
SM_START(ConnInit)
2003-05-12 18:31:51 +00:00
2004-01-14 21:06:49 +00:00
SM_STATE(ConnInit)
2003-05-12 18:31:51 +00:00
2004-01-14 21:06:49 +00:00
SM_PROCEED(WaitConn)
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
SM_STATE(WaitConn)
2003-09-07 20:25:33 +00:00
2004-01-14 21:06:49 +00:00
Loaded = FALSE;
Syslog('+', "Binkp: node %s", ascfnode(remote->addr, 0x1f));
IsDoing("Connect binkp %s", ascfnode(remote->addr, 0xf));
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
rc = binkp_banner();
if (rc) {
SM_ERROR;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
/*
* Build a list of aka's to send, the primary aka first.
*/
ra.zone = remote->addr->zone;
ra.net = remote->addr->net;
ra.node = remote->addr->node;
ra.point = remote->addr->point;
2004-01-14 21:06:49 +00:00
primary = bestaka_s(remote->addr);
p = xstrcpy(ascfnode(primary, 0x1f));
2004-01-14 21:06:49 +00:00
/*
* Add all other aka's exept primary aka.
*/
for (i = 0; i < 40; i++) {
if ((CFG.aka[i].zone) && (CFG.akavalid[i]) &&
((CFG.aka[i].zone != primary->zone) || (CFG.aka[i].net != primary->net) ||
(CFG.aka[i].node != primary->node) || (CFG.aka[i].point!= primary->point))) {
p = xstrcat(p, (char *)" ");
p = xstrcat(p, aka2str(CFG.aka[i]));
}
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
rc = binkp_send_command(MM_ADR, "%s", p);
free(p);
tidy_faddr(primary);
if (rc) {
SM_ERROR;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
SM_PROCEED(WaitAddr)
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
SM_STATE(WaitAddr)
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
for (;;) {
2004-01-14 21:06:49 +00:00
if ((rc = binkp_recv_command(bp.rxbuf, &bufl, &cmd))) {
2003-05-14 20:25:11 +00:00
Syslog('!', "Binkp: error receiving remote info");
2002-06-16 16:44:38 +00:00
SM_ERROR;
}
if (cmd) {
2004-01-14 21:06:49 +00:00
if (bp.rxbuf[0] == MM_ADR) {
p = xstrcpy(bp.rxbuf + 1);
2002-06-16 16:44:38 +00:00
tidy_falist(&remote);
2004-01-14 21:06:49 +00:00
remote = NULL;
tmp = &remote;
for (q = strtok(p, " "); q; q = strtok(NULL, " ")) {
if ((fa = parsefnode(q))) {
dupe = FALSE;
for (tmpa = remote; tmpa; tmpa = tmpa->next) {
2002-12-27 19:50:14 +00:00
if ((tmpa->addr->zone == fa->zone) && (tmpa->addr->net == fa->net) &&
2004-01-14 21:06:49 +00:00
(tmpa->addr->node == fa->node) && (tmpa->addr->point == fa->point) &&
(strcmp(tmpa->addr->domain, fa->domain) == 0)) {
dupe = TRUE;
Syslog('b', "Binkp: double address %s", ascfnode(tmpa->addr, 0x1f));
break;
2002-12-27 19:50:14 +00:00
}
}
2004-01-14 21:06:49 +00:00
if (!dupe) {
2002-12-27 19:50:14 +00:00
*tmp = (fa_list*)malloc(sizeof(fa_list));
(*tmp)->next = NULL;
(*tmp)->addr = fa;
tmp = &((*tmp)->next);
}
2002-06-16 16:44:38 +00:00
} else {
2004-01-14 21:06:49 +00:00
Syslog('!', "Binkp: unparsable remote address: \"%s\"", printable(q, 0));
binkp_send_command(MM_ERR, "Unparsable address \"%s\"", printable(q, 0));
SM_ERROR;
2002-06-16 16:44:38 +00:00
}
2003-09-07 20:25:33 +00:00
}
2004-01-14 21:06:49 +00:00
free(p);
2004-01-25 10:57:35 +00:00
tmp = &remote;
while (*tmp) {
if (nodelock((*tmp)->addr, mypid)) {
Syslog('+', "address : %s is locked, removed from aka list",ascfnode((*tmp)->addr,0x1f));
tmpa=*tmp;
*tmp=(*tmp)->next;
free(tmpa);
} else {
/*
* With the loaded flag we prevent removing the noderecord
* when the remote presents us an address we don't know about.
*/
akas++;
Syslog('+', "address : %s",ascfnode((*tmp)->addr,0x1f));
if (!Loaded) {
if (noderecord((*tmp)->addr))
Loaded = TRUE;
}
tmp = &((*tmp)->next);
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
}
2004-01-25 10:57:35 +00:00
if (akas == 0) {
binkp_send_command(MM_BSY, "All aka's busy");
Syslog('+', "Binkp: abort, all aka's are busy");
SM_ERROR;
}
2004-01-14 21:06:49 +00:00
history.aka.zone = remote->addr->zone;
history.aka.net = remote->addr->net;
history.aka.node = remote->addr->node;
history.aka.point = remote->addr->point;
2004-04-29 19:25:14 +00:00
sprintf(history.aka.domain, "%s", printable(remote->addr->domain, 0));
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
SM_PROCEED(SendPasswd)
} else if (bp.rxbuf[0] == MM_BSY) {
Syslog('!', "Binkp: M_BSY \"%s\"", printable(bp.rxbuf +1, 0));
2002-06-16 16:44:38 +00:00
SM_ERROR;
2004-01-14 21:06:49 +00:00
} else if (bp.rxbuf[0] == MM_ERR) {
Syslog('!', "Binkp: M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
SM_ERROR;
} else if (bp.rxbuf[0] == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
2003-09-07 20:25:33 +00:00
} else {
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_ERR, "Unexpected frame");
2003-09-07 20:25:33 +00:00
SM_ERROR;
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
} else {
binkp_send_command(MM_ERR, "Unexpected frame");
SM_ERROR;
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(SendPasswd)
2003-07-06 20:37:58 +00:00
2003-09-07 20:25:33 +00:00
if (Loaded && strlen(nodes.Spasswd)) {
pwd = xstrcpy(nodes.Spasswd);
SendPass = TRUE;
} else {
pwd = xstrcpy((char *)"-");
}
2004-01-14 21:06:49 +00:00
if (bp.MD_Challenge) {
char *tp = NULL;
tp = MD_buildDigest(pwd, bp.MD_Challenge);
2003-07-06 20:37:58 +00:00
if (!tp) {
Syslog('!', "Unable to build MD5 digest");
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_ERR, "CRAM authentication failed, internal error");
2003-07-06 20:37:58 +00:00
SM_ERROR;
}
2004-01-14 21:06:49 +00:00
bp.CRAMflag = TRUE;
rc = binkp_send_command(MM_PWD, "%s", tp);
2003-07-07 18:06:02 +00:00
free(tp);
2003-07-06 20:37:58 +00:00
} else {
2004-01-14 21:06:49 +00:00
rc = binkp_send_command(MM_PWD, "%s", pwd);
2003-07-06 20:37:58 +00:00
}
2003-09-07 20:25:33 +00:00
free(pwd);
2004-01-14 21:06:49 +00:00
if (rc) {
SM_ERROR;
}
2003-09-07 20:25:33 +00:00
SM_PROCEED(AuthRemote)
2003-07-06 20:37:58 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(AuthRemote)
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
rc = 0;
for (tmpa = remote; tmpa; tmpa = tmpa->next) {
if ((tmpa->addr->zone == ra.zone) && (tmpa->addr->net == ra.net) &&
(tmpa->addr->node == ra.node) && (tmpa->addr->point == ra.point)) {
2004-01-14 21:06:49 +00:00
rc = 1;
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
if (rc) {
2003-09-07 20:25:33 +00:00
SM_PROCEED(IfSecure)
2002-06-16 16:44:38 +00:00
} else {
2003-05-14 20:25:11 +00:00
Syslog('!', "Binkp: error, the wrong node is reached");
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_ERR, "No AKAs in common or all AKAs busy");
2002-06-16 16:44:38 +00:00
SM_ERROR;
}
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(IfSecure)
SM_PROCEED(WaitOk)
SM_STATE(WaitOk)
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
for (;;) {
2004-01-14 21:06:49 +00:00
if ((rc = binkp_recv_command(bp.rxbuf, &bufl, &cmd))) {
2003-05-14 20:25:11 +00:00
Syslog('!', "Binkp: error waiting for remote acknowledge");
2002-06-16 16:44:38 +00:00
SM_ERROR;
}
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
if (cmd) {
2004-01-14 21:06:49 +00:00
if (bp.rxbuf[0] == MM_OK) {
Syslog('b', "Binkp: M_OK \"%s\"", printable(bp.rxbuf +1, 0));
if (SendPass)
bp.Secure = TRUE;
Syslog('+', "Binkp: %s%sprotected session", bp.CRAMflag ? "MD5 ":"", bp.Secure ? "":"un");
2003-09-07 20:25:33 +00:00
SM_PROCEED(Opts)
2004-01-14 21:06:49 +00:00
} else if (bp.rxbuf[0] == MM_BSY) {
Syslog('!', "Binkp: M_BSY \"%s\"", printable(bp.rxbuf +1, 0));
SM_ERROR;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.rxbuf[0] == MM_ERR) {
Syslog('!', "Binkp: M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
SM_ERROR;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.rxbuf[0] == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
2003-09-07 20:25:33 +00:00
} else {
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_ERR, "Unexpected frame");
SM_ERROR;
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
} else {
binkp_send_command(MM_ERR, "Unexpected frame");
SM_ERROR;
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(Opts)
IsDoing("Binkp to %s", ascfnode(remote->addr, 0xf));
#ifdef USE_BINKDZLIB
Syslog('b', "Binkp: check for extcmd, current %d", bp.extcmd);
if (bp.extcmd) {
#ifdef HAVE_BZLIB_H
if (bp.BZ2flag == TheyWant) {
Syslog('b', "Binkp: BZ2 compression active");
bp.BZ2flag = Active;
}
#endif
#ifdef HAVE_ZLIB_H
if (bp.GZflag == TheyWant) {
bp.GZflag = Active;
Syslog('b', "Binkp: GZ compression active");
}
#endif
}
2005-06-19 15:58:42 +00:00
#endif
#ifdef HAVE_ZLIB_H
if (bp.PLZflag == TheyWant) {
bp.PLZflag = Active;
Syslog('b', "Binkp: PLZ compression active");
}
#endif
2003-09-07 20:25:33 +00:00
SM_SUCCESS;
2001-08-17 05:46:24 +00:00
SM_END
SM_RETURN
2004-01-14 21:06:49 +00:00
/************************************************************************************/
2001-08-17 05:46:24 +00:00
/*
2004-01-14 21:06:49 +00:00
* Answer Session Setup
2001-08-17 05:46:24 +00:00
*/
2004-01-14 21:06:49 +00:00
2001-08-17 05:46:24 +00:00
SM_DECL(ansbinkp, (char *)"ansbinkp")
SM_STATES
2004-01-25 13:42:05 +00:00
ConnInit,
2003-09-07 20:25:33 +00:00
WaitConn,
WaitAddr,
IsPasswd,
WaitPwd,
PwdAck,
Opts
2001-08-17 05:46:24 +00:00
SM_NAMES
2004-01-25 13:42:05 +00:00
(char *)"ConnInit",
2003-09-07 20:25:33 +00:00
(char *)"WaitConn",
(char *)"WaitAddr",
(char *)"IsPasswd",
(char *)"WaitPwd",
(char *)"PwdAck",
(char *)"Opts"
2001-08-17 05:46:24 +00:00
SM_EDECL
2004-01-25 13:42:05 +00:00
char *p, *q, *pw;
int i, rc, cmd, dupe, we_have_pwd = FALSE, akas = 0;
unsigned long bufl;
fa_list **tmp, *tmpa;
faddr *fa;
SM_START(ConnInit)
2001-08-17 05:46:24 +00:00
2004-01-25 13:42:05 +00:00
SM_STATE(ConnInit)
SM_PROCEED(WaitConn)
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(WaitConn)
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
Loaded = FALSE;
2003-07-06 20:37:58 +00:00
2003-08-10 15:24:25 +00:00
if (strncmp(SockR("SBBS:0;"), "100:2,1", 7) == 0) {
Syslog('+', "Binkp: system is closed, sending M_BSY");
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_BSY, "This system is closed, try again later");
2003-08-10 15:24:25 +00:00
SM_ERROR;
}
2004-01-14 21:06:49 +00:00
if (!CFG.NoMD5 && ((bp.MD_Challenge = MD_getChallenge(NULL, &peeraddr)) != NULL)) {
2003-07-06 20:37:58 +00:00
/*
* Answering site MUST send CRAM message as very first M_NUL
*/
char s[MD5_DIGEST_LEN*2+15]; /* max. length of opt string */
strcpy(s, "OPT ");
2004-01-25 13:42:05 +00:00
MD_toString(s + strlen(s), bp.MD_Challenge[0], bp.MD_Challenge+1);
2004-01-14 21:06:49 +00:00
bp.CRAMflag = TRUE;
if ((rc = binkp_send_command(MM_NUL, "%s", s))) {
SM_ERROR;
}
}
if ((rc = binkp_banner())) {
SM_ERROR;
2003-07-06 20:37:58 +00:00
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
p = xstrcpy((char *)"");
for (i = 0; i < 40; i++) {
2002-06-16 16:44:38 +00:00
if ((CFG.aka[i].zone) && (CFG.akavalid[i])) {
p = xstrcat(p, (char *)" ");
p = xstrcat(p, aka2str(CFG.aka[i]));
}
2004-01-14 21:06:49 +00:00
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
rc = binkp_send_command(MM_ADR, "%s", p);
2002-06-16 16:44:38 +00:00
free(p);
2004-01-14 21:06:49 +00:00
if (rc) {
SM_ERROR;
}
2003-09-07 20:25:33 +00:00
SM_PROCEED(WaitAddr)
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(WaitAddr)
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
for (;;) {
2004-01-14 21:06:49 +00:00
if ((rc = binkp_recv_command(bp.rxbuf, &bufl, &cmd))) {
2003-05-14 20:25:11 +00:00
Syslog('!', "Binkp: error waiting for remote info");
2002-06-16 16:44:38 +00:00
SM_ERROR;
}
if (cmd) {
2004-01-14 21:06:49 +00:00
if (bp.rxbuf[0] == MM_ADR) {
p = xstrcpy(bp.rxbuf +1);
2002-06-16 16:44:38 +00:00
tidy_falist(&remote);
remote = NULL;
tmp = &remote;
2004-01-14 21:06:49 +00:00
for (q = strtok(p, " "); q; q = strtok(NULL, " ")) {
2002-06-16 16:44:38 +00:00
if ((fa = parsefnode(q))) {
2002-12-27 19:50:14 +00:00
dupe = FALSE;
for (tmpa = remote; tmpa; tmpa = tmpa->next) {
if ((tmpa->addr->zone == fa->zone) && (tmpa->addr->net == fa->net) &&
(tmpa->addr->node == fa->node) && (tmpa->addr->point == fa->point) &&
(strcmp(tmpa->addr->domain, fa->domain) == 0)) {
dupe = TRUE;
2003-08-10 15:24:25 +00:00
Syslog('b', "Binkp: double address %s", ascfnode(tmpa->addr, 0x1f));
2002-12-27 19:50:14 +00:00
break;
}
}
if (!dupe) {
*tmp = (fa_list*)malloc(sizeof(fa_list));
(*tmp)->next = NULL;
(*tmp)->addr = fa;
tmp = &((*tmp)->next);
}
2002-06-16 16:44:38 +00:00
} else {
2004-01-14 21:06:49 +00:00
Syslog('!', "Binkp: unparsable remote address: \"%s\"", printable(q, 0));
binkp_send_command(MM_ERR, "Unparsable address \"%s\"", printable(q, 0));
SM_ERROR;
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
}
2004-01-25 10:57:35 +00:00
tmp = &remote;
while (*tmp) {
if (nodelock((*tmp)->addr, mypid)) {
Syslog('+', "address : %s is locked, removed from aka list",ascfnode((*tmp)->addr,0x1f));
tmpa=*tmp;
*tmp=(*tmp)->next;
free(tmpa);
} else {
/*
* With the loaded flag we prevent removing the noderecord
* when the remote presents us an address we don't know about.
*/
akas++;
Syslog('+', "address : %s",ascfnode((*tmp)->addr,0x1f));
if (!Loaded) {
if (noderecord((*tmp)->addr))
Loaded = TRUE;
}
tmp = &((*tmp)->next);
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
}
2004-01-25 10:57:35 +00:00
if (akas == 0) {
binkp_send_command(MM_BSY, "All aka's busy");
Syslog('+', "Binkp: abort, all aka's are busy");
SM_ERROR;
}
2002-06-16 16:44:38 +00:00
for (tmpa = remote; tmpa; tmpa = tmpa->next) {
if (((nlent = getnlent(tmpa->addr))) && (nlent->pflag != NL_DUMMY)) {
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: remote is a listed system");
UserCity(mypid, nlent->sysop, nlent->location);
break;
2002-06-16 16:44:38 +00:00
}
}
2004-01-14 21:06:49 +00:00
if (nlent)
2002-06-16 16:44:38 +00:00
rdoptions(Loaded);
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
history.aka.zone = remote->addr->zone;
history.aka.net = remote->addr->net;
history.aka.node = remote->addr->node;
history.aka.point = remote->addr->point;
2004-04-29 19:25:14 +00:00
sprintf(history.aka.domain, "%s", printable(remote->addr->domain, 0));
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
SM_PROCEED(IsPasswd)
} else if (bp.rxbuf[0] == MM_ERR) {
Syslog('!', "Binkp: M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
2002-06-16 16:44:38 +00:00
SM_ERROR;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.rxbuf[0] == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
} else if (bp.rxbuf[0] <= MM_MAX) {
binkp_send_command(MM_ERR, "Unexpected frame");
2003-09-07 20:25:33 +00:00
SM_ERROR;
2004-01-14 21:06:49 +00:00
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
} else {
binkp_send_command(MM_ERR, "Unexpected frame");
SM_ERROR;
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(IsPasswd)
if (Loaded && strlen(nodes.Spasswd)) {
we_have_pwd = TRUE;
}
SM_PROCEED(WaitPwd)
SM_STATE(WaitPwd)
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
for (;;) {
2004-01-14 21:06:49 +00:00
if ((rc = binkp_recv_command(bp.rxbuf, &bufl, &cmd))) {
2003-05-14 20:25:11 +00:00
Syslog('!', "Binkp: error waiting for password");
2002-06-16 16:44:38 +00:00
SM_ERROR;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if (cmd) {
if (bp.rxbuf[0] == MM_PWD) {
2003-09-07 20:25:33 +00:00
SM_PROCEED(PwdAck)
2004-01-14 21:06:49 +00:00
} else if (bp.rxbuf[0] == MM_ERR) {
Syslog('!', "Binkp: M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
SM_ERROR;
} else if (bp.rxbuf[0] == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
} else if (bp.rxbuf[0] <= MM_MAX) {
binkp_send_command(MM_ERR, "Unexpected frame");
2003-09-07 20:25:33 +00:00
SM_ERROR;
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
} else {
binkp_send_command(MM_ERR, "Unexpected frame");
SM_ERROR;
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
2003-09-07 20:25:33 +00:00
SM_STATE(PwdAck)
2002-06-16 16:44:38 +00:00
2003-09-07 20:25:33 +00:00
if (we_have_pwd) {
2003-07-06 20:37:58 +00:00
pw = xstrcpy(nodes.Spasswd);
2003-09-07 20:25:33 +00:00
} else {
pw = xstrcpy((char *)"-");
}
2004-01-14 21:06:49 +00:00
if ((strncmp(bp.rxbuf +1, "CRAM-", 5) == 0) && bp.CRAMflag) {
char *sp;
sp = MD_buildDigest(pw, bp.MD_Challenge);
2003-07-06 20:37:58 +00:00
if (sp != NULL) {
2004-01-14 21:06:49 +00:00
if (strcmp(bp.rxbuf +1, sp)) {
2003-07-06 20:37:58 +00:00
Syslog('+', "Binkp: bad MD5 crypted password");
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_ERR, "Bad password");
2003-07-06 20:37:58 +00:00
free(sp);
sp = NULL;
2003-09-07 20:25:33 +00:00
free(pw);
2003-07-06 20:37:58 +00:00
SM_ERROR;
} else {
free(sp);
sp = NULL;
2003-09-07 20:25:33 +00:00
if (we_have_pwd)
2004-01-14 21:06:49 +00:00
bp.Secure = TRUE;
2003-07-06 20:37:58 +00:00
}
} else {
2003-09-07 20:25:33 +00:00
free(pw);
2003-08-10 15:24:25 +00:00
Syslog('!', "Binkp: could not build MD5 digest");
2004-01-14 21:06:49 +00:00
binkp_send_command(MM_ERR, "*** Internal error ***");
2003-07-06 20:37:58 +00:00
SM_ERROR;
}
2004-01-14 21:06:49 +00:00
} else if ((strcmp(bp.rxbuf +1, pw) == 0)) {
2003-09-07 20:25:33 +00:00
if (we_have_pwd)
2004-01-14 21:06:49 +00:00
bp.Secure = TRUE;
2002-06-16 16:44:38 +00:00
} else {
2003-09-07 20:25:33 +00:00
free(pw);
2004-01-14 21:06:49 +00:00
Syslog('?', "Binkp: password error: expected \"%s\", got \"%s\"", nodes.Spasswd, bp.rxbuf +1);
binkp_send_command(MM_ERR, "Bad password");
2002-06-16 16:44:38 +00:00
SM_ERROR;
}
2003-09-07 20:25:33 +00:00
free(pw);
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: %s%sprotected session", bp.CRAMflag ? "MD5 ":"", bp.Secure ? "":"un");
inbound_open(remote->addr, bp.Secure);
binkp_send_command(MM_OK, "%ssecure", bp.Secure ? "":"non-");
2003-09-07 20:25:33 +00:00
SM_PROCEED(Opts)
SM_STATE(Opts)
IsDoing("Binkp from %s", ascfnode(remote->addr, 0xf));
#ifdef USE_BINKDZLIB
Syslog('b', "Binkp: check for extcmd, current %d", bp.extcmd);
if (bp.extcmd) {
#ifdef HAVE_BZLIB_H
if (bp.BZ2flag == TheyWant) {
Syslog('+', "Binkp: BZ2 compression active");
bp.BZ2flag = Active;
}
#endif
#ifdef HAVE_ZLIB_H
if (bp.GZflag == TheyWant) {
bp.GZflag = Active;
Syslog('+', "Binkp: GZ compression active");
}
#endif
} else {
#ifdef HAVE_ZLIB_H
Syslog('b', "Binkp: remote doesn't support EXTCMD, turn GZ off");
bp.GZflag = No;
#endif
#ifdef HAVE_BZLIB_H
Syslog('b', "Binkp: remote doesn't support EXTCMD, turn BZ2 off");
bp.BZ2flag = No;
#endif
}
2005-06-19 15:58:42 +00:00
#endif
#ifdef HAVE_ZLIB_H
if (bp.PLZflag == TheyWant) {
bp.PLZflag = Active;
Syslog('+', "Binkp: PLZ compression active");
}
#endif
2003-09-07 20:25:33 +00:00
SM_SUCCESS;
2001-08-17 05:46:24 +00:00
SM_END
SM_RETURN
2004-01-14 21:06:49 +00:00
/************************************************************************************/
/*
* File Transfer State
*/
2004-01-14 21:06:49 +00:00
/*
* We do not use the normal state machine because that produces a lot
* of debug logging that will drive up the CPU usage.
*/
int file_transfer(void)
{
int rc = 0;
TrType Trc = Ok;
for (;;) {
switch (bp.FtState) {
case InitTransfer: binkp_settimer(BINKP_TIMEOUT);
bp.RxState = RxWaitF;
bp.TxState = TxGNF;
bp.FtState = Switch;
bp.messages = 0;
break;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
case Switch: if ((bp.RxState == RxDone) && (bp.TxState == TxDone)) {
bp.FtState = DeinitTransfer;
break;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
rc = binkp_poll_frame();
if (rc == -1) {
Syslog('b', "Binkp: receiver error detected");
bp.rc = rc = MBERR_FTRANSFER;
bp.FtState = DeinitTransfer;
break;
} else if (rc == 1) {
bp.FtState = Receive;
break;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
/*
* Check if there is room in the output buffer
*/
if (WAITPUTGET(-1) & 2) {
bp.FtState = Transmit;
break;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if (binkp_expired()) {
Syslog('+', "Binkp: transfer timeout");
bp.rc = 1;
bp.FtState = DeinitTransfer;
break;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
/*
* Nothing done, release
*/
2004-01-25 10:57:35 +00:00
msleep(1);
2004-01-14 21:06:49 +00:00
break;
2004-01-14 21:06:49 +00:00
case Receive: Trc = binkp_receiver();
if (Trc == Ok) {
if (bp.local_EOB && bp.remote_EOB)
bp.FtState = Transmit;
else
bp.FtState = Switch;
} else if (Trc == Failure) {
Syslog('+', "Binkp: receiver failure");
bp.rc = 1;
bp.FtState = DeinitTransfer;
}
break;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
case Transmit: Trc = binkp_transmitter();
if (Trc == Ok) {
bp.FtState = Switch;
} else if (Trc == Failure) {
Syslog('+', "Binkp: transmitter failure");
bp.rc = 1;
bp.FtState = DeinitTransfer;
}
break;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
case DeinitTransfer:/*
* In case of a transfer error the filelist is not yet cleared
*/
2004-07-16 14:51:07 +00:00
binkp_clear_filelist(bp.rc);
2004-01-14 21:06:49 +00:00
if (bp.rc)
return MBERR_FTRANSFER;
else
return 0;
break;
}
}
2001-08-17 05:46:24 +00:00
}
2004-01-14 21:06:49 +00:00
/************************************************************************************/
2003-12-24 19:52:23 +00:00
/*
2004-01-14 21:06:49 +00:00
* Receiver routine
2003-12-24 19:52:23 +00:00
*/
2004-01-14 21:06:49 +00:00
TrType binkp_receiver(void)
2003-12-24 19:52:23 +00:00
{
2004-01-14 21:06:49 +00:00
int bcmd, rc = 0;
struct statfs sfs;
long written;
off_t rxbytes;
2003-12-24 19:52:23 +00:00
2004-01-14 21:06:49 +00:00
if (bp.RxState == RxWaitF) {
if (! bp.GotFrame)
return Ok;
bp.GotFrame = FALSE;
bp.rxlen = 0;
bp.header = 0;
if (! bp.cmd) {
Syslog('b', "Binkp: got %d bytes DATA frame in %s state, ignored", bp.blklen, rxstate[bp.RxState]);
bp.blklen = 0;
return Ok;
}
bp.blklen = 0;
bcmd = bp.rxbuf[0];
if (bcmd == MM_ERR) {
Syslog('+', "Binkp: got M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
bp.RxState = RxDone;
return Failure;
} else if ((bcmd == MM_GET) || (bcmd == MM_GOT) || (bcmd == MM_SKIP)) {
binkp_add_message(bp.rxbuf);
return Ok;
} else if (bcmd == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
return Ok;
} else if (bcmd == MM_EOB) {
if ((bp.Major == 1) && (bp.Minor != 0)) {
if (bp.local_EOB && bp.remote_EOB) {
Syslog('b', "Binkp: receiver detects both sides in EOB state");
if ((bp.messages < 3) || binkp_pendingfiles()) {
Syslog('b', "Binkp: receiver detected end of session or pendingfiles, stay in RxWaitF");
return Ok;
} else {
bp.batchnr++;
bp.local_EOB = FALSE;
bp.remote_EOB = FALSE;
bp.messages = 0;
bp.TxState = TxGNF;
bp.RxState = RxWaitF;
Syslog('+', "Binkp: receiver starts batch %d", bp.batchnr + 1);
2004-07-16 14:51:07 +00:00
binkp_clear_filelist(0);
2004-01-14 21:06:49 +00:00
return Ok;
}
} else {
return Ok;
2003-12-24 19:52:23 +00:00
}
} else {
2004-01-14 21:06:49 +00:00
Syslog('b', "Binkp/1.0 mode and got M_EOB, goto RxEOB");
bp.RxState = RxEOB;
return Ok;
2003-12-24 19:52:23 +00:00
}
2004-01-14 21:06:49 +00:00
} else if (bcmd == MM_FILE) {
bp.RxState = RxAccF;
return Continue;
} else if (bcmd <= MM_MAX) {
Syslog('+', "Binkp: ERROR got unexpected %s frame in %s state", bstate[bcmd], rxstate[bp.RxState]);
bp.RxState = RxDone;
return Failure;
} else {
Syslog('+', "Binkp: got unexpected unknown frame, ignore");
return Ok;
}
} else if (bp.RxState == RxAccF) {
#ifdef USE_BINKDZLIB
if ((bp.rmode != CompNone) && bp.z_idata) {
decompress_deinit(bp.rmode, bp.z_idata);
bp.z_idata = NULL;
}
bp.rmode = CompNone;
#endif
2004-01-14 21:06:49 +00:00
if (strlen(bp.rxbuf) < 512) {
sprintf(bp.rname, "%s", strtok(bp.rxbuf+1, " \n\r"));
bp.rsize = atoi(strtok(NULL, " \n\r"));
bp.rtime = atoi(strtok(NULL, " \n\r"));
bp.roffs = atoi(strtok(NULL, " \n\r"));
sprintf(bp.ropts, "%s", strtok(NULL, " \n\r"));
Syslog('b', "Binkp: m_file options \"%s\"", bp.ropts);
if (strcmp((char *)"GZ", bp.ropts) == 0)
bp.rmode = CompGZ;
else if (strcmp((char *)"BZ2", bp.ropts) == 0)
bp.rmode = CompBZ2;
Syslog('b', "Binkp: m_file compression %s", cpstate[bp.rmode]);
2004-01-14 21:06:49 +00:00
} else {
/*
* Corrupted command, in case this was serious, send the M_GOT back so it's
* deleted at the remote.
*/
Syslog('+', "Binkp: got corrupted FILE frame, size %d bytes", strlen(bp.rxbuf));
bp.RxState = RxWaitF;
rc = binkp_send_command(MM_GOT, bp.rxbuf +1);
if (rc)
return Failure;
else
return Ok;
}
Syslog('+', "Binkp: receive file \"%s\" date %s size %ld offset %ld compress",
bp.rname, date(bp.rtime), bp.rsize, bp.roffs, cpstate[bp.rmode]);
2004-01-14 21:06:49 +00:00
(void)binkp2unix(bp.rname);
rxbytes = bp.rxbytes;
bp.rxfp = openfile(binkp2unix(bp.rname), bp.rtime, bp.rsize, &rxbytes, binkp_resync);
bp.rxbytes = rxbytes;
2004-01-26 12:12:08 +00:00
bp.rxcompressed = 0;
2004-01-14 21:06:49 +00:00
if (bp.DidSendGET) {
Syslog('b', "Binkp: DidSendGET is set");
/*
* The file was partly received, via the openfile the resync function
* has send a GET command to start this file with a offset. This means
* we will get a new FILE command to open this file with a offset.
*/
2004-01-26 13:54:05 +00:00
bp.RxState = RxWaitF;
2004-01-14 21:06:49 +00:00
return Ok;
2003-12-24 19:52:23 +00:00
}
2004-01-14 21:06:49 +00:00
gettimeofday(&rxtvstart, &bp.tz);
bp.rxpos = bp.roffs;
2003-12-24 19:52:23 +00:00
2004-03-20 23:00:22 +00:00
if (enoughspace(CFG.freespace) == 0) {
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: low diskspace, sending BSY");
binkp_send_command(MM_BSY, "Low diskspace, try again later");
bp.RxState = RxDone;
bp.TxState = TxDone;
bp.rc = MBERR_FTRANSFER;
return Failure;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if (statfs(tempinbound, &sfs) == 0) {
if ((bp.rsize / (sfs.f_bsize + 1)) >= sfs.f_bfree) {
Syslog('!', "Binkp: only %lu blocks free (need %lu) in %s for this file", sfs.f_bfree,
(unsigned long)(bp.rsize / (sfs.f_bsize + 1)), tempinbound);
closefile();
bp.rxfp = NULL; /* Force SKIP command */
}
}
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
if (bp.rsize == bp.rxbytes) {
/*
* We already got this file, send GOT so it will
* be deleted at the remote.
*/
Syslog('+', "Binkp: already got %s, sending GOT", bp.rname);
rc = binkp_send_command(MM_GOT, "%s %ld %ld", bp.rname, bp.rsize, bp.rtime);
bp.RxState = RxWaitF;
bp.rxfp = NULL;
if (rc)
return Failure;
else
return Ok;
} else if (!bp.rxfp) {
/*
* Some error, request to skip it
*/
Syslog('+', "Binkp: error file %s, sending SKIP", bp.rname);
rc = binkp_send_command(MM_SKIP, "%s %ld %ld", bp.rname, bp.rsize, bp.rtime);
bp.RxState = RxWaitF;
if (rc)
return Failure;
else
return Ok;
} else {
Syslog('b', "rsize=%d, rxbytes=%d, roffs=%d", bp.rsize, bp.rxbytes, bp.roffs);
bp.RxState = RxReceD;
return Ok;
}
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.RxState == RxReceD) {
if (! bp.GotFrame)
return Ok;
if (! bp.cmd) {
bp.RxState = RxWriteD;
return Continue;
}
bp.GotFrame = FALSE;
bp.rxlen = 0;
bp.header = 0;
bp.blklen = 0;
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
bcmd = bp.rxbuf[0];
if (bcmd == MM_ERR) {
Syslog('+', "Binkp: got M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
bp.RxState = RxDone;
return Failure;
} else if ((bcmd == MM_GET) || (bcmd == MM_GOT) || (bcmd == MM_SKIP)) {
binkp_add_message(bp.rxbuf);
return Ok;
} else if (bcmd == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
return Ok;
} else if (bcmd == MM_FILE) {
Syslog('+', "Binkp: partial received file, saving");
closefile();
bp.rxfp = NULL;
bp.RxState = RxAccF;
return Continue;
} else if (bcmd <= MM_MAX) {
Syslog('+', "Binkp: ERROR got unexpected %s frame in %s state", bstate[bcmd], rxstate[bp.RxState]);
bp.RxState = RxDone;
return Failure;
} else {
Syslog('+', "Binkp: got unexpected unknown frame, ignore");
return Ok;
}
} else if (bp.RxState == RxWriteD) {
2004-01-26 13:54:05 +00:00
2004-01-26 13:32:47 +00:00
if (bp.rxfp == NULL) {
2004-01-26 13:54:05 +00:00
/*
* After sending M_GET for restart at offset, the file is closed
* but some data frames may be underway, so ignore these.
*/
Syslog('b', "Binkp: file is closed, ignore data");
bp.GotFrame = FALSE;
bp.rxlen = 0;
bp.header = 0;
bp.RxState = RxReceD;
return Ok;
2004-01-26 13:32:47 +00:00
}
#ifdef USE_BINKDZLIB
written = 0;
if (bp.rmode) {
int rc1 = 0, nget = bp.blklen, zavail, nput;
char zbuf[ZBLKSIZE];
char *buf = bp.rxbuf;
if (bp.z_idata == NULL) {
if (decompress_init(bp.rmode, &bp.z_idata)) {
Syslog('+', "Binkp: can't init decompress");
bp.RxState = RxDone;
return Failure;
} else
Syslog('b', "Binkp: decompress_init success");
}
while (nget) {
zavail = ZBLKSIZE;
nput = nget;
rc1 = do_decompress(bp.rmode, zbuf, &zavail, buf, &nput, bp.z_idata);
if (rc1 < 0) {
Syslog('+', "Binkp: decompress %s error %d", bp.rname, rc1);
bp.RxState = RxDone;
return Failure;
} else {
// Syslog('b', "Binkp: %d bytes of data decompressed to %d", nput, zavail);
}
if (zavail != 0 && fwrite(zbuf, zavail, 1, bp.rxfp) < 1) {
Syslog('+', "$Binkp: write error");
decompress_abort(bp.rmode, bp.z_idata);
bp.z_idata = NULL;
bp.RxState = RxDone;
return Failure;
}
buf += nput;
nget -= nput;
written += zavail;
bp.rxcompressed += zavail - nput;
}
bp.blklen = written; /* Correct physical to virtual blocklength */
// Syslog('b', "Binkp: set bp.blklen %d rc=%d", written, rc1);
if (rc1 == 1) {
if ((rc1 = decompress_deinit(bp.rmode, bp.z_idata)) < 0)
Syslog('+', "Binkp: decompress_deinit retcode %d", rc1);
else
Syslog('b', "Binkp: decompress_deinit done");
bp.z_idata = NULL;
}
} else {
written = fwrite(bp.rxbuf, 1, bp.blklen, bp.rxfp);
}
#else
2004-01-14 21:06:49 +00:00
written = fwrite(bp.rxbuf, 1, bp.blklen, bp.rxfp);
#endif
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
bp.GotFrame = FALSE;
bp.rxlen = 0;
bp.header = 0;
2004-01-14 21:06:49 +00:00
if (bp.blklen && (bp.blklen != written)) {
Syslog('+', "Binkp: ERROR file write error");
2003-09-12 20:38:00 +00:00
bp.RxState = RxDone;
2004-01-14 21:06:49 +00:00
bp.blklen = 0;
return Failure;
}
bp.blklen = 0;
bp.rxpos += written;
if (bp.rxpos == bp.rsize) {
rc = binkp_send_command(MM_GOT, "%s %ld %ld", bp.rname, bp.rsize, bp.rtime);
closefile();
bp.rxpos = bp.rxpos - bp.rxbytes;
gettimeofday(&rxtvend, &bp.tz);
#if defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB_H)
2005-06-19 15:58:42 +00:00
if (bp.rxcompressed)
2004-01-26 12:12:08 +00:00
Syslog('+', "Binkp: %s", compress_stat(bp.rxpos, bp.rxcompressed));
#endif
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: OK %s", transfertime(rxtvstart, rxtvend, bp.rxpos, FALSE));
rcvdbytes += bp.rxpos;
bp.RxState = RxWaitF;
if (rc)
return Failure;
else
return Ok;
}
bp.RxState = RxReceD;
return Ok;
} else if (bp.RxState == RxEOB) {
if (! bp.GotFrame)
return Ok;
bp.GotFrame = FALSE;
bp.rxlen = 0;
bp.header = 0;
bp.blklen = 0;
if (bp.cmd) {
bcmd = bp.rxbuf[0];
if (bcmd == MM_ERR) {
Syslog('+', "Binkp: got M_ERR \"%s\"", printable(bp.rxbuf +1, 0));
bp.RxState = RxDone;
return Failure;
} else if ((bcmd == MM_GET) || (bcmd == MM_GOT) || (bcmd == MM_SKIP)) {
binkp_add_message(bp.rxbuf);
return Ok;
} else if (bcmd == MM_NUL) {
parse_m_nul(bp.rxbuf +1);
return Ok;
} else if (bcmd <= MM_MAX) {
Syslog('+', "Binkp: ERROR got unexpected %s frame in %s state", bstate[bcmd], rxstate[bp.RxState]);
bp.RxState = RxDone;
return Failure;
} else {
Syslog('+', "Binkp: got unexpected unknown frame, ignore");
return Ok;
}
} else {
Syslog('+', "Binkp: ERROR got unexpected data frame in %s state", rxstate[bp.RxState]);
bp.RxState = RxDone;
return Failure;
2002-06-16 16:44:38 +00:00
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.RxState == RxDone) {
return Ok;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
/*
* Cannot be here
*/
bp.RxState = RxDone;
return Failure;
}
2001-08-17 05:46:24 +00:00
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
/************************************************************************************/
/*
* Transmitter routine
*/
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
TrType binkp_transmitter(void)
{
2005-06-19 15:12:07 +00:00
int rc = 0, eof = FALSE;
#ifdef USE_BINKDZLIB
int sz, rc1 = 0;
#endif
char *nonhold_mail, *extra;
2004-01-14 21:06:49 +00:00
fa_list *eff_remote;
file_list *tsl;
static binkp_list *tmp;
if (bp.TxState == TxGNF) {
/*
* If we do not have a filelist yet, create one.
*/
if (tosend == NULL) {
Syslog('b', "Creating filelist");
nonhold_mail = (char *)ALL_MAIL;
bp.nethold = bp.mailhold = 0L;
cursend = NULL;
2002-06-16 16:44:38 +00:00
/*
2004-01-14 21:06:49 +00:00
* If remote doesn't have the 8.3 flag set, allow long names
2002-06-16 16:44:38 +00:00
*/
2004-01-14 21:06:49 +00:00
if (!nodes.FNC)
remote_flags &= ~SESSION_FNC;
eff_remote = remote;
tosend = create_filelist(eff_remote, nonhold_mail, 0);
if ((respond = respond_wazoo()) != NULL) {
for (tsl = tosend; tsl->next; tsl = tsl ->next);
tsl->next = respond;
Syslog('b', "Binkp: added requested files");
}
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
/*
* Build a new filelist from the existing filelist.
* This one is special for binkp behaviour.
*/
for (tsl = tosend; tsl; tsl = tsl->next) {
if (tsl->remote != NULL)
fill_binkp_list(&bll, tsl, 0L);
}
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
if ((bp.nethold || bp.mailhold) || (bp.batchnr == 0)) {
Syslog('+', "Binkp: mail %ld, files %ld bytes", bp.nethold, bp.mailhold);
if ((rc = binkp_send_command(MM_NUL, "TRF %ld %ld", bp.nethold, bp.mailhold))) {
bp.TxState = TxDone;
return Failure;
2001-08-17 05:46:24 +00:00
}
2004-01-14 21:06:49 +00:00
}
}
for (tmp = bll; tmp; tmp = tmp->next) {
if (tmp->state == NoState) {
2001-08-17 05:46:24 +00:00
/*
2004-01-14 21:06:49 +00:00
* There is something to send
2001-08-17 05:46:24 +00:00
*/
2004-01-14 21:06:49 +00:00
struct flock txflock;
txflock.l_type = F_RDLCK;
txflock.l_whence = 0;
txflock.l_start = 0L;
txflock.l_len = 0L;
bp.txfp = fopen(tmp->local, "r");
if (bp.txfp == NULL) {
if ((errno == ENOENT) || (errno == EINVAL)) {
Syslog('+', "Binkp: file %s doesn't exist, removing", MBSE_SS(tmp->local));
tmp->state = Got;
} else {
WriteError("$Binkp: can't open %s, skipping", MBSE_SS(tmp->local));
tmp->state = Skipped;
}
break;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if (fcntl(fileno(bp.txfp), F_SETLK, &txflock) != 0) {
WriteError("$Binkp: can't lock file %s, skipping", MBSE_SS(tmp->local));
fclose(bp.txfp);
bp.txfp = NULL;
tmp->state = Skipped;
break;
}
bp.tmode = CompNone;
extra = (char *)"";
#ifdef USE_BINKDZLIB
if ((tmp->compress == CompGZ) || (tmp->compress == CompBZ2)) {
bp.tmode = tmp->compress;
if ((rc1 = compress_init(bp.tmode, &bp.z_odata))) {
Syslog('+', "Binkp: compress_init failed (rc=%d)", rc1);
tmp->compress = CompNone;
bp.tmode = CompNone;
} else {
if (bp.tmode == CompBZ2)
extra = (char *)" BZ2";
else if (bp.tmode == CompGZ)
extra = (char *)" GZ";
Syslog('b', "Binkp: compress_init ok, extra=%s", extra);
}
2004-01-14 21:06:49 +00:00
}
#endif
2001-08-17 05:46:24 +00:00
// extra = (char *)""; /* FIXME: remove when code complete and to activate compression */
2005-06-19 15:12:07 +00:00
#ifdef USE_BINKDZLIB
bp.txpos = bp.txcpos = bp.stxpos = tmp->offset;
2005-06-19 15:12:07 +00:00
#else
bp.txpos = bp.stxpos = tmp->offset;
#endif
2004-01-26 12:12:08 +00:00
bp.txcompressed = 0;
bp.tfsize = tmp->size;
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: send \"%s\" as \"%s\"", MBSE_SS(tmp->local), MBSE_SS(tmp->remote));
Syslog('+', "Binkp: size %lu bytes, dated %s", (unsigned long)tmp->size, date(tmp->date));
rc = binkp_send_command(MM_FILE, "%s %lu %ld %ld%s", MBSE_SS(tmp->remote),
(unsigned long)tmp->size, (long)tmp->date, (unsigned long)tmp->offset, extra);
2004-01-14 21:06:49 +00:00
if (rc) {
bp.TxState = TxDone;
return Failure;
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
gettimeofday(&txtvstart, &bp.tz);
tmp->state = Sending;
cursend = tmp;
bp.TxState = TxTryR;
return Continue;
} /* if state == NoState */
} /* for */
if (tmp == NULL) {
/*
* No more files
*/
rc = binkp_send_command(MM_EOB, "");
bp.TxState = TxWLA;
if (rc)
return Failure;
else
return Continue;
}
return Ok;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.TxState == TxTryR) {
if (bp.msgs_on_queue == 0) {
bp.TxState = TxReadS;
return Continue;
} else {
if (binkp_process_messages()) {
bp.TxState = TxDone;
return Failure;
} else
return Continue;
}
} else if (bp.TxState == TxReadS) {
bp.TxState = TxTryR;
#if USE_BINKDZLIB
#if defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB_H)
if ((bp.tmode == CompBZ2) || (bp.tmode == CompGZ)) {
int nput = 0; /* number of compressed bytes */
int nget = 0; /* number of read uncompressed bytes from buffer */
int ocnt; /* number of bytes compressed by one call */
int rc2;
off_t fleft;
sz = bp.tfsize - ftell(bp.txfp);
if (bp.cmpblksize < sz)
sz = bp.cmpblksize;
2005-06-19 14:53:54 +00:00
// Syslog('b', "Binkp: sz=%d", sz);
while (TRUE) {
ocnt = bp.cmpblksize - nput;
nget = sz;
if (nget > ZBLKSIZE)
nget = ZBLKSIZE;
fleft = bp.tfsize - ftell(bp.txfp);
fseek(bp.txfp, bp.txpos, SEEK_SET);
nget = fread(bp.z_obuf, 1, nget, bp.txfp);
2005-06-19 14:53:54 +00:00
// Syslog('b', "Binkp: fread pos=%d nget=%d ocnt=%d", bp.txpos, nget, ocnt);
rc2 = do_compress(bp.tmode, bp.txbuf + nput, &ocnt, bp.z_obuf, &nget, fleft ? 0 : 1, bp.z_odata);
Syslog('b', "Binkp: do_compress ocnt=%d nget=%d fleft=%d rc=%d", ocnt, nget, fleft, rc2);
if (rc2 == -1) {
Syslog('+', "Binkp: compression error rc=%d", rc2);
return Failure;
}
bp.txpos += nget;
bp.txcpos += ocnt;
nput += ocnt;
2005-06-19 14:53:54 +00:00
// Syslog('b', "Binkp: txpos=%d txcpos=%d nput=%d", bp.txpos, bp.txcpos, nput);
2005-06-19 14:53:54 +00:00
/*
* Compressed block is filled for transmission
*/
if ((nput == bp.cmpblksize) || (fleft == 0)) {
2005-06-19 14:53:54 +00:00
bp.txlen = nput;
rc = binkp_send_frame(FALSE, bp.txbuf, bp.txlen);
if (rc)
return Failure;
2005-06-19 14:53:54 +00:00
sentbytes += bp.txlen;
if (rc2 == 1) {
/*
* Last compressed block is sent, set eof.
*/
eof = TRUE;
bp.txcompressed = bp.tfsize - bp.txcpos;
}
break;
}
}
} else {
#endif
#endif
2004-01-14 21:06:49 +00:00
/*
* Send uncompressed block
2004-01-14 21:06:49 +00:00
*/
fseek(bp.txfp, bp.txpos, SEEK_SET);
bp.txlen = fread(bp.txbuf, 1, bp.cmpblksize, bp.txfp);
eof = feof(bp.txfp);
2004-01-14 21:06:49 +00:00
if (ferror(bp.txfp)) {
WriteError("$Binkp: error reading from file");
bp.TxState = TxDone;
cursend->state = Skipped;
return Failure;
}
if (bp.txlen) {
2003-12-24 19:52:23 +00:00
bp.txpos += bp.txlen;
sentbytes += bp.txlen;
2004-01-14 21:06:49 +00:00
rc = binkp_send_frame(FALSE, bp.txbuf, bp.txlen);
if (rc)
return Failure;
2002-06-16 16:44:38 +00:00
}
#if USE_BINKDZLIB
#if defined(HAVE_ZLIB_H) || defined(HAVE_BZLIB_H)
}
#endif
#endif
if ((bp.txlen == 0) || eof) {
2004-01-14 21:06:49 +00:00
/*
* calculate time needed and bytes transferred
*/
gettimeofday(&txtvend, &bp.tz);
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
/*
* Close transmitter file
*/
fclose(bp.txfp);
bp.txfp = NULL;
if (bp.txpos >= 0) {
bp.stxpos = bp.txpos - bp.stxpos;
2004-01-26 12:12:08 +00:00
#ifdef HAVE_ZLIB_H
if (bp.txcompressed)
2004-01-26 12:12:08 +00:00
Syslog('+', "Binkp: %s", compress_stat(bp.stxpos, bp.txcompressed));
#endif
2004-01-14 21:06:49 +00:00
Syslog('+', "Binkp: OK %s", transfertime(txtvstart, txtvend, bp.stxpos, TRUE));
} else {
Syslog('+', "Binkp: transmitter skipped file after %ld seconds", txtvend.tv_sec - txtvstart.tv_sec);
2002-06-16 16:44:38 +00:00
}
#ifdef USE_BINKDZLIB
if ((bp.tmode == CompBZ2) || (bp.tmode == CompGZ)) {
compress_deinit(bp.tmode, bp.z_odata);
bp.z_odata = NULL;
bp.tmode = CompNone;
Syslog('b', "Binkp: compress_deinit done");
}
#endif
2004-01-14 21:06:49 +00:00
cursend->state = IsSent;
cursend = NULL;
bp.TxState = TxGNF;
return Ok;
}
return Ok;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
} else if (bp.TxState == TxWLA) {
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if ((bp.msgs_on_queue == 0) && (binkp_pendingfiles() == 0)) {
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if ((bp.RxState >= RxEOB) && (bp.Major == 1) && (bp.Minor == 0)) {
bp.TxState = TxDone;
if (bp.local_EOB && bp.remote_EOB) {
Syslog('b', "Binkp: binkp/1.0 session seems complete");
bp.RxState = RxDone;
}
2002-06-16 16:44:38 +00:00
2004-07-16 14:51:07 +00:00
binkp_clear_filelist(rc);
2004-01-14 21:06:49 +00:00
return Ok;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if ((bp.RxState < RxEOB) && (bp.Major == 1) && (bp.Minor == 0)) {
bp.TxState = TxWLA;
return Ok;
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
if ((bp.Major == 1) && (bp.Minor != 0)) {
if (bp.local_EOB && bp.remote_EOB) {
/*
* We did send EOB and got a EOB
*/
if (bp.messages < 3) {
/*
* Nothing sent anymore, finish
*/
Syslog('b', "Binkp: binkp/1.1 session seems complete");
bp.RxState = RxDone;
bp.TxState = TxDone;
} else {
/*
* Start new batch
*/
bp.batchnr++;
bp.local_EOB = FALSE;
bp.remote_EOB = FALSE;
bp.messages = 0;
bp.TxState = TxGNF;
bp.RxState = RxWaitF;
Syslog('+', "Binkp: transmitter starts batch %d", bp.batchnr + 1);
2004-07-16 14:51:07 +00:00
binkp_clear_filelist(rc);
2004-01-14 21:06:49 +00:00
return Ok; /* Continue is not good here, troubles with binkd on slow links. */
}
}
2001-08-17 05:46:24 +00:00
2004-07-16 14:51:07 +00:00
binkp_clear_filelist(rc);
2004-01-14 21:06:49 +00:00
return Ok;
}
}
if (bp.msgs_on_queue) {
if (binkp_process_messages()) {
return Failure;
} else {
return Continue;
}
}
return Ok;
} else if (bp.TxState == TxDone) {
return Ok;
}
/*
* Cannot be here
*/
bp.TxState = TxDone;
return Failure;
}
/************************************************************************************/
/*
* Functions
*/
/*
* Send a binkp frame
*/
int binkp_send_frame(int cmd, char *buf, int len)
{
unsigned short header = 0;
int rc, id;
2004-01-25 14:51:44 +00:00
#ifdef HAVE_ZLIB_H
2004-01-26 12:12:08 +00:00
int rcz, last;
2004-01-25 14:51:44 +00:00
unsigned long zlen;
char *zbuf;
if ((len >= BINKP_PLZ_BLOCK) && (bp.PLZflag == Active)) {
WriteError("Can't send block of %d bytes in PLZ mode", len);
return 1;
}
#endif
2004-01-14 21:06:49 +00:00
if (cmd) {
header = ((BINKP_CONTROL_BLOCK + len) & 0xffff);
bp.messages++;
id = buf[0];
if (id == MM_EOB) {
bp.local_EOB = TRUE;
}
if (len == 1)
Syslog('b', "Binkp: send %s", bstate[id]);
else
Syslog('b', "Binkp: send %s %s", bstate[id], printable(buf +1, len -1));
} else {
header = ((BINKP_DATA_BLOCK + len) & 0xffff);
Syslog('b', "Binkp: send data (%d)", len);
}
2004-01-25 14:51:44 +00:00
#ifdef HAVE_ZLIB_H
2004-01-26 12:12:08 +00:00
last = bp.cmpblksize;
2004-01-26 14:13:57 +00:00
/*
* Only use compression for DATA blocks larger then 20 bytes.
*/
if ((bp.PLZflag == Active) && (len > 20) && (!cmd)) {
2004-01-25 14:51:44 +00:00
zbuf = calloc(BINKP_ZIPBUFLEN, sizeof(char));
2004-01-29 21:12:21 +00:00
zlen = BINKP_PLZ_BLOCK -1;
2004-01-25 14:51:44 +00:00
rcz = compress2(zbuf, &zlen, buf, len, 9);
if (rcz == Z_OK) {
Syslog('b', "Binkp: compressed OK, srclen=%d, destlen=%d, will send compressed=%s",
len, zlen, (zlen < len) ?"yes":"no");
if (zlen < len) {
bp.txcompressed += (len - zlen);
2004-01-26 12:12:08 +00:00
/*
* Calculate the perfect blocksize for the next block
* using the current compression ratio. This gives
* a dynamic optimal blocksize. The average maximum
* blocksize on the line will be 4096 bytes.
*/
2004-01-26 14:13:57 +00:00
bp.cmpblksize = ((len * 4) / zlen) * 512;
if (bp.cmpblksize < SND_BLKSIZE)
bp.cmpblksize = SND_BLKSIZE;
if (bp.cmpblksize > (BINKP_PLZ_BLOCK -1))
bp.cmpblksize = (BINKP_PLZ_BLOCK -1);
2004-01-25 14:51:44 +00:00
/*
* Rebuild header for compressed block
*/
2004-01-26 14:13:57 +00:00
header = ((BINKP_DATA_BLOCK + BINKP_PLZ_BLOCK + zlen) & 0xffff);
2004-01-25 14:51:44 +00:00
rc = PUTCHAR((header >> 8) & 0x00ff);
if (!rc)
rc = PUTCHAR(header & 0x00ff);
if (zlen && !rc)
rc = PUT(zbuf, zlen);
} else {
rc = PUTCHAR((header >> 8) & 0x00ff);
if (!rc)
rc = PUTCHAR(header & 0x00ff);
if (len && !rc)
rc = PUT(buf, len);
2004-01-26 12:12:08 +00:00
if (!cmd)
bp.cmpblksize = SND_BLKSIZE;
2004-01-25 14:51:44 +00:00
}
} else {
2004-01-29 21:12:21 +00:00
Syslog('+', "Binkp: compress error %d", rcz);
2004-01-25 14:51:44 +00:00
rc = PUTCHAR((header >> 8) & 0x00ff);
if (!rc)
rc = PUTCHAR(header & 0x00ff);
if (len && !rc)
rc = PUT(buf, len);
2004-01-26 12:12:08 +00:00
if (!cmd)
bp.cmpblksize = SND_BLKSIZE;
2004-01-25 14:51:44 +00:00
}
free(zbuf);
} else {
rc = PUTCHAR((header >> 8) & 0x00ff);
if (!rc)
rc = PUTCHAR(header & 0x00ff);
if (len && !rc)
rc = PUT(buf, len);
2004-01-26 12:12:08 +00:00
if (!cmd)
bp.cmpblksize = SND_BLKSIZE;
2004-01-25 14:51:44 +00:00
}
#else
2004-01-14 21:06:49 +00:00
rc = PUTCHAR((header >> 8) & 0x00ff);
if (!rc)
rc = PUTCHAR(header & 0x00ff);
if (len && !rc)
rc = PUT(buf, len);
2004-01-26 12:12:08 +00:00
bp.cmpblksize = SND_BLKSIZE;
2004-01-25 14:51:44 +00:00
#endif
2004-01-14 21:06:49 +00:00
FLUSHOUT();
binkp_settimer(BINKP_TIMEOUT);
2004-01-29 21:12:21 +00:00
Nopper();
2004-01-14 21:06:49 +00:00
return rc;
}
/*
* Send a command message
*/
int binkp_send_command(int id, ...)
{
va_list args;
char *fmt;
static char buf[1024];
int sz, rc;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
va_start(args, id);
fmt = va_arg(args, char*);
if (fmt) {
vsprintf(buf, fmt, args);
sz = (strlen(buf) & 0x7fff);
} else {
buf[0]='\0';
sz = 0;
}
memmove(buf+1, buf, sz);
buf[0] = id & 0xff;
sz++;
rc = binkp_send_frame(TRUE, buf, sz);
va_end(args);
return rc;
}
/*
* Set binkp master timer
*/
void binkp_settimer(int interval)
{
Timer = time((time_t*)NULL) + interval;
}
/*
* Test if master timer is expired
*/
int binkp_expired(void)
{
time_t now;
now = time(NULL);
if (now >= Timer)
2004-01-26 15:22:17 +00:00
Syslog('+', "Binkp: session timeout");
2004-01-14 21:06:49 +00:00
return (now >= Timer);
}
/*
* Send system info to remote
*/
int binkp_banner(void)
{
time_t t;
int rc;
2005-06-19 15:12:07 +00:00
#ifdef USE_BINKDZLIB
char *p;
2005-06-19 15:12:07 +00:00
#endif
2004-01-14 21:06:49 +00:00
rc = binkp_send_command(MM_NUL,"SYS %s", CFG.bbs_name);
if (!rc)
rc = binkp_send_command(MM_NUL,"ZYZ %s", CFG.sysop_name);
if (!rc)
rc = binkp_send_command(MM_NUL,"LOC %s", CFG.location);
if (!rc)
2004-07-22 20:30:58 +00:00
rc = binkp_send_command(MM_NUL,"NDL %s", CFG.IP_Flags);
2004-01-14 21:06:49 +00:00
t = time(NULL);
if (!rc)
rc = binkp_send_command(MM_NUL,"TIME %s", rfcdate(t));
if (!rc) {
if (nodes.NoBinkp11 || bp.buggyIrex)
2004-01-14 21:06:49 +00:00
rc = binkp_send_command(MM_NUL,"VER mbcico/%s/%s-%s %s/%s", VERSION, OsName(), OsCPU(), PRTCLNAME, PRTCLOLD);
else
rc = binkp_send_command(MM_NUL,"VER mbcico/%s/%s-%s %s/%s", VERSION, OsName(), OsCPU(), PRTCLNAME, PRTCLVER);
}
2004-07-22 20:30:58 +00:00
if (strlen(CFG.IP_Phone) && !rc)
rc = binkp_send_command(MM_NUL,"PHN %s", CFG.IP_Phone);
2004-01-14 21:06:49 +00:00
if (strlen(CFG.comment) && !rc)
rc = binkp_send_command(MM_NUL,"OPM %s", CFG.comment);
#ifdef USE_BINKDZLIB
if (!rc /* && bp.Role */) {
p = xstrcpy((char *)"OPT EXTCMD");
2005-06-19 15:58:42 +00:00
#ifdef HAVE_BZLIB_H
p = xstrcat(p, (char *)" BZ2");
#endif
#ifdef HAVE_ZLIB_H
p = xstrcat(p, (char *)" GZ");
#endif
2005-06-19 15:58:42 +00:00
rc = binkp_send_command(MM_NUL,"%s", p);
free(p);
}
#endif
2005-06-19 15:58:42 +00:00
#ifdef HAVE_ZLIB_H
if (!rc) {
p = xstrcpy((char *)"OPT PLZ");
rc = binkp_send_command(MM_NUL,"%s", p);
free(p);
}
#endif
2005-06-19 15:58:42 +00:00
2004-01-14 21:06:49 +00:00
return rc;
}
/*
* Receive command frame
*/
2004-01-25 13:42:05 +00:00
int binkp_recv_command(char *buf, unsigned long *len, int *cmd)
2004-01-14 21:06:49 +00:00
{
2004-01-25 13:42:05 +00:00
int b0, b1;
2004-01-14 21:06:49 +00:00
*len = *cmd = 0;
b0 = GETCHAR(BINKP_TIMEOUT);
2004-01-29 21:12:21 +00:00
if (tty_status) {
Syslog('-', "Binkp: tty_status with b0");
2004-01-14 21:06:49 +00:00
goto to;
2004-01-29 21:12:21 +00:00
}
2004-01-14 21:06:49 +00:00
if (b0 & 0x80)
*cmd = 1;
2004-01-25 10:57:35 +00:00
b1 = GETCHAR(BINKP_TIMEOUT / 2);
2004-01-29 21:12:21 +00:00
if (tty_status) {
Syslog('-', "Binkp: tty_status with b1");
2004-01-14 21:06:49 +00:00
goto to;
2004-01-29 21:12:21 +00:00
}
2004-01-14 21:06:49 +00:00
*len = (b0 & 0x7f) << 8;
*len += b1;
GET(buf, *len, BINKP_TIMEOUT / 2);
buf[*len] = '\0';
2004-01-29 21:12:21 +00:00
if (tty_status) {
Syslog('-', "Binkp: tty_status with block len=%d", *len);
2004-01-14 21:06:49 +00:00
goto to;
2004-01-29 21:12:21 +00:00
}
2004-01-26 15:22:17 +00:00
binkp_settimer(BINKP_TIMEOUT);
2004-01-29 21:12:21 +00:00
Nopper();
2004-01-14 21:06:49 +00:00
to:
if (tty_status)
WriteError("Binkp: TCP rcv error, tty status: %s", ttystat[tty_status]);
return tty_status;
}
/*
* Parse a received M_NUL message
*/
void parse_m_nul(char *msg)
{
char *p, *q;
if (strncmp(msg, "SYS ", 4) == 0) {
Syslog('+', "System : %s", msg+4);
strncpy(history.system_name, msg+4, 35);
} else if (strncmp(msg, "ZYZ ", 4) == 0) {
Syslog('+', "Sysop : %s", msg+4);
strncpy(history.sysop, msg+4, 35);
} else if (strncmp(msg, "LOC ", 4) == 0) {
Syslog('+', "Location: %s", msg+4);
strncpy(history.location, msg+4, 35);
} else if (strncmp(msg, "NDL ", 4) == 0) {
Syslog('+', "Flags : %s", msg+4);
} else if (strncmp(msg, "TIME ", 5) == 0) {
Syslog('+', "Time : %s", msg+5);
} else if (strncmp(msg, "VER ", 4) == 0) {
Syslog('+', "Uses : %s", msg+4);
if ((p = strstr(msg+4, PRTCLNAME "/")) && (q = strstr(p, "."))) {
bp.Major = atoi(p + 6);
bp.Minor = atoi(q + 1);
}
2005-04-07 19:28:52 +00:00
/*
* Irex 2.24 upto 2.29 claims binkp/1.1 while it is binkp/1.0
* Set a flag so we can activate a workaround. This only works
* for incoming sessions.
*/
if ((p = strstr(msg+4, "Internet Rex 2."))) {
q = strtok(p + 15, (char *)" \0");
if ((atoi(q) >= 24) && (atoi(q) <= 29)) {
Syslog('b', " : Irex bug detected, workaround activated");
bp.buggyIrex = TRUE;
}
}
2004-01-14 21:06:49 +00:00
} else if (strncmp(msg, "PHN ", 4) == 0) {
Syslog('+', "Phone : %s", msg+4);
} else if (strncmp(msg, "OPM ", 4) == 0) {
Syslog('+', "Remark : %s", msg+4);
} else if (strncmp(msg, "TRF ", 4) == 0) {
Syslog('+', "Binkp: remote has %s mail/files for us", msg+4);
} else if (strncmp(msg, "OPT ", 4) == 0) {
Syslog('+', "Options : %s", msg+4);
2004-01-25 13:42:05 +00:00
p = msg;
q = strtok(p, " \n\r\0");
while ((q = strtok(NULL, " \r\n\0"))) {
if (strncmp(q, (char *)"CRAM-MD5-", 9) == 0) { /* No SHA-1 support */
if (CFG.NoMD5) {
Syslog('+', "Binkp: Remote supports MD5, but it's turned off here");
} else {
if (bp.MD_Challenge)
free(bp.MD_Challenge);
bp.MD_Challenge = MD_getChallenge(q, NULL);
}
2005-06-19 15:12:07 +00:00
#ifdef USE_BINKDZLIB
} else if (strncmp(q, (char *)"EXTCMD", 6) == 0) {
bp.extcmd = TRUE;
Syslog('+', "Binkp: remote supports EXTCMD mode");
#ifdef HAVE_BZLIB_H
} else if (strncmp(q, (char *)"BZ2", 3) == 0) {
if (bp.BZ2flag == WeCan) {
bp.BZ2flag = TheyWant;
Syslog('+', "Binkp: remote supports BZ2 mode");
} else {
Syslog('b', "BZ2flag is %s and received BZ2 option", opstate[bp.BZ2flag]);
}
#endif
#ifdef HAVE_ZLIB_H
} else if (strncmp(q, (char *)"GZ", 2) == 0) {
if (bp.GZflag == WeCan) {
bp.GZflag = TheyWant;
Syslog('+', "Binkp: remote supports GZ mode");
} else {
Syslog('b', "GZflag is %s and received GZ option", opstate[bp.GZflag]);
}
#endif
#endif /* use_binkdzlib */
#ifdef HAVE_ZLIB_H
2004-01-25 13:42:05 +00:00
} else if (strncmp(q, (char *)"PLZ", 3) == 0) {
if (bp.PLZflag == WeCan) {
bp.PLZflag = TheyWant;
2005-06-19 15:58:42 +00:00
Syslog('+', "Binkp: remote supports PLZ mode");
2004-01-25 13:42:05 +00:00
} else {
Syslog('b', "PLZflag is %s and received PLZ option", opstate[bp.PLZflag]);
}
#endif
2004-01-14 21:06:49 +00:00
}
}
2004-01-25 13:42:05 +00:00
2004-01-14 21:06:49 +00:00
} else {
Syslog('+', "Binkp: M_NUL \"%s\"", msg);
}
}
/*
* Poll for a frame, returns:
* -1 = Error
* 0 = Nothing yet
* 1 = Got a frame
* 2 = Frame not processed
2004-01-25 15:16:11 +00:00
* 3 = Uncompress error
2004-01-14 21:06:49 +00:00
*/
int binkp_poll_frame(void)
{
2004-01-26 12:12:08 +00:00
int c, rc = 0, bcmd;
2004-01-25 13:42:05 +00:00
#ifdef HAVE_ZLIB_H
unsigned long zlen;
char *zbuf;
#endif
2004-01-14 21:06:49 +00:00
for (;;) {
if (bp.GotFrame) {
Syslog('b', "Binkp: WARNING: frame not processed");
rc = 2;
break;
} else {
c = GETCHAR(0);
if (c < 0) {
c = -c;
if (c == STAT_TIMEOUT) {
2004-01-25 10:57:35 +00:00
msleep(1);
2004-01-14 21:06:49 +00:00
rc = 0;
break;
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
Syslog('?', "Binkp: receiver status %s", ttystat[c]);
bp.rc = (MBERR_TTYIO + (-c));
rc = -1;
break;
2002-06-16 16:44:38 +00:00
} else {
2004-01-14 21:06:49 +00:00
switch (bp.rxlen) {
case 0: bp.header = c << 8;
rc = 0;
break;
case 1: bp.header += c;
rc = 0;
break;
default:bp.rxbuf[bp.rxlen-2] = c;
}
if (bp.rxlen == 1) {
2004-01-25 13:42:05 +00:00
bp.cmd = bp.header & BINKP_CONTROL_BLOCK;
#ifdef HAVE_ZLIB_H
if ((bp.PLZflag == Active) && (bp.rmode == CompNone)) {
2004-01-25 13:42:05 +00:00
bp.blklen = bp.header & 0x3fff;
} else {
bp.blklen = bp.header & 0x7fff;
}
#else
2004-01-14 21:06:49 +00:00
bp.blklen = bp.header & 0x7fff;
2004-01-25 13:42:05 +00:00
#endif
2004-01-14 21:06:49 +00:00
}
if ((bp.rxlen == (bp.blklen + 1) && (bp.rxlen >= 1))) {
bp.GotFrame = TRUE;
2004-01-25 13:42:05 +00:00
#ifdef HAVE_ZLIB_H
/*
* Got a PLZ compressed block
*/
if ((bp.PLZflag == Active) && (bp.header & BINKP_PLZ_BLOCK) && (bp.rmode == CompNone) && bp.blklen) {
2004-01-25 14:51:44 +00:00
zbuf = calloc(BINKP_ZIPBUFLEN, sizeof(char));
2004-01-29 21:12:21 +00:00
zlen = BINKP_PLZ_BLOCK -1;
2004-01-25 15:16:11 +00:00
rc = uncompress(zbuf, &zlen, bp.rxbuf, bp.rxlen -1);
if (rc == Z_OK) {
2004-01-26 12:12:08 +00:00
bp.rxcompressed += (zlen - (bp.rxlen -1));
2004-01-25 15:16:11 +00:00
memmove(bp.rxbuf, zbuf, zlen);
bp.rxlen = zlen +1;
bp.blklen = zlen;
} else {
free(zbuf);
2004-01-29 21:12:21 +00:00
Syslog('!', "Binkp: uncompress error %d", rc);
2004-01-25 15:16:11 +00:00
return 3;
}
free(zbuf);
2004-01-25 13:42:05 +00:00
}
#endif
2004-01-14 21:06:49 +00:00
bp.rxbuf[bp.rxlen-1] = '\0';
if (bp.cmd) {
bp.messages++;
bcmd = bp.rxbuf[0];
Syslog('b', "Binkp: rcvd %s %s", bstate[bcmd], printable(bp.rxbuf+1, 0));
if (bcmd == MM_EOB) {
bp.remote_EOB = TRUE;
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
} else {
2004-01-14 21:06:49 +00:00
Syslog('b', "Binkp: rcvd data (%d)", bp.rxlen -1);
}
2004-01-26 15:22:17 +00:00
binkp_settimer(BINKP_TIMEOUT);
2004-01-29 21:12:21 +00:00
Nopper();
2004-01-14 21:06:49 +00:00
rc = 1;
break;
}
bp.rxlen++;
}
}
}
return rc;
}
/*
* Add received command frame to the queue, will be processed by the transmitter.
*/
void binkp_add_message(char *frame)
{
the_queue **tmpl;
int bcmd;
bcmd = frame[0];
Syslog('b', "Binkp: add message %s %s", bstate[bcmd], printable(frame +1, 0));
for (tmpl = &tql; *tmpl; tmpl = &((*tmpl)->next));
*tmpl = (the_queue *)malloc(sizeof(the_queue));
(*tmpl)->next = NULL;
(*tmpl)->cmd = frame[0];
(*tmpl)->data = xstrcpy(frame +1);
bp.msgs_on_queue++;
}
/*
* Process all messages on the queue, the oldest are on top, after
* processing release memory and reset the messages queue.
*/
int binkp_process_messages(void)
{
the_queue *tmpq, *oldq;
binkp_list *tmp;
file_list *tsl;
int Found, rmode;
char *lname, *ropts;
2004-01-14 21:06:49 +00:00
time_t ltime;
long lsize, loffs;
Syslog('b', "Binkp: Process The Messages Queue Start");
lname = calloc(512, sizeof(char));
ropts = calloc(512, sizeof(char));
2004-01-14 21:06:49 +00:00
for (tmpq = tql; tmpq; tmpq = tmpq->next) {
Syslog('+', "Binkp: %s \"%s\"", bstate[tmpq->cmd], printable(tmpq->data, 0));
if (tmpq->cmd == MM_GET) {
rmode = CompNone;
2004-01-14 21:06:49 +00:00
sprintf(lname, "%s", strtok(tmpq->data, " \n\r"));
lsize = atoi(strtok(NULL, " \n\r"));
ltime = atoi(strtok(NULL, " \n\r"));
loffs = atoi(strtok(NULL, " \n\r"));
sprintf(ropts, "%s", strtok(NULL, " \n\r"));
Syslog('b', "Binkp: m_file options \"%s\"", ropts);
if (strcmp((char *)"GZ", ropts) == 0)
rmode = CompGZ;
else if (strcmp((char *)"BZ2", ropts) == 0)
rmode = CompBZ2;
else if (strcmp((char *)"NZ", ropts) == 0) {
rmode = CompNone;
2005-06-19 15:12:07 +00:00
#ifdef USE_BINKDZLIB
#ifdef HAVE_ZLIB_H
bp.GZflag = No;
#endif
#ifdef HAVE_BZLIB_H
bp.BZ2flag = No;
2005-06-19 15:12:07 +00:00
#endif
#endif
Syslog('+', "Binkp: received NZ on M_GET command, compression turned off");
}
2004-01-14 21:06:49 +00:00
Found = FALSE;
for (tmp = bll; tmp; tmp = tmp->next) {
if ((strcmp(lname, tmp->remote) == 0) && (lsize == tmp->size) && (ltime == tmp->date)) {
if (lsize == loffs) {
/*
* Requested offset is filesize, close file.
*/
if (cursend && (strcmp(lname, cursend->remote) == 0) &&
(lsize == cursend->size) && (ltime == cursend->date)) {
Syslog('b', "Got M_GET with offset == filesize for current file, close");
2004-03-12 19:56:47 +00:00
/*
* calculate time needed and bytes transferred
*/
gettimeofday(&txtvend, &bp.tz);
2003-09-13 21:07:06 +00:00
/*
2004-01-14 21:06:49 +00:00
* Close transmitter file
2003-09-13 21:07:06 +00:00
*/
2004-01-14 21:06:49 +00:00
fclose(bp.txfp);
bp.txfp = NULL;
if (bp.txpos >= 0) {
bp.stxpos = bp.txpos - bp.stxpos;
Syslog('+', "Binkp: OK %s", transfertime(txtvstart, txtvend, bp.stxpos, TRUE));
} else {
Syslog('+', "Binkp: transmitter skipped file after %ld seconds",
txtvend.tv_sec - txtvstart.tv_sec);
}
2004-03-12 19:56:47 +00:00
cursend->state = Got; // 12-03-2004 changed from IsSent, bug from LdG with FT.
2004-01-14 21:06:49 +00:00
cursend = NULL;
bp.TxState = TxGNF;
2004-03-12 19:56:47 +00:00
for (tsl = tosend; tsl; tsl = tsl->next) { // Added 12-03
if (tsl->remote == NULL) {
execute_disposition(tsl);
} else {
if (strcmp(cursend->local, tsl->local) == 0) {
execute_disposition(tsl);
}
}
}
2004-01-14 21:06:49 +00:00
} else {
Syslog('+', "Binkp: requested offset = filesize, but is not the current file");
2004-03-12 19:56:47 +00:00
if (tmp->state == IsSent) { // Added 12-03
Syslog('b', "Binkp: file is sent, treat as m_got");
tmp->state = Got;
for (tsl = tosend; tsl; tsl = tsl->next) {
if (tsl->remote == NULL) {
execute_disposition(tsl);
} else {
if (strcmp(tmp->local, tsl->local) == 0) {
execute_disposition(tsl);
}
}
}
}
2003-09-13 21:07:06 +00:00
}
2004-01-14 21:06:49 +00:00
} else if (loffs < lsize) {
tmp->state = NoState;
tmp->offset = loffs;
if (loffs) {
Syslog('+', "Binkp: Remote wants %s for resync at offset %ld", tmp->remote, tmp->offset);
} else {
Syslog('+', "Binkp: Remote wants %s again from start", tmp->remote);
}
bp.TxState = TxGNF;
} else {
Syslog('+', "Binkp: requested offset > filesize, ignore");
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
Found = TRUE;
break;
}
if (!Found) {
Syslog('+', "Binkp: unexpected M_GET \"%s\"", tmpq->data); /* Ignore frame */
2001-08-17 05:46:24 +00:00
}
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
} else if (tmpq->cmd == MM_GOT) {
sprintf(lname, "%s", strtok(tmpq->data, " \n\r"));
lsize = atoi(strtok(NULL, " \n\r"));
ltime = atoi(strtok(NULL, " \n\r"));
Found = FALSE;
for (tmp = bll; tmp; tmp = tmp->next) {
if ((strcmp(lname, tmp->remote) == 0) && (lsize == tmp->size) && (ltime == tmp->date)) {
Found = TRUE;
if (tmp->state == Sending) {
Syslog('+', "Binkp: remote refused %s", tmp->remote);
fclose(bp.txfp);
bp.txfp = NULL;
bp.TxState = TxGNF;
cursend = NULL;
} else {
Syslog('+', "Binkp: remote GOT \"%s\"", tmp->remote);
}
tmp->state = Got;
for (tsl = tosend; tsl; tsl = tsl->next) {
if (tsl->remote == NULL) {
execute_disposition(tsl);
} else {
if (strcmp(tmp->local, tsl->local) == 0) {
execute_disposition(tsl);
}
}
}
break;
}
}
if (!Found) {
Syslog('+', "Binkp: unexpected M_GOT \"%s\"", tmpq->data); /* Ignore frame */
}
} else if (tmpq->cmd == MM_SKIP) {
sprintf(lname, "%s", strtok(tmpq->data, " \n\r"));
lsize = atoi(strtok(NULL, " \n\r"));
ltime = atoi(strtok(NULL, " \n\r"));
Found = FALSE;
for (tmp = bll; tmp; tmp = tmp->next) {
if ((strcmp(lname, tmp->remote) == 0) && (lsize == tmp->size) && (ltime == tmp->date)) {
Found = TRUE;
if (tmp->state == Sending) {
Syslog('+', "Binkp: remote skipped %s, may be accepted later", tmp->remote);
fclose(bp.txfp);
bp.txfp = NULL;
cursend = NULL;
bp.TxState = TxGNF;
} else {
Syslog('+', "Binkp: remote refused %s", tmp->remote);
}
tmp->state = Skipped;
break;
}
}
if (!Found) {
Syslog('+', "Binkp: unexpected M_SKIP \"%s\"", tmpq->data); /* Ignore frame */
}
} else {
/* Illegal message on the queue */
2001-08-17 05:46:24 +00:00
}
2004-01-14 21:06:49 +00:00
}
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
/*
* Tidy the messages queue and reset.
*/
for (tmpq = tql; tmpq; tmpq = oldq) {
oldq = tmpq->next;
if (tmpq->data)
free(tmpq->data);
free(tmpq);
}
tql = NULL;
free(lname);
bp.msgs_on_queue = 0;
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
Syslog('b', "Binkp: Process The Messages Queue End");
return 0;
}
2003-09-13 21:07:06 +00:00
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
/*
* Count number of pending files
*/
int binkp_pendingfiles(void)
{
binkp_list *tmpl;
int count = 0;
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
for (tmpl = bll; tmpl; tmpl = tmpl->next) {
if ((tmpl->state != Got) && (tmpl->state != Skipped))
count++;
}
if (count)
Syslog('b', "Binkp: %d pending files on queue", count);
return count;
}
/*
* This function is called two times if a partial file exists from openfile.
* 1. A partial file is detected, send a GET to the remote, set DidSendGET flag.
* 2. DidSendGET is set, return 0 and let openfile open the file in append mode.
*/
int binkp_resync(off_t off)
{
Syslog('b', "Binkp: resync(%d) DidSendGET=%s", off, bp.DidSendGET ?"TRUE":"FALSE");
if (!bp.DidSendGET) {
binkp_send_command(MM_GET, "%s %ld %ld %ld", bp.rname, bp.rsize, bp.rtime, off);
bp.DidSendGET = TRUE;
Syslog('+', "Binkp: already %lu bytes received, requested restart with offset", (unsigned long)off);
return -1; /* Signal openfile not to open the file */
}
bp.DidSendGET = FALSE;
return 0; /* Signal openfile to open the file in append mode */
}
/*
2004-07-16 14:51:07 +00:00
* Translate string to binkp escaped string, unsafe characters are escaped.
*/
2004-01-14 21:06:49 +00:00
char *unix2binkp(char *fn)
{
static char buf[PATH_MAX];
char *p, *q;
memset(&buf, 0, sizeof(buf));
p = fn;
q = buf;
while (*p) {
if (strspn(p, (char *)BNKCHARS)) {
*q++ = *p;
*q = '\0';
} else {
if (nodes.WrongEscape) {
sprintf(q, "\\%2x", p[0]);
} else {
sprintf(q, "\\x%2x", p[0]);
2003-09-13 21:07:06 +00:00
}
2004-01-14 21:06:49 +00:00
}
while (*q)
q++;
p++;
}
*q = '\0';
return buf;
}
2003-09-13 21:07:06 +00:00
2004-01-14 21:06:49 +00:00
/*
2004-07-16 14:51:07 +00:00
* Translate escaped binkp string to normal string.
*/
2004-01-14 21:06:49 +00:00
char *binkp2unix(char *fn)
{
static char buf[PATH_MAX];
char *p, *q, hex[3];
int c;
memset(&buf, 0, sizeof(buf));
p = fn;
q = buf;
while (*p) {
if (p[0] == '\\') {
p++;
if (*p == '\\') {
2002-06-16 16:44:38 +00:00
/*
2004-01-14 21:06:49 +00:00
* A backslash is transmitted
2002-06-16 16:44:38 +00:00
*/
2004-01-14 21:06:49 +00:00
*q++ = '\\';
*q = '\0';
} else {
2002-06-16 16:44:38 +00:00
/*
2004-01-14 21:06:49 +00:00
* If remote sends \x0a method instead of \0a, eat the x character.
* Remotes should send the x character, But some (Argus and Irex) don't.
2002-06-16 16:44:38 +00:00
*/
2004-01-14 21:06:49 +00:00
if ((*p == 'x') || (*p == 'X'))
p++;
/*
* Decode hex characters
*/
hex[0] = *p++;
hex[1] = *p;
hex[2] = '\0';
sscanf(hex, "%2x", &c);
*q++ = c;
*q = '\0';
2002-06-16 16:44:38 +00:00
}
2004-01-14 21:06:49 +00:00
} else {
*q++ = *p;
*q = '\0';
}
p++;
}
*q = '\0';
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
return buf;
}
2002-06-16 16:44:38 +00:00
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
/*
* Fill internal binkp filelist
*/
void fill_binkp_list(binkp_list **bkll, file_list *fal, off_t offs)
{
binkp_list **tmpl;
2004-07-16 14:51:07 +00:00
FILE *fp;
2004-01-14 21:06:49 +00:00
struct stat tstat;
int comp;
char *unp;
2001-08-17 05:46:24 +00:00
2004-01-14 21:06:49 +00:00
for (tmpl = bkll; *tmpl; tmpl = &((*tmpl)->next));
*tmpl = (binkp_list *)malloc(sizeof(binkp_list));
2002-06-16 16:44:38 +00:00
2004-01-14 21:06:49 +00:00
(*tmpl)->next = NULL;
(*tmpl)->state = NoState;
2004-07-16 14:51:07 +00:00
if ((fp = fopen(fal->local, "r")) == NULL) {
if ((errno == ENOENT) || (errno == EINVAL)) {
Syslog('+', "Binkp: file %s doesn't exist, removing", MBSE_SS(fal->local));
(*tmpl)->state = Got;
execute_disposition(fal);
}
} else {
fclose(fp);
stat(fal->local, &tstat);
if (strstr(fal->remote, (char *)".pkt"))
bp.nethold += tstat.st_size;
else
bp.mailhold += tstat.st_size;
}
(*tmpl)->get = FALSE;
(*tmpl)->local = xstrcpy(fal->local);
(*tmpl)->remote = xstrcpy(unix2binkp(fal->remote));
(*tmpl)->offset = offs;
(*tmpl)->size = tstat.st_size;
(*tmpl)->date = tstat.st_mtime;
(*tmpl)->compress = CompNone;
/*
* Search compression method, but only if GZ or BZ2 compression is active.
*/
comp = FALSE;
2005-06-19 15:12:07 +00:00
#ifdef USE_BINKDZLIB
#ifdef HAVE_ZLIB_H
if (bp.GZflag == Active)
comp = TRUE;
#endif
#ifdef HAVE_BZLIB_H
if (bp.BZ2flag == Active)
comp = TRUE;
2005-06-19 15:12:07 +00:00
#endif
#endif
if (!comp)
return;
/*
* Don't compress small files
*/
if (tstat.st_size <= 1024)
return;
/*
* Check file type
*/
unp = unpacker(fal->local);
if (unp && strcmp((char *)"TAR", unp)) {
Syslog('b', "Binkp: %s send as-is", fal->local);
return;
}
2005-06-19 15:12:07 +00:00
#ifdef USE_BINKDZLIB
#ifdef HAVE_BZLIB_H
/*
* Use BZ2 for files > 200K
*/
if ((bp.BZ2flag == Active) && (tstat.st_size > 202400)) {
(*tmpl)->compress = CompBZ2;
Syslog('b', "Binkp: %s compressor BZ2", fal->local);
return;
}
#endif
#ifdef HAVE_ZLIB_H
if (bp.GZflag == Active) {
(*tmpl)->compress = CompGZ;
Syslog('b', "Binkp: %s compressor GZ", fal->local);
return;
}
2005-06-19 15:12:07 +00:00
#endif
#endif
Syslog('+', "Binkp: compressor select internal error");
2004-01-14 21:06:49 +00:00
}
2002-06-16 16:44:38 +00:00
2003-08-23 14:57:51 +00:00
2004-01-14 21:06:49 +00:00
/*
* Clear current filelist
*/
2004-07-16 14:51:07 +00:00
void binkp_clear_filelist(int rc)
2004-01-14 21:06:49 +00:00
{
binkp_list *tmp;
2004-07-16 14:51:07 +00:00
file_list *fal;
2004-01-14 21:06:49 +00:00
if (tosend != NULL) {
2004-07-16 14:59:53 +00:00
Syslog('b', "Binkp: binkp_clear_filelist(%d)", rc);
2004-01-14 21:06:49 +00:00
for (tmp = bll; bll; bll = tmp) {
tmp = bll->next;
if (bll->local)
free(bll->local);
if (bll->remote)
free(bll->remote);
free(bll);
}
2004-07-16 14:51:07 +00:00
/* WARNING: Added 16-07-2004 to see if this is safe to clean /flo files.
*
* Remove sent fake files like .spl and .flo
*/
for (fal = tosend; fal; fal = fal->next) {
if ((fal->remote == NULL) && (rc == 0))
execute_disposition(fal);
}
2004-01-14 21:06:49 +00:00
tidy_filelist(tosend, TRUE);
tosend = NULL;
respond = NULL;
}
}
/*****************************************************************************
*
* Compression support for GZ and BZ2 modes. Routines from the original
* binkd package, slightly adopted for mbcico.
*
* Original written by val khokhlov, FIDONet 2:550/180
*
*/
#ifdef USE_BINKDZLIB
int compress_init(int type, void **data)
{
int lvl;
switch (type) {
#ifdef HAVE_BZLIB_H
case CompBZ2: {
*data = calloc(1, sizeof(bz_stream));
if (*data == NULL) {
Syslog('+', "Binkp: compress_init: not enough memory (%lu needed)", sizeof(bz_stream));
return BZ_MEM_ERROR;
}
lvl = 1; /* default is small (100K) buffer */
return BZ2_bzCompressInit((bz_stream *)*data, lvl, 0, 0);
}
#endif
#ifdef HAVE_ZLIB_H
case CompGZ: {
*data = calloc(1, sizeof(z_stream));
if (*data == NULL) {
Syslog('+', "Binkp: compress_init: not enough memory (%lu needed)", sizeof(z_stream));
return Z_MEM_ERROR;
}
lvl = 9; /* Maximum compression */
if (lvl <= 0) lvl = Z_DEFAULT_COMPRESSION;
return deflateInit((z_stream *)*data, lvl);
}
#endif
default:
Syslog('+', "Binkp: unknown compression method: %d; data lost", type);
}
return -1;
}
int do_compress(int type, char *dst, int *dst_len, char *src, int *src_len, int finish, void *data)
{
int rc;
switch (type) {
#ifdef HAVE_BZLIB_H
case CompBZ2: {
bz_stream *zstrm = (bz_stream *)data;
zstrm->next_in = (char *)src;
zstrm->avail_in = (unsigned int)*src_len;
zstrm->next_out = (char *)dst;
zstrm->avail_out = (unsigned int)*dst_len;
rc = BZ2_bzCompress(zstrm, finish ? BZ_FINISH : 0);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == BZ_RUN_OK || rc == BZ_FLUSH_OK || rc == BZ_FINISH_OK)
rc = 0;
if (rc == BZ_STREAM_END)
rc = 1;
return rc;
}
#endif
#ifdef HAVE_ZLIB_H
case CompGZ: {
z_stream *zstrm = (z_stream *)data;
zstrm->next_in = (Bytef *)src;
zstrm->avail_in = (uLong)*src_len;
zstrm->next_out = (Bytef *)dst;
zstrm->avail_out = (uLong)*dst_len;
rc = deflate(zstrm, finish ? Z_FINISH : 0);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == Z_STREAM_END)
rc = 1;
return rc;
}
#endif
default:
Syslog('+', "Binkp: unknown compression method: %d; data lost", type);
}
return -1;
}
void compress_deinit(int type, void *data)
{
switch (type) {
#ifdef HAVE_BZLIB_H
case CompBZ2: {
int rc = BZ2_bzCompressEnd((bz_stream *)data);
if (rc < 0)
Syslog('+', "Binkp: BZ2_bzCompressEnd error: %d", rc);
break;
}
#endif
#ifdef HAVE_ZLIB_H
case CompGZ: {
int rc = deflateEnd((z_stream *)data);
if (rc < 0)
Syslog('+', "Binkp: deflateEnd error: %d", rc);
break;
}
#endif
default:
Syslog('+', "Binkp: unknown compression method: %d", type);
}
free(data);
return;
}
void compress_abort(int type, void *data)
{
char buf[1024];
int i, j;
if (data) {
Syslog('b', "Binkp: purge compress buffers");
do {
i = sizeof(buf);
j = 0;
} while (do_compress(type, buf, &i, NULL, &j, 1, data) == 0 && i > 0);
compress_deinit(type, data);
}
}
int decompress_init(int type, void **data)
{
switch (type) {
#ifdef HAVE_BZLIB_H
case CompBZ2: {
*data = calloc(1, sizeof(bz_stream));
if (*data == NULL) {
Syslog('+', "Binkp: decompress_init: not enough memory (%lu needed)", sizeof(bz_stream));
return BZ_MEM_ERROR;
}
return BZ2_bzDecompressInit((bz_stream *)*data, 0, 0);
}
#endif
#ifdef HAVE_ZLIB_H
case CompGZ: {
*data = calloc(1, sizeof(z_stream));
if (*data == NULL) {
Syslog('+', "Binkp: decompress_init: not enough memory (%lu needed)", sizeof(z_stream));
return Z_MEM_ERROR;
}
return inflateInit((z_stream *)*data);
}
#endif
default:
Syslog('+', "Binkp: unknown compression method: %d; data lost", type);
}
return -1;
}
int do_decompress(int type, char *dst, int *dst_len, char *src, int *src_len, void *data)
{
int rc;
switch (type) {
#ifdef HAVE_BZLIB_H
case CompBZ2: {
bz_stream *zstrm = (bz_stream *)data;
zstrm->next_in = (char *)src;
zstrm->avail_in = (unsigned int)*src_len;
zstrm->next_out = (char *)dst;
zstrm->avail_out = (unsigned int)*dst_len;
rc = BZ2_bzDecompress(zstrm);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == BZ_RUN_OK || rc == BZ_FLUSH_OK)
rc = 0;
if (rc == BZ_STREAM_END)
rc = 1;
return rc;
}
#endif
#ifdef HAVE_ZLIB_H
case CompGZ: {
z_stream *zstrm = (z_stream *)data;
zstrm->next_in = (Bytef *)src;
zstrm->avail_in = (uLong)*src_len;
zstrm->next_out = (Bytef *)dst;
zstrm->avail_out = (uLong)*dst_len;
rc = inflate(zstrm, 0);
*src_len -= (int)zstrm->avail_in;
*dst_len -= (int)zstrm->avail_out;
if (rc == Z_STREAM_END)
rc = 1;
return rc;
}
#endif
default:
Syslog('+', "Binkp: unknown compression method: %d; data lost", type);
}
return 0;
}
int decompress_deinit(int type, void *data)
{
int rc = -1;
switch (type) {
#ifdef HAVE_BZLIB_H
case CompBZ2: {
rc = BZ2_bzDecompressEnd((bz_stream *)data);
break;
}
#endif
#ifdef HAVE_ZLIB_H
case CompGZ: {
rc = inflateEnd((z_stream *)data);
break;
}
#endif
default:
Syslog('+', "Binkp: unknown compression method: %d", type);
break;
}
free(data);
return rc;
}
int decompress_abort(int type, void *data)
{
char buf[1024];
int i, j;
if (data) {
Syslog('b', "Binkp: purge decompress buffers");
do {
i = sizeof(buf);
j = 0;
} while (do_decompress(type, buf, &i, NULL, &j, data) == 0 && i > 0);
return decompress_deinit(type, data);
}
return 0;
}
#endif