BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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  address( uint32_t _ip = 0 );
13  address( const std::string& s );
14 
15  address& operator=( const std::string& s );
16  operator std::string()const;
17  operator uint32_t()const;
18 
19  friend bool operator==( const address& a, const address& b );
20  friend bool operator!=( const address& a, const address& b );
21 
31  bool is_private_address()const;
35  bool is_multicast_address()const;
36 
38  bool is_public_address()const;
39  private:
40  uint32_t _ip;
41  };
42 
43  class endpoint {
44  public:
45  endpoint();
46  endpoint( const address& i, uint16_t p = 0);
47 
49  static endpoint from_string( const string& s );
51  operator string()const;
52 
53  void set_port(uint16_t p ) { _port = p; }
54  uint16_t port()const;
55  const address& get_address()const;
56 
57  friend bool operator==( const endpoint& a, const endpoint& b );
58  friend bool operator!=( const endpoint& a, const endpoint& b );
59  friend bool operator< ( const endpoint& a, const endpoint& b );
60 
61  private:
69  uint32_t _port;
70  address _ip;
71  };
72 
73  }
74  class variant;
75  void to_variant( const ip::endpoint& var, variant& vo, uint32_t _max_depth = 2 );
76  void from_variant( const variant& var, ip::endpoint& vo, uint32_t _max_depth = 2 );
77 
78  void to_variant( const ip::address& var, variant& vo, uint32_t _max_depth = 1 );
79  void from_variant( const variant& var, ip::address& vo, uint32_t _max_depth = 1 );
80 
81 
82  namespace raw
83  {
84  template<typename Stream>
85  void pack( Stream& s, const ip::address& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
86  {
87  FC_ASSERT( _max_depth > 0 );
88  fc::raw::pack( s, uint32_t(v), _max_depth - 1 );
89  }
90  template<typename Stream>
91  void unpack( Stream& s, ip::address& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH )
92  {
93  FC_ASSERT( _max_depth > 0 );
94  uint32_t _ip;
95  fc::raw::unpack( s, _ip, _max_depth - 1 );
96  v = ip::address(_ip);
97  }
98 
99  template<typename Stream>
100  inline void pack( Stream& s, const ip::endpoint& v, uint32_t _max_depth )
101  {
102  FC_ASSERT( _max_depth > 0 );
103  --_max_depth;
104  fc::raw::pack( s, v.get_address(), _max_depth );
105  fc::raw::pack( s, v.port(), _max_depth );
106  }
107  template<typename Stream>
108  inline void unpack( Stream& s, ip::endpoint& v, uint32_t _max_depth )
109  {
110  FC_ASSERT( _max_depth > 0 );
111  --_max_depth;
112  ip::address a;
113  uint16_t p;
114  fc::raw::unpack( s, a, _max_depth );
115  fc::raw::unpack( s, p, _max_depth );
116  v = ip::endpoint(a,p);
117  }
118 
119  }
120 } // namespace fc
123 namespace std
124 {
125  template<>
126  struct hash<fc::ip::endpoint>
127  {
128  size_t operator()( const fc::ip::endpoint& e )const
129  {
130  return fc::city_hash_size_t( (char*)&e, sizeof(e) );
131  }
132  };
133 }
bool is_public_address() const
Definition: ip.cpp:133
void pack(Stream &s, const ip::address &v, uint32_t _max_depth=FC_PACK_MAX_DEPTH)
Definition: ip.hpp:85
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
size_t city_hash_size_t(const char *buf, size_t len)
Definition: city.hpp:59
void unpack(Stream &s, ip::address &v, uint32_t _max_depth=FC_PACK_MAX_DEPTH)
Definition: ip.hpp:91
Defines types and macros used to provide reflection.
void set_port(uint16_t p)
Definition: ip.hpp:53
address(uint32_t _ip=0)
Definition: ip.cpp:10
bool is_multicast_address() const
Definition: ip.cpp:125
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
const address & get_address() const
Definition: ip.cpp:72
FC_REFLECT_TYPENAME(fc::log_message)
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
uint16_t port() const
Definition: ip.cpp:71
size_t operator()(const fc::ip::endpoint &e) const
Definition: ip.hpp:128
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
address & operator=(const std::string &s)
Definition: ip.cpp:29
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:240
bool is_private_address() const
Definition: ip.cpp:105
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
friend bool operator!=(const address &a, const address &b)
Definition: ip.cpp:25
bool operator<(const endpoint &a, const endpoint &b)
Definition: ip.cpp:64
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
Definition: api.hpp:15
#define FC_PACK_MAX_DEPTH
Definition: config.hpp:3
friend bool operator==(const address &a, const address &b)
Definition: ip.cpp:22