diff options
author | Martin Schwenke <martin@meltin.net> | 2016-05-23 10:35:10 +1000 |
---|---|---|
committer | Amitay Isaacs <amitay@samba.org> | 2016-12-16 08:42:32 +0100 |
commit | 362f066d617c8a186164db613537867329702ab7 (patch) | |
tree | d4d598e1925d67507e5c052dd8f20a2b1641cae4 /ctdb/protocol | |
parent | 25aad0df06038d0b595f09d947b9977dcc0ec8a8 (diff) | |
download | samba-362f066d617c8a186164db613537867329702ab7.tar.gz |
ctdb-protocol: Add generalised socket address comparison
Add new function ctdb_sock_addr_cmp(), which returns a 3-way result
useful for qsort(3). Reimplent ctdb_sock_addr_same() using this.
In the process, make arguments const so that ctdb_sock_addr_cmp() can
be used with qsort().
BUG: https://bugzilla.samba.org/show_bug.cgi?id=12470
Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
Diffstat (limited to 'ctdb/protocol')
-rw-r--r-- | ctdb/protocol/protocol_api.h | 10 | ||||
-rw-r--r-- | ctdb/protocol/protocol_util.c | 68 |
2 files changed, 55 insertions, 23 deletions
diff --git a/ctdb/protocol/protocol_api.h b/ctdb/protocol/protocol_api.h index e438e1dfc72..e967dea5a21 100644 --- a/ctdb/protocol/protocol_api.h +++ b/ctdb/protocol/protocol_api.h @@ -658,7 +658,13 @@ const char *ctdb_event_to_string(enum ctdb_event event); enum ctdb_event ctdb_event_from_string(const char *event_str); const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx, ctdb_sock_addr *addr); -bool ctdb_sock_addr_same_ip(ctdb_sock_addr *addr1, ctdb_sock_addr *addr2); -bool ctdb_sock_addr_same(ctdb_sock_addr *addr1, ctdb_sock_addr *addr2); +int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2); +int ctdb_sock_addr_cmp(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2); +bool ctdb_sock_addr_same_ip(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2); +bool ctdb_sock_addr_same(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2); #endif /* __CTDB_PROTOCOL_API_H__ */ diff --git a/ctdb/protocol/protocol_util.c b/ctdb/protocol/protocol_util.c index b91c652251f..0e1bf286799 100644 --- a/ctdb/protocol/protocol_util.c +++ b/ctdb/protocol/protocol_util.c @@ -141,55 +141,81 @@ const char *ctdb_sock_addr_to_string(TALLOC_CTX *mem_ctx, ctdb_sock_addr *addr) return cip; } -bool ctdb_sock_addr_same_ip(ctdb_sock_addr *addr1, ctdb_sock_addr *addr2) +int ctdb_sock_addr_cmp_ip(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2) { - if (addr1->sa.sa_family != addr2->sa.sa_family) { - return false; + int ret = 0; + + /* This is somewhat arbitrary. However, when used for sorting + * it just needs to be consistent. + */ + if (addr1->sa.sa_family < addr2->sa.sa_family) { + return -1; + } + if (addr1->sa.sa_family > addr2->sa.sa_family) { + return 1; } switch (addr1->sa.sa_family) { case AF_INET: - if (addr1->ip.sin_addr.s_addr != addr2->ip.sin_addr.s_addr) { - return false; - } + ret = memcmp(&addr1->ip.sin_addr.s_addr, + &addr2->ip.sin_addr.s_addr, 4); break; case AF_INET6: - if (memcmp(addr1->ip6.sin6_addr.s6_addr, - addr2->ip6.sin6_addr.s6_addr, 16) != 0) { - return false; - } + ret = memcmp(addr1->ip6.sin6_addr.s6_addr, + addr2->ip6.sin6_addr.s6_addr, 16); break; default: - return false; + ret = -1; } - return true; + return ret; } -bool ctdb_sock_addr_same(ctdb_sock_addr *addr1, ctdb_sock_addr *addr2) +int ctdb_sock_addr_cmp(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2) { - if (! ctdb_sock_addr_same_ip(addr1, addr2)) { - return false; + int ret = 0; + + ret = ctdb_sock_addr_cmp_ip(addr1, addr2); + if (ret != 0) { + return ret; } switch (addr1->sa.sa_family) { case AF_INET: - if (addr1->ip.sin_port != addr2->ip.sin_port) { - return false; + if (addr1->ip.sin_port < addr2->ip.sin_port) { + ret = -1; + } else if (addr1->ip.sin_port > addr2->ip.sin_port) { + ret = 1; } break; case AF_INET6: - if (addr1->ip6.sin6_port != addr2->ip6.sin6_port) { - return false; + if (addr1->ip6.sin6_port < addr2->ip6.sin6_port) { + ret = -1; + } else if (addr1->ip6.sin6_port > addr2->ip6.sin6_port) { + ret = 1; } break; default: - return false; + ret = -1; } - return true; + return ret; +} + +bool ctdb_sock_addr_same_ip(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2) +{ + return (ctdb_sock_addr_cmp_ip(addr1, addr2) == 0); +} + +bool ctdb_sock_addr_same(const ctdb_sock_addr *addr1, + const ctdb_sock_addr *addr2) +{ + return (ctdb_sock_addr_cmp(addr1, addr2) == 0); } |