19

I'm trying to obtain my local (not the external) IP address using the getaddrinfo() function, but I saw the examples provided here, and they where too complex for my needs. Also saw other posts and most of them really wanted to get the external IP, not the local one.

Could anyone provide a link to a simple example (or a simple example) about how to obtain my own local IP address using this function ?

Just to be clear when I say local, if a router is 192.168.0.1 , my local IP address could be something like 192.168.0.x ( just an example ).

glglgl
  • 89,107
  • 13
  • 149
  • 217
Goles
  • 11,599
  • 22
  • 79
  • 140
  • getaddrinfo was made to have an uniform interface between ipv4 and ipv6, this is why it looks a little complex. (If it get the answer I'll let you know later) – Aif Jan 27 '10 at 10:59
  • Thanks! , yes, it looks complex and I don't want to use gethostbyname() because it seems to be deprecated... – Goles Jan 27 '10 at 11:03
  • 1
    The last example on that page is how to do this... why is that too complex? Use AF_INET if you really want only an IPv4 address... but don't do that, make it support IPv6 as well. – Andrew McGregor Jan 27 '10 at 11:30
  • 1
    If you're using C++, have you considered using the ASIO networking library? http://asio.sourceforge.net/ –  Jan 27 '10 at 12:08
  • Link you provided as example is broken. Please update it if possible. – Fusion Jul 28 '19 at 09:32

2 Answers2

38

getaddrinfo() isn't for obtaining your local IP address - it's for looking up names and/or services to socket addresses. To obtain the local IP address(es), the function you want is getifaddrs() - here's a minimal example:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    struct ifaddrs *myaddrs, *ifa;
    void *in_addr;
    char buf[64];

    if(getifaddrs(&myaddrs) != 0)
    {
        perror("getifaddrs");
        exit(1);
    }

    for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
    {
        if (ifa->ifa_addr == NULL)
            continue;
        if (!(ifa->ifa_flags & IFF_UP))
            continue;

        switch (ifa->ifa_addr->sa_family)
        {
            case AF_INET:
            {
                struct sockaddr_in *s4 = (struct sockaddr_in *)ifa->ifa_addr;
                in_addr = &s4->sin_addr;
                break;
            }

            case AF_INET6:
            {
                struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)ifa->ifa_addr;
                in_addr = &s6->sin6_addr;
                break;
            }

            default:
                continue;
        }

        if (!inet_ntop(ifa->ifa_addr->sa_family, in_addr, buf, sizeof(buf)))
        {
            printf("%s: inet_ntop failed!\n", ifa->ifa_name);
        }
        else
        {
            printf("%s: %s\n", ifa->ifa_name, buf);
        }
    }

    freeifaddrs(myaddrs);
    return 0;
}
caf
  • 233,326
  • 40
  • 323
  • 462
-2

Pass the hostname after using gethostname(), to gethostbyname()

int gethostname(char *hostname, size_t size);
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
robert
  • 5
  • 1
  • No, the hostname may not be resolvable. It's not good to try to resolve it. It's a common programming error though, so some systems guarantee that the hostname is resolvable by returning "127.0.1.1" just so there isn't an error. Therefore, you can't even rely on the return because it might not even be a useful address. – Nicholas Wilson Aug 23 '13 at 15:51