00001 00014 // IpAddress.cpp -- implementation for the IpAddress class 00015 00016 #include "IpAddress.h" 00017 #include "Host.h" 00018 #include "NetworkEx.h" 00019 00020 00022 // class IpAddress 00023 00024 // static variable for local IpAddress 00025 std::auto_ptr<IpAddress> IpAddress::localIpAddress; 00026 00027 // Constructor for address assignment 00028 IpAddress::IpAddress( const std::string& address ) 00029 { 00030 char first = address[ 0 ]; 00031 if ( first >= '0' && first <= '9' ) 00032 set( address ); 00033 else 00034 setHostName( address ); 00035 } 00036 00037 // Construct from encoded IpAddress 00038 IpAddress::IpAddress( unsigned long code ) 00039 { 00040 set( code ); 00041 } 00042 00043 00044 // Construct from the Berkley Sockets in_addr structure 00045 IpAddress::IpAddress( const in_addr& address ) : ipAddress( address ) 00046 { 00047 } 00048 00049 00050 // Copy constructor 00051 IpAddress::IpAddress( const IpAddress& address ) : ipAddress( address.ipAddress ) 00052 { 00053 } 00054 00055 // Standard destructor. 00056 IpAddress::~IpAddress() 00057 { 00058 } 00059 00060 // Standard c++ iostream inserter 00061 std::ostream& IpAddress::operator<<( std::ostream& stream) const 00062 { 00063 stream << "IpAddress( "; 00064 stream << ( toLong() == INADDR_ANY ? "ANY" : ::inet_ntoa( ipAddress ) ); 00065 stream << " )"; 00066 return stream; 00067 } 00068 00069 // Comparison of IpAddress 00070 bool IpAddress::operator==( const IpAddress& address ) const 00071 { 00072 return toLong() == address.toLong(); 00073 } 00074 00075 // Comparison of IpAddress 00076 bool IpAddress::operator<( const IpAddress& address ) const 00077 { 00078 return toLong() < address.toLong(); 00079 } 00080 00081 // Assignment operator from Berkley Socket structure 00082 IpAddress& IpAddress::operator=(const in_addr& address) 00083 { 00084 ipAddress = address; 00085 return *this; 00086 } 00087 00088 // Assignment operator 00089 IpAddress& IpAddress::operator=( const IpAddress& address ) 00090 { 00091 ipAddress = address.ipAddress; 00092 return *this; 00093 } 00094 00095 // Verify whether IpAddress is the local host 00096 bool IpAddress::isLocal() const 00097 { 00098 return *this == getLocalIpAddress(); 00099 } 00100 00101 // Conversion to the Berkely Sockets in_addr structure 00102 IpAddress::operator in_addr() const 00103 { 00104 return ipAddress; 00105 } 00106 00107 // Validity check 00108 bool IpAddress::isDefined() const 00109 { 00110 return ipAddress.s_addr != 0; 00111 } 00112 00113 // Accessor to set the host name 00114 void IpAddress::setHostName( const std::string& hostName ) 00115 { 00116 Host host( hostName ); 00117 if ( host.isDefined() ) 00118 *this = host.getIpAddresses().front(); 00119 } 00120 00121 // Accessor to set the IpAddress 00122 void IpAddress::set( const std::string& string ) 00123 { 00124 long addr = ::inet_addr(string.c_str()); 00125 if ( addr == -1 ) 00126 NetworkException::throwNetworkException(NetworkException::invalid_address,string.c_str()); 00127 00128 ipAddress.s_addr = addr; 00129 } 00130 00131 // Static function to return IpAddress for the local host 00132 const IpAddress& IpAddress::getLocalIpAddress() 00133 { 00134 if ( localIpAddress.get() == 0 ) { 00135 try { 00136 localIpAddress = std::auto_ptr<IpAddress>(new IpAddress()); 00137 localIpAddress->set( Host::getLocalhost().getName() ); 00138 } 00139 catch ( std::exception& ) 00140 { 00141 // Only occurs with a bad machine configuration 00142 } 00143 } 00144 00145 return *(localIpAddress.get()); 00146 } 00147 00148 // Accessor to set the IpAddress 00149 void IpAddress::set( unsigned long code ) throw() 00150 { 00151 ipAddress.s_addr = htonl( code ); 00152 } 00153 00154 00155 // Conversion of IpAddress to a long 00156 unsigned long IpAddress::toLong() const throw() 00157 { 00158 return ntohl( ipAddress.s_addr ); 00159 }