version 1.61, 2007/09/12 22:28:53
|
version 1.68, 2008/03/07 19:17:34
|
|
|
return dirname; | return dirname; |
} | } |
| |
|
String System::getHostName() |
|
{ |
|
static String _hostname; |
|
static MutexType _mutex = PEGASUS_MUTEX_INITIALIZER; |
|
|
|
// Use double-checked locking pattern to avoid overhead of |
|
// mutex on subsequent calls. |
|
|
|
if (0 == _hostname.size()) |
|
{ |
|
mutex_lock(&_mutex); |
|
|
|
if (0 == _hostname.size()) |
|
{ |
|
char hostname[PEGASUS_MAXHOSTNAMELEN + 1]; |
|
// If gethostname() fails, an empty or truncated value is used. |
|
hostname[0] = 0; |
|
gethostname(hostname, sizeof(hostname)); |
|
hostname[sizeof(hostname)-1] = 0; |
|
_hostname.assign(hostname); |
|
} |
|
|
|
mutex_unlock(&_mutex); |
|
} |
|
|
|
return _hostname; |
|
} |
|
|
|
static String _getFullyQualifiedHostName() |
|
{ |
|
char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; |
|
|
|
// Get the short name of the local host. |
|
// If gethostname() fails, an empty or truncated value is used. |
|
hostName[0] = 0; |
|
gethostname(hostName, sizeof(hostName)); |
|
hostName[sizeof(hostName)-1] = 0; |
|
|
|
#if defined(PEGASUS_OS_ZOS)|| \ |
|
defined(PEGASUS_OS_VMS) |
|
|
|
String fqName; |
|
struct addrinfo *resolv; |
|
struct addrinfo hint; |
|
struct hostent *he; |
|
|
|
memset (&hint, 0, sizeof(struct addrinfo)); |
|
hint.ai_flags = AI_CANONNAME; |
|
hint.ai_family = AF_UNSPEC; // any family |
|
hint.ai_socktype = 0; // any socket type |
|
hint.ai_protocol = 0; // any protocol |
|
int success = System::getAddrInfo(hostName, NULL, &hint, &resolv); |
|
if (success==0) |
|
{ |
|
// assign fully qualified hostname |
|
fqName.assign(resolv->ai_canonname); |
|
} |
|
else |
|
{ |
|
if ((he = System::getHostByName(hostName))) |
|
{ |
|
strncpy(hostName, he->h_name, sizeof(hostName) - 1); |
|
} |
|
// assign hostName |
|
// if gethostbyname was successful assign that result |
|
// else assign unqualified hostname |
|
fqName.assign(hostName); |
|
} |
|
if (resolv) |
|
{ |
|
freeaddrinfo(resolv); |
|
} |
|
|
|
return fqName; |
|
|
|
#else /* !PEGASUS_OS_ZOS && !PEGASUS_OS_VMS */ |
|
|
|
char hostEntryBuffer[8192]; |
|
struct hostent hostEntryStruct; |
|
struct hostent* hostEntry = System::getHostByName( |
|
hostName, &hostEntryStruct, hostEntryBuffer, sizeof (hostEntryBuffer)); |
|
|
|
if (hostEntry) |
|
{ |
|
strncpy(hostName, hostEntry->h_name, sizeof(hostName) - 1); |
|
} |
|
return String(hostName); |
|
|
|
#endif |
|
} |
|
|
|
String System::getFullyQualifiedHostName() |
|
{ |
|
static String _hostname; |
|
static MutexType _mutex = PEGASUS_MUTEX_INITIALIZER; |
|
|
|
// Use double-checked locking pattern to avoid overhead of |
|
// mutex on subsequent calls. |
|
|
|
if (0 == _hostname.size()) |
|
{ |
|
mutex_lock(&_mutex); |
|
|
|
if (0 == _hostname.size()) |
|
{ |
|
try |
|
{ |
|
_hostname = _getFullyQualifiedHostName(); |
|
} |
|
catch (...) |
|
{ |
|
mutex_unlock(&_mutex); |
|
throw; |
|
} |
|
} |
|
|
|
mutex_unlock(&_mutex); |
|
} |
|
|
|
return _hostname; |
|
} |
|
|
Boolean System::getHostIP(const String &hostName, int *af, String &hostIP) | Boolean System::getHostIP(const String &hostName, int *af, String &hostIP) |
{ | { |
#ifdef PEGASUS_ENABLE_IPV6 | #ifdef PEGASUS_ENABLE_IPV6 |
|
|
} | } |
else | else |
{ | { |
|
#if defined(PEGASUS_OS_VMS) |
|
// vms has tcpipv6 support in the kernel so socket |
|
// will always work so a call to "bind" is needed |
|
// to complete this test. |
|
|
|
struct sockaddr_storage listenAddress; |
|
memset(&listenAddress, 0, sizeof (listenAddress)); |
|
SocketLength addressLength; |
|
|
|
HostAddress::convertTextToBinary( |
|
HostAddress::AT_IPV6, |
|
"::1", |
|
&reinterpret_cast<struct sockaddr_in6*>(&listenAddress)->sin6_addr); |
|
listenAddress.ss_family = AF_INET6; |
|
reinterpret_cast<struct sockaddr_in6*>(&listenAddress)->sin6_port = 0; |
|
|
|
addressLength = sizeof(struct sockaddr_in6); |
|
|
|
if (::bind( |
|
ip6Socket, |
|
reinterpret_cast<struct sockaddr*>(&listenAddress), |
|
addressLength) < 0) |
|
{ |
|
Socket::close(ip6Socket); |
|
return false; |
|
} |
|
#endif // PEGASUS_OS_VMS |
|
|
Socket::close(ip6Socket); | Socket::close(ip6Socket); |
} | } |
| |
|
|
// ------------------------------------------------------------------------ | // ------------------------------------------------------------------------ |
// Convert a hostname into a a single host unique integer representation | // Convert a hostname into a a single host unique integer representation |
// ------------------------------------------------------------------------ | // ------------------------------------------------------------------------ |
Boolean System::_acquireIP(const char* hostname, int *af, void *dst) |
Boolean System::acquireIP(const char* hostname, int *af, void *dst) |
{ | { |
#ifdef PEGASUS_ENABLE_IPV6 | #ifdef PEGASUS_ENABLE_IPV6 |
String ipAddress; | String ipAddress; |
|
|
#else | #else |
*af = AF_INET; | *af = AF_INET; |
Uint32 ip = 0xFFFFFFFF; | Uint32 ip = 0xFFFFFFFF; |
if (!hostname) return 0xFFFFFFFF; |
if (!hostname) |
|
{ |
|
*af = 0xFFFFFFFF; |
|
return false; |
|
} |
| |
//////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////// |
// This code used to check if the first character of "hostname" was alphabetic | // This code used to check if the first character of "hostname" was alphabetic |
|
|
} | } |
res1 = res1->ai_next; | res1 = res1->ai_next; |
} | } |
|
if (res1root) |
|
{ |
freeaddrinfo(res1root); | freeaddrinfo(res1root); |
|
} |
|
if (res2root) |
|
{ |
freeaddrinfo(res2root); | freeaddrinfo(res2root); |
|
} |
if (isLocal) | if (isLocal) |
{ | { |
return true; | return true; |
|
|
} | } |
res1 = res1->ai_next; | res1 = res1->ai_next; |
} | } |
|
if (res1root) |
|
{ |
freeaddrinfo(res1root); | freeaddrinfo(res1root); |
|
} |
|
if (res2root) |
|
{ |
freeaddrinfo(res2root); | freeaddrinfo(res2root); |
|
} |
return isLocal; | return isLocal; |
#else | #else |
| |
|
|
int rc = 0; | int rc = 0; |
unsigned int maxTries = 5; | unsigned int maxTries = 5; |
| |
|
#ifdef PEGASUS_OS_PASE |
|
CString hostNameCString; |
|
if (String::equalNoCase(hostname, "localhost")) |
|
{ |
|
hostNameCString = getHostName().getCString(); |
|
hostname = (const char*)hostNameCString; |
|
} |
|
#endif |
|
|
while ((rc = getaddrinfo(hostname, | while ((rc = getaddrinfo(hostname, |
servname, | servname, |
hints, | hints, |