print_ipaddress windows: Ip, mac

#include "Iphlpapi.h"

#pragma comment(lib, "Iphlpapi.lib")


bool Wrapper::print_ipaddress()

{
DWORD rv, size;
PIP_ADAPTER_ADDRESSES adapter_addresses, aa;
PIP_ADAPTER_UNICAST_ADDRESS ua;


rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &size);
//rv = GetAdaptersAddresses(AF_INET, 0,  NULL, NULL, &size);


if (rv != ERROR_BUFFER_OVERFLOW) {
ASLOG_tMsgA(LogLevel_KeyInfo, "GetAdaptersAddresses() failed...");
return false;
}
adapter_addresses = (PIP_ADAPTER_ADDRESSES)malloc(size);


rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addresses, &size);
//rv = GetAdaptersAddresses(AF_INET, 0,  NULL, adapter_addresses, &size);


if (rv != ERROR_SUCCESS) {
ASLOG_tMsgA(LogLevel_KeyInfo, "GetAdaptersAddresses() failed...");
free(adapter_addresses);
return false;
}


for (aa = adapter_addresses; aa != NULL; aa = aa->Next) {


if (aa->OperStatus == IfOperStatusUp)  
{  
ASLOG_tMsgA(LogLevel_KeyInfo, "Status: active\n");
}  
else  
{  
ASLOG_tMsgA(LogLevel_KeyInfo, "Status: deactive\n");



IP_ADAPTER_DNS_SERVER_ADDRESS *pDnServer = aa->FirstDnsServerAddress;
if(pDnServer)  
{
char buff[100];  
DWORD bufflen=100;
sockaddr_in *sa_in = (sockaddr_in *)pDnServer->Address.lpSockaddr;  
ASLOG_tMsgA(LogLevel_KeyInfo, "DNS:%s\n",inet_ntop(AF_INET,&(sa_in->sin_addr),buff,bufflen));  



//if (aa->PhysicalAddress[0] != 0)
{
CHAR szAddress[MAX_PATH] = {0};
//GetStrMacAddress(aa->PhysicalAddress, szAddress);
BYTE* PhysicalAddress = aa->PhysicalAddress;


sprintf(szAddress, "%02X-%02X-%02X-%02X-%02X-%02X",
PhysicalAddress[0], PhysicalAddress[1], PhysicalAddress[2], PhysicalAddress[3],
PhysicalAddress[4], PhysicalAddress[5]);


//MessageBoxA(NULL, szAddress, "MAC Address", MB_OK);
ASLOG_tMsgA(LogLevel_KeyInfo, "MAC Address: %s", szAddress);
}


//print_adapter(aa);
ASLOG_tMsgW(LogLevel_KeyInfo, L"adapter_name:%s\n", aa->FriendlyName);
for (ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) {
//print_addr(ua);


char buf[BUFSIZ] = {0};
int family = ua->Address.lpSockaddr->sa_family;
memset(buf, 0, BUFSIZ);
//getnameinfo(ua->Address.lpSockaddr, ua->Address.iSockaddrLength, buf, sizeof(buf), NULL, 0,NI_NUMERICHOST);

sprintf(buf, "%d.%d.%d.%d",
(BYTE)ua->Address.lpSockaddr->sa_data[2], 
(BYTE)ua->Address.lpSockaddr->sa_data[3], 
(BYTE)ua->Address.lpSockaddr->sa_data[4], 
(BYTE)ua->Address.lpSockaddr->sa_data[5]);
ASLOG_tMsgA(LogLevel_KeyInfo, "%s: %s\n", family == AF_INET ? "IPv4":"IPv6", buf);
}
}


free(adapter_addresses);


return true;
}


void Wrapper::getdIpMacAddr(char* szIpAddr, BYTE* szMacAddr)
{
DWORD dwBufLen = 0;
DWORD dwStatus = GetAdaptersInfo(
NULL,
&dwBufLen);


IP_ADAPTER_INFO* AdapterInfo = NULL;
if( dwStatus == ERROR_BUFFER_OVERFLOW ) {
AdapterInfo = ( IP_ADAPTER_INFO* )malloc( dwBufLen );
dwStatus = ::GetAdaptersInfo( AdapterInfo, &dwBufLen );
}
else
{
return;
}


//No network card? Other error?
if(dwStatus != ERROR_SUCCESS)
{
free(AdapterInfo);
return;
}


PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
//char szMacAddr[MAX_ADAPTER_ADDRESS_LENGTH] = {0};
//char szIpAddr[512] = {0};
DWORD dwMinIndex = FLS_OUT_OF_INDEXES;
while(pAdapterInfo)
{
if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
{
if (pAdapterInfo->Index < dwMinIndex)
{
dwMinIndex = pAdapterInfo->Index;


UINT nAddressLen = pAdapterInfo->AddressLength;
memset(szMacAddr, 0, MAX_ADAPTER_ADDRESS_LENGTH);
//memcpy(szMacAddr, pAdapterInfo->Address, sizeof(pAdapterInfo->Address));
for (int i = 0; i < (int) nAddressLen && szMacAddr; i++)
{
szMacAddr[i] = pAdapterInfo->Address[i];
}
if (nAddressLen >= 6)
{
char szMacOutput[512] = {0};
sprintf_s(szMacOutput, sizeof(szMacOutput), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x"
, pAdapterInfo->Address[0]
, pAdapterInfo->Address[1]
, pAdapterInfo->Address[2]
, pAdapterInfo->Address[3]
, pAdapterInfo->Address[4]
, pAdapterInfo->Address[5]
);


ASLOG_tMsgA(LogLevel_KeyInfo, "MAC: %s\n", szMacOutput);
}


IP_ADDR_STRING* pIPAddrNext = &( pAdapterInfo->IpAddressList );
if ( pIPAddrNext && szIpAddr) {// not while->only get the first IP addr
memset(szIpAddr, 0, sizeof(szIpAddr));
memcpy(szIpAddr, pIPAddrNext->IpAddress.String, sizeof(pIPAddrNext->IpAddress.String));
ASLOG_tMsgA(LogLevel_KeyInfo, "IP: %s\nSubnet: %s\n", pIPAddrNext->IpAddress.String, pIPAddrNext->IpMask.String);
pIPAddrNext = pIPAddrNext->Next;
}
}
}
pAdapterInfo = pAdapterInfo->Next;
}
free(AdapterInfo);
}

你可能感兴趣的:(print_ipaddress windows: Ip, mac)