From: Martin Willi Date: Fri, 12 Apr 2013 10:29:48 +0000 (+0200) Subject: kernel-utun: do interface lookup using getifaddrs() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=419c6b98ab035150d50deb478116f41ec6e0d701;p=thirdparty%2Fstrongswan.git kernel-utun: do interface lookup using getifaddrs() --- diff --git a/src/libhydra/plugins/kernel_utun/kernel_utun_net.c b/src/libhydra/plugins/kernel_utun/kernel_utun_net.c index 22ead0a6df..7d854a7ba8 100644 --- a/src/libhydra/plugins/kernel_utun/kernel_utun_net.c +++ b/src/libhydra/plugins/kernel_utun/kernel_utun_net.c @@ -13,6 +13,9 @@ * for more details. */ +#include +#include +#include #include "kernel_utun_net.h" @@ -41,7 +44,37 @@ METHOD(kernel_net_t, create_address_enumerator, enumerator_t*, METHOD(kernel_net_t, get_interface_name, bool, private_kernel_utun_net_t *this, host_t* ip, char **name) { - return FALSE; + struct ifaddrs *ifa, *current; + host_t *host; + bool found = FALSE; + + if (getifaddrs(&ifa) != 0) + { + return FALSE; + } + for (current = ifa; current; current = current->ifa_next) + { + if (current->ifa_addr) + { + host = host_create_from_sockaddr(current->ifa_addr); + if (host) + { + if (ip->ip_equals(ip, host)) + { + *name = strdup(current->ifa_name); + found = TRUE; + } + host->destroy(host); + } + } + if (found) + { + break; + } + } + freeifaddrs(ifa); + + return found; } METHOD(kernel_net_t, get_source_addr, host_t*, @@ -114,4 +147,4 @@ kernel_utun_net_t *kernel_utun_net_create() ); return &this->public; -} \ No newline at end of file +}