rippled
Loading...
Searching...
No Matches
IPAddress.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_NET_IPADDRESS_H_INCLUDED
21#define BEAST_NET_IPADDRESS_H_INCLUDED
22
23#include <xrpl/beast/hash/hash_append.h>
24#include <xrpl/beast/hash/uhash.h>
25#include <xrpl/beast/net/IPAddressV4.h>
26#include <xrpl/beast/net/IPAddressV6.h>
27#include <xrpl/beast/utility/instrumentation.h>
28#include <boost/asio/ip/address.hpp>
29#include <boost/functional/hash.hpp>
30#include <cstdint>
31#include <ios>
32#include <sstream>
33#include <string>
34#include <typeinfo>
35
36//------------------------------------------------------------------------------
37
38namespace beast {
39namespace IP {
40
41using Address = boost::asio::ip::address;
42
44inline std::string
45to_string(Address const& addr)
46{
47 return addr.to_string();
48}
49
51inline bool
52is_loopback(Address const& addr)
53{
54 return addr.is_loopback();
55}
56
58inline bool
60{
61 return addr.is_unspecified();
62}
63
65inline bool
67{
68 return addr.is_multicast();
69}
70
72inline bool
73is_private(Address const& addr)
74{
75 return (addr.is_v4()) ? is_private(addr.to_v4()) : is_private(addr.to_v6());
76}
77
79inline bool
80is_public(Address const& addr)
81{
82 return (addr.is_v4()) ? is_public(addr.to_v4()) : is_public(addr.to_v6());
83}
84
85} // namespace IP
86
87//------------------------------------------------------------------------------
88
89template <class Hasher>
90void
91hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
92{
94 if (addr.is_v4())
95 hash_append(h, addr.to_v4().to_bytes());
96 else if (addr.is_v6())
97 hash_append(h, addr.to_v6().to_bytes());
98 else
99 UNREACHABLE("beast::hash_append : invalid address type");
100}
101} // namespace beast
102
103namespace boost {
104template <>
105struct hash<::beast::IP::Address>
106{
107 explicit hash() = default;
108
111 {
112 return ::beast::uhash<>{}(addr);
113 }
114};
115} // namespace boost
116
117#endif
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition: IPAddress.h:66
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:52
bool is_public(Address const &addr)
Returns true if the address is a public routable address.
Definition: IPAddress.h:80
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:59
boost::asio::ip::address Address
Definition: IPAddress.h:41
bool is_private(Address const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddress.h:73
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition: IPAddress.h:45
std::enable_if_t< is_contiguously_hashable< T, Hasher >::value > hash_append(Hasher &h, T const &t) noexcept
Logically concatenate input data to a Hasher.
Definition: hash_append.h:236
std::size_t operator()(::beast::IP::Address const &addr) const
Definition: IPAddress.h:110