]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
missing const added (patch 1295036)
[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{
a324a7bc
GL
85 GAddress_destroy(m_address);
86 m_address = GAddress_copy(address);
f4ada568
GL
87}
88
1539c2f5 89wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
f4ada568 90{
a324a7bc
GL
91 SetAddress(addr.GetAddress());
92 return *this;
f4ada568
GL
93}
94
a324a7bc 95void 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
105wxIPaddress::wxIPaddress()
106 : wxSockAddress()
107{
108}
109
110wxIPaddress::wxIPaddress(const wxIPaddress& other)
111 : wxSockAddress(other)
112{
113}
114
115wxIPaddress::~wxIPaddress()
116{
117}
118
a324a7bc
GL
119// ---------------------------------------------------------------------------
120// wxIPV4address
121// ---------------------------------------------------------------------------
122
123wxIPV4address::wxIPV4address()
be4bd463 124 : wxIPaddress()
1539c2f5
VZ
125{
126}
127
128wxIPV4address::wxIPV4address(const wxIPV4address& other)
be4bd463 129 : wxIPaddress(other)
f4ada568 130{
a324a7bc 131}
f4ada568 132
a324a7bc
GL
133wxIPV4address::~wxIPV4address()
134{
135}
f4ada568 136
a324a7bc
GL
137bool 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
149bool 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
159bool wxIPV4address::Service(const wxString& name)
160{
f6bcfd97 161 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
162}
163
164bool wxIPV4address::Service(unsigned short port)
165{
a324a7bc 166 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
167}
168
169bool wxIPV4address::LocalHost()
170{
a324a7bc 171 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
172}
173
be4bd463
JS
174bool wxIPV4address::IsLocalHost() const
175{
176 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
177}
178
d3ea6527
GRG
179bool wxIPV4address::AnyAddress()
180{
181 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
182}
183
be4bd463 184wxString 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 193unsigned short wxIPV4address::Service() const
f4ada568 194{
acd15a3f 195 return GAddress_INET_GetPort(m_address);
f4ada568
GL
196}
197
ce22d615
RD
198wxSockAddress *wxIPV4address::Clone() const
199{
200 wxIPV4address *addr = new wxIPV4address(*this);
201 addr->m_origHostname = m_origHostname;
202 return addr;
203}
204
d9552598 205wxString wxIPV4address::IPAddress() const
d775fa82
WS
206{
207 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
208 return wxString::Format(
209 _T("%u.%u.%u.%u"),
210 (unsigned char)((raw>>24) & 0xff),
211 (unsigned char)((raw>>16) & 0xff),
212 (unsigned char)((raw>>8) & 0xff),
213 (unsigned char)(raw & 0xff)
214 );
d9552598
VZ
215}
216
1d6d8b5d
JS
217bool wxIPV4address::operator==(wxIPV4address& addr)
218{
d775fa82
WS
219 if(Hostname().Cmp(addr.Hostname().c_str()) == 0 && Service() == addr.Service()) return true;
220 return false;
1d6d8b5d
JS
221}
222
be4bd463 223#if wxUSE_IPV6
a324a7bc
GL
224// ---------------------------------------------------------------------------
225// wxIPV6address
226// ---------------------------------------------------------------------------
f4ada568
GL
227
228wxIPV6address::wxIPV6address()
be4bd463 229 : wxIPaddress()
f4ada568 230{
f4ada568
GL
231}
232
1539c2f5 233wxIPV6address::wxIPV6address(const wxIPV6address& other)
be4bd463 234 : wxIPaddress(other)
1539c2f5
VZ
235{
236}
237
f4ada568
GL
238wxIPV6address::~wxIPV6address()
239{
240}
241
f4ada568
GL
242bool wxIPV6address::Hostname(const wxString& name)
243{
525d8583 244 if (name.empty())
be4bd463
JS
245 {
246 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
d775fa82 247 return false;
be4bd463
JS
248 }
249 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
250}
251
be4bd463 252bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr))
f4ada568 253{
d775fa82 254 return true;
f4ada568
GL
255}
256
be4bd463 257bool wxIPV6address::Service(const wxString& name)
f4ada568 258{
be4bd463 259 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
260}
261
262bool wxIPV6address::Service(unsigned short port)
263{
a324a7bc 264 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
265}
266
267bool wxIPV6address::LocalHost()
268{
a324a7bc 269 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
270}
271
be4bd463
JS
272bool wxIPV6address::IsLocalHost() const
273{
274 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
275}
276
277bool wxIPV6address::AnyAddress()
278{
279 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
280}
281
282wxString wxIPV6address::IPAddress() const
d775fa82
WS
283{
284 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
285 return wxString::Format(
286 _T("%u.%u.%u.%u"),
287 (unsigned char)((raw>>24) & 0xff),
288 (unsigned char)((raw>>16) & 0xff),
289 (unsigned char)((raw>>8) & 0xff),
290 (unsigned char)(raw & 0xff)
291 );
be4bd463
JS
292}
293
294wxString wxIPV6address::Hostname() const
f4ada568 295{
be4bd463
JS
296 char hostname[1024];
297
298 hostname[0] = 0;
299 GAddress_INET_GetHostName(m_address, hostname, 1024);
300 return wxString::FromAscii(hostname);
f4ada568
GL
301}
302
be4bd463 303unsigned short wxIPV6address::Service() const
f4ada568 304{
acd15a3f 305 return GAddress_INET_GetPort(m_address);
f4ada568
GL
306}
307
be4bd463 308#endif // wxUSE_IPV6
f4ada568 309
97c153e4 310#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
1539c2f5 311
a324a7bc
GL
312// ---------------------------------------------------------------------------
313// wxUNIXaddress
314// ---------------------------------------------------------------------------
f4ada568
GL
315
316wxUNIXaddress::wxUNIXaddress()
1539c2f5
VZ
317 : wxSockAddress()
318{
319}
320
321wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
322 : wxSockAddress(other)
f4ada568 323{
f4ada568
GL
324}
325
326wxUNIXaddress::~wxUNIXaddress()
327{
328}
329
f4ada568
GL
330void wxUNIXaddress::Filename(const wxString& fname)
331{
a324a7bc 332 GAddress_UNIX_SetPath(m_address, fname.fn_str());
f4ada568
GL
333}
334
335wxString wxUNIXaddress::Filename()
336{
a324a7bc 337 char path[1024];
f4ada568 338
a324a7bc
GL
339 path[0] = 0;
340 GAddress_UNIX_GetPath(m_address, path, 1024);
d775fa82 341
2b5f62a0 342 return wxString::FromAscii(path);
f4ada568
GL
343}
344
1539c2f5 345#endif // __UNIX__
35a4dab7 346
acd15a3f 347#endif
35a4dab7 348 // wxUSE_SOCKETS