Change in libosmo-netif[master]: Add descriptor functions to stream client/server

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Mon Feb 4 14:05:45 UTC 2019


Max has uploaded this change for review. ( https://gerrit.osmocom.org/12827


Change subject: Add descriptor functions to stream client/server
......................................................................

Add descriptor functions to stream client/server

Add functions to get the description of a server link or client
connection which examine data on corresponding socket.

Those functions use static buffers and intended for single use in
log/printf statements as illustarted by corresponding example changes.

Change-Id: If9a8e211da85956781479862a63c4fc6e53ed6be
---
M examples/stream-client.c
M examples/stream-server.c
M include/osmocom/netif/stream.h
M src/stream.c
4 files changed, 35 insertions(+), 2 deletions(-)



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

diff --git a/examples/stream-client.c b/examples/stream-client.c
index 6178dcd..b283ee4 100644
--- a/examples/stream-client.c
+++ b/examples/stream-client.c
@@ -39,7 +39,7 @@
 
 static int connect_cb(struct osmo_stream_cli *conn)
 {
-	LOGP(DSTREAMTEST, LOGL_NOTICE, "connected\n");
+	LOGP(DSTREAMTEST, LOGL_NOTICE, "connected: %s\n", osmo_stream_cli_get_descr(conn));
 	return 0;
 }
 
diff --git a/examples/stream-server.c b/examples/stream-server.c
index 14b4123..f54b763 100644
--- a/examples/stream-server.c
+++ b/examples/stream-server.c
@@ -171,7 +171,7 @@
 	kbd_ofd->cb = kbd_cb;
 	osmo_fd_register(kbd_ofd);
 
-	LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop\n");
+	LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop on %s\n", osmo_stream_srv_link_get_descr(srv));
 
 	while(1) {
 		osmo_select_main(0);
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index 969d42f..7015ce9 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -27,6 +27,7 @@
 void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)(struct osmo_stream_srv_link *link, int fd));
 void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data);
 void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link);
+char *osmo_stream_srv_link_get_descr(const struct osmo_stream_srv_link *link);
 struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link);
 
 int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link);
@@ -60,6 +61,7 @@
 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);
+char *osmo_stream_cli_get_descr(const struct osmo_stream_cli *cli);
 struct osmo_fd *osmo_stream_cli_get_ofd(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_read_cb(struct osmo_stream_cli *cli, int (*read_cb)(struct osmo_stream_cli *cli));
diff --git a/src/stream.c b/src/stream.c
index dbbaa9c..b1ea3db 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -395,6 +395,18 @@
 	return cli->data;
 }
 
+/*! \brief Get the stream client socket description.
+ *  \param[in] cli Stream Client to examine
+ *  \returns Socket description or NULL in case of error */
+char *osmo_stream_cli_get_descr(const struct osmo_stream_cli *cli)
+{
+	static char buf[OSMO_SOCK_NAME_MAXLEN];
+
+	osmo_sock_get_name_buf(buf, OSMO_SOCK_NAME_MAXLEN, cli->ofd.fd);
+
+	return buf;
+}
+
 /*! \brief Get Osmocom File Descriptor of the stream client socket
  *  \param[in] cli Stream Client to modify
  *  \returns Pointer to \ref osmo_fd */
@@ -702,6 +714,25 @@
 	return link->data;
 }
 
+/*! \brief Get description of the stream server link e. g. 127.0.0.1:1234
+ *  \param[in] link Stream Server Link to examine
+ *  \returns Link description or NULL in case of error */
+char *osmo_stream_srv_link_get_descr(const struct osmo_stream_srv_link *link)
+{
+	static char buf[INET6_ADDRSTRLEN + 6];
+	int rc = osmo_sock_get_local_ip(link->ofd.fd, buf, INET6_ADDRSTRLEN);
+	if (rc < 0)
+		return NULL;
+
+	buf[strnlen(buf, INET6_ADDRSTRLEN + 6)] = ':';
+
+	rc = osmo_sock_get_local_ip_port(link->ofd.fd, buf + strnlen(buf, INET6_ADDRSTRLEN + 6), 6);
+	if (rc < 0)
+		return NULL;
+
+	return buf;
+}
+
 /*! \brief Get Osmocom File Descriptor of the stream server link
  *  \param[in] link Stream Server Link
  *  \returns Pointer to \ref osmo_fd */

-- 
To view, visit https://gerrit.osmocom.org/12827
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: If9a8e211da85956781479862a63c4fc6e53ed6be
Gerrit-Change-Number: 12827
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190204/fcf9751b/attachment.htm>


More information about the gerrit-log mailing list