]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sckaddr.cpp | |
3 | // Purpose: Network address manager | |
4 | // Author: Guilhem Lavaux | |
5 | // Modified by: | |
6 | // Created: 26/04/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997, 1998 Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
f4ada568 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
fcc6dddd JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
2df7be7f | 16 | #pragma hdrstop |
fcc6dddd JS |
17 | #endif |
18 | ||
35a4dab7 GL |
19 | #if wxUSE_SOCKETS |
20 | ||
6c0d0845 VZ |
21 | #ifndef WX_PRECOMP |
22 | #include "wx/defs.h" | |
23 | #include "wx/object.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/intl.h" | |
ce3ed50d | 26 | |
6c0d0845 VZ |
27 | #include <stdio.h> |
28 | #include <stdlib.h> | |
29 | #include <ctype.h> | |
30 | ||
31 | #if !defined(__MWERKS__) && !defined(__SALFORDC__) | |
32 | #include <memory.h> | |
33 | #endif | |
34 | #endif // !WX_PRECOMP | |
f4ada568 | 35 | |
3096bd2f | 36 | #include "wx/gsocket.h" |
6c0d0845 | 37 | #include "wx/socket.h" |
3096bd2f | 38 | #include "wx/sckaddr.h" |
f4ada568 | 39 | |
f4ada568 | 40 | IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject) |
be4bd463 JS |
41 | IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress) |
42 | IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress) | |
43 | #if wxUSE_IPV6 | |
44 | IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress) | |
f4ada568 | 45 | #endif |
97c153e4 | 46 | #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__)) |
f4ada568 GL |
47 | IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress) |
48 | #endif | |
f4ada568 | 49 | |
a324a7bc | 50 | // --------------------------------------------------------------------------- |
be4bd463 | 51 | // wxSockAddress |
a324a7bc GL |
52 | // --------------------------------------------------------------------------- |
53 | ||
6c0d0845 VZ |
54 | void wxSockAddress::Init() |
55 | { | |
56 | if ( !wxSocketBase::IsInitialized() ) | |
57 | { | |
58 | // we must do it before using GAddress_XXX functions | |
59 | (void)wxSocketBase::Initialize(); | |
60 | } | |
61 | } | |
62 | ||
a324a7bc | 63 | wxSockAddress::wxSockAddress() |
69919241 | 64 | { |
6c0d0845 VZ |
65 | Init(); |
66 | ||
67 | m_address = GAddress_new(); | |
f4ada568 GL |
68 | } |
69 | ||
1539c2f5 | 70 | wxSockAddress::wxSockAddress(const wxSockAddress& other) |
01babda3 | 71 | : wxObject() |
1539c2f5 | 72 | { |
6c0d0845 VZ |
73 | Init(); |
74 | ||
1539c2f5 VZ |
75 | m_address = GAddress_copy(other.m_address); |
76 | } | |
77 | ||
a324a7bc | 78 | wxSockAddress::~wxSockAddress() |
f4ada568 | 79 | { |
a324a7bc | 80 | GAddress_destroy(m_address); |
f4ada568 GL |
81 | } |
82 | ||
a324a7bc | 83 | void wxSockAddress::SetAddress(GAddress *address) |
f4ada568 | 84 | { |
a324a7bc GL |
85 | GAddress_destroy(m_address); |
86 | m_address = GAddress_copy(address); | |
f4ada568 GL |
87 | } |
88 | ||
1539c2f5 | 89 | wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr) |
f4ada568 | 90 | { |
a324a7bc GL |
91 | SetAddress(addr.GetAddress()); |
92 | return *this; | |
f4ada568 GL |
93 | } |
94 | ||
a324a7bc | 95 | void wxSockAddress::Clear() |
acd15a3f | 96 | { |
a324a7bc GL |
97 | GAddress_destroy(m_address); |
98 | m_address = GAddress_new(); | |
f4ada568 | 99 | } |
f4ada568 | 100 | |
be4bd463 JS |
101 | // --------------------------------------------------------------------------- |
102 | // wxIPaddress | |
103 | // --------------------------------------------------------------------------- | |
104 | ||
105 | wxIPaddress::wxIPaddress() | |
106 | : wxSockAddress() | |
107 | { | |
108 | } | |
109 | ||
110 | wxIPaddress::wxIPaddress(const wxIPaddress& other) | |
111 | : wxSockAddress(other) | |
112 | { | |
113 | } | |
114 | ||
115 | wxIPaddress::~wxIPaddress() | |
116 | { | |
117 | } | |
118 | ||
a324a7bc GL |
119 | // --------------------------------------------------------------------------- |
120 | // wxIPV4address | |
121 | // --------------------------------------------------------------------------- | |
122 | ||
123 | wxIPV4address::wxIPV4address() | |
be4bd463 | 124 | : wxIPaddress() |
1539c2f5 VZ |
125 | { |
126 | } | |
127 | ||
128 | wxIPV4address::wxIPV4address(const wxIPV4address& other) | |
be4bd463 | 129 | : wxIPaddress(other) |
f4ada568 | 130 | { |
a324a7bc | 131 | } |
f4ada568 | 132 | |
a324a7bc GL |
133 | wxIPV4address::~wxIPV4address() |
134 | { | |
135 | } | |
f4ada568 | 136 | |
a324a7bc GL |
137 | bool wxIPV4address::Hostname(const wxString& name) |
138 | { | |
f61815af | 139 | // Some people are sometimes fool. |
525d8583 | 140 | if (name.empty()) |
58c837a4 RR |
141 | { |
142 | wxLogWarning( _("Trying to solve a NULL hostname: giving up") ); | |
d775fa82 | 143 | return false; |
f61815af | 144 | } |
ce22d615 | 145 | m_origHostname = name; |
f6bcfd97 | 146 | return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR); |
f4ada568 GL |
147 | } |
148 | ||
149 | bool wxIPV4address::Hostname(unsigned long addr) | |
150 | { | |
ce22d615 RD |
151 | bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR); |
152 | if (rv) | |
153 | m_origHostname = Hostname(); | |
154 | else | |
2b5f62a0 | 155 | m_origHostname = wxEmptyString; |
ce22d615 | 156 | return rv; |
f4ada568 GL |
157 | } |
158 | ||
159 | bool wxIPV4address::Service(const wxString& name) | |
160 | { | |
f6bcfd97 | 161 | return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR); |
f4ada568 GL |
162 | } |
163 | ||
164 | bool wxIPV4address::Service(unsigned short port) | |
165 | { | |
a324a7bc | 166 | return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR); |
f4ada568 GL |
167 | } |
168 | ||
169 | bool wxIPV4address::LocalHost() | |
170 | { | |
a324a7bc | 171 | return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR); |
f4ada568 GL |
172 | } |
173 | ||
be4bd463 JS |
174 | bool wxIPV4address::IsLocalHost() const |
175 | { | |
176 | return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1")); | |
177 | } | |
178 | ||
d3ea6527 GRG |
179 | bool wxIPV4address::AnyAddress() |
180 | { | |
181 | return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR); | |
182 | } | |
183 | ||
be4bd463 | 184 | wxString wxIPV4address::Hostname() const |
f4ada568 | 185 | { |
a324a7bc | 186 | char hostname[1024]; |
f4ada568 | 187 | |
a324a7bc GL |
188 | hostname[0] = 0; |
189 | GAddress_INET_GetHostName(m_address, hostname, 1024); | |
2b5f62a0 | 190 | return wxString::FromAscii(hostname); |
f4ada568 GL |
191 | } |
192 | ||
be4bd463 | 193 | unsigned short wxIPV4address::Service() const |
f4ada568 | 194 | { |
acd15a3f | 195 | return GAddress_INET_GetPort(m_address); |
f4ada568 GL |
196 | } |
197 | ||
ce22d615 RD |
198 | wxSockAddress *wxIPV4address::Clone() const |
199 | { | |
200 | wxIPV4address *addr = new wxIPV4address(*this); | |
201 | addr->m_origHostname = m_origHostname; | |
202 | return addr; | |
203 | } | |
204 | ||
d9552598 | 205 | wxString wxIPV4address::IPAddress() const |
d775fa82 WS |
206 | { |
207 | unsigned long raw = GAddress_INET_GetHostAddress(m_address); | |
17a1ebd1 VZ |
208 | return wxString::Format(_T("%lu.%lu.%lu.%lu"), |
209 | (raw>>24) & 0xff, | |
210 | (raw>>16) & 0xff, | |
211 | (raw>>8) & 0xff, | |
212 | raw & 0xff | |
d775fa82 | 213 | ); |
d9552598 VZ |
214 | } |
215 | ||
fbfb8bcc | 216 | bool wxIPV4address::operator==(const wxIPV4address& addr) const |
1d6d8b5d | 217 | { |
17a1ebd1 VZ |
218 | return Hostname().Cmp(addr.Hostname().c_str()) == 0 && |
219 | Service() == addr.Service(); | |
1d6d8b5d JS |
220 | } |
221 | ||
be4bd463 | 222 | #if wxUSE_IPV6 |
a324a7bc GL |
223 | // --------------------------------------------------------------------------- |
224 | // wxIPV6address | |
225 | // --------------------------------------------------------------------------- | |
f4ada568 GL |
226 | |
227 | wxIPV6address::wxIPV6address() | |
be4bd463 | 228 | : wxIPaddress() |
f4ada568 | 229 | { |
f4ada568 GL |
230 | } |
231 | ||
1539c2f5 | 232 | wxIPV6address::wxIPV6address(const wxIPV6address& other) |
be4bd463 | 233 | : wxIPaddress(other) |
1539c2f5 VZ |
234 | { |
235 | } | |
236 | ||
f4ada568 GL |
237 | wxIPV6address::~wxIPV6address() |
238 | { | |
239 | } | |
240 | ||
f4ada568 GL |
241 | bool wxIPV6address::Hostname(const wxString& name) |
242 | { | |
525d8583 | 243 | if (name.empty()) |
be4bd463 JS |
244 | { |
245 | wxLogWarning( _("Trying to solve a NULL hostname: giving up") ); | |
d775fa82 | 246 | return false; |
be4bd463 JS |
247 | } |
248 | return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR); | |
f4ada568 GL |
249 | } |
250 | ||
be4bd463 | 251 | bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr)) |
f4ada568 | 252 | { |
d775fa82 | 253 | return true; |
f4ada568 GL |
254 | } |
255 | ||
be4bd463 | 256 | bool wxIPV6address::Service(const wxString& name) |
f4ada568 | 257 | { |
be4bd463 | 258 | return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR); |
f4ada568 GL |
259 | } |
260 | ||
261 | bool wxIPV6address::Service(unsigned short port) | |
262 | { | |
a324a7bc | 263 | return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR); |
f4ada568 GL |
264 | } |
265 | ||
266 | bool wxIPV6address::LocalHost() | |
267 | { | |
a324a7bc | 268 | return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR); |
f4ada568 GL |
269 | } |
270 | ||
be4bd463 JS |
271 | bool wxIPV6address::IsLocalHost() const |
272 | { | |
273 | return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1")); | |
274 | } | |
275 | ||
276 | bool wxIPV6address::AnyAddress() | |
277 | { | |
278 | return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR); | |
279 | } | |
280 | ||
281 | wxString wxIPV6address::IPAddress() const | |
d775fa82 WS |
282 | { |
283 | unsigned long raw = GAddress_INET_GetHostAddress(m_address); | |
284 | return wxString::Format( | |
285 | _T("%u.%u.%u.%u"), | |
286 | (unsigned char)((raw>>24) & 0xff), | |
287 | (unsigned char)((raw>>16) & 0xff), | |
288 | (unsigned char)((raw>>8) & 0xff), | |
289 | (unsigned char)(raw & 0xff) | |
290 | ); | |
be4bd463 JS |
291 | } |
292 | ||
293 | wxString wxIPV6address::Hostname() const | |
f4ada568 | 294 | { |
be4bd463 JS |
295 | char hostname[1024]; |
296 | ||
297 | hostname[0] = 0; | |
298 | GAddress_INET_GetHostName(m_address, hostname, 1024); | |
299 | return wxString::FromAscii(hostname); | |
f4ada568 GL |
300 | } |
301 | ||
be4bd463 | 302 | unsigned short wxIPV6address::Service() const |
f4ada568 | 303 | { |
acd15a3f | 304 | return GAddress_INET_GetPort(m_address); |
f4ada568 GL |
305 | } |
306 | ||
be4bd463 | 307 | #endif // wxUSE_IPV6 |
f4ada568 | 308 | |
97c153e4 | 309 | #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__)) |
1539c2f5 | 310 | |
a324a7bc GL |
311 | // --------------------------------------------------------------------------- |
312 | // wxUNIXaddress | |
313 | // --------------------------------------------------------------------------- | |
f4ada568 GL |
314 | |
315 | wxUNIXaddress::wxUNIXaddress() | |
1539c2f5 VZ |
316 | : wxSockAddress() |
317 | { | |
318 | } | |
319 | ||
320 | wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other) | |
321 | : wxSockAddress(other) | |
f4ada568 | 322 | { |
f4ada568 GL |
323 | } |
324 | ||
325 | wxUNIXaddress::~wxUNIXaddress() | |
326 | { | |
327 | } | |
328 | ||
f4ada568 GL |
329 | void wxUNIXaddress::Filename(const wxString& fname) |
330 | { | |
a324a7bc | 331 | GAddress_UNIX_SetPath(m_address, fname.fn_str()); |
f4ada568 GL |
332 | } |
333 | ||
334 | wxString wxUNIXaddress::Filename() | |
335 | { | |
a324a7bc | 336 | char path[1024]; |
f4ada568 | 337 | |
a324a7bc GL |
338 | path[0] = 0; |
339 | GAddress_UNIX_GetPath(m_address, path, 1024); | |
d775fa82 | 340 | |
2b5f62a0 | 341 | return wxString::FromAscii(path); |
f4ada568 GL |
342 | } |
343 | ||
1539c2f5 | 344 | #endif // __UNIX__ |
35a4dab7 | 345 | |
acd15a3f | 346 | #endif |
35a4dab7 | 347 | // wxUSE_SOCKETS |