sprint 1-alpha
sprint/io/network/pmftpp.h
00001 /*****************************************************************************\
00002 *                                                                             *
00003 * pmftpp.h       -  File Transfert Protocol     C++                                                               *
00004 *                                                                             *
00005 *               Version Win32 / Windows NT                                    *
00006 *                                                                             *
00007 *               Copyright (c) 1999-2003, Paolo Medici Software                 *
00008 *                                                                             *
00009 \*****************************************************************************/
00010 
00011 #ifndef PM_FTP_PLUS
00012 #define PM_FTP_PLUS
00013 
00014 #include <xsocket.h>
00015 
00016 /* status */
00017 enum FTP_CONNECTION_STATES {
00018     FTP_DISCONNECTED,
00019     FTP_CONNECTION_CONNECTED,
00020     FTP_CONNECTION_AUTHENTICATION,
00021     FTP_USER_LOGGED,
00022         FTP_DATA_CONNECTION_WAITING,
00023     FTP_DATA_CONNECTION_ESTABLISHED,
00024     FTP_TRANSFER,
00025 };
00026 
00027 /* FTP RESPONSE */
00028 #define FTP_RESPONSE_RESTATRT_MARKER_REPLY                              110
00029 #define FTP_RESPONSE_SERVICE_READY_IN_MINUTES                   120
00030 #define FTP_RESPONSE_DATA_CONNECTION_ALREADY_OPEN               125
00031 #define FTP_RESPONSE_FILE_STATUS_OK                                             150
00032 #define FTP_RESPONSE_COMMAND_OK                                                 200
00033 #define FTP_RESPONSE_COMMAND_NOT_IMPLEMENTED_SUPERFLUOUS_AT_THIS_SITE   202
00034 #define FTP_RESPONSE_SYSTEM_STATUS_OR_SYSTEM_HELP_REPLY         211
00035 #define FTP_RESPONSE_DIRECTORY_STATUS                                   212
00036 #define FTP_RESPONSE_FILE_STATUS                                                213
00037 #define FTP_RESPONSE_HELP_MESSAGE                                               214
00038 #define FTP_RESPONSE_NAME_SYSTEM_TYPE                                   215
00039 #define FTP_RESPONSE_SERVICE_READY_FOR_NEW_USER                 220
00040 #define FTP_RESPONSE_SERVICE_CLOSING_CONTROL_CONNECTION 221
00041 #define FTP_RESPONSE_DATA_CONNECTION_OPEN                               225
00042 #define FTP_RESPONSE_CLOSING_DATA_CONNECTION                    226
00043 #define FTP_RESPONSE_ENTERING_PASSIVE_MODE                              227
00044 #define FTP_RESPONSE_USER_LOGGED_IN                                             230
00045 #define FTP_RESPONSE_REQUESTED_FILE_ACTION_OK_COMPLETED 250
00046 #define FTP_RESPONSE_PATHNAME_CREATED                                   257
00047 #define FTP_RESPONSE_USER_NAME_OK_NEED_PASSWORD                 331
00048 #define FTP_RESPONSE_NEED_ACCOUNT_FOR_LOGIN                             332
00049 #define FTP_RESPONSE_REQUESTED_FILE_ACTION_PENDING_FURTHER_INFO                 350
00050 #define FTP_RESPONSE_SERVICE_NOT_AVAILABLE_CLOSING_CONTROL_CONNECTION   421
00051 #define FTP_RESPONSE_CANNOT_OPEN_DATA_CONNECTION                                                425
00052 #define FTP_RESPONSE_CONNECTION_CLOSED_TRANSFER_ABORTED                                 426
00053 #define FTP_RESPONSE_REQUESTED_FILE_ACTION_NOT_TAKEN                                    450
00054 #define FTP_RESPONSE_REQUESTED_ACTION_ABORTED                                                   451
00055 #define FTP_RESPONSE_REQUESTED_ACTION_NOT_TAKEN                                                 452
00056 #define FTP_RESPONSE_SYNTAX_ERROR_COMMAND_UNRECOGNIZED                                  500
00057 #define FTP_RESPONSE_SYNTAX_ERROR_IN_PARAMETERS_OR_ARGUMENTS                    501
00058 #define FTP_RESPONSE_COMMAND_NOT_IMPLEMENTED                                                    502
00059 #define FTP_RESPONSE_BAD_SEQUENCE_OF_COMMANDS                                                   503
00060 #define FTP_RESPONSE_COMMAND_NOT_IMPLEMENTED_FOR_THAT_PARAMETER                 504
00061 #define FTP_RESPONSE_NOT_LOGGED_IN                                                                              530
00062 #define FTP_RESPONSE_NEED_ACCOUNT_FOR_STORING_FILES                                             532
00063 #define FTP_RESPONSE_REQUESTED_ACTION_NOT_TAKEN_FILE_UNAVAILABLE                550
00064 #define FTP_RESPONSE_REQUESTED_ACTION_ABORTED_PAGE_TYPE_UNKNOWN                 551
00065 #define FTP_RESPONSE_REQUESTED_FILE_ACTION_ABORTED_EXCEEDED_STORAGE_ALLOCATION          552
00066 #define FTP_RESPONSE_REQUESTED_ACTION_NOT_TAKEN_FILE_NAME_NOT_ALLOWED   553
00067 
00068 
00069 /* FUNCTION ERROR */
00070 #define HR_OK                           0
00071 #define HR_INVALID_ADDRESS      1
00072 #define HR_CANNOT_CONNECT       2
00073 #define HR_RESPONSE_TIMEOUT     3
00074 #define HR_CANCELLED            4
00075 #define HR_CANNOT_CREATE        5
00076 
00077 enum FtpTransferModes {
00078     FTP_ASCII_MODE,
00079     FTP_IMAGE_MODE
00080 };
00081 
00082 #define DEFAULT_FTP_PORT        21
00083 
00084 
00085 typedef BOOL (* ENUMFILESCALLBACK) (LPVOID lpContext);
00086 
00087 class FtpClient: protected XSocket {
00088         FTP_CONNECTION_STATES status;
00089         UINT uLastFtpResp;
00090         char ftpresp[512];
00091 
00092         bool passive_mode;
00093 
00094         FtpTransferModes curmode;
00095         sockaddr_in passive_addr;
00096         XSocket file;   /* Trasfer file */
00097         XSocket port;   /* Port */
00098 
00099         bool _ftp;
00100         bool _file;
00101 
00102         DWORD dwFileSize;
00103 
00104 protected:
00105         char *GetResponse();
00106         unsigned int GetResponseId();
00107         unsigned int GetResponseIdSync(UINT uTimeOut, BOOL bIgnoreComment=FALSE);
00108 
00109         HRESULT RenameFrom(LPCSTR lpszFrom);
00110         HRESULT RenameTo(LPCSTR lpszTo);
00111 
00112         HRESULT Pasv();
00113         HRESULT Port();
00114 
00115         HRESULT Mode(FtpTransferModes newmode);
00116 
00117         HRESULT WaitForIncomingConnection();
00118         HRESULT ConnectToServer();
00119 
00120         HRESULT _setup_transfer();
00121         HRESULT _begin_transfer();
00122         HRESULT _end_transfer();
00123 
00124 public:
00125         /*********************** client interface ************************************* */
00126         virtual void print(const char *string);
00127         void printf(const char *, ...);
00128         /****************************************************************************** */
00129 
00130         const char *ResponseToString(HRESULT hr);
00131 
00132         /****************************************************************************** */
00133 
00134         FtpClient();
00135         ~FtpClient();
00136 
00137         bool Init();
00138 
00139         /****************************************************************************** */
00140 
00141         HRESULT FtpCommand(LPCSTR lpszCommand);
00142 
00143         HRESULT Connect(LPCSTR lpszServer, UINT nServerPort, LPCSTR lpszUsername, LPCSTR lpszPassword);
00144         HRESULT Disconnect();
00145 
00146         inline FTP_CONNECTION_STATES GetState() {       
00147                 return status;
00148         }
00149 
00150         inline UINT GetLastFtpResponse()
00151         {
00152                 return uLastFtpResp;
00153         }
00154 
00155         /****************************************************************************** */
00156 
00157         HRESULT GetCurrentDirectory(LPSTR lpszCurrentDirectory, DWORD dwBufSize);
00158         HRESULT SetCurrentDirectory(LPCSTR lpszCurrentDirectory);
00159 
00160         HRESULT CreateDirectory(LPCSTR lpszDirectory);
00161         HRESULT RemoveDirectory(LPCSTR lpszDirectory);
00162 
00163         HRESULT Rename(LPCSTR lpszItem, LPCSTR lpszNewName);
00164 
00165         HRESULT EnumFiles(ENUMFILESCALLBACK efcProc, LPVOID lpContext);
00166 
00167         /****************************************************************************** */
00168 
00169         HRESULT SetPassiveMode(bool bPassiveMode);
00170         HRESULT SetTransferMode(FtpTransferModes tm);
00171 
00172         HRESULT PutFile(LPCSTR lpszLocalFile, LPCSTR lpszNewRemoteFile);
00173         HRESULT GetFile(LPCSTR lpszRemoteFile, LPCSTR lpszNewFile);
00174 
00175         HRESULT DeleteFile(LPCSTR lpszFileName);
00176 
00177         HRESULT OpenFile(LPCSTR lpszFileName);
00178         HRESULT CreateFile(LPCSTR lpszFileName);
00179         HRESULT WriteFile(const char *lpBuffer, DWORD dwBytesToWrite, LPDWORD lpdwBytesWrited);
00180         HRESULT ReadFile(char *lpBuffer, DWORD dwBytesToRead, LPDWORD lpdwBytesReaded);
00181 
00182         inline DWORD GetFileSize()      {
00183                 return dwFileSize;
00184         }
00185 
00186         HRESULT CloseFile();
00187 
00188         /****************************************************************************** */
00189 
00190 
00191 };
00192 
00193 
00194 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines