⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftplib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ftpLib.c - File Transfer Protocol (FTP) library *//* Copyright 1984 - 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------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 VXWORKSVxWorks provides an I/O driver, netDrv, that allows transparent access toremote files via standard I/O system calls.  The FTP facilities of ftpLibare primarily used by netDrv to access remote files.  Thus for mostpurposes, it is not necessary to be familiar with ftpLib.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(),and ftpCommand() provide the primitives necessary to create and usecontrol and data connections to remote FTP servers.  The following exampleshows how to use these low-level routines.  It implements roughly the samefunction as ftpXfer()..CSchar *host, *user, *passwd, *acct, *dirname, *filename;int ctrlSock = ERROR;int dataSock = ERROR;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);    }.CEINCLUDE FILES: ftpLib.hSEE ALSO: netDrv*/#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 "private/ftpLibP.h"#include "private/funcBindP.h"typedef struct sockaddr_in SOCKADDR_IN;#define FTP_PORT	21BOOL ftpVerbose = FALSE;	/* TRUE = print all incoming messages */BOOL ftpDebug   = FALSE;	/* TRUE = print outgoing messages *//********************************************************************************* ftpCommand - send an FTP command and get the reply** 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    )    {    char buffer [128];    int len;    if (ftpDebug)	{	printErr ("---> ");	printErr (fmt, arg1, arg2, arg3, arg4, arg5, arg6);	printErr ("\n");        }    /* Format Command to send to FTP server */    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)	return(ERROR);    return (ftpReplyGet (ctrlSock, !strcmp (fmt, "QUIT")));    }/********************************************************************************* 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;**     if (ftpXfer ("server", "fred", "magic", "",*                  "RETR %s", "/usr/fred", "myfile",*                  &ctrlSock, &dataSock) == ERROR)*         return (ERROR);**     while ((nBytes = read (dataSock, buf, sizeof (buf))) > 0)*         {*         ...*         }**     close (dataSock);**     if (nBytes < 0)             /@ read error? @/*         status = ERROR;**     if (ftpReplyGet (ctrlSock, TRUE) != FTP_COMPLETE)*         status = ERROR;**     if (ftpCommand (ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0) != FTP_COMPLETE)*         status = ERROR;**     close (ctrlSock);* .CE** RETURNS:* OK, or ERROR if any socket cannot be created or if a connection cannot be* made.** SEE ALSO: ftpReplyGet()*/STATUS ftpXfer    (    char *host,         /* name of server host */    char *user,         /* user name for host login */    char *passwd,       /* password for host login */    char *acct,         /* account for host login */    char *cmd,          /* command to send to host */    char *dirname,      /* directory to 'cd' to before sending command */    char *filename,     /* filename to send with command */    int *pCtrlSock,     /* where to return control socket fd */    int *pDataSock      /* where to return data socket fd, */                        /* (NULL == don't open data connection) */    )    {    FAST int ctrlSock = ERROR;    FAST int dataSock = ERROR;    if (((ctrlSock = ftpHookup (host)) == ERROR)		       ||        (ftpLogin (ctrlSock, user, passwd, acct) != OK)		       ||        (ftpCommand (ctrlSock, "TYPE I",0,0,0,0,0,0) != FTP_COMPLETE)  ||	((dirname[0] != EOS) && (ftpCommand (ctrlSock, "CWD %s", 	(int)dirname,0,0,0,0,0) != FTP_COMPLETE)))	{	/* Detected an error during command establishment */	close (ctrlSock);	return (ERROR);	}    /*     * This is a special when an FTP command does not need to establish a     * data connection.     */	    if (pDataSock == NULL)	{	if (ftpCommand (ctrlSock, cmd, (int)filename, 0, 0, 0, 0, 0)                 != FTP_COMPLETE)	    {            /* FTP command error. */	    close (ctrlSock);	    return (ERROR);	    }	}    else	{	struct fd_set readFds;	int		withd;	int		cmdResult;	/*  Set up local data port and send the PORT command */tryAnotherPort:	if ((dataSock = ftpDataConnInit (ctrlSock)) == ERROR)	    {	    close (ctrlSock);	    return (ERROR);	    }	/* Send the FTP command.  */        cmdResult = ftpCommand (ctrlSock, cmd, (int)filename,0,0,0,0,0);		if (cmdResult != FTP_PRELIM)	    {	    /* 	     * Command failed.  Close the data socket and decode the error.	     * If the error was due to transient error (e.x. the data port	     * was not available) retry the command.  Exit for any other error.	     */	    close (dataSock);	    if (cmdResult == FTP_TRANSIENT)		goto tryAnotherPort;	    close (ctrlSock);	    return (ERROR);	    }	/* At this point do a select on the data & control socket */	FD_ZERO (&readFds);	FD_SET  (ctrlSock, &readFds);	FD_SET  (dataSock, &readFds);	withd  = (dataSock > ctrlSock) ? dataSock : ctrlSock;	withd++;	if (select (withd, &readFds, NULL, NULL, NULL) == ERROR)	    {	    close (dataSock);	    close (ctrlSock);	    return (ERROR);	    }	/* If the control socket is ready process it and take a decisition,	 * to try again or return error. If the data socket is ready call	 * ftpDataConnGet next.	 */	if (FD_ISSET (ctrlSock, &readFds))	    {	    close (dataSock);	    if (ftpReplyGet (ctrlSock, FALSE) == FTP_TRANSIENT)		goto tryAnotherPort;	    /* Regardless of response close sockets */     	    (void) ftpCommand (ctrlSock, "QUIT", 0, 0, 0, 0, 0, 0);	    close (ctrlSock);	    return (ERROR);	    }	/* 	 * The data socket must be ready. Wait for the FTP server to connect	 * to us.	 */	if ((dataSock = ftpDataConnGet (dataSock)) == ERROR)	    {	    close (ctrlSock);	    return (ERROR);	    }	}    *pCtrlSock = ctrlSock;    if (pDataSock != NULL)	*pDataSock = dataSock;    return (OK);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -