version 1.34, 2005/11/15 14:40:29
|
version 1.37, 2006/01/30 16:17:08
|
|
|
//%2005//////////////////////////////////////////////////////////////////////// |
//%2006//////////////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development | // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development |
// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. | // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. |
|
|
// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. | // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. |
// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; | // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// EMC Corporation; VERITAS Software Corporation; The Open Group. | // EMC Corporation; VERITAS Software Corporation; The Open Group. |
|
// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
|
// EMC Corporation; Symantec Corporation; The Open Group. |
// | // |
// Permission is hereby granted, free of charge, to any person obtaining a copy | // Permission is hereby granted, free of charge, to any person obtaining a copy |
// of this software and associated documentation files (the "Software"), to | // of this software and associated documentation files (the "Software"), to |
|
|
// Robert Kieninger, IBM (kieningr@de.ibm.com) for Bug#667 | // Robert Kieninger, IBM (kieningr@de.ibm.com) for Bug#667 |
// Dave Sudlik, IBM (dsudlik@us.ibm.com) for Bug#1462 | // Dave Sudlik, IBM (dsudlik@us.ibm.com) for Bug#1462 |
// Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com) | // Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com) |
|
// David Dillard, Symantec Corp. (david_dillard@symantec.com) |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
|
#include "System.h" | #include "System.h" |
#include "Socket.h" | #include "Socket.h" |
| |
#ifdef PEGASUS_PLATFORM_WIN32_IX86_MSVC |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
# include <windows.h> | # include <windows.h> |
#else | #else |
# include <arpa/inet.h> | # include <arpa/inet.h> |
|
|
| |
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; |