aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Couzens <lynxis@fe80.eu>2020-07-27 22:33:01 +0200
committerAlexander Couzens <lynxis@fe80.eu>2020-09-07 16:22:21 +0200
commit0f36421ba8dc117fdc55129f0dff54895a3d55e1 (patch)
tree1db074676059465921110317dabd01f590792b6b
parent3b688835b05afa3339a4c0d2e5e2ead9e34595b2 (diff)
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
-rw-r--r--include/osmocom/core/socket.h2
-rw-r--r--src/socket.c23
2 files changed, 25 insertions, 0 deletions
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index e417f429..47a372c7 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -113,6 +113,8 @@ int osmo_sock_mcast_subscribe(int fd, const char *grp_addr);
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 803af313..f078242c 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1662,6 +1662,29 @@ int osmo_sock_local_ip(char *local_ip, const char *remote_ip)
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