laforge has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmo-netif/+/35072?usp=email )


Change subject: Introduce generic osmo_stream_{cli,srv}_get_fd() API
......................................................................

Introduce generic osmo_stream_{cli,srv}_get_fd() API

The old osmo_stream_{cli,srv}_get_ofd() API only works for streams
in OSMO_FD mode.  However, it is legitimate for an application
wanting to get low-level access to the file descriptor, for example
to issue some {get,set}sockopt() calls on it.

Change-Id: Ib0737f21150f6ac8d524b92c7ddb098f2afdeaab
Related: OS#5753
---
M include/osmocom/netif/stream.h
M src/stream_cli.c
M src/stream_srv.c
3 files changed, 45 insertions(+), 6 deletions(-)



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

diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index a24244c..218b635 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -64,6 +64,7 @@
 void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn);
 struct osmo_stream_srv_link *osmo_stream_srv_get_master(struct osmo_stream_srv 
*conn);
 struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv);
+int osmo_stream_srv_get_fd(const struct osmo_stream_srv *srv);
 void osmo_stream_srv_destroy(struct osmo_stream_srv *conn);

 void osmo_stream_srv_set_flush_and_destroy(struct osmo_stream_srv *conn);
@@ -96,6 +97,7 @@
 void *osmo_stream_cli_get_data(struct osmo_stream_cli *cli);
 char *osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli);
 struct osmo_fd *osmo_stream_cli_get_ofd(struct osmo_stream_cli *cli);
+int osmo_stream_cli_get_fd(const struct osmo_stream_cli *cli);
 void osmo_stream_cli_set_connect_cb(struct osmo_stream_cli *cli, int 
(*connect_cb)(struct osmo_stream_cli *cli));
 void osmo_stream_cli_set_disconnect_cb(struct osmo_stream_cli *cli, int 
(*disconnect_cb)(struct osmo_stream_cli *cli));
 void osmo_stream_cli_set_read_cb(struct osmo_stream_cli *cli, int 
(*read_cb)(struct osmo_stream_cli *cli));
diff --git a/src/stream_cli.c b/src/stream_cli.c
index ef571cc..6769051 100644
--- a/src/stream_cli.c
+++ b/src/stream_cli.c
@@ -210,7 +210,11 @@
        }
 }

-static inline int osmo_stream_cli_fd(const struct osmo_stream_cli *cli)
+/*! \brief Get File Descriptor of the stream client socket
+ *  \param[in] cli Stream Client to modify
+ *  \returns File descriptor */
+int
+osmo_stream_cli_get_fd(const struct osmo_stream_cli *cli)
 {
        switch (cli->mode) {
        case OSMO_STREAM_MODE_OSMO_FD:
@@ -314,7 +318,7 @@
 #ifdef SO_NOSIGPIPE
        int ret;
        int val = 1;
-       ret = setsockopt(osmo_stream_cli_fd(cli), SOL_SOCKET, SO_NOSIGPIPE, 
(void *)&val, sizeof(val));
+       ret = setsockopt(osmo_stream_cli_get_fd(cli), SOL_SOCKET, SO_NOSIGPIPE, 
(void *)&val, sizeof(val));
        if (ret < 0)
                LOGSCLI(cli, LOGL_ERROR, "Failed setting SO_NOSIGPIPE: %s\n", 
strerror(errno));
        return ret;
@@ -328,7 +332,7 @@
        int error, ret = res;
        socklen_t len = sizeof(error);

-       int fd = osmo_stream_cli_fd(cli);
+       int fd = osmo_stream_cli_get_fd(cli);
        OSMO_ASSERT(fd >= 0);

        if (ret < 0) {
@@ -347,7 +351,7 @@
                osmo_fd_write_disable(&cli->ofd);

        /* Update sockname based on socket info: */
-       osmo_sock_get_name_buf(cli->sockname, sizeof(cli->sockname), 
osmo_stream_cli_fd(cli));
+       osmo_sock_get_name_buf(cli->sockname, sizeof(cli->sockname), 
osmo_stream_cli_get_fd(cli));

        LOGSCLI(cli, LOGL_INFO, "connection established\n");
        cli->state = STREAM_CLI_STATE_CONNECTED;
@@ -683,7 +687,7 @@
 {
        static char buf[OSMO_SOCK_NAME_MAXLEN];

-       osmo_sock_get_name_buf(buf, OSMO_SOCK_NAME_MAXLEN, 
osmo_stream_cli_fd(cli));
+       osmo_sock_get_name_buf(buf, OSMO_SOCK_NAME_MAXLEN, 
osmo_stream_cli_get_fd(cli));
 
        return buf;
 }
@@ -837,7 +841,7 @@
        int ret, fd = -1;

        /* we are reconfiguring this socket, close existing first. */
-       if ((cli->flags & OSMO_STREAM_CLI_F_RECONF) && osmo_stream_cli_fd(cli) 
>= 0)
+       if ((cli->flags & OSMO_STREAM_CLI_F_RECONF) && 
osmo_stream_cli_get_fd(cli) >= 0)
                osmo_stream_cli_close(cli);

        cli->flags &= ~OSMO_STREAM_CLI_F_RECONF;
diff --git a/src/stream_srv.c b/src/stream_srv.c
index 36e21cc..42b04ad 100644
--- a/src/stream_srv.c
+++ b/src/stream_srv.c
@@ -838,6 +838,24 @@
        return &conn->ofd;
 }

+/*! \brief Get File Descriptor of the stream server
+ *  \param[in] conn Stream Server
+ *  \returns file descriptor or negative on error */
+int
+osmo_stream_srv_get_fd(const struct osmo_stream_srv *conn)
+{
+       switch (conn->mode) {
+       case OSMO_STREAM_MODE_OSMO_FD:
+               return conn->ofd.fd;
+       case OSMO_STREAM_MODE_OSMO_IO:
+               if (conn->iofd)
+                       return osmo_iofd_get_fd(conn->iofd);
+       default:
+               break;
+       }
+       return -EINVAL;
+}
+
 /*! \brief Get the master (Link) from a Stream Server
  *  \param[in] conn Stream Server of which we want to know the Link
  *  \returns Link through which the given Stream Server is established */

--
To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/35072?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-Change-Id: Ib0737f21150f6ac8d524b92c7ddb098f2afdeaab
Gerrit-Change-Number: 35072
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <lafo...@osmocom.org>
Gerrit-MessageType: newchange

Reply via email to