]>
Commit | Line | Data |
---|---|---|
f4ada568 | 1 | ///////////////////////////////////////////////////////////////////////////// |
5fab0c8d | 2 | // Name: wx/sckaddr.h |
f4ada568 GL |
3 | // Purpose: Network address classes |
4 | // Author: Guilhem Lavaux | |
e143fb63 | 5 | // Modified by: Vadim Zeitlin to switch to wxSockAddressImpl implementation |
f4ada568 | 6 | // Created: 26/04/1997 |
f4ada568 | 7 | // Copyright: (c) 1997, 1998 Guilhem Lavaux |
e143fb63 | 8 | // (c) 2008, 2009 Vadim Zeitlin |
65571936 | 9 | // Licence: wxWindows licence |
f4ada568 | 10 | ///////////////////////////////////////////////////////////////////////////// |
2df7be7f | 11 | |
8ac36abb VZ |
12 | #ifndef _WX_SCKADDR_H_ |
13 | #define _WX_SCKADDR_H_ | |
f4ada568 | 14 | |
2df7be7f RR |
15 | #include "wx/defs.h" |
16 | ||
17 | #if wxUSE_SOCKETS | |
18 | ||
2df7be7f | 19 | #include "wx/string.h" |
a324a7bc | 20 | |
c9bccf23 | 21 | class wxSockAddressImpl; |
f4ada568 | 22 | |
5fab0c8d VZ |
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 | ||
8ac36abb VZ |
28 | // Any socket address kind |
29 | class WXDLLIMPEXP_NET wxSockAddress : public wxObject | |
30 | { | |
f4ada568 | 31 | public: |
c9bccf23 | 32 | enum Family |
8ac36abb | 33 | { |
c9bccf23 VZ |
34 | NONE, |
35 | IPV4, | |
36 | IPV6, | |
37 | UNIX | |
38 | }; | |
8ac36abb VZ |
39 | |
40 | wxSockAddress(); | |
41 | wxSockAddress(const wxSockAddress& other); | |
42 | virtual ~wxSockAddress(); | |
f4ada568 | 43 | |
8ac36abb | 44 | wxSockAddress& operator=(const wxSockAddress& other); |
a324a7bc | 45 | |
8ac36abb | 46 | virtual void Clear(); |
c9bccf23 | 47 | virtual Family Type() = 0; |
1539c2f5 | 48 | |
5fab0c8d VZ |
49 | // accessors for the low level address represented by this object |
50 | const sockaddr *GetAddressData() const; | |
51 | int GetAddressDataLen() const; | |
52 | ||
8ac36abb VZ |
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; | |
f4ada568 | 56 | |
f4ada568 | 57 | |
8ac36abb | 58 | // implementation only, don't use |
c9bccf23 VZ |
59 | const wxSockAddressImpl& GetAddress() const { return *m_impl; } |
60 | void SetAddress(const wxSockAddressImpl& address); | |
f4ada568 | 61 | |
a324a7bc | 62 | protected: |
c9bccf23 | 63 | wxSockAddressImpl *m_impl; |
6c0d0845 VZ |
64 | |
65 | private: | |
8ac36abb VZ |
66 | void Init(); |
67 | DECLARE_ABSTRACT_CLASS(wxSockAddress) | |
f4ada568 GL |
68 | }; |
69 | ||
8ac36abb VZ |
70 | // An IP address (either IPv4 or IPv6) |
71 | class WXDLLIMPEXP_NET wxIPaddress : public wxSockAddress | |
72 | { | |
be4bd463 | 73 | public: |
c9bccf23 VZ |
74 | wxIPaddress() : wxSockAddress() { } |
75 | wxIPaddress(const wxIPaddress& other) | |
76 | : wxSockAddress(other), | |
77 | m_origHostname(other.m_origHostname) | |
78 | { | |
79 | } | |
80 | ||
81 | bool operator==(const wxIPaddress& addr) const; | |
be4bd463 | 82 | |
c9bccf23 VZ |
83 | bool Hostname(const wxString& name); |
84 | bool Service(const wxString& name); | |
85 | bool Service(unsigned short port); | |
be4bd463 | 86 | |
c9bccf23 | 87 | bool LocalHost(); |
8ac36abb | 88 | virtual bool IsLocalHost() const = 0; |
be4bd463 | 89 | |
c9bccf23 | 90 | bool AnyAddress(); |
be4bd463 | 91 | |
8ac36abb | 92 | virtual wxString IPAddress() const = 0; |
be4bd463 | 93 | |
c9bccf23 VZ |
94 | wxString Hostname() const; |
95 | unsigned short Service() const; | |
96 | ||
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(); | |
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 | ||
8ac36abb VZ |
115 | |
116 | DECLARE_ABSTRACT_CLASS(wxIPaddress) | |
be4bd463 JS |
117 | }; |
118 | ||
8ac36abb VZ |
119 | // An IPv4 address |
120 | class WXDLLIMPEXP_NET wxIPV4address : public wxIPaddress | |
121 | { | |
f4ada568 | 122 | public: |
c9bccf23 VZ |
123 | wxIPV4address() : wxIPaddress() { } |
124 | wxIPV4address(const wxIPV4address& other) : wxIPaddress(other) { } | |
f4ada568 | 125 | |
8ac36abb | 126 | // implement wxSockAddress pure virtuals: |
c9bccf23 VZ |
127 | virtual Family Type() { return IPV4; } |
128 | virtual wxSockAddress *Clone() const { return new wxIPV4address(*this); } | |
be4bd463 | 129 | |
f4ada568 | 130 | |
8ac36abb | 131 | // implement wxIPaddress pure virtuals: |
8ac36abb | 132 | virtual bool IsLocalHost() const; |
f4ada568 | 133 | |
8ac36abb VZ |
134 | virtual wxString IPAddress() const; |
135 | ||
8ac36abb VZ |
136 | |
137 | // IPv4-specific methods: | |
8ac36abb | 138 | bool Hostname(unsigned long addr); |
c9bccf23 | 139 | |
8dc5f051 VZ |
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 | |
144 | wxString Hostname() const { return wxIPaddress::Hostname(); } | |
145 | bool Hostname(const wxString& name) { return wxIPaddress::Hostname(name); } | |
c9bccf23 | 146 | |
8dc5f051 | 147 | bool BroadcastAddress(); |
ce22d615 RD |
148 | |
149 | private: | |
c9bccf23 | 150 | virtual void DoInitImpl(); |
f4ada568 | 151 | |
8ac36abb VZ |
152 | DECLARE_DYNAMIC_CLASS(wxIPV4address) |
153 | }; | |
2ff35079 | 154 | |
2ff35079 | 155 | |
be4bd463 JS |
156 | #if wxUSE_IPV6 |
157 | ||
8ac36abb VZ |
158 | // An IPv6 address |
159 | class WXDLLIMPEXP_NET wxIPV6address : public wxIPaddress | |
160 | { | |
f4ada568 | 161 | public: |
c9bccf23 VZ |
162 | wxIPV6address() : wxIPaddress() { } |
163 | wxIPV6address(const wxIPV6address& other) : wxIPaddress(other) { } | |
8ac36abb VZ |
164 | |
165 | // implement wxSockAddress pure virtuals: | |
c9bccf23 | 166 | virtual Family Type() { return IPV6; } |
8ac36abb VZ |
167 | virtual wxSockAddress *Clone() const { return new wxIPV6address(*this); } |
168 | ||
169 | ||
170 | // implement wxIPaddress pure virtuals: | |
8ac36abb VZ |
171 | virtual bool IsLocalHost() const; |
172 | ||
8ac36abb VZ |
173 | virtual wxString IPAddress() const; |
174 | ||
8ac36abb | 175 | // IPv6-specific methods: |
8ac36abb VZ |
176 | bool Hostname(unsigned char addr[16]); |
177 | ||
c9bccf23 VZ |
178 | using wxIPaddress::Hostname; |
179 | ||
8ac36abb | 180 | private: |
c9bccf23 | 181 | virtual void DoInitImpl(); |
8ac36abb VZ |
182 | |
183 | DECLARE_DYNAMIC_CLASS(wxIPV6address) | |
f4ada568 | 184 | }; |
be4bd463 JS |
185 | |
186 | #endif // wxUSE_IPV6 | |
f4ada568 | 187 | |
c9bccf23 VZ |
188 | // Unix domain sockets are only available under, well, Unix |
189 | #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) | |
190 | #define wxHAS_UNIX_DOMAIN_SOCKETS | |
e1c70641 | 191 | #endif |
f4ada568 | 192 | |
c9bccf23 VZ |
193 | #ifdef wxHAS_UNIX_DOMAIN_SOCKETS |
194 | ||
8ac36abb VZ |
195 | // A Unix domain socket address |
196 | class WXDLLIMPEXP_NET wxUNIXaddress : public wxSockAddress | |
197 | { | |
f4ada568 | 198 | public: |
c9bccf23 VZ |
199 | wxUNIXaddress() : wxSockAddress() { } |
200 | wxUNIXaddress(const wxUNIXaddress& other) : wxSockAddress(other) { } | |
8ac36abb VZ |
201 | |
202 | void Filename(const wxString& name); | |
c9bccf23 | 203 | wxString Filename() const; |
f4ada568 | 204 | |
c9bccf23 | 205 | virtual Family Type() { return UNIX; } |
8ac36abb VZ |
206 | virtual wxSockAddress *Clone() const { return new wxUNIXaddress(*this); } |
207 | ||
208 | private: | |
c9bccf23 VZ |
209 | wxSockAddressImpl& GetUNIX(); |
210 | const wxSockAddressImpl& GetUNIX() const | |
211 | { | |
212 | return const_cast<wxUNIXaddress *>(this)->GetUNIX(); | |
213 | } | |
f4ada568 | 214 | |
8ac36abb | 215 | DECLARE_DYNAMIC_CLASS(wxUNIXaddress) |
f4ada568 | 216 | }; |
f4ada568 | 217 | |
c9bccf23 | 218 | #endif // wxHAS_UNIX_DOMAIN_SOCKETS |
65ccd2b8 | 219 | |
8ac36abb VZ |
220 | #endif // wxUSE_SOCKETS |
221 | ||
222 | #endif // _WX_SCKADDR_H_ |