[PATCH] libosmocore[master]: socket: add function osmo_sock_local_ip() to query local ip

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/.

dexter gerrit-no-reply at lists.osmocom.org
Fri Oct 20 17:45:21 UTC 2017


Review at  https://gerrit.osmocom.org/4372

socket: add function osmo_sock_local_ip() to query local ip

In some cases it is required to know the ip-address of the interface
through that a given remote IP-Address can be reached.

Add function osmo_sock_local_ip() to determine the local ip-address
for a given remote ip-address

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


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/72/4372/1

diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index ebfcab3..47d9f30 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -57,4 +57,6 @@
 int osmo_sock_mcast_all_set(int fd, bool enable);
 int osmo_sock_mcast_subscribe(int fd, const char *grp_addr);
 
+int osmo_sock_local_ip(char *local_ip, const char *remote_ip);
+
 /*! @} */
diff --git a/src/socket.c b/src/socket.c
index 457c991..9e3d842 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -786,4 +786,52 @@
 
 #endif /* HAVE_SYS_SOCKET_H */
 
+/*! Determine the matching local IP-address for a given remote IP-Address.
+ *  \param[out] local_ip caller provided memory for resulting local IP-address
+ *  \param[in] remote_ip remote IP-address
+ *  \param[in] fd file descriptor of related scoket */
+int osmo_sock_local_ip(char *local_ip, const char *remote_ip)
+{
+	int sfd;
+	int rc;
+	struct addrinfo addrinfo_hint;
+	struct addrinfo *addrinfo = NULL;
+	struct sockaddr_in local_addr;
+	socklen_t local_addr_len;
+	uint16_t family;
+
+	/* Find out the address family (AF_INET or AF_INET6?) */
+	memset(&addrinfo_hint, '\0', sizeof(addrinfo_hint));
+	addrinfo_hint.ai_family = PF_UNSPEC;
+	addrinfo_hint.ai_flags = AI_NUMERICHOST;
+	rc = getaddrinfo(remote_ip, NULL, &addrinfo_hint, &addrinfo);
+	if (rc)
+		return -EINVAL;
+	family = addrinfo->ai_family;
+	freeaddrinfo(addrinfo);
+
+	/* Connect a dummy socket to trick the kernel into determining the
+	 * ip-address of the interface that would be used if we would send
+	 * out an actual packet */
+	sfd = osmo_sock_init2(family, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, remote_ip, 0, OSMO_SOCK_F_CONNECT);
+	if (sfd < 0)
+		return -EINVAL;
+
+	/* Request the IP address of the interface that the kernel has
+	 * actually choosen. */
+	memset(&local_addr, 0, sizeof(local_addr));
+	local_addr_len = sizeof(local_addr);
+	rc = getsockname(sfd, (struct sockaddr *)&local_addr, &local_addr_len);
+	if (rc < 0)
+		return -EINVAL;
+	if (local_addr.sin_family == AF_INET)
+		strncpy(local_ip, inet_ntoa(local_addr.sin_addr), INET_ADDRSTRLEN);
+	else if (local_addr.sin_family == AF_INET6)
+		strncpy(local_ip, inet_ntoa(local_addr.sin_addr), INET6_ADDRSTRLEN);
+	else
+		return -EINVAL;
+
+	return 0;
+}
+
 /*! @} */

-- 
To view, visit https://gerrit.osmocom.org/4372
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2988cc52b196fc8476703d1287e24cb4a48491c2
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: dexter <pmaier at sysmocom.de>



More information about the gerrit-log mailing list