ywkaras commented on code in PR #10944:
URL: https://github.com/apache/trafficserver/pull/10944#discussion_r1430914857


##########
lib/swoc/include/swoc/swoc_ip_util.h:
##########
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright Network Geographics 2014
+/** @file
+   Shared utilities for IP address classes.
+ */
+
+#pragma once
+#include <netinet/in.h>
+
+#include "swoc/swoc_version.h"
+
+// These have to be global namespace, unfortunately.
+inline bool
+operator==(in6_addr const& lhs, in6_addr const& rhs) {
+  return 0 == memcmp(&lhs, &rhs, sizeof(in6_addr));
+}
+
+inline bool
+operator!=(in6_addr const& lhs, in6_addr const& rhs) {
+  return 0 != memcmp(&lhs, &rhs, sizeof(in6_addr));
+}
+
+namespace swoc { inline namespace SWOC_VERSION_NS {
+
+/// Internal IP address utilities.
+namespace ip {
+
+inline bool is_loopback_host_order(in_addr_t addr) {
+  return (addr & 0xFF000000) == 0x7F000000;
+}
+
+inline bool is_link_local_host_order(in_addr_t addr) {
+  return (addr & 0xFFFF0000) == 0xA9FE0000; // 169.254.0.0/16
+}
+
+inline bool is_multicast_host_order(in_addr_t addr) {
+  return IN_MULTICAST(addr);
+}
+
+inline bool is_private_host_order(in_addr_t addr) {
+  return (((addr & 0xFF000000) == 0x0A000000) || // 10.0.0.0/8
+          ((addr & 0xFFC00000) == 0x64400000) || // 100.64.0.0/10
+          ((addr & 0xFFF00000) == 0xAC100000) || // 172.16.0.0/12
+          ((addr & 0xFFFF0000) == 0xC0A80000)    // 192.168.0.0/16
+  );
+}
+
+// There really is no "host order" for IPv6, so only the network order 
utilities are defined.
+// @c IP6Addr uses an idiosyncratic ordering for performance, not really 
useful to expose to
+// clients.
+
+inline bool is_loopback_network_order(in6_addr const& addr) {
+  return addr == in6addr_loopback;
+}
+
+inline bool is_multicast_network_order(in6_addr const& addr) {
+  return addr.s6_addr[0] == 0xFF;
+}
+
+inline bool is_link_local_network_order(in6_addr const& addr) {
+  return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0x80; // 
fe80::/10
+}
+
+inline bool is_private_network_order(in6_addr const& addr) {
+  return (addr.s6_addr[0] & 0xFE) == 0xFC; // fc00::/7
+}
+
+#if BYTE_ORDER == LITTLE_ENDIAN

Review Comment:
   I think it would be more robust to use:
   `#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__`
   
   https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to