00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CABAL_ADDRESS_H
00015 #define CABAL_ADDRESS_H
00016
00017 #include <cabal_os.h>
00018 #include <cassert>
00019
00020 #define CABAL_MAX_ALIASES 5
00021
00022 namespace Cabal {
00023
00032 class Address
00033 {
00034 SockAddrInet m_address;
00035
00036 char m_ip_string[ CABAL_MAX_IP_SIZE ];
00037
00039 NetAddr m_ip_alias[ CABAL_MAX_ALIASES ];
00040
00042 std::string m_host;
00043
00044 public:
00045 Address()
00046 {
00047 Net::initSockAddr( m_address );
00048 m_ip_string[ 0 ] = '\0';
00049 m_ip_alias[ 0 ] = CABAL_INVALID_ADDR;
00050 }
00051
00052 Address( const std::string &c_address, const NetPort nPort = 0 )
00053 {
00054 set( c_address, nPort );
00055 }
00056
00057 Address( const char *c_address, const NetPort nPort = 0 )
00058 {
00059 set( c_address, nPort );
00060 }
00061
00062 Address( const Address &src )
00063 {
00064 copy( src );
00065 }
00066
00067 Address( const SockAddrInet &addr )
00068 {
00069 set( addr );
00070 }
00071
00072 void set( const std::string &str, const NetPort nPort = 0 )
00073 {
00074 set( str.c_str() );
00075 }
00076
00077 void set( const char *str, const NetPort nPort = 0 )
00078 {
00079 if ( Net::isAddressString(str) ) {
00080 strncpy( m_ip_string, str, CABAL_MAX_IP_SIZE );
00081 m_ip_string[ CABAL_MAX_IP_SIZE - 1 ] = '\0';
00082 Net::initSockAddr( m_address, str, nPort );
00083 }
00084 else {
00085 Net::initSockAddr( m_address );
00086 m_host = str;
00087 }
00088 m_ip_alias[0] = CABAL_INVALID_ADDR;
00089 }
00090
00091 void set( const SockAddrInet &addr )
00092 {
00093 m_address = addr;
00094 m_ip_string[ 0 ] = '\0';
00095 }
00096
00097 void copy( const Address &addr )
00098 {
00099 m_address = addr.m_address;
00100 memcpy( m_ip_string, addr.m_ip_string, sizeof( m_ip_string ) );
00101 m_host = addr.m_host;
00102 memcpy( m_ip_alias, addr.m_ip_alias, sizeof( m_ip_alias) );
00103 }
00104
00105 Address *clone()
00106 {
00107 return new Address( *this );
00108 }
00109
00110 const std::string &host() {
00111 if ( m_host == "" ) {
00112 m_host = ip();
00113 }
00114 return m_host;
00115 }
00116
00121 void host( const std::string &h ) {
00122 m_host = h;
00123 Net::initSockAddr( m_address );
00124 m_ip_string[ 0 ] = '\0';
00125 m_ip_alias[ 0 ] = CABAL_INVALID_ADDR;
00126 }
00127
00128
00129 const char *ip() {
00130 if ( m_ip_string[ 0 ] == '\0' ) {
00131 Net::addrToString( m_address, m_ip_string );
00132 }
00133 return m_ip_string;
00134 }
00135
00139 const SockAddrInet &address() const { return m_address; }
00140
00151 SockAddrInet &varAddress()
00152 {
00153 invalidate();
00154 return m_address;
00155 }
00156
00161 void invalidate() { m_ip_string[ 0 ] = '\0'; }
00162
00173 StringList *listOfIps() const;
00174
00175 inline const NetAddr *aliasIpList() const { return m_ip_alias; }
00176 inline int port() const { return Net::SockAddrGetPort( m_address ); }
00177 inline void port( const int nPort ) { Net::SockAddrSetPort( m_address, nPort ); }
00178
00179 bool isReady() const { return ! Net::SockAddrIsZero( m_address ); }
00180
00192 bool Address::scan()
00193 {
00194
00195 assert ( host() != "" );
00196 return Net::getHostList( m_host, m_host, m_ip_string, m_ip_alias, CABAL_MAX_ALIASES );
00197 }
00198
00202 bool Address::scan( const std::string &host ) {
00203 m_host = host;
00204 return scan();
00205 }
00206
00211 inline void operator=( const Address &orig ) { copy(orig); }
00212
00213
00214 inline bool operator==( const Address &a2 ) const
00215 {
00216 return Net::SockAddrIsSame( m_address, a2.m_address );
00217 }
00218
00219 inline bool operator!=( const Address &a2 ) const
00220 {
00221 return !Net::SockAddrIsSame( m_address, a2.m_address );
00222 }
00223
00224 };
00225
00226 }
00227
00228 #endif
00229
00230