Added get_port() method to socket_t to learn the listening port.
authorTobias Brunner <tobias@strongswan.org>
Fri, 20 Apr 2012 08:38:31 +0000 (10:38 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 8 Aug 2012 13:12:25 +0000 (15:12 +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
src/libcharon/plugins/socket_raw/socket_raw_socket.c

index be87503..4a4ef52 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2010 Tobias Brunner
+ * Copyright (C) 2006-2012 Tobias Brunner
  * Copyright (C) 2005-2010 Martin Willi
  * Copyright (C) 2006 Daniel Roethlisberger
  * Copyright (C) 2005 Jan Hutter
@@ -68,6 +68,14 @@ struct socket_t {
        status_t (*send) (socket_t *this, packet_t *packet);
 
        /**
+        * Get the port this socket is listening on.
+        *
+        * @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);
+
+       /**
         * Destroy a socket implementation.
         */
        void (*destroy) (socket_t *this);
index 72a4543..d2736de 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Tobias Brunner
+ * Copyright (C) 2010-2012 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  * Copyright (C) 2010 Martin Willi
  * Copyright (C) 2010 revosec AG
@@ -89,6 +89,19 @@ METHOD(socket_manager_t, sender, status_t,
        return status;
 }
 
+METHOD(socket_manager_t, get_port, u_int16_t,
+       private_socket_manager_t *this, bool nat_t)
+{
+       u_int16_t port = 0;
+       this->lock->read_lock(this->lock);
+       if (this->socket)
+       {
+               port = this->socket->get_port(this->socket, nat_t);
+       }
+       this->lock->unlock(this->lock);
+       return port;
+}
+
 static void create_socket(private_socket_manager_t *this)
 {
        socket_constructor_t create;
@@ -153,6 +166,7 @@ socket_manager_t *socket_manager_create()
                .public = {
                        .send = _sender,
                        .receive = _receiver,
+                       .get_port = _get_port,
                        .add_socket = _add_socket,
                        .remove_socket = _remove_socket,
                        .destroy = _destroy,
index 94185d2..1909d1f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Tobias Brunner
+ * Copyright (C) 2010-2012 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  * Copyright (C) 2010 Martin Willi
  * Copyright (C) 2010 revosec AG
@@ -53,6 +53,14 @@ struct socket_manager_t {
        status_t (*send) (socket_manager_t *this, packet_t *packet);
 
        /**
+        * Get the port the registered socket is listening on.
+        *
+        * @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);
+
+       /**
         * Register a socket constructor.
         *
         * @param create                constructor for the socket
index 8da574a..beab3ea 100644 (file)
@@ -305,7 +305,7 @@ METHOD(socket_t, sender, status_t,
        /* send data */
        sport = src->get_port(src);
        family = dst->get_family(dst);
-       if (sport == CHARON_UDP_PORT)
+       if (sport == 0 || sport == CHARON_UDP_PORT)
        {
                if (family == AF_INET)
                {
@@ -405,6 +405,12 @@ METHOD(socket_t, sender, status_t,
        return SUCCESS;
 }
 
+METHOD(socket_t, get_port, u_int16_t,
+       private_socket_default_socket_t *this, bool nat_t)
+{
+       return nat_t ? CHARON_NATT_PORT : CHARON_UDP_PORT;
+}
+
 /**
  * open a socket to send and receive packets
  */
@@ -533,6 +539,7 @@ socket_default_socket_t *socket_default_socket_create()
                        .socket = {
                                .send = _sender,
                                .receive = _receiver,
+                               .get_port = _get_port,
                                .destroy = _destroy,
                        },
                },
index 3c3b17b..e0e0af6 100644 (file)
@@ -467,6 +467,7 @@ METHOD(socket_t, sender, status_t,
        dst = packet->get_destination(packet);
        family = src->get_family(src);
        port = src->get_port(src);
+       port = port ?: CHARON_UDP_PORT;
        skt = find_socket(this, family, port);
        if (!skt)
        {
@@ -534,6 +535,14 @@ METHOD(socket_t, sender, status_t,
        return SUCCESS;
 }
 
+METHOD(socket_t, get_port, u_int16_t,
+       private_socket_dynamic_socket_t *this, bool nat_t)
+{
+       /* we return 0 here for users that have no explicit port configured, the
+        * sender will default to the default port in this case */
+       return 0;
+}
+
 METHOD(socket_t, destroy, void,
        private_socket_dynamic_socket_t *this)
 {
@@ -567,6 +576,7 @@ socket_dynamic_socket_t *socket_dynamic_socket_create()
                        .socket = {
                                .send = _sender,
                                .receive = _receiver,
+                               .get_port = _get_port,
                                .destroy = _destroy,
                        },
                },
index 37d13f1..1b3c9e1 100644 (file)
@@ -309,7 +309,7 @@ METHOD(socket_t, sender, status_t,
        /* send data */
        sport = src->get_port(src);
        family = dst->get_family(dst);
-       if (sport == CHARON_UDP_PORT)
+       if (sport == 0 || sport == CHARON_UDP_PORT)
        {
                if (family == AF_INET)
                {
@@ -470,6 +470,12 @@ static int open_send_socket(private_socket_raw_socket_t *this,
        return skt;
 }
 
+METHOD(socket_t, get_port, u_int16_t,
+       private_socket_raw_socket_t *this, bool nat_t)
+{
+       return nat_t ? CHARON_NATT_PORT : CHARON_UDP_PORT;
+}
+
 /**
  * open a socket to receive packets
  */
@@ -616,6 +622,7 @@ socket_raw_socket_t *socket_raw_socket_create()
                        .socket = {
                                .send = _sender,
                                .receive = _receiver,
+                               .get_port = _get_port,
                                .destroy = _destroy,
                        },
                },