On Wed, 9 Feb 2005, Jochen Witte wrote: > Hello, > > I would like to get a list of all my current ip-adresses in a C program. > How do I do this? Do I really have to use ioctl? Or is there something > simple/portable out there? Something like this: #include <sys/types.h> #include <sys/socket.h> #include <ifaddrs.h> #include <net/if.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include "libinet6/debug.h" int debug_sockaddr(int family, const struct sockaddr *sockaddr) { int maxlen; void *addr; char addr_str[INET6_ADDRSTRLEN+1]; switch (family) { case AF_INET: maxlen = INET_ADDRSTRLEN; addr = &(((struct sockaddr_in *) sockaddr)->sin_addr); break; case AF_INET6: maxlen = INET6_ADDRSTRLEN; addr = &(((struct sockaddr_in6 *) sockaddr)->sin6_addr); break; default: maxlen = 0; } if (!inet_ntop(family, addr, addr_str, maxlen)) { printf(stderr, "inet_ntop"); return -1; } printf("%s\n", addr_str); return 0; } int main(int argc,char *argv[]) { struct ifaddrs *g_ifaces = NULL, *g_iface; struct if_nameindex *i_ifaces = NULL, *i_iface; int err = 0; char *default_str = "<unknown>"; char addr_str[INET6_ADDRSTRLEN+1]; /* Segfault? Alloc this dynamically?x */ /* getifaddrs */ err = getifaddrs(&g_ifaces); if (err) { fprintf(stderr,"getifaddr failed\n"); goto out; } printf("===getifaddrs===\n"); for (g_iface = g_ifaces; g_iface; g_iface = g_iface->ifa_next) { sa_family_t family = g_iface->ifa_addr->sa_family; printf("name: %s, family: %d, address: ", g_iface->ifa_name, family); debug_sockaddr("", family, g_iface->ifa_addr); } /* if_nameindex */ printf("===nameindex===\n"); i_ifaces = if_nameindex(); for (i_iface = i_ifaces; i_iface->if_index; i_iface++) { printf("name: %s index: %d\n", i_iface->if_name, i_iface->if_index); } out: if (g_ifaces) freeifaddrs(g_ifaces); if (i_ifaces) if_freenameindex(i_ifaces); } -- Miika Komu miika@xxxxxx http://www.iki.fi/miika/ - : send the line "unsubscribe linux-net" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html