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!
", ifa->ifa_name);
}
else
{
printf("%s: %s
", ifa->ifa_name, buf);
}
}
freeifaddrs(myaddrs);
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…