dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/35254?usp=email )
Change subject: tlv: improve flatten_dict_lists for multiple keys
......................................................................
tlv: improve flatten_dict_lists for multiple keys
when we flatten a list of dicts into a single dict we must make sure
that duplicate key names are not swallowed. This may happen when there
is an array of objects that share the same identifier. When those are
stored all following objects are stored at the same key so that only the
content of the last one survives.
One prominent example where this bug is triggered is the key_reference
in pin_status_template_do. When the MF is selected, there should be 4
key_reference values, but only the last appears.
Change I7f6d03bf323a153f3172853a3ef171cbec8aece7 fixes the problem, but
now the problematic dicts are no longer flattned, which results in a
strange looking output. When append an index to the key, we can still
flatten the dicts and the output looks compact again.
Related: OS#6211
Change-Id: Ie3b418ca3965b0221bbf5f85d7d0e0fbb39d9d87
---
M pySim/tlv.py
1 file changed, 46 insertions(+), 5 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/54/35254/1
diff --git a/pySim/tlv.py b/pySim/tlv.py
index c85d92b..d3c75e2 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -435,17 +435,33 @@
return False
return True
- def are_elements_unique(lod):
- set_of_keys = set([list(x.keys())[0] for x in lod])
- return len(lod) == len(set_of_keys)
+ def add_element_to_newdict(newdict:dict, key:str, element) -> str:
+ newkey = key
+ count = 1
+ while 1:
+ # Test if the new key (which is equal to key at the beginning) is
+ # already in the newdict. If this is a case we append an index
+ # number and try again.
+ if newkey not in newdict and newkey + "_0" not in newdict:
+ # Rename the already existing zeroth element to element_0, so
+ # that we maintain a consistent element counting.
+ if key in newdict and newkey != key:
+ newdict[key + "_0"] = newdict.pop(key)
+ # Store new element under the new key and leave
+ newdict[newkey] = element
+ return
+
+ # Compute the next key to try
+ newkey = key + "_" + str(count)
+ count = count + 1
if isinstance(inp, list):
- if are_all_elements_dict(inp) and are_elements_unique(inp):
+ if are_all_elements_dict(inp):
# flatten into one shared dict
newdict = {}
for e in inp:
key = list(e.keys())[0]
- newdict[key] = e[key]
+ add_element_to_newdict(newdict, key, e[key])
inp = newdict
# process result as any native dict
return {k:flatten_dict_lists(inp[k]) for k in inp.keys()}
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/35254?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie3b418ca3965b0221bbf5f85d7d0e0fbb39d9d87
Gerrit-Change-Number: 35254
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier(a)sysmocom.de>
Gerrit-MessageType: newchange
pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmo-sccp/+/35253?usp=email )
Change subject: vty: show cs7 instance asp: Swap columns SCTP Role and Remote Address list
......................................................................
vty: show cs7 instance asp: Swap columns SCTP Role and Remote Address list
The Remote Address is by far the potentially largest column, as well as
the one with more variable length, so move it to the end for better formatting.
Change-Id: I4854219f8898266ae47b9117ef79dbad30a5b0fd
---
M src/osmo_ss7_vty.c
M tests/vty/ss7_asp_test.vty
2 files changed, 48 insertions(+), 36 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmo-sccp refs/changes/53/35253/1
diff --git a/src/osmo_ss7_vty.c b/src/osmo_ss7_vty.c
index 6ab2401..35f0dda 100644
--- a/src/osmo_ss7_vty.c
+++ b/src/osmo_ss7_vty.c
@@ -1149,9 +1149,9 @@
return CMD_WARNING;
}
- vty_out(vty, " Current Primary Link%s", VTY_NEWLINE);
- vty_out(vty, "ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role%s", VTY_NEWLINE);
- vty_out(vty, "------------ ------------ ------------- ---- ---- ----------------------- ---------%s", VTY_NEWLINE);
+ vty_out(vty, " Current Primary Link%s", VTY_NEWLINE);
+ vty_out(vty, "ASP Name AS Name State Type Role SCTP Role Remote Addresses%s", VTY_NEWLINE);
+ vty_out(vty, "------------ ------------ ------------- ---- ---- --------- -----------------------%s", VTY_NEWLINE);
llist_for_each_entry(asp, &inst->asp_list, list) {
if (asp->cfg.proto == OSMO_SS7_ASP_PROT_IPA && asp->cfg.remote.port == 0 && asp->server) {
@@ -1163,14 +1163,14 @@
snprintf(buf, sizeof(buf), "%s:%s", hostbuf, portbuf);
} else
osmo_ss7_asp_peer_snprintf(buf, sizeof(buf), &asp->cfg.remote);
- vty_out(vty, "%-12s %-12s %-13s %-4s %-4s %-23s %-9s%s",
+ vty_out(vty, "%-12s %-12s %-13s %-4s %-4s %-9s %-23s%s",
asp->cfg.name,
as_list_for_asp(asp, as_buf, sizeof(as_buf)),
asp->fi? osmo_fsm_inst_state_name(asp->fi) : "uninitialized",
get_value_string(osmo_ss7_asp_protocol_vals, asp->cfg.proto),
osmo_str_tolower(get_value_string(osmo_ss7_asp_role_names, asp->cfg.role)),
- buf,
asp->cfg.is_server ? "server" : "client",
+ buf,
VTY_NEWLINE);
}
return CMD_SUCCESS;
diff --git a/tests/vty/ss7_asp_test.vty b/tests/vty/ss7_asp_test.vty
index dcea4ca..73a13b5 100644
--- a/tests/vty/ss7_asp_test.vty
+++ b/tests/vty/ss7_asp_test.vty
@@ -256,27 +256,27 @@
ss7_asp_vty_test(config-cs7-asp)# remote-ip 127.0.0.200
ss7_asp_vty_test(config-cs7-asp)# local-ip 127.0.0.100
ss7_asp_vty_test(config-cs7-asp)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp ? uninitialized m3ua sg 127.0.0.200:12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp ? uninitialized m3ua sg server 127.0.0.200:12345
ss7_asp_vty_test(config-cs7-asp)# remote-ip 127.0.0.201
ss7_asp_vty_test(config-cs7-asp)# local-ip 127.0.0.101
ss7_asp_vty_test(config-cs7-asp)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp ? uninitialized m3ua sg (127.0.0.200|127.0.0.201):12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp ? uninitialized m3ua sg server (127.0.0.200|127.0.0.201):12345
ss7_asp_vty_test(config-cs7-asp)# ! Mark as primary:
ss7_asp_vty_test(config-cs7-asp)# remote-ip 127.0.0.201 primary
ss7_asp_vty_test(config-cs7-asp)# ! 'local-ip 127.0.0.101 primary' cannot be tested here since output may be different based on sysctl available
ss7_asp_vty_test(config-cs7-asp)# local-ip 127.0.0.101
...
ss7_asp_vty_test(config-cs7-asp)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp ? uninitialized m3ua sg (127.0.0.200|127.0.0.201*):12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp ? uninitialized m3ua sg server (127.0.0.200|127.0.0.201*):12345
ss7_asp_vty_test(config-cs7-asp)# show running-config
...
local-ip 127.0.0.100
@@ -289,10 +289,10 @@
ss7_asp_vty_test(config-cs7-asp)# remote-ip 127.0.0.201
ss7_asp_vty_test(config-cs7-asp)# local-ip 127.0.0.101
ss7_asp_vty_test(config-cs7-asp)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp ? uninitialized m3ua sg (127.0.0.200|127.0.0.201):12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp ? uninitialized m3ua sg server (127.0.0.200|127.0.0.201):12345
ss7_asp_vty_test(config-cs7-asp)# show running-config
...
local-ip 127.0.0.100
@@ -361,26 +361,26 @@
ss7_asp_vty_test(config-cs7-as)# routing-key 0 3.2.1
ss7_asp_vty_test(config-cs7-as)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp my-ass ASP_DOWN m3ua sg (127.0.0.200|127.0.0.201):12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp my-ass ASP_DOWN m3ua sg server (127.0.0.200|127.0.0.201):12345
ss7_asp_vty_test(config-cs7-as)# exit
ss7_asp_vty_test(config-cs7)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp my-ass ASP_DOWN m3ua sg (127.0.0.200|127.0.0.201):12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp my-ass ASP_DOWN m3ua sg server (127.0.0.200|127.0.0.201):12345
ss7_asp_vty_test(config-cs7)# exit
ss7_asp_vty_test(config)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
-my-asp my-ass ASP_DOWN m3ua sg (127.0.0.200|127.0.0.201):12345 server
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
+my-asp my-ass ASP_DOWN m3ua sg server (127.0.0.200|127.0.0.201):12345
ss7_asp_vty_test(config)# do show cs7 instance 0 as all
Routing Routing Key Cic Cic Traffic
@@ -423,9 +423,9 @@
ss7_asp_vty_test(config-cs7)# no asp my-asp
ss7_asp_vty_test(config-cs7)# do show cs7 instance 0 asp
- Current Primary Link
-ASP Name AS Name State Type Role Remote IPaddr & Port SCTP Role
------------- ------------ ------------- ---- ---- ----------------------- ---------
+ Current Primary Link
+ASP Name AS Name State Type Role SCTP Role Remote Addresses
+------------ ------------ ------------- ---- ---- --------- -----------------------
ss7_asp_vty_test(config-cs7)# do show cs7 instance 0 as all
Routing Routing Key Cic Cic Traffic
--
To view, visit https://gerrit.osmocom.org/c/libosmo-sccp/+/35253?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: libosmo-sccp
Gerrit-Branch: master
Gerrit-Change-Id: I4854219f8898266ae47b9117ef79dbad30a5b0fd
Gerrit-Change-Number: 35253
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-MessageType: newchange
Attention is currently required from: laforge.
pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-netif/+/35240?usp=email )
Change subject: stream: Use new multiaddr APIs to log whole set of sock addresses
......................................................................
Patch Set 5:
(1 comment)
Patchset:
PS4:
> I'm wondering if we really want to store/display/... […]
I'm also fine with not merging this patch, since I agree prefixes in log lines end up being quite long if the user has several addresses configured in a socket (or uses 0.0.0.0|::).
--
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: 5
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-CC: laforge <laforge(a)osmocom.org>
Gerrit-Attention: laforge <laforge(a)osmocom.org>
Gerrit-Comment-Date: Thu, 07 Dec 2023 13:32:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: comment
pespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmo-netif/+/35241?usp=email )
Change subject: stream: Add missing osmo_stream_srv_link_get_fd() API
......................................................................
stream: Add missing osmo_stream_srv_link_get_fd() API
osmo_stream_srv and osmo_stream_cli already had that API introduced in
order to use it instead of *_get_ofd(), since the later will eventually
be deprecated due to incoming osmo_io.
Change-Id: I1bd3f790d93af74c150938a59108b882ad2820f3
---
M include/osmocom/netif/stream.h
M src/stream_srv.c
2 files changed, 22 insertions(+), 0 deletions(-)
Approvals:
osmith: Looks good to me, approved
Jenkins Builder: Verified
diff --git a/include/osmocom/netif/stream.h b/include/osmocom/netif/stream.h
index 218b635..681f9ec 100644
--- a/include/osmocom/netif/stream.h
+++ b/include/osmocom/netif/stream.h
@@ -38,6 +38,7 @@
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);
struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link);
+int osmo_stream_srv_link_get_fd(const 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);
void osmo_stream_srv_link_close(struct osmo_stream_srv_link *link);
diff --git a/src/stream_srv.c b/src/stream_srv.c
index 42b04ad..f5a0b42 100644
--- a/src/stream_srv.c
+++ b/src/stream_srv.c
@@ -366,6 +366,14 @@
return &link->ofd;
}
+/*! \brief Get File Descriptor of the stream server link
+ * \param[in] conn Stream Server Link
+ * \returns file descriptor or negative on error */
+int osmo_stream_srv_link_get_fd(const struct osmo_stream_srv_link *link)
+{
+ return link->ofd.fd;
+}
+
/*! \brief Set the accept() call-back of the stream server link
* \param[in] link Stream Server Link
* \param[in] accept_cb Call-back function executed upon accept() */
--
To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/35241?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: I1bd3f790d93af74c150938a59108b882ad2820f3
Gerrit-Change-Number: 35241
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: osmith <osmith(a)sysmocom.de>
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-MessageType: merged
Attention is currently required from: pespin.
Hello Jenkins Builder,
I'd like you to reexamine a change. Please visit
https://gerrit.osmocom.org/c/libosmo-netif/+/35240?usp=email
to look at the new patch set (#5).
The following approvals got outdated and were removed:
Verified-1 by Jenkins Builder
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 src/stream_cli.c
M src/stream_srv.c
3 files changed, 61 insertions(+), 22 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmo-netif refs/changes/40/35240/5
--
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: 5
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-CC: laforge <laforge(a)osmocom.org>
Gerrit-Attention: pespin <pespin(a)sysmocom.de>
Gerrit-MessageType: newpatchset
Attention is currently required from: pespin.
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmo-netif/+/35240?usp=email )
Change subject: stream: Use new multiaddr APIs to log whole set of sock addresses
......................................................................
Patch Set 4:
(1 comment)
Patchset:
PS4:
I'm wondering if we really want to store/display/... those absurdly long strings consisting of all the socket addresses here.
It's fine if there is a single VTY command that shows the user all the gory detail. But having potentially hundreds of characters as prefix in a log message? I don't really think this is where we want to move.
--
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: 4
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-CC: laforge <laforge(a)osmocom.org>
Gerrit-Attention: pespin <pespin(a)sysmocom.de>
Gerrit-Comment-Date: Thu, 07 Dec 2023 13:25:18 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )
Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
......................................................................
socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
This API will be used internally by osmo_sock_multiaddr_get_name_buf()
in a follow-up patch.
This API can also be used directly by user who wish to obtain a list of
local/remote IP addresses and port from an SCTP socket.
Related: SYS#6636
Related: OS#5581
Change-Id: I19d560ab4aadec18a4c0f94115675ec1d7ab14d7
---
M TODO-RELEASE
M include/osmocom/core/socket.h
M src/core/libosmocore.map
M src/core/socket.c
4 files changed, 109 insertions(+), 0 deletions(-)
Approvals:
osmith: Looks good to me, but someone else must approve
Jenkins Builder: Verified
pespin: Looks good to me, approved
laforge: Looks good to me, but someone else must approve
diff --git a/TODO-RELEASE b/TODO-RELEASE
index fa7bc57..e365746 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -8,6 +8,7 @@
# If any interfaces have been removed or changed since the last public release: c:r:0.
#library what description / commit summary line
core ADD osmo_sock_multiaddr_{add,del}_local_addr()
+core ADD osmo_sock_multiaddr_get_ip_and_port()
core ADD gsmtap_inst_fd2() core, DEPRECATE gsmtap_inst_fd()
isdn ABI change add states and flags for external T200 handling
gsm ABI change add T200 timer states to lapdm_datalink
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 03f1d39..b8b04f8 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -191,6 +191,8 @@
int osmo_sock_get_remote_ip(int fd, char *host, size_t len);
int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len);
+int osmo_sock_multiaddr_get_ip_and_port(int fd, int ip_proto, char *ip, size_t *ip_cnt, size_t ip_len,
+ char *port, size_t port_len, bool local);
int osmo_sock_multiaddr_add_local_addr(int sfd, const char **addrs, size_t addrs_cnt);
int osmo_sock_multiaddr_del_local_addr(int sfd, const char **addrs, size_t addrs_cnt);
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index 30c4927..ffbbd2f 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -436,6 +436,7 @@
osmo_sock_mcast_ttl_set;
osmo_sock_multiaddr_add_local_addr;
osmo_sock_multiaddr_del_local_addr;
+osmo_sock_multiaddr_get_ip_and_port;
osmo_sock_set_dscp;
osmo_sock_set_priority;
osmo_sock_unix_init;
diff --git a/src/core/socket.c b/src/core/socket.c
index 7fc1b72..51703ef 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -1868,6 +1868,95 @@
return 0;
}
+/*! Get multiple IP addresses and/or port number on socket in separate string buffers
+ * \param[in] fd file descriptor of socket.
+ * \param[out] ip_proto IPPROTO of the socket, eg: IPPROTO_SCTP.
+ * \param[out] ip Pointer to memory holding consecutive buffers of size ip_len.
+ * \param[out] ip_cnt length ip array pointer. on return it contains the number of addresses found.
+ * \param[in] ip_len length of each of the string buffer in the the ip array.
+ * \param[out] port number (will be filled in when not NULL).
+ * \param[in] port_len length of the port buffer.
+ * \param[in] local (true) or remote (false) name will get looked at.
+ * \returns 0 on success; negative otherwise.
+ *
+ * Upon return, ip_cnt can be set to a higher value than the one set by the
+ * caller. This can be used by the caller to find out the required array length
+ * and then obtaining by calling the function twice. Only up to ip_cnt addresses
+ * are filed in, as per the value provided by the caller.
+ *
+ * Usage example retrieving all (up to OSMO_SOCK_MAX_ADDRS, 32) bound IP addresses and bound port:
+ * char hostbuf[OSMO_SOCK_MAX_ADDRS][INET6_ADDRSTRLEN];
+ * size_t num_hostbuf = ARRAY_SIZE(hostbuf);
+ * char portbuf[6];
+ * rc = osmo_sock_multiaddr_get_ip_and_port(fd, IPPROTO_SCTP, &hostbuf[0][0], &num_hostbuf,
+ * sizeof(hostbuf[0]), portbuf, sizeof(portbuf), true);
+ * if (rc < 0)
+ * goto error;
+ * if (num_hostbuf > ARRAY_SIZE(hostbuf))
+ * goto not_enough_buffers;
+ */
+int osmo_sock_multiaddr_get_ip_and_port(int fd, int ip_proto, char *ip, size_t *ip_cnt, size_t ip_len,
+ char *port, size_t port_len, bool local)
+{
+ struct sockaddr *addrs = NULL;
+ unsigned int n_addrs, i;
+ void *addr_buf;
+ int rc;
+
+ switch (ip_proto) {
+ case IPPROTO_SCTP:
+ break; /* continue below */
+ default:
+ if (*ip_cnt == 0) {
+ *ip_cnt = 1;
+ return 0;
+ }
+ *ip_cnt = 1;
+ return osmo_sock_get_ip_and_port(fd, ip, ip_len, port, port_len, local);
+ }
+
+ rc = local ? sctp_getladdrs(fd, 0, &addrs) : sctp_getpaddrs(fd, 0, &addrs);
+ if (rc < 0)
+ return rc;
+ if (rc == 0)
+ return -ENOTCONN;
+
+ n_addrs = rc;
+ addr_buf = (void *)addrs;
+ for (i = 0; i < n_addrs; i++) {
+ struct sockaddr *sa_addr = (struct sockaddr *)addr_buf;
+ size_t addrlen;
+
+ if (i >= *ip_cnt)
+ break;
+
+ switch (sa_addr->sa_family) {
+ case AF_INET:
+ addrlen = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ addrlen = sizeof(struct sockaddr_in6);
+ break;
+ default:
+ rc = -EINVAL;
+ goto free_addrs_ret;
+ }
+
+ rc = getnameinfo(sa_addr, addrlen, &ip[i * ip_len], ip_len,
+ port, port_len,
+ NI_NUMERICHOST | NI_NUMERICSERV);
+ if (rc < 0)
+ goto free_addrs_ret;
+ addr_buf += addrlen;
+ }
+
+ *ip_cnt = n_addrs;
+ rc = 0;
+free_addrs_ret:
+ local ? sctp_freeladdrs(addrs) : sctp_freepaddrs(addrs);
+ return rc;
+}
+
/*! Get local IP address on socket
* \param[in] fd file descriptor of socket
* \param[out] ip IP address (will be filled in)
--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I19d560ab4aadec18a4c0f94115675ec1d7ab14d7
Gerrit-Change-Number: 35179
Gerrit-PatchSet: 5
Gerrit-Owner: pespin <pespin(a)sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: osmith <osmith(a)sysmocom.de>
Gerrit-Reviewer: pespin <pespin(a)sysmocom.de>
Gerrit-CC: neels <nhofmeyr(a)sysmocom.de>
Gerrit-MessageType: merged