]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
use proper dllexport declaration with _WX_LIST_HELPER_
[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
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 40IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
be4bd463
JS
41IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress)
42IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress)
43#if wxUSE_IPV6
44IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress)
f4ada568 45#endif
97c153e4 46#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
f4ada568
GL
47IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
48#endif
f4ada568 49
a324a7bc 50// ---------------------------------------------------------------------------
be4bd463 51// wxSockAddress
a324a7bc
GL
52// ---------------------------------------------------------------------------
53
6c0d0845
VZ
54void 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 63wxSockAddress::wxSockAddress()
69919241 64{
6c0d0845
VZ
65 Init();
66
67 m_address = GAddress_new();
f4ada568
GL
68}
69
1539c2f5 70wxSockAddress::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 78wxSockAddress::~wxSockAddress()
f4ada568 79{
a324a7bc 80 GAddress_destroy(m_address);
f4ada568
GL
81}
82
a324a7bc 83void wxSockAddress::SetAddress(GAddress *address)
f4ada568 84{
fccb65a2
VZ
85 if ( address != m_address )
86 {
87 GAddress_destroy(m_address);
88 m_address = GAddress_copy(address);
89 }
f4ada568
GL
90}
91
1539c2f5 92wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
f4ada568 93{
a324a7bc
GL
94 SetAddress(addr.GetAddress());
95 return *this;
f4ada568
GL
96}
97
a324a7bc 98void wxSockAddress::Clear()
acd15a3f 99{
a324a7bc
GL
100 GAddress_destroy(m_address);
101 m_address = GAddress_new();
f4ada568 102}
f4ada568 103
be4bd463
JS
104// ---------------------------------------------------------------------------
105// wxIPaddress
106// ---------------------------------------------------------------------------
107
108wxIPaddress::wxIPaddress()
109 : wxSockAddress()
110{
111}
112
113wxIPaddress::wxIPaddress(const wxIPaddress& other)
114 : wxSockAddress(other)
115{
116}
117
118wxIPaddress::~wxIPaddress()
119{
120}
121
a324a7bc
GL
122// ---------------------------------------------------------------------------
123// wxIPV4address
124// ---------------------------------------------------------------------------
125
126wxIPV4address::wxIPV4address()
be4bd463 127 : wxIPaddress()
1539c2f5
VZ
128{
129}
130
131wxIPV4address::wxIPV4address(const wxIPV4address& other)
be4bd463 132 : wxIPaddress(other)
f4ada568 133{
a324a7bc 134}
f4ada568 135
a324a7bc
GL
136wxIPV4address::~wxIPV4address()
137{
138}
f4ada568 139
a324a7bc
GL
140bool wxIPV4address::Hostname(const wxString& name)
141{
f61815af 142 // Some people are sometimes fool.
525d8583 143 if (name.empty())
58c837a4
RR
144 {
145 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
d775fa82 146 return false;
f61815af 147 }
ce22d615 148 m_origHostname = name;
f6bcfd97 149 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
150}
151
152bool wxIPV4address::Hostname(unsigned long addr)
153{
ce22d615
RD
154 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
155 if (rv)
156 m_origHostname = Hostname();
157 else
2b5f62a0 158 m_origHostname = wxEmptyString;
ce22d615 159 return rv;
f4ada568
GL
160}
161
162bool wxIPV4address::Service(const wxString& name)
163{
f6bcfd97 164 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
165}
166
167bool wxIPV4address::Service(unsigned short port)
168{
a324a7bc 169 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
170}
171
172bool wxIPV4address::LocalHost()
173{
a324a7bc 174 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
175}
176
be4bd463
JS
177bool wxIPV4address::IsLocalHost() const
178{
179 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
180}
181
d3ea6527
GRG
182bool wxIPV4address::AnyAddress()
183{
184 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
185}
186
be4bd463 187wxString wxIPV4address::Hostname() const
f4ada568 188{
a324a7bc 189 char hostname[1024];
f4ada568 190
a324a7bc
GL
191 hostname[0] = 0;
192 GAddress_INET_GetHostName(m_address, hostname, 1024);
2b5f62a0 193 return wxString::FromAscii(hostname);
f4ada568
GL
194}
195
be4bd463 196unsigned short wxIPV4address::Service() const
f4ada568 197{
acd15a3f 198 return GAddress_INET_GetPort(m_address);
f4ada568
GL
199}
200
ce22d615
RD
201wxSockAddress *wxIPV4address::Clone() const
202{
203 wxIPV4address *addr = new wxIPV4address(*this);
204 addr->m_origHostname = m_origHostname;
205 return addr;
206}
207
d9552598 208wxString wxIPV4address::IPAddress() const
d775fa82
WS
209{
210 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
17a1ebd1
VZ
211 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
212 (raw>>24) & 0xff,
213 (raw>>16) & 0xff,
214 (raw>>8) & 0xff,
215 raw & 0xff
d775fa82 216 );
d9552598
VZ
217}
218
fbfb8bcc 219bool wxIPV4address::operator==(const wxIPV4address& addr) const
1d6d8b5d 220{
17a1ebd1
VZ
221 return Hostname().Cmp(addr.Hostname().c_str()) == 0 &&
222 Service() == addr.Service();
1d6d8b5d
JS
223}
224
be4bd463 225#if wxUSE_IPV6
a324a7bc
GL
226// ---------------------------------------------------------------------------
227// wxIPV6address
228// ---------------------------------------------------------------------------
f4ada568
GL
229
230wxIPV6address::wxIPV6address()
be4bd463 231 : wxIPaddress()
f4ada568 232{
f4ada568
GL
233}
234
1539c2f5 235wxIPV6address::wxIPV6address(const wxIPV6address& other)
be4bd463 236 : wxIPaddress(other)
1539c2f5
VZ
237{
238}
239
f4ada568
GL
240wxIPV6address::~wxIPV6address()
241{
242}
243
f4ada568
GL
244bool wxIPV6address::Hostname(const wxString& name)
245{
525d8583 246 if (name.empty())
be4bd463
JS
247 {
248 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
d775fa82 249 return false;
be4bd463
JS
250 }
251 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
252}
253
be4bd463 254bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr))
f4ada568 255{
d775fa82 256 return true;
f4ada568
GL
257}
258
be4bd463 259bool wxIPV6address::Service(const wxString& name)
f4ada568 260{
be4bd463 261 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
262}
263
264bool wxIPV6address::Service(unsigned short port)
265{
a324a7bc 266 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
267}
268
269bool wxIPV6address::LocalHost()
270{
a324a7bc 271 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
272}
273
be4bd463
JS
274bool wxIPV6address::IsLocalHost() const
275{
276 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
277}
278
279bool wxIPV6address::AnyAddress()
280{
281 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
282}
283
284wxString wxIPV6address::IPAddress() const
d775fa82
WS
285{
286 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
287 return wxString::Format(
288 _T("%u.%u.%u.%u"),
289 (unsigned char)((raw>>24) & 0xff),
290 (unsigned char)((raw>>16) & 0xff),
291 (unsigned char)((raw>>8) & 0xff),
292 (unsigned char)(raw & 0xff)
293 );
be4bd463
JS
294}
295
296wxString wxIPV6address::Hostname() const
f4ada568 297{
be4bd463
JS
298 char hostname[1024];
299
300 hostname[0] = 0;
301 GAddress_INET_GetHostName(m_address, hostname, 1024);
302 return wxString::FromAscii(hostname);
f4ada568
GL
303}
304
be4bd463 305unsigned short wxIPV6address::Service() const
f4ada568 306{
acd15a3f 307 return GAddress_INET_GetPort(m_address);
f4ada568
GL
308}
309
be4bd463 310#endif // wxUSE_IPV6
f4ada568 311
97c153e4 312#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
1539c2f5 313
a324a7bc
GL
314// ---------------------------------------------------------------------------
315// wxUNIXaddress
316// ---------------------------------------------------------------------------
f4ada568
GL
317
318wxUNIXaddress::wxUNIXaddress()
1539c2f5
VZ
319 : wxSockAddress()
320{
321}
322
323wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
324 : wxSockAddress(other)
f4ada568 325{
f4ada568
GL
326}
327
328wxUNIXaddress::~wxUNIXaddress()
329{
330}
331
f4ada568
GL
332void wxUNIXaddress::Filename(const wxString& fname)
333{
a324a7bc 334 GAddress_UNIX_SetPath(m_address, fname.fn_str());
f4ada568
GL
335}
336
337wxString wxUNIXaddress::Filename()
338{
a324a7bc 339 char path[1024];
f4ada568 340
a324a7bc
GL
341 path[0] = 0;
342 GAddress_UNIX_GetPath(m_address, path, 1024);
d775fa82 343
2b5f62a0 344 return wxString::FromAscii(path);
f4ada568
GL
345}
346
1539c2f5 347#endif // __UNIX__
35a4dab7 348
acd15a3f 349#endif
35a4dab7 350 // wxUSE_SOCKETS