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


...全文
135 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文系统研究了永磁同步电机(PMSM)的速度控制问题,重点探讨了矢量控制(FOC)技术的原理与实现方法,并基于Simulink搭建了完整的仿真模型进行验证。文章深入剖析了矢量控制的核心环节,包括Clarke变换与Park变换在内的坐标变换原理、磁场定向控制策略、双闭环(电流环与速度环)PI控制器的设计,以及空间矢量脉宽调制(SVPWM)技术的应用。通过仿真实验,验证了该控制策略在调节PMSM转速和转矩时具备良好的动态响应性能和稳态精度,为高性能电机驱动系统的研发提供了详实的理论依据和技术参考。; 适合人群:具备电机学、电力电子技术和自动控制理论基础的电气工程、自动化、机电一体化等相关专业的本科生、研究生、科研人员及从事电机驱动开发的工程技术人员。; 使用场景及目标:①深入学习和掌握永磁同步电机矢量控制的理论基础与关键技术;②利用Simulink进行电机控制系统的建模、仿真与算法验证,服务于课程设计、毕业设计或科研项目;③为工业领域中高性能伺服驱动器、新能源汽车电驱系统等产品的开发提供技术方案与仿真支持。; 阅读建议:建议读者结合提供的Simulink仿真模型,动手实践并调整控制器参数(如PI参数),通过对比不同参数下的系统响应曲线,深刻理解各控制环节的作用和系统动态性能的优化方法。
内容概要:本文以电商用户行为数据为基础,完整呈现了一个从零构建用户购买意愿预测系统的数据挖掘实战项目。项目遵循“数据采集→清洗→探索分析→特征工程→建模评估→部署监控”的标准流程,使用Python模拟生成20万条用户行为日志,经过缺失值、非法行为、重复记录和异常时间戳的逐层清洗后,构建了用户维度(如total_pv、total_cart、active_days)和商品维度(如item_cart_rate)的聚合特征。采用随机森林与逻辑回归进行对比建模,结果显示随机森林在各项指标上全面领先,且通过SMOTE类别平衡与网格搜索调参进一步提升了召回率与F1值。文章还提供了模型部署、线上监控(AUC衰减、特征漂移等)及迭代重训的闭环方案,并总结了可复用的方法论与未来优化方向。; 适合人群:具备一定Python编程基础,熟悉pandas、scikit-learn等工具,有1-3年数据分析或机器学习相关经验的研发人员或数据科学家。; 使用场景及目标:①掌握用户行为分析类项目的端到端实施路径;②学习如何从原始日志中构造有效特征并进行模型选型、调优与评估;③了解模型上线后的监控机制与迭代策略,实现数据挖掘成果的业务落地。; 阅读建议:建议结合代码实践,逐步运行各环节脚本,重点关注数据清洗的逻辑设计、特征工程的业务含义、模型对比的评估维度以及监控模块的实现细节,深入理解每一步对最终业务目标的支持作用。

65,213

社区成员

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

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