Electroneum
tor_address.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018, 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 "tor_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"
41 #include "string_tools.h"
42 
43 namespace net
44 {
45  namespace
46  {
47  constexpr const char tld[] = u8".onion";
48  constexpr const char unknown_host[] = "<unknown tor host>";
49 
50  constexpr const unsigned v2_length = 16;
51  constexpr const unsigned v3_length = 56;
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 
64  if (host.size() != v2_length && host.size() != v3_length)
66  if (host.find_first_not_of(base32_alphabet) != boost::string_ref::npos)
68 
69  return success();
70  }
71 
72  struct tor_serialized
73  {
74  std::string host;
76 
78  KV_SERIALIZE(host)
81  };
82  }
83 
84  tor_address::tor_address(const boost::string_ref host, const std::uint16_t port) noexcept
85  : port_(port)
86  {
87  // this is a private constructor, throw if moved to public
88  assert(host.size() < sizeof(host_));
89 
90  const std::size_t length = std::min(sizeof(host_) - 1, host.size());
91  std::memcpy(host_, host.data(), length);
92  std::memset(host_ + length, 0, sizeof(host_) - length);
93  }
94 
95  const char* tor_address::unknown_str() noexcept
96  {
97  return unknown_host;
98  }
99 
101  : port_(0)
102  {
103  static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size");
104  std::memcpy(host_, unknown_host, sizeof(unknown_host));
105  std::memset(host_ + sizeof(unknown_host), 0, sizeof(host_) - sizeof(unknown_host));
106  }
107 
108  expect<tor_address> tor_address::make(const boost::string_ref address, const std::uint16_t default_port)
109  {
110  boost::string_ref host = address.substr(0, address.rfind(':'));
111  const boost::string_ref port =
112  address.substr(host.size() + (host.size() == address.size() ? 0 : 1));
113 
114  ELECTRONEUM_CHECK(host_check(host));
115 
116  std::uint16_t porti = default_port;
118  return {net::error::invalid_port};
119 
120  static_assert(v2_length <= v3_length, "bad internal host size");
121  static_assert(v3_length + sizeof(tld) == sizeof(tor_address::host_), "bad internal host size");
122  return tor_address{host, porti};
123  }
124 
126  {
127  tor_serialized in{};
128  if (in._load(src, hparent) && in.host.size() < sizeof(host_) && (in.host == unknown_host || !host_check(in.host).has_error()))
129  {
130  std::memcpy(host_, in.host.data(), in.host.size());
131  std::memset(host_ + in.host.size(), 0, sizeof(host_) - in.host.size());
132  port_ = in.port;
133  return true;
134  }
135  static_assert(sizeof(unknown_host) <= sizeof(host_), "bad buffer size");
136  std::memcpy(host_, unknown_host, sizeof(unknown_host)); // include null terminator
137  port_ = 0;
138  return false;
139  }
140 
142  {
143  const tor_serialized out{std::string{host_}, port_};
144  return out.store(dest, hparent);
145  }
146 
148  : port_(rhs.port_)
149  {
150  std::memcpy(host_, rhs.host_, sizeof(host_));
151  }
152 
154  {
155  if (this != std::addressof(rhs))
156  {
157  port_ = rhs.port_;
158  std::memcpy(host_, rhs.host_, sizeof(host_));
159  }
160  return *this;
161  }
162 
163  bool tor_address::is_unknown() const noexcept
164  {
165  static_assert(1 <= sizeof(host_), "host size too small");
166  return host_[0] == '<'; // character is not allowed otherwise
167  }
168 
169  bool tor_address::equal(const tor_address& rhs) const noexcept
170  {
171  return port_ == rhs.port_ && is_same_host(rhs);
172  }
173 
174  bool tor_address::less(const tor_address& rhs) const noexcept
175  {
176  return std::strcmp(host_str(), rhs.host_str()) < 0 || port() < rhs.port();
177  }
178 
179  bool tor_address::is_same_host(const tor_address& rhs) const noexcept
180  {
182  return std::strcmp(host_str(), rhs.host_str()) == 0;
183  }
184 
186  {
187  const std::size_t host_length = std::strlen(host_str());
188  const std::size_t port_length =
189  port_ == 0 ? 0 : std::numeric_limits<std::uint16_t>::digits10 + 2;
190 
191  std::string out{};
192  out.reserve(host_length + port_length);
193  out.assign(host_str(), host_length);
194 
195  if (port_ != 0)
196  {
197  out.push_back(':');
198  namespace karma = boost::spirit::karma;
199  karma::generate(std::back_inserter(out), karma::ushort_, port());
200  }
201  return out;
202  }
203 }
::std::string string
Definition: gtest-port.h:1097
CXA_THROW_INFO_T void(* dest)(void *))
Definition: stack_trace.cpp:91
static const char * unknown_str() noexcept
Definition: tor_address.cpp:95
bool is_same_host(const tor_address &rhs) const noexcept
bool is_unknown() const noexcept
bool store(epee::serialization::portable_storage &dest, epee::serialization::section *hparent) const
Store in epee p2p format.
STL namespace.
unsigned short uint16_t
Definition: stdint.h:125
#define KV_SERIALIZE(varialble)
std::string str() const
bool less(const tor_address &rhs) const noexcept
Definition: expect.h:70
tor_address() noexcept
An object with port() == 0 and host_str() == unknown_str().
#define ELECTRONEUM_CHECK(...)
Check expect<void> and return errors in current scope.
Definition: expect.h:47
bool _load(epee::serialization::portable_storage &src, epee::serialization::section *hparent)
Load from epee p2p format, and.
boost::endian::big_uint16_t port
Definition: socks.cpp:60
bool equal(const tor_address &rhs) const noexcept
expect< void > success() noexcept
Definition: expect.h:397
Tor onion address; internal format not condensed/decoded.
Definition: tor_address.h:51
void * memcpy(void *a, const void *b, size_t c)
tor_address & operator=(const tor_address &rhs) noexcept
Invalid base32 or length.
const char * address
Definition: multisig.cpp:37
#define END_KV_SERIALIZE_MAP()
Outside of 0-65535 range.
static expect< tor_address > make(boost::string_ref address, std::uint16_t default_port=0)
PUSH_WARNINGS bool get_xtype_from_string(OUT XType &val, const std::string &str_id)
Definition: string_tools.h:125
Expected a tld.
unsigned char u8
Definition: chacha_private.h:9
#define BEGIN_KV_SERIALIZE_MAP()