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