Windows網路程式設計(六):IP Helper
阿新 • • 發佈:2018-11-27
IP Helper是Windows系統與IP配置和管理的重要介面,通過IP Helper 可以獲得很多跟網路配置相關的資訊。比如說本機IP、閘道器設定、網絡卡數量和連線資訊。
#include <windows.h>
#include "iphlpapi.h"
/* 全域性變數 */
ULONG ulOutBufLen;
DWORD dwRetVal;
HANDLE hHeap = GetProcessHeap();
/* 巨集定義*/
#define MyAlloc(size) HeapAlloc(hHeap, 0, size)
#define MyFree(ptr) HeapFree(hHeap, 0, ptr)
/*************************************
* main
* 功能 演示IP Helper函式的使用方法
**************************************/
void main(int ac, char* av[])
{
// GetNetworkParams
printf("------------------------\n");
printf("GetNetworkParams\n\n");
// 所需的變數
FIXED_INFO *pFixedInfo;
IP_ADDR_STRING *pIPAddr;
// 分配記憶體
pFixedInfo = (FIXED_INFO *)MyAlloc(sizeof(FIXED_INFO));
ulOutBufLen = sizeof(FIXED_INFO);
if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
// 空間不足,重新分配
MyFree(pFixedInfo);
pFixedInfo = (FIXED_INFO *)MyAlloc(ulOutBufLen);
}
// 獲取資訊
if (dwRetVal = GetNetworkParams (pFixedInfo, &ulOutBufLen) != NO_ERROR)
{
printf("Call to GetNetworkParams failed.\n");
}
else
{
// 列印輸出
printf("\tHost Name: %s\n", pFixedInfo->HostName);// 主機名
printf("\tDomain Name: %s\n", pFixedInfo->DomainName);// DNS主機名
printf("\tDNS Servers:\n");//DNS 伺服器
printf("\t\t%s\n", pFixedInfo->DnsServerList.IpAddress.String);// IP,字串形式的
pIPAddr = pFixedInfo->DnsServerList.Next;// 備用DNS
while (pIPAddr)
{
printf("\t\t%s\n", pIPAddr->IpAddress.String);
pIPAddr = pIPAddr->Next;
}
if (pFixedInfo->EnableRouting)// 是否路由
printf("\tEnable Routing: Yes\n");
else
printf("\tEnable Routing: No\n");
if (pFixedInfo->EnableProxy)// 是否有代理
printf("\tEnable Proxy: Yes\n");
else
printf("\tEnable Proxy: No\n");
if (pFixedInfo->EnableDns)// 是否有DNS
printf("\tEnable DNS: Yes\n");
else
printf("\tEnable DNS: No\n");
}
MyFree(pFixedInfo);// 釋放記憶體
// GetNetworkParams
printf("------------------------\n");
printf("GetInterfaceInfo\n\n");
// 變數定義、分配記憶體空間
PIP_INTERFACE_INFO pInfo;
ulOutBufLen = sizeof(IP_INTERFACE_INFO);
pInfo = (PIP_INTERFACE_INFO)MyAlloc(ulOutBufLen);
while (1)
{
dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen);
// 內在不足,重新分配
if (dwRetVal == ERROR_INSUFFICIENT_BUFFER)
{
MyFree(pInfo);
pInfo = (IP_INTERFACE_INFO *)MyAlloc(ulOutBufLen);
}
else if (dwRetVal == NO_ERROR)
{
// 顯示輸出每個網絡卡的資訊
int i;
printf("Num Adapters: %ld\n", pInfo->NumAdapters);
for (i = 0; i < pInfo->NumAdapters; i++)
{
printf("(%d)Adapter Name: %ws\n", i, pInfo->Adapter[i].Name);
printf("(%d)Adapter Index: %ld\n", i, pInfo->Adapter[i].Index);
}
break;
}
else
printf("GetInterfaceInfo: %u\n", GetLastError());
}
MyFree(pInfo);// 釋放記憶體
// GetIpAddrTable
printf("----------------------\n");
printf("GetIpAddrTable\n\n");
// 變數定義、記憶體分配
MIB_IPADDRTABLE *pIPAddrTable;
DWORD dwSize;
in_addr IPAddr;
char *strIPAddr;
pIPAddrTable = (MIB_IPADDRTABLE*)MyAlloc(sizeof(MIB_IPADDRTABLE));
dwSize = 0;
IPAddr.S_un.S_addr = ntohl(pIPAddrTable->table[1].dwAddr);
strIPAddr = inet_ntoa(IPAddr);
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
// 空間不足,重新分配
MyFree(pIPAddrTable);
pIPAddrTable = (MIB_IPADDRTABLE *)MyAlloc(dwSize);
}
// 獲得資訊並顯示
if ((dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0)) != NO_ERROR)
{
printf("Call to GetIpAddrTable failed.\n");
}
// IP、掩碼等是以DWORD的形式儲存的
printf("Address: %u.%u.%u.%u\n",
pIPAddrTable->table[0].dwAddr & 0x000000FF,
(pIPAddrTable->table[0].dwAddr & 0x0000FF00) >> 8,
(pIPAddrTable->table[0].dwAddr & 0x00FF0000) >> 16,
(pIPAddrTable->table[0].dwAddr & 0xFF000000) >> 24);
printf("Mask: %lu\n", pIPAddrTable->table[0].dwMask);
printf("Index: %lu\n", pIPAddrTable->table[0].dwIndex);
printf("BCast: %lu\n", pIPAddrTable->table[0].dwBCastAddr);
printf("Reasm: %lu\n", pIPAddrTable->table[0].dwReasmSize);
MyFree(pIPAddrTable);// 釋放記憶體
// GetIPStatistics
printf("-------------------------\n");
printf("GetIPStatistics\n\n");
// 定義變數,分配記憶體
MIB_IPSTATS *pStats;
pStats = (MIB_IPSTATS*)MyAlloc(sizeof(MIB_IPSTATS));
// 獲取資訊
if ((dwRetVal = GetIpStatistics(pStats)) != NO_ERROR)
{
printf("\tError getting stats.\n");
}
// 顯示
printf("\tNumber of IP addresses: %ld\n", pStats->dwNumAddr);
printf("\tNumber of Interfaces: %ld\n", pStats->dwNumIf);
printf("\tReceives: %ld\n", pStats->dwInReceives);
printf("\tOut Requests: %ld\n", pStats->dwOutRequests);
printf("\tRoutes: %ld\n", pStats->dwNumRoutes);
printf("\tTimeout Time: %ld\n", pStats->dwReasmTimeout);
printf("\tIn Delivers: %ld\n", pStats->dwInDelivers);
printf("\tIn Discards: %ld\n", pStats->dwInDiscards);
printf("\tTotal In: %ld\n", pStats->dwInDelivers + pStats->dwInDiscards);
printf("\tIn Header Errors: %ld\n", pStats->dwInHdrErrors);
MyFree(pStats);// 釋放
// GetTCPStatistics
printf("-------------------------\n");
printf("GetTCPStatistics\n\n");
// 定義變數,分配空間
MIB_TCPSTATS *pTCPStats;
pTCPStats = (MIB_TCPSTATS*)MyAlloc(sizeof(MIB_TCPSTATS));
// 獲取資訊,並顯示
if ((dwRetVal = GetTcpStatistics(pTCPStats)) != NO_ERROR)
printf("Error getting TCP Stats.\n");
printf("\tActive Opens: %ld\n", pTCPStats->dwActiveOpens);
printf("\tPassive Opens: %ld\n", pTCPStats->dwPassiveOpens);
printf("\tSegments Recv: %ld\n", pTCPStats->dwInSegs);
printf("\tSegments Xmit: %ld\n", pTCPStats->dwOutSegs);
printf("\tTotal # Conxs: %ld\n", pTCPStats->dwNumConns);
MyFree(pTCPStats);// 釋放記憶體
// GetAdapatersInfo
printf("------------------------\n");
printf("GetAdapatersInfo\n\n");
// 定義變數,分配記憶體
IP_ADAPTER_INFO *pAdapterInfo;
IP_ADAPTER_INFO *pAdapter;
pAdapterInfo = (IP_ADAPTER_INFO *)MyAlloc(sizeof(IP_ADAPTER_INFO));
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
// Test,所需記憶體大小
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS)
{
MyFree(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MyAlloc(ulOutBufLen);
}
// 獲得資訊
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) != NO_ERROR)
{
printf("Call to GetAdaptersInfo failed.\n");
}
pAdapter = pAdapterInfo;
// 顯示各個網絡卡的資訊
while (pAdapter)
{
printf("\n\t***********\n");
printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);// 網絡卡裝置名
printf("\tAdapter Desc: \t%s\n", pAdapter->Description);// 描述
printf("\tAdapter Addr: \t%ld\n", pAdapter->Address);// MAC
printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);// IP
printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);// 掩碼
printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);// 閘道器
printf("\t***\n");
if (pAdapter->DhcpEnabled)// DHCP
{
printf("\tDHCP Enabled: Yes\n");
printf("\t\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
printf("\tLease Obtained: %ld\n", pAdapter->LeaseObtained);
}
else
printf("\tDHCP Enabled: No\n");
if (pAdapter->HaveWins)// Wins
{
printf("\tHave Wins: Yes\n");
printf("\t\tPrimary Wins Server: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
printf("\t\tSecondary Wins Server: \t%s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
printf("\tHave Wins: No\n");
pAdapter = pAdapter->Next;
}
MyFree(pAdapterInfo);// 釋放
}