Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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
8namespace fc { namespace ip {
9
11 :_ip(ip){}
12
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
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 fc::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){ }
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) + ':' + fc::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 )
141 {
142 vo = fc::string(var);
143 }
144 void from_variant( const variant& var, ip::endpoint& vo )
145 {
147 }
148
149 void to_variant( const ip::address& var, variant& vo )
150 {
151 vo = fc::string(var);
152 }
153 void from_variant( const variant& var, ip::address& vo )
154 {
155 vo = ip::address(var.as_string());
156 }
157
158}
const mie::Vuint & p
Definition bn.cpp:27
address & operator=(const fc::string &s)
Definition ip.cpp:29
bool is_private_address() const
Definition ip.cpp:105
bool is_public_address() const
Definition ip.cpp:133
bool is_multicast_address() const
Definition ip.cpp:125
address(uint32_t _ip=0)
Definition ip.cpp:10
const address & get_address() const
Definition ip.cpp:72
static endpoint from_string(const string &s)
Definition ip.cpp:74
uint16_t port() const
Definition ip.cpp:71
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define FC_RETHROW_EXCEPTIONS(LOG_LEVEL, FORMAT,...)
Catchs all exception's, std::exceptions, and ... and rethrows them after appending the provided log m...
bool operator<(const endpoint &a, const endpoint &b)
Definition ip.cpp:64
bool operator!=(const address &a, const address &b)
Definition ip.cpp:25
bool operator==(const address &a, const address &b)
Definition ip.cpp:22
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
char * s