LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc_ip_util.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Network Geographics 2014
6
7#pragma once
8#include <netinet/in.h>
9
10#include "swoc/swoc_version.h"
11
12// These have to be global namespace, unfortunately.
13inline bool
14operator==(in6_addr const& lhs, in6_addr const& rhs) {
15 return 0 == memcmp(&lhs, &rhs, sizeof(in6_addr));
16}
17
18inline bool
19operator!=(in6_addr const& lhs, in6_addr const& rhs) {
20 return 0 != memcmp(&lhs, &rhs, sizeof(in6_addr));
21}
22
23namespace swoc { inline namespace SWOC_VERSION_NS {
24
26namespace ip {
27
28inline bool is_loopback_host_order(in_addr_t addr) {
29 return (addr & 0xFF000000) == 0x7F000000;
30}
31
32inline bool is_link_local_host_order(in_addr_t addr) {
33 return (addr & 0xFFFF0000) == 0xA9FE0000; // 169.254.0.0/16
34}
35
36inline bool is_multicast_host_order(in_addr_t addr) {
37 return IN_MULTICAST(addr);
38}
39
40inline bool is_private_host_order(in_addr_t addr) {
41 return (((addr & 0xFF000000) == 0x0A000000) || // 10.0.0.0/8
42 ((addr & 0xFFC00000) == 0x64400000) || // 100.64.0.0/10
43 ((addr & 0xFFF00000) == 0xAC100000) || // 172.16.0.0/12
44 ((addr & 0xFFFF0000) == 0xC0A80000) // 192.168.0.0/16
45 );
46}
47
48// There really is no "host order" for IPv6, so only the network order utilities are defined.
49// @c IP6Addr uses an idiosyncratic ordering for performance, not really useful to expose to
50// clients.
51
52inline bool is_loopback_network_order(in6_addr const& addr) {
53 return addr == in6addr_loopback;
54}
55
56inline bool is_multicast_network_order(in6_addr const& addr) {
57 return addr.s6_addr[0] == 0xFF;
58}
59
60inline bool is_link_local_network_order(in6_addr const& addr) {
61 return addr.s6_addr[0] == 0xFE && (addr.s6_addr[1] & 0xC0) == 0x80; // fe80::/10
62}
63
64inline bool is_private_network_order(in6_addr const& addr) {
65 return (addr.s6_addr[0] & 0xFE) == 0xFC; // fc00::/7
66}
67
68#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
69
70inline bool is_loopback_network_order(in_addr_t addr) {
71 return (addr & 0xFF) == 0x7F;
72}
73
74inline bool is_private_network_order(in_addr_t addr) {
75 return (((addr & 0xFF) == 0x0A) || // 10.0.0.0/8
76 ((addr & 0xC0FF) == 0x4064) || // 100.64.0.0/10
77 ((addr & 0xF0FF) == 0x10AC) || // 172.16.0.0/12
78 ((addr & 0xFFFF) == 0xA8C0) // 192.168.0.0/16
79 );
80}
81
82inline bool is_link_local_network_order(in_addr_t addr) {
83 return (addr & 0xFFFF) == 0xFEA9; // 169.254.0.0/16
84}
85
86inline bool is_multicast_network_order(in_addr_t addr) {
87 return (addr & 0xF0) == 0xE0;
88}
89
90#else
91
92inline bool is_loopback_network_order(in_addr_t addr) {
93 return is_loopback_host_order(addr);
94}
95
96inline bool is_link_local_network_order(inaddr_t addr) {
97 return is_link_local_host_order(addr);
98}
99
100inline bool is_private_network_order(in_addr_t addr) {
101 return is_link_local_host_order(addr);
102}
103
104inline bool is_multicast_network_order(in_addr_t addr) {
105 return is_multicast_host_order(addr);
106}
107
108#endif
109
113inline bool is_loopback(sockaddr const * sa) {
114 return ( sa->sa_family == AF_INET && is_loopback_network_order(reinterpret_cast<sockaddr_in const *>(sa)->sin_addr.s_addr)) ||
115 ( sa->sa_family == AF_INET6 && is_loopback_network_order(reinterpret_cast<sockaddr_in6 const *>(sa)->sin6_addr))
116 ;
117}
118
122inline bool is_multicast(sockaddr const * sa) {
123 return ( sa->sa_family == AF_INET && is_multicast_network_order(reinterpret_cast<sockaddr_in const *>(sa)->sin_addr.s_addr)) ||
124 ( sa->sa_family == AF_INET6 && is_multicast_network_order(reinterpret_cast<sockaddr_in6 const *>(sa)->sin6_addr))
125 ;
126}
127
131inline bool is_link_local(sockaddr const* sa) {
132 return ( sa->sa_family == AF_INET && is_link_local_network_order(reinterpret_cast<sockaddr_in const *>(sa)->sin_addr.s_addr)) ||
133 ( sa->sa_family == AF_INET6 && is_link_local_network_order(reinterpret_cast<sockaddr_in6 const *>(sa)->sin6_addr))
134 ;
135}
136
140inline bool is_private(sockaddr const* sa) {
141 return ( sa->sa_family == AF_INET && is_private_network_order(reinterpret_cast<sockaddr_in const *>(sa)->sin_addr.s_addr)) ||
142 ( sa->sa_family == AF_INET6 && is_private_network_order(reinterpret_cast<sockaddr_in6 const *>(sa)->sin6_addr))
143 ;
144}
145
146} // hnamespace ip
147
148}} // namespace swoc::SWOC_VERSION_NS
Internal IP address utilities.
bool is_multicast(sockaddr const *sa)
bool is_private(sockaddr const *sa)
bool is_loopback(sockaddr const *sa)
bool is_link_local(sockaddr const *sa)
For template deduction guides.
Definition ArenaWriter.cc:9
int memcmp(std::string_view const &lhs, std::string_view const &rhs)