Change in libosmocore[master]: sockaddr_str: add osmo_sockaddr_str_from_str2() which doesn't set the...

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

lynxis lazus gerrit-no-reply at lists.osmocom.org
Sun Jul 5 09:27:35 UTC 2020


lynxis lazus has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/19142 )


Change subject: sockaddr_str: add osmo_sockaddr_str_from_str2() which doesn't set the port
......................................................................

sockaddr_str: add osmo_sockaddr_str_from_str2() which doesn't set the port

In case the port isn't known at the time osmo_sockaddr_str_from_str2()
parse only the ip and don't touch the port.
This is the case when a user has different vty commands for ip and port.

Change-Id: Ifd4e282586b8bd40b912a9f1c25f9e8208420106
---
M include/osmocom/core/sockaddr_str.h
M src/sockaddr_str.c
2 files changed, 34 insertions(+), 14 deletions(-)



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

diff --git a/include/osmocom/core/sockaddr_str.h b/include/osmocom/core/sockaddr_str.h
index e42216a..d96b743 100644
--- a/include/osmocom/core/sockaddr_str.h
+++ b/include/osmocom/core/sockaddr_str.h
@@ -74,6 +74,7 @@
 int osmo_sockaddr_str_cmp(const struct osmo_sockaddr_str *a, const struct osmo_sockaddr_str *b);
 
 int osmo_sockaddr_str_from_str(struct osmo_sockaddr_str *sockaddr_str, const char *ip, uint16_t port);
+int osmo_sockaddr_str_from_str2(struct osmo_sockaddr_str *sockaddr_str, const char *ip);
 
 int osmo_sockaddr_str_from_in_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in_addr *addr, uint16_t port);
 int osmo_sockaddr_str_from_in6_addr(struct osmo_sockaddr_str *sockaddr_str, const struct in6_addr *addr, uint16_t port);
diff --git a/src/sockaddr_str.c b/src/sockaddr_str.c
index c38a05c..f5508a0 100644
--- a/src/sockaddr_str.c
+++ b/src/sockaddr_str.c
@@ -188,6 +188,34 @@
 	return AF_UNSPEC;
 }
 
+/*! Safely copy the given ip string to sockaddr_str, classify to AF_INET or AF_INET6.
+ * Data will be written to sockaddr_str even if an error is returned.
+ * \param[out] sockaddr_str  The instance to copy to.
+ * \param[in] ip  Valid IP address string.
+ * \return 0 on success, negative if copying the address string failed (e.g. too long), if the address family could
+ *         not be detected (i.e. if osmo_ip_str_type() returned AF_UNSPEC), or if sockaddr_str is NULL.
+ */
+int osmo_sockaddr_str_from_str2(struct osmo_sockaddr_str *sockaddr_str, const char *ip)
+{
+	int rc;
+	if (!sockaddr_str)
+		return -ENOSPC;
+	if (!ip)
+		ip = "";
+	sockaddr_str->af = osmo_ip_str_type(ip);
+	/* to be compatible with previous behaviour, zero the full IP field.
+	 * Allow the usage of memcmp(&sockaddr_str, ...) */
+	memset(sockaddr_str->ip, 0x0, sizeof(sockaddr_str->ip));
+	rc = osmo_strlcpy(sockaddr_str->ip, ip, sizeof(sockaddr_str->ip));
+	if (rc <= 0)
+		return -EIO;
+	if (rc >= sizeof(sockaddr_str->ip))
+		return -ENOSPC;
+	if (sockaddr_str->af == AF_UNSPEC)
+		return -EINVAL;
+	return 0;
+}
+
 /*! Safely copy the given ip string to sockaddr_str, classify to AF_INET or AF_INET6, and set the port.
  * Data will be written to sockaddr_str even if an error is returned.
  * \param[out] sockaddr_str  The instance to copy to.
@@ -201,20 +229,11 @@
 	int rc;
 	if (!sockaddr_str)
 		return -ENOSPC;
-	if (!ip)
-		ip = "";
-	*sockaddr_str = (struct osmo_sockaddr_str){
-		.af = osmo_ip_str_type(ip),
-		.port = port,
-	};
-	rc = osmo_strlcpy(sockaddr_str->ip, ip, sizeof(sockaddr_str->ip));
-	if (rc <= 0)
-		return -EIO;
-	if (rc >= sizeof(sockaddr_str->ip))
-		return -ENOSPC;
-	if (sockaddr_str->af == AF_UNSPEC)
-		return -EINVAL;
-	return 0;
+
+	rc = osmo_sockaddr_str_from_str2(sockaddr_str, ip);
+	sockaddr_str->port = port;
+
+	return rc;
 }
 
 /*! Convert IPv4 address to osmo_sockaddr_str, and set port.

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ifd4e282586b8bd40b912a9f1c25f9e8208420106
Gerrit-Change-Number: 19142
Gerrit-PatchSet: 1
Gerrit-Owner: lynxis lazus <lynxis at fe80.eu>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200705/dec5ae61/attachment.htm>


More information about the gerrit-log mailing list