Change in libosmocore[master]: socket.c add osmo_sockaddr_valid()

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:36 UTC 2020


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


Change subject: socket.c add osmo_sockaddr_valid()
......................................................................

socket.c add osmo_sockaddr_valid()

osmo_sockaddr_valid() is a validator of osmo_sockaddr.
It ensure the sock_addr has address and port set.

Change-Id: Ib6fb050e1bfe3f3a8d8bbe5e762351ce6b7cc48c
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 26 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/44/19144/1

diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 6bfcc4c..161691c 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -110,4 +110,6 @@
 
 int osmo_sock_local_ip(char *local_ip, const char *remote_ip);
 
+int osmo_sockaddr_valid(struct osmo_sockaddr *addr);
+
 /*! @} */
diff --git a/src/socket.c b/src/socket.c
index c7fd28b..1d5d1a5 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1512,6 +1512,30 @@
 	return 0;
 }
 
+/*! Determine the if address is valid to start a bind or connect.
+ *  \param[in] addr the address to be tested
+ *  \returns 0 if osmo_sockaddr is valid
+ */
+int osmo_sockaddr_valid(struct osmo_sockaddr *addr)
+{
+	struct in6_addr zero = { 0 };
+	if (addr == NULL)
+		return -EINVAL;
+
+	if (addr->u.sas.ss_family == AF_INET) {
+		struct sockaddr_in *sin = &addr->u.sin;
+		return !(sin->sin_addr.s_addr > 0 && sin->sin_port > 0);
+	} else if (addr->u.sas.ss_family == AF_INET6) {
+		struct sockaddr_in6 *sin6 = &addr->u.sin6;
+		if (sin6->sin6_port == 0)
+			return 1;
+		if (memcmp(&sin6->sin6_addr, &zero, sizeof(struct in6_addr)) == 0)
+			return 1;
+	}
+
+	return -ENOTSUP;
+}
+
 #endif /* HAVE_SYS_SOCKET_H */
 
 /*! @} */

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ib6fb050e1bfe3f3a8d8bbe5e762351ce6b7cc48c
Gerrit-Change-Number: 19144
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/72b6020c/attachment.htm>


More information about the gerrit-log mailing list