[PATCH] libosmocore[master]: socket: Introduce function to obtain socket name

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Jan 27 09:30:29 UTC 2017


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

socket: Introduce function to obtain socket name

Using this function, one can obtain a human-readable string identifying
the host and port names of the socket.

Change-Id: Ib5de5c7b9effe1b0a363e4473a7be7fa38ca6ef3
---
M TODO-RELEASE
M include/osmocom/core/socket.h
M src/socket.c
3 files changed, 46 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/97/1697/1

diff --git a/TODO-RELEASE b/TODO-RELEASE
index fb0bfea..5c6bfa3 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -7,3 +7,4 @@
 # If any interfaces have been added since the last public release: c:r:a + 1.
 # If any interfaces have been removed or changed since the last public release: c:r:0.
 #library	what			description / commit summary line
+libosmocore	new function		osmo_sock_get_name()
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 6ef0912..4f00e30 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -38,4 +38,6 @@
 int osmo_sock_unix_init_ofd(struct osmo_fd *ofd, uint16_t type, uint8_t proto,
 			    const char *socket_path, unsigned int flags);
 
+char *osmo_sock_get_name(void *ctx, int fd);
+
 /*! @} */
diff --git a/src/socket.c b/src/socket.c
index cdafadd..3c5548f 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1,5 +1,5 @@
 /*
- * (C) 2011 by Harald Welte <laforge at gnumonks.org>
+ * (C) 2011-2017 by Harald Welte <laforge at gnumonks.org>
  *
  * All Rights Reserved
  *
@@ -34,6 +34,7 @@
 #include <osmocom/core/logging.h>
 #include <osmocom/core/select.h>
 #include <osmocom/core/socket.h>
+#include <osmocom/core/talloc.h>
 
 #include <sys/ioctl.h>
 #include <sys/socket.h>
@@ -393,6 +394,47 @@
 	return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags));
 }
 
+/*! \brief Get address/port information on soocket in dyn-alloc string
+ *  \param[in] ctx talloc context from which to allocate string buffer
+ *  \param[in] fd file descriptor of socket
+ *  \returns string identifying the connection of this socket
+ */
+char *osmo_sock_get_name(void *ctx, int fd)
+{
+	struct sockaddr sa_l, sa_r;
+	socklen_t sa_len_l = sizeof(sa_l);
+	socklen_t sa_len_r = sizeof(sa_r);
+	char hostbuf_l[64], hostbuf_r[64];
+	char portbuf_l[16], portbuf_r[16];
+	int rc;
+
+	rc = getsockname(fd, &sa_l, &sa_len_l);
+	if (rc < 0)
+		return NULL;
+
+	rc = getnameinfo(&sa_l, sa_len_l, hostbuf_l, sizeof(hostbuf_l),
+			 portbuf_l, sizeof(portbuf_l),
+			 NI_NUMERICHOST | NI_NUMERICSERV);
+	if (rc < 0)
+		return NULL;
+
+	rc = getpeername(fd, &sa_r, &sa_len_r);
+	if (rc < 0)
+		goto local_only;
+
+	rc = getnameinfo(&sa_r, sa_len_r, hostbuf_r, sizeof(hostbuf_r),
+			 portbuf_r, sizeof(portbuf_r),
+			 NI_NUMERICHOST | NI_NUMERICSERV);
+	if (rc < 0)
+		goto local_only;
+
+	return talloc_asprintf(ctx, "(%s:%s<->%s:%s)", hostbuf_r, portbuf_r,
+				hostbuf_l, portbuf_l);
+
+local_only:
+	return talloc_asprintf(ctx, "(NULL<->%s:%s)", hostbuf_l, portbuf_l);
+}
+
 #endif /* HAVE_SYS_SOCKET_H */
 
 /*! @} */

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib5de5c7b9effe1b0a363e4473a7be7fa38ca6ef3
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>


More information about the gerrit-log mailing list