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
Mon Sep 7 14:32:16 UTC 2020


lynxis lazus has submitted this change. ( 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, 25 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  pespin: Looks good to me, approved



diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index e417f42..47a372c 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -113,6 +113,8 @@
 
 int osmo_sock_local_ip(char *local_ip, const char *remote_ip);
 
+int osmo_sockaddr_local_ip(struct osmo_sockaddr *local_ip,
+			   const struct osmo_sockaddr *remote_ip);
 int osmo_sockaddr_cmp(struct osmo_sockaddr *a, struct osmo_sockaddr *b);
 
 #endif /* (!EMBEDDED) */
diff --git a/src/socket.c b/src/socket.c
index 803af31..f078242 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1662,6 +1662,29 @@
 	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, const struct osmo_sockaddr *remote_ip)
+{
+	int sfd;
+	int rc;
+	socklen_t local_ip_len;
+
+	sfd = osmo_sock_init_osa(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);
+
+	return rc;
+}
+
 /*! Compare two osmo_sockaddr.
  * \param[in] a
  * \param[in] b

-- 
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: 20
Gerrit-Owner: lynxis lazus <lynxis at fe80.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <dwillmann at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: lynxis lazus <lynxis at fe80.eu>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200907/e0a03c67/attachment.htm>


More information about the gerrit-log mailing list