📄 dcpxfer.c
字号:
xfer_stream = NULL;
return XFER_LOST;
}
if (!pktgetstr((char *)databuf)) /* Receive CY or CN */
return XFER_LOST; /* Bomb the connection if no packet */
if ((*databuf != 'C') || ((databuf[1] != 'N') && (databuf[1] != 'Y')))
{
printmsg(0,"Invalid response from remote: %s",
( char *) databuf);
return XFER_ABORT;
}
if (!equaln((char *) databuf, "CY", 2))
printmsg(0,"seof: Host was unable to save file after transmission");
/*--------------------------------------------------------------------*/
/* If local spool file, delete it */
/*--------------------------------------------------------------------*/
if (purge_file && !equal(dName,"D.0"))
{
char hostName[FILENAME_MAX];
importpath(hostName, dName, rmtname);
unlink( hostName );
printmsg(4,"seof: Deleted file %s (%s)", dName, hostName );
} /* if (purge_file && !equal(dName,"D.0")) */
/*--------------------------------------------------------------------*/
/* Update stats */
/*--------------------------------------------------------------------*/
remote_stats.fsent++;
remote_stats.bsent += bytes;
if (bflag[F_SYSLOG] || (debuglevel > 2 ))
{
ftime(&now);
ticks = (now.time - startTime.time) * 1000 +
((long) now.millitm - (long) startTime.millitm);
printmsg(2, "Transfer completed, %ld chars/sec",
(long) ((bytes * 1000) / (ticks ? ticks : 1) ));
if (bflag[F_SYSLOG])
{
tmx = localtime(&now.time);
if ( bflag[F_MULTITASK] )
syslog = FOPEN(SYSLOG, "a",TEXT_MODE);
if ( syslog == NULL )
printerr(SYSLOG);
#ifndef _Windows
else if ((bflag[F_MULTITASK] && setvbuf( syslog, NULL, _IONBF, 0)))
printerr(SYSLOG);
#endif
else {
fprintf( syslog,
"%s!%s %c %s (%d/%d-%02d:%02d:%02d) -> %ld"
" / %ld.%02d secs\n",
hostp->via,
userid,
type,
lName,
(tmx->tm_mon+1), tmx->tm_mday,
tmx->tm_hour, tmx->tm_min, tmx->tm_sec, bytes,
ticks / 1000 , (int) ((ticks % 1000) / 10) );
if ( bflag[F_MULTITASK] )
{
fclose( syslog );
syslog = NULL;
}
}
} /* if (bflag[F_SYSLOG]) */
} /* if (bflag[F_SYSLOG] || (debuglevel > 2 )) */
/*--------------------------------------------------------------------*/
/* Return success to caller */
/*--------------------------------------------------------------------*/
return XFER_FILEDONE; /* go get the next file to process */
} /*seof*/
/*--------------------------------------------------------------------*/
/* n e w r e q u e s t */
/* */
/* Determine the next request to be sent to other host */
/*--------------------------------------------------------------------*/
XFER_STATE newrequest( void )
{
int i;
/*--------------------------------------------------------------------*/
/* Verify we have no work in progress */
/*--------------------------------------------------------------------*/
if (!(xfer_stream == NULL))
return XFER_ABORT; /* Something is already being
transferred; we're in trouble! */
/*--------------------------------------------------------------------*/
/* Look for work in the current call file; if we do not find */
/* any, the job is complete and we can delete all the files we */
/* worked on in the file */
/*--------------------------------------------------------------------*/
if (fgets(databuf, xferBufSize, fwork) == nil(char)) /* More data? */
{ /* No --> clean up list of files */
printmsg(3, "newrequest: EOF for workfile %s",workfile);
fclose(fwork);
fwork = nil(FILE);
unlink(workfile); /* Delete completed call file */
return XFER_NEXTJOB; /* Get next C.* file to process */
} /* if (fgets(databuf, xferBufSize, fwork) == nil(char)) */
/*--------------------------------------------------------------------*/
/* We have a new request to process */
/*--------------------------------------------------------------------*/
i = strlen(databuf) - 1;
printmsg(3, "newrequest: got command from %s",workfile);
if (databuf[i] == '\n') /* remove new_line from card */
databuf[i] = '\0';
*cmdopts = *dName = '\0';
sscanf(databuf, "%c %s %s %s %s %s",
&type, fName, tName, spolName, cmdopts, dName);
if ( !strlen( dName ))
strcpy( dName, "D.0");
spolName[ sizeof userid - 1] = '\0';
strcpy( userid, spolName );
/*--------------------------------------------------------------------*/
/* Reset counters */
/*--------------------------------------------------------------------*/
bytes = 0;
ftime(&startTime);
(*filepkt)(); /* Init for file transfer */
/*--------------------------------------------------------------------*/
/* Process the command according to its type */
/*--------------------------------------------------------------------*/
switch( type )
{
case 'R':
return XFER_GETFILE;
case 'S':
return XFER_PUTFILE;
default:
return XFER_FILEDONE; /* Ignore the line */
} /* switch */
} /* newrequest */
/*--------------------------------------------------------------------*/
/* s s f i l e */
/* */
/* Send File Header for file to be sent */
/*--------------------------------------------------------------------*/
XFER_STATE ssfile( void )
{
char hostFile[FILENAME_MAX];
char *fileName;
/*--------------------------------------------------------------------*/
/* Convert the file name to our local name */
/*--------------------------------------------------------------------*/
if (equal(dName, "D.0")) /* Is there a spool file? */
{
fileName = fName; /* No --> Use the real name */
strcpy( hostFile, fileName );
}
else {
fileName = dName; /* Yes --> Use it */
importpath(hostFile, fileName, rmtname); /* And map to local */
}
lName = fName; // Always log the real name
/*--------------------------------------------------------------------*/
/* Try to open the file; if we fail, we just continue, because we */
/* may have sent the file on a previous call which failed part */
/* way through this job */
/*--------------------------------------------------------------------*/
xfer_stream = FOPEN( hostFile, "r", BINARY_MODE);
/* Open stream to send */
if (xfer_stream == NULL)
{
printmsg(0, "ssfile: Cannot open file %s (%s).", fileName, hostFile);
printerr(hostFile);
return XFER_FILEDONE; /* Try next file in this job */
} /* if */
/*--------------------------------------------------------------------*/
/* The file is open, now set its buffering */
/*--------------------------------------------------------------------*/
#ifndef _Windows // Leave file buffered under Windows
if (setvbuf( xfer_stream, NULL, _IONBF, 0))
{
printmsg(0, "ssfile: Cannot unbuffer file %s (%s).",
fileName, hostFile);
printerr(hostFile);
fclose(xfer_stream);
xfer_stream = NULL;
return XFER_ABORT; /* Clearly not our day; quit */
} /* if */
#endif
/*--------------------------------------------------------------------*/
/* Okay, we have a file to process; offer it to the other host */
/*--------------------------------------------------------------------*/
printmsg( equal(fName,dName) ? 2 : 0,
"Sending \"%s\" (%s) as \"%s\"", fName, hostFile, tName);
if (!pktsendstr( databuf )) /* Tell them what is coming at them */
{
fclose(xfer_stream);
xfer_stream = NULL;
return XFER_LOST;
}
if (!pktgetstr((char *)databuf))
{
fclose(xfer_stream);
xfer_stream = NULL;
return XFER_LOST;
}
if ((*databuf != 'S') || ((databuf[1] != 'N') && (databuf[1] != 'Y')))
{
printmsg(0,"Invalid response from remote: %s",databuf);
fclose(xfer_stream);
xfer_stream = NULL;
return XFER_ABORT;
}
if (databuf[1] != 'Y') /* Otherwise reject file transfer? */
{ /* Yes --> Look for next file */
printmsg(0, "ssfile: Remote host rejected file %s, reason %s",
tName,
databuf[2] ? (char *) &databuf[2] : "unknown" );
fclose( xfer_stream );
xfer_stream = NULL;
return XFER_FILEDONE;
}
return XFER_SENDDATA; /* Enter data transmission mode */
} /*ssfile*/
/*--------------------------------------------------------------------*/
/* s r f i l e */
/* */
/* Send File Header for file to be received */
/*--------------------------------------------------------------------*/
XFER_STATE srfile( void )
{
struct stat statbuf;
/*--------------------------------------------------------------------*/
/* Convert the filename to our local name */
/*--------------------------------------------------------------------*/
strcpy( spolName, normalize(tName));
// Assume the local user can type
/*--------------------------------------------------------------------*/
/* If the destination is a directory, put the originating */
/* original file name at the end of the path */
/*--------------------------------------------------------------------*/
if ((spolName[strlen(spolName) - 1] == '/') ||
((stat(spolName , &statbuf) == 0) && (statbuf.st_mode & S_IFDIR)))
{
char *slash = strrchr( fName, '/');
if ( slash == NULL )
slash = fName;
else
slash ++ ;
printmsg(3, "srfile: Destination \"%s\" is directory, \
appending file name \"%s\"", spolName, slash);
if (spolName[strlen(spolName) - 1] != '/')
strcat(spolName, "/");
strcat( spolName, slash );
} /* if */
printmsg(0, "Receiving \"%s\" as \"%s\" (%s)", fName, tName, spolName);
if (!pktsendstr( databuf ))
return XFER_LOST;
if (!pktgetstr((char *)databuf))
return XFER_LOST;
if ((*databuf != 'R') || ((databuf[1] != 'N') && (databuf[1] != 'Y')))
{
printmsg(0,"Invalid response from remote: %s",
databuf);
return XFER_ABORT;
}
if (databuf[1] != 'Y') /* Otherwise reject file transfer? */
{ /* Yes --> Look for next file */
printmsg(0, "srfile: Remote host denied access to file %s, reason %s",
fName, databuf[2] ? (char *) &databuf[2] : "unknown" );
return XFER_FILEDONE;
}
/*--------------------------------------------------------------------*/
/* We should verify the directory exists if the user doesn't */
/* specify the -d option, but I've got enough problems this */
/* week; we'll just auto-create using FOPEN() */
/*--------------------------------------------------------------------*/
xfer_stream = FOPEN(spolName, "w", BINARY_MODE);
/* Allow auto-create of directory */
if (xfer_stream == NULL)
{
printmsg(0, "srfile: cannot create %s", spolName);
printerr(spolName);
return XFER_ABORT;
}
/*--------------------------------------------------------------------*/
/* Set buffering for the file */
/*--------------------------------------------------------------------*/
#ifndef _Windows // Leave file buffered under Windows
if (setvbuf( xfer_stream, NULL, _IONBF, 0))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -