1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Network address classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to switch to wxSockAddressImpl implementation
7 // Copyright: (c) 1997, 1998 Guilhem Lavaux
8 // (c) 2008, 2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_SCKADDR_H_
13 #define _WX_SCKADDR_H_
19 #include "wx/string.h"
21 class wxSockAddressImpl
;
23 // forward declare it instead of including the system headers defining it which
24 // can bring in <windows.h> under Windows which we don't want to include from
28 // Any socket address kind
29 class WXDLLIMPEXP_NET wxSockAddress
: public wxObject
41 wxSockAddress(const wxSockAddress
& other
);
42 virtual ~wxSockAddress();
44 wxSockAddress
& operator=(const wxSockAddress
& other
);
47 virtual Family
Type() = 0;
49 // accessors for the low level address represented by this object
50 const sockaddr
*GetAddressData() const;
51 int GetAddressDataLen() const;
53 // we need to be able to create copies of the addresses polymorphically
54 // (i.e. without knowing the exact address class)
55 virtual wxSockAddress
*Clone() const = 0;
58 // implementation only, don't use
59 const wxSockAddressImpl
& GetAddress() const { return *m_impl
; }
60 void SetAddress(const wxSockAddressImpl
& address
);
63 wxSockAddressImpl
*m_impl
;
67 DECLARE_ABSTRACT_CLASS(wxSockAddress
)
70 // An IP address (either IPv4 or IPv6)
71 class WXDLLIMPEXP_NET wxIPaddress
: public wxSockAddress
74 wxIPaddress() : wxSockAddress() { }
75 wxIPaddress(const wxIPaddress
& other
)
76 : wxSockAddress(other
),
77 m_origHostname(other
.m_origHostname
)
81 bool operator==(const wxIPaddress
& addr
) const;
83 bool Hostname(const wxString
& name
);
84 bool Service(const wxString
& name
);
85 bool Service(unsigned short port
);
88 virtual bool IsLocalHost() const = 0;
92 virtual wxString
IPAddress() const = 0;
94 wxString
Hostname() const;
95 unsigned short Service() const;
97 wxString
OrigHostname() const { return m_origHostname
; }
100 // get m_impl initialized to the right family if it hadn't been done yet
101 wxSockAddressImpl
& GetImpl();
102 const wxSockAddressImpl
& GetImpl() const
104 return const_cast<wxIPaddress
*>(this)->GetImpl();
107 // host name originally passed to Hostname()
108 wxString m_origHostname
;
111 // create the wxSockAddressImpl object of the correct family if it's
112 // currently uninitialized
113 virtual void DoInitImpl() = 0;
116 DECLARE_ABSTRACT_CLASS(wxIPaddress
)
120 class WXDLLIMPEXP_NET wxIPV4address
: public wxIPaddress
123 wxIPV4address() : wxIPaddress() { }
124 wxIPV4address(const wxIPV4address
& other
) : wxIPaddress(other
) { }
126 // implement wxSockAddress pure virtuals:
127 virtual Family
Type() { return IPV4
; }
128 virtual wxSockAddress
*Clone() const { return new wxIPV4address(*this); }
131 // implement wxIPaddress pure virtuals:
132 virtual bool IsLocalHost() const;
134 virtual wxString
IPAddress() const;
137 // IPv4-specific methods:
138 bool Hostname(unsigned long addr
);
140 // make base class methods hidden by our overload visible
142 // FIXME-VC6: replace this with "using IPAddress::Hostname" (not supported
143 // by VC6) when support for it is dropped
144 wxString
Hostname() const { return wxIPaddress::Hostname(); }
145 bool Hostname(const wxString
& name
) { return wxIPaddress::Hostname(name
); }
147 bool BroadcastAddress();
150 virtual void DoInitImpl();
152 DECLARE_DYNAMIC_CLASS(wxIPV4address
)
159 class WXDLLIMPEXP_NET wxIPV6address
: public wxIPaddress
162 wxIPV6address() : wxIPaddress() { }
163 wxIPV6address(const wxIPV6address
& other
) : wxIPaddress(other
) { }
165 // implement wxSockAddress pure virtuals:
166 virtual Family
Type() { return IPV6
; }
167 virtual wxSockAddress
*Clone() const { return new wxIPV6address(*this); }
170 // implement wxIPaddress pure virtuals:
171 virtual bool IsLocalHost() const;
173 virtual wxString
IPAddress() const;
175 // IPv6-specific methods:
176 bool Hostname(unsigned char addr
[16]);
178 using wxIPaddress::Hostname
;
181 virtual void DoInitImpl();
183 DECLARE_DYNAMIC_CLASS(wxIPV6address
)
188 // Unix domain sockets are only available under, well, Unix
189 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
190 #define wxHAS_UNIX_DOMAIN_SOCKETS
193 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
195 // A Unix domain socket address
196 class WXDLLIMPEXP_NET wxUNIXaddress
: public wxSockAddress
199 wxUNIXaddress() : wxSockAddress() { }
200 wxUNIXaddress(const wxUNIXaddress
& other
) : wxSockAddress(other
) { }
202 void Filename(const wxString
& name
);
203 wxString
Filename() const;
205 virtual Family
Type() { return UNIX
; }
206 virtual wxSockAddress
*Clone() const { return new wxUNIXaddress(*this); }
209 wxSockAddressImpl
& GetUNIX();
210 const wxSockAddressImpl
& GetUNIX() const
212 return const_cast<wxUNIXaddress
*>(this)->GetUNIX();
215 DECLARE_DYNAMIC_CLASS(wxUNIXaddress
)
218 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
220 #endif // wxUSE_SOCKETS
222 #endif // _WX_SCKADDR_H_