Get IP info from a FreeBSD machine 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(&amp;ifaddrp))
    {
        return false;
    }

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

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

        char * netip = inet_ntoa(sai-&gt;sin_addr);

        if (!strncmp(netip, "10.", 3))
        {
            strlcpy(g_szInternalIP, netip, sizeof(g_szInternalIP));
            fprintf(stderr, "INTERNAL_IP: %s interface %s\n", netip, ifap-&gt;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-&gt;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);
        }
    }
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(&amp;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;