📄 adinfo.c
字号:
memset(&(AdInfo->NetworkAddresses[AdInfo->NNetworkAddresses].SubnetMask), 0, sizeof(struct sockaddr_storage));
memset(&(AdInfo->NetworkAddresses[AdInfo->NNetworkAddresses].Broadcast), 0, sizeof(struct sockaddr_storage));
AdInfo->NNetworkAddresses ++;
}
}
}
}
GlobalFreePtr(AdBuffer);
return TRUE;
}
#endif // _WINNT4
/*!
\brief Adds an entry to the adapter description list, gathering its values from the IP Helper API.
\param IphAd PIP_ADAPTER_INFO IP Helper API structure containing the parameters of the adapter that must be added to the list.
\return If the function succeeds, the return value is TRUE.
\note we suppose that we are called after having acquired the AdaptersInfoMutex mutex
*/
#ifndef _WINNT4
BOOLEAN AddAdapterIPH(PIP_ADAPTER_INFO IphAd)
{
PIP_ADAPTER_INFO AdList = NULL;
ULONG OutBufLen=0;
PADAPTER_INFO TmpAdInfo, SAdInfo;
PIP_ADDR_STRING TmpAddrStr;
UINT i;
struct sockaddr_in *TmpAddr;
CHAR TName[256];
LPADAPTER adapter;
PWCHAR UAdName;
// Create the NPF device name from the original device name
strcpy(TName, "\\Device\\NPF_");
_snprintf(TName + 12, ADAPTER_NAME_LENGTH - 12, "%s", IphAd->AdapterName);
// Scan the adapters list to see if this one is already present
for(SAdInfo = AdaptersInfoList; SAdInfo != NULL; SAdInfo = SAdInfo->Next)
{
if(strcmp(TName, SAdInfo->Name) == 0)
{
ODS("PacketGetAdaptersIPH: Adapter already present in the list\n");
goto SkipAd;
}
}
if(IphAd->Type == IF_TYPE_PPP || IphAd->Type == IF_TYPE_SLIP)
{
if (!WanPacketTestAdapter())
goto SkipAd;
}
else
{
//convert the string to unicode, as OpenAdapterNPF accepts unicode strings, only.
UAdName = SChar2WChar(TName);
if (UAdName == NULL)
{
ODS("AddAdapterIPH: unable to convert an ASCII string to UNICODE\n");
goto SkipAd;
}
adapter = PacketOpenAdapterNPF((PCHAR)UAdName);
GlobalFreePtr(UAdName);
if(adapter == NULL)
{
// We are not able to open this adapter. Skip to the next one.
ODS("PacketGetAdaptersIPH: unable to open the adapter\n");
goto SkipAd;
}
else
{
PacketCloseAdapter(adapter);
}
}
//
// Adapter valid and not yet present in the list. Allocate the ADAPTER_INFO structure
//
TmpAdInfo = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(ADAPTER_INFO));
if (TmpAdInfo == NULL) {
ODS("PacketGetAdaptersIPH: GlobalAlloc Failed\n");
return FALSE;
}
// Copy the device name
strcpy(TmpAdInfo->Name, TName);
// Copy the description
_snprintf(TmpAdInfo->Description, ADAPTER_DESC_LENGTH, "%s", IphAd->Description);
// Copy the MAC address
TmpAdInfo->MacAddressLen = IphAd->AddressLength;
memcpy(TmpAdInfo->MacAddress,
IphAd->Address,
(MAX_MAC_ADDR_LENGTH<MAX_ADAPTER_ADDRESS_LENGTH)? MAX_MAC_ADDR_LENGTH:MAX_ADAPTER_ADDRESS_LENGTH);
// Calculate the number of IP addresses of this interface
for(TmpAddrStr = &IphAd->IpAddressList, i = 0; TmpAddrStr != NULL; TmpAddrStr = TmpAddrStr->Next, i++)
{
}
TmpAdInfo->NetworkAddresses = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, MAX_NETWORK_ADDRESSES * sizeof(npf_if_addr));
if (TmpAdInfo->NetworkAddresses == NULL) {
ODS("PacketGetAdaptersIPH: GlobalAlloc Failed\n");
GlobalFreePtr(TmpAdInfo);
return FALSE;
}
// Scan the addresses, convert them to addrinfo structures and put each of them in the list
for(TmpAddrStr = &IphAd->IpAddressList, i = 0; TmpAddrStr != NULL; TmpAddrStr = TmpAddrStr->Next)
{
TmpAddr = (struct sockaddr_in *)&(TmpAdInfo->NetworkAddresses[i].IPAddress);
if((TmpAddr->sin_addr.S_un.S_addr = inet_addr(TmpAddrStr->IpAddress.String))!= INADDR_NONE)
{
TmpAddr->sin_family = AF_INET;
TmpAddr = (struct sockaddr_in *)&(TmpAdInfo->NetworkAddresses[i].SubnetMask);
TmpAddr->sin_addr.S_un.S_addr = inet_addr(TmpAddrStr->IpMask.String);
TmpAddr->sin_family = AF_INET;
TmpAddr = (struct sockaddr_in *)&(TmpAdInfo->NetworkAddresses[i].Broadcast);
TmpAddr->sin_addr.S_un.S_addr = 0xffffffff; // Consider 255.255.255.255 as broadcast address since IP Helper API doesn't provide information about it
TmpAddr->sin_family = AF_INET;
i++;
}
}
TmpAdInfo->NNetworkAddresses = i;
// Now Add IPv6 Addresses
PacketAddIP6Addresses(TmpAdInfo);
if(IphAd->Type == IF_TYPE_PPP || IphAd->Type == IF_TYPE_SLIP)
{
// NdisWan adapter
TmpAdInfo->Flags = INFO_FLAG_NDISWAN_ADAPTER;
}
// Update the AdaptersInfo list
TmpAdInfo->Next = AdaptersInfoList;
AdaptersInfoList = TmpAdInfo;
SkipAd:
return TRUE;
}
#endif // _WINNT4
/*!
\brief Updates the list of the adapters querying the IP Helper API.
\return If the function succeeds, the return value is nonzero.
This function populates the list of adapter descriptions, retrieving the information from a query to
the IP Helper API. The IP Helper API is used as a support of the standard registry query method to obtain
adapter information, so PacketGetAdaptersIPH() add only information about the adapters that were not
found by PacketGetAdapters().
*/
#ifndef _WINNT4
BOOLEAN PacketGetAdaptersIPH()
{
PIP_ADAPTER_INFO AdList = NULL;
PIP_ADAPTER_INFO TmpAd;
ULONG OutBufLen=0;
ODS("PacketGetAdaptersIPH\n");
// Find the size of the buffer filled by GetAdaptersInfo
//NOTE: this function seems to be leaky, at least on
//Windows XP Pro SP1.
if(GetAdaptersInfo(AdList, &OutBufLen) == ERROR_NOT_SUPPORTED)
{
ODS("IP Helper API not supported on this system!\n");
return FALSE;
}
// Allocate the buffer
AdList = GlobalAllocPtr(GMEM_MOVEABLE, OutBufLen);
if (AdList == NULL) {
ODS("PacketGetAdaptersIPH: GlobalAlloc Failed\n");
return FALSE;
}
// Retrieve the adapters information using the IP helper API
GetAdaptersInfo(AdList, &OutBufLen);
// Scan the list of adapters obtained from the IP helper API, create a new ADAPTER_INFO
// structure for every new adapter and put it in our global list
for(TmpAd = AdList; TmpAd != NULL; TmpAd = TmpAd->Next)
{
AddAdapterIPH(TmpAd);
}
return TRUE;
}
#endif // _WINNT4
/*!
\brief Adds an entry to the adapter description list.
\param AdName Name of the adapter to add
\return If the function succeeds, the return value is nonzero.
Used by PacketGetAdapters(). Queries the registry to fill the PADAPTER_INFO describing the new adapter.
*/
BOOLEAN AddAdapter(PCHAR AdName)
{
//this function should acquire the AdaptersInfoMutex, since it's NOT called with an ADAPTER_INFO as parameter
DWORD RegKeySize=0;
LONG Status;
LPADAPTER adapter;
PPACKET_OID_DATA OidData;
int i=0;
PADAPTER_INFO TmpAdInfo;
PADAPTER_INFO TAdInfo;
PWCHAR UAdName;
ODS("AddAdapter\n");
WaitForSingleObject(AdaptersInfoMutex, INFINITE);
for(TAdInfo = AdaptersInfoList; TAdInfo != NULL; TAdInfo = TAdInfo->Next)
{
if(strcmp(AdName, TAdInfo->Name) == 0)
{
ODS("AddAdapter: Adapter already present in the list\n");
ReleaseMutex(AdaptersInfoMutex);
return TRUE;
}
}
UAdName = SChar2WChar(AdName);
//here we could have released the mutex, but what happens if two threads try to add the same adapter?
//The adapter would be duplicated on the linked list
// Try to Open the adapter
adapter = PacketOpenAdapterNPF((PCHAR)UAdName);
GlobalFreePtr(UAdName);
if(adapter == NULL)
{
// We are not able to open this adapter. Skip to the next one.
ReleaseMutex(AdaptersInfoMutex);
return FALSE;
}
// Allocate a buffer to get the vendor description from the driver
OidData = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,512);
if (OidData == NULL) {
ODS("AddAdapter: GlobalAlloc Failed\n");
PacketCloseAdapter(adapter);
ReleaseMutex(AdaptersInfoMutex);
return FALSE;
}
//
// PacketOpenAdapter was succesful. Consider this a valid adapter and allocate an entry for it
// In the adapter list
//
TmpAdInfo = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(ADAPTER_INFO));
if (TmpAdInfo == NULL)
{
ODS("AddAdapter: GlobalAlloc Failed\n");
GlobalFreePtr(OidData);
PacketCloseAdapter(adapter);
ReleaseMutex(AdaptersInfoMutex);
return FALSE;
}
// Copy the device name
strcpy(TmpAdInfo->Name, AdName);
// Retrieve the adapter description querying the NIC driver
OidData->Oid = OID_GEN_VENDOR_DESCRIPTION;
OidData->Length = 256;
ZeroMemory(OidData->Data, 256);
Status = PacketRequest(adapter, FALSE, OidData);
if(Status==0 || ((char*)OidData->Data)[0]==0)
{
ODS("AddAdapter: unable to get a valid adapter description from the NIC driver\n");
}
ODSEx("Adapter Description=%s\n\n",OidData->Data);
// Copy the description
strcpy(TmpAdInfo->Description, OidData->Data);
PacketGetLinkLayerFromRegistry(adapter, &(TmpAdInfo->LinkLayer));
// Retrieve the adapter description querying the NIC driver
OidData->Oid = OID_802_3_CURRENT_ADDRESS; // XXX At the moment only Ethernet is supported.
// Waiting a patch to support other Link Layers
OidData->Length = 256;
ZeroMemory(OidData->Data, 256);
Status = PacketRequest(adapter, FALSE, OidData);
if(Status)
{
memcpy(TmpAdInfo->MacAddress, OidData->Data, 6);
TmpAdInfo->MacAddressLen = 6;
}
else
{
memset(TmpAdInfo->MacAddress, 0, 6);
TmpAdInfo->MacAddressLen = 0;
}
TmpAdInfo->NetworkAddresses = GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, MAX_NETWORK_ADDRESSES * sizeof(npf_if_addr));
if (TmpAdInfo->NetworkAddresses == NULL) {
ODS("AddAdapter: GlobalAlloc Failed\n");
PacketCloseAdapter(adapter);
GlobalFreePtr(OidData);
GlobalFreePtr(TmpAdInfo);
ReleaseMutex(AdaptersInfoMutex);
return FALSE;
}
TmpAdInfo->NNetworkAddresses = MAX_NETWORK_ADDRESSES;
if(!PacketGetAddressesFromRegistry((LPTSTR)TmpAdInfo->Name, TmpAdInfo->NetworkAddresses, &TmpAdInfo->NNetworkAddresses))
{
#ifndef _WINNT4
// Try to see if the interface has some IPv6 addresses
TmpAdInfo->NNetworkAddresses = 0; // We have no addresses because PacketGetAddressesFromRegistry() failed
if(!PacketAddIP6Addresses(TmpAdInfo))
{
#endif // _WINNT4
GlobalFreePtr(TmpAdInfo->NetworkAddresses);
TmpAdInfo->NetworkAddresses = NULL;
TmpAdInfo->NNetworkAddresses = 0;
#ifndef _WINNT4
}
#endif // _WINNT4
}
#ifndef _WINNT4
// Now Add IPv6 Addresses
PacketAddIP6Addresses(TmpAdInfo);
#endif // _WINNT4
TmpAdInfo->Flags = INFO_FLAG_NDIS_ADAPTER; // NdisWan adapters are not exported by the NPF driver,
// therefore it's impossible to see them here
// Update the AdaptersInfo list
TmpAdInfo->Next = AdaptersInfoList;
AdaptersInfoList = TmpAdInfo;
PacketCloseAdapter(adapter);
GlobalFreePtr(OidData);
ReleaseMutex(AdaptersInfoMutex);
return TRUE;
}
/*!
\brief Updates the list of the adapters querying the registry.
\return If the function succeeds, the return value is nonzero.
This function populates the list of adapter descriptions, retrieving the information from the registry.
*/
BOOLEAN PacketGetAdapters()
{
HKEY LinkageKey,AdapKey, OneAdapKey;
DWORD RegKeySize=0;
LONG Status;
ULONG Result;
INT i=0,k;
DWORD dim;
DWORD RegType;
WCHAR TName[256];
CHAR TAName[256];
TCHAR AdapName[256];
PTSTR BpStr;
ODS("PacketGetAdapters\n");
Status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}"),
0,
KEY_READ,
&AdapKey);
if ( Status != ERROR_SUCCESS ){
ODS("PacketGetAdapters: RegOpenKeyEx ( Class\\{networkclassguid} ) Failed\n");
goto nt4;
}
i=0;
ODS("PacketGetAdapters: Cycling through the adapters:\n");
//
// Cycle through the entries inside the {4D36E972-E325-11CE-BFC1-08002BE10318} key
// To get the names of the adapters
//
while((Result = RegEnumKey(AdapKey, i, AdapName, sizeof(AdapName)/2)) == ERROR_SUCCESS)
{
i++;
ODSEx(" %d) ", i);
//
// Get the adapter name from the registry key
//
Status=RegOpenKeyEx(AdapKey, AdapName, 0, KEY_READ, &OneAdapKey);
if ( Status != ERROR_SUCCESS )
{
ODS("PacketGetAdapters: RegOpenKeyEx ( OneAdapKey ) Failed\n");
continue;
}
Status=RegOpenKeyEx(OneAdapKey, L"Linkage", 0, KEY_READ, &LinkageKey);
if (Status != ERROR_SUCCESS)
{
RegCloseKey(OneAdapKey);
ODS("PacketGetAdapters: RegOpenKeyEx ( LinkageKey ) Failed\n");
continue;
}
dim = sizeof(TName);
Status=RegQueryValueEx(LinkageKey,
L"Export",
NULL,
NULL,
(PBYTE)TName,
&dim);
if(Status != ERROR_SUCCESS)
{
RegCloseKey(OneAdapKey);
RegCloseKey(LinkageKey);
ODS("Name = SKIPPED (error reading the key)\n");
continue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -