Debian Codegraph @

contrib/epee/src/net_utils_base.cpp

raw symbols SCIP
1
2 #include "net/net_utils_base.h"
3
4 #include <boost/uuid/uuid_io.hpp>
5
6 #include "string_tools.h"
7 #include "net/local_ip.h"
8
9 static inline uint32_t make_address_v4_from_v6(const boost::asio::ip::address_v6& a)
10 {
11 const auto &bytes = a.to_bytes();
12 uint32_t v4 = 0;
13 v4 = (v4 << 8) | bytes[12];
14 v4 = (v4 << 8) | bytes[13];
15 v4 = (v4 << 8) | bytes[14];
16 v4 = (v4 << 8) | bytes[15];
17 return htonl(v4);
18 }
19
20 namespace epee { namespace net_utils
21 {
22 bool ipv4_network_address::equal(const ipv4_network_address& other) const noexcept
23 { return is_same_host(other) && port() == other.port(); }
24
25 bool ipv4_network_address::less(const ipv4_network_address& other) const noexcept
26 { return is_same_host(other) ? port() < other.port() : ip() < other.ip(); }
27
28 std::string ipv4_network_address::str() const
29 { return string_tools::get_ip_string_from_int32(ip()) + ":" + std::to_string(port()); }
30
31 std::string ipv4_network_address::host_str() const { return string_tools::get_ip_string_from_int32(ip()); }
32 bool ipv4_network_address::is_loopback() const { return net_utils::is_ip_loopback(ip()); }
33 bool ipv4_network_address::is_local() const { return net_utils::is_ip_local(ip()); }
34
35 bool ipv6_network_address::equal(const ipv6_network_address& other) const noexcept
36 { return is_same_host(other) && port() == other.port(); }
37
38 bool ipv6_network_address::less(const ipv6_network_address& other) const noexcept
39 { return is_same_host(other) ? port() < other.port() : m_address < other.m_address; }
40
41 std::string ipv6_network_address::str() const
42 { return std::string("[") + host_str() + "]:" + std::to_string(port()); }
43
44 std::string ipv6_network_address::host_str() const { return m_address.to_string(); }
45 bool ipv6_network_address::is_loopback() const { return m_address.is_loopback(); }
46 bool ipv6_network_address::is_local() const { return m_address.is_link_local(); }
47
48
49 bool ipv4_network_subnet::equal(const ipv4_network_subnet& other) const noexcept
50 { return is_same_host(other) && m_mask == other.m_mask; }
51
52 bool ipv4_network_subnet::less(const ipv4_network_subnet& other) const noexcept
53 { return subnet() < other.subnet() ? true : (other.subnet() < subnet() ? false : (m_mask < other.m_mask)); }
54
55 std::string ipv4_network_subnet::str() const
56 { return string_tools::get_ip_string_from_int32(subnet()) + "/" + std::to_string(m_mask); }
57
58 std::string ipv4_network_subnet::host_str() const { return string_tools::get_ip_string_from_int32(subnet()) + "/" + std::to_string(m_mask); }
59 bool ipv4_network_subnet::is_loopback() const { return net_utils::is_ip_loopback(subnet()); }
60 bool ipv4_network_subnet::is_local() const { return net_utils::is_ip_local(subnet()); }
61 bool ipv4_network_subnet::matches(const ipv4_network_address &address) const
62 {
63 return (address.ip() & ~(0xffffffffull << m_mask)) == subnet();
64 }
65
66
67 bool network_address::equal(const network_address& other) const
68 {
69 // clang typeid workaround
70 network_address::interface const* const self_ = self.get();
71 network_address::interface const* const other_self = other.self.get();
72 if (self_ == other_self) return true;
73 if (!self_ || !other_self) return false;
74 if (typeid(*self_) != typeid(*other_self)) return false;
75 return self_->equal(*other_self);
76 }
77
78 bool network_address::less(const network_address& other) const
79 {
80 // clang typeid workaround
81 network_address::interface const* const self_ = self.get();
82 network_address::interface const* const other_self = other.self.get();
83 if (self_ == other_self) return false;
84 if (!self_ || !other_self) return self == nullptr;
85 if (typeid(*self_) != typeid(*other_self))
86 return self_->get_type_id() < other_self->get_type_id();
87 return self_->less(*other_self);
88 }
89
90 bool network_address::is_same_host(const network_address& other) const
91 {
92 // clang typeid workaround
93 network_address::interface const* const self_ = self.get();
94 network_address::interface const* const other_self = other.self.get();
95 if (self_ == other_self) return true;
96 if (!self_ || !other_self) return false;
97 if (typeid(*self_) == typeid(*other_self))
98 return self_->is_same_host(*other_self);
99 const auto this_id = get_type_id();
100 if (this_id == ipv4_network_address::get_type_id() && other.get_type_id() == ipv6_network_address::get_type_id())
101 {
102 const boost::asio::ip::address_v6 &actual_ip = other.as<const epee::net_utils::ipv6_network_address>().ip();
103 if (actual_ip.is_v4_mapped())
104 {
105 const uint32_t v4ip = make_address_v4_from_v6(actual_ip);
106 return is_same_host(ipv4_network_address(v4ip, 0));
107 }
108 }
109 else if (this_id == ipv6_network_address::get_type_id() && other.get_type_id() == ipv4_network_address::get_type_id())
110 {
111 const boost::asio::ip::address_v6 &actual_ip = this->as<const epee::net_utils::ipv6_network_address>().ip();
112 if (actual_ip.is_v4_mapped())
113 {
114 const uint32_t v4ip = make_address_v4_from_v6(actual_ip);
115 return other.is_same_host(ipv4_network_address(v4ip, 0));
116 }
117 }
118 return false;
119 }
120
121 std::string print_connection_context(const connection_context_base& ctx)
122 {
123 std::stringstream ss;
124 ss << ctx.m_remote_address.str() << " " << ctx.m_connection_id << (ctx.m_is_income ? " INC":" OUT");
125 return ss.str();
126 }
127
128 std::string print_connection_context_short(const connection_context_base& ctx)
129 {
130 std::stringstream ss;
131 ss << ctx.m_remote_address.str() << (ctx.m_is_income ? " INC":" OUT");
132 return ss.str();
133 }
134
135 const char* zone_to_string(zone value) noexcept
136 {
137 switch (value)
138 {
139 case zone::public_:
140 return "public";
141 case zone::i2p:
142 return "i2p";
143 case zone::tor:
144 return "tor";
145 default:
146 break;
147 }
148 return "invalid";
149 }
150
151 zone zone_from_string(const boost::string_ref value) noexcept
152 {
153 if (value == "public")
154 return zone::public_;
155 if (value == "i2p")
156 return zone::i2p;
157 if (value == "tor")
158 return zone::tor;
159 return zone::invalid;
160 }
161 }}
162