rippled
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 <ripple/beast/net/IPAddressV4.h>
24 #include <ripple/beast/net/IPAddressV6.h>
25 #include <ripple/beast/hash/hash_append.h>
26 #include <ripple/beast/hash/uhash.h>
27 #include <boost/functional/hash.hpp>
28 #include <boost/asio/ip/address.hpp>
29 #include <cassert>
30 #include <cstdint>
31 #include <ios>
32 #include <string>
33 #include <sstream>
34 #include <typeinfo>
35 
36 //------------------------------------------------------------------------------
37 
38 namespace beast {
39 namespace IP {
40 
41 using Address = boost::asio::ip::address;
42 
44 inline
46 to_string (Address const& addr)
47 {
48  return addr.to_string ();
49 }
50 
52 inline
53 bool
54 is_loopback (Address const& addr)
55 {
56  return addr.is_loopback();
57 }
58 
60 inline
61 bool
62 is_unspecified (Address const& addr)
63 {
64  return addr.is_unspecified();
65 }
66 
68 inline
69 bool
70 is_multicast (Address const& addr)
71 {
72  return addr.is_multicast();
73 }
74 
76 inline
77 bool
78 is_private (Address const& addr)
79 {
80  return (addr.is_v4 ())
81  ? is_private (addr.to_v4 ())
82  : is_private (addr.to_v6 ());
83 }
84 
86 inline
87 bool
88 is_public (Address const& addr)
89 {
90  return (addr.is_v4 ())
91  ? is_public (addr.to_v4 ())
92  : is_public (addr.to_v6 ());
93 }
94 
95 }
96 
97 //------------------------------------------------------------------------------
98 
99 template <class Hasher>
100 void
101 hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
102 {
103  using beast::hash_append;
104  if (addr.is_v4 ())
105  hash_append(h, addr.to_v4().to_bytes());
106  else if (addr.is_v6 ())
107  hash_append(h, addr.to_v6().to_bytes());
108  else
109  assert (false);
110 }
111 }
112 
113 namespace std {
114 template <>
115 struct hash <beast::IP::Address>
116 {
117  explicit hash() = default;
118 
120  operator() (beast::IP::Address const& addr) const
121  {
122  return beast::uhash<>{} (addr);
123  }
124 };
125 }
126 
127 namespace boost {
128 template <>
129 struct hash <::beast::IP::Address>
130 {
131  explicit hash() = default;
132 
134  operator() (::beast::IP::Address const& addr) const
135  {
136  return ::beast::uhash<>{} (addr);
137  }
138 };
139 }
140 
141 #endif
std::hash::operator()
T operator()(T... args)
sstream
std::string
STL class.
beast::hash_append
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
beast::IP::is_loopback
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:54
boost
Definition: IPAddress.h:127
beast::IP::is_multicast
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition: IPAddress.h:70
beast::IP::is_private
bool is_private(AddressV4 const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddressV4.cpp:28
beast::IP::Address
boost::asio::ip::address Address
Definition: IPAddress.h:41
beast::IP::to_string
std::string to_string(Address const &addr)
Returns the address represented as a string.
Definition: IPAddress.h:46
cstdint
beast::IP::is_public
bool is_public(AddressV4 const &addr)
Returns true if the address is a public routable address.
Definition: IPAddressV4.cpp:37
std
STL namespace.
cassert
typeinfo
std::size_t
beast::uhash<>
ios
beast::IP::is_unspecified
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:62
std::hash
beast
Definition: base_uint.h:582
string