IPAddress.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // Copyright @ 2015 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2015-07
  4. //
  5. #ifndef __IPADDRESS_H
  6. #define __IPADDRESS_H
  7. #include "BaseTypes.h"
  8. #include "BaseHeaders.h"
  9. #include "BaseCalls.h"
  10. #include "ErrorCode.h"
  11. #if defined(WIN32) || defined(WINDOWS)
  12. #include <ws2tcpip.h>
  13. #include <ws2ipdef.h>
  14. #elif defined(LINUX)
  15. #include <sys/socket.h>
  16. #include <netinet/in.h>
  17. #include <arpa/inet.h>
  18. #include <sys/un.h>
  19. #endif
  20. namespace tzc {
  21. // network address family
  22. enum __ADDR_FAMILY {
  23. AF_V4,
  24. AF_V6,
  25. AF_UN,
  26. AF_UNKNOWN
  27. };
  28. void DECLDLL InitOSNetwork();
  29. void DECLDLL UninitOSNetwork();
  30. class IPAddressImp {
  31. public:
  32. virtual TZ_Uint32 Size() const = 0;
  33. virtual TZ_Uint32 Scope() const = 0;
  34. virtual TZ_Uint32 Family() const = 0;
  35. virtual std::string ToString() const = 0;
  36. virtual const void * Addr() const = 0;
  37. virtual TZ_BOOL IsWildcard() const = 0;
  38. virtual TZ_BOOL IsBroadcast() const = 0;
  39. virtual TZ_BOOL IsLoopback() const = 0;
  40. virtual TZ_BOOL IsMulticast() const = 0;
  41. virtual TZ_INT SetAddress(const void * bytes) = 0;
  42. virtual ~IPAddressImp()
  43. {
  44. tzc::UninitOSNetwork();
  45. }
  46. protected:
  47. IPAddressImp()
  48. {
  49. tzc::InitOSNetwork();
  50. }
  51. private:
  52. IPAddressImp(const IPAddressImp &);
  53. IPAddressImp & operator = (const IPAddressImp &);
  54. };
  55. class IPv4IPAddressImp : public IPAddressImp {
  56. public:
  57. IPv4IPAddressImp();
  58. IPv4IPAddressImp(const void * bytes);
  59. virtual TZ_Uint32 Size() const;
  60. virtual TZ_Uint32 Scope() const;
  61. virtual TZ_Uint32 Family() const;
  62. virtual std::string ToString() const;
  63. virtual const void * Addr() const;
  64. virtual TZ_BOOL IsWildcard() const;
  65. virtual TZ_BOOL IsBroadcast() const;
  66. virtual TZ_BOOL IsLoopback() const;
  67. virtual TZ_BOOL IsMulticast() const;
  68. virtual TZ_INT SetAddress(const void * bytes);
  69. static IPv4IPAddressImp * Parse(const std::string & addr);
  70. private:
  71. struct in_addr m_addr;
  72. };
  73. class IPv6IPAddressImp : public IPAddressImp {
  74. public:
  75. IPv6IPAddressImp();
  76. IPv6IPAddressImp(const void * bytes);
  77. IPv6IPAddressImp(const void * bytes, TZ_Uint32 scope);
  78. virtual TZ_Uint32 Size() const;
  79. virtual TZ_Uint32 Scope() const;
  80. virtual TZ_Uint32 Family() const;
  81. virtual std::string ToString() const;
  82. virtual const void * Addr() const;
  83. virtual TZ_BOOL IsWildcard() const;
  84. virtual TZ_BOOL IsBroadcast() const;
  85. virtual TZ_BOOL IsLoopback() const;
  86. virtual TZ_BOOL IsMulticast() const;
  87. virtual TZ_INT SetAddress(const void * bytes);
  88. static IPv6IPAddressImp * Parse(const std::string & addr);
  89. private:
  90. struct in6_addr m_addr;
  91. TZ_Uint32 m_scope;
  92. };
  93. #if defined(LINUX) // only for Linux
  94. class UnixIPAddressImp : public IPAddressImp {
  95. public:
  96. UnixIPAddressImp();
  97. UnixIPAddressImp(const std::string & addr);
  98. virtual TZ_Uint32 Size() const;
  99. virtual TZ_Uint32 Scope() const;
  100. virtual TZ_Uint32 Family() const;
  101. virtual std::string ToString() const;
  102. virtual const void * Addr() const;
  103. virtual TZ_BOOL IsWildcard() const;
  104. virtual TZ_BOOL IsBroadcast() const;
  105. virtual TZ_BOOL IsLoopback() const;
  106. virtual TZ_BOOL IsMulticast() const;
  107. virtual TZ_INT SetAddress(const void * bytes);
  108. private:
  109. TZ_CHAR m_addr[108];
  110. };
  111. #endif
  112. class DECLDLL IPAddress {
  113. public:
  114. IPAddress();
  115. IPAddress(TZ_Uint32 family);
  116. IPAddress(const IPAddress & addr);
  117. explicit IPAddress(const std::string & addr, TZ_BOOL isUnix = FALSE);
  118. // when @size is INVALID_VALUE32, then it indicates
  119. // the address is a unix socket address
  120. IPAddress(const void * bytes, TZ_Uint32 size);
  121. // just for IPv6
  122. IPAddress(const void * bytes, TZ_Uint32 size, TZ_Uint32 scope);
  123. ~IPAddress();
  124. // returns E_OK on success, otherwise returns E_FAILED
  125. TZ_INT SetAddress(const std::string & addr);
  126. // returns E_OK on success, otherwise returns E_FAILED
  127. TZ_INT SetAddress(const void * bytes, TZ_Uint32 size);
  128. void Swap(IPAddress & addr);
  129. TZ_Uint32 Size() const;
  130. TZ_Uint32 Scope() const;
  131. TZ_Uint32 Family() const;
  132. std::string ToString() const;
  133. const void * Addr() const;
  134. public:
  135. IPAddress & operator = (const IPAddress & addr);
  136. bool operator == (const IPAddress & addr) const;
  137. bool operator != (const IPAddress & addr) const;
  138. bool operator < (const IPAddress & addr) const;
  139. bool operator <= (const IPAddress & addr) const;
  140. bool operator > (const IPAddress & addr) const;
  141. bool operator >= (const IPAddress & addr) const;
  142. public:
  143. // returns TRUE if the address is a wildcard (all zero) address
  144. TZ_BOOL IsWildcard() const;
  145. //
  146. // for IPv4,
  147. // returns TRUE if the address is a broadcast (all one) address
  148. // for IPv6 or UnixSocket
  149. // always returns FALSE
  150. TZ_BOOL IsBroadcast() const;
  151. //
  152. // returns true if the address is a loopback address
  153. // for IPv4, the address is 127.0.0.1
  154. // for IPv6, the address is ::1
  155. // for UnixSocket, always returns FALSE
  156. TZ_BOOL IsLoopback() const;
  157. //
  158. // returns true if the address is a multicast address
  159. // for IPv4, multicast addresses are in the 224.0.0.0 to 239.255.255.255 range
  160. // for IPv6, multicast addresses are in the FFxx:x:x:x:x:x:x:x range
  161. // for UnixSocket, always returns FALSE
  162. TZ_BOOL IsMulticast() const;
  163. //
  164. // returns TRUE if the address is neither a wildcard,
  165. // broadcast or multicast address
  166. TZ_BOOL IsUnicast() const;
  167. private:
  168. IPAddressImp * m_imp;
  169. };
  170. //
  171. // inlines
  172. //
  173. inline TZ_Uint32 IPAddress::Size() const
  174. {
  175. return m_imp->Size();
  176. }
  177. inline TZ_Uint32 IPAddress::Scope() const
  178. {
  179. return m_imp->Scope();
  180. }
  181. inline TZ_Uint32 IPAddress::Family() const
  182. {
  183. return m_imp->Family();
  184. }
  185. inline std::string IPAddress::ToString() const
  186. {
  187. return m_imp->ToString();
  188. }
  189. inline const void * IPAddress::Addr() const
  190. {
  191. return m_imp->Addr();
  192. }
  193. inline bool IPAddress::operator == (const IPAddress & addr) const
  194. {
  195. return (this->Family() == addr.Family() &&
  196. this->ToString() == addr.ToString());
  197. }
  198. inline bool IPAddress::operator != (const IPAddress & addr) const
  199. {
  200. return (this->Family() != addr.Family() ||
  201. this->ToString() != addr.ToString());
  202. }
  203. inline TZ_BOOL IPAddress::IsWildcard() const
  204. {
  205. return m_imp->IsWildcard();
  206. }
  207. inline TZ_BOOL IPAddress::IsBroadcast() const
  208. {
  209. return m_imp->IsBroadcast();
  210. }
  211. inline TZ_BOOL IPAddress::IsLoopback() const
  212. {
  213. return m_imp->IsLoopback();
  214. }
  215. inline TZ_BOOL IPAddress::IsMulticast() const
  216. {
  217. return m_imp->IsMulticast();
  218. }
  219. inline TZ_BOOL IPAddress::IsUnicast() const
  220. {
  221. return (!this->IsWildcard() && !this->IsBroadcast() && !this->IsMulticast());
  222. }
  223. }; // namespace tzc
  224. #endif /* ----- #ifndef __IPADDRESS_H ----- */