BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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& address::operator=( uint32_t ip )
40  {
41  _ip = ip;
42  return *this;
43  }
44 
45  address::operator std::string()const
46  {
47  try
48  {
49  return boost::asio::ip::address_v4(_ip).to_string().c_str();
50  }
51  FC_RETHROW_EXCEPTIONS(error, "Error parsing IP address to string")
52  }
53  address::operator uint32_t()const {
54  return _ip;
55  }
56 
57 
58  endpoint::endpoint(const address& a, uint16_t p)
59  :_port(p),_ip(a){}
60 
61  endpoint::endpoint(uint32_t a, uint16_t p)
62  :_port(p),_ip(a){}
63 
64  bool operator==( const endpoint& a, const endpoint& b ) {
65  return a._port == b._port && a._ip == b._ip;
66  }
67  bool operator!=( const endpoint& a, const endpoint& b ) {
68  return a._port != b._port || a._ip != b._ip;
69  }
70 
71  bool operator< ( const endpoint& a, const endpoint& b )
72  {
73  return uint32_t(a.get_address()) < uint32_t(b.get_address()) ||
74  (uint32_t(a.get_address()) == uint32_t(b.get_address()) &&
75  uint32_t(a.port()) < uint32_t(b.port()));
76  }
77 
78  uint16_t endpoint::port()const { return _port; }
79  const address& endpoint::get_address()const { return _ip; }
80 
81  endpoint endpoint::from_string( const string& endpoint_string )
82  {
83  try
84  {
85  endpoint ep;
86  auto pos = endpoint_string.find(':');
87  ep._ip = boost::asio::ip::address_v4::from_string(endpoint_string.substr( 0, pos ) ).to_ulong();
88  ep._port = boost::lexical_cast<uint16_t>( endpoint_string.substr( pos+1, endpoint_string.size() ) );
89  return ep;
90  }
91  FC_RETHROW_EXCEPTIONS(warn, "error converting string to IP endpoint")
92  }
93 
94  endpoint::operator string()const
95  {
96  try
97  {
98  return string(_ip) + ':' + std::string(boost::lexical_cast<std::string>(_port).c_str());
99  }
100  FC_RETHROW_EXCEPTIONS(warn, "error converting IP endpoint to string")
101  }
102 
103  /*
104  * @return true if the ip is in the following ranges:
105  *
106  * 10.0.0.0 to 10.255.255.255
107  * 172.16.0.0 to 172.31.255.255
108  * 192.168.0.0 to 192.168.255.255
109  * 169.254.0.0 to 169.254.255.255
110  *
111  */
113  {
114  static address min10_ip("10.0.0.0");
115  static address max10_ip("10.255.255.255");
116  static address min172_ip("172.16.0.0");
117  static address max172_ip("172.31.255.255");
118  static address min192_ip("192.168.0.0");
119  static address max192_ip("192.168.255.255");
120  static address min169_ip("169.254.0.0");
121  static address max169_ip("169.254.255.255");
122  if( _ip >= min10_ip._ip && _ip <= max10_ip._ip ) return true;
123  if( _ip >= min172_ip._ip && _ip <= max172_ip._ip ) return true;
124  if( _ip >= min192_ip._ip && _ip <= max192_ip._ip ) return true;
125  if( _ip >= min169_ip._ip && _ip <= max169_ip._ip ) return true;
126  return false;
127  }
128 
129  /*
130  * 224.0.0.0 to 239.255.255.255
131  */
133  {
134  static address min_ip("224.0.0.0");
135  static address max_ip("239.255.255.255");
136  return _ip >= min_ip._ip && _ip <= max_ip._ip;
137  }
138 
139  /*
140  * 127.0.0.0 to 127.255.255.255
141  */
143  {
144  static address min_ip("127.0.0.0");
145  static address max_ip("127.255.255.255");
146  return _ip >= min_ip._ip && _ip <= max_ip._ip;
147  }
148 
149  // not private, not loopback, and not multicast
151  {
153  }
154 
155 } // namespace ip
156 
157  void to_variant( const ip::endpoint& var, variant& vo, uint32_t max_depth )
158  {
159  vo = std::string(var);
160  }
161  void from_variant( const variant& var, ip::endpoint& vo, uint32_t max_depth )
162  {
164  }
165 
166  void to_variant( const ip::address& var, variant& vo, uint32_t max_depth )
167  {
168  vo = std::string(var);
169  }
170  void from_variant( const variant& var, ip::address& vo, uint32_t max_depth )
171  {
172  vo = ip::address(var.as_string());
173  }
174 
175 }
ip.hpp
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::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::operator!=
bool operator!=(const address &a, const address &b)
Definition: ip.cpp:25
fc::ip::address::is_loopback_address
bool is_loopback_address() const
Definition: ip.cpp:142
fc::variant::as_string
std::string as_string() const
Definition: variant.cpp:469
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
fc::ip::endpoint
Definition: ip.hpp:65
fc::ip::endpoint::get_address
const address & get_address() const
Definition: ip.cpp:79
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::operator==
bool operator==(const address &a, const address &b)
Definition: ip.cpp:22
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
exception.hpp
Defines exception's used by fc.
FC_RETHROW_EXCEPTIONS
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
Definition: exception.hpp:464
variant.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::ip::operator<
bool operator<(const endpoint &a, const endpoint &b)
Definition: ip.cpp:71