lynxis lazus has submitted this change. (
https://gerrit.osmocom.org/c/libosmocore/+/19415 )
Change subject: socket: add osmo_sockaddr_cmp()
......................................................................
socket: add osmo_sockaddr_cmp()
Compare two osmo_sockaddr.
Change-Id: I2d12ebae2710ffd17cf071e6ada0804e73f87dd6
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 31 insertions(+), 0 deletions(-)
Approvals:
Jenkins Builder: Verified
laforge: Looks good to me, approved
pespin: Looks good to me, approved
daniel: Looks good to me, but someone else must approve
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 4441449..e417f42 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -113,5 +113,7 @@
int osmo_sock_local_ip(char *local_ip, const char *remote_ip);
+int osmo_sockaddr_cmp(struct osmo_sockaddr *a, struct osmo_sockaddr *b);
+
#endif /* (!EMBEDDED) */
/*! @} */
diff --git a/src/socket.c b/src/socket.c
index 6c06063..803af31 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1662,6 +1662,35 @@
return 0;
}
+/*! Compare two osmo_sockaddr.
+ * \param[in] a
+ * \param[in] b
+ * \return 0 if a and b are equal. Otherwise it follows memcmp()
+ */
+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 OSMO_CMP(a->u.sa.sa_family, b->u.sa.sa_family);
+ }
+
+ switch (a->u.sa.sa_family) {
+ case AF_INET:
+ return memcmp(&a->u.sin, &b->u.sin, sizeof(struct sockaddr_in));
+ case AF_INET6:
+ return memcmp(&a->u.sin6, &b->u.sin6, sizeof(struct
sockaddr_in6));
+ default:
+ /* fallback to memcmp for remaining AF over the full
osmo_sockaddr length */
+ 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: 19
Gerrit-Owner: lynxis lazus <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: lynxis lazus <[email protected]>
Gerrit-Reviewer: neels <[email protected]>
Gerrit-Reviewer: pespin <[email protected]>
Gerrit-MessageType: merged