📄 hxnetif.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 "hxcom.h"
#include <string.h>
#include <stdio.h>
#ifndef _WINCE
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif /* _WINCE */
#include <windows.h>
#include "hlxclib/sys/socket.h"
#include "hlxosstr.h"
#include "hxresult.h"
#include "hxslist.h"
#include "netbyte.h"
#include "hxengin.h"
#include "hxnetif.h"
#define CHECK_ADDR_CHANGE_INTERVAL 60000
HXNetInterface::HXNetInterface(IUnknown* pContext)
: m_lRefCount(0)
, m_bInitialized(FALSE)
, m_hIPLib(NULL)
, m_hWinSockLib(NULL)
, m_hAddrChangeEvent(NULL)
, m_pScheduler(NULL)
, m_pAddrChangeCallback(NULL)
, m_pNetInterfaceList(NULL)
, m_pSinkList(NULL)
, _pGetIfTable(NULL)
, _pGetIpAddrTable(NULL)
, _pNotifyAddrChange(NULL)
, _hxWSAStartup(NULL)
, _hxWSACleanup(NULL)
#ifdef _WINCE
, _pInetAddr(NULL)
,_pGetAdaptersInfo(NULL)
#else
, _hxsocket(NULL)
, _hxclosesocket(NULL)
, _raWSAIoctl(NULL)
#endif /* _WINCE */
, m_handle(NULL)
{
m_pContext = pContext;
HX_ADDREF(m_pContext);
}
HXNetInterface::~HXNetInterface()
{
Close();
}
STDMETHODIMP
HXNetInterface::QueryInterface(REFIID riid, void**ppvObj)
{
if (IsEqualIID(riid, IID_IUnknown))
{
AddRef();
*ppvObj = this;
return HXR_OK;
}
else if (IsEqualIID(riid, IID_IHXNetInterfaces))
{
AddRef();
*ppvObj = (IHXNetInterfaces*)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_(ULONG32)
HXNetInterface::AddRef()
{
return InterlockedIncrement(&m_lRefCount);
}
/////////////////////////////////////////////////////////////////////////
// Method:
// IUnknown::Release
// Purpose:
// Everyone usually implements this the same... feel free to use
// this implementation.
//
STDMETHODIMP_(ULONG32)
HXNetInterface::Release()
{
if (InterlockedDecrement(&m_lRefCount) > 0)
{
return m_lRefCount;
}
delete this;
return 0;
}
STDMETHODIMP
HXNetInterface::UpdateNetInterfaces(void)
{
HX_RESULT rc = HXR_OK;
if (!m_bInitialized)
{
m_bInitialized = TRUE;
rc = RetrieveNetInterface0(m_pNetInterfaceList);
if (!m_pNetInterfaceList || m_pNetInterfaceList->GetCount() == 0)
{
// try the generic method
RetrieveNetInterface1(m_pNetInterfaceList);
}
if (_pNotifyAddrChange)
{
m_hAddrChangeEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
#ifndef _WINCE
m_overLapped.Internal = NULL;
m_overLapped.InternalHigh = NULL;
m_overLapped.Offset = 0;
m_overLapped.OffsetHigh = 0;
m_overLapped.hEvent = m_hAddrChangeEvent;
if (ERROR_IO_PENDING == _pNotifyAddrChange(&m_handle, &m_overLapped))
#else
// Overlapped param is not supported in wince 3.0 in the second param to NotifyAddrChange
if(NO_ERROR == _pNotifyAddrChange(&m_hAddrChangeEvent, NULL))
#endif /* _WINCE */
{
if (!m_pScheduler)
{
m_pContext->QueryInterface(IID_IHXScheduler, (void**)&m_pScheduler);
}
if (m_pScheduler)
{
m_pAddrChangeCallback = new AddrChangeCallback;
m_pAddrChangeCallback->m_pParent = this;
m_pAddrChangeCallback->AddRef();
m_pAddrChangeCallback->m_bIsCallbackPending = TRUE;
m_pAddrChangeCallback->m_PendingHandle = m_pScheduler->RelativeEnter(m_pAddrChangeCallback, CHECK_ADDR_CHANGE_INTERVAL);
}
}
}
}
else if (IsNetInterfaceChanged())
{
if (m_pSinkList)
{
CHXSimpleList::Iterator ndx = m_pSinkList->Begin();
for (; ndx != m_pSinkList->End(); ++ndx)
{
IHXNetInterfacesAdviseSink* pSink = (IHXNetInterfacesAdviseSink*) (*ndx);
pSink->NetInterfacesUpdated();
}
}
}
return rc;
}
STDMETHODIMP_(UINT32)
HXNetInterface::GetNumOfNetInterfaces()
{
if (!m_bInitialized)
{
UpdateNetInterfaces();
}
return m_pNetInterfaceList ? m_pNetInterfaceList->GetCount() : 0;
}
STDMETHODIMP
HXNetInterface::GetNetInterfaces(UINT16 lIndex,
REF(NIInfo*) pNIInfo)
{
HX_RESULT rc = HXR_OK;
int i = 0;
CHXSimpleList::Iterator iter;
pNIInfo = NULL;
if (!m_bInitialized)
{
UpdateNetInterfaces();
}
if (m_pNetInterfaceList)
{
iter = m_pNetInterfaceList->Begin();
for (; iter != m_pNetInterfaceList->End(); ++iter, ++i)
{
NIInfo* pInfo = (NIInfo*)(*iter);
if (i == lIndex)
{
pNIInfo = pInfo;
break;
}
}
}
if (!pNIInfo)
{
rc = HXR_FAILED;
}
return rc;
}
STDMETHODIMP
HXNetInterface::AddAdviseSink(IHXNetInterfacesAdviseSink* pSink)
{
HX_RESULT rc = HXR_OK;
if (!m_pSinkList)
{
m_pSinkList = new CHXSimpleList();
}
pSink->AddRef();
m_pSinkList->AddTail(pSink);
return rc;
}
STDMETHODIMP
HXNetInterface::RemoveAdviseSink(IHXNetInterfacesAdviseSink* pSink)
{
HX_RESULT rc = HXR_OK;
LISTPOSITION lPosition = m_pSinkList->Find(pSink);
if (!lPosition)
{
rc = HXR_UNEXPECTED;
goto cleanup;
}
m_pSinkList->RemoveAt(lPosition);
pSink->Release();
cleanup:
return rc;
}
HX_RESULT
HXNetInterface::RetrieveNetInterface0(CHXSimpleList*& pNetInterfaceList)
{
HX_RESULT rc = HXR_OK;
char temp;
DWORD dwFlags = 0;
UINT32 ulSize = 1;
UINT32 ulNetAddress = 0;
UINT32 ulNetMask = 0;
DWORD i = 0;
DWORD j = 0;
PMIB_IFTABLE ifTable = NULL;
PMIB_IPADDRTABLE ipAddrTable = NULL;
NIType type = UNKNOWN;
NIInfo* pNIInfo = NULL;
if (!m_hIPLib)
{
m_hIPLib = LoadLibrary(OS_STRING("IPHLPAPI.DLL"));
if (m_hIPLib)
{
_pGetIfTable = (GETIFTABLE)GetProcAddress(m_hIPLib, OS_STRING("GetIfTable"));
_pGetIpAddrTable = (GETIPADDRTABLE)GetProcAddress(m_hIPLib, OS_STRING("GetIpAddrTable"));
_pNotifyAddrChange = (NOTIFYADDRCHANGE)GetProcAddress(m_hIPLib, OS_STRING("NotifyAddrChange"));
}
}
if (!_pGetIfTable ||
!_pGetIpAddrTable ||
!_pNotifyAddrChange)
{
rc = HXR_FAILED;
goto cleanup;
}
// get interface table
rc = _pGetIfTable(PMIB_IFTABLE(&temp),&ulSize,TRUE);
if(HXR_OK == rc)
{
rc = HXR_FAILED;
goto cleanup;
}
ifTable = (PMIB_IFTABLE) new char[ulSize];
rc = _pGetIfTable(ifTable, &ulSize, TRUE);
if (HXR_OK != rc)
{
rc = HXR_FAILED;
goto cleanup;
}
// get ip addresses table
ulSize = 1;
rc = _pGetIpAddrTable(PMIB_IPADDRTABLE(&temp), &ulSize, TRUE);
if (HXR_OK == rc)
{
rc = HXR_FAILED;
goto cleanup;
}
ipAddrTable = (PMIB_IPADDRTABLE) new char[ulSize];
rc = _pGetIpAddrTable(ipAddrTable, &ulSize, TRUE);
if (HXR_OK != rc)
{
rc = HXR_FAILED;
goto cleanup;
}
// check if interface exists
if (0 == ifTable->dwNumEntries)
{
rc = HXR_FAILED;
goto cleanup;
}
// enumerate all interfaces
for (i = 0; i < ifTable->dwNumEntries; i++)
{
// ignore the LOOPBACK
if (ifTable->table[i].dwType == MIB_IF_TYPE_LOOPBACK)
{
continue;;
}
else if (ifTable->table[i].dwType == MIB_IF_TYPE_PPP &&
ifTable->table[i].dwOperStatus)
{
type = POINTTOPOINT;
}
else if (ifTable->table[i].dwOperStatus)
{
type = BROADCAST;
}
else
{
continue;
}
// get corresponding IP
for(j=0; j < ipAddrTable->dwNumEntries; j++)
{
if(ipAddrTable->table[j].dwIndex == ifTable->table[i].dwIndex)
{
ulNetAddress = ipAddrTable->table[j].dwAddr;
ulNetMask = ipAddrTable->table[j].dwMask;
break;
}
}
// ignore net interface with invalid IP/Mask
if (ulNetAddress == 0 || ulNetMask == 0)
{
continue;
}
pNIInfo = new NIInfo;
pNIInfo->bActive = TRUE;
pNIInfo->type = type;
pNIInfo->ulNetAddress = ulNetAddress;
pNIInfo->ulNetMask = ulNetMask;
if (!pNetInterfaceList)
{
pNetInterfaceList = new CHXSimpleList();
}
pNetInterfaceList->AddTail(pNIInfo);
}
cleanup:
if (ifTable)
{
delete[] ((char*)ifTable);
}
if (ipAddrTable)
{
delete[] ((char*)ipAddrTable);
}
return rc;
}
HX_RESULT
HXNetInterface::RetrieveNetInterface1(CHXSimpleList*& pNetInterfaceList)
{
HX_RESULT rc = HXR_OK;
int iStructures = 0;
int iPosition = 0;
long lFlags = 0;
UINT32 ulBytes = 0;
UINT32 ulNetAddress = 0;
UINT32 ulNetMask = 0;
SOCKET s = 0;
WORD wVersionRequested = 0;
NIType type = UNKNOWN;
NIInfo* pNIInfo = NULL;
WSADATA wsaData;
#ifdef _WINCE
PIP_ADAPTER_INFO pAdapterInfo = NULL;
PIP_ADDR_STRING pAddrList = NULL;
ULONG ulSize = 0;
DWORD dwResult = ERROR_NOT_SUPPORTED;
#else
INTERFACE_INFO* pInfo = NULL;
#endif
if (!m_hWinSockLib)
{
#ifdef _WINCE
m_hWinSockLib = LoadLibrary(OS_STRING("winsock.dll"));
#else
m_hWinSockLib = LoadLibrary(OS_STRING("ws2_32.dll"));
#endif /* _WINCE */
if (m_hWinSockLib)
{
_hxWSAStartup = (WSASTARTUP)GetProcAddress(m_hWinSockLib, OS_STRING("WSAStartup"));
_hxWSACleanup = (WSACLEANUP)GetProcAddress(m_hWinSockLib, OS_STRING("WSACleanup"));
#ifdef _WINCE
_pInetAddr = (INET_ADDR) ::GetProcAddress(m_hWinSockLib, OS_STRING("inet_addr"));
#else
_hxsocket = (HXSOCKET)GetProcAddress(m_hWinSockLib, OS_STRING("socket"));
_hxclosesocket = (CLOSESOCKET)GetProcAddress(m_hWinSockLib, OS_STRING("closesocket"));
_raWSAIoctl = (WSAIOCTL)GetProcAddress(m_hWinSockLib, OS_STRING("WSAIoctl"));
#endif /* _WINCE */
}
}
#ifdef _WINCE
if (!_hxWSAStartup ||
!_hxWSACleanup ||
!_pInetAddr)
#else
if (!_hxsocket ||
!_hxclosesocket ||
!_hxWSAStartup ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -