Hostnamefilter_src

csucdl 2009-02-05 07:13:02
// DB.CPP - This module implements the database routines for the authentication filter.

#include "stdafx.h"
#include "HostnameFilter.h"

/*
Routine Description:
Looks up the hostname and confirms the host is allowed access to the
server

Arguments:
pszHostname - The hostname to validate
pfValid - Set to TRUE if the host should be allowed

Return Value:
TRUE on success, FALSE on failure
*/
BOOL CHostnameFilter::ValidateHost(const CHAR* pszHostname, OUT BOOL* pfValid)
{
// Assume we're going to fail validation
*pfValid = FALSE;

BOOL fFound;

// Lookup the host in the cache, if that fails, get the host from the
// database and add the retrieved host to the cache
if ( !LookupHostInCache( pszHostname, &fFound ))
return FALSE;

if ( !fFound )
{
if ( !LookupHostInDb( pszHostname, &fFound ))
return FALSE;

if ( fFound )
AddHostToCache( pszHostname );
}

if ( !fFound )
{
ISAPITRACE1("[ValidateHost] Failed to find host %s\n", pszHostname );
return TRUE;
}

*pfValid = TRUE;

return TRUE;
}

/*
Routine Description:
Retrieves the hostlist from the file. If the hosts were coming from a
database, this routine would connect to the database.

Return Value:
TRUE on success, FALSE on failure

*/
BOOL CHostnameFilter::InitializeHostDatabase()
{
DWORD cbRead;

// Open and read the file. The System account must have access to the file.
HANDLE hFile = CreateFile( HOST_LIST_FILE, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if ( hFile == INVALID_HANDLE_VALUE )
{
ISAPITRACE2("[InitializeHostDatabase] Error %d openning %s\n", GetLastError(), HOST_LIST_FILE );
return FALSE;
}

DWORD cbFile = GetFileSize( hFile, NULL );
if ( cbFile == (DWORD) -1 )
{
CloseHandle( hFile );
return FALSE;
}

m_pszHostFile = (CHAR *)LocalAlloc( LPTR, cbFile + 1 );
if ( !m_pszHostFile )
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
CloseHandle( hFile );
return FALSE;
}

if ( !ReadFile( hFile, m_pszHostFile, cbFile, &cbRead, NULL ))
{
CloseHandle( hFile );
LocalFree( m_pszHostFile );
return FALSE;
}

CloseHandle( hFile );

// Zero terminate the file data
m_pszHostFile[cbRead] = '\0';

return TRUE;
}

/*
Routine Description:
Looks up the hostname in the database
The file data is not sorted to simulate the cost of an external database
lookup.

Arguments:
pszHostname - The hostname to find in the database (case insensitive)
pfFound - Set to TRUE if the specified host name was found in the
database

Return Value:
TRUE on success, FALSE on failure
*/
BOOL CHostnameFilter::LookupHostInDb(const CHAR* pszHostname, OUT BOOL* pfFound)
{
*pfFound = FALSE;

// Find the external hostname. We're expecting one host per line
CHAR* pch = m_pszHostFile;
CHAR* pszDomain = strchr( pszHostname, '.' );
if (pszDomain)
pszDomain++;
DWORD cchHost = strlen( pszHostname );

while ( pch && *pch )
{
while ( ISWHITE( *pch ) )
pch++;

if (*pch == '*')
{
if (pch[1] == '\r' || pch[1] == '\n') // * alone in a line?
goto Found;

pch += 2; // * and .
char* pDom = pszDomain;
while (pDom)
{
DWORD cchDomain = strlen( pDom );
if ( toupper( *pch ) == toupper( *pDom ) &&
!strnicmp( pDom, pch, cchDomain ) &&
(pch[cchDomain] == '\r' || pch[cchDomain] == '\n') )
{
goto Found;
}

pDom = strchr(pDom, '.');
if (pDom)
pDom++;
}
}
else
if ( toupper( *pch ) == toupper( *pszHostname ) &&
!strnicmp( pszHostname, pch, cchHost ) &&
(pch[cchHost] == '\r' || pch[cchHost] == '\n') )
{
goto Found;
}

pch = strchr( pch+1, '\n' );
}

// Not found
return TRUE;

Found:
*pfFound = TRUE;

return TRUE;
}

/*
Routine Description:
Shutsdown the host database.
*/
VOID CHostnameFilter::TerminateHostDatabase()
{
if ( m_pszHostFile )
LocalFree(m_pszHostFile );
}

// HOSTNAMEFILTER.CPP - Implementation file for your Internet Server
// Hostname Sample Filter

#include "stdafx.h"
#include "HostnameFilter.h"

#include <winsock2.h>

///////////////////////////////////////////////////////////////////////
// The one and only CWinApp object
// NOTE: You may remove this object if you alter your project to no
// longer use MFC in a DLL.

CWinApp theApp;



///////////////////////////////////////////////////////////////////////
// The one and only CHostnameFilter object

CHostnameFilter theFilter;

///////////////////////////////////////////////////////////////////////
// CHostnameFilter static members

DWORD CHostnameFilter::m_cCacheItems = 0;
CHAR* CHostnameFilter::m_pszHostFile = NULL;
LIST_ENTRY CHostnameFilter::m_CacheListHead;
CRITICAL_SECTION CHostnameFilter::m_csCacheLock;


///////////////////////////////////////////////////////////////////////
// CHostnameFilter implementation

CHostnameFilter::CHostnameFilter()
{
m_fCacheInitialized = FALSE;
}

CHostnameFilter::~CHostnameFilter()
{
TerminateCache();
TerminateHostDatabase();
WSACleanup();
}

BOOL CHostnameFilter::Initialize()
{
WORD wVersionRequested = MAKEWORD( 2, 0 );
WSADATA wsaData;
if ( WSAStartup( wVersionRequested, &wsaData ))
return FALSE;
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
return FALSE;

return (InitializeHostDatabase() && InitializeCache());
}

BOOL CHostnameFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
{
ISAPITRACE2( "[GetFilterVersion] Server filter version is %d.%d\n",
HIWORD( pVer->dwServerFilterVersion ),
LOWORD( pVer->dwServerFilterVersion ) );

if ( !Initialize() )
{
ISAPITRACE1("[GetFilterVersion] Database or cache failed, error %d\n", GetLastError() );
return FALSE;
}

// Call default implementation for initialization
CHttpFilter::GetFilterVersion(pVer);

// Set the flags we are interested in
pVer->dwFlags = SF_NOTIFY_ORDER_DEFAULT |
SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT |
SF_NOTIFY_URL_MAP;

// Load description string
TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
_tcscpy(pVer->lpszFilterDesc, sz);
return TRUE;
}

DWORD CHostnameFilter::OnUrlMap(CHttpFilterContext* pCtxt, PHTTP_FILTER_URL_MAP pMapInfo)
{
char szAddress[80];
DWORD dwSize = sizeof(szAddress);
if (pCtxt->GetServerVariable("REMOTE_ADDR", szAddress, &dwSize))
{
char nAddr[4];
nAddr[0] = nAddr[1] = nAddr[2] = nAddr[3] = 0;
int nIndex = 0;
char *pstr = szAddress;
while (*pstr != '\0' && nIndex < 4)
{
if (*pstr == '.')
nIndex++;
else
{
nAddr[nIndex] *= 10;
nAddr[nIndex] += (*pstr - '0');
}
pstr++;
}
// perform DNS reverse lookup using winsock
HOSTENT* pResult;
pResult = gethostbyaddr((const char *)&nAddr, 4, PF_INET);
if (pResult == NULL)
strncpy(pMapInfo->pszPhysicalPath, NO_NAME, pMapInfo->cbPathBuff-1);
else
{
// Make sure this host is a valid host
BOOL fAllowed;
if ( !ValidateHost(pResult->h_name, &fAllowed ))
{
ISAPITRACE2("[OnUrlMap] Error %d validating host %s\n", GetLastError(), pResult->h_name );
return SF_STATUS_REQ_ERROR;
}

if ( !fAllowed )
{
// This host isn't allowed access. Map to information URL
strncpy(pMapInfo->pszPhysicalPath, NO_ACCESS, pMapInfo->cbPathBuff-1);
}
}
}

return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CHostnameFilter, CHttpFilter)
//{{AFX_MSG_MAP(CHostnameFilter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0


...全文
136 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
源码直接下载地址: https://pan.quark.cn/s/854bd57df23a 在金融行业中,房产购置贷款是众多人在购置不动产时必须进行的一项财务规划。Matlab作为一种功能强大的数学及数据分析平台,经常被应用于各类复杂的运算,其中包括住房贷款的计算。本文将详细研究运用Matlab开发住房贷款计算程序的相关知识要点,以协助读者掌握如何借助编程技术处理实际的信贷问题。 1. **信贷种类** - **商业信贷**:此类贷款通常由金融机构提供,其利率会随市场行情变化,借款人可选用固定利率或浮动利率方案。 - **公积金信贷**:该类贷款获得政府机构的支持,利率一般低于商业信贷,但设有最高贷款金额的限制。 2. **偿付模式** - **等额本金偿付**:每月支付相同数量的本金,利息部分逐月减少,因此整体还款总额会逐步降低。 - **等额本息偿付**:每月支付固定金额的本金与利息组合,利息部分随本金的逐月减少而递减。 3. **住房贷款计算公式** - **等额本息偿付**:每月还款额=贷款本金×[月利率×(1+月利率)^(贷款期数)]÷[(1+月利率)^(贷款期数)-1]。 - **等额本金偿付**:每月应还本金=贷款总额/贷款期数,每月利息=(贷款余额-累计已还本金)×月利率。 4. **Matlab编程基础** - **变量设定**:在Matlab环境中,首要任务是设定贷款数额、年化利率、贷款时长等核心参数。 - **循环控制结构**:借助for循环来模拟每个还款周期的过程,进行本金与利息的计算。 - **函数构建**:将住房贷款的计算逻辑封装为函数,便于重复使用和测试不同条件下的结果。 5. **Matlab代码编写** - **等额本息函数实现**...

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧