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/hash/hash_append.h>
24 #include <ripple/beast/hash/uhash.h>
25 #include <ripple/beast/net/IPAddressV4.h>
26 #include <ripple/beast/net/IPAddressV6.h>
27 #include <boost/asio/ip/address.hpp>
28 #include <boost/functional/hash.hpp>
29 #include <cassert>
30 #include <cstdint>
31 #include <ios>
32 #include <sstream>
33 #include <string>
34 #include <typeinfo>
35 
36 //------------------------------------------------------------------------------
37 
38 namespace beast {
39 namespace IP {
40 
41 using Address = boost::asio::ip::address;
42 
44 inline std::string
45 to_string(Address const& addr)
46 {
47  return addr.to_string();
48 }
49 
51 inline bool
52 is_loopback(Address const& addr)
53 {
54  return addr.is_loopback();
55 }
56 
58 inline bool
59 is_unspecified(Address const& addr)
60 {
61  return addr.is_unspecified();
62 }
63 
65 inline bool
66 is_multicast(Address const& addr)
67 {
68  return addr.is_multicast();
69 }
70 
72 inline bool
73 is_private(Address const& addr)
74 {
75  return (addr.is_v4()) ? is_private(addr.to_v4()) : is_private(addr.to_v6());
76 }
77 
79 inline bool
80 is_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 
89 template <class Hasher>
90 void
91 hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
92 {
93  using beast::hash_append;
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  assert(false);
100 }
101 } // namespace beast
102 
103 namespace boost {
104 template <>
105 struct hash<::beast::IP::Address>
106 {
107  explicit hash() = default;
108 
110  operator()(::beast::IP::Address const& addr) const
111  {
112  return ::beast::uhash<>{}(addr);
113  }
114 };
115 } // namespace boost
116 
117 #endif
sstream
boost::hash<::beast::IP::Address >::operator()
std::size_t operator()(::beast::IP::Address const &addr) const
Definition: IPAddress.h:110
std::string
STL class.
beast::IP::is_loopback
bool is_loopback(Address const &addr)
Returns true if this is a loopback address.
Definition: IPAddress.h:52
boost
Definition: IPAddress.h:103
beast::IP::is_multicast
bool is_multicast(Address const &addr)
Returns true if the address is a multicast address.
Definition: IPAddress.h:66
beast::IP::is_private
bool is_private(AddressV4 const &addr)
Returns true if the address is a private unroutable address.
Definition: IPAddressV4.cpp:29
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:45
cstdint
beast::IP::is_public
bool is_public(AddressV4 const &addr)
Returns true if the address is a public routable address.
Definition: IPAddressV4.cpp:41
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
cassert
typeinfo
std::size_t
ios
beast::IP::is_unspecified
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
Definition: IPAddress.h:59
beast
Definition: base_uint.h:641
string