]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
use wxPEN/BRUSH_XXX instead of wxXXX (#9812)
[wxWidgets.git] / src / common / sckaddr.cpp
CommitLineData
f4ada568 1/////////////////////////////////////////////////////////////////////////////
8898456d 2// Name: src/common/sckaddr.cpp
f4ada568
GL
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__
8898456d 16 #pragma hdrstop
fcc6dddd
JS
17#endif
18
35a4dab7
GL
19#if wxUSE_SOCKETS
20
6c0d0845 21#ifndef WX_PRECOMP
6c0d0845
VZ
22 #include "wx/object.h"
23 #include "wx/log.h"
24 #include "wx/intl.h"
ce3ed50d 25
6c0d0845
VZ
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29
f172cb82 30 #if !defined(__MWERKS__)
6c0d0845
VZ
31 #include <memory.h>
32 #endif
33#endif // !WX_PRECOMP
f4ada568 34
3096bd2f 35#include "wx/gsocket.h"
6c0d0845 36#include "wx/socket.h"
3096bd2f 37#include "wx/sckaddr.h"
f4ada568 38
f4ada568 39IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
be4bd463
JS
40IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress)
41IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress)
42#if wxUSE_IPV6
43IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress)
f4ada568 44#endif
0ad76eea 45#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
f4ada568
GL
46IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
47#endif
f4ada568 48
a324a7bc 49// ---------------------------------------------------------------------------
be4bd463 50// wxSockAddress
a324a7bc
GL
51// ---------------------------------------------------------------------------
52
6c0d0845
VZ
53void wxSockAddress::Init()
54{
55 if ( !wxSocketBase::IsInitialized() )
56 {
57 // we must do it before using GAddress_XXX functions
58 (void)wxSocketBase::Initialize();
59 }
60}
61
a324a7bc 62wxSockAddress::wxSockAddress()
69919241 63{
6c0d0845
VZ
64 Init();
65
66 m_address = GAddress_new();
f4ada568
GL
67}
68
1539c2f5 69wxSockAddress::wxSockAddress(const wxSockAddress& other)
01babda3 70 : wxObject()
1539c2f5 71{
6c0d0845
VZ
72 Init();
73
1539c2f5
VZ
74 m_address = GAddress_copy(other.m_address);
75}
76
a324a7bc 77wxSockAddress::~wxSockAddress()
f4ada568 78{
a324a7bc 79 GAddress_destroy(m_address);
f4ada568
GL
80}
81
a324a7bc 82void wxSockAddress::SetAddress(GAddress *address)
f4ada568 83{
fccb65a2
VZ
84 if ( address != m_address )
85 {
86 GAddress_destroy(m_address);
87 m_address = GAddress_copy(address);
88 }
f4ada568
GL
89}
90
1539c2f5 91wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
f4ada568 92{
a324a7bc
GL
93 SetAddress(addr.GetAddress());
94 return *this;
f4ada568
GL
95}
96
a324a7bc 97void wxSockAddress::Clear()
acd15a3f 98{
a324a7bc
GL
99 GAddress_destroy(m_address);
100 m_address = GAddress_new();
f4ada568 101}
f4ada568 102
be4bd463
JS
103// ---------------------------------------------------------------------------
104// wxIPaddress
105// ---------------------------------------------------------------------------
106
107wxIPaddress::wxIPaddress()
108 : wxSockAddress()
109{
110}
111
112wxIPaddress::wxIPaddress(const wxIPaddress& other)
113 : wxSockAddress(other)
114{
115}
116
117wxIPaddress::~wxIPaddress()
118{
119}
120
a324a7bc
GL
121// ---------------------------------------------------------------------------
122// wxIPV4address
123// ---------------------------------------------------------------------------
124
125wxIPV4address::wxIPV4address()
be4bd463 126 : wxIPaddress()
1539c2f5
VZ
127{
128}
129
130wxIPV4address::wxIPV4address(const wxIPV4address& other)
be4bd463 131 : wxIPaddress(other)
f4ada568 132{
a324a7bc 133}
f4ada568 134
a324a7bc
GL
135wxIPV4address::~wxIPV4address()
136{
137}
f4ada568 138
a324a7bc
GL
139bool wxIPV4address::Hostname(const wxString& name)
140{
f61815af 141 // Some people are sometimes fool.
525d8583 142 if (name.empty())
58c837a4
RR
143 {
144 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
d775fa82 145 return false;
f61815af 146 }
ce22d615 147 m_origHostname = name;
f6bcfd97 148 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
149}
150
151bool wxIPV4address::Hostname(unsigned long addr)
152{
ce22d615
RD
153 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
154 if (rv)
155 m_origHostname = Hostname();
156 else
2b5f62a0 157 m_origHostname = wxEmptyString;
ce22d615 158 return rv;
f4ada568
GL
159}
160
161bool wxIPV4address::Service(const wxString& name)
162{
f6bcfd97 163 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
164}
165
166bool wxIPV4address::Service(unsigned short port)
167{
a324a7bc 168 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
169}
170
171bool wxIPV4address::LocalHost()
172{
a324a7bc 173 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
174}
175
be4bd463
JS
176bool wxIPV4address::IsLocalHost() const
177{
178 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
179}
180
60edcf45
VZ
181bool wxIPV4address::BroadcastAddress()
182{
183 return (GAddress_INET_SetBroadcastAddress(m_address) == GSOCK_NOERROR);
184}
185
d3ea6527
GRG
186bool wxIPV4address::AnyAddress()
187{
188 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
189}
190
be4bd463 191wxString wxIPV4address::Hostname() const
f4ada568 192{
a324a7bc 193 char hostname[1024];
f4ada568 194
a324a7bc
GL
195 hostname[0] = 0;
196 GAddress_INET_GetHostName(m_address, hostname, 1024);
2b5f62a0 197 return wxString::FromAscii(hostname);
f4ada568
GL
198}
199
be4bd463 200unsigned short wxIPV4address::Service() const
f4ada568 201{
acd15a3f 202 return GAddress_INET_GetPort(m_address);
f4ada568
GL
203}
204
ce22d615
RD
205wxSockAddress *wxIPV4address::Clone() const
206{
207 wxIPV4address *addr = new wxIPV4address(*this);
208 addr->m_origHostname = m_origHostname;
209 return addr;
210}
211
d9552598 212wxString wxIPV4address::IPAddress() const
d775fa82
WS
213{
214 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
17a1ebd1
VZ
215 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
216 (raw>>24) & 0xff,
217 (raw>>16) & 0xff,
218 (raw>>8) & 0xff,
219 raw & 0xff
d775fa82 220 );
d9552598
VZ
221}
222
fbfb8bcc 223bool wxIPV4address::operator==(const wxIPV4address& addr) const
1d6d8b5d 224{
c9f78968
VS
225 return Hostname().Cmp(addr.Hostname()) == 0 &&
226 Service() == addr.Service();
1d6d8b5d
JS
227}
228
be4bd463 229#if wxUSE_IPV6
a324a7bc
GL
230// ---------------------------------------------------------------------------
231// wxIPV6address
232// ---------------------------------------------------------------------------
f4ada568
GL
233
234wxIPV6address::wxIPV6address()
be4bd463 235 : wxIPaddress()
f4ada568 236{
f4ada568
GL
237}
238
1539c2f5 239wxIPV6address::wxIPV6address(const wxIPV6address& other)
8575ff50 240 : wxIPaddress(other), m_origHostname(other.m_origHostname)
1539c2f5
VZ
241{
242}
243
f4ada568
GL
244wxIPV6address::~wxIPV6address()
245{
246}
247
f4ada568
GL
248bool wxIPV6address::Hostname(const wxString& name)
249{
525d8583 250 if (name.empty())
be4bd463
JS
251 {
252 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
d775fa82 253 return false;
be4bd463 254 }
8575ff50
VZ
255 m_origHostname = name;
256 return (GAddress_INET6_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
257}
258
8575ff50 259bool wxIPV6address::Hostname(unsigned char addr[16])
f4ada568 260{
8575ff50
VZ
261 wxString name;
262 unsigned short wk[8];
263 for ( int i = 0; i < 8; ++i )
264 {
265 wk[i] = addr[2*i];
266 wk[i] <<= 8;
267 wk[i] |= addr[2*i+1];
268 }
269 name.Printf("%x:%x:%x:%x:%x:%x:%x:%x",
270 wk[0], wk[1], wk[2], wk[3], wk[4], wk[5], wk[6], wk[7]);
271 return Hostname(name);
f4ada568
GL
272}
273
be4bd463 274bool wxIPV6address::Service(const wxString& name)
f4ada568 275{
8575ff50 276 return (GAddress_INET6_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
277}
278
279bool wxIPV6address::Service(unsigned short port)
280{
8575ff50 281 return (GAddress_INET6_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
282}
283
284bool wxIPV6address::LocalHost()
285{
8575ff50 286 return (GAddress_INET6_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
287}
288
be4bd463
JS
289bool wxIPV6address::IsLocalHost() const
290{
8575ff50
VZ
291 if ( Hostname() == "localhost" )
292 return true;
293
294 wxString addr = IPAddress();
295 return addr == wxT("::1") ||
296 addr == wxT("0:0:0:0:0:0:0:1") ||
297 addr == wxT("::ffff:127.0.0.1");
be4bd463
JS
298}
299
60edcf45
VZ
300bool wxIPV6address::BroadcastAddress()
301{
8575ff50
VZ
302 wxFAIL_MSG( "not implemented" );
303
304 return false;
60edcf45
VZ
305}
306
be4bd463
JS
307bool wxIPV6address::AnyAddress()
308{
8575ff50 309 return (GAddress_INET6_SetAnyAddress(m_address) == GSOCK_NOERROR);
be4bd463
JS
310}
311
312wxString wxIPV6address::IPAddress() const
d775fa82 313{
8575ff50
VZ
314 unsigned char addr[16];
315 GAddress_INET6_GetHostAddress(m_address,(in6_addr*)addr);
316
317 wxUint16 words[8];
318 int i,
319 prefix_zero_count = 0;
320 for ( i = 0; i < 8; ++i )
321 {
322 words[i] = addr[i*2];
323 words[i] <<= 8;
324 words[i] |= addr[i*2+1];
325 if ( i == prefix_zero_count && words[i] == 0 )
326 ++prefix_zero_count;
327 }
328
329 wxString result;
330 if ( prefix_zero_count == 8 )
331 {
332 result = wxT( "::" );
333 }
334 else if ( prefix_zero_count == 6 && words[5] == 0xFFFF )
335 {
336 // IPv4 mapped
337 result.Printf("::ffff:%d.%d.%d.%d",
338 addr[12], addr[13], addr[14], addr[15]);
339 }
340 else // general case
341 {
342 result = ":";
343 for ( i = prefix_zero_count; i < 8; ++i )
344 {
345 result += wxString::Format(":%x", words[i]);
346 }
347 }
348
349 return result;
be4bd463
JS
350}
351
352wxString wxIPV6address::Hostname() const
f4ada568 353{
be4bd463 354 char hostname[1024];
be4bd463 355 hostname[0] = 0;
8575ff50
VZ
356
357 if ( GAddress_INET6_GetHostName(m_address,
358 hostname,
359 WXSIZEOF(hostname)) != GSOCK_NOERROR )
360 return wxEmptyString;
361
be4bd463 362 return wxString::FromAscii(hostname);
f4ada568
GL
363}
364
be4bd463 365unsigned short wxIPV6address::Service() const
f4ada568 366{
8575ff50 367 return GAddress_INET6_GetPort(m_address);
f4ada568
GL
368}
369
be4bd463 370#endif // wxUSE_IPV6
f4ada568 371
0ad76eea 372#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
1539c2f5 373
a324a7bc
GL
374// ---------------------------------------------------------------------------
375// wxUNIXaddress
376// ---------------------------------------------------------------------------
f4ada568
GL
377
378wxUNIXaddress::wxUNIXaddress()
1539c2f5
VZ
379 : wxSockAddress()
380{
381}
382
383wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
384 : wxSockAddress(other)
f4ada568 385{
f4ada568
GL
386}
387
388wxUNIXaddress::~wxUNIXaddress()
389{
390}
391
f4ada568
GL
392void wxUNIXaddress::Filename(const wxString& fname)
393{
a324a7bc 394 GAddress_UNIX_SetPath(m_address, fname.fn_str());
f4ada568
GL
395}
396
397wxString wxUNIXaddress::Filename()
398{
a324a7bc 399 char path[1024];
f4ada568 400
a324a7bc
GL
401 path[0] = 0;
402 GAddress_UNIX_GetPath(m_address, path, 1024);
d775fa82 403
2b5f62a0 404 return wxString::FromAscii(path);
f4ada568
GL
405}
406
1539c2f5 407#endif // __UNIX__
35a4dab7 408
acd15a3f 409#endif
35a4dab7 410 // wxUSE_SOCKETS