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

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
Tue Jul 28 10:45:08 UTC 2020


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


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

socket.c: add osmo_sockaddr_cmp()

Compare two osmo_sockaddr.

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



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/15/19415/1

diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 6bfcc4c..b162066 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_cmp(struct osmo_sockaddr *a, struct osmo_sockaddr *b);
+
 /*! @} */
diff --git a/src/socket.c b/src/socket.c
index 80d0996..70f96c4 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1529,6 +1529,37 @@
 	return 0;
 }
 
+/*! Compare two osmo_sockaddr. Return 0 if they are same
+ * \brief osmo_sockaddr_cmp
+ * \param[in] a
+ * \param[in] b
+ * \return 0 if a and b are equal.
+ */
+int osmo_sockaddr_cmp(struct osmo_sockaddr *a, struct osmo_sockaddr *b)
+{
+	if (!a && !b)
+		return 0;
+	if (!a)
+		return 1;
+	if (!b)
+		return 1;
+	if (a->u.sa.sa_family != b->u.sa.sa_family)
+		return 1;
+
+	switch (a->u.sa.sa_family) {
+	case AF_INET:
+		/* AF_INET has padding, ignore padding */
+		return !(a->u.sin.sin_addr.s_addr == b->u.sin.sin_addr.s_addr &&
+			 a->u.sin.sin_port == b->u.sin.sin_port);
+	case AF_INET6:
+		/* AF_INET6 usally not contain any padding */
+		return memcmp(&a->u.sin6, &b->u.sin6, sizeof(struct sockaddr_in6));
+	}
+
+	/* fallback to memcmp for remaing AF */
+	return memcmp(a, b, sizeof(struct osmo_sockaddr));
+}
+
 #endif /* HAVE_SYS_SOCKET_H */
 
 /*! @} */

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I2d12ebae2710ffd17cf071e6ada0804e73f87dd6
Gerrit-Change-Number: 19415
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/20200728/ac9cb06f/attachment.htm>


More information about the gerrit-log mailing list