Change in libosmocore[master]: Add osmo_sockaddrin_to_str_and_uint()

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

Max gerrit-no-reply at lists.osmocom.org
Tue Nov 20 14:20:23 UTC 2018


Max has uploaded this change for review. ( https://gerrit.osmocom.org/11842


Change subject: Add osmo_sockaddrin_to_str_and_uint()
......................................................................

Add osmo_sockaddrin_to_str_and_uint()

It's similar to osmo_sockaddr_to_str_and_uint() but does not require odd
typecasting for AF_INET case. Make osmo_sockaddr_to_str_and_uint() into
wrapper around new function and make sure to check for address family
before typecasting. Also use proper return type.

Change-Id: Ie384483124d407a960ab6732e6a7fd90554389d2
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 24 insertions(+), 6 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/42/11842/1

diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 28f89a5..0930de0 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -11,7 +11,8 @@
 #include <stdbool.h>
 #include <stddef.h>
 
-struct sockaddr;
+#include <arpa/inet.h>
+
 struct osmo_fd;
 
 /* flags for osmo_sock_init. */
@@ -49,6 +50,8 @@
 
 unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
 					   const struct sockaddr *sa);
+size_t osmo_sockaddrin_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
+				       const struct sockaddr_in *sin);
 
 int osmo_sock_unix_init(uint16_t type, uint8_t proto,
 			const char *socket_path, unsigned int flags);
diff --git a/src/socket.c b/src/socket.c
index 0e17a28..698b126 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -569,6 +569,25 @@
 	return 0;
 }
 
+/*! Convert sockaddr_in to IP address as char string and port as uint16_t.
+ *  \param[out] addr  String buffer to write IP address to, or NULL.
+ *  \param[out] addr_len  Size of \a addr.
+ *  \param[out] port  Pointer to uint16_t to write the port number to, or NULL.
+ *  \param[in] sin  Sockaddr to convert.
+ *  \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a addr is NULL.
+ */
+size_t osmo_sockaddrin_to_str_and_uint(char *addr, unsigned int addr_len, uint16_t *port,
+				       const struct sockaddr_in *sin)
+{
+	if (port)
+		*port = ntohs(sin->sin_port);
+
+	if (addr)
+		return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len);
+
+	return 0;
+}
+
 /*! Convert sockaddr to IP address as char string and port as uint16_t.
  *  \param[out] addr  String buffer to write IP address to, or NULL.
  *  \param[out] addr_len  Size of \a addr.
@@ -581,11 +600,7 @@
 {
 	const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
 
-	if (port)
-		*port = ntohs(sin->sin_port);
-	if (addr)
-		return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len);
-	return 0;
+	return osmo_sockaddrin_to_str_and_uint(addr, addr_len, port, sin);
 }
 
 /*! Initialize a unix domain socket (including bind/connect)

-- 
To view, visit https://gerrit.osmocom.org/11842
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: Ie384483124d407a960ab6732e6a7fd90554389d2
Gerrit-Change-Number: 11842
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181120/f5364c66/attachment.htm>


More information about the gerrit-log mailing list