Network I/O API (Netioapi)

The Network I/O API (Netioapi) provides a set of functions for managing and querying network interface and protocol information on Windows. This API is built upon the underlying network stack and offers a programmatic way to interact with network adapters, IP addresses, routing tables, and other network-related configurations.

Overview

Netioapi functions allow developers to:

These functionalities are crucial for developing network-aware applications, network monitoring tools, and network configuration utilities.

Key Concepts

Understanding the following concepts is essential when working with Netioapi:

Network Interfaces (Adapters)

A network interface, or adapter, is a hardware component that connects a computer to a network. Netioapi provides structures and functions to enumerate and retrieve detailed information about each network adapter installed on the system.

IP Addressing

The API allows for the management of IPv4 and IPv6 addresses, including adding, deleting, and querying IP addresses on specific interfaces. This includes unicast, multicast, and broadcast addresses.

Routing Tables

Netioapi enables programmatic access to the system's routing table, which dictates how network traffic is directed. You can query existing routes and, in some cases, add or delete routes.

Network Protocols

Information about various network protocols, such as their statistics and configuration, can be retrieved using Netioapi functions. This can include details about TCP connections, UDP sockets, and ICMP traffic.

Common Functions

Here are some of the frequently used functions within the Netioapi:

Function Name Description
GetIfTable2 Retrieves a table of network interface information.
GetIpAddrTable2 Retrieves a table of IP address information for all network interfaces.
CreateIpForwardEntry2 Adds a new IP route to the routing table.
DeleteIpForwardEntry2 Deletes an IP route from the routing table.
GetUdpTable2 Retrieves a table of UDP listener information.
NotifyRouteChange2 Registers to receive notifications about routing table changes.

Example: Retrieving Network Interface Information

The following C++ code snippet demonstrates how to use GetIfTable2 to list network 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 pAddresses = NULL;
    ULONG outBufLen = 0;
    DWORD dwRetVal = 0;

    // Allocate memory for the adapter addresses
    dwRetVal = GetAdaptersAddresses(
        AF_UNSPEC,          // Get addresses for both IPv4 and IPv6
        GAA_FLAG_INCLUDE_PREFIX, // Include prefix information
        NULL,               // Reserved
        pAddresses,         // Pointer to buffer
        &outBufLen);        // Size of buffer

    if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
        pAddresses = (IP_ADAPTER_ADDRESSES*) malloc(outBufLen);
        if (pAddresses == NULL) {
            printf("Error allocating memory\n");
            return 1;
        }
    } else if (dwRetVal != NO_ERROR) {
        printf("Error getting adapter addresses: %d\n", dwRetVal);
        return 1;
    }

    // Iterate through the linked list of adapters
    PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses;
    while (pCurrAddresses) {
        printf("Adapter Name: %S\n", pCurrAddresses->FriendlyName);
        printf("  Description: %S\n", pCurrAddresses->Description);

        // Print IP addresses
        IP_ADAPTER_UNICAST_ADDRESS* pUnicast = pCurrAddresses->FirstUnicastAddress;
        while (pUnicast) {
            sockaddr_in* ipv4 = (sockaddr_in*) pUnicast->Address.lpSockaddr;
            if (pUnicast->Address.iSockaddrLength >= sizeof(sockaddr_in)) {
                char ipstr[INET_ADDRSTRLEN];
                inet_ntop(AF_INET, &(ipv4->sin_addr), ipstr, sizeof(ipstr));
                printf("  IPv4 Address: %s\n", ipstr);
            }
            pUnicast = pUnicast->Next;
        }
        printf("\n");
        pCurrAddresses = pCurrAddresses->Next;
    }

    if (pAddresses) {
        free(pAddresses);
    }

    return 0;
}
        

Note on Deprecation

While Netioapi is still widely used, some of its functionalities might be superseded by newer APIs or Windows versions. Always refer to the official Microsoft documentation for the most up-to-date information and best practices.

Related Topics