]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
wxTextCtrk::GetRange() shouldn't crash on out of range request
[wxWidgets.git] / src / common / sckaddr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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/object.h"
23 #include "wx/log.h"
24 #include "wx/intl.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29
30 #if !defined(__MWERKS__)
31 #include <memory.h>
32 #endif
33 #endif // !WX_PRECOMP
34
35 #include "wx/gsocket.h"
36 #include "wx/socket.h"
37 #include "wx/sckaddr.h"
38
39 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
40 IMPLEMENT_ABSTRACT_CLASS(wxIPaddress, wxSockAddress)
41 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxIPaddress)
42 #if wxUSE_IPV6
43 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxIPaddress)
44 #endif
45 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
46 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
47 #endif
48
49 // ---------------------------------------------------------------------------
50 // wxSockAddress
51 // ---------------------------------------------------------------------------
52
53 void 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
62 wxSockAddress::wxSockAddress()
63 {
64 Init();
65
66 m_address = GAddress_new();
67 }
68
69 wxSockAddress::wxSockAddress(const wxSockAddress& other)
70 : wxObject()
71 {
72 Init();
73
74 m_address = GAddress_copy(other.m_address);
75 }
76
77 wxSockAddress::~wxSockAddress()
78 {
79 GAddress_destroy(m_address);
80 }
81
82 void wxSockAddress::SetAddress(GAddress *address)
83 {
84 if ( address != m_address )
85 {
86 GAddress_destroy(m_address);
87 m_address = GAddress_copy(address);
88 }
89 }
90
91 wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
92 {
93 SetAddress(addr.GetAddress());
94 return *this;
95 }
96
97 void wxSockAddress::Clear()
98 {
99 GAddress_destroy(m_address);
100 m_address = GAddress_new();
101 }
102
103 // ---------------------------------------------------------------------------
104 // wxIPaddress
105 // ---------------------------------------------------------------------------
106
107 wxIPaddress::wxIPaddress()
108 : wxSockAddress()
109 {
110 }
111
112 wxIPaddress::wxIPaddress(const wxIPaddress& other)
113 : wxSockAddress(other)
114 {
115 }
116
117 wxIPaddress::~wxIPaddress()
118 {
119 }
120
121 // ---------------------------------------------------------------------------
122 // wxIPV4address
123 // ---------------------------------------------------------------------------
124
125 wxIPV4address::wxIPV4address()
126 : wxIPaddress()
127 {
128 }
129
130 wxIPV4address::wxIPV4address(const wxIPV4address& other)
131 : wxIPaddress(other)
132 {
133 }
134
135 wxIPV4address::~wxIPV4address()
136 {
137 }
138
139 bool wxIPV4address::Hostname(const wxString& name)
140 {
141 // Some people are sometimes fool.
142 if (name.empty())
143 {
144 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
145 return false;
146 }
147 m_origHostname = name;
148 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
149 }
150
151 bool wxIPV4address::Hostname(unsigned long addr)
152 {
153 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
154 if (rv)
155 m_origHostname = Hostname();
156 else
157 m_origHostname = wxEmptyString;
158 return rv;
159 }
160
161 bool wxIPV4address::Service(const wxString& name)
162 {
163 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
164 }
165
166 bool wxIPV4address::Service(unsigned short port)
167 {
168 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
169 }
170
171 bool wxIPV4address::LocalHost()
172 {
173 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
174 }
175
176 bool wxIPV4address::IsLocalHost() const
177 {
178 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
179 }
180
181 bool wxIPV4address::BroadcastAddress()
182 {
183 return (GAddress_INET_SetBroadcastAddress(m_address) == GSOCK_NOERROR);
184 }
185
186 bool wxIPV4address::AnyAddress()
187 {
188 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
189 }
190
191 wxString wxIPV4address::Hostname() const
192 {
193 char hostname[1024];
194
195 hostname[0] = 0;
196 GAddress_INET_GetHostName(m_address, hostname, 1024);
197 return wxString::FromAscii(hostname);
198 }
199
200 unsigned short wxIPV4address::Service() const
201 {
202 return GAddress_INET_GetPort(m_address);
203 }
204
205 wxSockAddress *wxIPV4address::Clone() const
206 {
207 wxIPV4address *addr = new wxIPV4address(*this);
208 addr->m_origHostname = m_origHostname;
209 return addr;
210 }
211
212 wxString wxIPV4address::IPAddress() const
213 {
214 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
215 return wxString::Format(_T("%lu.%lu.%lu.%lu"),
216 (raw>>24) & 0xff,
217 (raw>>16) & 0xff,
218 (raw>>8) & 0xff,
219 raw & 0xff
220 );
221 }
222
223 bool wxIPV4address::operator==(const wxIPV4address& addr) const
224 {
225 return Hostname().Cmp(addr.Hostname()) == 0 &&
226 Service() == addr.Service();
227 }
228
229 #if wxUSE_IPV6
230 // ---------------------------------------------------------------------------
231 // wxIPV6address
232 // ---------------------------------------------------------------------------
233
234 wxIPV6address::wxIPV6address()
235 : wxIPaddress()
236 {
237 }
238
239 wxIPV6address::wxIPV6address(const wxIPV6address& other)
240 : wxIPaddress(other), m_origHostname(other.m_origHostname)
241 {
242 }
243
244 wxIPV6address::~wxIPV6address()
245 {
246 }
247
248 bool wxIPV6address::Hostname(const wxString& name)
249 {
250 if (name.empty())
251 {
252 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
253 return false;
254 }
255 m_origHostname = name;
256 return (GAddress_INET6_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
257 }
258
259 bool wxIPV6address::Hostname(unsigned char addr[16])
260 {
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);
272 }
273
274 bool wxIPV6address::Service(const wxString& name)
275 {
276 return (GAddress_INET6_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
277 }
278
279 bool wxIPV6address::Service(unsigned short port)
280 {
281 return (GAddress_INET6_SetPort(m_address, port) == GSOCK_NOERROR);
282 }
283
284 bool wxIPV6address::LocalHost()
285 {
286 return (GAddress_INET6_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
287 }
288
289 bool wxIPV6address::IsLocalHost() const
290 {
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");
298 }
299
300 bool wxIPV6address::BroadcastAddress()
301 {
302 wxFAIL_MSG( "not implemented" );
303
304 return false;
305 }
306
307 bool wxIPV6address::AnyAddress()
308 {
309 return (GAddress_INET6_SetAnyAddress(m_address) == GSOCK_NOERROR);
310 }
311
312 wxString wxIPV6address::IPAddress() const
313 {
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;
350 }
351
352 wxString wxIPV6address::Hostname() const
353 {
354 char hostname[1024];
355 hostname[0] = 0;
356
357 if ( GAddress_INET6_GetHostName(m_address,
358 hostname,
359 WXSIZEOF(hostname)) != GSOCK_NOERROR )
360 return wxEmptyString;
361
362 return wxString::FromAscii(hostname);
363 }
364
365 unsigned short wxIPV6address::Service() const
366 {
367 return GAddress_INET6_GetPort(m_address);
368 }
369
370 #endif // wxUSE_IPV6
371
372 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__)
373
374 // ---------------------------------------------------------------------------
375 // wxUNIXaddress
376 // ---------------------------------------------------------------------------
377
378 wxUNIXaddress::wxUNIXaddress()
379 : wxSockAddress()
380 {
381 }
382
383 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
384 : wxSockAddress(other)
385 {
386 }
387
388 wxUNIXaddress::~wxUNIXaddress()
389 {
390 }
391
392 void wxUNIXaddress::Filename(const wxString& fname)
393 {
394 GAddress_UNIX_SetPath(m_address, fname.fn_str());
395 }
396
397 wxString wxUNIXaddress::Filename()
398 {
399 char path[1024];
400
401 path[0] = 0;
402 GAddress_UNIX_GetPath(m_address, path, 1024);
403
404 return wxString::FromAscii(path);
405 }
406
407 #endif // __UNIX__
408
409 #endif
410 // wxUSE_SOCKETS