in activemq-cpp/src/main/decaf/internal/net/URIHelper.cpp [517:642]
bool URIHelper::isValidIP6Address( const std::string& ipAddress ) {
std::size_t length = ipAddress.length();
bool doubleColon = false;
int numberOfColons = 0;
int numberOfPeriods = 0;
std::string word = "";
char c = 0;
char prevChar = 0;
int offset = 0; // offset for [] ip addresses
if( length < 2 ) {
return false;
}
for( std::size_t i = 0; i < length; i++ ) {
prevChar = c;
c = ipAddress.at( i );
switch( c ) {
// case for an open bracket [x:x:x:...x]
case '[':
if( i != 0 ) {
return false; // must be first character
}
if( ipAddress.at( length - 1 ) != ']' ) {
return false; // must have a close ]
}
if( ( ipAddress.at( 1 ) == ':' ) && ( ipAddress.at( 2 )
!= ':' ) ) {
return false;
}
offset = 1;
if( length < 4 ) {
return false;
}
break;
// case for a closed bracket at end of IP [x:x:x:...x]
case ']':
if( i != length - 1 ) {
return false; // must be last character
}
if( ipAddress.at( 0 ) != '[' ) {
return false; // must have a open [
}
break;
// case for the last 32-bits represented as IPv4
// x:x:x:x:x:x:d.d.d.d
case '.':
numberOfPeriods++;
if( numberOfPeriods > 3 ) {
return false;
}
if( !isValidIP4Word( word ) ) {
return false;
}
if( numberOfColons != 6 && !doubleColon ) {
return false;
}
// a special case ::1:2:3:4:5:d.d.d.d allows 7 colons
// with
// an IPv4 ending, otherwise 7 :'s is bad
if( numberOfColons == 7 &&
ipAddress.at( 0 + offset ) != ':' &&
ipAddress.at( 1 + offset ) != ':' ) {
return false;
}
word = "";
break;
case ':':
numberOfColons++;
if( numberOfColons > 7 ) {
return false;
}
if( numberOfPeriods > 0 ) {
return false;
}
if( prevChar == ':' ) {
if( doubleColon ) {
return false;
}
doubleColon = true;
}
word = "";
break;
default:
if( word.length() > 3 ) {
return false;
}
if( !isValidHexChar( c ) ) {
return false;
}
word += c;
}
}
// Check if we have an IPv4 ending
if( numberOfPeriods > 0 ) {
if( numberOfPeriods != 3 || !isValidIP4Word( word ) ) {
return false;
}
} else {
// If we're at then end and we haven't had 7 colons then there
// is a problem unless we encountered a doubleColon
if( numberOfColons != 7 && !doubleColon ) {
return false;
}
// If we have an empty word at the end, it means we ended in
// either a : or a .
// If we did not end in :: then this is invalid
if( word == "" && ipAddress.at( length - 1 - offset ) != ':' &&
ipAddress.at( length - 2 - offset ) != ':' ) {
return false;
}
}
return true;
}