HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Get IP info from a FreeBSD machine

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
infofreebsdgetmachinefrom

Problem

I'm try to get the public IP address from my FreeBSD machine and use it for the rest of my code.

Is this implementation good? Can it be optimized more? Can you see some gaps here? Something that maybe sometimes will throw an error?

GET.CPP

char g_szPublicIP[16] = "0";
    char g_szInternalIP[16] = "0";

bool GetIPInfo()
{
    struct ifaddrs* ifaddrp = NULL;

    if (0 != getifaddrs(&ifaddrp))
    {
        return false;
    }

    for( struct ifaddrs* ifap = ifaddrp ; NULL != ifap ; ifap = ifap->ifa_next )
    {
        struct sockaddr_in * sai = (struct sockaddr_in *) ifap->ifa_addr;

        if (!ifap->ifa_netmask ||  // ignore if no netmask
                sai->sin_addr.s_addr == 0 || // ignore if address is 0.0.0.0
                sai->sin_addr.s_addr == 16777343) // ignore if address is 127.0.0.1
        {
            continue;
        }

        char * netip = inet_ntoa(sai->sin_addr);

        if (!strncmp(netip, "10.", 3))
        {
            strlcpy(g_szInternalIP, netip, sizeof(g_szInternalIP));
            fprintf(stderr, "INTERNAL_IP: %s interface %s\n", netip, ifap->ifa_name);
        }
        else if (g_szPublicIP[0] == '0')
        {
            strlcpy(g_szPublicIP, netip, sizeof(g_szPublicIP));
            fprintf(stderr, "PUBLIC_IP: %s interface %s\n", netip, ifap->ifa_name);
        }
    }

    freeifaddrs( ifaddrp );

    if (g_szPublicIP[0] != '0')
    {
        return true;
    }
    else
    {
        return false;
    }
}

    int main()
    {
        if (!GetIPInfo()) // Check is getIpInfo
        {
            fprintf(stderr, "Can not get public ip adress (1)-GetIpInfo\n");
            exit(1);
        }

        if(g_szPublicIP[0] == '0') // Check is 
        {
            fprintf(stderr, "Can not get public ip address (2)-EndGetIpInfo\n");
            exit(1);
        }
    }

Solution

-
main() doesn't need to be indented out as such, same with the global arrays.

-
main() should have a void parameter since it's not taking command-line arguments.

-
You're using "Yoda conditions" in some places:

if (0 != getifaddrs(&ifaddrp))


NULL != ifap


and rest elsewhere in most places. Stay consistent with one of these types.

-
It looks like netip could be const since it's not being modified and is only used as a const parameter in strlcpy().

-
You can simplify this:

if (g_szPublicIP[0] != '0')
{
    return true;
}
else
{
    return false;
}


to this:

return g_szPublicIP[0] != '0';


It'll automatically return the outcome of the function call.

However, if you still prefer to see true and false, you can use a ternary:

return (g_szPublicIP[0] != '0') ? true : false;

Code Snippets

if (0 != getifaddrs(&ifaddrp))
NULL != ifap
if (g_szPublicIP[0] != '0')
{
    return true;
}
else
{
    return false;
}
return g_szPublicIP[0] != '0';
return (g_szPublicIP[0] != '0') ? true : false;

Context

StackExchange Code Review Q#152665, answer score: 2

Revisions (0)

No revisions yet.