重新设置代码格式
This commit is contained in:
@@ -1,155 +1,147 @@
|
||||
#ifdef WIN32
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <windows.h>
|
||||
#include <iphlpapi.h>
|
||||
#include <netioapi.h>
|
||||
#include "Net_Adapter.h"
|
||||
#pragma comment(lib, "iphlpapi.lib")
|
||||
#if defined(os_is_win)
|
||||
#include <WinSock2.h>
|
||||
#include <Iphlpapi.h>
|
||||
#endif //! defined(os_is_win)
|
||||
|
||||
|
||||
|
||||
template <typename... Args>
|
||||
static std::string str_format(const std::string& format, Args... args) {
|
||||
auto size_buf = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
||||
std::unique_ptr<char[]> buf(new (std::nothrow) char[size_buf]);
|
||||
if (!buf) return "";
|
||||
std::snprintf(buf.get(), size_buf, format.c_str(), args...);
|
||||
return std::string(buf.get(), buf.get() + size_buf - 1);
|
||||
static std::string str_format(const std::string &format, Args... args) {
|
||||
auto size_buf = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
||||
std::unique_ptr<char[]> buf(new (std::nothrow) char[size_buf]);
|
||||
if (!buf)
|
||||
return "";
|
||||
std::snprintf(buf.get(), size_buf, format.c_str(), args...);
|
||||
return std::string(buf.get(), buf.get() + size_buf - 1);
|
||||
}
|
||||
namespace Psc {
|
||||
std::map<std::string, Net_Adapter_Info> get_net_adapter_info_map()
|
||||
{
|
||||
std::map<std::string, Net_Adapter_Info> ret_list;
|
||||
std::map<std::string, Net_Adapter_Info> get_net_adapter_info_map() {
|
||||
std::map<std::string, Net_Adapter_Info> ret_list;
|
||||
|
||||
std::unique_ptr< IP_ADAPTER_INFO> pai(new(std::nothrow) IP_ADAPTER_INFO);
|
||||
std::unique_ptr<IP_ADAPTER_INFO> pai(new (std::nothrow) IP_ADAPTER_INFO);
|
||||
|
||||
// 1. failed to apply space
|
||||
if (nullptr == pai || NULL == pai)
|
||||
return ret_list;
|
||||
// 1. failed to apply space
|
||||
if (nullptr == pai || NULL == pai)
|
||||
return ret_list;
|
||||
|
||||
// 2. to get the size of IP_ADAPTER_INFO structure
|
||||
unsigned long iai_size = sizeof(IP_ADAPTER_INFO);
|
||||
// 2. to get the size of IP_ADAPTER_INFO structure
|
||||
unsigned long iai_size = sizeof(IP_ADAPTER_INFO);
|
||||
|
||||
// 3.调用GetAdaptersInfo函数, 填充pIpAdapterInfo指针变量; 其中stSize参数既是一个输入量也是一个输出量
|
||||
auto ret_val = GetAdaptersInfo(pai.get(), &iai_size);
|
||||
// 3.调用GetAdaptersInfo函数, 填充pIpAdapterInfo指针变量;
|
||||
// 其中stSize参数既是一个输入量也是一个输出量
|
||||
auto ret_val = GetAdaptersInfo(pai.get(), &iai_size);
|
||||
|
||||
if (ERROR_BUFFER_OVERFLOW == ret_val)
|
||||
{
|
||||
pai.release();
|
||||
if (ERROR_BUFFER_OVERFLOW == ret_val) {
|
||||
pai.release();
|
||||
|
||||
//重新申请内存空间用来存储所有网卡信息
|
||||
pai.reset((IP_ADAPTER_INFO*)(new(std::nothrow) char[iai_size]));
|
||||
// 重新申请内存空间用来存储所有网卡信息
|
||||
pai.reset((IP_ADAPTER_INFO *)(new (std::nothrow) char[iai_size]));
|
||||
|
||||
if (nullptr == pai || NULL == pai)
|
||||
{
|
||||
return ret_list;
|
||||
}
|
||||
if (nullptr == pai || NULL == pai) {
|
||||
return ret_list;
|
||||
}
|
||||
|
||||
//再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
|
||||
ret_val = GetAdaptersInfo(pai.get(), &iai_size);
|
||||
}
|
||||
// 再次调用GetAdaptersInfo函数,填充pIpAdapterInfo指针变量
|
||||
ret_val = GetAdaptersInfo(pai.get(), &iai_size);
|
||||
}
|
||||
|
||||
if (ERROR_SUCCESS == ret_val)
|
||||
{
|
||||
// 3. to get information
|
||||
if (ERROR_SUCCESS == ret_val) {
|
||||
// 3. to get information
|
||||
|
||||
IP_ADAPTER_INFO *ppai = pai.get();
|
||||
IP_ADAPTER_INFO *ppai = pai.get();
|
||||
|
||||
while (ppai)
|
||||
{
|
||||
Net_Adapter_Info item;
|
||||
item.index = ppai->Index;
|
||||
item.name = std::string(ppai->AdapterName);
|
||||
item.description = std::string(ppai->Description);
|
||||
while (ppai) {
|
||||
Net_Adapter_Info item;
|
||||
item.index = ppai->Index;
|
||||
item.name = std::string(ppai->AdapterName);
|
||||
item.description = std::string(ppai->Description);
|
||||
|
||||
// dev
|
||||
std::string str_type;
|
||||
switch (ppai->Type)
|
||||
{
|
||||
case MIB_IF_TYPE_OTHER:
|
||||
str_type = {"OTHER"};
|
||||
break;
|
||||
// dev
|
||||
std::string str_type;
|
||||
switch (ppai->Type) {
|
||||
case MIB_IF_TYPE_OTHER:
|
||||
str_type = {"OTHER"};
|
||||
break;
|
||||
|
||||
case MIB_IF_TYPE_ETHERNET:
|
||||
str_type = { "ETHERNET" };
|
||||
break;
|
||||
case MIB_IF_TYPE_ETHERNET:
|
||||
str_type = {"ETHERNET"};
|
||||
break;
|
||||
|
||||
case MIB_IF_TYPE_TOKENRING:
|
||||
str_type = { "TOKENRING" };
|
||||
break;
|
||||
case MIB_IF_TYPE_TOKENRING:
|
||||
str_type = {"TOKENRING"};
|
||||
break;
|
||||
|
||||
case MIB_IF_TYPE_FDDI:
|
||||
str_type = { "FDDI" };
|
||||
break;
|
||||
case MIB_IF_TYPE_FDDI:
|
||||
str_type = {"FDDI"};
|
||||
break;
|
||||
|
||||
case MIB_IF_TYPE_PPP:
|
||||
str_type = { "PPP" };
|
||||
break;
|
||||
case MIB_IF_TYPE_PPP:
|
||||
str_type = {"PPP"};
|
||||
break;
|
||||
|
||||
case MIB_IF_TYPE_LOOPBACK:
|
||||
str_type = { "LOOPBACK" };
|
||||
break;
|
||||
case MIB_IF_TYPE_LOOPBACK:
|
||||
str_type = {"LOOPBACK"};
|
||||
break;
|
||||
|
||||
case MIB_IF_TYPE_SLIP:
|
||||
str_type = { "SLP" };
|
||||
break;
|
||||
case MIB_IF_TYPE_SLIP:
|
||||
str_type = {"SLP"};
|
||||
break;
|
||||
|
||||
default:
|
||||
str_type = { "" };
|
||||
break;
|
||||
}
|
||||
default:
|
||||
str_type = {""};
|
||||
break;
|
||||
}
|
||||
|
||||
item.dev_type = str_type;
|
||||
item.dev_type = str_type;
|
||||
|
||||
// mac
|
||||
std::string str_mac;
|
||||
for (DWORD i = 0; i < ppai->AddressLength; i++)
|
||||
{
|
||||
if (i < ppai->AddressLength - 1)
|
||||
str_mac += str_format("%02X-", ppai->Address[i]);
|
||||
else
|
||||
str_mac += str_format("%02X", ppai->Address[i]);
|
||||
}
|
||||
// mac
|
||||
std::string str_mac;
|
||||
for (DWORD i = 0; i < ppai->AddressLength; i++) {
|
||||
if (i < ppai->AddressLength - 1)
|
||||
str_mac += str_format("%02X-", ppai->Address[i]);
|
||||
else
|
||||
str_mac += str_format("%02X", ppai->Address[i]);
|
||||
}
|
||||
|
||||
item.mac = str_mac;
|
||||
item.mac = str_mac;
|
||||
|
||||
// ip information
|
||||
|
||||
IP_ADDR_STRING *pial = &(ppai->IpAddressList);
|
||||
|
||||
// ip information
|
||||
for (;;) {
|
||||
IPV4 ii_item;
|
||||
if (NULL != pial && nullptr != pial) {
|
||||
ii_item.inet = std::string(pial->IpAddress.String);
|
||||
ii_item.subnet_mask = std::string(pial->IpMask.String);
|
||||
ii_item.gate = std::string(pai->GatewayList.IpAddress.String);
|
||||
|
||||
IP_ADDR_STRING *pial = &(ppai->IpAddressList);
|
||||
item.ipv4s.push_back(ii_item);
|
||||
pial = pial->Next;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
IPV4 ii_item;
|
||||
if (NULL != pial && nullptr != pial)
|
||||
{
|
||||
ii_item.inet = std::string(pial->IpAddress.String);
|
||||
ii_item.subnet_mask = std::string(pial->IpMask.String);
|
||||
ii_item.gate = std::string(pai->GatewayList.IpAddress.String);
|
||||
ret_list[item.name] = item;
|
||||
|
||||
item.ipv4s.push_back(ii_item);
|
||||
pial = pial->Next;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
ppai = ppai->Next;
|
||||
} // end while
|
||||
} else {
|
||||
; // error
|
||||
}
|
||||
|
||||
|
||||
ret_list[item.name] = item;
|
||||
|
||||
|
||||
ppai = ppai->Next;
|
||||
} // end while
|
||||
}
|
||||
else
|
||||
{
|
||||
;// error
|
||||
}
|
||||
|
||||
return ret_list;
|
||||
}
|
||||
return ret_list;
|
||||
}
|
||||
|
||||
} // namespace Psc
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user