version 1.1.2.2, 2007/06/12 09:31:58
|
version 1.1.2.5, 2007/06/18 14:13:07
|
|
|
| |
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
#ifdef PEGASUS_OS_VMS |
|
const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; |
|
#endif |
|
|
|
#if defined (PEGASUS_OS_TYPE_WINDOWS) || !defined (PEGASUS_ENABLE_IPV6) | #if defined (PEGASUS_OS_TYPE_WINDOWS) || !defined (PEGASUS_ENABLE_IPV6) |
| |
/* | /* |
|
|
return 1; | return 1; |
} | } |
| |
#ifdef PEGASUS_ENABLE_IPV6 |
|
/* | /* |
Converts given ipv6 text address (ex. ::1) to binary form and stroes | Converts given ipv6 text address (ex. ::1) to binary form and stroes |
in "dst" buffer (ex. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1). Returns 1 | in "dst" buffer (ex. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1). Returns 1 |
|
|
{ | { |
return 0; | return 0; |
} | } |
|
memset(dst, 0, PEGASUS_IN6_ADDR_SIZE); |
memset(dst, 0, sizeof(struct in6_addr)); |
|
|
|
for (int i = 0, j = 0; i < 8 ; ++i) | for (int i = 0, j = 0; i < 8 ; ++i) |
{ | { |
if (ccIndex == i) | if (ccIndex == i) |
|
|
} | } |
return 1; | return 1; |
} | } |
#endif |
|
| |
/* | /* |
Converts given ipv4 address in binary form to text form. Ex. 0x7f000001 | Converts given ipv4 address in binary form to text form. Ex. 0x7f000001 |
|
|
return dst; | return dst; |
} | } |
| |
#ifdef PEGASUS_ENABLE_IPV6 |
|
/* | /* |
Converts given ipv6 address in binary form to text form. Ex. | Converts given ipv6 address in binary form to text form. Ex. |
0000000000000001 to ::1. | 0000000000000001 to ::1. |
|
|
int zeroCnt = 0; | int zeroCnt = 0; |
int index = 0; | int index = 0; |
| |
memcpy (n, src, sizeof (struct in6_addr)); |
memcpy (n, src, PEGASUS_IN6_ADDR_SIZE); |
memset(dst, 0, size); | memset(dst, 0, size); |
for (int i = 0; i < 8 ; ++i) | for (int i = 0; i < 8 ; ++i) |
{ | { |
|
|
| |
return dst; | return dst; |
} | } |
#endif |
|
#endif // defined (PEGASUS_OS_TYPE_WINDOWS) || !defined (PEGASUS_ENABLE_IPV6) | #endif // defined (PEGASUS_OS_TYPE_WINDOWS) || !defined (PEGASUS_ENABLE_IPV6) |
| |
void HostAddress::_init() | void HostAddress::_init() |
{ | { |
_hostAddress = String::EMPTY; |
|
_hostAddrStr = String::EMPTY; | _hostAddrStr = String::EMPTY; |
_portNumber = PORT_UNSPECIFIED; |
|
_isValid = false; | _isValid = false; |
_addrType = AT_INVALID; | _addrType = AT_INVALID; |
} | } |
|
|
{ | { |
if (this != &rhs) | if (this != &rhs) |
{ | { |
_hostAddress = rhs._hostAddress; |
|
_hostAddrStr = rhs._hostAddrStr; | _hostAddrStr = rhs._hostAddrStr; |
_portNumber = rhs._portNumber; |
|
_isValid = rhs._isValid; | _isValid = rhs._isValid; |
_addrType = rhs._addrType; | _addrType = rhs._addrType; |
} | } |
|
|
_parseAddress(); | _parseAddress(); |
} | } |
| |
Uint32 HostAddress::getPort() |
|
{ |
|
return _portNumber; |
|
} |
|
|
|
String HostAddress::getPortString() |
|
{ |
|
char portStr[10]; |
|
sprintf(portStr, "%u", _portNumber); |
|
return String(portStr); |
|
} |
|
|
|
Uint32 HostAddress::getAddressType() | Uint32 HostAddress::getAddressType() |
{ | { |
return _addrType; | return _addrType; |
|
|
| |
String HostAddress::getHost() | String HostAddress::getHost() |
{ | { |
return _hostAddress; |
return _hostAddrStr; |
} |
|
|
|
Boolean HostAddress::isPortSpecified() |
|
{ |
|
return _portNumber != PORT_UNSPECIFIED && _portNumber != PORT_INVALID; |
|
} | } |
| |
Boolean HostAddress::equal(int af, void *p1, void *p2) | Boolean HostAddress::equal(int af, void *p1, void *p2) |
{ | { |
switch (af) | switch (af) |
{ | { |
#ifdef PEGASUS_ENABLE_IPV6 |
case AT_IPV6: |
case AF_INET6: |
return !memcmp(p1, p2, PEGASUS_IN6_ADDR_SIZE); |
return !memcmp(p1, p2, sizeof(struct in6_addr)); |
case AT_IPV4: |
#endif |
|
case AF_INET: |
|
return !memcmp(p1, p2, sizeof(struct in_addr)); | return !memcmp(p1, p2, sizeof(struct in_addr)); |
} | } |
| |
|
|
{ | { |
if (_hostAddrStr == String::EMPTY) | if (_hostAddrStr == String::EMPTY) |
{ | { |
_isValid = false; |
|
return; | return; |
} | } |
Uint32 i = 0; |
if ((_isValid = isValidIPV6Address(_hostAddrStr))) |
#ifdef PEGASUS_ENABLE_IPV6 |
|
if (_hostAddrStr[0] == '[') |
|
{ |
|
i = 1; |
|
while (_hostAddrStr[i] && _hostAddrStr[i] != ']') |
|
{ |
|
++i; |
|
} |
|
if (_hostAddrStr[i] == ']') |
|
{ |
|
_hostAddress = _hostAddrStr.subString (1, i - 1); |
|
if( !(_isValid = isValidIPV6Address(_hostAddress))) |
|
{ |
|
return; |
|
} |
|
_addrType = AT_IPV6; |
|
++i; |
|
} |
|
else |
|
{ |
|
_hostAddress = _hostAddrStr; |
|
return; |
|
} |
|
} |
|
else if ((_isValid = isValidIPV6Address(_hostAddrStr))) |
|
{ | { |
_addrType = AT_IPV6; | _addrType = AT_IPV6; |
_hostAddress = _hostAddrStr; |
|
return; |
|
} | } |
else |
else if( (_isValid = isValidIPV4Address(_hostAddrStr))) |
#endif |
|
if (!_isValid) |
|
{ |
|
while (_hostAddrStr[i] && _hostAddrStr[i] != ':') |
|
{ |
|
++i; |
|
} |
|
_hostAddress = _hostAddrStr.subString(0, i); |
|
if( !(_isValid = isValidIPV4Address(_hostAddress))) |
|
{ |
|
// Not a valid IPV4 address, check for valid hostName |
|
if(!(_isValid = isValidHostName(_hostAddress))) |
|
{ |
|
return; |
|
} |
|
_addrType = AT_HOSTNAME; |
|
} |
|
else |
|
{ | { |
_addrType = AT_IPV4; | _addrType = AT_IPV4; |
} | } |
} |
else if((_isValid = isValidHostName(_hostAddrStr))) |
// Check for Port number. |
|
Uint32 port = 0; |
|
if (_hostAddrStr[i] == ':') |
|
{ |
|
++i; |
|
while (isascii(_hostAddrStr[i]) && isdigit(_hostAddrStr[i])) |
|
{ | { |
port = port * 10 + ( _hostAddrStr[i] - '0' ); |
_addrType = AT_HOSTNAME; |
if (port > MAX_PORT_NUMBER) |
|
{ |
|
_portNumber = PORT_INVALID; |
|
_isValid = false; |
|
return; |
|
} | } |
++i; |
|
} | } |
| |
if (_hostAddrStr[i-1] != ':' && _hostAddrStr[i] == char(0)) |
Boolean HostAddress::isValidIPV6Address (const String &ipv6Address) |
{ | { |
_portNumber = port; |
int i = 0; |
} |
while (ipv6Address[i]) |
else |
|
{ | { |
_portNumber = PORT_INVALID; |
if (!isascii(ipv6Address[i++])) |
_isValid = false; |
{ |
} |
return false; |
} | } |
} | } |
| |
#ifdef PEGASUS_ENABLE_IPV6 |
|
Boolean HostAddress::isValidIPV6Address (const String &ipv6Address) |
|
{ |
|
CString addr = ipv6Address.getCString(); | CString addr = ipv6Address.getCString(); |
|
#ifdef PEGASUS_ENABLE_IPV6 |
struct in6_addr iaddr; | struct in6_addr iaddr; |
|
#else |
return 1 == convertTextToBinary(AF_INET6, (const char*) addr, |
char iaddr[PEGASUS_IN6_ADDR_SIZE]; |
|
#endif |
|
return 1 == convertTextToBinary(AT_IPV6, (const char*) addr, |
(void*)&iaddr); | (void*)&iaddr); |
} | } |
#endif |
|
| |
Boolean HostAddress::isValidIPV4Address (const String &ipv4Address) | Boolean HostAddress::isValidIPV4Address (const String &ipv4Address) |
{ | { |
|
int i = 0; |
|
while (ipv4Address[i]) |
|
{ |
|
if (!isascii(ipv4Address[i++])) |
|
{ |
|
return false; |
|
} |
|
} |
|
|
CString addr = ipv4Address.getCString(); | CString addr = ipv4Address.getCString(); |
struct in_addr iaddr; | struct in_addr iaddr; |
| |
return 1 == convertTextToBinary(AF_INET, (const char*) addr, |
return 1 == convertTextToBinary(AT_IPV4, (const char*) addr, |
(void*)&iaddr); | (void*)&iaddr); |
} | } |
| |
|
|
| |
int HostAddress::convertTextToBinary(int af, const char *src, void *dst) | int HostAddress::convertTextToBinary(int af, const char *src, void *dst) |
{ | { |
#ifndef PEGASUS_ENABLE_IPV6 |
#if defined (PEGASUS_OS_TYPE_WINDOWS) || !defined (PEGASUS_ENABLE_IPV6) |
if (af == AF_INET) |
if (af == AT_IPV4) |
{ |
|
return _inet_ptonv4(src, dst); |
|
} |
|
return -1; |
|
#else |
|
#if defined (PEGASUS_OS_TYPE_UNIX) || defined (PEGASUS_OS_VMS) |
|
return ::inet_pton(af, src, dst); |
|
#else |
|
if (af == AF_INET) |
|
{ | { |
return _inet_ptonv4(src, dst); | return _inet_ptonv4(src, dst); |
} | } |
else if(af == AF_INET6) |
else if(af == AT_IPV6) |
{ | { |
return _inet_ptonv6(src, dst); | return _inet_ptonv6(src, dst); |
} | } |
else |
|
{ |
|
return -1; // Unsupported address family. | return -1; // Unsupported address family. |
} |
#else |
#endif // PEGASUS_OS_TYPE_UNIX |
return ::inet_pton(af, src, dst); |
#endif // PEGASUS_ENABLE_IPV6 |
#endif |
} | } |
| |
const char * HostAddress::convertBinaryToText(int af, const void *src, | const char * HostAddress::convertBinaryToText(int af, const void *src, |
char *dst, Uint32 size) | char *dst, Uint32 size) |
{ | { |
#ifndef PEGASUS_ENABLE_IPV6 |
#if defined (PEGASUS_OS_TYPE_WINDOWS) || !defined (PEGASUS_ENABLE_IPV6) |
if (af == AF_INET) |
if (af == AT_IPV6) |
{ | { |
return _inet_ntopv4(src, dst, size); |
return _inet_ntopv6(src, dst, size); |
} | } |
return 0; |
else if (af == AT_IPV4) |
#else |
|
#if defined (PEGASUS_OS_TYPE_UNIX) || defined (PEGASUS_OS_VMS) |
|
return ::inet_ntop(af, src, dst, size); |
|
#else |
|
if (af == AF_INET) |
|
{ | { |
return _inet_ntopv4(src, dst, size); | return _inet_ntopv4(src, dst, size); |
} | } |
else if (af == AF_INET6) |
|
{ |
|
return _inet_ntopv6(src, dst, size); |
|
} |
|
else |
|
{ |
|
return 0; // Unsupported address family. | return 0; // Unsupported address family. |
} |
#else |
#endif // PEGASUS_OS_TYPE_UNIX |
return ::inet_ntop(af, src, dst, size); |
#endif // PEGASUS_ENABLE_IPV6 |
#endif |
} | } |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |