IP Helper API Reference
The IP Helper Application Programming Interface (API) provides functions that retrieve information about network configurations and modify network configurations. These functions are included in the IP Helper API. The functions in this API are designed to provide a higher-level abstraction over Winsock.
Overview
The IP Helper API allows applications to:
- Retrieve information about IP addresses, interfaces, and routes.
- Query and modify TCP/IP parameters.
- Register for notifications about network configuration changes.
Key Concepts
Understanding the following concepts is crucial when working with the IP Helper API:
- IP Addresses: IPv4 and IPv6 addresses associated with network interfaces.
- Network Interfaces: The network adapters on a system (e.g., Ethernet card, Wi-Fi adapter).
- Routing Table: Information about how network traffic is directed between networks.
- TCP/IP Parameters: Various settings that control the behavior of the TCP/IP stack.
Core Functions
Retrieving IP Address Information
These functions allow you to get details about IP addresses configured on the system.
GetAdaptersInfo: Retrieves information about network adapters.GetAdaptersAddresses: Retrieves detailed information about network adapters, including IP addresses.GetIpAddrTable: Retrieves the IP address table for IPv4.GetIpAddrTable2: Retrieves the IP address table for IPv4 and IPv6.
Managing Routing Entries
Manipulate the system's routing table.
GetIpNetTable: Retrieves the IP-to-Physical Address translation table.CreateIpForwardEntry: Adds a new entry to the IP routing table.DeleteIpForwardEntry: Deletes an entry from the IP routing table.
Network Configuration and Parameters
Query and set various TCP/IP parameters.
GetTcpTable: Retrieves the TCP connection table.GetUdpTable: Retrieves the UDP listener table.GetTcpGlobalInformation: Retrieves global TCP configuration information.SetTcpGlobalInformation: Sets global TCP configuration parameters.
Notifications
Receive notifications when network configurations change.
NotifyIpInterfaceChange: Registers to receive notifications for IP interface changes.NotifyRouteChange: Registers to receive notifications for route changes.
Example Usage
The following C++ code snippet demonstrates how to retrieve IP address information for all adapters:
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
int main() {
PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL;
ULONG outBufLen = 0;
DWORD dwRetVal = 0;
// Allocate a buffer for the adapter addresses
dwRetVal = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &outBufLen);
if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
pAdapterAddresses = (IP_ADAPTER_ADDRESSES *) malloc(outBufLen);
if (pAdapterAddresses == NULL) {
printf("Error allocating memory\n");
return 1;
}
} else {
printf("Error getting buffer size: %d\n", dwRetVal);
return 1;
}
// Get adapter addresses
dwRetVal = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAdapterAddresses, &outBufLen);
if (dwRetVal == NO_ERROR) {
printf("Successfully retrieved adapter addresses.\n\n");
for (PIP_ADAPTER_ADDRESSES pAdapter = pAdapterAddresses; pAdapter != NULL; pAdapter = pAdapter->Next) {
printf("Adapter Name: %S\n", pAdapter->FriendlyName);
printf(" Description: %S\n", pAdapter->Description);
for (PIP_ADAPTER_UNICAST_ADDRESS pUnicast = pAdapter->FirstUnicastAddress; pUnicast != NULL; pUnicast = pUnicast->Next) {
if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) {
char ipstr[INET_ADDRSTRLEN);
struct sockaddr_in *sin = (struct sockaddr_in *) pUnicast->Address.lpSockaddr;
inet_ntop(AF_INET, &sin->sin_addr, ipstr, sizeof(ipstr));
printf(" IPv4 Address: %s\n", ipstr);
} else if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6) {
char ipstr[INET6_ADDRSTRLEN);
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) pUnicast->Address.lpSockaddr;
inet_ntop(AF_INET6, &sin6->sin6_addr, ipstr, sizeof(ipstr));
printf(" IPv6 Address: %s\n", ipstr);
}
}
printf("\n");
}
} else {
printf("Error getting adapter addresses: %d\n", dwRetVal);
}
if (pAdapterAddresses) {
free(pAdapterAddresses);
}
return 0;
}
Note: Ensure that you link against
iphlpapi.lib and ws2_32.lib when compiling your application.