peerplays-fc/src/network/ip.cpp

102 lines
2.7 KiB
C++
Raw Normal View History

#include <fc/network/ip.hpp>
#include <fc/variant.hpp>
2012-09-08 21:37:25 +00:00
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
namespace fc { namespace ip {
address::address( uint32_t ip )
:_ip(ip){}
address::address( const fc::string& s ) {
_ip = boost::asio::ip::address_v4::from_string(s.c_str()).to_ulong();
}
2012-09-09 03:46:19 +00:00
bool operator==( const address& a, const address& b ) {
return uint32_t(a) == uint32_t(b);
}
bool operator!=( const address& a, const address& b ) {
return uint32_t(a) != uint32_t(b);
}
2012-09-09 03:46:19 +00:00
2012-09-08 21:37:25 +00:00
address& address::operator=( const fc::string& s ) {
_ip = boost::asio::ip::address_v4::from_string(s.c_str()).to_ulong();
return *this;
}
address::operator fc::string()const {
return boost::asio::ip::address_v4(_ip).to_string().c_str();
}
2012-09-09 03:46:19 +00:00
address::operator uint32_t()const {
return _ip;
}
2012-09-08 21:37:25 +00:00
endpoint::endpoint()
2012-09-25 21:45:28 +00:00
:_port(0){ }
2012-09-08 21:37:25 +00:00
endpoint::endpoint(const address& a, uint16_t p)
:_port(p),_ip(a){}
2012-09-09 03:46:19 +00:00
bool operator==( const endpoint& a, const endpoint& b ) {
return a._port == b._port && a._ip == b._ip;
}
bool operator!=( const endpoint& a, const endpoint& b ) {
return a._port != b._port || a._ip != b._ip;
}
bool operator< ( const endpoint& a, const endpoint& b )
{
return uint32_t(a.get_address()) < uint32_t(b.get_address()) ||
(uint32_t(a.get_address()) == uint32_t(b.get_address()) &&
uint32_t(a.port()) < uint32_t(b.port()));
}
2012-09-08 21:37:25 +00:00
uint16_t endpoint::port()const { return _port; }
const address& endpoint::get_address()const { return _ip; }
endpoint endpoint::from_string( const string& s ) {
endpoint ep;
const std::string& st = reinterpret_cast<const std::string&>(s);
auto pos = st.find(':');
ep._ip = boost::asio::ip::address_v4::from_string(st.substr( 0, pos ) ).to_ulong();
ep._port = boost::lexical_cast<uint16_t>( st.substr( pos+1, s.size() ) );
return ep;
}
endpoint::operator string()const {
2012-09-10 01:44:57 +00:00
return string(_ip) + ':' + fc::string(boost::lexical_cast<std::string>(_port).c_str());
2012-09-08 21:37:25 +00:00
}
} // namespace ip
void to_variant( const ip::endpoint& var, variant& vo )
{
vo = fc::string(var);
}
void from_variant( const variant& var, ip::endpoint& vo )
{
vo = ip::endpoint::from_string(var.as_string());
}
void to_variant( const ip::address& var, variant& vo )
{
vo = fc::string(var);
}
void from_variant( const variant& var, ip::address& vo )
{
vo = ip::address(var.as_string());
}
}
2013-07-12 05:21:53 +00:00
namespace std
{
size_t hash<fc::ip::endpoint>::operator()( const fc::ip::endpoint& e )const
{
auto h = fc::sha1::hash( (char*)&e, sizeof(e) );
size_t s;
memcpy( (char*)&s, (char*)&h, sizeof(s) );
return s;
}
}