WarAPI.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:9k
源码类别:
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 License 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 : WarAPI.cpp
- // PURPOSE : Daemon extention dll core
- // PROGRAM :
- // DATE : Febr 17 1997
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include "WarSoftware.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- static const LPCSTR KnownFuncNames[] =
- {
- "?CallApiInitInstance@@YAHPAXHIJ@Z",
- "?CallApiExitInstance@@YAHPAXHIJ@Z",
- "?CallOnLogin@@YAHPAXHIJ@Z",
- "?CallOnLogout@@YAHPAXHIJ@Z",
- "?CallOnPassword@@YAHPAXHIJ@Z",
- "?CallOnNewSocket@@YAHPAXHIJ@Z",
- "?CallOnNewTextSocket@@YAHPAXHIJ@Z",
- "?CallOnNewFTPDataSocket@@YAHPAXHIJ@Z",
- "?CallOnFTPDCoreCtrlSock@@YAHPAXHIJ@Z",
- "?CallOnPreFTPDAccept@@YAHPAXHIJ@Z",
- "?CallOnPostFTPDAccept@@YAHPAXHIJ@Z",
- };
- ////////////////////////////////////////////////////////////////////////////////
- // CSocketAPI
- CSocketAPI::CSocketAPI()
- {
- }
- CSocketAPI::~CSocketAPI()
- {
- ASSERT(CAPIHandler::GetPtr() != NULL);
- if (CAPIHandler::GetPtr())
- CAPIHandler::GetPtr()->Unload(this);
- }
- ////////////////////////////////////////////////////////////////////////////////
- // CFuncList
- int CFuncList::Process(int nEvent, WPARAM wParam, LPARAM lParam)
- {
- int Rval, SuggestRval = 0;
- CLinkedListItem *pNext;
- for (CLinkedListItem *Item = First(); Item; Item = pNext)
- {
- pNext = Next(Item);
- SFUNC *pFunc = (SFUNC *)Ptr(Item);
- Rval = pFunc->pFunc(pFunc->pOrigin, nEvent, wParam, lParam);
- if (Rval)
- {
- switch(Rval)
- {
- case OkNoNext:
- return OkContinue;
- case SuggestNo:
- case SuggestYes:
- SuggestRval = Rval;
- break;
- default:
- return Rval;
- }
- }
- }
- if (SuggestRval == SuggestNo)
- return AbortError;
- return OkContinue;
- }
- LPVOID CFuncList::Alloc(CDllInfo *pDLL, int Index, CDaemonAPI *pAPI, int (*pCallFunc)(LPVOID,int,WPARAM,LPARAM))
- {
- SFUNC *pFunc = new SFUNC;
- pFunc->pFunc = pCallFunc;
- pFunc->pOrigin = pAPI;
- SFUNCREF *pFR = new SFUNCREF;
- pFR->pFunc = pFunc;
- pFR->Index = Index;
- pFR->pList = this;
- pDLL->m_Refs.AddLast((LPVOID)pFR);
- return (LPVOID)pFunc;
- }
- BOOL CFuncList::AddFirst(CDllInfo *pDLL, int Index, CDaemonAPI *pAPI, int (*pCallFunc)(LPVOID,int,WPARAM,LPARAM))
- {
- CLinkedList::AddFirst(Alloc(pDLL, Index, pAPI, pCallFunc));
- return TRUE;
- }
- BOOL CFuncList::AddLast(CDllInfo *pDLL, int Index, CDaemonAPI *pAPI, int (*pCallFunc)(LPVOID,int,WPARAM,LPARAM))
- {
- CLinkedList::AddLast(Alloc(pDLL, Index, pAPI, pCallFunc));
- return TRUE;
- }
- BOOL CFuncList::AddFirst(CDllInfo *pDLL, int Index, CSocketAPI *pAPI, int (*pFunc)(LPVOID,int,WPARAM,LPARAM))
- {
- return AddFirst(pDLL, Index, (CDaemonAPI *)pAPI, pFunc);
- }
- BOOL CFuncList::AddLast(CDllInfo *pDLL, int Index, CSocketAPI *pAPI, int (*pFunc)(LPVOID,int,WPARAM,LPARAM))
- {
- return AddLast(pDLL, Index, (CDaemonAPI *)pAPI, pFunc);
- }
- CAPIHandler *CAPIHandler::m_pMe;
- CDaemonAPI *CAPIHandler::m_pCurrentLoadingModule;
- int CAPIHandler::m_CurrentLoadingModuleNum;
- CAPIHandler::CAPIHandler()
- {
- if (m_pMe)
- AfxThrowUserException();
- DECLARE_APIHANDLER_OPTIONS
- Create(NULL,NULL,"CAPIHandler", COPTION_APIHANDLER);
- m_pMe = this;
- LoadAll();
- }
- CAPIHandler::~CAPIHandler()
- {
- CDllInfo *pDLL;
- PrcExt(ApiExitInstance, 0,0,0);
- while(pDLL = (CDllInfo *)m_Plugins.FirstPtr())
- {
- if (pDLL->m_pHI)
- FreeLibrary(pDLL->m_pHI);
- m_Plugins.GetAndDeleteFirst();
- delete pDLL;
- }
- SaveAll();
- m_pMe = NULL;
- }
- // Load all extentions for this server type
- BOOL CAPIHandler::LoadExtentions()
- {
- CString SrcPath, SrcPathSave;
- SrcPath.Format("%s\%s", GetStartupPath(), m_ScanDir);
- if (!SrcPath.IsEmpty() && (SrcPath[SrcPath.GetLength() -1] != '\'))
- SrcPath += '\';
- SrcPathSave = SrcPath;
- SrcPath += "*.emod";
- HANDLE h;
- WIN32_FIND_DATA Data;
- if ((h = FindFirstFile(SrcPath,&Data)) != INVALID_HANDLE_VALUE)
- {
- do
- {
- CString Name;
- Name.Format("%s%s", SrcPathSave, Data.cFileName);
- CDaemonStatus *pStat = CDaemonStatus::GetStat();
- BOOL DoLoad = GetPrivateProfileInt("type", pStat->m_ServerType, 0, Name)
- ||GetPrivateProfileInt("type", "ALL", 0, Name);
- DoLoadModule(Name, DoLoad);
- } while (FindNextFile(h,&Data));
- FindClose(h);
- }
- PrcExt(ApiInitInstance, 0,0,0);
- return TRUE;
- }
- BOOL CAPIHandler::DoLoadModule(LPCSTR IniName, BOOL DoLoad)
- {
- CString Name, Path, MgrPath, Descr, bBuf;
- HINSTANCE hi = NULL;
- m_CurrentLoadingModuleNum = 0;
- GetPrivateProfileString("inf", "Filename", "", Path.GetBuffer(MAX_PATH), MAX_PATH, IniName);
- GetPrivateProfileString("mgr", "Filename", "", MgrPath.GetBuffer(MAX_PATH), MAX_PATH, IniName);
- GetPrivateProfileString("inf", "Name", "", Name.GetBuffer(MAX_PATH), MAX_PATH, IniName);
- GetPrivateProfileString("inf", "Descr", "", Descr.GetBuffer(MAX_PATH), MAX_PATH, IniName);
- Path.ReleaseBuffer();
- MgrPath.ReleaseBuffer();
- Name.ReleaseBuffer();
- Descr.ReleaseBuffer();
- CDllInfo *pDLL = new CDllInfo;
- pDLL->m_Name = Name;
- pDLL->m_Description = Descr;
- pDLL->m_ServerPath = Path;
- pDLL->m_ManagerPath = MgrPath;
- pDLL->m_pHI = NULL;
- pDLL->m_OptionNum = 0;
- pDLL->m_pAPI = NULL;
- pDLL->m_IniPath = IniName;
- if (DoLoad)
- {
- if (hi = LoadLibrary(Path))
- {
- CLog::GetLog()->LogMsg(
- LOGF_SYSTEM,"Loading extention module %s (%s) %s...",
- Name, Path, Descr);
- // Search for functions to use
- for(int Index = 0; Index < invalid; Index++)
- {
- int (*func)(LPVOID,int,WPARAM,LPARAM);
- if (func = (int (*)(LPVOID,int,WPARAM,LPARAM))GetProcAddress(hi, KnownFuncNames[Index]))
- m_Funcs[Index].AddLast(pDLL, Index, m_pCurrentLoadingModule, func);
- }
- }
- else
- {
- CLog::GetLog()->LogMsg(
- LOGF_ERROR,"Failed to load extention module %s (%s) - %s",
- Path, Path, GetLastErrorText(GetLastError()));
- }
- }
- pDLL->m_pHI = hi;
- pDLL->m_OptionNum = m_CurrentLoadingModuleNum;
- pDLL->m_pAPI = m_pCurrentLoadingModule;
- m_Plugins.AddFirst((LPVOID)pDLL);
- return TRUE;
- }
- // List all known extensions, including verison number
- CString CAPIHandler::ListAll()
- {
- CLinkedListItem *Item;
- CString cBuf("Enum, Name, Description, SvrPath, MgrPath"), cTmp;
- for(Item = GetPtr()->m_Plugins.First(); Item; Item = GetPtr()->m_Plugins.Next(Item))
- {
- CDllInfo *pDLL = (CDllInfo *)GetPtr()->m_Plugins.Ptr(Item);
- cTmp.Format("n%d 1%s1 1%s1 1%s1 1%s1",
- pDLL->m_OptionNum,
- pDLL->m_Name,
- pDLL->m_Description,
- pDLL->m_ServerPath,
- pDLL->m_ManagerPath);
- cBuf += cTmp;
- }
- return cBuf;
- }
- // Unload all entry points used by an extension dll
- void CAPIHandler::Unload(CDaemonAPI *pAPI)
- {
- CLinkedListItem *ApiItem, *Item;
- for(ApiItem = m_Plugins.First(); ApiItem; ApiItem = m_Plugins.Next(ApiItem))
- {
- CDllInfo *pDLL = (CDllInfo *)m_Plugins.Ptr(ApiItem);
- if (pDLL->m_pAPI == pAPI)
- {
- // Found it. Now, unloadd all resources...
- for(Item = pDLL->m_Refs.First(); Item;)
- {
- CLinkedListItem *Curr = Item;
- Item = pDLL->m_Refs.Next(Item);
- SFUNCREF *pRef = (SFUNCREF *)pDLL->m_Refs.Ptr(Curr);
- pRef->pList->DeletePtr(pRef->pFunc);
- delete pRef->pFunc;
- delete pRef;
- pDLL->m_Refs.DeleteItem(Curr);
- // Set the dll info to 'list only' state
- pDLL->m_pAPI = NULL;
- pDLL->m_pHI = NULL;
- pDLL->m_OptionNum = 0;
- }
- break; // No need to test the rest...
- }
- }
- }
- // Unload all entry points used by a socket
- void CAPIHandler::Unload(CSocketAPI *pSockAPI)
- {
- CLinkedListItem *ApiItem, *Item;
- for(ApiItem = m_Plugins.First(); ApiItem; ApiItem = m_Plugins.Next(ApiItem))
- {
- CDllInfo *pDLL = (CDllInfo *)m_Plugins.Ptr(ApiItem);
- for(Item = pDLL->m_Refs.First(); Item;)
- {
- CLinkedListItem *Curr = Item;
- Item = pDLL->m_Refs.Next(Item);
- SFUNCREF *pRef = (SFUNCREF *)pDLL->m_Refs.Ptr(Curr);
- if (pRef->pFunc->pOrigin == (CDaemonAPI *)pSockAPI)
- {
- // Got one!
- pRef->pList->DeletePtr(pRef->pFunc); // This deletes the socket's own reference
- delete pRef->pFunc;
- delete pRef;
- pDLL->m_Refs.DeleteItem(Curr);
- }
- }
- }
- }
- BOOL CAPIHandler::EnableDll(LPCSTR Cmd)
- {
- CLinkedListItem *Item;
- BOOL Enable;
- CString Name = Cmd;
- CDaemonStatus *pStat = CDaemonStatus::GetStat();
- LPSTR p = strchr(Name.GetBuffer(1), '=');
- if (!p)
- return FALSE;
- *p++ = 0;
- Enable = atoi(p) != 0;
- Name.ReleaseBuffer();
- Name.TrimRight();
- for(Item = m_Plugins.First(); Item; Item = m_Plugins.Next(Item))
- {
- CDllInfo *pDLL = (CDllInfo *)m_Plugins.Ptr(Item);
- if (!stricmp(pDLL->m_Name, Name))
- {
- WritePrivateProfileString("type", pStat->m_ServerType, Enable ? "1" : "0", pDLL->m_IniPath);
- return TRUE;
- }
- }
- return FALSE;
- }