BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
ip.cpp
Go to the documentation of this file.
1 #include <fc/network/ip.hpp>
2 #include <fc/variant.hpp>
4 #include <boost/asio.hpp>
5 #include <boost/lexical_cast.hpp>
6 #include <string>
7 
8 namespace fc { namespace ip {
9 
10  address::address( uint32_t ip )
11  :_ip(ip){}
12 
13  address::address( const std::string& s )
14  {
15  try
16  {
17  _ip = boost::asio::ip::address_v4::from_string(s.c_str()).to_ulong();
18  }
19  FC_RETHROW_EXCEPTIONS(error, "Error parsing IP address ${address}", ("address", s))
20  }
21 
22  bool operator==( const address& a, const address& b ) {
23  return uint32_t(a) == uint32_t(b);
24  }
25  bool operator!=( const address& a, const address& b ) {
26  return uint32_t(a) != uint32_t(b);
27  }
28 
29  address& address::operator=( const std::string& s )
30  {
31  try
32  {
33  _ip = boost::asio::ip::address_v4::from_string(s.c_str()).to_ulong();
34  }
35  FC_RETHROW_EXCEPTIONS(error, "Error parsing IP address ${address}", ("address", s))
36  return *this;
37  }
38 
39  address::operator std::string()const
40  {
41  try
42  {
43  return boost::asio::ip::address_v4(_ip).to_string().c_str();
44  }
45  FC_RETHROW_EXCEPTIONS(error, "Error parsing IP address to string")
46  }
47  address::operator uint32_t()const {
48  return _ip;
49  }
50 
51 
53  :_port(0){ }
54  endpoint::endpoint(const address& a, uint16_t p)
55  :_port(p),_ip(a){}
56 
57  bool operator==( const endpoint& a, const endpoint& b ) {
58  return a._port == b._port && a._ip == b._ip;
59  }
60  bool operator!=( const endpoint& a, const endpoint& b ) {
61  return a._port != b._port || a._ip != b._ip;
62  }
63 
64  bool operator< ( const endpoint& a, const endpoint& b )
65  {
66  return uint32_t(a.get_address()) < uint32_t(b.get_address()) ||
67  (uint32_t(a.get_address()) == uint32_t(b.get_address()) &&
68  uint32_t(a.port()) < uint32_t(b.port()));
69  }
70 
71  uint16_t endpoint::port()const { return _port; }
72  const address& endpoint::get_address()const { return _ip; }
73 
74  endpoint endpoint::from_string( const string& endpoint_string )
75  {
76  try
77  {
78  endpoint ep;
79  auto pos = endpoint_string.find(':');
80  ep._ip = boost::asio::ip::address_v4::from_string(endpoint_string.substr( 0, pos ) ).to_ulong();
81  ep._port = boost::lexical_cast<uint16_t>( endpoint_string.substr( pos+1, endpoint_string.size() ) );
82  return ep;
83  }
84  FC_RETHROW_EXCEPTIONS(warn, "error converting string to IP endpoint")
85  }
86 
87  endpoint::operator string()const
88  {
89  try
90  {
91  return string(_ip) + ':' + std::string(boost::lexical_cast<std::string>(_port).c_str());
92  }
93  FC_RETHROW_EXCEPTIONS(warn, "error converting IP endpoint to string")
94  }
95 
106  {
107  static address min10_ip("10.0.0.0");
108  static address max10_ip("10.255.255.255");
109  static address min172_ip("172.16.0.0");
110  static address max172_ip("172.31.255.255");
111  static address min192_ip("192.168.0.0");
112  static address max192_ip("192.168.255.255");
113  static address min169_ip("169.254.0.0");
114  static address max169_ip("169.254.255.255");
115  if( _ip >= min10_ip._ip && _ip <= max10_ip._ip ) return true;
116  if( _ip >= min172_ip._ip && _ip <= max172_ip._ip ) return true;
117  if( _ip >= min192_ip._ip && _ip <= max192_ip._ip ) return true;
118  if( _ip >= min169_ip._ip && _ip <= max169_ip._ip ) return true;
119  return false;
120  }
121 
126  {
127  static address min_ip("224.0.0.0");
128  static address max_ip("239.255.255.255");
129  return _ip >= min_ip._ip && _ip <= max_ip._ip;
130  }
131 
134  {
135  return !( is_private_address() || is_multicast_address() );
136  }
137 
138 } // namespace ip
139 
140  void to_variant( const ip::endpoint& var, variant& vo, uint32_t max_depth )
141  {
142  vo = std::string(var);
143  }
144  void from_variant( const variant& var, ip::endpoint& vo, uint32_t max_depth )
145  {
147  }
148 
149  void to_variant( const ip::address& var, variant& vo, uint32_t max_depth )
150  {
151  vo = std::string(var);
152  }
153  void from_variant( const variant& var, ip::address& vo, uint32_t max_depth )
154  {
155  vo = ip::address(var.as_string());
156  }
157 
158 }
bool is_public_address() const
Definition: ip.cpp:133
friend bool operator!=(const endpoint &a, const endpoint &b)
Definition: ip.cpp:60
address(uint32_t _ip=0)
Definition: ip.cpp:10
bool is_multicast_address() const
Definition: ip.cpp:125
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception&#39;s, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:463
const address & get_address() const
Definition: ip.cpp:72
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
friend bool operator==(const endpoint &a, const endpoint &b)
Definition: ip.cpp:57
address & operator=(const std::string &s)
Definition: ip.cpp:29
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
static endpoint from_string(const string &s)
Definition: ip.cpp:74
Defines exception&#39;s used by fc.
friend bool operator!=(const address &a, const address &b)
Definition: ip.cpp:25
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
Definition: api.hpp:15
friend bool operator==(const address &a, const address &b)
Definition: ip.cpp:22
std::string as_string() const
Definition: variant.cpp:469
friend bool operator<(const endpoint &a, const endpoint &b)
Definition: ip.cpp:64