WarSoftwareLib.h
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:19k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : WarSoftwareLib.h
- // PURPOSE :
- // PROGRAM :
- // DATE : Sept. 19 1996
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- /////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////
- // B A S E C L A S S E S
- /////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////
- class DLL_WAR_SOFTWARE_ CNotificationWnd : public CWnd
- {
- public:
- static CWnd *m_pWnd;
- static CWinThread *m_pThread;
- // Construction
- public:
- CNotificationWnd();
- ~CNotificationWnd();
- static CWnd *GetWin() {return m_pWnd;}
- static HWND GetHwnd() {return m_pWnd->m_hWnd;}
- static BOOL IsSameThread() {return m_pThread == AfxGetThread();}
- LONG (* m_OnTrayIconMessage)(WPARAM WParam, LPARAM LParam);
- protected:
- //{{AFX_MSG(CNotificationWnd)
- LRESULT OnRctdreq(WPARAM wParam, LPARAM lParam);
- LRESULT OnlogLine(WPARAM WParam, LPARAM LParam);
- LRESULT OnDNS(WPARAM WParam, LPARAM LParam);
- LONG OnTrayIconMessage(WPARAM WParam, LPARAM LParam);
- afx_msg void OnStopandquit();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- ////////////////////////////////////////////////////////////////////
- // CLog class
- // Log Base class
- // Must be overloaded in order to be initialized before use.
- class DLL_WAR_SOFTWARE_ CLog : public CLogOptions
- {
- public:
- CWarTimer m_Timer; // Last flush
- CCriticalSection m_Lock;
- CString m_LogQueue; // Queued buffer to flush
- CHistory m_LogHistory; // History of the n last lines on log display
- BOOL m_UseHistory;
- HANDLE m_EventHandle;
- static CLog *m_pLog;
- DWORD m_Flags;
- CLog();
- ~CLog();
- void LogMsg(int LogType, LPCSTR Format, ...);
- void LogMsgV(int LogType, LPCTSTR Format, va_list argList);
- static LPCSTR MyTime(LPSTR buf);
- static LPCSTR FormatTime(SYSTEMTIME& st, LPSTR buf);
- static void SendHistory(LPVOID pOrigin, CDaemonEvent *pCliEv);
- BOOL Flush(BOOL Force);
- virtual void LoggedLine(int LogType, LPCSTR Line); // Override to log to window
- static CLog *GetLog() {return m_pLog;}
- };
- #define ShouldLog(pLog, flag) (pLog && (pLog->m_Flags &flag))
- #define LOGF_DEBUG 0x0001
- #define LOGF_ERROR 0x0002
- #define LOGF_FILEACC 0x0004
- #define LOGF_INOUT 0x0008
- #define LOGF_SECURITY 0x0010
- #define LOGF_WARNINGS 0x0020
- #define LOGF_SYSTEM 0x0040
- #define LOGF_WINSOCK 0x0080
- ///////////////////////////////////////////////////////////////////////////////////////
- // CSock
- // Base class for all async socket communicatios
- class CSock;
- class DLL_WAR_SOFTWARE_ CDNSLookup
- {
- public:
- CDNSLookup();
- BOOL Create(CSock *pOrigin, LPCSTR Name = NULL, LPARAM LParam = NULL);
- static void DNSlookupCallback(WPARAM Handle, LPARAM Status);
- DWORD m_ID; // CSock m_SocketID
- char m_Buf[MAXGETHOSTSTRUCT];
- LPARAM m_LParam;
- CString m_QueryName;
- private:
- ~CDNSLookup();
- HANDLE m_ReqHandle;
- static CLinkedList m_List;
- };
- class DLL_WAR_SOFTWARE_ CSocketException
- {
- private:
- CSocketException();
- public:
- static void CSocketException::Throw(CSock *pSock,
- LPCSTR Module, int ErrorNum, LPCSTR Format, ...);
- CString m_FromModule;
- CString m_ErrorText;
- int m_ErrorNum;
- CSock *m_pSock;
- };
- class DLL_WAR_SOFTWARE_ CSock : public CAsyncSocket
- {
- public:
- DECLARE_DYNAMIC(CSock)
- CSock();
- ~CSock();
- virtual BOOL IsOkToDelete(BOOL IgnoreSuspend = FALSE);
- void SuspendDelete() {++m_SuspendDelete;};
- void ReleaseSuspendDelete()
- {
- --m_SuspendDelete;
- ASSERT(m_SuspendDelete >= 0);
- };
- virtual void SetName(LPCSTR Name);
- static void SetLog(CLog *LogPtr);
- virtual void OnAccept( int nErrorCode );
- virtual void OnClose( int nErrorCode );
- virtual void OnConnect( int nErrorCode );
- virtual void _OnConnect( int nErrorCode );
- virtual void OnOutOfBandData( int nErrorCode );
- virtual void OnReceive( int nErrorCode );
- virtual void OnSend( int nErrorCode );
- virtual BOOL Accept( CAsyncSocket& rConnectedSocket, SOCKADDR* lpSockAddr = NULL, int* lpSockAddrLen = NULL );
- virtual void Close( );
- virtual int Receive( void* lpBuf, int nBufLen, int nFlags = 0 );
- virtual int Send( const void* lpBuf, int nBufLen, int nFlags = 0 );
- virtual void LogLastError(int flag, int eval, LPCSTR msg);
- virtual void LogMsg(int flag, LPCSTR Format, ...);
- virtual void OnFileCompletion(int nErrorCode);
- static BOOL OnIdle( LONG lCount );
- virtual CUserFsys *GetFsys();
- virtual float GetLapTime();
- virtual float GetCPS();
- static BOOL GetHostIpNumber(CString& cIpName);
- virtual void KillAllChildren();
- virtual void IncXferCnt(BOOL Add);
- static CSock *GetFromID(DWORD ID);
- static CWnd *m_DNSwin;
- void InitiateDNSLookup();
- virtual void OnDNSLookup(int nErrorCode, CDNSLookup *pDNS);
- static void SetDNSWin(CWnd *pWnd);
- virtual BOOL OnHandleWinsockErr(int nErrorCode );
- static float GetTotalCPS();
- void SetIdleTime(int limit);
- void SetLogonTime(int limit);
- protected:
- BOOL DoAccept(CSock *NewSocket );
- virtual void SayGoodbye();
- public:
- CString m_SocketName; // Our own name for the socket
- CString m_PeerName; // IP address to remote host
- CString m_DNSName;
- CString m_HostName; // local IP address for the socket
- int m_Type; // Service type
- int m_State; // Connection state
- BOOL m_IsConnected; // Is the socket connected?
- BOOL m_SuspendOnClose; // Dont process OnClose!
- CWarTimer m_Timer;
- CWarTimer m_ConnectTime;
- CWarTimer m_LastSend;
- CWarTimer m_LastReceive;
- CWarTimer m_StartTime; // First send or receive
- int m_MaxIdleTime;
- int m_MaxOnlineTime;
- BOOL m_ClearToSend;
- BOOL m_ClearToReceive;
- DWORD m_BytesSent;
- DWORD m_BytesReceived;
- DWORD m_SocketID; // Sequence number
- DWORD m_AsyncSelectMask;
- CSock *m_Father; // Ptr to listening socket
- static DWORD m_SequenceCounter;
- static CLog *m_Log;
- static DWORD m_NumSocketsInUse;
- static CCriticalSection m_GlobalLock;
- static LPSTR DaemonTypes[4];
- static m_OverlappedCnt; // Total number of pending overlapped requests
- static CLinkedList m_SocketList;
- BOOL m_DestructorIsActive; // The destructor is called.
- int m_SuspendDelete;
- static CWarTimer m_Housekeeping;
- // Plugin support
- enum // Hooks
- {
- OnPreFSYSAccess,
- OnPostFSYSAccess,
- OnGetSecurityDescriptor,
- iOnConnect,
- iOnReceive,
- iOnSend,
- iOnCommand,
- iOnClose,
- iSendCtrlMsg,
- iOnSITECmd,
- iOnIdle,
- iOnVerifyIPAddress,
- iOnVerifyUploadedFile,
- iOnVerifyTransferRequest,
- iOnSocketIsDestroyed,
- iOnBadPassword,
- iOnHasLoggedOn,
- iOnVerifyLogin,
- iReserved2,
- iReserved3,
- iReserved4,
- iReserved5,
- iReserved6,
- iReserved7,
- iReserved8,
- iReserved9,
- iReserved10,
- invalid
- };
- CFuncList m_Funcs[invalid];
- };
- #define PrcSockExt(FuncID, Event, wParam, lParam)
- m_Funcs[FuncID].Process(Event,wParam,lParam)
- enum // states m_State
- {
- PRELOGIN,
- GOTNAME,
- PROCESS,
- HOLD,
- GETFILE,
- PUTFILE,
- QUITTING,
- STATE_INVALID
- };
- enum // Socket type m_Type
- {
- LT_FTP,
- LT_FTP_DATA,
- LT_TELNET,
- LT_REMOTE,
- LT_INVALID
- };
- #define SOCKERR_IDLETIME -100 // Idle time expired
- #define SOCKERR_LOGINTIME -101 // Login time expired
- ///////////////////////////////////////////////////////////////////////////////////////
- // CBufferSock
- // Base class for buffered socket outpot
- // Will accept large buffers to speed things up.
- class DLL_WAR_SOFTWARE_ CBufferSock : public CSock
- {
- public:
- DECLARE_DYNAMIC(CBufferSock);
- CBufferSock();
- ~CBufferSock();
- virtual void OnSend( int nErrorCode );
- virtual int Send( const void* lpBuf, int nBufLen, int nFlags);
- long SendBuffer(LPCSTR Buffer, DWORD Size);
- virtual BOOL DoOverlapped();
- void TrashOutputBuffer();
- protected:
- virtual BOOL CBufferSock::DoSend();
- protected:
- LPSTR m_OutBuf;
- DWORD m_MaxBufferSize; // Can be increased during usage
- DWORD m_BufferSize; // Current allocated buffer
- DWORD m_BufferUsed; // Bytes in use
- int m_MaxSendLen; // Bytes to send to Winsock at one time
- };
- #define BUFFERSOCK_DEFUALT_BUFFER_SIZE (1024 * 8)
- #define BUFFERSOCK_DEFUALT_SEND_SIZE (1024 * 8)
- #define BufferSock_BufferFree() (m_BufferSize - m_BufferUsed)
- ///////////////////////////////////////////////////////////////////////////////////////
- // CTextSock
- // Base class for ASCII based communications.
- // Splits incomming packages into CRLF delimited lines and
- // call OnCommand() for each complete line.
- // Parse command *must* be overridden
- // Also handles Telnet negotiation.
- // Outgoing packages are always sent as text buffers.
- // Support for standard Internet nnn[-]<sp>Text code replies
- // The buffers are queued automatically until a given limit.
- class DLL_WAR_SOFTWARE_ CTextSock : public CBufferSock
- {
- public:
- DECLARE_DYNAMIC(CTextSock);
- int m_LocalEcho; // 1 == normal echo, >1 = echo fixed char (password mode)
- BOOL m_HandleBackspace;
- int m_SuspendExpandMacros;
- CString m_LoginName; // Login name: jarle@jgaa.ldp.no
- int m_User; // User handle
- CString m_RootDir;
- CString m_HomeDir;
- CString m_CWD; // Current Working Directory
- BOOL m_IsLoggedInAnonymously;
- CLinkedList *m_EventList; // List of events we have installed
- CLinkedList *m_EventQueue;
- CDaemonEvent *m_Events;
- int m_LoginNum;
- static int m_LoginCounter;
- BOOL m_IsLoggedIn;
- int m_StateSave;
- // Send file support
- BOOL m_SuspendEvents;
- HANDLE m_FileHandle;
- private:
- LPSTR m_InBuf;
- DWORD m_BufferSize; // Current allocated buffer
- DWORD m_BufferUsed; // Bytes in use
- DWORD m_SavedIndex;
- BOOL m_HaveIAC;
- BOOL m_HaveCR;
- protected:
- BOOL m_Virgin; // Call the virgin() function until FALSE
- int m_LastRcode;
- int m_InsertOffset;
- // Server side login event managment
- static CLinkedList m_UserConnections;
- static HANDLE m_SvrEvents;
- CString m_Info;
- public:
- static BOOL InitSvrEvents();
- static BOOL TerminateSvrEvents();
- virtual void NotifyLogin(DWORD Flags, LPCSTR InfoTxt = NULL, CDaemonEvent *pCliEv = NULL);
- static void WhoSendAll(LPVOID pOrigin, CDaemonEvent *pCliEv);
- public:
- CTextSock();
- ~CTextSock();
- virtual BOOL OnCommand(LPCSTR Text);
- virtual void OnReceive( int nErrorCode );
- virtual void OnConnect( int nErrorCode );
- virtual void OnClose(int nErrorCode);
- virtual BOOL Send(LPCSTR Text);
- virtual BOOL SendCtrlMsg(int Rcode, LPCSTR Text, BOOL More = FALSE, BOOL CrLf = TRUE);
- virtual BOOL SendMsg(int Rcode, LPCSTR Format, ...);
- virtual BOOL DoSend();
- virtual void SetName(LPCSTR Name);
- virtual BOOL OnHandleWinsockErr(int nErrorCode );
- BOOL SendFile(LPCSTR Path, LPCSTR Name);
- void SuspendMacros() {m_SuspendExpandMacros++;}
- void EnableMacros() {if (m_SuspendExpandMacros) m_SuspendExpandMacros--;}
- protected:
- virtual void Virgin();
- virtual BOOL ProcessTelnetNegotiation(int ch1, int ch2);
- virtual void InsertPendingText();
- virtual BOOL ExpandMacro(LPSTR *To, LPCSTR MacroName, int AvailBytes);
- virtual void SayGoodbye();
- };
- #define TEXTSOCK_DEFUALT_BUFFER_SIZE (1024 * 4)
- #define BufferText_BufferFree() (m_BufferSize - m_BufferUsed)
- #define ENL_NEW 0x0001 // New user on list
- #define ENL_DEL 0x0002 // Delete user from list
- #define ENL_STATE 0x0004 // State has changed
- #define ENL_NAME 0x0008 // Name has changed
- #define ENL_UPDATE 0x0010 // Update all fields
- ///////////////////////////////////////////////////////////////////////////////////////
- // CFTPDataSock
- // FTP Data channel socket - ready to use
- // Override to add statistics for User and update
- // of VfSys
- class CFTPOverlapped
- {
- public:
- OVERLAPPED m_OverlappedData;
- LPSTR m_Buffer;
- BOOL m_KillMe;
- CFTPDataSock *m_Origin;
- CWarFile *m_pFile;
- public:
- CFTPOverlapped();
- ~CFTPOverlapped();
- };
- class DLL_WAR_SOFTWARE_ CFTPPassiveSock : public CSock
- {
- public:
- CFTPDataSock *m_Origin;
- BOOL m_HasAccepted;
- SOCKADDR m_Sockaddr;
- public:
- CFTPPassiveSock();
- ~CFTPPassiveSock();
- virtual void OnAccept( int nErrorCode );
- virtual BOOL Create(CFTPDataSock *Origin, int Port);
- private:
- void _OnAccept( int nErrorCode );
- };
- class CRemoteInterface;
- class DLL_WAR_SOFTWARE_ CFTPDataSock : public CBufferSock
- {
- public:
- DECLARE_DYNAMIC(CFTPDataSock);
- CFTPDataSock **m_PtrToOriginsPtrToMe;
- CTextSock *m_OriginD; // Origin for FTP Daemon
- CRemoteInterface *m_OriginC; // Origin for FTP Client
- CSock *m_OriginB; // Origin base - for simpllicity
- CFTPPassiveSock *m_ListeningSock; // Used for passive mode
- int m_FTPmode;
- int m_FTPtype;
- int m_FTPstru;
- int m_FTPform;
- DWORD m_Flags;
- struct sockaddr m_Sockaddr; // Used to connect to remote data socket
- BOOL m_IsTmpFile;
- CString m_FileName;
- CString m_VisualFileName;
- FLEN m_FileLength;
- FLEN m_StartOffset;
- FLEN m_CurrentPosition;
- BOOL m_HaveToldUserThatTransfereIsComplete;
- BOOL m_UseOverlappedIO;
- BOOL m_SendMode; // Send file, false on receive file
- CWarFile *m_pFile;
- LPSTR m_DataBuffers[2];
- BOOL m_OverlappedIOReady;
- BOOL m_EOF;
- int m_OverlappedIOUsed;
- int m_OverlappedIOToggle;
- CFTPOverlapped *m_Overlapped;
- #ifdef _DEBUG
- int dm_OverlappedCnt;
- #endif
- public:
- CFTPDataSock();
- ~CFTPDataSock();
- BOOL Create(CTextSock *Origin, CFTPDataSock ** Ptr,
- int FTPmode, int FTPstru, int FTPform, int FTPtype,
- sockaddr *Sockaddr, BOOL IsPassive, DWORD Flags = 0);
- BOOL Create(CRemoteInterface *Origin, CFTPDataSock ** Ptr,
- int FTPmode, int FTPstru, int FTPform, int FTPtype, sockaddr *Sockaddr,
- BOOL Passive, DWORD Flags = 0);
- BOOL Create(CRemoteInterface *OriginC, CTextSock *OriginD, CFTPDataSock ** Ptr,
- int FTPmode, int FTPstru, int FTPform, int FTPtype, sockaddr *Sockaddr,
- BOOL Passive, DWORD Flags = 0);
- virtual BOOL SendFile(LPCSTR FileName, LPCSTR VisualFileName,
- BOOL IsTmp, FLEN StartOffset, FLEN FileLength);
- virtual BOOL ReceiveFile(LPCSTR FileName, LPCSTR VisualFileName, BOOL IsTmp,
- FLEN StartOffset, FLEN FileLength);
- virtual BOOL XmitFile(LPCSTR FileName, LPCSTR VisualFileName, BOOL IsTmp,
- FLEN StartOffset, FLEN FileLength);
- virtual BOOL DoConnect();
- virtual void OnConnect( int nErrorCode );
- virtual void OnReceive( int nErrorCode );
- virtual BOOL DoSend();
- virtual void OnSend( int nErrorCode );
- virtual BOOL DoOverlapped();
- virtual void OnFileCompletion(int nErrorCode);
- virtual void OnClose( int nErrorCode );
- virtual BOOL ReadWriteOverlapped();
- static VOID WINAPI OnOverlappedIO(DWORD dwErrorCode,
- DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped);
- virtual BOOL IsOkToDelete(BOOL IgnoreSuspend = FALSE);
- virtual BOOL OnHandleWinsockErr(int nErrorCode );
- static void CallbackFileIsCreated(CWarFile *pFile, BOOL Success, LPARAM lParam);
- virtual BOOL XmitFileCallback(BOOL Success);
- private:
- void _OnConnect( int nErrorCode );
- void _OnReceive( int nErrorCode );
- void _OnClose( int nErrorCode );
- };
- ///////////////////////////////////////////////////////////////////////////////////////
- // CDaemonEvent
- // Class to pass events from a module to a remote process.
- class CRegisteredEvents
- {
- public:
- CRegisteredEvents();
- ~CRegisteredEvents();
- public:
- int m_Event;
- CLinkedList m_ClientEventObjects;
- void (* m_DsNotify)(LPVOID pOrigin, CDaemonEvent *pCliEv); // Notification about new event member
- LPVOID m_pOrigin;
- };
- class DLL_WAR_SOFTWARE_ CDaemonEvent
- {
- public:
- CLinkedList *m_DsList; // Pointer to daemon object list to this event
- int m_MaxQueueItems;
- CLinkedList m_EventQueue;
- int m_EventTypes;
- CTextSock *m_Client;
- BOOL m_ClosePending;
- static CLinkedList m_DsEventRegister; // Installed events
- public:
- // Daemon side functions
- static HANDLE DsRegister(int EventType, LPVOID pOrigin,
- void (*)(LPVOID pOrigin, CDaemonEvent *pCliEv));
- static BOOL DsClose(HANDLE h);
- static BOOL DsEvent(HANDLE h, LPCSTR EventMsg, BOOL Priority = FALSE);
- BOOL Event(HANDLE h, LPCSTR EventMsg, BOOL Priority = FALSE, CString *MyCs = NULL);
- // Socket side
- CDaemonEvent(CTextSock *Socket, int MaxQueueItems);
- ~CDaemonEvent();
- BOOL Register(int EventType); // Allocate and enable events
- BOOL Process(); // Send events to client program
- BOOL Close();
- BOOL Close(int Events);
- private:
- static CRegisteredEvents *FindRegisteredEvent(int EventType);
- };
- #define EVT_LOG 0x0001
- #define EVT_STAT 0x0002
- #define EVT_CONN 0x0004
- ///////////////////////////////////////////////////////////////////////////////////////
- // CDaemonStatus
- // Current server state
- class DLL_WAR_SOFTWARE_ CDaemonStatus : public CObject
- {
- public:
- DECLARE_SERIAL(CDaemonStatus)
- SYSTEMTIME m_ServerStarted; // Used by server side
- int m_UserConnections;
- int m_AnonUserConnections;
- int m_OperatorConnections;
- int m_UserConnectionsTotal;
- int m_AnonUserConnectionsTotal;
- int m_OperatorConnectionsTotal;
- int m_FileXfers;
- int m_FileXfersTotal;
- BOOL m_IsOnline;
- BOOL m_ShutdownPending;
- CString m_ServerName;
- CString m_ServerEmail;
- CString m_ServerType;
- int m_ServerPort;
- int m_RemoteAPort;
- CString m_ServerListeningIP;
- CString m_ServerStartedFmt; // Used by client side
- public:
- CDaemonStatus();
- ~CDaemonStatus();
- BOOL Extract(LPSTR Msg);
- void Serialize(CArchive& ar);
- static CDaemonStatus * GetStat() {return m_pMe; }
- private:
- static CDaemonStatus *m_pMe;
- };
- ///////////////////////////////////////////////////////////////////////////////////////
- // DES13 password encryption
- DLL_WAR_SOFTWARE_ LPCSTR crypt(LPCSTR buf,LPCSTR argSalt, char *buff);
- DLL_WAR_SOFTWARE_ BOOL IsDES13validPwd(LPCSTR Password, LPCSTR StoredPassword);
- /////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////
- // G L O B A L V A R I A B L E S a n d m a n i f e s t s
- /////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////
- class DLL_WAR_SOFTWARE_ CProgramInfo
- {
- public:
- static LPCSTR m_PROGRAM;
- static LPCSTR m_VERSION;
- static LPCSTR m_COPYRIGHT;
- static int m_SVRTYPE;
- enum // Server types
- {
- stFTP = 1,
- stRemoteInterface,
- stInvalid
- };
- };
- #define RIOOB_HDR_LEN 10 // Remote Interface OOB header length
- #define OOB_HDR "1oob" // OOB header