/** \brief Overload the substraction */ ip_addr_t ip_addr_t::operator- (const uint32_t val) const throw() { // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // return the result return ip_addr_t( get_v4_addr() - val ); }
// Get the underlying size of the endpoint in the native type. std::size_t size() const { if (is_v4()) return sizeof(boost::asio::detail::sockaddr_in4_type); else return sizeof(boost::asio::detail::sockaddr_in6_type); }
bool ip_address::operator==(const ip_address& rhs) const { if( af != rhs.af ) return false; if( is_v4() ) { return std::memcmp(v4addr(), rhs.v4addr(), sizeof(struct in_addr)) == 0; } return std::memcmp(v6addr(), rhs.v6addr(), sizeof(struct in6_addr)) == 0; }
string SocketImpl::get_remote_endpoint( void ) { error_code error; tcp::endpoint endpoint; #ifdef BUILD_SSL if ( m_socket not_eq nullptr ) { #endif endpoint = m_socket->remote_endpoint( error ); #ifdef BUILD_SSL } else { endpoint = m_ssl_socket->lowest_layer( ).remote_endpoint( error ); } #endif if ( error ) { m_is_open = false; } auto address = endpoint.address( ); auto remote = address.is_v4( ) ? address.to_string( ) + ":" : "[" + address.to_string( ) + "]:"; remote += ::to_string( endpoint.port( ) ); return remote; }
std::string ip_address::str() const { if( is_v4() ) { char dst[INET_ADDRSTRLEN]; return std::string(inet_ntop(AF_INET, v4addr(), dst, INET_ADDRSTRLEN)); } char dst[INET6_ADDRSTRLEN]; return std::string(inet_ntop(AF_INET6, v6addr(), dst, INET6_ADDRSTRLEN)); }
/** \brief return true is the ip_addr is linklocal - as in rfc3927.2.1 */ bool ip_addr_t::is_linklocal() const throw() { // if it is_null(), return false now if( is_null() ) return false; // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // test against linklocal network address if( ip_netaddr_t("169.254.0.0", 16).contain(*this) ) return true; return false; }
/** \brief return true if the ip_addr_t is broadcast */ bool ip_addr_t::is_broadcast() const throw() { // if it is_null(), return false now if( is_null() ) return false; // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // test if( *this != "255.255.255.255" ) return false; return true; }
/** \brief return true is the ip_addr is multicast */ bool ip_addr_t::is_multicast() const throw() { // if it is_null(), return false now if( is_null() ) return false; // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // test against multicast address if( ip_netaddr_t("224.0.0.0", 4).contain(*this) ) return true; return false; }
std::string to_string() const { char Dest[46]; if(is_v4()) _inet_ntop(AF_INET, (void*)&ip_[0], Dest, 46); else if(is_v6()) _inet_ntop(AF_INET6, (void*)&ip_[0], Dest, 46); else Dest[0] = 0; return Dest; }
/** \brief convert the address to a string */ std::string ip_addr_t::to_string() const throw() { if( is_null() ) return "null"; // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // convert the address into a string struct in_addr inaddr; inaddr.s_addr = htonl(get_v4_addr()); // put the string into the ostream return inet_ntoa( inaddr ); }
//========================================================================================================== //========================================================================================================== bool IP_Address::is_multicast() const { if (is_v4()) { return (_addr.ipv4.sin_addr.s_addr & 0x000000F0) == 0x000000E0; } else { return (_addr.ipv6.sin6_addr.s6_addr[0] == 0xff); } }
struct in_addr hlt_addr_to_in4(hlt_addr addr, hlt_exception** excpt, hlt_execution_context* ctx) { if ( ! is_v4(addr) ) { hlt_set_exception(excpt, &hlt_exception_value_error, 0, ctx); struct in_addr sa; return sa; } unsigned long a = (unsigned long)addr.a2; struct in_addr sa = { hlt_hton32(a) }; return sa; }
//========================================================================================================== //========================================================================================================== bool IP_Address::is_link_local() const { if (is_v4()) { return (_addr.ipv4.sin_addr.s_addr & 0x0000FFFF) == 0x0000FEA9; } else { return ((_addr.ipv6.sin6_addr.s6_addr[0] == 0xFE) && (_addr.ipv6.sin6_addr.s6_addr[1] >= 0x80) && (_addr.ipv6.sin6_addr.s6_addr[1] < 0xc0)); } }
/** \brief return true is the ip_addr is private - as in rfc1918.3 */ bool ip_addr_t::is_private() const throw() { // if it is_null(), return false now if( is_null() ) return false; // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // test against rfc1918 network address if( ip_netaddr_t("10.0.0.0", 8).contain(*this) ) return true; if( ip_netaddr_t("172.16.0.0", 12).contain(*this) ) return true; if( ip_netaddr_t("192.168.0.0", 16).contain(*this) ) return true; return false; }
/** \brief convert a ip_addr_t to a sockaddr_in */ struct sockaddr_in ip_addr_t::to_sockaddr_in() const throw() { struct sockaddr_in sa_in; // sanity check - currently only handle ipv4 DBG_ASSERT( is_v4() ); // zero the struct memset( &sa_in, 0, sizeof(sa_in) ); // set the struct sa_in.sin_family = AF_INET; sa_in.sin_addr.s_addr = htonl( address.v4 ); // return the built object return sa_in; }
//========================================================================================================== //========================================================================================================== std::string IP_Address::to_string() const { char result[MAX_IP_ADDRESS_STR_LENGTH]; if (is_v4()) { inet_ntop(AF_INET, &_addr.ipv4.sin_addr.s_addr, result, sizeof(result)); } else { inet_ntop(AF_INET6, &_addr.ipv6.sin6_addr.s6_addr, result, sizeof(result)); } return string(result); }
/** \brief compare 2 ip_addr_t and return value ala strcmp/memcmp */ int ip_addr_t::compare( const ip_addr_t &other ) const throw() { // handle the null case if( is_null() && !other.is_null() ) return -1; if( !is_null() && other.is_null() ) return +1; if( is_null() && other.is_null() ) return 0; // NOTE: here both are not null // handle only v4 address DBG_ASSERT( is_v4() && other.is_v4() ); // handle the address if( get_v4_addr() < other.get_v4_addr() ) return -1; if( get_v4_addr() > other.get_v4_addr() ) return +1; return 0; }
//========================================================================================================== //========================================================================================================== bool IP_Address::is_loopback() const { if(is_v4()) { return (_addr.ipv4.sin_addr.s_addr & 0x000000FF) == 0x0000007F; } else { return ((_addr.ipv6.sin6_addr.s6_addr[0] == 0) && (_addr.ipv6.sin6_addr.s6_addr[1] == 0) && (_addr.ipv6.sin6_addr.s6_addr[2] == 0) && (_addr.ipv6.sin6_addr.s6_addr[3] == 0) && (_addr.ipv6.sin6_addr.s6_addr[4] == 0) && (_addr.ipv6.sin6_addr.s6_addr[5] == 0) && (_addr.ipv6.sin6_addr.s6_addr[6] == 0) && (_addr.ipv6.sin6_addr.s6_addr[7] == 0) && (_addr.ipv6.sin6_addr.s6_addr[8] == 0) && (_addr.ipv6.sin6_addr.s6_addr[9] == 0) && (_addr.ipv6.sin6_addr.s6_addr[10] == 0) && (_addr.ipv6.sin6_addr.s6_addr[11] == 0) && (_addr.ipv6.sin6_addr.s6_addr[12] == 0) && (_addr.ipv6.sin6_addr.s6_addr[13] == 0) && (_addr.ipv6.sin6_addr.s6_addr[14] == 0) && (_addr.ipv6.sin6_addr.s6_addr[15] == 1)); } }
//========================================================================================================== //========================================================================================================== bool IP_Address::is_unspecified() const { if (is_v4()) { return _addr.ipv4.sin_addr.s_addr == 0; } else { return ((_addr.ipv6.sin6_addr.s6_addr[0] == 0) && (_addr.ipv6.sin6_addr.s6_addr[1] == 0) && (_addr.ipv6.sin6_addr.s6_addr[2] == 0) && (_addr.ipv6.sin6_addr.s6_addr[3] == 0) && (_addr.ipv6.sin6_addr.s6_addr[4] == 0) && (_addr.ipv6.sin6_addr.s6_addr[5] == 0) && (_addr.ipv6.sin6_addr.s6_addr[6] == 0) && (_addr.ipv6.sin6_addr.s6_addr[7] == 0) && (_addr.ipv6.sin6_addr.s6_addr[8] == 0) && (_addr.ipv6.sin6_addr.s6_addr[9] == 0) && (_addr.ipv6.sin6_addr.s6_addr[10] == 0) && (_addr.ipv6.sin6_addr.s6_addr[11] == 0) && (_addr.ipv6.sin6_addr.s6_addr[12] == 0) && (_addr.ipv6.sin6_addr.s6_addr[13] == 0) && (_addr.ipv6.sin6_addr.s6_addr[14] == 0) && (_addr.ipv6.sin6_addr.s6_addr[15] == 0)); } }
hlt_string hlt_addr_to_string(const hlt_type_info* type, const void* obj, int32_t options, __hlt_pointer_stack* seen, hlt_exception** excpt, hlt_execution_context* ctx) { assert(type->type == HLT_TYPE_ADDR); hlt_addr addr = *((hlt_addr *)obj); if ( is_v4(addr) ) { unsigned long a = (unsigned long)addr.a2; struct in_addr sa = { hlt_hton32(a) }; char buffer[INET_ADDRSTRLEN]; if ( ! inet_ntop(AF_INET, &sa, buffer, sizeof(buffer)) ) { hlt_set_exception(excpt, &hlt_exception_os_error, 0, ctx); return 0; } return hlt_string_from_asciiz(buffer, excpt, ctx); } else { struct in6_addr sa; uint64_t a = hlt_hton64(addr.a1); memcpy(&sa, &a, 8); a = hlt_hton64(addr.a2); memcpy(((char*)&sa) + 8, &a, 8); char buffer[INET6_ADDRSTRLEN]; if ( ! inet_ntop(AF_INET6, &sa, buffer, sizeof(buffer)) ) { hlt_set_exception(excpt, &hlt_exception_os_error, 0, ctx); return 0; } return hlt_string_from_asciiz(buffer, excpt, ctx); } }
//========================================================================================================== //========================================================================================================== void IP_Address::to_sockaddr(sockaddr *sa) const { memset(sa, 0, sizeof(*sa)); if(is_v4()) { inet_pton(AF_INET, to_string().c_str(), &((sockaddr_in *)sa)->sin_addr); ((sockaddr_in *)sa)->sin_family = AF_INET; ((sockaddr_in *)sa)->sin_port = _addr.ipv4.sin_port; #ifdef __APPLE__ ((sockaddr_in *)sa)->sin_len = sizeof(sockaddr_in); #endif } else { inet_pton(AF_INET6, to_string().c_str(), &((sockaddr_in6 *)sa)->sin6_addr); ((sockaddr_in6 *)sa)->sin6_family = AF_INET6; ((sockaddr_in6 *)sa)->sin6_port = _addr.ipv6.sin6_port; #ifdef __APPLE__ ((sockaddr_in6 *)sa)->sin6_len = sizeof(sockaddr_in6); #endif } }
string SocketImpl::get_remote_endpoint( void ) const { tcp::endpoint endpoint; #ifdef BUILD_SSL if ( m_socket not_eq nullptr ) { #endif endpoint = m_socket->remote_endpoint( ); #ifdef BUILD_SSL } else { endpoint = m_ssl_socket->lowest_layer( ).remote_endpoint( ); } #endif auto address = endpoint.address( ); auto remote = address.is_v4( ) ? address.to_string( ) : "[" + address.to_string( ) + "]:"; remote += ::to_string( endpoint.port( ) ); return remote; }
address_v4(const address& other) : address(other) {TORRENT_ASSERT(is_v4());}
const address& to_v4() const {TORRENT_ASSERT(is_v4()); return *this;}
int8_t hlt_addr_is_v6(hlt_addr addr, hlt_exception** excpt, hlt_execution_context* ctx) { return ! is_v4(addr); }