net: Socket implementations report the address families they support
authorTobias Brunner <tobias@strongswan.org>
Wed, 3 Jul 2013 15:32:40 +0000 (17:32 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 5 Jul 2013 07:48:01 +0000 (09:48 +0200)
src/libcharon/network/socket.h
src/libcharon/network/socket_manager.c
src/libcharon/network/socket_manager.h
src/libcharon/plugins/socket_default/socket_default_socket.c
src/libcharon/plugins/socket_dynamic/socket_dynamic_socket.c

index f6c8a86..e3cda3b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2012 Tobias Brunner
+ * Copyright (C) 2006-2013 Tobias Brunner
  * Copyright (C) 2005-2010 Martin Willi
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005 Jan Hutter
@@ -25,6 +25,7 @@
 #define SOCKET_H_
 
 typedef struct socket_t socket_t;
+typedef enum socket_family_t socket_family_t;
 
 #include <library.h>
 #include <networking/packet.h>
@@ -37,6 +38,31 @@ typedef struct socket_t socket_t;
 typedef socket_t *(*socket_constructor_t)();
 
 /**
+ * Address families supported by socket implementations.
+ */
+enum socket_family_t {
+       /**
+        * No address families supported
+        */
+       SOCKET_FAMILY_NONE = 0,
+
+       /**
+        * IPv4
+        */
+       SOCKET_FAMILY_IPV4 = (1 << 0),
+
+       /**
+        * IPv6
+        */
+       SOCKET_FAMILY_IPV6 = (1 << 1),
+
+       /**
+        * Both address families supported
+        */
+       SOCKET_FAMILY_BOTH = (1 << 2) - 1,
+};
+
+/**
  * Socket interface definition.
  */
 struct socket_t {
@@ -52,7 +78,7 @@ struct socket_t {
         *                                              - SUCCESS when packet successfully received
         *                                              - FAILED when unable to receive
         */
-       status_t (*receive) (socket_t *this, packet_t **packet);
+       status_t (*receive)(socket_t *this, packet_t **packet);
 
        /**
         * Send a packet.
@@ -65,7 +91,7 @@ struct socket_t {
         *                                              - SUCCESS when packet successfully sent
         *                                              - FAILED when unable to send
         */
-       status_t (*send) (socket_t *this, packet_t *packet);
+       status_t (*send)(socket_t *this, packet_t *packet);
 
        /**
         * Get the port this socket is listening on.
@@ -73,12 +99,19 @@ struct socket_t {
         * @param nat_t                 TRUE to get the port used to float in case of NAT-T
         * @return                              the port
         */
-       u_int16_t (*get_port) (socket_t *this, bool nat_t);
+       u_int16_t (*get_port)(socket_t *this, bool nat_t);
+
+       /**
+        * Get the address families this socket is listening on.
+        *
+        * @return                              supported families
+        */
+       socket_family_t (*supported_families)(socket_t *this);
 
        /**
         * Destroy a socket implementation.
         */
-       void (*destroy) (socket_t *this);
+       void (*destroy)(socket_t *this);
 };
 
 /**
index bf1fe5b..2a07e50 100644 (file)
@@ -102,6 +102,19 @@ METHOD(socket_manager_t, get_port, u_int16_t,
        return port;
 }
 
+METHOD(socket_manager_t, supported_families, socket_family_t,
+       private_socket_manager_t *this)
+{
+       socket_family_t families = SOCKET_FAMILY_NONE;
+       this->lock->read_lock(this->lock);
+       if (this->socket)
+       {
+               families = this->socket->supported_families(this->socket);
+       }
+       this->lock->unlock(this->lock);
+       return families;
+}
+
 static void create_socket(private_socket_manager_t *this)
 {
        socket_constructor_t create;
@@ -167,6 +180,7 @@ socket_manager_t *socket_manager_create()
                        .send = _sender,
                        .receive = _receiver,
                        .get_port = _get_port,
+                       .supported_families = _supported_families,
                        .add_socket = _add_socket,
                        .remove_socket = _remove_socket,
                        .destroy = _destroy,
index 1909d1f..a07d080 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 Tobias Brunner
+ * Copyright (C) 2010-2013 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  * Copyright (C) 2010 Martin Willi
  * Copyright (C) 2010 revosec AG
@@ -40,7 +40,7 @@ struct socket_manager_t {
         *                                              - SUCCESS when packet successfully received
         *                                              - FAILED when unable to receive
         */
-       status_t (*receive) (socket_manager_t *this, packet_t **packet);
+       status_t (*receive)(socket_manager_t *this, packet_t **packet);
 
        /**
         * Send a packet using the registered socket.
@@ -50,7 +50,7 @@ struct socket_manager_t {
         *                                              - SUCCESS when packet successfully sent
         *                                              - FAILED when unable to send
         */
-       status_t (*send) (socket_manager_t *this, packet_t *packet);
+       status_t (*send)(socket_manager_t *this, packet_t *packet);
 
        /**
         * Get the port the registered socket is listening on.
@@ -58,7 +58,14 @@ struct socket_manager_t {
         * @param nat_t                 TRUE to get the port used to float in case of NAT-T
         * @return                              the port, or 0, if no socket is registered
         */
-       u_int16_t (*get_port) (socket_manager_t *this, bool nat_t);
+       u_int16_t (*get_port)(socket_manager_t *this, bool nat_t);
+
+       /**
+        * Get the address families the registered socket is listening on.
+        *
+        * @return                              address families
+        */
+       socket_family_t (*supported_families)(socket_manager_t *this);
 
        /**
         * Register a socket constructor.
index 494bf57..c1ed22e 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-2010 Martin Willi
  * Copyright (C) 2005 Jan Hutter
@@ -501,6 +501,22 @@ METHOD(socket_t, get_port, u_int16_t,
        return nat_t ? this->natt : this->port;
 }
 
+METHOD(socket_t, supported_families, socket_family_t,
+       private_socket_default_socket_t *this)
+{
+       socket_family_t families = SOCKET_FAMILY_NONE;
+
+       if (this->ipv4 != -1 || this->ipv4_natt != -1)
+       {
+               families |= SOCKET_FAMILY_IPV4;
+       }
+       if (this->ipv6 != -1 || this->ipv6_natt != -1)
+       {
+               families |= SOCKET_FAMILY_IPV6;
+       }
+       return families;
+}
+
 /**
  * open a socket to send and receive packets
  */
@@ -671,6 +687,7 @@ socket_default_socket_t *socket_default_socket_create()
                                .send = _sender,
                                .receive = _receiver,
                                .get_port = _get_port,
+                               .supported_families = _supported_families,
                                .destroy = _destroy,
                        },
                },
index b7c7394..aecbd52 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-2010 Martin Willi
  * Copyright (C) 2005 Jan Hutter
@@ -620,6 +620,14 @@ METHOD(socket_t, get_port, u_int16_t,
        return 0;
 }
 
+METHOD(socket_t, supported_families, socket_family_t,
+       private_socket_dynamic_socket_t *this)
+{
+       /* we could return only the families of the opened sockets, but it could
+        * be that both families are supported even if no socket is yet open */
+       return SOCKET_FAMILY_BOTH;
+}
+
 METHOD(socket_t, destroy, void,
        private_socket_dynamic_socket_t *this)
 {
@@ -654,6 +662,7 @@ socket_dynamic_socket_t *socket_dynamic_socket_create()
                                .send = _sender,
                                .receive = _receiver,
                                .get_port = _get_port,
+                               .supported_families = _supported_families,
                                .destroy = _destroy,
                        },
                },