📄 ftplib.c
字号:
FTPLDEBUG ("ftpDataConnInit: error - reply was %d(decimal) - not FTP_COMPLETE or FTP_PRELIM.\n",\ FTPL_DEBUG_ERRORS,result,1,2,3,4,5); close (dataSock); return (ERROR); } else { return (dataSock); } } /* second try - try to get port # correct by default */ close (dataSock); dataSock = socket (AF_INET, SOCK_STREAM, 0); if (dataSock < 0) { FTPLDEBUG ("ftpDataConnInit: error - 2nd try to get port number failed on socket(). errno:0x%08x\n",\ FTPL_DEBUG_ERRORS,errno,1,2,3,4,5); return (ERROR); } optval = 1; if ((setsockopt (dataSock, SOL_SOCKET, SO_REUSEADDR, (caddr_t) &optval, sizeof (optval)) < 0) || (bind (dataSock, (struct sockaddr *)&ctrlAddr, sizeof (ctrlAddr)) < 0)) { FTPLDEBUG ("ftpDataConnInit: error - bind failed on 2nd attempt errno :0x%08x\n", \ FTPL_DEBUG_ERRORS, errno,1,2,3,4,5); close (dataSock); return (ERROR); } if (listen (dataSock, 1) < 0) { FTPLDEBUG ("ftpDataConnInit: error - listen failed on 2nd attempt errno :0x%08x\n", \ FTPL_DEBUG_ERRORS,errno,1,2,3,4,5); close (dataSock); return (ERROR); } return (dataSock); }/********************************************************************************* ftpDataConnGet - get a completed FTP data connection** This routine completes a data connection initiated by a call to* ftpDataConnInit(). It waits for a connection on the specified socket from* the remote FTP server. The specified socket should be the one returned by* ftpDataConnInit(). The connection is established on a new socket, whose* file descriptor is returned as the result of this function. The original * socket, specified in the argument to this routine, is closed.** Usually this routine is called after ftpDataConnInit() and ftpCommand() to* initiate a data transfer from/to the remote FTP server.** RETURNS:* The file descriptor of the new data socket, or ERROR if the connection* failed.** SEE ALSO: ftpDataConnInit(), ftpCommand()*/int ftpDataConnGet ( int dataSock /* fd of data socket on which to await connection */ ) { int newDataSock; SOCKADDR_IN from; int fromlen = sizeof (from); newDataSock = accept (dataSock, (struct sockaddr *) &from, &fromlen); close (dataSock); return (newDataSock); }/********************************************************************************* ftpLs - list directory contents via FTP** This routine lists the contents of a directory. The content list* is obtained via an NLST FTP transaction.** The local device name must be the same as the remote host name* with a colon ":" as a suffix. (For example "wrs:" is the device* name for the "wrs" host.)** RETURNS : OK, or ERROR if could not open directory.*/STATUS ftpLs ( char * dirName /* name of directory to list */ ) { DEV_HDR * pDevHdr; char fullFileName [MAX_FILENAME_LENGTH]; char hostName [MAXHOSTNAMELEN]; int hostLength; char usr [MAX_IDENTITY_LEN]; char passwd [MAX_IDENTITY_LEN]; int dataSock; int cntrlSock; char buffer [BUFSIZ]; int nChars; /* Get device header and complete filename */ ioFullFileNameGet (dirName, &pDevHdr, fullFileName); /* Get the host name, and remove the trailing ":" */ strcpy (hostName, pDevHdr->name); hostLength = strlen (hostName); hostName [hostLength - 1] = EOS; /* Get user ID information */ remCurIdGet (usr, passwd); if (ftpXfer (hostName, usr, passwd, "", "NLST", fullFileName, "", &cntrlSock, &dataSock) != OK) { FTPLDEBUG ("Can't open directory \"%s\"\n", FTPL_DEBUG_ERRORS,dirName,1,2,3,4,5); return (ERROR); } /* Write out the listing */ while ((nChars = read (dataSock, (char *) buffer, BUFSIZ)) > 0) write (STD_OUT, (char *) buffer, nChars); /* Close the sockets opened by ftpXfer */ close (cntrlSock); close (dataSock); return (OK); }/********************************************************************************* ftpLibDebugOptionSet - set the debug level of the ftp library routines** This routine enables the debugging of ftp transactions using the ftp library.** \ts* Debugging Level | Meaning* ----------------------------------------------------------------* FTPL_DEBUG_OFF | No debugging messages. * FTPL_DEBUG_INCOMING | Display all incoming responses. * FTPL_DEBUG_OUTGOING | Display all outgoing commands. * FTPL_DEBUG_ERRORS | Display warnings and errors* \te* EXAMPLE* .CS* ftpLibDebugOptionsSet (FTPL_DEBUG_ERRORS); /@ Display any runtime errors @/* ftpLibDebugOptionsSet (FTPL_DEBUG_OUTGOING); /@ Display outgoing commands @/* ftpLibDebugOptionsSet (FTPL_DEBUG_INCOMING); /@ Display incoming replies @/* ftpLibDebugOptionsSet (FTPL_DEBUG_INCOMING | /@ Display both commands and @/ * FTPL_DEBUG_OUTGOING); /@ replies @/* .CE**/void ftpLibDebugOptionsSet ( UINT32 debugLevel ) { ftplDebug = debugLevel; }/********************************************************************************* ftpTransientConfigSet - set parameters for host FTP_TRANSIENT responses ** This routine adjusts the delay between retries in response to receiving * FTP_PRELIM and the maximum retry count permitted before failing.** RETURNS : OK*/STATUS ftpTransientConfigSet ( UINT32 maxRetryCount, /* The maximum number of attempts to retry */ UINT32 retryInterval /* time (in system clock ticks) between retries */ ) { /* Set the values */ ftplTransientMaxRetryCount = maxRetryCount; ftplTransientRetryInterval = retryInterval; return (OK); }/********************************************************************************* ftpTransientConfigGet - get parameters for host FTP_TRANSIENT responses ** This routine retrieves the delay between retries in response to receiving * FTP_TRANSIENT and the maximum retry count permitted before failing.** RETURNS : OK** SEE ALSO : ftpTransientConfigSet, tickLib*/STATUS ftpTransientConfigGet ( UINT32 *maxRetryCount, /* The maximum number of attempts to retry */ UINT32 *retryInterval /* time (in system clock ticks) between retries */ ) { /* return the values */ if (maxRetryCount != NULL) *maxRetryCount = ftplTransientMaxRetryCount; if (retryInterval != NULL) *retryInterval = ftplTransientRetryInterval; return (OK); }/********************************************************************************* ftpTransientFatal - applette to terminate FTP transient host responses** ftpXfer will normally retry a command if the host responds with a 4xx* reply. If this applette is installed, it can immediately terminate* the retry sequence.*** RETURNS ** TRUE, terminate retry attempts; FALSE, continue retry attempts.** INTERNAL * * This is the default routine if the customer does not install* an applette.** SEE ALSO : ftpTransientFatalInstall(), ftpTransientConfigSet()**/LOCAL BOOL ftpTransientFatal ( UINT32 reply /* Three digit code defined in RFC #959 */ ) { switch (reply) { case (421): /* Service not available */ case (450): /* File unavailable */ case (451): /* error in processing */ case (452): /* insufficient storage */ { /* yes, these are actually non-recoverable replies */ return (TRUE); break; } /* attempt to retry the last command */ default: return (FALSE); } }/********************************************************************************* ftpTransientFatalInstall - set applette to stop FTP transient host responses** The routine installs a function which will determine if a transient response * should be fatal.* Some ftp servers incorrectly use 'transient' responses instead of * 'error' to describe conditions such as 'disk full'.** RETURNS * * OK if the installation is successful, or ERROR if the installation fails.** SEE ALSO ** ftpTransientConfigSet(), ftpTransientFatal() in * target/config/comps/src/net/usrFtp.c**/STATUS ftpTransientFatalInstall ( FUNCPTR pApplette /* function that returns TRUE or FALSE */ ) { /* At least prevent this from being a disaster */ if (pApplette == NULL) return (ERROR); _func_ftpTransientFatal = pApplette; return (OK); /* attempt to retry the last command */ }/********************************************************************************* ftpPasvReplyParse - Parse the reply of a PASV command** ftpPasvReplyParse expects to receive a string generated as a response from* a PASV command. The string will begin with '227' and end with a series* of six integer values encapsulated in parentheses and separated by commas.* EXAMPLE STRING:** 227 Entering passive mode (147,11,1,23,1027,1028)* * RETURNS : OK - Sucessfull parse* ERROR - Error in parse** SEE ALSO : ftpCommandEnhanced(), ftpReplyGetEnhanced()** NOMANUAL*/LOCAL STATUS ftpPasvReplyParse ( char *responseString, /* NULL terminated string */ UINT32 *argument1, /* First argument */ UINT32 *argument2, /* Second argument */ UINT32 *argument3, /* Third argument */ UINT32 *argument4, /* Fourth argument */ UINT32 *argument5, /* Fifth argument */ UINT32 *argument6 /* Six argument */ ) { char *index; UINT32 tmpArg1; UINT32 tmpArg2; UINT32 tmpArg3; UINT32 tmpArg4; UINT32 tmpArg5; UINT32 tmpArg6; if (responseString == NULL) return (ERROR); /* Sanity check: Check for '227' at the beginning of the reply */ if (strstr (responseString, "227") == NULL) { FTPLDEBUG ("ftpPasvReplyParse: error - '227' not part of PASV response\n", \ FTPL_DEBUG_ERRORS, 0,1,2,3,4,5); return (ERROR); } index = strstr(responseString, "("); if (index == NULL) { FTPLDEBUG ("ftpPasvReplyParse: error - '(' not part of PASV response\n", \ FTPL_DEBUG_ERRORS, 0,1,2,3,4,5); return (ERROR); } /* scan in the arguments */ sscanf (index+1, "%d,%d,%d,%d,%d,%d", &tmpArg1, &tmpArg2, &tmpArg3, &tmpArg4, &tmpArg5, &tmpArg6); /* Store arguments as neccesary */ if (argument1) *argument1 = tmpArg1; if (argument2) *argument2 = tmpArg2; if (argument3) *argument3 = tmpArg3; if (argument4) *argument4 = tmpArg4; if (argument5) *argument5 = tmpArg5; if (argument6) *argument6 = tmpArg6; return OK; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -