kernel: Add an event kernel interfaces can raise if they create/destroy a TUN device
authorTobias Brunner <tobias@strongswan.org>
Thu, 13 Jun 2013 15:03:17 +0000 (17:03 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 21 Jun 2013 15:03:21 +0000 (17:03 +0200)
src/libhydra/kernel/kernel_interface.c
src/libhydra/kernel/kernel_interface.h
src/libhydra/kernel/kernel_listener.h

index be247df..90637fa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 Tobias Brunner
+ * Copyright (C) 2008-2013 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  * Copyright (C) 2010 Martin Willi
  * Copyright (C) 2010 revosec AG
@@ -644,6 +644,25 @@ METHOD(kernel_interface_t, roam, void,
        this->mutex->unlock(this->mutex);
 }
 
+METHOD(kernel_interface_t, tun, void,
+       private_kernel_interface_t *this, tun_device_t *tun, bool created)
+{
+       kernel_listener_t *listener;
+       enumerator_t *enumerator;
+       this->mutex->lock(this->mutex);
+       enumerator = this->listeners->create_enumerator(this->listeners);
+       while (enumerator->enumerate(enumerator, &listener))
+       {
+               if (listener->tun &&
+                       !listener->tun(listener, tun, created))
+               {
+                       this->listeners->remove_at(this->listeners, enumerator);
+               }
+       }
+       enumerator->destroy(enumerator);
+       this->mutex->unlock(this->mutex);
+}
+
 METHOD(kernel_interface_t, register_algorithm, void,
        private_kernel_interface_t *this, u_int16_t alg_id, transform_type_t type,
        u_int16_t kernel_id, char *kernel_name)
@@ -764,6 +783,7 @@ kernel_interface_t *kernel_interface_create()
                        .mapping = _mapping,
                        .migrate = _migrate,
                        .roam = _roam,
+                       .tun = _tun,
                        .destroy = _destroy,
                },
                .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
index ab2b382..2e8a4c0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2013 Tobias Brunner
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005-2006 Martin Willi
  * Copyright (C) 2005 Jan Hutter
@@ -562,6 +562,14 @@ struct kernel_interface_t {
        void (*roam)(kernel_interface_t *this, bool address);
 
        /**
+        * Raise a tun event.
+        *
+        * @param tun                   TUN device
+        * @param created               TRUE if created, FALSE if going to be destroyed
+        */
+       void (*tun)(kernel_interface_t *this, tun_device_t *tun, bool created);
+
+       /**
         * Register a new algorithm with the kernel interface.
         *
         * @param alg_id                        the IKE id of the algorithm
index 27ea947..4382a43 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Tobias Brunner
+ * Copyright (C) 2010-2013 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
 
 typedef struct kernel_listener_t kernel_listener_t;
 
-#include <kernel/kernel_ipsec.h>
-#include <selectors/traffic_selector.h>
 #include <networking/host.h>
+#include <networking/tun_device.h>
+#include <selectors/traffic_selector.h>
+#include <kernel/kernel_ipsec.h>
 
 /**
  * Interface for components interested in kernel events.
@@ -91,6 +92,15 @@ struct kernel_listener_t {
         * @return                              TRUE to remain registered, FALSE to unregister
         */
        bool (*roam)(kernel_listener_t *this, bool address);
+
+       /**
+        * Hook called after a TUN device was created for a virtual IP address, or
+        * before such a device gets destroyed.
+        *
+        * @param tun                   TUN device
+        * @param created               TRUE if created, FALSE if going to be destroyed
+        */
+       bool (*tun)(kernel_listener_t *this, tun_device_t *tun, bool created);
 };
 
 #endif /** KERNEL_LISTENER_H_ @}*/