BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
ip.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/crypto/sha1.hpp>
3 #include <fc/io/raw_fwd.hpp>
4 #include <fc/crypto/city.hpp>
5 #include <fc/reflect/reflect.hpp>
6 
7 namespace fc {
8 
9  namespace ip {
10  class address {
11  public:
12  explicit address( uint32_t ip = 0 );
13  explicit address( const std::string& s );
14 
15  address& operator=( const std::string& s );
16  address& operator=( uint32_t ip );
17  explicit operator std::string()const;
18  // Note: there are quite a few places where it is used implicitly, so we haven't marked it as explicit so far.
19  operator uint32_t()const;
20 
21  friend bool operator==( const address& a, const address& b );
22  friend bool operator!=( const address& a, const address& b );
23 
33  bool is_private_address()const;
37  bool is_multicast_address()const;
41  bool is_loopback_address()const;
42 
60  bool is_public_address()const;
61  private:
62  uint32_t _ip;
63  };
64 
65  class endpoint {
66  public:
67  endpoint() = default;
68  explicit endpoint( const address& i, uint16_t p = 0);
69  explicit endpoint( uint32_t i, uint16_t p = 0);
70 
72  static endpoint from_string( const string& s );
74  explicit operator string()const;
75 
76  void set_port(uint16_t p ) { _port = p; }
77  uint16_t port()const;
78  const address& get_address()const;
79 
80  friend bool operator==( const endpoint& a, const endpoint& b );
81  friend bool operator!=( const endpoint& a, const endpoint& b );
82  friend bool operator< ( const endpoint& a, const endpoint& b );
83 
84  private:
92  uint32_t _port = 0;
93  address _ip;
94  };
95 
96  }
97  class variant;
98  void to_variant( const ip::endpoint& var, variant& vo, uint32_t _max_depth = 2 );
99  void from_variant( const variant& var, ip::endpoint& vo, uint32_t _max_depth = 2 );
100 
101  void to_variant( const ip::address& var, variant& vo, uint32_t _max_depth = 1 );
102  void from_variant( const variant& var, ip::address& vo, uint32_t _max_depth = 1 );
103 
104 
105  namespace raw
106  {
107  template<typename Stream>
108  void pack( Stream& s, const ip::address& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
109  {
110  FC_ASSERT( _max_depth > 0 );
111  fc::raw::pack( s, uint32_t(v), _max_depth - 1 );
112  }
113  template<typename Stream>
114  void unpack( Stream& s, ip::address& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
115  {
116  FC_ASSERT( _max_depth > 0 );
117  uint32_t _ip;
118  fc::raw::unpack( s, _ip, _max_depth - 1 );
119  v = ip::address(_ip);
120  }
121 
122  template<typename Stream>
123  inline void pack( Stream& s, const ip::endpoint& v, uint32_t _max_depth )
124  {
125  FC_ASSERT( _max_depth > 0 );
126  --_max_depth;
127  fc::raw::pack( s, v.get_address(), _max_depth );
128  fc::raw::pack( s, v.port(), _max_depth );
129  }
130  template<typename Stream>
131  inline void unpack( Stream& s, ip::endpoint& v, uint32_t _max_depth )
132  {
133  FC_ASSERT( _max_depth > 0 );
134  --_max_depth;
135  ip::address a;
136  uint16_t p;
137  fc::raw::unpack( s, a, _max_depth );
138  fc::raw::unpack( s, p, _max_depth );
139  v = ip::endpoint(a,p);
140  }
141 
142  }
143 } // namespace fc
146 namespace std
147 {
148  template<>
149  struct hash<fc::ip::endpoint>
150  {
151  size_t operator()( const fc::ip::endpoint& e )const
152  {
153  return fc::city_hash_size_t( (char*)&e, sizeof(e) );
154  }
155  };
156 }
FC_PACK_MAX_DEPTH
#define FC_PACK_MAX_DEPTH
Definition: config.hpp:3
fc::ip::address::operator!=
friend bool operator!=(const address &a, const address &b)
Definition: ip.cpp:25
fc::ip::address::is_multicast_address
bool is_multicast_address() const
Definition: ip.cpp:132
fc::ip::address::address
address(uint32_t ip=0)
Definition: ip.cpp:10
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc::ip::address::is_private_address
bool is_private_address() const
Definition: ip.cpp:112
fc
Definition: api.hpp:15
fc::ip::address
Definition: ip.hpp:10
fc::ip::endpoint::endpoint
endpoint()=default
fc::ip::endpoint::from_string
static endpoint from_string(const string &s)
Definition: ip.cpp:81
fc::ip::endpoint::operator<
friend bool operator<(const endpoint &a, const endpoint &b)
Definition: ip.cpp:71
fc::ip::address::is_loopback_address
bool is_loopback_address() const
Definition: ip.cpp:142
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
reflect.hpp
Defines types and macros used to provide reflection.
fc::ip::endpoint::set_port
void set_port(uint16_t p)
Definition: ip.hpp:76
fc::asio::tcp::endpoint
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:239
fc::ip::endpoint
Definition: ip.hpp:65
fc::ip::endpoint::operator==
friend bool operator==(const endpoint &a, const endpoint &b)
Definition: ip.cpp:64
fc::ip::endpoint::get_address
const address & get_address() const
Definition: ip.cpp:79
sha1.hpp
fc::ip::address::is_public_address
bool is_public_address() const
Definition: ip.cpp:150
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::ip::address::operator==
friend bool operator==(const address &a, const address &b)
Definition: ip.cpp:22
fc::city_hash_size_t
size_t city_hash_size_t(const char *buf, size_t len)
Definition: city.hpp:59
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::ip::endpoint::operator!=
friend bool operator!=(const endpoint &a, const endpoint &b)
Definition: ip.cpp:67
city.hpp
std
Definition: zeroed_array.hpp:76
raw_fwd.hpp
fc::ip::endpoint::port
uint16_t port() const
Definition: ip.cpp:78
fc::ip::address::operator=
address & operator=(const std::string &s)
Definition: ip.cpp:29
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
std::hash< fc::ip::endpoint >::operator()
size_t operator()(const fc::ip::endpoint &e) const
Definition: ip.hpp:151