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 
12 #ifndef _WX_SCKADDR_H_
13 #define _WX_SCKADDR_H_
14 
15 #include "wx/defs.h"
16 
17 #if wxUSE_SOCKETS
18 
19 #include "wx/string.h"
20 
21 class wxSockAddressImpl;
22 
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
25 // public wx headers
26 struct sockaddr;
27 
28 // Any socket address kind
29 class WXDLLIMPEXP_NET wxSockAddress : public wxObject
30 {
31 public:
32     enum Family
33     {
34         NONE,
35         IPV4,
36         IPV6,
37         UNIX
38     };
39 
40     wxSockAddress();
41     wxSockAddress(const wxSockAddress& other);
42     virtual ~wxSockAddress();
43 
44     wxSockAddress& operator=(const wxSockAddress& other);
45 
46     virtual void Clear();
47     virtual Family Type() = 0;
48 
49     // accessors for the low level address represented by this object
50     const sockaddr *GetAddressData() const;
51     int GetAddressDataLen() const;
52 
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;
56 
57 
58     // implementation only, don't use
GetAddress()59     const wxSockAddressImpl& GetAddress() const { return *m_impl; }
60     void SetAddress(const wxSockAddressImpl& address);
61 
62 protected:
63     wxSockAddressImpl *m_impl;
64 
65 private:
66     void Init();
67     DECLARE_ABSTRACT_CLASS(wxSockAddress)
68 };
69 
70 // An IP address (either IPv4 or IPv6)
71 class WXDLLIMPEXP_NET wxIPaddress : public wxSockAddress
72 {
73 public:
wxIPaddress()74     wxIPaddress() : wxSockAddress() { }
wxIPaddress(const wxIPaddress & other)75     wxIPaddress(const wxIPaddress& other)
76         : wxSockAddress(other),
77           m_origHostname(other.m_origHostname)
78     {
79     }
80 
81     bool operator==(const wxIPaddress& addr) const;
82 
83     bool Hostname(const wxString& name);
84     bool Service(const wxString& name);
85     bool Service(unsigned short port);
86 
87     bool LocalHost();
88     virtual bool IsLocalHost() const = 0;
89 
90     bool AnyAddress();
91 
92     virtual wxString IPAddress() const = 0;
93 
94     wxString Hostname() const;
95     unsigned short Service() const;
96 
OrigHostname()97     wxString OrigHostname() const { return m_origHostname; }
98 
99 protected:
100     // get m_impl initialized to the right family if it hadn't been done yet
101     wxSockAddressImpl& GetImpl();
GetImpl()102     const wxSockAddressImpl& GetImpl() const
103     {
104         return const_cast<wxIPaddress *>(this)->GetImpl();
105     }
106 
107     // host name originally passed to Hostname()
108     wxString m_origHostname;
109 
110 private:
111     // create the wxSockAddressImpl object of the correct family if it's
112     // currently uninitialized
113     virtual void DoInitImpl() = 0;
114 
115 
116     DECLARE_ABSTRACT_CLASS(wxIPaddress)
117 };
118 
119 // An IPv4 address
120 class WXDLLIMPEXP_NET wxIPV4address : public wxIPaddress
121 {
122 public:
wxIPV4address()123     wxIPV4address() : wxIPaddress() { }
wxIPV4address(const wxIPV4address & other)124     wxIPV4address(const wxIPV4address& other) : wxIPaddress(other) { }
125 
126     // implement wxSockAddress pure virtuals:
Type()127     virtual Family Type() { return IPV4; }
Clone()128     virtual wxSockAddress *Clone() const { return new wxIPV4address(*this); }
129 
130 
131     // implement wxIPaddress pure virtuals:
132     virtual bool IsLocalHost() const;
133 
134     virtual wxString IPAddress() const;
135 
136 
137     // IPv4-specific methods:
138     bool Hostname(unsigned long addr);
139 
140     // make base class methods hidden by our overload visible
141     //
142     // FIXME-VC6: replace this with "using IPAddress::Hostname" (not supported
143     //            by VC6) when support for it is dropped
Hostname()144     wxString Hostname() const { return wxIPaddress::Hostname(); }
Hostname(const wxString & name)145     bool Hostname(const wxString& name) { return wxIPaddress::Hostname(name); }
146 
147     bool BroadcastAddress();
148 
149 private:
150     virtual void DoInitImpl();
151 
152     DECLARE_DYNAMIC_CLASS(wxIPV4address)
153 };
154 
155 
156 #if wxUSE_IPV6
157 
158 // An IPv6 address
159 class WXDLLIMPEXP_NET wxIPV6address : public wxIPaddress
160 {
161 public:
wxIPV6address()162     wxIPV6address() : wxIPaddress() { }
wxIPV6address(const wxIPV6address & other)163     wxIPV6address(const wxIPV6address& other) : wxIPaddress(other) { }
164 
165     // implement wxSockAddress pure virtuals:
Type()166     virtual Family Type() { return IPV6; }
Clone()167     virtual wxSockAddress *Clone() const { return new wxIPV6address(*this); }
168 
169 
170     // implement wxIPaddress pure virtuals:
171     virtual bool IsLocalHost() const;
172 
173     virtual wxString IPAddress() const;
174 
175     // IPv6-specific methods:
176     bool Hostname(unsigned char addr[16]);
177 
178     using wxIPaddress::Hostname;
179 
180 private:
181     virtual void DoInitImpl();
182 
183     DECLARE_DYNAMIC_CLASS(wxIPV6address)
184 };
185 
186 #endif // wxUSE_IPV6
187 
188 // Unix domain sockets are only available under, well, Unix
189 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
190     #define wxHAS_UNIX_DOMAIN_SOCKETS
191 #endif
192 
193 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
194 
195 // A Unix domain socket address
196 class WXDLLIMPEXP_NET wxUNIXaddress : public wxSockAddress
197 {
198 public:
wxUNIXaddress()199     wxUNIXaddress() : wxSockAddress() { }
wxUNIXaddress(const wxUNIXaddress & other)200     wxUNIXaddress(const wxUNIXaddress& other) : wxSockAddress(other) { }
201 
202     void Filename(const wxString& name);
203     wxString Filename() const;
204 
Type()205     virtual Family Type() { return UNIX; }
Clone()206     virtual wxSockAddress *Clone() const { return new wxUNIXaddress(*this); }
207 
208 private:
209     wxSockAddressImpl& GetUNIX();
GetUNIX()210     const wxSockAddressImpl& GetUNIX() const
211     {
212         return const_cast<wxUNIXaddress *>(this)->GetUNIX();
213     }
214 
215     DECLARE_DYNAMIC_CLASS(wxUNIXaddress)
216 };
217 
218 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
219 
220 #endif // wxUSE_SOCKETS
221 
222 #endif // _WX_SCKADDR_H_
223