From 5ccee1e1b115a5b39e6bad7b7d45d5188f885b7d Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 20 Nov 2023 12:24:03 +0100 Subject: 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 --- include/osmocom/netif/stream.h | 2 ++ src/stream_cli.c | 16 ++++++++++------ src/stream_srv.c | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) 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_set_closed_cb(struct osmo_stream_srv *conn, int (*closed_cb 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_set_reconnect_timeout(struct osmo_stream_cli *cli, int time 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..1e7ebeb 100644 --- a/src/stream_cli.c +++ b/src/stream_cli.c @@ -210,7 +210,11 @@ void osmo_stream_cli_close(struct osmo_stream_cli *cli) } } -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 of which we want to obtain the file descriptor + * \returns File descriptor or negative in case of error */ +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 @@ static int _setsockopt_nosigpipe(struct osmo_stream_cli *cli) #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 @@ static void stream_cli_handle_connecting(struct osmo_stream_cli *cli, int res) 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 @@ static void stream_cli_handle_connecting(struct osmo_stream_cli *cli, int res) 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 @@ char *osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli) { 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 osmo_stream_cli_open(struct osmo_stream_cli *cli) 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 @@ osmo_stream_srv_get_ofd(struct osmo_stream_srv *conn) 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 */ -- cgit v1.2.3