Example #1
0
NETWORK_API void NetworkUtility::GetIPAndPortFromString( const rString& input, rString& outIP, unsigned short& outPort )
{
	if ( input == "" )
		return;

	outIP	= "";
	outPort = INVALID_PORT;

	size_t colonCount = std::count( input.begin(), input.end(), ':' );

	if ( colonCount == 1 )
	{
		outIP = GetIPFromString( input );
		size_t colonPosition = input.find( ':' );
		rString portString = input.substr( colonPosition + 1 );
		if ( StringUtility::IsDigitsOnly( portString ) )
		{
			outPort = GetPortFromString( portString );
		}
	}
	else if ( StringUtility::IsDigitsOnly( input ) )
	{
		outPort = GetPortFromString( input );
	}
	else
	{
		outIP = GetIPFromString( input );
	}
}
Example #2
0
rString NetworkUtility::GetIPFromString( const rString& input )
{
	if ( input.find_first_not_of( ".0123456789:" ) != std::string::npos ) // Check if DNS
	{
		return GetIPFromDNS( input );
	}
	size_t dotCount = std::count( input.begin(), input.end(), '.' );
	if ( dotCount != DOTS_IN_IPv4ADDRESS )
		return "";

	unsigned int adressParts[4];
	rString portString;
	rString processString = input;
	for ( int i = 0; i < 4; ++i )
	{
		size_t pos = processString.find_first_of( ".:" );
		rString currentAdressPart = processString.substr( 0, pos );
		try
		{
			adressParts[i] = static_cast<unsigned int>( std::stoul( currentAdressPart.c_str() ) );
		} 
		catch ( ... )
		{
			return "";
		}
		if ( adressParts[i] >= 256 )
		{
			return "";
		}
		processString.erase( 0, pos + 1 );
	}

	rOStringStream iposs;
	iposs << adressParts[0] << "." << adressParts[1] << "." << adressParts[2] << "." << adressParts[3];
	return iposs.str();
}