[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-07 Thread laforge
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   whatdescription / 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
 gsmABI 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;
+   

[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-06 Thread pespin
Attention is currently required from: neels.

pespin has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )

Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
..


Patch Set 4: Code-Review+2


--
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: 4
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: osmith 
Gerrit-Reviewer: pespin 
Gerrit-CC: neels 
Gerrit-Attention: neels 
Gerrit-Comment-Date: Wed, 06 Dec 2023 14:54:06 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-06 Thread osmith
Attention is currently required from: neels, pespin.

osmith has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )

Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
..


Patch Set 4: Code-Review+1


--
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: 4
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: osmith 
Gerrit-CC: neels 
Gerrit-Attention: neels 
Gerrit-Attention: pespin 
Gerrit-Comment-Date: Wed, 06 Dec 2023 14:24:54 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-06 Thread pespin
Attention is currently required from: neels, osmith.

pespin has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )

Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
..


Patch Set 4:

(7 comments)

File src/core/socket.c:

https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/e21aa3ab_8a5bd2ac
PS1, Line 1810: /*! Get the IP and/or port number on socket in separate string 
buffers.
> Ack
Done


https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/9fcdd5c0_afa3cad6
PS1, Line 1819:  *  \returns 0 on success; negative otherwise
> Yes, you pass char ip[CNT][LEN]. You pass it as "&ip[0][0]". Number of CNT is 
> passed in ip_cnt. […]
Done


https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/cc4e5eaf_99dfbe1e
PS1, Line 1843: c
> Ack
Done


https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/25fdbc3d_7128ca45
PS1, Line 1870: local ? sctp_freeladdrs(addrs) : 
sctp_freepaddrs(addrs);
> I'm not sure what's new for you here? the fact that you can use a tri 
> operator with functions return […]
Done


https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/cbbe4676_c98bb647
PS1, Line 1874: *
> Ack
Done


https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/110b505b_eeb8b197
PS1, Line 1878: local ? sctp_freeladdrs(addrs) : 
sctp_freepaddrs(addrs);
> I'll see if it looks better that way.
Done


File src/core/socket.c:

https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/2c5076fe_f0e254df
PS2, Line 1827:  * char hostbuf[32][INET6_ADDRSTRLEN];
> I need to s/32/OSMO_SOCK_MAX_ADDRS/g here.
Done



--
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: 4
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: osmith 
Gerrit-CC: neels 
Gerrit-Attention: osmith 
Gerrit-Attention: neels 
Gerrit-Comment-Date: Wed, 06 Dec 2023 14:19:44 +
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: neels 
Comment-In-Reply-To: pespin 
Gerrit-MessageType: comment


[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-06 Thread laforge
Attention is currently required from: neels, osmith, pespin.

laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )

Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
..


Patch Set 4: Code-Review+1


--
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: 4
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: osmith 
Gerrit-CC: neels 
Gerrit-Attention: osmith 
Gerrit-Attention: neels 
Gerrit-Attention: pespin 
Gerrit-Comment-Date: Wed, 06 Dec 2023 14:12:31 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-06 Thread pespin
Attention is currently required from: laforge, neels, osmith.

pespin has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )

Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
..


Patch Set 4:

(2 comments)

Commit Message:

https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/ffd19adc_084848dd
PS1, Line 8:
> Ack
Done


File src/core/socket.c:

https://gerrit.osmocom.org/c/libosmocore/+/35179/comment/d49980be_fa303ad6
PS1, Line 1881: addr_buf += addrlen;
> It can easily be changed to uint8_t* afaiu, I'll have a look since anyway I 
> ned to fix the s/32/OSMO […]
Done



--
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: 4
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: osmith 
Gerrit-CC: neels 
Gerrit-Attention: osmith 
Gerrit-Attention: neels 
Gerrit-Attention: laforge 
Gerrit-Comment-Date: Wed, 06 Dec 2023 14:08:17 +
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: neels 
Comment-In-Reply-To: laforge 
Comment-In-Reply-To: pespin 
Gerrit-MessageType: comment


[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-12-06 Thread pespin
Attention is currently required from: laforge, neels.

pespin has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/35179?usp=email )

Change subject: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()
..


Patch Set 3:

This change is ready for review.


--
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: 3
Gerrit-Owner: pespin 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-CC: neels 
Gerrit-Attention: neels 
Gerrit-Attention: laforge 
Gerrit-Comment-Date: Wed, 06 Dec 2023 13:30:40 +
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment


[M] Change in libosmocore[master]: socket: Introduce API osmo_sock_multiaddr_get_ip_and_port()

2023-11-30 Thread pespin
pespin has uploaded this change for review. ( 
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()

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, 94 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/79/35179/1

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   whatdescription / 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
 gsmABI change  add T200 timer states to lapdm_datalink
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 03f1d39..9397343 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_len, size_t *ip_cnt,
+   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 3d6aa42..1d00fe8 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 1dc8e46..1d97018 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -1807,6 +1807,85 @@
return 0;
 }

+/*! Get the IP 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[in] ip_len length of each of the string buffer in the the ip array
+ *  \param[in] ip_cnt length ip array pointer. on return it contains the 
number of addresses found.
+ *  \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.
+ */
+int osmo_sock_multiaddr_get_ip_and_port(int fd, int ip_proto, char *ip, size_t 
ip_len, size_t *ip_cnt,
+   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;
+   rc = osmo_sock_get_ip_and_port(fd, ip, ip_len, port, port_len, 
local);
+   return rc;
+   }
+
+   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;
+   def