📄 ftplib.c
字号:
/* ftpLib.c - File Transfer Protocol (FTP) library *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------02q,26jun02,elr Correction for passive mode STOR and RETR command in ftpXfer() (SPR #79108)02p,06jun02,elr Documentation (SPR #78008)02o,23may02,elr Added ftplPasvModeDisable flag02n,22may02,elr added PASV mode to ftpDataConnInit() (SPR #77169) to correct long delays during frequent reboots changed doc of ftpReplyGet() and ftpReplyGetEnhanced() (SPR #76317) changed ftpLibDebugLevelSet() to ftpLibDebugOptionsSet()02m,10may02,kbw making man page edits02l,22mar02,elr Cleanup compiler warnings02k,14mar02,elr Define host response return codes (SPR #68838) Host response FTP_PRELIM is ok during ftpXfer (SPR #71954) Limit FTP_TRANSIENT response retries during ftpXfer() ftpPrelimConfig() (SPR #70907) (SPR #6259) (SPR #33119) Added ftpLibDebugLevelSet() and added debugging messages Added documentation about stack memory allocation and tuning (SPR #64220) Moved some error message to FTPLDEBUG (SPR #71496) and removed ftpErrorSuppress in favor ftplDebug Added documentation concerning ftpHookup() and ftpDataConnInit() socket connections (SPR #62289) (SPR #30556)02j,15oct01,rae merge from truestack ver 02n, base 02i (SPR #67644)02i,22sep99,cno change FD_ISSET control & data connection tests (SPR27234)02h,15mar99,elg change erroneous example code in ftpXfer() (SPR 9989).02g,12mar99,p_m Fixed SPR# 9022 by publishing ftpLs().02f,05oct98,jmp doc: cleanup.02e,23jan98,spm fixed ftpXfer to expect correct return codes for commands which do not involve data transfer (SPR #20017)02d,05jun95,jag Changed ftpXfer to handle error 425 at boot time.02c,30aug93,jag Changed ftpCommand to issue a single write (SPR #2492)02b,11aug93,jmm Changed ioctl.h and socket.h to sys/ioctl.h and sys/socket.h02a,23feb93,jdi doc: changed ftpCommand() examples to fixed no. of args.01z,20jan93,jdi documentation cleanup for 5.1.01y,20sep92,kdl added ftpLs; moved ftpErrorSuppress to funcBind.c.01x,11sep92,jmm added ftpErrorSuppress for lsOld() (SPR #1257)01w,19aug92,smb Changed systime.h to sys/times.h.01v,18jul92,smb Changed errno.h to errnoLib.h.01u,26may92,rrr the tree shuffle -changed includes to have absolute path from h/01t,19nov91,rrr shut up some ansi warnings.01s,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed copyright notice01r,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by dnw.01q,12feb91,jaa documentation.01p,02oct90,hjb added a call to htons() where needed.01o,07may90,hjb added some documentation to ftpXfer routine to issue a "QUIT" at the end of the file transfer session via FTP.01n,22feb90,jdi documentation cleanup.01m,07jun89,gae changed SOCKADDR back to "struct sockaddr".01l,23sep88,gae documentation touchup.01k,30may88,dnw changed to v4 names.01j,28may88,dnw changed fioStdErr call to STD_ERR.01i,05apr88,gae changed fprintf() call to fdprintf()01h,17nov87,ecs lint: added include of inetLib.h.01g,11nov87,jlf documentation01f,06nov87,dnw fixed bug in use of setsockopt().01e,01nov87,llk changed remInetAddr() to UNIX compatible inet_addr().01d,01apr87,ecs hushed lint in ftpGetReply. changed "VARARGS 2" to "VARARGS2" in ftpCommand removed extraneous 4th arg from calls to bind, socket, accept, & connect.01c,19mar87,dnw documentation prepended FTP_ to ftp reply codes.01b,14feb87,dnw changed to use getsockname() instead of using privileged port.01a,07nov86,dnw written*//*DESCRIPTIONThis library provides facilities for transferring files to and from a hostvia File Transfer Protocol (FTP). This library implements only the"client" side of the FTP facilities.FTP IN VXWORKSFor most purposes, you should access the services of ftpLib by means of netDrv, a VxWorks I/O driver that supports transparent access to remote files by means of standard I/O system calls. Before attempting to access ftpLib services directly, you should check whether netDrv already provides the same access for less trouble.HIGH-LEVEL INTERFACEThe routines ftpXfer() and ftpReplyGet() provide the highest level ofdirect interface to FTP. The routine ftpXfer() connects to a specifiedremote FTP server, logs in under a specified user name, and initiates aspecified data transfer command. The routine ftpReplyGet() receivescontrol reply messages sent by the remote FTP server in response to thecommands sent.LOW-LEVEL INTERFACEThe routines ftpHookup(), ftpLogin(), ftpDataConnInit(), ftpDataConnGet(),ftpCommand(), ftpCommandEnhanced() provide the primitives necessary to create and use control and data connections to remote FTP servers. The following example shows how to use these low-level routines. It implements roughly the same function as ftpXfer()..CSchar *host, *user, *passwd, *acct, *dirname, *filename;int ctrlSock = ERROR; /@ This is the control socket file descriptor @/int dataSock = ERROR; /@ This is the data path socket file descriptor @/if (((ctrlSock = ftpHookup (host)) == ERROR) || (ftpLogin (ctrlSock, user, passwd, acct) == ERROR) || (ftpCommand (ctrlSock, "TYPE I", 0, 0, 0, 0, 0, 0) != FTP_COMPLETE) || (ftpCommand (ctrlSock, "CWD %s", dirname, 0, 0, 0, 0, 0) != FTP_COMPLETE) || ((dataSock = ftpDataConnInit (ctrlSock)) == ERROR) || (ftpCommand (ctrlSock, "RETR %s", filename, 0, 0, 0, 0, 0) != FTP_PRELIM) || ((dataSock = ftpDataConnGet (dataSock)) == ERROR)) { /@ an error occurred; close any open sockets and return @/ if (ctrlSock != ERROR) close (ctrlSock); if (dataSock != ERROR) close (dataSock); return (ERROR); }.CEFor even lower-level access, please note that the sockets provided by ftpHookup() and ftpDataConnInit() are standard TCP/IP sockets. Developers may implement read(), write() and select() calls using these sockets for maximum flexibility.To use this feature, include the following component:INCLUDE_FTPTUNING FOR MULTIPLE FILE ACCESS: Please note that accessing multiple files simultaneously may require increasing the memory available to the network stack. You can examine memory requirements by using netStackSysPoolShow() and netStackDataPoolShow()before opening and after closing files.You may need to modify the following macro definitions according to your specific memory requirements: NUM_64 NUM_128 NUM_256 NUM_512 NUM_1024 NUM_2048 NUM_SYS_64 NUM_SYS_128 NUM_SYS_256 NUM_SYS_512 NUM_SYS_1024 NUM_SYS_2048Please also note that each concurrent file access requires three file descriptors (File, Control and Socket). The following macro definition may need modification per your application:NUM_FILESDevelopers are encouraged to enable the error reporting facility during debugging using the function ftpLibDebugOptionsSet(). The output is displayed via the logging facility.INCLUDE FILES: ftpLib.hSEE ALSO: netDrv, logLib*/#include "vxWorks.h"#include "ctype.h"#include "sys/socket.h"#include "netinet/in.h"#include "ftpLib.h"#include "inetLib.h"#include "sys/times.h"#include "hostLib.h"#include "sockLib.h"#include "stdio.h"#include "string.h"#include "unistd.h"#include "errnoLib.h"#include "iosLib.h"#include "remLib.h"#include "selectLib.h"#include "taskLib.h"#include "vwModNum.h"#include "private/ftpLibP.h"#include "private/funcBindP.h"typedef struct sockaddr_in SOCKADDR_IN;#define UCA(n) (((int)(((char *)&dataAddr.sin_addr)[n])) & 0xff)#define UCP(n) (((int)(((char *)&dataAddr.sin_port)[n])) & 0xff)#define FTPLDEBUG(string, debugLevel, param1, param2, param3, param4, param5, param6)\ {\ if ((_func_logMsg != NULL) && (ftplDebug & debugLevel))\ (* _func_logMsg) (string, param1, param2, param3, param4, param5, param6);\ }#define FTP_PORT 21#define PASV_REPLY_STRING_LENGTH 256/* XXX ftplPasvModeDisable - XXX this will be removed in the next release XXX */BOOL ftplPasvModeDisable = FALSE;LOCAL STATUS ftpPasvReplyParse (char *, UINT32 *, UINT32 *, UINT32 *, \ UINT32 *, UINT32 *, UINT32 *);LOCAL BOOL ftpTransientFatal (UINT32 reply);LOCAL char pasvReplyString[PASV_REPLY_STRING_LENGTH];UINT32 ftplTransientMaxRetryCount = 1; /* Retry just once for now */UINT32 ftplTransientRetryInterval = 0; /* Default with no delay */FUNCPTR _func_ftpTransientFatal = ftpTransientFatal;/********************************************************************************* ftpCommand - send an FTP command and get the reply ** This command has been superceded by ftpCommandEnhanced() ** This routine sends the specified command on the specified socket, which* should be a control connection to a remote FTP server.* The command is specified as a string in printf() format with up* to six arguments.** After the command is sent, ftpCommand() waits for the reply from the* remote server. The FTP reply code is returned in the same way as in* ftpReplyGet().** EXAMPLE* .CS* ftpCommand (ctrlSock, "TYPE I", 0, 0, 0, 0, 0, 0); /@ image-type xfer @/* ftpCommand (ctrlSock, "STOR %s", file, 0, 0, 0, 0, 0); /@ init file write @/* .CE** RETURNS:** 1 = FTP_PRELIM (positive preliminary)* 2 = FTP_COMPLETE (positive completion)* 3 = FTP_CONTINUE (positive intermediate)* 4 = FTP_TRANSIENT (transient negative completion)* 5 = FTP_ERROR (permanent negative completion)** ERROR if there is a read/write error or an unexpected EOF.** SEE ALSO: ftpReplyGet()** VARARGS2*/int ftpCommand ( int ctrlSock, /* fd of control connection socket */ char *fmt, /* format string of command to send */ int arg1, /* first of six args to format string */ int arg2, int arg3, int arg4, int arg5, int arg6 ) { /* return most significant digit of the reply */ return (ftpCommandEnhanced (ctrlSock, fmt, arg1, arg2, arg3, arg4, arg5, arg6, NULL, 0) / 100); }/********************************************************************************* ftpCommandEnhanced - send an FTP command and get the complete RFC reply code** This command supercedes ftpCommand() ** This routine sends the specified command on the specified socket, which* should be a control connection to a remote FTP server.* The command is specified as a string in printf() format with up* to six arguments.** After the command is sent, ftpCommand() waits for the reply from the* remote server. The FTP reply code is returned in the same way as in* ftpReplyGetEnhanced().** EXAMPLE* .CS* ftpCommandEnhanced (ctrlSock, "TYPE I", 0, 0, 0, 0, 0, 0, 0, 0); /@ image-type xfer @/* ftpCommandEnhanced (ctrlSock, "STOR %s", file, 0, 0, 0, 0, 0, 0, 0); /@ init file write @/* ftpCommandEnhanced (ctrlSock, "PASV", file, 0, 0, 0, 0, 0, reply, rplyLen); /@ Get port @/* .CE** RETURNS:* The complete FTP response code (see RFC #959)** ERROR if there is a read/write error or an unexpected EOF.** SEE ALSO: ftpReplyGetEnhanced(), ftpReplyGet()** VARARGS2*/int ftpCommandEnhanced ( int ctrlSock, /* fd of control connection socket */ char *fmt, /* format string of command to send */ int arg1, /* first of six args to format string */ int arg2, int arg3, int arg4, int arg5, int arg6, char *replyString, /* storage for the last line of the server response or NULL */ int replyStringLength /* Maximum character length of the replyString */ ) { char buffer [128]; int len; if (ftplDebug & FTPL_DEBUG_OUTGOING) { printErr ("---> "); printErr (fmt, arg1, arg2, arg3, arg4, arg5, arg6); printErr ("\n"); } /* Format Command to send to FTP server */ /* The next line will generate a warning with gcc 2.96+, this is O.K. */ sprintf (buffer, fmt, arg1, arg2, arg3, arg4, arg5, arg6); len = strlen(buffer); /* Append CR LF to format copy to force a single write to TCP */ sprintf(&buffer[len],"%s","\r\n"); len = strlen(buffer); if (write(ctrlSock, buffer, len) < len) { FTPLDEBUG ("ftpCommandEnhanced: error during write to control socket - errno:0x%08x", \ FTPL_DEBUG_ERRORS,errno,1,2,3,4,5); return(ERROR); } return (ftpReplyGetEnhanced (ctrlSock, !strcmp (fmt, "QUIT"), replyString, replyStringLength)); }/********************************************************************************* ftpXfer - initiate a transfer via FTP** This routine initiates a transfer via a remote FTP server* in the following order:* .IP (1) 4* Establishes a connection to the FTP server on the specified host.* .IP (2)* Logs in with the specified user name, password, and account,* as necessary for the particular host.* .IP (3)* Sets the transfer type to image by sending the command "TYPE I".* .IP (4)* Changes to the specified directory by sending* the command "CWD <dirname>".* .IP (5)* Sends the specified transfer command* with the specified filename as an argument, and establishes a data connection.* Typical transfer commands are "STOR %s", to write to a remote file,* or "RETR %s", to read a remote file.* .LP* The resulting control and data connection file descriptors are returned* via <pCtrlSock> and <pDataSock>, respectively.** After calling this routine, the data can be read or written to the remote* server by reading or writing on the file descriptor returned in* <pDataSock>. When all incoming data has been read (as indicated by * an EOF when reading the data socket) and/or all outgoing data has been* written, the data socket fd should be closed. The routine ftpReplyGet()* should then be called to receive the final reply on the control socket,* after which the control socket should be closed.** If the FTP command does not involve data transfer, <pDataSock> should be * NULL, in which case no data connection will be established. The only * FTP commands supported for this case are DELE, RMD, and MKD.** EXAMPLE* The following code fragment reads the file "/usr/fred/myfile" from the* host "server", logged in as user "fred", with password "magic"* and no account name.** .CS* #include "vxWorks.h"* #include "ftpLib.h"** int ctrlSock;* int dataSock;* char buf [512];* int nBytes;* STATUS status;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -