00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : IPAddress 00006 * Package : Zeus.ZeusBase.Net 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef IPAddressH 00033 #define IPAddressH 00034 00035 #include <zeusbase/System/ZObject.h> 00036 #include <zeusbase/System/ByteArray.hpp> 00037 00038 #if defined(ENABLE_LINUX_BINDING) 00039 #include <netinet/in.h> 00040 #include <arpa/inet.h> 00041 #elif defined(ENABLE_WIN32_BINDING) 00042 00043 #ifdef USE_WINDOWS_H 00044 #include <windows.h> 00045 #endif 00046 00047 #if defined(ENABLE_IPV6_BINDING) 00048 #include <winsock2.h> 00049 #else 00050 #include <winsock.h> 00051 #endif 00052 00053 #endif 00054 00055 BEGIN_NAMESPACE_Zeus 00056 00057 /***************************************************************************/ 00061 /***************************************************************************/ 00062 zeusbase_class TIPAddress : public TZObject 00063 { 00064 public: 00065 TIPAddress(); 00066 TIPAddress(const IString& rHostAddress, Uint uiPort); 00067 00068 virtual ~TIPAddress(); 00069 00070 virtual Uint getPort() const; 00071 virtual TString getHostAddress() const; 00072 virtual bool isEqual(const TIPAddress& rAddress) const; 00073 00074 /************************************************************************/ 00078 /************************************************************************/ 00079 virtual TIPAddress* createNewAddress() = 0; 00080 00081 /************************************************************************/ 00084 /************************************************************************/ 00085 virtual TString getHostName() const=0; 00086 00087 /************************************************************************/ 00093 /************************************************************************/ 00094 virtual bool getRawAddress(IList<Uint16>& rArray) const=0; 00095 00096 /************************************************************************/ 00099 /************************************************************************/ 00100 virtual bool isValid() const=0; 00101 00102 /************************************************************************/ 00105 /************************************************************************/ 00106 virtual const struct sockaddr& getSocketAddress() const = 0; 00107 00108 /************************************************************************/ 00111 /************************************************************************/ 00112 virtual Int getSocketAddressSize() const = 0; 00113 00114 /************************************************************************/ 00117 /************************************************************************/ 00118 virtual int getSocketFamily() const = 0; 00119 00120 /************************************************************************/ 00123 /************************************************************************/ 00124 virtual void reset() = 0; 00125 00126 00127 //set methods 00128 virtual void setPort(Uint uiPort); 00129 00130 //operators 00131 inline bool operator==(const TIPAddress& rAddress) const { return isEqual(rAddress); } 00132 00133 00134 //static methods 00135 static Retval createAddress(const IString& rstrAddr, Uint uiPort, TIPAddress*& rpAddress); 00136 00137 protected: 00139 Uint m_uiPort; 00141 TString m_strHostAddress; 00142 00143 //No problem for linux, only windows 00144 #if defined(ENABLE_WIN32_BINDING) 00145 static int inet_pton(int iAF, const char* pcSource, void* pDest, bool b); 00146 static const char* inet_ntop(int iAF, const void* pSource, char* pcDest, int cnt, bool b); 00147 #endif 00148 00149 private: 00150 00151 //prohibited constructors and operators 00152 #if defined(__COLINUX__) 00153 inline TIPAddress(const TIPAddress& rAddress) : TZObject(rAddress) {} 00154 #else 00155 inline TIPAddress(const TIPAddress& /*rAddress*/) {} 00156 #endif 00157 00158 inline TIPAddress& operator=(const TIPAddress& /*rInPar*/) { return *this; } 00159 00160 }; 00161 00162 /****************************************************************************/ 00165 /****************************************************************************/ 00166 inline Uint TIPAddress::getPort() const 00167 { 00168 return m_uiPort; 00169 } 00170 00171 /****************************************************************************/ 00174 /****************************************************************************/ 00175 inline TString TIPAddress::getHostAddress() const 00176 { 00177 return m_strHostAddress; 00178 } 00179 00180 /****************************************************************************/ 00183 /****************************************************************************/ 00184 inline void TIPAddress::setPort(Uint uiPort) 00185 { 00186 m_uiPort = uiPort; 00187 } 00188 00189 /****************************************************************************/ 00195 /****************************************************************************/ 00196 inline bool TIPAddress::isEqual(const TIPAddress& rAddress) const 00197 { 00198 return (m_uiPort == rAddress.m_uiPort && 00199 m_strHostAddress == rAddress.m_strHostAddress); 00200 } 00201 00202 00203 END_NAMESPACE_Zeus 00204 00205 #endif