pespin has uploaded this change for review.
stream_srv_link: osmo_stream_srv_link_get_sockname() now returns the full set of addresses
As a result, internal stream_srv_link logging will also show the whole
set of listening addresses. This is mostly fine since it mainly happens
only once, during connection accept(), and this way it provides full
view of where from and where to the client connected.
Related: SYS#5581
Change-Id: I216502a9aeafe638940f110bc9fddf2504b2ac3a
---
M src/stream_srv.c
1 file changed, 47 insertions(+), 12 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/87/35287/1
diff --git a/src/stream_srv.c b/src/stream_srv.c
index 0dca10a..98b996b 100644
--- a/src/stream_srv.c
+++ b/src/stream_srv.c
@@ -85,7 +85,7 @@
struct osmo_stream_srv_link {
struct osmo_fd ofd;
char *name;
- char sockname[OSMO_SOCK_NAME_MAXLEN];
+ char sockname[OSMO_STREAM_MAX_ADDRS * INET6_ADDRSTRLEN + OSMO_STREAM_MAX_ADDRS + 2 + 6 + 1];
char *addr[OSMO_STREAM_MAX_ADDRS];
uint8_t addrcnt;
uint16_t port;
@@ -338,22 +338,42 @@
return link->data;
}
+/* Similar to osmo_sock_multiaddr_get_name_buf(), but aimed at listening sockets (only local part): */
+static char *get_local_sockname_buf(char *buf, size_t buf_len, int fd, int proto)
+{
+ char hostbuf[OSMO_STREAM_MAX_ADDRS][INET6_ADDRSTRLEN];
+ size_t num_hostbuf = ARRAY_SIZE(hostbuf);
+ char portbuf[6];
+ struct osmo_strbuf sb = { .buf = buf, .len = buf_len };
+ bool need_more_bufs;
+ int rc;
+
+ rc = osmo_sock_multiaddr_get_ip_and_port(fd, proto, &hostbuf[0][0],
+ &num_hostbuf, sizeof(hostbuf[0]),
+ portbuf, sizeof(portbuf), true);
+ if (rc < 0)
+ return NULL;
+
+ need_more_bufs = num_hostbuf > ARRAY_SIZE(hostbuf);
+ if (need_more_bufs)
+ num_hostbuf = ARRAY_SIZE(hostbuf);
+ OSMO_STRBUF_APPEND(sb, osmo_multiaddr_ip_and_port_snprintf,
+ &hostbuf[0][0], num_hostbuf, sizeof(hostbuf[0]), portbuf);
+ if (need_more_bufs)
+ OSMO_STRBUF_PRINTF(sb, "<need-more-bufs!>");
+
+ return buf;
+}
+
/*! \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_sockname(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;
+ static char buf[sizeof(link->sockname)];
- 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;
-
+ if (!get_local_sockname_buf(buf, sizeof(buf), link->ofd.fd, link->proto))
+ OSMO_STRLCPY_ARRAY(buf, "<sockname-error>");
return buf;
}
@@ -443,7 +463,7 @@
return -EIO;
}
- OSMO_STRLCPY_ARRAY(link->sockname, osmo_stream_srv_link_get_sockname(link));
+ get_local_sockname_buf(link->sockname, sizeof(link->sockname), link->ofd.fd, link->proto);
return 0;
}
To view, visit change 35287. To unsubscribe, or for help writing mail filters, visit settings.