static int ConfigureNetworkInterfaceWithStaticIp()

in CodeSnippets/Networking/StaticIp/static_ip.c [28:62]


static int ConfigureNetworkInterfaceWithStaticIp(void)
{
    struct in_addr staticIpAddress;
    struct in_addr subnetMask;
    struct in_addr gatewayIpAddress;

    Networking_IpConfig ipConfig;

    // Convert the addresses from the numbers-and-dots notation into integers.
    if (inet_pton(AF_INET, staticIpInDotNotation, &staticIpAddress) != 1) {
        Log_Debug("ERROR: Invalid static IP address or address family specified.\n");
        return -1;
    }
    if (inet_pton(AF_INET, subnetMaskInDotNotation, &subnetMask) != 1) {
        Log_Debug("ERROR: Invalid subnet mask or address family specified.\n");
        return -1;
    }
    if (inet_pton(AF_INET, gatewayIpInDotNotation, &gatewayIpAddress) != 1) {
        Log_Debug("ERROR: Invalid gateway IP address or address family specified.\n");
        return -1;
    }

    Networking_IpConfig_Init(&ipConfig);
    Networking_IpConfig_EnableStaticIp(&ipConfig, staticIpAddress, subnetMask, gatewayIpAddress);

    int result = Networking_IpConfig_Apply(networkInterfaceToConfigure, &ipConfig);
    Networking_IpConfig_Destroy(&ipConfig);

    if (result != 0) {
        Log_Debug("ERROR: Networking_IpConfig_Apply: %d (%s)\n", errno, strerror(errno));
        return -1;
    }

    return 0;
}