Debian Codegraph @

src/net/i2p_address.cpp

raw symbols SCIP
1 // Copyright (c) 2019-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "i2p_address.h"
30
31 #include <algorithm>
32 #include <boost/spirit/include/karma_generate.hpp>
33 #include <boost/spirit/include/karma_uint.hpp>
34 #include <cassert>
35 #include <cstring>
36 #include <limits>
37
38 #include "net/error.h"
39 #include "serialization/keyvalue_serialization.h"
40 #include "storages/portable_storage.h"
41 #include "string_tools_lexical.h"
42
43 namespace net
44 {
45 namespace
46 {
47 // !TODO only b32 addresses right now
48 constexpr const char tld[] = u8".b32.i2p";
49 constexpr const char unknown_host[] = "<unknown i2p host>";
50
51 constexpr const unsigned b32_length = 52;
52
53 constexpr const char base32_alphabet[] =
54 u8"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz234567";
55
56 expect<void> host_check(boost::string_ref host) noexcept
57 {
58 if (!host.ends_with(tld))
59 return {net::error::expected_tld};
60
61 host.remove_suffix(sizeof(tld) - 1);
62
63 if (host.size() != b32_length)
64 return {net::error::invalid_i2p_address};
65 if (host.find_first_not_of(base32_alphabet) != boost::string_ref::npos)
66 return {net::error::invalid_i2p_address};
67
68 return success();
69 }
70
71 struct i2p_serialized
72 {
73 std::string host;
74 std::uint16_t port; //! Leave for compatability with older clients
75
76 BEGIN_KV_SERIALIZE_MAP()
77 KV_SERIALIZE(host)
78 KV_SERIALIZE(port)
79 END_KV_SERIALIZE_MAP()
80 };
81 }
82
83 i2p_address::i2p_address(const boost::string_ref host) noexcept
84 {
85 // this is a private constructor, throw if moved to public
86 assert(host.size() < sizeof(host_));
87
88 const std::size_t length = std::min(sizeof(host_) - 1, host.size());
89 std::memcpy(host_, host.data(), length);
90 std::memset(host_ + length, 0, sizeof(host_) - length);
91 }
92
93 const char* i2p_address::unknown_str() noexcept
94 {
95 return unknown_host;
96 }
97
98 i2p_address::i2p_address() noexcept
99 {
100 static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size");
101 std::memcpy(host_, unknown_host, sizeof(unknown_host));
102 std::memset(host_ + sizeof(unknown_host), 0, sizeof(host_) - sizeof(unknown_host));
103 }
104
105 expect<i2p_address> i2p_address::make(const boost::string_ref address)
106 {
107 boost::string_ref host = address.substr(0, address.rfind(':'));
108 MONERO_CHECK(host_check(host));
109
110 static_assert(b32_length + sizeof(tld) == sizeof(i2p_address::host_), "bad internal host size");
111 return i2p_address{host};
112 }
113
114 bool i2p_address::_load(epee::serialization::portable_storage& src, epee::serialization::section* hparent)
115 {
116 i2p_serialized in{};
117 if (in._load(src, hparent) && in.host.size() < sizeof(host_) && (in.host == unknown_host || !host_check(in.host).has_error()))
118 {
119 std::memcpy(host_, in.host.data(), in.host.size());
120 std::memset(host_ + in.host.size(), 0, sizeof(host_) - in.host.size());
121 return true;
122 }
123 static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size");
124 std::memcpy(host_, unknown_host, sizeof(unknown_host)); // include null terminator
125 return false;
126 }
127
128 bool i2p_address::store(epee::serialization::portable_storage& dest, epee::serialization::section* hparent) const
129 {
130 // Set port to 1 for backwards compatability; zero is invalid port
131 const i2p_serialized out{std::string{host_}, 1};
132 return out.store(dest, hparent);
133 }
134
135 i2p_address::i2p_address(const i2p_address& rhs) noexcept
136 {
137 std::memcpy(host_, rhs.host_, sizeof(host_));
138 }
139
140 i2p_address& i2p_address::operator=(const i2p_address& rhs) noexcept
141 {
142 if (this != std::addressof(rhs))
143 {
144 std::memcpy(host_, rhs.host_, sizeof(host_));
145 }
146 return *this;
147 }
148
149 bool i2p_address::is_unknown() const noexcept
150 {
151 static_assert(1 <= sizeof(host_), "host size too small");
152 return host_[0] == '<'; // character is not allowed otherwise
153 }
154
155 bool i2p_address::equal(const i2p_address& rhs) const noexcept
156 {
157 return is_same_host(rhs);
158 }
159
160 bool i2p_address::less(const i2p_address& rhs) const noexcept
161 {
162 return std::strcmp(host_str(), rhs.host_str()) < 0;
163 }
164
165 bool i2p_address::is_same_host(const i2p_address& rhs) const noexcept
166 {
167 return std::strcmp(host_str(), rhs.host_str()) == 0;
168 }
169
170 std::string i2p_address::str() const
171 {
172 return host_str();
173 }
174 }