Change in libosmocore[master]: add osmo_sockaddr_local_ip() to determine the local address for a rem...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

lynxis lazus gerrit-no-reply at lists.osmocom.org
Tue Jul 28 10:45:09 UTC 2020


lynxis lazus has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/19416 )


Change subject: add osmo_sockaddr_local_ip() to determine the local address for a remote.
......................................................................

add osmo_sockaddr_local_ip() to determine the local address for a remote.

Similiar to osmo_sock_local_ip but for osmo_sockaddr.

Change-Id: I9cd2c5ceb28183e2fd2d28f9c9088c3fcac643d2
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 27 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/16/19416/1

diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index b162066..e5383ba 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -110,6 +110,8 @@
 
 int osmo_sock_local_ip(char *local_ip, const char *remote_ip);
 
+int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip,
+			   struct osmo_sockaddr *remote_ip);
 int osmo_sockaddr_cmp(struct osmo_sockaddr *a, struct osmo_sockaddr *b);
 
 /*! @} */
diff --git a/src/socket.c b/src/socket.c
index 70f96c4..bc0f904 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1529,6 +1529,31 @@
 	return 0;
 }
 
+/*! Determine the matching local address for a given remote address.
+ *  \param[out] local_ip caller provided memory for resulting local address
+ *  \param[in] remote_ip remote address
+ *  \returns 0 on success; negative otherwise
+ */
+int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip, struct osmo_sockaddr *remote_ip)
+{
+	int sfd;
+	int rc;
+	socklen_t local_ip_len;
+
+	sfd = osmo_sock_init3(SOCK_DGRAM, IPPROTO_UDP, NULL, remote_ip, OSMO_SOCK_F_CONNECT);
+	if (sfd < 0)
+		return -EINVAL;
+
+	memset(local_ip, 0, sizeof(*local_ip));
+	local_ip_len = sizeof(*local_ip);
+	rc = getsockname(sfd, (struct sockaddr *)local_ip, &local_ip_len);
+	close(sfd);
+	if (rc < 0)
+		return -EINVAL;
+
+	return rc;
+}
+
 /*! Compare two osmo_sockaddr. Return 0 if they are same
  * \brief osmo_sockaddr_cmp
  * \param[in] a

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19416
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I9cd2c5ceb28183e2fd2d28f9c9088c3fcac643d2
Gerrit-Change-Number: 19416
Gerrit-PatchSet: 1
Gerrit-Owner: lynxis lazus <lynxis at fe80.eu>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200728/66a82bb3/attachment.htm>


More information about the gerrit-log mailing list