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