]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
kernel-utun: do interface lookup using getifaddrs()
authorMartin Willi <martin@revosec.ch>
Fri, 12 Apr 2013 10:29:48 +0000 (12:29 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 18 Apr 2013 12:43:55 +0000 (14:43 +0200)
src/libhydra/plugins/kernel_utun/kernel_utun_net.c

index 22ead0a6df6b303bb6a087a8e7137f3b3e7272d2..7d854a7ba8fdabf51b0aa2b219703fa06a45400d 100644 (file)
@@ -13,6 +13,9 @@
  * for more details.
  */
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <ifaddrs.h>
 
 #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
+}