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


Change subject: ss7_asp_peer: Improvements in ss7_asp_peer_snprintf()
......................................................................

ss7_asp_peer: Improvements in ss7_asp_peer_snprintf()

Convert it to osmo_strbuf, which simplifies the logic of the function,
taking osmo_multiaddr_ip_and_port_snprintf() as reference to implement it.
As a result:
* Single IPv6 addresses now get properly enclosed in [], eg:
  "[::]:2905".
* We avoid passing NULL to snprintf(), which is unspecified on some
  platforms.

Change-Id: I115951627adb0c6778a9a36159237c3eea130300
---
M src/ss7_asp_peer.c
1 file changed, 24 insertions(+), 23 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmo-sigtran 
refs/changes/53/43753/1

diff --git a/src/ss7_asp_peer.c b/src/ss7_asp_peer.c
index c31cdd9..f49e210 100644
--- a/src/ss7_asp_peer.c
+++ b/src/ss7_asp_peer.c
@@ -53,35 +53,36 @@

 int ss7_asp_peer_snprintf(char *buf, size_t buf_len, struct osmo_ss7_asp_peer 
*peer)
 {
-       int len = 0, offset = 0, rem = buf_len;
-       int ret, i;
-       char *after;
+       struct osmo_strbuf sb = { .buf = buf, .len = buf_len };
+       bool is_v6 = false;
+       unsigned int i;
        char *primary;

-       if (buf_len < 3)
-               return -EINVAL;
-
-       if (peer->host_cnt > 1) {
-               ret = snprintf(buf, rem, "(");
-               if (ret < 0)
-                       return ret;
-               OSMO_SNPRINTF_RET(ret, rem, offset, len);
+       if (peer->host_cnt == 0) {
+               OSMO_STRBUF_PRINTF(sb, "NULL:%u", peer->port);
+               return sb.chars_needed;
        }
-       for (i = 0; i < peer->host_cnt; i++) {
+
+       if (peer->host_cnt > 1)
+               OSMO_STRBUF_PRINTF(sb, "(");
+       if ((is_v6 = !!strchr(peer->host[0], ':'))) /* IPv6, add [] to separate 
from port. */
+               OSMO_STRBUF_PRINTF(sb, "[");
+
+       for (i = 0; i < peer->host_cnt - 1; i++) {
                primary = (peer->idx_primary == i) ? "*" : "";
-               if (peer->host_cnt == 1)
-                       after = "";
-               else
-                       after = (i == (peer->host_cnt - 1)) ? ")" : "|";
-               ret = snprintf(buf + offset, rem, "%s%s%s", peer->host[i] ? : 
"0.0.0.0", primary, after);
-               OSMO_SNPRINTF_RET(ret, rem, offset, len);
+               OSMO_STRBUF_PRINTF(sb, "%s%s|", peer->host[i] ? : "NULL", 
primary);
        }
-       ret = snprintf(buf + offset, rem, ":%u", peer->port);
-       if (ret < 0)
-               return ret;
-       OSMO_SNPRINTF_RET(ret, rem, offset, len);
+       primary = (peer->idx_primary == i) ? "*" : "";
+       OSMO_STRBUF_PRINTF(sb, "%s%s", peer->host[i] ? : "NULL", primary);

-       return len;
+       if (peer->host_cnt > 1)
+               OSMO_STRBUF_PRINTF(sb, ")");
+       else if (is_v6)
+               OSMO_STRBUF_PRINTF(sb, "]");
+
+       OSMO_STRBUF_PRINTF(sb, ":%u", peer->port);
+
+       return sb.chars_needed;
 }

 /*! \brief Set (copy) addresses for a given ASP peer. Previous addresses are 
freed.

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

Gerrit-MessageType: newchange
Gerrit-Project: libosmo-sigtran
Gerrit-Branch: master
Gerrit-Change-Id: I115951627adb0c6778a9a36159237c3eea130300
Gerrit-Change-Number: 43753
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <[email protected]>

Reply via email to