binkp reengineered

master
Sergey Dorofeev 13 years ago
parent d2d1edf80a
commit 185cff17a5

@ -19,12 +19,12 @@ int readstr(int s, char *buf, int len) {
while(1) { while(1) {
r=recv(s, &c, 1, 0); r=recv(s, &c, 1, 0);
if(r==0){ if(r==0){
puts("remote socket shutdown"); //puts("remote socket shutdown");
return -3; return -3;
} }
if(r==-1){ if(r==-1){
puts("error reading string"); //puts("error reading string");
printf("%d %s\n", errno, strerror(errno)); //printf("%d %s\n", errno, strerror(errno));
return -3; return -3;
} }
if(c=='\n') { if(c=='\n') {
@ -34,7 +34,7 @@ int readstr(int s, char *buf, int len) {
*buf++=c; *buf++=c;
len--; len--;
if(!len) { if(!len) {
puts("string buffer overflow"); //puts("string buffer overflow");
return -3; return -3;
} }
} }
@ -48,24 +48,24 @@ int sendstr(int s, const char *buf) {
while(l>0) { while(l>0) {
c=send(s, buf, l, 0); c=send(s, buf, l, 0);
if(c==-1){ if(c==-1){
puts("error sending string"); //puts("error sending string");
printf("%d %s\n", errno, strerror(errno)); //printf("%d %s\n", errno, strerror(errno));
return -3; return -3;
} }
if(c==0){ if(c==0){
puts("zero send: may loop infinitely"); //puts("zero send: may loop infinitely");
return -3; return -3;
} }
l-=c; l-=c;
} }
c=send(s, &nl, 1, 0); c=send(s, &nl, 1, 0);
if(c==-1){ if(c==-1){
puts("error sending string"); //puts("error sending string");
printf("%d %s\n", errno, strerror(errno)); //printf("%d %s\n", errno, strerror(errno));
return -3; return -3;
} }
if(c==0){ if(c==0){
puts("zero send: may loop infinitely"); //puts("zero send: may loop infinitely");
return -3; return -3;
} }
return 0; return 0;
@ -107,7 +107,8 @@ void netspool_start(s_netspool_state *state, const char *host, const char *port,
while(a) { while(a) {
r = connect(s, a->ai_addr, a->ai_addrlen); r = connect(s, a->ai_addr, a->ai_addrlen);
if(r==-1) { if(r==-1) {
printf("%d %s\n", errno, strerror(errno)); //printf("%d %s\n", errno, strerror(errno));
;
} else { } else {
break; break;
} }
@ -122,16 +123,16 @@ void netspool_start(s_netspool_state *state, const char *host, const char *port,
} }
r = readstr(s, strbuf, STRBUF); r = readstr(s, strbuf, STRBUF);
if( r ) { state->state = NS_ERROR; state->error = "IO error"; } if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; }
puts(strbuf); //puts(strbuf); -- hello from remote
snprintf(strbuf, STRBUF, "ADDRESS %s", address); snprintf(strbuf, STRBUF, "ADDRESS %s", address);
r = sendstr(s, strbuf); r = sendstr(s, strbuf);
if( r ) { state->state = NS_ERROR; state->error = "IO error"; } if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; }
snprintf(strbuf, STRBUF, "PASSWORD %s", password); snprintf(strbuf, STRBUF, "PASSWORD %s", password);
r = sendstr(s, strbuf); r = sendstr(s, strbuf);
if( r ) { state->state = NS_ERROR; state->error = "IO error"; } if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; }
state->state = NS_READY; state->state = NS_READY;
} }
@ -157,7 +158,7 @@ void netspool_receive(s_netspool_state *state)
r = readstr(state->socket, strbuf, STRBUF); r = readstr(state->socket, strbuf, STRBUF);
if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; } if( r ) { state->state = NS_ERROR; state->error = "IO error"; return; }
puts(strbuf); //puts(strbuf);
if(strcmp(strbuf, "QUEUE EMPTY")==0) { if(strcmp(strbuf, "QUEUE EMPTY")==0) {
state->state = NS_READY; state->state = NS_READY;
return; return;
@ -165,7 +166,7 @@ void netspool_receive(s_netspool_state *state)
if(strncmp(strbuf, "FILENAME ", 9)==0) { if(strncmp(strbuf, "FILENAME ", 9)==0) {
strcpy(state->filename, strbuf+9); strcpy(state->filename, strbuf+9);
puts(state->filename); //puts(state->filename);
} else { } else {
state->state = NS_ERROR; state->state = NS_ERROR;
state->error = "expected filename or queue empty"; state->error = "expected filename or queue empty";
@ -180,7 +181,7 @@ void netspool_receive(s_netspool_state *state)
exit(-5); exit(-5);
} */ } */
sscanf(strbuf+7, "%Lu", &state->length); sscanf(strbuf+7, "%Lu", &state->length);
printf("length=%Lu\n", state->length); //printf("length=%Lu\n", state->length);
state->state = NS_RECVFILE; state->state = NS_RECVFILE;
} else { } else {
state->state = NS_ERROR; state->state = NS_ERROR;
@ -194,7 +195,7 @@ int netspool_read(s_netspool_state *state, void *buf, int buflen)
{ {
int n; int n;
if( state->length == 0 ) { if( state->length == 0 ) {
puts("everithing is read"); //puts("everithing is read");
return 0; return 0;
} }
@ -207,8 +208,8 @@ int netspool_read(s_netspool_state *state, void *buf, int buflen)
} }
if(n==-1) { if(n==-1) {
puts("error reading data"); //puts("error reading data");
printf("%d %s\n", errno, strerror(errno)); //printf("%d %s\n", errno, strerror(errno));
state->state = NS_ERROR; state->state = NS_ERROR;
state->error = "IO error"; state->error = "IO error";
return -1; return -1;

File diff suppressed because it is too large Load Diff

@ -21,198 +21,12 @@
#include "prot_common.h" #include "prot_common.h"
#include "prot_binkp.h" #include "prot_binkp.h"
/*****************************************************************************
* Initialise binkp state structure, allocate memory for buffers, etc.
*
* Arguments:
* bpi pointer to the binkp state structure (s_bpinfo)
*
* Return value:
* None
*/
void binkp_init_bpinfo(s_bpinfo *bpi)
{
memset(bpi, '\0', sizeof(s_bpinfo));
bpi->obuf = (char*)xmalloc(BINKP_MAX_BLKSIZE + BINKP_BLK_HDRSIZE + 1);
bpi->opos = 0;
bpi->optr = bpi->obuf;
bpi->inew = TRUE;
bpi->isize = -1;
bpi->ibuf = (char*)xmalloc(BINKP_MAX_BLKSIZE + BINKP_BLK_HDRSIZE + 1);
bpi->timeout = conf_number(cf_binkp_timeout);
if( !bpi->timeout )
bpi->timeout = 300;
}
/*****************************************************************************
* DeInitialise binkp state structure, release allocated memory, etc.
*
* Arguments:
* bpi pointer to the binkp state structure (s_bpinfo)
*
* Return value:
* None
*/
void binkp_deinit_bpinfo(s_bpinfo *bpi)
{
if( bpi->obuf )
free(bpi->obuf);
if( bpi->ibuf )
free(bpi->ibuf);
if( bpi->msgqueue )
free(bpi->msgqueue);
memset(bpi, '\0', sizeof(s_bpinfo));
}
/*****************************************************************************
* Fills s[0] and s[1] with binkp frame header using value of u
*
* Arguments:
* s pointer to the binkp's frame header (2 bytes)
* val value to put
*
* Return value:
* None
*/
void binkp_puthdr(char *s, unsigned int val)
{
s[0] = ( (unsigned long) val >> 8 ) & 0xff;
s[1] = ( (unsigned long) val ) & 0xff;
}
/*****************************************************************************
* Fills s[0] with binkp message type value
*
* Arguments:
* s pointer to the destination buffer
* msg message type
*
* Return value:
* None
*/
void binkp_putmsgtype(char *s, e_bpmsg msg)
{
s[0] = ( (unsigned int) msg ) & 0xff;
}
/*****************************************************************************
* Extract size from binkp frame header
*
* Arguments:
* s pointer to the source buffer
*
* Return value:
* Value, extracted from binkp header
*/
int binkp_gethdr(const char *s)
{
return ( (unsigned int) (((unsigned char) s[0]) & 0x7f) << 8 )
| ( (unsigned int) (((unsigned char) s[1]) & 0xff) );
}
/*****************************************************************************
* Extract message type from s[0]
*
* Arguments:
* s pointer to the source buffer (1 byte)
*
* Return value:
* Value, extracted from binkp header, or -1 if extracted message type is
* out of range
*/
int binkp_getmsgtype(char ch)
{
int val = ( ((unsigned char)ch) & 0x7f );
if( BPMSG_MIN > val || val > BPMSG_MAX )
return -1;
else
return val;
}
/*****************************************************************************
* Puts a message to the output messages queue. These msgs will be send
* right after the current data block
*
* Arguments:
* bpi binkp state structure
* msg message type
* s1 message text
* s2 message text (will be concatenated with s1)
*
* Return value:
* None
*/
void binkp_queuemsg(s_bpinfo *bpi, e_bpmsg msg, const char *s1, const char *s2)
{
bpi->msgqueue = xrealloc(bpi->msgqueue, sizeof(s_bpmsg)*(bpi->n_msgs + 1));
memset(&bpi->msgqueue[bpi->n_msgs], '\0', sizeof(s_bpmsg));
/* Set message type */
bpi->msgqueue[bpi->n_msgs].type = msg;
/* Set message data size (without frame header) */
bpi->msgqueue[bpi->n_msgs].size = 1;
if( s1 ) bpi->msgqueue[bpi->n_msgs].size += strlen(s1);
if( s2 ) bpi->msgqueue[bpi->n_msgs].size += strlen(s2);
/* Set message data */
bpi->msgqueue[bpi->n_msgs].data = xmalloc(bpi->msgqueue[bpi->n_msgs].size+3);
binkp_puthdr(bpi->msgqueue[bpi->n_msgs].data,
(unsigned)(bpi->msgqueue[bpi->n_msgs].size | 0x8000));
binkp_putmsgtype(bpi->msgqueue[bpi->n_msgs].data + 2, msg);
bpi->msgqueue[bpi->n_msgs].data[3] = '\0';
if( s1 ) strcat(bpi->msgqueue[bpi->n_msgs].data + 3, s1);
if( s2 ) strcat(bpi->msgqueue[bpi->n_msgs].data + 3, s2);
DEB((D_PROT, "binkp_queuemsg: queued message #%d, %ld bytes, \"%s\"",
(int)bpi->msgqueue[bpi->n_msgs].type,
(long)bpi->msgqueue[bpi->n_msgs].size,
(char*)bpi->msgqueue[bpi->n_msgs].data+3));
++bpi->n_msgs;
++bpi->msgs_in_batch;
}
/*****************************************************************************
* Sends a message using format string
*
* Arguments:
* bpi binkp state structure
* msg message type
* fmt pointer to the format string, (man printf)
*
* Return value:
* None
*/
void binkp_queuemsgf(s_bpinfo *bpi, e_bpmsg msg, const char *fmt, ...)
{
char msg_text[2048];
va_list ap;
va_start(ap, fmt);
#ifdef HAVE_SNPRINTF
vsnprintf(msg_text, sizeof(msg_text), fmt, ap);
#else
vsprintf(msg_text, fmt, ap);
#endif
va_end(ap);
binkp_queuemsg(bpi, msg, msg_text, NULL);
}
/***************************************************************************** /*****************************************************************************
* Parse most common message arguments, send error message to remote * Parse most common message arguments, send error message to remote
* if needed. * if needed.
* *
* Arguments: * Arguments:
* s pointer to the string to parse * str pointer to the string to parse
* fn pointer to the pointer for extracted file name * fn pointer to the pointer for extracted file name
* sz pointer to the extracted file size * sz pointer to the extracted file size
* tm pointer to the extracted file modification time * tm pointer to the extracted file modification time
@ -221,272 +35,21 @@ void binkp_queuemsgf(s_bpinfo *bpi, e_bpmsg msg, const char *fmt, ...)
* Return value: * Return value:
* non-zero value on error, zero on success * non-zero value on error, zero on success
*/ */
int binkp_parsfinfo(char *str,char **fn,size_t *sz,time_t *tm,size_t *offs){
char *n; int binkp_parsfinfo(const char *str, s_bpfinfo *fi, bool with_offset)
char *p_fname = NULL;
char *p_size = NULL;
char *p_time = NULL;
char *p_offs = NULL;
static char *s = NULL;
/* Attention, offs may be NULL! */
ASSERT(str != NULL && fn != NULL && sz != NULL && tm != NULL);
DEB((D_PROT, "binkp_parsemsg: want parse \"%s\"", s));
if (s) free (s);
s = xstrcpy (str);
p_fname = string_token(s, &n, NULL, 0);
p_size = string_token(NULL, &n, NULL, 0);
p_time = string_token(NULL, &n, NULL, 0);
if( offs ) p_offs = string_token(NULL, &n, NULL, 0);
if( p_fname && p_size && p_time && (!offs || p_offs) )
{
if( ISDEC(p_size) && ISDEC(p_time) &&
(!offs || ISDEC(p_offs) || !strcmp (p_offs, "-1")) )
{
(*fn) = p_fname;
(*sz) = atol(p_size);
(*tm) = atol(p_time);
if( offs ) *offs = atol(p_offs);
DEB((D_PROT, "binkp_parsemsg: file = \"%s\", size = %d, time = %d",
*fn, *sz, *tm));
return 0;
}
}
return 1;
}
/*****************************************************************************
* Put message to a buffer
*
* Arguments:
* bpi binkp state structure
* msg message
*
* Return value:
* Zero on success, and non-zero value if no more free space left
* in the buffer
*/
int binkp_buffer_message(s_bpinfo *bpi, s_bpmsg *msg)
{
DEB((D_PROT, "binkp_buffer_message: buffer message #%d, %ld byte(s)",
(int)msg->type, (long)msg->size));
/* Check for possible internal error */
if( msg->size > BINKP_MAX_BLKSIZE )
{
log("size of msg we want to send is too big (%db)", msg->size);
return 1;
}
/* Is there space for the new msg? */
if( bpi->opos + msg->size > BINKP_MAX_BLKSIZE ) return 1;
if( msg->data )
{
memcpy(bpi->obuf + bpi->opos, msg->data, msg->size + BINKP_BLK_HDRSIZE);
bpi->opos += msg->size + BINKP_BLK_HDRSIZE;
free(msg->data);
msg->data = NULL;
}
return 0;
}
/*****************************************************************************
* Send as much buffered data as possible (non-blocking)
*
* Arguments:
* bpi binkp state structure
*
* Return value:
* Zero on success, and non-zero value on errors
*/
int binkp_send_buffer(s_bpinfo *bpi)
{
int n;
DEB((D_PROT, "binkp_send_buffer: sending %ld byte(s)", (long)bpi->opos));
n = tty_write(bpi->optr, bpi->opos);
if( n >= bpi->opos )
{ bpi->optr = bpi->obuf; bpi->opos = 0; }
else if( n > 0 )
{ bpi->optr += n; bpi->opos -= n; }
else
return -1;
return 0;
}
/*****************************************************************************
* Send as much buffered data or messages from the queue as possible
*
* Arguments:
* bpi binkp state structure
*
* Return value:
* Zero on success, and non-zero value on errors
*/
int binkp_send(s_bpinfo *bpi)
{ {
int i; int r;
if( strlen(str)>PATH_MAX ) {
if( bpi->opos == 0 && bpi->msgqueue ) log("too long string, overflow may occur");
{ return -1;
/* }
* Buffer is empty and there are unsent msgs r = sscanf(str, with_offset? "%s %d %d %d": "%s %d %d", &fi->fn, &fi->sz, &fi->tm, &fi->offs);
*/ if (r==(with_offset? 4: 3)) {
for( i = 0; i < bpi->n_msgs; i++ ) return 0;
{ }
if( bpi->msgqueue[i].sent == FALSE ) else {
{ return -1;
if( binkp_buffer_message(bpi, &bpi->msgqueue[i]) ) }
break;
else
bpi->msgqueue[i].sent = TRUE;
}
}
/* If the message queue is empty, free it */
if( i >= bpi->n_msgs )
{
if( bpi->msgqueue )
{ free(bpi->msgqueue); bpi->msgqueue = NULL; }
bpi->n_msgs = 0;
}
}
return bpi->opos ? binkp_send_buffer(bpi) : 0;
}
/*****************************************************************************
* Flush all buffers and queues that can contain unsent data
*
* Arguments:
* bpi binkp state structure
* timeout abort flushing if this time will be exceeded
*
* Return value:
* Zero on success, and non-zero value on errors
*/
int binkp_flush_queue(s_bpinfo *bpi, int timeout)
{
bool send_ready = FALSE;
time_t flush_timer;
timer_set(&flush_timer, timeout);
while( bpi->opos > 0 || bpi->n_msgs > 0 )
{
if( timer_expired(flush_timer)
|| tty_select(NULL, &send_ready, 10) < 0 )
return -1;
if( send_ready && binkp_send(bpi) < 0 )
return -1;
}
return 0;
}
/*****************************************************************************
* Try to receive next message or data block in non-blocking mode
*
* Arguments:
* bpi binkp state structure
*
* Return value:
* One of the BPMSG_* values
*/
int binkp_recv(s_bpinfo *bpi)
{
int n = 0;
int size;
if( bpi->inew || bpi->isize == -1 )
{
bpi->inew = FALSE;
bpi->isize = -1;
size = BINKP_BLK_HDRSIZE;
}
else
size = bpi->isize;
if( size > 0 )
{
n = tty_read(bpi->ibuf + bpi->ipos, size - bpi->ipos);
if( n <= 0 ) return BPMSG_EXIT;
}
bpi->ipos += n;
if( bpi->ipos == size )
{
if( bpi->isize == -1 )
{
/* We got block header */
bpi->ipos = 0;
bpi->imsg = ((bpi->ibuf[0] >> 7) & 0377) ? TRUE : FALSE;
bpi->isize = binkp_gethdr(bpi->ibuf);
DEB((D_PROT, "binkp_recv: received header: %ld byte(s) (%s)",
(long)bpi->isize, bpi->imsg ? "msg" : "data"));
if( bpi->isize > BINKP_MAX_BLKSIZE )
{
log("internal error: got %ld bytes block size", bpi->isize);
return BPMSG_EXIT;
}
else if( bpi->isize == 0 )
{
bpi->ipos = 0;
bpi->inew = TRUE;
bpi->isize = -1;
if( bpi->imsg == TRUE )
log("zero length message from remote");
}
}
else /* We got whole data block/message */
{
if( bpi->imsg == TRUE ) /* Is it message? */
{
bpi->ipos = 0;
bpi->inew = TRUE;
bpi->ibuf[size] = '\0';
bpi->imsgtype = binkp_getmsgtype(bpi->ibuf[0]);
DEB((D_PROT, "binkp_recv: got message #%d, %ld byte(s), \"%s\"",
(int)bpi->imsgtype, bpi->isize, bpi->ibuf+1));
++bpi->msgs_in_batch;
if( bpi->imsgtype < 0 )
{
bpi->isize = -1;
log("got incorrect message type");
if( ++bpi->junkcount >= 5 )
{
log("junk count exceeded");
return BPMSG_EXIT;
}
return BPMSG_NONE;
}
return bpi->imsgtype;
}
else /* It is data */
{
bpi->ipos = 0;
bpi->inew = TRUE;
bpi->ibuf[size] = '\0';
DEB((D_PROT, "binkp_recv: got data block, %ld byte(s)",
(long)bpi->isize));
return BPMSG_DATA;
}
}
}
return BPMSG_NONE;
} }
/***************************************************************************** /*****************************************************************************
@ -499,6 +62,7 @@ int binkp_recv(s_bpinfo *bpi)
* Return value: * Return value:
* None * None
*/ */
/*
void binkp_queue_sysinfo(s_bpinfo *bpi, s_binkp_sysinfo *binkp) void binkp_queue_sysinfo(s_bpinfo *bpi, s_binkp_sysinfo *binkp)
{ {
int i; int i;
@ -549,6 +113,7 @@ void binkp_queue_sysinfo(s_bpinfo *bpi, s_binkp_sysinfo *binkp)
free (szOpt); free (szOpt);
} }
} }
*/
/***************************************************************************** /*****************************************************************************
* Write options to the log * Write options to the log
@ -574,6 +139,7 @@ void binkp_log_options(s_binkp_sysinfo *remote)
* Return value: * Return value:
* None * None
*/ */
void binkp_log_sysinfo(s_binkp_sysinfo *binkp) void binkp_log_sysinfo(s_binkp_sysinfo *binkp)
{ {
int i; int i;
@ -628,6 +194,7 @@ void binkp_log_sysinfo(s_binkp_sysinfo *binkp)
log(" Time : %s", string_printable(binkp->timestr)); log(" Time : %s", string_printable(binkp->timestr));
} }
/***************************************************************************** /*****************************************************************************
* Set our local system information * Set our local system information
* *
@ -727,6 +294,7 @@ void binkp_set_sysinfo(s_binkp_sysinfo *binkp, s_faddr *remote_addr, bool caller
} }
} }
void binkp_parse_options(s_binkp_sysinfo *binkp, char *options) void binkp_parse_options(s_binkp_sysinfo *binkp, char *options)
{ {
char *p, *n; char *p, *n;
@ -734,11 +302,9 @@ void binkp_parse_options(s_binkp_sysinfo *binkp, char *options)
for( p = string_token(options, &n, NULL, 0); p; for( p = string_token(options, &n, NULL, 0); p;
p = string_token(NULL, &n, NULL, 0) ) p = string_token(NULL, &n, NULL, 0) )
{ {
#ifndef NETSPOOL
if( !strcmp(p, "NR") ) { if( !strcmp(p, "NR") ) {
binkp->options |= BINKP_OPT_NR; binkp->options |= BINKP_OPT_NR;
} else } else
#endif
if( !strcmp(p, "MB") ) if( !strcmp(p, "MB") )
binkp->options |= BINKP_OPT_MB; binkp->options |= BINKP_OPT_MB;
else if( !strcmp(p, "MPWD") ) else if( !strcmp(p, "MPWD") )
@ -776,3 +342,4 @@ void binkp_parse_options(s_binkp_sysinfo *binkp, char *options)
} }
} }

@ -40,8 +40,10 @@ const char *Protocols[] =
* Return value: * Return value:
* Zero value on success and non-zero if nothing to send * Zero value on success and non-zero if nothing to send
*/ */
static int prot_get_next_file(s_filelist **dest, s_protinfo *pi)
static int prot_get_next_file(s_filelist **dest, s_protinfo *pi, s_filehint *hint)
{ {
log("prot_get_next_file %d", hint);
s_filelist *ptrl = NULL; s_filelist *ptrl = NULL;
s_filelist *best = NULL; s_filelist *best = NULL;
s_fsqueue *q = &state.queue; s_fsqueue *q = &state.queue;
@ -49,7 +51,9 @@ static int prot_get_next_file(s_filelist **dest, s_protinfo *pi)
*dest = NULL; *dest = NULL;
/* local queue */ /* local queue */
for( ptrl = q->fslist; ptrl; ptrl = ptrl->next ) for( ptrl = q->fslist; ptrl; ptrl = ptrl->next ) {
//log("scan %s", ptrl->fname);
if (hint) if (strcmp(hint->fn, ptrl->fname) !=0 || hint->sz != ptrl->size) continue;
if( ptrl->status == STATUS_WILLSEND ) if( ptrl->status == STATUS_WILLSEND )
{ {
if( pi->reqs_only ) if( pi->reqs_only )
@ -85,6 +89,8 @@ static int prot_get_next_file(s_filelist **dest, s_protinfo *pi)
else else
best = ptrl; best = ptrl;
} }
}
//log("scan1 done");
if( best ) if( best )
{ {
@ -92,16 +98,21 @@ static int prot_get_next_file(s_filelist **dest, s_protinfo *pi)
return 0; return 0;
} }
for( ptrl = pi->filelist; ptrl; ptrl = ptrl->next ) for( ptrl = pi->filelist; ptrl; ptrl = ptrl->next ) {
if (hint) if (strcmp(hint->fn, ptrl->fname) !=0 || hint->sz != ptrl->size) continue;
if( ptrl->status == STATUS_WILLSEND ) if( ptrl->status == STATUS_WILLSEND )
{ {
*dest = ptrl; *dest = ptrl;
return 0; return 0;
} }
}
//log("scan2 done");
/* network queue */ /* network queue */
#ifdef NETSPOOL #ifdef NETSPOOL
if (hint) return 1; // cannot choose
/*log("netspool next file");*/ /*log("netspool next file");*/
if(state.netspool.state == NS_NOTINIT) { if(state.netspool.state == NS_NOTINIT) {
/*log("new netspool connection");*/ /*log("new netspool connection");*/
@ -132,7 +143,7 @@ static int prot_get_next_file(s_filelist **dest, s_protinfo *pi)
} }
if(state.netspool.state == NS_RECEIVING) { if(state.netspool.state == NS_RECEIVING) {
/*log("netspool begin receive");*/ log("netspool begin receive");
netspool_receive(&state.netspool); netspool_receive(&state.netspool);
} else { } else {
log("netspool could not start receive"); log("netspool could not start receive");
@ -208,7 +219,7 @@ void prot_update_traffic(s_protinfo *pi, const s_fsqueue *q)
* Return value: * Return value:
* Zero value on success and non-zero if have nothing to send * Zero value on success and non-zero if have nothing to send
*/ */
int p_tx_fopen(s_protinfo *pi) int p_tx_fopen(s_protinfo *pi, s_filehint *hint)
{ {
FILE *fp; FILE *fp;
struct stat st; struct stat st;
@ -218,11 +229,14 @@ int p_tx_fopen(s_protinfo *pi)
return 1; return 1;
get_next_file: get_next_file:
if( prot_get_next_file(&ptrl, pi) ) if( prot_get_next_file(&ptrl, pi, hint) ) {
return 1; log("no next file");
return 1;
}
if( ptrl ) { if( ptrl ) {
log("sending local file");
/* Mark this file as "processed" */ /* Mark this file as "processed" */
ptrl->status = STATUS_SENDING; ptrl->status = STATUS_SENDING;
@ -261,12 +275,14 @@ get_next_file:
*/ */
if( pi->sentfiles && pi->n_sentfiles > 0 ) if( pi->sentfiles && pi->n_sentfiles > 0 )
{ {
log("adding file to sentfile");
pi->sentfiles = (s_finfo *)xrealloc(pi->sentfiles, sizeof(s_finfo)*(pi->n_sentfiles+1)); pi->sentfiles = (s_finfo *)xrealloc(pi->sentfiles, sizeof(s_finfo)*(pi->n_sentfiles+1));
memset(&pi->sentfiles[pi->n_sentfiles], '\0', sizeof(s_finfo)); memset(&pi->sentfiles[pi->n_sentfiles], '\0', sizeof(s_finfo));
pi->send = &pi->sentfiles[pi->n_sentfiles++]; pi->send = &pi->sentfiles[pi->n_sentfiles++];
} }
else else
{ {
log("adding file to new sentfile");
pi->sentfiles = (s_finfo *)xmalloc(sizeof(s_finfo)); pi->sentfiles = (s_finfo *)xmalloc(sizeof(s_finfo));
memset(pi->sentfiles, '\0', sizeof(s_finfo)); memset(pi->sentfiles, '\0', sizeof(s_finfo));
pi->send = pi->sentfiles; pi->send = pi->sentfiles;
@ -292,6 +308,8 @@ get_next_file:
pi->send->status = FSTAT_PROCESS; pi->send->status = FSTAT_PROCESS;
pi->send->action = ACTION_ACKNOWLEDGE; pi->send->action = ACTION_ACKNOWLEDGE;
pi->send->flodsc = -1; pi->send->flodsc = -1;
pi->send->waitack = true;
pi->send->netspool = true;
} else { } else {
#endif #endif
@ -310,7 +328,8 @@ get_next_file:
pi->send->type = ptrl->type; pi->send->type = ptrl->type;
pi->send->action = ptrl->action; pi->send->action = ptrl->action;
pi->send->flodsc = ptrl->flodsc; pi->send->flodsc = ptrl->flodsc;
pi->send->waitack = false;
pi->send->netspool = false;
#ifdef NETSPOOL #ifdef NETSPOOL
} }
#endif #endif
@ -332,6 +351,15 @@ get_next_file:
return 0; return 0;
} }
int p_tx_rewind(s_protinfo *pi, size_t pos)
{
if( !pi || !pi->send || !pi->send->fp) {
log("cannot rewind");
return -1;
}
return fseek(pi->send->fp, pos, SEEK_SET);
}
void prot_traffic_update(s_traffic *traf, size_t size, int xtime, int type) void prot_traffic_update(s_traffic *traf, size_t size, int xtime, int type)
{ {
if( type & TYPE_REQANSW ) if( type & TYPE_REQANSW )
@ -387,6 +415,11 @@ int p_tx_fclose(s_protinfo *pi)
long trans_time = 0; long trans_time = 0;
long cps = 0; long cps = 0;
if (!pi->send) {
log("already closed");
return -1;
}
if( pi->send->fp ) if( pi->send->fp )
{ {
(void)file_close(pi->send->fp); (void)file_close(pi->send->fp);
@ -462,6 +495,8 @@ int p_tx_fclose(s_protinfo *pi)
} }
} }
pi->send = NULL;
return 0; return 0;
} }
@ -478,14 +513,14 @@ int p_tx_fclose(s_protinfo *pi)
* -1 - error reading file (must try to send file later) * -1 - error reading file (must try to send file later)
* -2 - file must be skipped (send never) * -2 - file must be skipped (send never)
*/ */
int p_tx_readfile(char *buffer, size_t buflen, s_protinfo *pi) long int p_tx_readfile(char *buffer, size_t buflen, s_protinfo *pi)
{ {
int n; int n;
struct stat st; struct stat st;
long ftell_pos; long ftell_pos;
#ifdef NETSPOOL #ifdef NETSPOOL
if(pi->send->fname==NULL && strcmp(pi->send->local_name, "NETSPOOL")==0 ) { if (pi->send->netspool) {
/*log("reading netspool file");*/ /*log("reading netspool file");*/
if( state.netspool.state != NS_RECVFILE ) { if( state.netspool.state != NS_RECVFILE ) {
log("send: wrong netspool state"); log("send: wrong netspool state");
@ -575,7 +610,7 @@ int p_tx_readfile(char *buffer, size_t buflen, s_protinfo *pi)
* -1 - error writing file (receive later) * -1 - error writing file (receive later)
* -2 - file must be skipped (receive never) * -2 - file must be skipped (receive never)
*/ */
int p_rx_writefile(const char *buffer, size_t buflen, s_protinfo *pi) long int p_rx_writefile(const char *buffer, size_t buflen, s_protinfo *pi)
{ {
struct stat st; struct stat st;
long ftell_pos = ftell(pi->recv->fp); long ftell_pos = ftell(pi->recv->fp);
@ -918,7 +953,7 @@ int p_rx_fopen(s_protinfo *pi, char *fn, size_t sz, time_t tm, mode_t mode)
* directory, do you know who could left it here ? :) * directory, do you know who could left it here ? :)
*/ */
log("recv: allready have \"%s\"", pi->recv->fname); log("recv: already have \"%s\"", pi->recv->fname);
if( p_move2inbound(pi) == 0 ) if( p_move2inbound(pi) == 0 )
pi->recv->status = FSTAT_SKIPPED; pi->recv->status = FSTAT_SKIPPED;

@ -1327,7 +1327,7 @@ int hydra_batch(s_hydrainfo *hi, s_protinfo *pi)
if( pi->send && pi->send->fp ) if( pi->send && pi->send->fp )
p_tx_fclose(pi); p_tx_fclose(pi);
send_EOB = p_tx_fopen(pi) ? TRUE : FALSE; send_EOB = p_tx_fopen(pi, NULL) ? TRUE : FALSE;
txtries = 0; txtries = 0;
txstate = HTX_FINFO; txstate = HTX_FINFO;

@ -213,7 +213,7 @@ int tx_zmodem(s_protinfo *pi, bool caller)
if( pi->send && pi->send->fp ) if( pi->send && pi->send->fp )
p_tx_fclose(pi); p_tx_fclose(pi);
txtries = 0; txtries = 0;
txstate = p_tx_fopen(pi) ? ZTX_FIN : ZTX_FINFO; txstate = p_tx_fopen(pi, NULL) ? ZTX_FIN : ZTX_FINFO;
break; break;
case ZTX_FINFO: case ZTX_FINFO:

@ -132,7 +132,7 @@ int session_addrs_check(s_sysaddr *addrs, int anum, const char *passwd,
char pbuf[32]; char pbuf[32];
bool failure = FALSE; bool failure = FALSE;
bool success = FALSE; bool success = FALSE;
bool cram = (challenge && *challenge); bool cram = challenge;
if( !anum ) if( !anum )
return -1; return -1;
@ -156,11 +156,12 @@ int session_addrs_check(s_sysaddr *addrs, int anum, const char *passwd,
{ {
char digest_bin[16]; char digest_bin[16];
char digest_hex[33]; char digest_hex[33];
md5_cram_get(pbuf, challenge, challenge_length, digest_bin); md5_cram_get(pbuf, challenge, challenge_length, digest_bin);
/* Encode digest to the hex string */ /* Encode digest to the hex string */
string_bin_to_hex(digest_hex, digest_bin, 16); string_bin_to_hex(digest_hex, digest_bin, 16);
log("good password is %s", digest_hex);
if( strcasecmp(passwd, digest_hex) == 0 ) if( strcasecmp(passwd, digest_hex) == 0 )
good_passwd = TRUE; good_passwd = TRUE;
@ -185,7 +186,7 @@ int session_addrs_check(s_sysaddr *addrs, int anum, const char *passwd,
} }
} }
else else
/* not password protected address */ log("not password protected address");
addrs[i].good = TRUE; addrs[i].good = TRUE;
} }
@ -813,7 +814,7 @@ int session(void)
switch(state.handshake->protocol) { switch(state.handshake->protocol) {
case PROT_BINKP: case PROT_BINKP:
rc = binkp_transfer(&pi); rc = binkp_transfer((s_binkp_sysinfo *)state.handshake->local_data, (s_binkp_sysinfo *)state.handshake->remote_data, &pi);
break; break;
case PROT_ZMODEM: case PROT_ZMODEM:
case PROT_ZEDZAP: case PROT_ZEDZAP:

@ -154,7 +154,6 @@
/* /*
* Definition of new pretty types * Definition of new pretty types
*/ */
typedef char bool;
typedef unsigned long UINT32; typedef unsigned long UINT32;
typedef unsigned short UINT16; typedef unsigned short UINT16;
typedef unsigned char UINT8; typedef unsigned char UINT8;

@ -16,6 +16,7 @@
#include "config.h" #include "config.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>

@ -54,6 +54,16 @@
#define BINKP_OPT_SHA1 0x10 /* CRAM-SHA1 authentication */ #define BINKP_OPT_SHA1 0x10 /* CRAM-SHA1 authentication */
#define BINKP_OPT_DES 0x20 /* CRAM-DES authentication */ #define BINKP_OPT_DES 0x20 /* CRAM-DES authentication */
typedef enum binkp_mode {
bmode_failoff,
bmode_incoming_handshake,
bmode_outgoing_handshake,
bmode_transfer,
bmode_complete
} e_binkp_mode;
typedef struct { typedef struct {
s_sysaddr *addrs; s_sysaddr *addrs;
int anum; int anum;
@ -79,9 +89,9 @@ typedef enum {
/* /*
* Pseudo message types, returned by binkp_recv() * Pseudo message types, returned by binkp_recv()
*/ */
BPMSG_EXIT = -3, /* Terminate session */ // BPMSG_EXIT = -3, /* Terminate session */
BPMSG_NONE = -2, /* Got nothing intresting */ // BPMSG_NONE = -2, /* Got nothing intresting */
BPMSG_DATA = -1, /* Got data block */ // BPMSG_DATA = -1, /* Got data block */
/* /*
* Real BinkP message types * Real BinkP message types
*/ */
@ -101,62 +111,70 @@ typedef enum {
} e_bpmsg; } e_bpmsg;
typedef struct { //typedef struct {
bool sent; /* Sent message (queued) */ // bool sent; /* Sent message (queued) */
e_bpmsg type; /* Message type */ // e_bpmsg type; /* Message type */
int size; /* Message size (length of text) */ // int size; /* Message size (length of text) */
char *data; /* Message text */ // char *data; /* Message text */
} s_bpmsg; //} s_bpmsg;
//typedef struct {
// char *obuf; /* Output buffer */
// int opos; /* Unsent bytes left in buffer */
// char *optr; /* Send data from this position */
//
// char *ibuf; /* Our input buffer */
// int ipos;
// int isize;
//// bool imsg; /* Is it message? */
/// bool inew;
// e_bpmsg imsgtype; /* Message type */
//
// int junkcount;
// s_bpmsg *msgqueue; /* Outgoing messages queue */
// int n_msgs; /* Number of messages in queue */
// int msgs_in_batch; /* Number of messages in batch */
// int timeout;
//} s_bpinfo;
typedef struct { typedef struct {
char *obuf; /* Output buffer */ char fn[PATH_MAX];
int opos; /* Unsent bytes left in buffer */ size_t sz;
char *optr; /* Send data from this position */ time_t tm;
size_t offs;
char *ibuf; /* Our input buffer */ } s_bpfinfo;
int ipos;
int isize;
bool imsg; /* Is it message? */
bool inew;
e_bpmsg imsgtype; /* Message type */
int junkcount;
s_bpmsg *msgqueue; /* Outgoing messages queue */
int n_msgs; /* Number of messages in queue */
int msgs_in_batch; /* Number of messages in batch */
int timeout;
} s_bpinfo;
/* prot_binkp.c */ /* prot_binkp.c */
int binkp_outgoing(s_binkp_sysinfo *local_data, s_binkp_sysinfo *remote_data); int binkp_outgoing(s_binkp_sysinfo *local_data, s_binkp_sysinfo *remote_data);
int binkp_incoming(s_binkp_sysinfo *local_data, s_binkp_sysinfo *remote_data); int binkp_incoming(s_binkp_sysinfo *local_data, s_binkp_sysinfo *remote_data);
int binkp_transfer(s_protinfo *pi); int binkp_transfer(s_binkp_sysinfo *local_data, s_binkp_sysinfo *remote_data, s_protinfo *pi);
/* prot_binkp_misc.c */ /* prot_binkp_misc.c */
void binkp_init_bpinfo(s_bpinfo *bpi); //void binkp_init_bpinfo(s_bpinfo *bpi);
void binkp_deinit_bpinfo(s_bpinfo *bpi); //void binkp_deinit_bpinfo(s_bpinfo *bpi);
int binkp_gethdr(const char *s); int binkp_gethdr(const char *s);
int binkp_getmsgtype(char ch); int binkp_getmsgtype(char ch);
void binkp_puthdr(char *s, unsigned int u); void binkp_puthdr(char *s, unsigned int u);
void binkp_putmsgtype(char *s, e_bpmsg msg); void binkp_putmsgtype(char *s, e_bpmsg msg);
void binkp_queuemsg(s_bpinfo *bpi, e_bpmsg msg, const char *s1, const char *s2); //void binkp_queuemsg(s_bpinfo *bpi, e_bpmsg msg, const char *s1, const char *s2);
void binkp_queuemsgf(s_bpinfo *bpi, e_bpmsg msg, const char *fmt, ...); //void binkp_queuemsgf(s_bpinfo *bpi, e_bpmsg msg, const char *fmt, ...);
int binkp_parsfinfo(char *s, char **fn, size_t *sz, time_t *tm, size_t *offs); int binkp_parsfinfo(const char *s, s_bpfinfo *fi, bool with_offset);
int binkp_buffer_message(s_bpinfo *bpi, s_bpmsg *msg); //int binkp_buffer_message(s_bpinfo *bpi, s_bpmsg *msg);
int binkp_send_buffer(s_bpinfo *bpi); //int binkp_send_buffer(s_bpinfo *bpi);
int binkp_send(s_bpinfo *bpi); //int binkp_send(s_bpinfo *bpi);
int binkp_flush_queue(s_bpinfo *bpi, int timeout); //int binkp_flush_queue(s_bpinfo *bpi, int timeout);
int binkp_recv(s_bpinfo *bpi); //int binkp_recv(s_bpinfo *bpi);
void binkp_update_sysinfo(s_binkp_sysinfo *binkp); void binkp_update_sysinfo(s_binkp_sysinfo *binkp);
void binkp_log_options(s_binkp_sysinfo *remote); void binkp_log_options(s_binkp_sysinfo *remote);
void binkp_log_sysinfo(s_binkp_sysinfo *binkp); void binkp_log_sysinfo(s_binkp_sysinfo *binkp);
void binkp_queue_sysinfo(s_bpinfo *bpi, s_binkp_sysinfo *binkp); //void binkp_queue_sysinfo(s_bpinfo *bpi, s_binkp_sysinfo *binkp);
void binkp_set_sysinfo(s_binkp_sysinfo *binkp, s_faddr *remote_addr, bool caller); void binkp_set_sysinfo(s_binkp_sysinfo *binkp, s_faddr *remote_addr, bool caller);
void binkp_parse_options(s_binkp_sysinfo *binkp, char *options); void binkp_parse_options(s_binkp_sysinfo *binkp, char *options);
/* prot_binkp_api.c */ /* prot_binkp_api.c */
extern s_handshake_protocol handshake_protocol_binkp; extern s_handshake_protocol handshake_protocol_binkp;
#endif /* _P_BINKP_H_ */ #endif /* _P_BINKP_H_ */

@ -50,22 +50,24 @@ typedef struct traffic {
* Information on currently receiveing/transmitting file * Information on currently receiveing/transmitting file
*/ */
typedef struct finfo { typedef struct finfo {
FILE *fp; /* File descriptor of current file */ FILE *fp; /* File descriptor of current file */
char *local_name; /* Local file name that is used at our side */ char *local_name; /* Local file name that is used at our side */
char *net_name; /* File name as it known to the remote side */ char *net_name; /* File name as it known to the remote side */
char *fname; /* Local file name with full path */ char *fname; /* Local file name with full path */
time_t start_time; /* Start of current file rx/tx transactions */ time_t start_time; /* Start of current file rx/tx transactions */
time_t mod_time; /* File last modification time */ time_t mod_time; /* File last modification time */
mode_t mode; mode_t mode;
size_t bytes_total; /* File size */ size_t bytes_total; /* File size */
size_t bytes_sent; size_t bytes_sent;
size_t bytes_received; size_t bytes_received;
size_t bytes_skipped; /* crash recovery */ size_t bytes_skipped; /* crash recovery */
int eofseen; /* end of file seen */ int eofseen; /* end of file seen */
int status; int status;
int action; int action;
int type; int type;
int flodsc; /* Index number in flo files table */ int flodsc; /* Index number in flo files table */
bool waitack;
bool netspool;
} s_finfo; } s_finfo;
/* /*
@ -113,10 +115,17 @@ typedef struct protinfo {
extern const char *Protocols[]; /* Protocol names */ extern const char *Protocols[]; /* Protocol names */
int p_tx_fopen(s_protinfo *pi); typedef struct {
char *fn;
size_t sz;
time_t tm;
} s_filehint;
int p_tx_fopen(s_protinfo *pi, s_filehint *hint);
int p_tx_fclose(s_protinfo *pi); int p_tx_fclose(s_protinfo *pi);
int p_tx_readfile(char *buf, size_t sz, s_protinfo *pi); int p_tx_rewind(s_protinfo *pi, size_t pos);
int p_rx_writefile(const char *buf, size_t sz, s_protinfo *pi); long int p_tx_readfile(char *buf, size_t sz, s_protinfo *pi);
long int p_rx_writefile(const char *buf, size_t sz, s_protinfo *pi);
int p_compfinfo(s_finfo *finf, const char *fn, size_t sz, time_t tm); int p_compfinfo(s_finfo *finf, const char *fn, size_t sz, time_t tm);
int p_rx_fopen(s_protinfo *pi, char *f_name, size_t f_size, time_t f_modtime, mode_t f_mode); int p_rx_fopen(s_protinfo *pi, char *f_name, size_t f_size, time_t f_modtime, mode_t f_mode);
int p_rx_fclose(s_protinfo *pi); int p_rx_fclose(s_protinfo *pi);

@ -11,4 +11,6 @@ CC=$PREFIX-gcc
export PATH CC CPP export PATH CC CPP
#make clean
#./configure --prefix=/opt/bforce --host=mips-openwrt-linux --disable-syslog --enable-netspool #./configure --prefix=/opt/bforce --host=mips-openwrt-linux --disable-syslog --enable-netspool
make && scp bin/bforce root@gw-home:/opt/bforce/bin

Loading…
Cancel
Save