version 1.35, 2005/11/29 20:06:14
|
version 1.36, 2006/01/27 19:21:20
|
|
|
| |
String System::getHostIP(const String &hostName) | String System::getHostIP(const String &hostName) |
{ | { |
struct hostent * phostent; |
struct hostent* hostEntry; |
struct in_addr inaddr; | struct in_addr inaddr; |
String ipAddress = String::EMPTY; |
String ipAddress; |
CString csName = hostName.getCString(); |
CString hostNameCString = hostName.getCString(); |
const char * ccName = csName; |
const char* hostNamePtr = hostNameCString; |
#ifndef PEGASUS_OS_OS400 |
|
if ((phostent = ::gethostbyname(ccName)) != NULL) |
#if defined(PEGASUS_OS_LINUX) |
#else |
char hostEntryBuffer[8192]; |
|
struct hostent hostEntryStruct; |
|
int hostEntryErrno; |
|
|
|
gethostbyname_r( |
|
hostNamePtr, |
|
&hostEntryStruct, |
|
hostEntryBuffer, |
|
sizeof(hostEntryBuffer), |
|
&hostEntry, |
|
&hostEntryErrno); |
|
#elif defined(PEGASUS_OS_SOLARIS) |
|
char hostEntryBuffer[8192]; |
|
struct hostent hostEntryStruct; |
|
int hostEntryErrno; |
|
|
|
hostEntry = gethostbyname_r( |
|
(char *)hostNamePtr, |
|
&hostEntryStruct, |
|
hostEntryBuffer, |
|
sizeof(hostEntryBuffer), |
|
&hostEntryErrno); |
|
#elif defined(PEGASUS_OS_OS400) |
char ebcdicHost[PEGASUS_MAXHOSTNAMELEN]; | char ebcdicHost[PEGASUS_MAXHOSTNAMELEN]; |
if (strlen(ccName) < PEGASUS_MAXHOSTNAMELEN) |
if (strlen(hostNamePtr) < PEGASUS_MAXHOSTNAMELEN) |
strcpy(ebcdicHost, ccName); |
strcpy(ebcdicHost, hostNamePtr); |
else | else |
return ipAddress; | return ipAddress; |
AtoE(ebcdicHost); | AtoE(ebcdicHost); |
if ((phostent = ::gethostbyname(ebcdicHost)) != NULL) |
hostEntry = gethostbyname(ebcdicHost); |
|
#else |
|
hostEntry = gethostbyname(hostNamePtr); |
#endif | #endif |
|
|
|
if (hostEntry) |
{ | { |
::memcpy( &inaddr, phostent->h_addr,4); |
::memcpy( &inaddr, hostEntry->h_addr,4); |
#if defined(PEGASUS_OS_OS400) | #if defined(PEGASUS_OS_OS400) |
char * gottenIPAdress = NULL; | char * gottenIPAdress = NULL; |
gottenIPAdress = ::inet_ntoa( inaddr ); | gottenIPAdress = ::inet_ntoa( inaddr ); |
|
|
Uint32 tmp_addr = inet_addr((char *) hostname); | Uint32 tmp_addr = inet_addr((char *) hostname); |
#endif | #endif |
| |
struct hostent *entry; |
struct hostent* hostEntry; |
| |
// Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255). | // Note: 0xFFFFFFFF is actually a valid IP address (255.255.255.255). |
// A better solution would be to use inet_aton() or equivalent, as | // A better solution would be to use inet_aton() or equivalent, as |
|
|
| |
if (tmp_addr == 0xFFFFFFFF) // if hostname is not an IP address | if (tmp_addr == 0xFFFFFFFF) // if hostname is not an IP address |
{ | { |
#ifdef PEGASUS_PLATFORM_SOLARIS_SPARC_CC |
#if defined(PEGASUS_OS_LINUX) |
#define HOSTENT_BUFF_SIZE 8192 |
char hostEntryBuffer[8192]; |
char buf[HOSTENT_BUFF_SIZE]; |
struct hostent hostEntryStruct; |
int h_errorp; |
int hostEntryErrno; |
struct hostent hp; |
|
|
gethostbyname_r( |
entry = gethostbyname_r((char *)hostname, &hp, buf, |
hostname, |
HOSTENT_BUFF_SIZE, &h_errorp); |
&hostEntryStruct, |
|
hostEntryBuffer, |
|
sizeof(hostEntryBuffer), |
|
&hostEntry, |
|
&hostEntryErrno); |
|
#elif defined(PEGASUS_OS_SOLARIS) |
|
char hostEntryBuffer[8192]; |
|
struct hostent hostEntryStruct; |
|
int hostEntryErrno; |
|
|
|
hostEntry = gethostbyname_r( |
|
(char *)hostname, |
|
&hostEntryStruct, |
|
hostEntryBuffer, |
|
sizeof(hostEntryBuffer), |
|
&hostEntryErrno); |
#elif defined(PEGASUS_OS_OS400) | #elif defined(PEGASUS_OS_OS400) |
entry = gethostbyname(ebcdicHost); |
hostEntry = gethostbyname(ebcdicHost); |
#elif defined(PEGASUS_OS_ZOS) | #elif defined(PEGASUS_OS_ZOS) |
char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; | char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; |
if (String::equalNoCase("localhost",String(hostname))) | if (String::equalNoCase("localhost",String(hostname))) |
{ | { |
gethostname( hostName, PEGASUS_MAXHOSTNAMELEN ); | gethostname( hostName, PEGASUS_MAXHOSTNAMELEN ); |
hostName[sizeof(hostName)-1] = 0; | hostName[sizeof(hostName)-1] = 0; |
entry = gethostbyname(hostName); |
hostEntry = gethostbyname(hostName); |
} else | } else |
{ | { |
entry = gethostbyname((char *)hostname); |
hostEntry = gethostbyname((char *)hostname); |
} | } |
#else | #else |
entry = gethostbyname((char *)hostname); |
hostEntry = gethostbyname((char *)hostname); |
#endif | #endif |
if (!entry) |
if (!hostEntry) |
{ | { |
return 0xFFFFFFFF; | return 0xFFFFFFFF; |
} | } |
unsigned char ip_part1,ip_part2,ip_part3,ip_part4; | unsigned char ip_part1,ip_part2,ip_part3,ip_part4; |
| |
ip_part1 = entry->h_addr[0]; |
ip_part1 = hostEntry->h_addr[0]; |
ip_part2 = entry->h_addr[1]; |
ip_part2 = hostEntry->h_addr[1]; |
ip_part3 = entry->h_addr[2]; |
ip_part3 = hostEntry->h_addr[2]; |
ip_part4 = entry->h_addr[3]; |
ip_part4 = hostEntry->h_addr[3]; |
ip = ip_part1; | ip = ip_part1; |
ip = (ip << 8) + ip_part2; | ip = (ip << 8) + ip_part2; |
ip = (ip << 8) + ip_part3; | ip = (ip << 8) + ip_part3; |
|
|
// resolve hostaddr to a real host entry | // resolve hostaddr to a real host entry |
// casting to (const char *) as (char *) will work as (void *) too, those it fits all platforms | // casting to (const char *) as (char *) will work as (void *) too, those it fits all platforms |
#ifndef PEGASUS_OS_OS400 | #ifndef PEGASUS_OS_OS400 |
entry = gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET); |
hostEntry = gethostbyaddr((const char *) &tmp_addr, sizeof(tmp_addr), AF_INET); |
#else | #else |
entry = gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET); |
hostEntry = gethostbyaddr((char *) &tmp_addr, sizeof(tmp_addr), AF_INET); |
#endif | #endif |
if (entry == 0) |
if (hostEntry == 0) |
{ | { |
// error, couldn't resolve the ip | // error, couldn't resolve the ip |
return 0xFFFFFFFF; | return 0xFFFFFFFF; |
|
|
| |
unsigned char ip_part1,ip_part2,ip_part3,ip_part4; | unsigned char ip_part1,ip_part2,ip_part3,ip_part4; |
| |
ip_part1 = entry->h_addr[0]; |
ip_part1 = hostEntry->h_addr[0]; |
ip_part2 = entry->h_addr[1]; |
ip_part2 = hostEntry->h_addr[1]; |
ip_part3 = entry->h_addr[2]; |
ip_part3 = hostEntry->h_addr[2]; |
ip_part4 = entry->h_addr[3]; |
ip_part4 = hostEntry->h_addr[3]; |
ip = ip_part1; | ip = ip_part1; |
ip = (ip << 8) + ip_part2; | ip = (ip << 8) + ip_part2; |
ip = (ip << 8) + ip_part3; | ip = (ip << 8) + ip_part3; |