Skip to content
Snippets Groups Projects
Commit 305f12ff authored by rswindell's avatar rswindell
Browse files

Allow a maximum receive file size to be specified in sexyz.ini file and on

command-line (via -m<size> option). Default maximum is 0 (unlimited).
parent 6d908c90
No related branches found
No related tags found
No related merge requests found
......@@ -77,14 +77,15 @@
#define SINGLE_THREADED FALSE
#define MIN_OUTBUF_SIZE 1024
#define MAX_OUTBUF_SIZE (64*1024)
#define INBUF_SIZE (64*1024)
#define INBUF_SIZE (64*1024)
#define MAX_FILE_SIZE 0 /* Default value for max recv file size, 0 = unlimited*/
/***************/
/* Global Vars */
/***************/
long mode=0; /* Program mode */
long zmode=0L; /* Zmodem mode */
uchar block[XMODEM_MAX_BLOCK_SIZE]; /* Block buffer */
uchar block[XMODEM_MAX_BLOCK_SIZE]; /* Block buffer */
ulong block_num; /* Block number */
char* dszlog;
BOOL dszlog_path=TRUE; /* Log complete path to filename */
......@@ -92,6 +93,7 @@ BOOL dszlog_short=FALSE; /* Log Micros~1 short filename */
BOOL dszlog_quotes=FALSE; /* Quote filenames in DSZLOG */
int log_level=LOG_INFO;
BOOL use_syslog=FALSE;
ulong max_file_size=MAX_FILE_SIZE;
xmodem_t xm;
zmodem_t zm;
......@@ -329,8 +331,8 @@ void dump(BYTE* buf, int len)
int sock_sendbuf(SOCKET s, void *buf, size_t buflen)
{
int sent=0;
int ret;
size_t sent=0;
int ret;
fd_set socket_set;
for(;;) {
......@@ -1067,11 +1069,11 @@ static int receive_files(char** fname_list, int fnames)
lprintf(LOG_WARNING,"Throwing out received: %s",chr((uchar)i));
while(is_connected(NULL)) {
file_bytes=0x7fffffff;
if(mode&XMODEM) {
SAFECOPY(str,fname_list[0]); /* we'll have at least one fname */
file_bytes=file_bytes_left=0x7fffffff;
file_bytes_left=file_bytes;
}
else {
if(mode&YMODEM) {
lprintf(LOG_INFO,"Fetching YMODEM header block");
......@@ -1095,7 +1097,7 @@ static int receive_files(char** fname_list, int fnames)
lprintf(LOG_INFO,"Received YMODEM termination block");
return(0);
}
file_bytes=ftime=total_files=total_bytes=0;
ftime=total_files=total_bytes=0;
i=sscanf(block+strlen(block)+1,"%lu %lo %lo %lo %u %lu"
,&file_bytes /* file size (decimal) */
,&ftime /* file time (octal unix format) */
......@@ -1131,9 +1133,6 @@ static int receive_files(char** fname_list, int fnames)
return(-1);
}
}
if(!file_bytes)
file_bytes=0x7fffffff; /* Should we use 0xffffffff instead? */
file_bytes_left=file_bytes;
if(!total_files)
total_files=fnames-fnum;
......@@ -1192,6 +1191,17 @@ static int receive_files(char** fname_list, int fnames)
xmodem_cancel(&xm);
return(1);
}
if(!(mode&XMODEM) && max_file_size!=0 && file_bytes > max_file_size) {
lprintf(LOG_WARNING,"%s file size (%u) exceeds specified maximum: %u bytes", str, file_bytes, max_file_size);
if(mode&ZMODEM) {
zmodem_send_zskip(&zm);
continue;
}
xmodem_cancel(&xm);
return(1);
}
if((fp=fnopen(NULL,str,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY))==NULL
&& (fp=fopen(str,"wb"))==NULL) {
lprintf(LOG_ERR,"Error %d creating %s",errno,str);
......@@ -1229,7 +1239,13 @@ static int receive_files(char** fname_list, int fnames)
block_num=1;
xmodem_put_nak(&xm, block_num);
while(is_connected(NULL)) {
xmodem_progress(NULL,block_num,ftell(fp),file_bytes,startfile);
ulong pos=ftell(fp);
if(max_file_size!=0 && pos>=max_file_size) {
lprintf(LOG_WARNING,"Specified maximum file size (%lu bytes) reached at offset %lu"
,max_file_size, pos);
break;
}
xmodem_progress(NULL,block_num,pos,file_bytes,startfile);
i=xmodem_get_block(&xm, block, block_num);
if(i!=SUCCESS) {
......@@ -1280,14 +1296,18 @@ static int receive_files(char** fname_list, int fnames)
}
/* Use correct file size */
fflush(fp);
if(file_bytes < (ulong)filelength(fileno(fp))) {
lprintf(LOG_INFO,"Truncating file to %lu bytes", file_bytes);
chsize(fileno(fp),file_bytes);
} else
file_bytes = filelength(fileno(fp));
if(mode&ZMODEM)
file_bytes = zm.current_file_size; /* file can grow in transit */
else {
fflush(fp);
if(file_bytes < (ulong)filelength(fileno(fp))) {
lprintf(LOG_INFO,"Truncating file to %lu bytes", file_bytes);
chsize(fileno(fp),file_bytes);
} else
file_bytes = filelength(fileno(fp));
}
fclose(fp);
t=time(NULL)-startfile;
if(!t) t=1;
if(zm.file_skipped)
......@@ -1373,6 +1393,7 @@ static const char* usage=
" -2 set maximum Zmodem block size to 2K\n"
" -4 set maximum Zmodem block size to 4K\n"
" -8 set maximum Zmodem block size to 8K (ZedZap)\n"
" -m# set maximum receive file size to # bytes (0=unlimited, default=%u)\n"
" -! to pause after abnormal exit (error)\n"
" -telnet to enable Telnet mode (the default)\n"
" -rlogin or -ssh or -raw to disable Telnet mode\n"
......@@ -1460,6 +1481,7 @@ int main(int argc, char **argv)
outbuf_size =iniReadInteger(fp,ROOT_SECTION,"OutbufSize",16*1024);
progress_interval =iniReadInteger(fp,ROOT_SECTION,"ProgressInterval",1);
max_file_size =iniReadLongInt(fp,ROOT_SECTION,"MaxFileSize",MAX_FILE_SIZE);
if(iniReadBool(fp,ROOT_SECTION,"Debug",FALSE))
log_level=LOG_DEBUG;
......@@ -1482,7 +1504,7 @@ int main(int argc, char **argv)
zm.recv_timeout =iniReadInteger(fp,"Zmodem","RecvTimeout",zm.recv_timeout); /* seconds */
zm.crc_timeout =iniReadInteger(fp,"Zmodem","CrcTimeout",zm.crc_timeout); /* seconds */
zm.block_size =iniReadInteger(fp,"Zmodem","BlockSize",zm.block_size); /* 1024 */
zm.max_block_size =iniReadInteger(fp,"Zmodem","MaxBlockSize",zm.max_block_size); /* 1024 or 8192 */
zm.max_block_size =iniReadInteger(fp,"Zmodem","MaxBlockSize", zm.max_block_size); /* 1024 or 8192 */
zm.max_errors =iniReadInteger(fp,"Zmodem","MaxErrors",zm.max_errors);
zm.recv_bufsize =iniReadInteger(fp,"Zmodem","RecvBufSize",0);
zm.no_streaming =!iniReadBool(fp,"Zmodem","Streaming",TRUE);
......@@ -1567,7 +1589,7 @@ int main(int argc, char **argv)
break;
default:
fprintf(statfp,"Unrecognized command '%s'\n\n",argv[i]);
fprintf(statfp,usage);
fprintf(statfp,usage,MAX_FILE_SIZE);
bail(1);
}
continue;
......@@ -1641,6 +1663,9 @@ int main(int argc, char **argv)
case '!':
pause_on_abend=TRUE;
break;
case 'M': /* MaxFileSize */
max_file_size=strtoul(arg++,NULL,0);
break;
}
}
}
......@@ -1687,6 +1712,8 @@ int main(int argc, char **argv)
if(!telnet)
zm.escape_telnet_iac = FALSE;
zm.max_file_size = max_file_size;
if(sock==INVALID_SOCKET || sock<1) {
#ifdef __unix__
if(STDOUT_FILENO > STDIN_FILENO)
......@@ -1699,7 +1726,7 @@ int main(int argc, char **argv)
telnet=FALSE;
#else
fprintf(statfp,"!No socket descriptor specified\n\n");
fprintf(errfp,usage);
fprintf(errfp,usage,MAX_FILE_SIZE);
bail(1);
#endif
}
......@@ -1710,13 +1737,13 @@ int main(int argc, char **argv)
if(!(mode&(SEND|RECV))) {
fprintf(statfp,"!No command specified\n\n");
fprintf(statfp,usage);
fprintf(statfp,usage,MAX_FILE_SIZE);
bail(1);
}
if(mode&(SEND|XMODEM) && !fnames) { /* Sending with any or recv w/Xmodem */
fprintf(statfp,"!Must specify filename or filelist\n\n");
fprintf(statfp,usage);
fprintf(statfp,usage,MAX_FILE_SIZE);
bail(1);
}
......
......@@ -2121,6 +2121,12 @@ unsigned zmodem_recv_file_data(zmodem_t* zm, FILE* fp, uint32_t offset)
if((pos=ftell(fp)) > zm->current_file_size)
zm->current_file_size = pos;
if(zm->max_file_size!=0 && pos >= zm->max_file_size) {
lprintf(zm,LOG_WARNING,"Specified maximum file size (%lu bytes) reached at offset %lu"
,zm->max_file_size, pos);
break;
}
if(type!=ENDOFFRAME)
zmodem_send_pos_header(zm, ZRPOS, ftell(fp), /* Hex? */ TRUE);
......
......@@ -271,6 +271,7 @@ typedef struct {
unsigned max_errors;
unsigned block_size;
unsigned max_block_size;
uint32_t max_file_size; /* 0 = unlimited */
/* Callbacks */
void* cbdata;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment