📄 netchck.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hlxclib/windows.h"
#include "prefdefs.h"
#include "netchck.h"
#include "hxtick.h"
#include "hxassert.h"
#include "hxver.h"
#include <raserror.h> // for ras error codes
#define DEF_DNS_PORT 53 //tcp
#define DIALOG_CLASS "#32770"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
// Comment out this definition for release versions that shouldn't have
// logging code compiled in.
#define USE_NETDETECT_LOG 1
#ifdef USE_NETDETECT_LOG
#include "hlxclib/fcntl.h" // for file constants
#include "chxdataf.h" // CHXDataFile
#include "hlxclib/time.h"
#include "pref.h" // CPref
#include "ihxpckts.h" // IHXBuffer
#endif
namespace NetDetectLog
{
#ifndef USE_NETDETECT_LOG
#define WRITE_LOG0(format) do {;} while(0)
#define WRITE_LOG1(format, x1) do {;} while(0)
#define INITIALIZE_LOG() do {;} while(0)
#define CLOSE_LOG() do {;} while(0)
#define WRITE_LOG_WINET(result, flags) do {;} while(0)
#else
#define STR_EOL "\r\n"
const char* const kszNetdetectLogFileName = "netdtlog.txt";
const char* const kszNetdetectLogPrefKey = "netdetectlog";
static CHXDataFile* g_pLogFile = NULL;
HX_RESULT InitializeLog();
HX_RESULT CloseLog();
HX_RESULT WriteToLog(const CHXString& msg);
HX_RESULT WriteToLogWinInetGetConnected(BOOL result, DWORD flags);
#define INITIALIZE_LOG() do { InitializeLog(); } while(0)
#define CLOSE_LOG() do { CloseLog(); } while(0)
#define WRITE_LOG0(format) \
WriteToLog(format);
#define WRITE_LOG1(format, x1){ \
CHXString strFormat; \
strFormat.Format(format, x1); \
WriteToLog(strFormat);}
#define WRITE_LOG_WINET(result, flags) \
WriteToLogWinInetGetConnected(result, flags)
HX_RESULT WriteToLogWinInetGetConnected(BOOL result, DWORD flags)
{
// Flags for InternetGetConnectedState
#define INTERNET_CONNECTION_MODEM 0x01
#define INTERNET_CONNECTION_LAN 0x02
#define INTERNET_CONNECTION_PROXY 0x04
#define INTERNET_CONNECTION_MODEM_BUSY 0x08 /* no longer used */
#define INTERNET_RAS_INSTALLED 0x10
#define INTERNET_CONNECTION_OFFLINE 0x20
#define INTERNET_CONNECTION_CONFIGURED 0x40
CHXString strFormat;
if(result)
{
CHXString format("InternetGetConnectedState returned TRUE; flag = %s");
if(flags & INTERNET_CONNECTION_MODEM)
{
strFormat.Format(format, "INTERNET_CONNECTION_MODEM");
}
else if(flags & INTERNET_CONNECTION_LAN)
{
strFormat.Format(format, "INTERNET_CONNECTION_LAN");
}
else if(flags & INTERNET_CONNECTION_PROXY)
{
strFormat.Format(format, "INTERNET_CONNECTION_PROXY");
}
else if(flags & INTERNET_CONNECTION_MODEM_BUSY)
{
strFormat.Format(format, "INTERNET_CONNECTION_MODEM_BUSY");
}
else if(flags & INTERNET_RAS_INSTALLED)
{
strFormat.Format(format, "INTERNET_RAS_INSTALLED");
}
else if(flags & INTERNET_CONNECTION_OFFLINE)
{
strFormat.Format(format, "INTERNET_CONNECTION_OFFLINE");
}
else if(flags & INTERNET_CONNECTION_CONFIGURED)
{
strFormat.Format(format, "INTERNET_CONNECTION_CONFIGURED");
}
}
else
{
strFormat = "InternetGetConnectedState returned FALSE";
}
return WriteToLog(strFormat);
}
HX_RESULT InitializeLog()
{
HX_RESULT res = HXR_FAIL;
BOOL bShouldWriteLogFile = FALSE;
// Read from the preferences to see if we should write a log file.
CPref* pPreferences = CPref::open_shared_pref( HXVER_COMMUNITY );
if( pPreferences )
{
IHXBuffer* pBuffer = NULL;
if( SUCCEEDED( pPreferences->read_pref( kszNetdetectLogPrefKey, pBuffer )))
{
bShouldWriteLogFile =
( atoi( reinterpret_cast<const char*>(pBuffer->GetBuffer())) == 1 );
HX_RELEASE(pBuffer);
}
HX_DELETE( pPreferences );
}
if( !bShouldWriteLogFile )
{
return res;
}
// First try to open the file for appending. If that doesn't work,
// then just create a new one.
g_pLogFile = CHXDataFile::Construct();
if (g_pLogFile)
{
res = g_pLogFile->Open(kszNetdetectLogFileName,
O_BINARY | O_WRONLY | O_APPEND, TRUE);
if (FAILED(res))
{
res = g_pLogFile->Open(kszNetdetectLogFileName,
O_BINARY | O_CREAT | O_TRUNC | O_WRONLY, TRUE);
}
if( SUCCEEDED( res ))
{
CHXString strMsg;
strMsg.Format("%s################################################"
"##############################%s",
STR_EOL, STR_EOL);
g_pLogFile->Write(strMsg, strMsg.GetLength());
}
else
{
HX_DELETE(g_pLogFile);
}
}
return res;
}
HX_RESULT CloseLog()
{
if (g_pLogFile)
{
g_pLogFile->Close();
HX_DELETE(g_pLogFile);
}
return HXR_OK;
}
HX_RESULT WriteToLog(const CHXString& msg)
{
HX_RESULT res = HXR_FAIL;
if (g_pLogFile)
{
time_t now = time(NULL);
struct tm* date = localtime(&now);
char szTime[20] = { 0 }; /* Flawfinder: ignore */
#if !defined(WIN32_PLATFORM_PSPC)
if(date)
{
strftime(szTime, 19, "%X", date);
}
#endif /* !defined(WIN32_PLATFORM_PSPC) */
CHXString newMsg;
newMsg.Format("%s: %s%s", szTime, (const char*)msg, STR_EOL);
res = (g_pLogFile->Write(newMsg, newMsg.GetLength())
== newMsg.GetLength()) ? HXR_OK : HXR_FAIL;
}
return res;
}
#endif // USE_NETDETECT_LOG
// "resource aquisition is init" - Init/ Close log file
class NetDetectLogLib
{
public:
NetDetectLogLib() { INITIALIZE_LOG(); }
~NetDetectLogLib() { CLOSE_LOG(); }
};
} //NetDetectLog namespace
CHXNetCheck::CHXNetCheck(UINT32 timeout) :
XHXNetCheck(timeout)
,m_pRmaNetServices(0)
, m_pRmaTCPSocket(0)
, m_pContext(0)
, m_fConnected(FALSE)
, m_fFailed(FALSE)
, m_lRefCount(0)
#ifndef _WIN16
, m_hRasApiModule(0)
, m_pRasEnumConnections(0)
#endif
{
}
CHXNetCheck::~CHXNetCheck()
{
if (m_pContext)
m_pContext->Release();
m_pContext = NULL;
if (m_pRmaNetServices)
m_pRmaNetServices->Release();
m_pRmaNetServices = NULL;
if (m_pRmaTCPSocket)
m_pRmaTCPSocket->Release();
m_pRmaTCPSocket = NULL;
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::QueryInterface
// Purpose:
// Implement this to export the interfaces supported by your
// object.
//
STDMETHODIMP CHXNetCheck::QueryInterface(REFIID riid, void** ppvObj)
{
if (IsEqualIID(riid, IID_IUnknown))
{
AddRef();
*ppvObj = this;
return HXR_OK;
}
if (IsEqualIID(riid, IID_IHXTCPResponse))
{
AddRef();
*ppvObj = (IHXTCPResponse*)this;
return HXR_OK;
}
*ppvObj = NULL;
return HXR_NOINTERFACE;
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::AddRef
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(UINT32) CHXNetCheck::AddRef()
{
return InterlockedIncrement(&m_lRefCount);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::Release
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(UINT32) CHXNetCheck::Release()
{
if (InterlockedDecrement(&m_lRefCount) > 0)
{
return m_lRefCount;
}
delete this;
return 0;
}
/*
* IHXTCPResponse methods
*/
/************************************************************************
* Method:
* IHXTCPResponse::ConnectDone
* Purpose:
* A Connect operation has been completed or an error has occurred.
*/
STDMETHODIMP CHXNetCheck::ConnectDone (HX_RESULT status)
{
HX_ASSERT(m_fConnected == FALSE); // We shouldn't be getting called if
// we aren't expecting it.
if (status == HXR_OK)
m_fConnected = TRUE;
else
m_fFailed = TRUE;
return HXR_OK;
}
/************************************************************************
* Method:
* IHXTCPResponse::ReadDone
* Purpose:
* A Read operation has been completed or an error has occurred.
* The data is returned in the IHXBuffer.
*/
STDMETHODIMP CHXNetCheck::ReadDone (HX_RESULT status,
IHXBuffer* pBuffer)
{
HX_ASSERT(FALSE);
return HXR_OK;
}
/************************************************************************
* Method:
* IHXTCPResponse::WriteReady
* Purpose:
* This is the response method for WantWrite.
* If HX_RESULT is ok, then the TCP channel is ok to Write to.
*/
STDMETHODIMP CHXNetCheck::WriteReady (HX_RESULT status)
{
HX_ASSERT(FALSE);
return HXR_OK;
}
/************************************************************************
* Method:
* IHXTCPResponse::Closed
* Purpose:
* This method is called to inform you that the TCP channel has
* been closed by the peer or closed due to error.
*/
STDMETHODIMP CHXNetCheck::Closed(HX_RESULT status)
{
m_fConnected = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -