sckaddr.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/sckaddr.h
  3. // Purpose: Network address classes
  4. // Author: Guilhem Lavaux
  5. // Modified by: Vadim Zeitlin to switch to wxSockAddressImpl implementation
  6. // Created: 26/04/1997
  7. // Copyright: (c) 1997, 1998 Guilhem Lavaux
  8. // (c) 2008, 2009 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_SCKADDR_H_
  12. #define _WX_SCKADDR_H_
  13. #include "wx/defs.h"
  14. #if wxUSE_SOCKETS
  15. #include "wx/string.h"
  16. class wxSockAddressImpl;
  17. // forward declare it instead of including the system headers defining it which
  18. // can bring in <windows.h> under Windows which we don't want to include from
  19. // public wx headers
  20. struct sockaddr;
  21. // Any socket address kind
  22. class WXDLLIMPEXP_NET wxSockAddress : public wxObject
  23. {
  24. public:
  25. enum Family
  26. {
  27. NONE,
  28. IPV4,
  29. IPV6,
  30. UNIX
  31. };
  32. wxSockAddress();
  33. wxSockAddress(const wxSockAddress& other);
  34. virtual ~wxSockAddress();
  35. wxSockAddress& operator=(const wxSockAddress& other);
  36. virtual void Clear();
  37. virtual Family Type() = 0;
  38. // accessors for the low level address represented by this object
  39. const sockaddr *GetAddressData() const;
  40. int GetAddressDataLen() const;
  41. // we need to be able to create copies of the addresses polymorphically
  42. // (i.e. without knowing the exact address class)
  43. virtual wxSockAddress *Clone() const = 0;
  44. // implementation only, don't use
  45. const wxSockAddressImpl& GetAddress() const { return *m_impl; }
  46. void SetAddress(const wxSockAddressImpl& address);
  47. protected:
  48. wxSockAddressImpl *m_impl;
  49. private:
  50. void Init();
  51. DECLARE_ABSTRACT_CLASS(wxSockAddress)
  52. };
  53. // An IP address (either IPv4 or IPv6)
  54. class WXDLLIMPEXP_NET wxIPaddress : public wxSockAddress
  55. {
  56. public:
  57. wxIPaddress() : wxSockAddress() { }
  58. wxIPaddress(const wxIPaddress& other)
  59. : wxSockAddress(other),
  60. m_origHostname(other.m_origHostname)
  61. {
  62. }
  63. bool operator==(const wxIPaddress& addr) const;
  64. bool Hostname(const wxString& name);
  65. bool Service(const wxString& name);
  66. bool Service(unsigned short port);
  67. bool LocalHost();
  68. virtual bool IsLocalHost() const = 0;
  69. bool AnyAddress();
  70. virtual wxString IPAddress() const = 0;
  71. wxString Hostname() const;
  72. unsigned short Service() const;
  73. wxString OrigHostname() const { return m_origHostname; }
  74. protected:
  75. // get m_impl initialized to the right family if it hadn't been done yet
  76. wxSockAddressImpl& GetImpl();
  77. const wxSockAddressImpl& GetImpl() const
  78. {
  79. return const_cast<wxIPaddress *>(this)->GetImpl();
  80. }
  81. // host name originally passed to Hostname()
  82. wxString m_origHostname;
  83. private:
  84. // create the wxSockAddressImpl object of the correct family if it's
  85. // currently uninitialized
  86. virtual void DoInitImpl() = 0;
  87. DECLARE_ABSTRACT_CLASS(wxIPaddress)
  88. };
  89. // An IPv4 address
  90. class WXDLLIMPEXP_NET wxIPV4address : public wxIPaddress
  91. {
  92. public:
  93. wxIPV4address() : wxIPaddress() { }
  94. wxIPV4address(const wxIPV4address& other) : wxIPaddress(other) { }
  95. // implement wxSockAddress pure virtuals:
  96. virtual Family Type() { return IPV4; }
  97. virtual wxSockAddress *Clone() const { return new wxIPV4address(*this); }
  98. // implement wxIPaddress pure virtuals:
  99. virtual bool IsLocalHost() const;
  100. virtual wxString IPAddress() const;
  101. // IPv4-specific methods:
  102. bool Hostname(unsigned long addr);
  103. // make base class methods hidden by our overload visible
  104. //
  105. // FIXME-VC6: replace this with "using IPAddress::Hostname" (not supported
  106. // by VC6) when support for it is dropped
  107. wxString Hostname() const { return wxIPaddress::Hostname(); }
  108. bool Hostname(const wxString& name) { return wxIPaddress::Hostname(name); }
  109. bool BroadcastAddress();
  110. private:
  111. virtual void DoInitImpl();
  112. DECLARE_DYNAMIC_CLASS(wxIPV4address)
  113. };
  114. #if wxUSE_IPV6
  115. // An IPv6 address
  116. class WXDLLIMPEXP_NET wxIPV6address : public wxIPaddress
  117. {
  118. public:
  119. wxIPV6address() : wxIPaddress() { }
  120. wxIPV6address(const wxIPV6address& other) : wxIPaddress(other) { }
  121. // implement wxSockAddress pure virtuals:
  122. virtual Family Type() { return IPV6; }
  123. virtual wxSockAddress *Clone() const { return new wxIPV6address(*this); }
  124. // implement wxIPaddress pure virtuals:
  125. virtual bool IsLocalHost() const;
  126. virtual wxString IPAddress() const;
  127. // IPv6-specific methods:
  128. bool Hostname(unsigned char addr[16]);
  129. using wxIPaddress::Hostname;
  130. private:
  131. virtual void DoInitImpl();
  132. DECLARE_DYNAMIC_CLASS(wxIPV6address)
  133. };
  134. #endif // wxUSE_IPV6
  135. // Unix domain sockets are only available under, well, Unix
  136. #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
  137. #define wxHAS_UNIX_DOMAIN_SOCKETS
  138. #endif
  139. #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
  140. // A Unix domain socket address
  141. class WXDLLIMPEXP_NET wxUNIXaddress : public wxSockAddress
  142. {
  143. public:
  144. wxUNIXaddress() : wxSockAddress() { }
  145. wxUNIXaddress(const wxUNIXaddress& other) : wxSockAddress(other) { }
  146. void Filename(const wxString& name);
  147. wxString Filename() const;
  148. virtual Family Type() { return UNIX; }
  149. virtual wxSockAddress *Clone() const { return new wxUNIXaddress(*this); }
  150. private:
  151. wxSockAddressImpl& GetUNIX();
  152. const wxSockAddressImpl& GetUNIX() const
  153. {
  154. return const_cast<wxUNIXaddress *>(this)->GetUNIX();
  155. }
  156. DECLARE_DYNAMIC_CLASS(wxUNIXaddress)
  157. };
  158. #endif // wxHAS_UNIX_DOMAIN_SOCKETS
  159. #endif // wxUSE_SOCKETS
  160. #endif // _WX_SCKADDR_H_