NetTunnelWindows/NetTunnelSDK/network.cpp

79 lines
2.2 KiB
C++
Raw Normal View History

#include "pch.h"
#include "tunnel.h"
#include "usrerr.h"
#include <strsafe.h>
#include <tchar.h>
#include <Iphlpapi.h>
#include <iostream>
#include "globalcfg.h"
#pragma comment(lib,"Iphlpapi.lib")
TUNNEL_API int GetAllNICInfo(PNIC_CONTENT pInfo, int* pItemCounts)
{
int nRel, id = 0;
IP_ADDR_STRING* pIpAddrString;
PIP_ADAPTER_INFO pIpAdapterInfo;
unsigned long stSize;
if (pItemCounts == nullptr || pInfo == nullptr)
{
return -ERR_INPUT_PARAMS;
}
pIpAdapterInfo = new IP_ADAPTER_INFO[NET_CARD_MAX];
stSize = sizeof(IP_ADAPTER_INFO) * NET_CARD_MAX;
// WIN32 API get net card information
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
if (ERROR_BUFFER_OVERFLOW == nRel)
{
delete[] pIpAdapterInfo;
return -ERR_MALLOC_MEMORY;
}
PIP_ADAPTER_INFO cur = pIpAdapterInfo;
while (cur)
{
StringCbCopy(pInfo[id].NetCardName, MAX_PATH, cur->AdapterName);
StringCbCopy(pInfo[id].NetCardDescription, MAX_PATH, cur->Description);
switch (cur->Type)
{
case MIB_IF_TYPE_ETHERNET:
pIpAddrString = &(cur->IpAddressList);
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_PATH, pIpAddrString->IpAddress.String);
StringCbCopy(pInfo[id].NetCardNetmask, MAX_PATH, pIpAddrString->IpMask.String);
break;
case MIB_IF_TYPE_OTHER:
case MIB_IF_TYPE_TOKENRING:
case MIB_IF_TYPE_FDDI:
case MIB_IF_TYPE_PPP:
case MIB_IF_TYPE_LOOPBACK:
case MIB_IF_TYPE_SLIP:
break;
default: // WIFI ,Unknown type
pIpAddrString = &(cur->IpAddressList);
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_PATH, pIpAddrString->IpAddress.String);
StringCbCopy(pInfo[id].NetCardNetmask, MAX_PATH, pIpAddrString->IpMask.String);
break;
}
StringCbPrintf(pInfo[id].NetCardMacAddr, MAX_PATH, "%02X:%02X:%02X:%02X:%02X:%02X",
cur->Address[0], cur->Address[1], cur->Address[2], cur->Address[3], cur->Address[4],
cur->Address[5]);
id++;
cur = cur->Next;
}
*pItemCounts = id;
delete[] pIpAdapterInfo;
return ERR_SUCCESS;
}