1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Network address classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to switch to wxSockAddressImpl implementation
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // (c) 2008, 2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_SCKADDR_H_
14 #define _WX_SCKADDR_H_
20 #include "wx/string.h"
22 class wxSockAddressImpl
;
24 // forward declare it instead of including the system headers defining it which
25 // can bring in <windows.h> under Windows which we don't want to include from
29 // Any socket address kind
30 class WXDLLIMPEXP_NET wxSockAddress
: public wxObject
42 wxSockAddress(const wxSockAddress
& other
);
43 virtual ~wxSockAddress();
45 wxSockAddress
& operator=(const wxSockAddress
& other
);
48 virtual Family
Type() = 0;
50 // accessors for the low level address represented by this object
51 const sockaddr
*GetAddressData() const;
52 int GetAddressDataLen() const;
54 // we need to be able to create copies of the addresses polymorphically
55 // (i.e. without knowing the exact address class)
56 virtual wxSockAddress
*Clone() const = 0;
59 // implementation only, don't use
60 const wxSockAddressImpl
& GetAddress() const { return *m_impl
; }
61 void SetAddress(const wxSockAddressImpl
& address
);
64 wxSockAddressImpl
*m_impl
;
68 DECLARE_ABSTRACT_CLASS(wxSockAddress
)
71 // An IP address (either IPv4 or IPv6)
72 class WXDLLIMPEXP_NET wxIPaddress
: public wxSockAddress
75 wxIPaddress() : wxSockAddress() { }
76 wxIPaddress(const wxIPaddress
& other
)
77 : wxSockAddress(other
),
78 m_origHostname(other
.m_origHostname
)
82 bool operator==(const wxIPaddress
& addr
) const;
84 bool Hostname(const wxString
& name
);
85 bool Service(const wxString
& name
);
86 bool Service(unsigned short port
);
89 virtual bool IsLocalHost() const = 0;
93 virtual wxString
IPAddress() const = 0;
95 wxString
Hostname() const;
96 unsigned short Service() const;
98 wxString
OrigHostname() const { return m_origHostname
; }
101 // get m_impl initialized to the right family if it hadn't been done yet
102 wxSockAddressImpl
& GetImpl();
103 const wxSockAddressImpl
& GetImpl() const
105 return const_cast<wxIPaddress
*>(this)->GetImpl();
108 // host name originally passed to Hostname()
109 wxString m_origHostname
;
112 // create the wxSockAddressImpl object of the correct family if it's
113 // currently uninitialized
114 virtual void DoInitImpl() = 0;
117 DECLARE_ABSTRACT_CLASS(wxIPaddress
)
121 class WXDLLIMPEXP_NET wxIPV4address
: public wxIPaddress
124 wxIPV4address() : wxIPaddress() { }
125 wxIPV4address(const wxIPV4address
& other
) : wxIPaddress(other
) { }
127 // implement wxSockAddress pure virtuals:
128 virtual Family
Type() { return IPV4
; }
129 virtual wxSockAddress
*Clone() const { return new wxIPV4address(*this); }
132 // implement wxIPaddress pure virtuals:
133 virtual bool IsLocalHost() const;
135 virtual wxString
IPAddress() const;
138 // IPv4-specific methods:
139 bool Hostname(unsigned long addr
);
141 // make base class methods hidden by our overload visible
143 // FIXME-VC6: replace this with "using IPAddress::Hostname" (not supported
144 // by VC6) when support for it is dropped
145 wxString
Hostname() const { return wxIPaddress::Hostname(); }
146 bool Hostname(const wxString
& name
) { return wxIPaddress::Hostname(name
); }
148 bool BroadcastAddress();
151 virtual void DoInitImpl();
153 DECLARE_DYNAMIC_CLASS(wxIPV4address
)
160 class WXDLLIMPEXP_NET wxIPV6address
: public wxIPaddress
163 wxIPV6address() : wxIPaddress() { }
164 wxIPV6address(const wxIPV6address
& other
) : wxIPaddress(other
) { }
166 // implement wxSockAddress pure virtuals:
167 virtual Family
Type() { return IPV6
; }
168 virtual wxSockAddress
*Clone() const { return new wxIPV6address(*this); }
171 // implement wxIPaddress pure virtuals:
172 virtual bool IsLocalHost() const;
174 virtual wxString
IPAddress() const;
176 // IPv6-specific methods:
177 bool Hostname(unsigned char addr
[16]);
179 using wxIPaddress::Hostname
;
182 virtual void DoInitImpl();
184 DECLARE_DYNAMIC_CLASS(wxIPV6address
)
189 // Unix domain sockets are only available under, well, Unix
190 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
191 #define wxHAS_UNIX_DOMAIN_SOCKETS
194 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
196 // A Unix domain socket address
197 class WXDLLIMPEXP_NET wxUNIXaddress
: public wxSockAddress
200 wxUNIXaddress() : wxSockAddress() { }
201 wxUNIXaddress(const wxUNIXaddress
& other
) : wxSockAddress(other
) { }
203 void Filename(const wxString
& name
);
204 wxString
Filename() const;
206 virtual Family
Type() { return UNIX
; }
207 virtual wxSockAddress
*Clone() const { return new wxUNIXaddress(*this); }
210 wxSockAddressImpl
& GetUNIX();
211 const wxSockAddressImpl
& GetUNIX() const
213 return const_cast<wxUNIXaddress
*>(this)->GetUNIX();
216 DECLARE_DYNAMIC_CLASS(wxUNIXaddress
)
219 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
221 #endif // wxUSE_SOCKETS
223 #endif // _WX_SCKADDR_H_