]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/sckaddr.cpp
Semicolon needed.
[wxWidgets.git] / src / common / sckaddr.cpp
... / ...
CommitLineData
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
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#if wxUSE_SOCKETS
20
21#ifndef WX_PRECOMP
22 #include "wx/defs.h"
23 #include "wx/object.h"
24 #include "wx/log.h"
25 #include "wx/intl.h"
26
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
35
36#include "wx/gsocket.h"
37#include "wx/socket.h"
38#include "wx/sckaddr.h"
39
40IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
41IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress)
42IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress)
43#if wxUSE_IPV6
44IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress)
45#endif
46#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
47IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
48#endif
49
50// ---------------------------------------------------------------------------
51// wxSockAddress
52// ---------------------------------------------------------------------------
53
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
63wxSockAddress::wxSockAddress()
64{
65 Init();
66
67 m_address = GAddress_new();
68}
69
70wxSockAddress::wxSockAddress(const wxSockAddress& other)
71 : wxObject()
72{
73 Init();
74
75 m_address = GAddress_copy(other.m_address);
76}
77
78wxSockAddress::~wxSockAddress()
79{
80 GAddress_destroy(m_address);
81}
82
83void wxSockAddress::SetAddress(GAddress *address)
84{
85 if ( address != m_address )
86 {
87 GAddress_destroy(m_address);
88 m_address = GAddress_copy(address);
89 }
90}
91
92wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
93{
94 SetAddress(addr.GetAddress());
95 return *this;
96}
97
98void wxSockAddress::Clear()
99{
100 GAddress_destroy(m_address);
101 m_address = GAddress_new();
102}
103
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
122// ---------------------------------------------------------------------------
123// wxIPV4address
124// ---------------------------------------------------------------------------
125
126wxIPV4address::wxIPV4address()
127 : wxIPaddress()
128{
129}
130
131wxIPV4address::wxIPV4address(const wxIPV4address& other)
132 : wxIPaddress(other)
133{
134}
135
136wxIPV4address::~wxIPV4address()
137{
138}
139
140bool wxIPV4address::Hostname(const wxString& name)
141{
142 // Some people are sometimes fool.
143 if (name.empty())
144 {
145 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
146 return false;
147 }
148 m_origHostname = name;
149 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
150}
151
152bool wxIPV4address::Hostname(unsigned long addr)
153{
154 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
155 if (rv)
156 m_origHostname = Hostname();
157 else
158 m_origHostname = wxEmptyString;
159 return rv;
160}
161
162bool wxIPV4address::Service(const wxString& name)
163{
164 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
165}
166
167bool wxIPV4address::Service(unsigned short port)
168{
169 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
170}
171
172bool wxIPV4address::LocalHost()
173{
174 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
175}
176
177bool wxIPV4address::IsLocalHost() const
178{
179 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
180}
181
182bool wxIPV4address::AnyAddress()
183{
184 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
185}
186
187wxString wxIPV4address::Hostname() const
188{
189 char hostname[1024];
190
191 hostname[0] = 0;
192 GAddress_INET_GetHostName(m_address, hostname, 1024);
193 return wxString::FromAscii(hostname);
194}
195
196unsigned short wxIPV4address::Service() const
197{
198 return GAddress_INET_GetPort(m_address);
199}
200
201wxSockAddress *wxIPV4address::Clone() const
202{
203 wxIPV4address *addr = new wxIPV4address(*this);
204 addr->m_origHostname = m_origHostname;
205 return addr;
206}
207
208wxString wxIPV4address::IPAddress() const
209{
210 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
211 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
212 (raw>>24) & 0xff,
213 (raw>>16) & 0xff,
214 (raw>>8) & 0xff,
215 raw & 0xff
216 );
217}
218
219bool wxIPV4address::operator==(const wxIPV4address& addr) const
220{
221 return Hostname().Cmp(addr.Hostname().c_str()) == 0 &&
222 Service() == addr.Service();
223}
224
225#if wxUSE_IPV6
226// ---------------------------------------------------------------------------
227// wxIPV6address
228// ---------------------------------------------------------------------------
229
230wxIPV6address::wxIPV6address()
231 : wxIPaddress()
232{
233}
234
235wxIPV6address::wxIPV6address(const wxIPV6address& other)
236 : wxIPaddress(other)
237{
238}
239
240wxIPV6address::~wxIPV6address()
241{
242}
243
244bool wxIPV6address::Hostname(const wxString& name)
245{
246 if (name.empty())
247 {
248 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
249 return false;
250 }
251 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
252}
253
254bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr))
255{
256 return true;
257}
258
259bool wxIPV6address::Service(const wxString& name)
260{
261 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
262}
263
264bool wxIPV6address::Service(unsigned short port)
265{
266 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
267}
268
269bool wxIPV6address::LocalHost()
270{
271 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
272}
273
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
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 );
294}
295
296wxString wxIPV6address::Hostname() const
297{
298 char hostname[1024];
299
300 hostname[0] = 0;
301 GAddress_INET_GetHostName(m_address, hostname, 1024);
302 return wxString::FromAscii(hostname);
303}
304
305unsigned short wxIPV6address::Service() const
306{
307 return GAddress_INET_GetPort(m_address);
308}
309
310#endif // wxUSE_IPV6
311
312#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
313
314// ---------------------------------------------------------------------------
315// wxUNIXaddress
316// ---------------------------------------------------------------------------
317
318wxUNIXaddress::wxUNIXaddress()
319 : wxSockAddress()
320{
321}
322
323wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
324 : wxSockAddress(other)
325{
326}
327
328wxUNIXaddress::~wxUNIXaddress()
329{
330}
331
332void wxUNIXaddress::Filename(const wxString& fname)
333{
334 GAddress_UNIX_SetPath(m_address, fname.fn_str());
335}
336
337wxString wxUNIXaddress::Filename()
338{
339 char path[1024];
340
341 path[0] = 0;
342 GAddress_UNIX_GetPath(m_address, path, 1024);
343
344 return wxString::FromAscii(path);
345}
346
347#endif // __UNIX__
348
349#endif
350 // wxUSE_SOCKETS