SetIpForwardEntry Function

The SetIpForwardEntry function adds a new route or modifies an existing route in the IP routing table.

Syntax


MIB_IPFORWARDROW SetIpForwardEntry(
  [in] PMIB_IPFORWARDROW pRoute
);
            

Parameters

Parameter Type Description
pRoute PMIB_IPFORWARDROW A pointer to a MIB_IPFORWARDROW structure that specifies the properties of the route to add or modify.

Return Value

Return Value Description
NO_ERROR The route was successfully added or modified.
ERROR_INVALID_PARAMETER One or more parameters are invalid.
ERROR_NOT_FOUND The specified route was not found (for modification).
ERROR_NOT_ENOUGH_MEMORY Not enough memory is available to complete the operation.
ERROR_INVALID_CONFIG The configuration is invalid.

Remarks

The SetIpForwardEntry function is used to manage the IP routing table on a Windows system. You can use it to:

The MIB_IPFORWARDROW structure contains information about the route, including the destination address, subnet mask, gateway (next hop), interface, and metrics.

When modifying an existing route, the function identifies the route to be modified using the dwForwardDest, dwForwardMask, and dwForwardNextHop members of the MIB_IPFORWARDROW structure. If you want to modify the route associated with a particular dwForwardIfIndex, ensure that it is also correctly set in the structure.

Requirements

Header Iphlpapi.h
Library Iphlpapi.lib
DLL Iphlpapi.dll

See Also

Example: Adding a Static Route

The following C++ code snippet demonstrates how to add a static route using SetIpForwardEntry.


#include <winsock2.h>
#include <iprtrmib.h>
#include <iphlpapi.h>
#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")

void AddStaticRoute(DWORD dest, DWORD mask, DWORD gateway, DWORD ifIndex) {
    MIB_IPFORWARDROW route;
    ZeroMemory(&route, sizeof(route));

    route.dwForwardDest = dest;      // Destination network address
    route.dwForwardMask = mask;      // Subnet mask for the destination network
    route.dwForwardNextHop = gateway; // IP address of the next hop router
    route.dwForwardProto = PROTO_IP_STATIC; // Static route protocol
    route.dwForwardType = MIB_IPROUTE_TYPE_REMOTE; // Remote network
    route.dwForwardPolicy = 0;       // No specific policy
    route.dwForwardAge = 0;          // Not applicable for static routes
    route.dwForwardMetric1 = 1;      // Route metric (lower is better)
    route.dwForwardMetric2 = 0;      // Not used
    route.dwForwardMetric3 = 0;      // Not used
    route.dwForwardMetric4 = 0;      // Not used
    route.dwForwardMetric5 = 0;      // Not used
    route.dwForwardInfo = 0;         // Not used
    route.dwForwardIfIndex = ifIndex; // Interface index to use for this route

    DWORD dwRetVal = SetIpForwardEntry(&route);

    if (dwRetVal == NO_ERROR) {
        printf("Successfully added static route.\n");
    } else {
        printf("Failed to add static route. Error code: %lu\n", dwRetVal);
    }
}

int main() {
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("WSAStartup failed.\n");
        return 1;
    }

    // Example: Add a route to 192.168.2.0/24 via gateway 10.0.0.1 on interface 12
    // You would typically get these values dynamically.
    DWORD destination = inet_addr("192.168.2.0");
    DWORD subnetMask = inet_addr("255.255.255.0");
    DWORD gateway = inet_addr("10.0.0.1");
    DWORD interfaceIndex = 12; // Replace with actual interface index

    AddStaticRoute(destination, subnetMask, gateway, interfaceIndex);

    WSACleanup();
    return 0;
}