From 9ef9110c26fd104714eb34eafc8f75480019015d Mon Sep 17 00:00:00 2001 From: Tom Early Date: Fri, 12 Mar 2021 11:54:09 -0700 Subject: [PATCH] include port number for (in)equality operators in CIp --- src/cip.cpp | 26 ++++++++++++++++++++------ src/main.h | 2 +- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/cip.cpp b/src/cip.cpp index 6feafca..48a3b6f 100644 --- a/src/cip.cpp +++ b/src/cip.cpp @@ -109,40 +109,54 @@ void CIp::Initialize(const int family, const uint16_t port, const char *address) } } -bool CIp::operator==(const CIp &rhs) const // doesn't compare ports, only addresses and families +bool CIp::operator==(const CIp &rhs) const // compares ports, addresses and families { + // if anything is not equal, then we are done if (addr.ss_family != rhs.addr.ss_family) return false; if (AF_INET == addr.ss_family) { auto l = (struct sockaddr_in *)&addr; auto r = (struct sockaddr_in *)&rhs.addr; - return (l->sin_addr.s_addr == r->sin_addr.s_addr); + if (l->sin_addr.s_addr == r->sin_addr.s_addr) + return l->sin_port == r->sin_port; + else + return false; } else if (AF_INET6 == addr.ss_family) { auto l = (struct sockaddr_in6 *)&addr; auto r = (struct sockaddr_in6 *)&rhs.addr; - return (0 == memcmp(&(l->sin6_addr), &(r->sin6_addr), sizeof(struct in6_addr))); + if (0 == memcmp(&(l->sin6_addr), &(r->sin6_addr), sizeof(struct in6_addr))) + return l->sin6_port == r->sin6_port; + else + return false; } return false; } -bool CIp::operator!=(const CIp &rhs) const // doesn't compare ports, only addresses and families +bool CIp::operator!=(const CIp &rhs) const // compares ports, addresses and families { + // if anything is not equal, then we are done if (addr.ss_family != rhs.addr.ss_family) return true; if (AF_INET == addr.ss_family) { auto l = (struct sockaddr_in *)&addr; auto r = (struct sockaddr_in *)&rhs.addr; - return (l->sin_addr.s_addr != r->sin_addr.s_addr); + if (l->sin_addr.s_addr != r->sin_addr.s_addr) + return true; + else + return l->sin_port != r->sin_port; } else if (AF_INET6 == addr.ss_family) { auto l = (struct sockaddr_in6 *)&addr; auto r = (struct sockaddr_in6 *)&rhs.addr; - return (0 != memcmp(&(l->sin6_addr), &(r->sin6_addr), sizeof(struct in6_addr))); + if (0 != memcmp(&(l->sin6_addr), &(r->sin6_addr), sizeof(struct in6_addr))) + return true; + else + return l->sin6_port != r->sin6_port; } return true; } diff --git a/src/main.h b/src/main.h index 4cd7fd2..e8ae07c 100644 --- a/src/main.h +++ b/src/main.h @@ -66,7 +66,7 @@ #define VERSION_MAJOR 2 #define VERSION_MINOR 4 -#define VERSION_REVISION 29 +#define VERSION_REVISION 30 // global ------------------------------------------------------