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


Change subject: stream: Use new multiaddr APIs to log whole set of sock 
addresses
......................................................................

stream: Use new multiaddr APIs to log whole set of sock addresses

Until now, the used (existing) APIs were only retrieving 1 local and 1
remote address, even if the socket actually handled several, as it's
possible with SCTP multi-homing setups.

Related: SYS#6636
Depends: libosmocore.git Change-Id I48950754ed6f61ee5ffa04a447fab8903f10acc0
Change-Id: I7ae300595825836cc7d6fa07238c0c2f15d14e85
---
M TODO-RELEASE
M include/osmocom/netif/stream.h
M src/stream_cli.c
M src/stream_srv.c
4 files changed, 77 insertions(+), 26 deletions(-)



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

diff --git a/TODO-RELEASE b/TODO-RELEASE
index 2346129..3c05362 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -7,4 +7,5 @@
 # If any interfaces have been added since the last public release: c:r:a + 1.
 # If any interfaces have been removed or changed since the last public 
release: c:r:0.
 #library       what            description / commit summary line
-libosmocore >1.9.0  working osmo_sock_init2_multiaddr2() without setting flag 
OSMO_SOCK_F_BIND
\ No newline at end of file
+libosmocore >1.9.0  working osmo_sock_init2_multiaddr2() without setting flag 
OSMO_SOCK_F_BIND
+libosmocore >1.9.0  use osmo_sock_multiaddr_get_name_buf(), 
osmo_sock_multiaddr_get_ip_and_port()
\ No newline at end of file
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index 218b635..946aee0 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -36,7 +36,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_sockname(const struct osmo_stream_srv_link 
*link);
+const char *osmo_stream_srv_link_get_sockname(const struct 
osmo_stream_srv_link *link);
 struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link 
*link);
 bool osmo_stream_srv_link_is_opened(const struct osmo_stream_srv_link *link);
 int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link);
diff --git a/src/stream_cli.c b/src/stream_cli.c
index 4f2963d..e075161 100644
--- a/src/stream_cli.c
+++ b/src/stream_cli.c
@@ -96,7 +96,7 @@

 struct osmo_stream_cli {
        char *name;
-       char sockname[OSMO_SOCK_NAME_MAXLEN];
+       char sockname[OSMO_STREAM_MAX_ADDRS * OSMO_SOCK_NAME_MAXLEN];
        enum osmo_stream_mode mode;
        union {
                struct osmo_fd                  ofd;
@@ -351,7 +351,8 @@
                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_get_fd(cli));
+       osmo_sock_multiaddr_get_name_buf(cli->sockname, sizeof(cli->sockname),
+                                        osmo_stream_cli_get_fd(cli), 
cli->proto);

        LOGSCLI(cli, LOGL_INFO, "connection established\n");
        cli->state = STREAM_CLI_STATE_CONNECTED;
@@ -685,9 +686,10 @@
  *  \returns Socket description or NULL in case of error */
 char *osmo_stream_cli_get_sockname(const struct osmo_stream_cli *cli)
 {
-       static char buf[OSMO_SOCK_NAME_MAXLEN];
+       static char buf[OSMO_STREAM_MAX_ADDRS * OSMO_SOCK_NAME_MAXLEN];

-       osmo_sock_get_name_buf(buf, OSMO_SOCK_NAME_MAXLEN, 
osmo_stream_cli_get_fd(cli));
+       osmo_sock_multiaddr_get_name_buf(buf, sizeof(buf),
+                                        osmo_stream_cli_get_fd(cli), 
cli->proto);

        return buf;
 }
@@ -989,7 +991,7 @@
 #ifdef HAVE_LIBSCTP
                case IPPROTO_SCTP:
                {
-                       char log_pfx[128];
+                       char log_pfx[4096];
                        snprintf(log_pfx, sizeof(log_pfx), "CLICONN(%s,%s)", 
cli->name ? : "", cli->sockname);
                        ret = stream_sctp_recvmsg_wrapper(cli->ofd.fd, msg, 
log_pfx);
                        break;
diff --git a/src/stream_srv.c b/src/stream_srv.c
index 42b04ad..75b7526 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,23 +338,55 @@
        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 is_v6 = false;
+       bool need_more_bufs;
+       unsigned int i;
+       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;
+
+       is_v6 = false;
+       need_more_bufs = num_hostbuf > ARRAY_SIZE(hostbuf);
+       if (need_more_bufs)
+               num_hostbuf = ARRAY_SIZE(hostbuf);
+
+       if (num_hostbuf > 1)
+               OSMO_STRBUF_PRINTF(sb, "(");
+       else if ((is_v6 = !!strchr(hostbuf[0], ':'))) /* IPv6, add [] to 
separate from port. */
+               OSMO_STRBUF_PRINTF(sb, "[");
+
+       for (i = 0; i < num_hostbuf - 1; i++)
+               OSMO_STRBUF_PRINTF(sb, "%s|", hostbuf[i]);
+       OSMO_STRBUF_PRINTF(sb, "%s", hostbuf[i]);
+
+       if (num_hostbuf > 1)
+               OSMO_STRBUF_PRINTF(sb, ")");
+       else if (is_v6)
+               OSMO_STRBUF_PRINTF(sb, "]");
+       if (need_more_bufs)
+               OSMO_STRBUF_PRINTF(sb, "<need-more-bufs!>");
+       OSMO_STRBUF_PRINTF(sb, ":%s", portbuf);
+
+       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)
+const 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;
-
-       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;
+       return link->sockname;
 }

 /*! \brief Get Osmocom File Descriptor of the stream server link
@@ -435,7 +467,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;
 }

@@ -513,7 +545,7 @@
 struct osmo_stream_srv {
        struct osmo_stream_srv_link     *srv;
        char                            *name;
-       char                            sockname[OSMO_SOCK_NAME_MAXLEN];
+       char                            sockname[OSMO_STREAM_MAX_ADDRS * 
OSMO_SOCK_NAME_MAXLEN];
        enum osmo_stream_mode mode;
        union {
                struct osmo_fd                  ofd;
@@ -704,7 +736,8 @@
        conn->data = data;
        INIT_LLIST_HEAD(&conn->tx_queue);

-       osmo_sock_get_name_buf(conn->sockname, sizeof(conn->sockname), fd);
+       osmo_sock_multiaddr_get_name_buf(conn->sockname, sizeof(conn->sockname),
+                                        fd, link->proto);

        if (osmo_fd_register(&conn->ofd) < 0) {
                LOGSSRV(conn, LOGL_ERROR, "could not register FD\n");
@@ -734,7 +767,7 @@
        conn->mode = OSMO_STREAM_MODE_OSMO_IO;
        conn->srv = link;

-       osmo_sock_get_name_buf(conn->sockname, sizeof(conn->sockname), fd);
+       osmo_sock_multiaddr_get_name_buf(conn->sockname, 
sizeof(conn->sockname), fd, link->proto);

        conn->iofd = osmo_iofd_setup(conn, fd, conn->sockname,
                                     OSMO_IO_FD_MODE_READ_WRITE, &srv_ioops, 
conn);
@@ -949,7 +982,7 @@
 #ifdef HAVE_LIBSCTP
                case IPPROTO_SCTP:
                {
-                       char log_pfx[128];
+                       char log_pfx[4096];
                        snprintf(log_pfx, sizeof(log_pfx), "SRV(%s,%s)", 
conn->name ? : "", conn->sockname);
                        ret = stream_sctp_recvmsg_wrapper(conn->ofd.fd, msg, 
log_pfx);
                        break;

--
To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/35240?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: I7ae300595825836cc7d6fa07238c0c2f15d14e85
Gerrit-Change-Number: 35240
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pes...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to