Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()

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

osmith gerrit-no-reply at lists.osmocom.org
Thu Oct 25 09:59:17 UTC 2018


osmith has uploaded this change for review. ( https://gerrit.osmocom.org/11455


Change subject: add osmo_sock_get_{local,remote}_ip{,_port}()
......................................................................

add osmo_sock_get_{local,remote}_ip{,_port}()

Return only the IP or port of either the local or remote connection,
not the whole set of IP and port of both the local and remote
connection like osmo_sock_get_name() does it. This is needed for
OS#2841, where we only want to print the remote IP.

Related: OS#2841
Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 35 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/1

diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index f23a243..6f3de0f 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -56,6 +56,11 @@
 			    const char *socket_path, unsigned int flags);
 
 char *osmo_sock_get_name(void *ctx, int fd);
+char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port);
+#define osmo_sock_get_local_ip(ctx, fd) osmo_sock_get_name2(ctx, fd, false, false)
+#define osmo_sock_get_local_ip_port(ctx, fd) osmo_sock_get_name2(ctx, fd, false, true)
+#define osmo_sock_get_remote_ip(ctx, fd) osmo_sock_get_name2(ctx, fd, true, false)
+#define osmo_sock_get_remote_ip_port(ctx, fd) osmo_sock_get_name2(ctx, fd, true, true)
 
 int osmo_sock_mcast_loop_set(int fd, bool enable);
 int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl);
diff --git a/src/socket.c b/src/socket.c
index bb5505f..dedf2b6 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -682,6 +682,36 @@
 	return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags));
 }
 
+/*! Get one IP or port number on socket in dyn-alloc string. This is
+ *  for internal usage. Convenience wrappers: osmo_sock_get_local_ip(),
+ *  osmo_sock_get_local_ip_port(), osmo_sock_get_remote_ip() and
+ *  osmo_sock_get_remote_ip_port().
+ *  \param[in] ctx talloc context from which to allocate string buffer
+ *  \param[in] fd file descriptor of socket
+ *  \param[in] local (true) or remote (false) name will get looked at
+ *  \param[in] port (true) or ip address (false) will be returned
+ *  \returns string with IP or port
+ */
+char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port)
+{
+	struct sockaddr sa;
+	socklen_t len = sizeof(sa);
+	char hostbuf[64], portbuf[16];
+	int rc;
+
+	rc = local ? getsockname(fd, &sa, &len) : getpeername(fd, &sa, &len);
+	if (rc < 0)
+		return NULL;
+
+	rc = getnameinfo(&sa, len, hostbuf, sizeof(hostbuf),
+			 portbuf, sizeof(portbuf),
+			 NI_NUMERICHOST | NI_NUMERICSERV);
+	if (rc < 0)
+		return NULL;
+
+	return talloc_strdup(ctx, port ? portbuf : hostbuf);
+}
+
 /*! Get address/port information on socket in dyn-alloc string
  *  \param[in] ctx talloc context from which to allocate string buffer
  *  \param[in] fd file descriptor of socket

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1
Gerrit-Change-Number: 11455
Gerrit-PatchSet: 1
Gerrit-Owner: osmith <osmith at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181025/ef2cb3eb/attachment.htm>


More information about the gerrit-log mailing list