Review at  https://gerrit.osmocom.org/2251

stream+datagram: Allow local bind + connect for client sockets

This uses the new osmo_sock_init2() features introduced in libosmocore
Change-Id Idab124bcca47872f55311a82d6818aed590965e6 to bind *and*
connect a given socket during creation.

Change-Id: I013f4cc10b26d332d52d231f252bb0f03df8c54b
---
M include/osmocom/netif/datagram.h
M include/osmocom/netif/stream.h
M src/datagram.c
M src/stream.c
4 files changed, 48 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/51/2251/1

diff --git a/include/osmocom/netif/datagram.h b/include/osmocom/netif/datagram.h
index 33d3d30..b7ecfe3 100644
--- a/include/osmocom/netif/datagram.h
+++ b/include/osmocom/netif/datagram.h
@@ -8,6 +8,8 @@
 
 void osmo_dgram_tx_set_addr(struct osmo_dgram_tx *conn, const char *addr);
 void osmo_dgram_tx_set_port(struct osmo_dgram_tx *conn, uint16_t port);
+void osmo_dgram_tx_set_local_addr(struct osmo_dgram_tx *conn, const char 
*addr);
+void osmo_dgram_tx_set_local_port(struct osmo_dgram_tx *conn, uint16_t port);
 void osmo_dgram_tx_set_data(struct osmo_dgram_tx *conn, void *data);
 
 int osmo_dgram_tx_open(struct osmo_dgram_tx *conn);
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index 254b4c5..63eccf8 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -48,6 +48,8 @@
 void osmo_stream_cli_set_addr(struct osmo_stream_cli *cli, const char *addr);
 void osmo_stream_cli_set_port(struct osmo_stream_cli *cli, uint16_t port);
 void osmo_stream_cli_set_proto(struct osmo_stream_cli *cli, uint16_t proto);
+void osmo_stream_cli_set_local_addr(struct osmo_stream_cli *cli, const char 
*addr);
+void osmo_stream_cli_set_local_port(struct osmo_stream_cli *cli, uint16_t 
port);
 void osmo_stream_cli_set_data(struct osmo_stream_cli *cli, void *data);
 void osmo_stream_cli_set_reconnect_timeout(struct osmo_stream_cli *cli, int 
timeout);
 void *osmo_stream_cli_get_data(struct osmo_stream_cli *cli);
diff --git a/src/datagram.c b/src/datagram.c
index 6316552..cb2a64f 100644
--- a/src/datagram.c
+++ b/src/datagram.c
@@ -12,6 +12,7 @@
 
 #include <osmocom/core/linuxlist.h>
 #include <osmocom/core/select.h>
+#include <osmocom/core/utils.h>
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/logging.h>
@@ -40,6 +41,8 @@
        struct llist_head               tx_queue;
        const char                      *addr;
        uint16_t                        port;
+       char                            *local_addr;
+       uint16_t                        local_port;
        int (*write_cb)(struct osmo_dgram_tx *conn);
        void                            *data;
        unsigned int                    flags;
@@ -140,6 +143,26 @@
        conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
 }
 
+/*! \brief Set the local address from which we transmit
+ *  \param[in] conn Datagram Transmitter to modify
+ *  \param[in] addr Local IP address */
+void
+osmo_dgram_tx_set_local_addr(struct osmo_dgram_tx *conn, const char *addr)
+{
+       osmo_talloc_replace_string(conn, &conn->local_addr, addr);
+       conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
+}
+
+/*! \brief Set the local port from which we transmit
+ *  \param[in] conn Datagram Transmitter to modify
+ *  \param[in] port Local Port Number */
+void
+osmo_dgram_tx_set_local_port(struct osmo_dgram_tx *conn, uint16_t port)
+{
+       conn->local_port = port;
+       conn->flags |= OSMO_DGRAM_CLI_F_RECONF;
+}
+
 /*! \brief Set application private data of the datagram transmitter
  *  \param[in] conn Datagram Transmitter to modify
  *  \param[in] data User-specific data (available in call-back functions) */
@@ -169,9 +192,9 @@
 
        conn->flags &= ~OSMO_DGRAM_CLI_F_RECONF;
 
-       ret = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
-                            conn->addr, conn->port,
-                            OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
+       ret = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+                             conn->local_addr, conn->local_port, conn->addr, 
conn->port,
+                             
OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_NONBLOCK);
        if (ret < 0)
                return ret;
 
diff --git a/src/stream.c b/src/stream.c
index e71e420..52521d7 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -12,6 +12,7 @@
 
 #include <osmocom/core/timer.h>
 #include <osmocom/core/select.h>
+#include <osmocom/core/utils.h>
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/logging.h>
@@ -82,6 +83,7 @@
        enum osmo_stream_cli_state      state;
        const char                      *addr;
        uint16_t                        port;
+       char                            *local_addr;
        uint16_t                        local_port;
        uint16_t                        proto;
        int (*connect_cb)(struct osmo_stream_cli *srv);
@@ -277,7 +279,7 @@
        cli->flags |= OSMO_STREAM_CLI_F_RECONF;
 }
 
-/*! \brief Set the local port number for the socket
+/*! \brief Set the local port number for the socket (to be bound to)
  *  \param[in] cli Stream Client to modify
  *  \param[in] port Local port number
  */
@@ -285,6 +287,17 @@
 osmo_stream_cli_set_local_port(struct osmo_stream_cli *cli, uint16_t port)
 {
        cli->local_port = port;
+       cli->flags |= OSMO_STREAM_CLI_F_RECONF;
+}
+
+/*! \brief Set the local address for the socket (to be bound to)
+ *  \param[in] cli Stream Client to modify
+ *  \param[in] port Local host name
+ */
+void
+osmo_stream_cli_set_local_addr(struct osmo_stream_cli *cli, const char *addr)
+{
+       osmo_talloc_replace_string(cli, &cli->local_addr, addr);
        cli->flags |= OSMO_STREAM_CLI_F_RECONF;
 }
 
@@ -376,9 +389,10 @@
 
        cli->flags &= ~OSMO_STREAM_CLI_F_RECONF;
 
-       ret = osmo_sock_init(AF_INET, SOCK_STREAM, cli->proto,
-                            cli->addr, cli->port,
-                            OSMO_SOCK_F_CONNECT);
+       ret = osmo_sock_init2(AF_INET, SOCK_STREAM, cli->proto,
+                             cli->local_addr, cli->local_port,
+                             cli->addr, cli->port,
+                             OSMO_SOCK_F_CONNECT|OSMO_SOCK_F_BIND);
        if (ret < 0) {
                if (reconnect && errno == ECONNREFUSED)
                        osmo_stream_cli_reconnect(cli);

-- 
To view, visit https://gerrit.osmocom.org/2251
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I013f4cc10b26d332d52d231f252bb0f03df8c54b
Gerrit-PatchSet: 1
Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <[email protected]>

Reply via email to