@@ -63,21 +63,21 @@ namespace util {
6363 * @return true if the addresses are equal, false otherwise.
6464 */
6565 friend bool operator ==(const sock_t & a, const sock_t & b) {
66- if (a.sock .sa_family != b.sock .sa_family ) {
67- return false ;
66+ if ((a.sock .sa_family != AF_INET && a.sock .sa_family != AF_INET6)
67+ || (b.sock .sa_family != AF_INET && b.sock .sa_family != AF_INET6)) {
68+ throw std::system_error (EAFNOSUPPORT, std::system_category (), " Unsupported address family" );
6869 }
6970
70- switch (a.sock .sa_family ) {
71- case AF_INET:
72- return a.ipv4 .sin_port == b.ipv4 .sin_port && a.ipv4 .sin_addr .s_addr == b.ipv4 .sin_addr .s_addr ;
73- case AF_INET6:
74- return a.ipv6 .sin6_port == b.ipv6 .sin6_port
75- && std::memcmp (&a.ipv6 .sin6_addr , &b.ipv6 .sin6_addr , sizeof (in6_addr)) == 0 ;
76- default :
77- throw std::system_error (EAFNOSUPPORT, std::system_category (), " Unsupported address family" );
71+ if (a.sock .sa_family != b.sock .sa_family ) {
72+ return false ;
7873 }
74+ return a.sock .sa_family == AF_INET
75+ ? a.ipv4 .sin_port == b.ipv4 .sin_port && a.ipv4 .sin_addr .s_addr == b.ipv4 .sin_addr .s_addr
76+ : a.ipv6 .sin6_port == b.ipv6 .sin6_port
77+ && std::memcmp (&a.ipv6 .sin6_addr , &b.ipv6 .sin6_addr , sizeof (in6_addr)) == 0 ;
7978 }
8079
80+
8181 /* *
8282 * Inequality comparison operator for sock_t.
8383 *
@@ -99,21 +99,21 @@ namespace util {
9999 * @return true if a is less than b, false otherwise
100100 */
101101 friend bool operator <(const sock_t & a, const sock_t & b) {
102+ if ((a.sock .sa_family != AF_INET && a.sock .sa_family != AF_INET6)
103+ || (b.sock .sa_family != AF_INET && b.sock .sa_family != AF_INET6)) {
104+ throw std::system_error (EAFNOSUPPORT, std::system_category (), " Unsupported address family" );
105+ }
106+
102107 if (a.sock .sa_family != b.sock .sa_family ) {
103108 return a.sock .sa_family < b.sock .sa_family ;
104109 }
105110
106- switch (a.sock .sa_family ) {
107- case AF_INET:
108- return std::forward_as_tuple (a.ipv4 .sin_addr .s_addr , ntohs (a.ipv4 .sin_port ))
109- < std::forward_as_tuple (b.ipv4 .sin_addr .s_addr , ntohs (b.ipv4 .sin_port ));
110- case AF_INET6: {
111- int cmp = std::memcmp (&a.ipv6 .sin6_addr , &b.ipv6 .sin6_addr , sizeof (in6_addr));
112- return cmp < 0 || (cmp == 0 && ntohs (a.ipv6 .sin6_port ) < ntohs (b.ipv6 .sin6_port ));
113- }
114- default :
115- throw std::system_error (EAFNOSUPPORT, std::system_category (), " Unsupported address family" );
116- }
111+ return a.sock .sa_family == AF_INET
112+ ? std::forward_as_tuple (a.ipv4 .sin_addr .s_addr , ntohs (a.ipv4 .sin_port ))
113+ < std::forward_as_tuple (b.ipv4 .sin_addr .s_addr , ntohs (b.ipv4 .sin_port ))
114+ : std::memcmp (&a.ipv6 .sin6_addr , &b.ipv6 .sin6_addr , sizeof (in6_addr)) < 0
115+ || (std::memcmp (&a.ipv6 .sin6_addr , &b.ipv6 .sin6_addr , sizeof (in6_addr)) == 0
116+ && ntohs (a.ipv6 .sin6_port ) < ntohs (b.ipv6 .sin6_port ));
117117 }
118118
119119 /* *
0 commit comments