4
* 2001-December-14 Jason Rohrer
6
* Has not been compiled or tested under windows.
8
* 2002-March-25 Jason Rohrer
9
* Added function for conversion to numerical addresses.
11
* 2002-March-26 Jason Rohrer
12
* Changed to use strdup.
13
* Fixed problem on some platforms where local address does
14
* not include domain name.
15
* Fixed so that numerical address is actually retrieved by
16
* getNumericalAddress().
18
* 2002-April-6 Jason Rohrer
19
* Replaced use of strdup.
21
* 2002-April-7 Jason Rohrer
22
* Changed to return numerical address from getLocalAddress,
23
* since getdomainname() call not available on Win32.
25
* 2002-April-8 Jason Rohrer
26
* Changed to init socket framework before getting local address.
27
* Changed to check if address is numerical before
28
* performing a lookup on it.
30
* 2004-January-4 Jason Rohrer
31
* Added use of network function locks.
33
* 2008-November-2 Jason Rohrer
34
* Added framework init in getNumericalAddress.
39
#include "minorGems/network/HostAddress.h"
40
#include "minorGems/network/NetworkFunctionLocks.h"
41
#include "minorGems/util/stringUtils.h"
49
#include "minorGems/network/Socket.h"
53
HostAddress *HostAddress::getLocalAddress() {
55
// first make sure sockets are initialized
56
if( !Socket::isFrameworkInitialized() ) {
58
// try to init the framework
60
int error = Socket::initSocketFramework();
64
printf( "initializing network socket framework failed\n" );
72
int bufferLength = 200;
73
char *buffer = new char[ bufferLength ];
75
gethostname( buffer, bufferLength );
77
char *name = stringDuplicate( buffer );
82
// this class will destroy name for us
83
HostAddress *nameAddress = new HostAddress( name, 0 );
85
HostAddress *fullAddress = nameAddress->getNumericalAddress();
95
HostAddress *HostAddress::getNumericalAddress() {
97
// make sure we're not already numerical
98
if( this->isNumerical() ) {
103
if( !Socket::isFrameworkInitialized() ) {
105
// try to init the framework
107
int error = Socket::initSocketFramework();
111
printf( "initializing network socket framework failed\n" );
116
// need to use two function locks here
118
// first, lock for gethostbyname
119
NetworkFunctionLocks::mGetHostByNameLock.lock();
120
struct hostent *host = gethostbyname( mAddressString );
124
// this line adapted from the
126
struct in_addr *inetAddress = (struct in_addr *) *host->h_addr_list;
129
// now lock for inet_ntoa
130
NetworkFunctionLocks::mInet_ntoaLock.lock();
131
// the returned string is statically allocated, so copy it
132
char *inetAddressString = stringDuplicate( inet_ntoa( *inetAddress ) );
134
NetworkFunctionLocks::mInet_ntoaLock.unlock();
137
// done with returned hostent now, so unlock first lock
138
NetworkFunctionLocks::mGetHostByNameLock.unlock();
140
return new HostAddress( inetAddressString, mPort );
144
// unlock first lock before returning
145
NetworkFunctionLocks::mGetHostByNameLock.unlock();
154
char HostAddress::isNumerical() {
156
unsigned long returnedValue = inet_addr( mAddressString );
159
if( returnedValue == INADDR_NONE ) {