Skip to content

Commit f14b2b7

Browse files
committed
Migrate from legacy Beast to boost::asio types
1 parent c5d87f4 commit f14b2b7

36 files changed

+323
-678
lines changed

Builds/CMake/RippledCore.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ target_sources (xrpl_core PRIVATE
4343
src/ripple/beast/insight/impl/Metric.cpp
4444
src/ripple/beast/insight/impl/NullCollector.cpp
4545
src/ripple/beast/insight/impl/StatsDCollector.cpp
46-
src/ripple/beast/net/impl/IPAddressConversion.cpp
47-
src/ripple/beast/net/impl/IPAddressV4.cpp
48-
src/ripple/beast/net/impl/IPAddressV6.cpp
4946
src/ripple/beast/net/impl/IPEndpoint.cpp
5047
src/ripple/beast/utility/src/beast_Journal.cpp
5148
src/ripple/beast/utility/src/beast_PropertyStream.cpp

src/ripple/basics/impl/ResolverAsio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class ResolverAsioImpl : public ResolverAsio,
265265
while (iter != boost::asio::ip::tcp::resolver::iterator())
266266
{
267267
addresses.push_back(
268-
beast::IPAddressConversion::from_asio(*iter));
268+
beast::IP::from_asio(*iter));
269269
++iter;
270270
}
271271
}

src/ripple/beast/net/IPAddress.h

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,57 +27,22 @@
2727
#include <boost/asio/ip/address.hpp>
2828
#include <boost/functional/hash.hpp>
2929
#include <cassert>
30-
#include <cstdint>
31-
#include <ios>
32-
#include <sstream>
33-
#include <string>
34-
#include <typeinfo>
3530

3631
//------------------------------------------------------------------------------
3732

3833
namespace beast {
3934
namespace IP {
4035

41-
using Address = boost::asio::ip::address;
42-
43-
/** Returns the address represented as a string. */
44-
inline std::string
45-
to_string(Address const& addr)
46-
{
47-
return addr.to_string();
48-
}
49-
50-
/** Returns `true` if this is a loopback address. */
51-
inline bool
52-
is_loopback(Address const& addr)
53-
{
54-
return addr.is_loopback();
55-
}
56-
57-
/** Returns `true` if the address is unspecified. */
58-
inline bool
59-
is_unspecified(Address const& addr)
60-
{
61-
return addr.is_unspecified();
62-
}
63-
64-
/** Returns `true` if the address is a multicast address. */
65-
inline bool
66-
is_multicast(Address const& addr)
67-
{
68-
return addr.is_multicast();
69-
}
70-
7136
/** Returns `true` if the address is a private unroutable address. */
7237
inline bool
73-
is_private(Address const& addr)
38+
is_private(boost::asio::ip::address const& addr)
7439
{
7540
return (addr.is_v4()) ? is_private(addr.to_v4()) : is_private(addr.to_v6());
7641
}
7742

7843
/** Returns `true` if the address is a public routable address. */
7944
inline bool
80-
is_public(Address const& addr)
45+
is_public(boost::asio::ip::address const& addr)
8146
{
8247
return (addr.is_v4()) ? is_public(addr.to_v4()) : is_public(addr.to_v6());
8348
}
@@ -88,7 +53,7 @@ is_public(Address const& addr)
8853

8954
template <class Hasher>
9055
void
91-
hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
56+
hash_append(Hasher& h, boost::asio::ip::address const& addr) noexcept
9257
{
9358
using beast::hash_append;
9459
if (addr.is_v4())
@@ -102,12 +67,12 @@ hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
10267

10368
namespace boost {
10469
template <>
105-
struct hash<::beast::IP::Address>
70+
struct hash<::boost::asio::ip::address>
10671
{
10772
explicit hash() = default;
10873

10974
std::size_t
110-
operator()(::beast::IP::Address const& addr) const
75+
operator()(::boost::asio::ip::address const& addr) const
11176
{
11277
return ::beast::uhash<>{}(addr);
11378
}

src/ripple/beast/net/IPAddressConversion.h

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,65 +22,26 @@
2222

2323
#include <ripple/beast/net/IPEndpoint.h>
2424

25-
#include <sstream>
26-
2725
#include <boost/asio.hpp>
2826

2927
namespace beast {
3028
namespace IP {
3129

32-
/** Convert to Endpoint.
33-
The port is set to zero.
34-
*/
35-
Endpoint
36-
from_asio(boost::asio::ip::address const& address);
37-
3830
/** Convert to Endpoint. */
39-
Endpoint
40-
from_asio(boost::asio::ip::tcp::endpoint const& endpoint);
41-
42-
/** Convert to asio::ip::address.
43-
The port is ignored.
44-
*/
45-
boost::asio::ip::address
46-
to_asio_address(Endpoint const& endpoint);
31+
[[nodiscard]] inline Endpoint
32+
from_asio(boost::asio::ip::tcp::endpoint const& endpoint)
33+
{
34+
return Endpoint{endpoint.address(), endpoint.port()};
35+
}
4736

4837
/** Convert to asio::ip::tcp::endpoint. */
49-
boost::asio::ip::tcp::endpoint
50-
to_asio_endpoint(Endpoint const& endpoint);
51-
52-
} // namespace IP
53-
} // namespace beast
54-
55-
namespace beast {
56-
57-
// DEPRECATED
58-
struct IPAddressConversion
38+
[[nodiscard]] inline boost::asio::ip::tcp::endpoint
39+
to_asio_endpoint(Endpoint const& endpoint)
5940
{
60-
explicit IPAddressConversion() = default;
61-
62-
static IP::Endpoint
63-
from_asio(boost::asio::ip::address const& address)
64-
{
65-
return IP::from_asio(address);
66-
}
67-
static IP::Endpoint
68-
from_asio(boost::asio::ip::tcp::endpoint const& endpoint)
69-
{
70-
return IP::from_asio(endpoint);
71-
}
72-
static boost::asio::ip::address
73-
to_asio_address(IP::Endpoint const& address)
74-
{
75-
return IP::to_asio_address(address);
76-
}
77-
static boost::asio::ip::tcp::endpoint
78-
to_asio_endpoint(IP::Endpoint const& address)
79-
{
80-
return IP::to_asio_endpoint(address);
81-
}
82-
};
41+
return boost::asio::ip::tcp::endpoint{endpoint.address(), endpoint.port()};
42+
}
8343

44+
} // namespace IP
8445
} // namespace beast
8546

8647
#endif

src/ripple/beast/net/IPAddressV4.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,30 @@
2020
#ifndef BEAST_NET_IPADDRESSV4_H_INCLUDED
2121
#define BEAST_NET_IPADDRESSV4_H_INCLUDED
2222

23-
#include <ripple/beast/hash/hash_append.h>
2423
#include <boost/asio/ip/address_v4.hpp>
25-
#include <cstdint>
26-
#include <functional>
27-
#include <ios>
28-
#include <string>
29-
#include <utility>
3024

3125
namespace beast {
3226
namespace IP {
3327

34-
using AddressV4 = boost::asio::ip::address_v4;
35-
3628
/** Returns `true` if the address is a private unroutable address. */
37-
bool
38-
is_private(AddressV4 const& addr);
29+
[[nodiscard]] inline bool
30+
is_private(boost::asio::ip::address_v4 const& addr)
31+
{
32+
return ((addr.to_ulong() & 0xff000000) ==
33+
0x0a000000) || // Prefix /8, 10. #.#.#
34+
((addr.to_ulong() & 0xfff00000) ==
35+
0xac100000) || // Prefix /12 172. 16.#.# - 172.31.#.#
36+
((addr.to_ulong() & 0xffff0000) ==
37+
0xc0a80000) || // Prefix /16 192.168.#.#
38+
addr.is_loopback();
39+
}
3940

4041
/** Returns `true` if the address is a public routable address. */
41-
bool
42-
is_public(AddressV4 const& addr);
43-
44-
/** Returns the address class for the given address.
45-
@note Class 'D' represents multicast addresses (224.*.*.*).
46-
*/
47-
char
48-
get_class(AddressV4 const& address);
42+
[[nodiscard]] inline bool
43+
is_public(boost::asio::ip::address_v4 const& addr)
44+
{
45+
return !is_private(addr) && !addr.is_multicast();
46+
}
4947

5048
} // namespace IP
5149
} // namespace beast

src/ripple/beast/net/IPAddressV6.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,30 @@
2121
#define BEAST_NET_IPADDRESSV6_H_INCLUDED
2222

2323
#include <boost/asio/ip/address_v6.hpp>
24-
#include <cassert>
25-
#include <cstdint>
26-
#include <functional>
27-
#include <ios>
28-
#include <string>
29-
#include <utility>
3024

3125
namespace beast {
3226
namespace IP {
3327

34-
using AddressV6 = boost::asio::ip::address_v6;
35-
3628
/** Returns `true` if the address is a private unroutable address. */
37-
bool
38-
is_private(AddressV6 const& addr);
29+
[[nodiscard]] inline bool
30+
is_private(boost::asio::ip::address_v6 const& addr)
31+
{
32+
auto b0 = addr.to_bytes()[0];
33+
return (
34+
addr.is_link_local() || // fe80::/10
35+
addr.is_loopback() || // ::1
36+
((b0 & 0xfe) == 0xfc) || // fc00::/7 (all ULA)
37+
(addr.is_v4_mapped() && is_private(addr.to_v4())));
38+
}
3939

4040
/** Returns `true` if the address is a public routable address. */
41-
bool
42-
is_public(AddressV6 const& addr);
41+
[[nodiscard]] inline bool
42+
is_public(boost::asio::ip::address_v6 const& addr)
43+
{
44+
return !is_private(addr) &&
45+
!addr.is_multicast() &&
46+
!addr.is_unspecified();
47+
}
4348

4449
} // namespace IP
4550
} // namespace beast

0 commit comments

Comments
 (0)