]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
set error to GSOCK_TIMEOUT if the socket timed out (modified and extended patch 1303554)
[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(
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 );
215 }
216
217 bool wxIPV4address::operator==(const wxIPV4address& addr) const
218 {
219 if(Hostname().Cmp(addr.Hostname().c_str()) == 0 && Service() == addr.Service()) return true;
220 return false;
221 }
222
223 #if wxUSE_IPV6
224 // ---------------------------------------------------------------------------
225 // wxIPV6address
226 // ---------------------------------------------------------------------------
227
228 wxIPV6address::wxIPV6address()
229 : wxIPaddress()
230 {
231 }
232
233 wxIPV6address::wxIPV6address(const wxIPV6address& other)
234 : wxIPaddress(other)
235 {
236 }
237
238 wxIPV6address::~wxIPV6address()
239 {
240 }
241
242 bool wxIPV6address::Hostname(const wxString& name)
243 {
244 if (name.empty())
245 {
246 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
247 return false;
248 }
249 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
250 }
251
252 bool wxIPV6address::Hostname(unsigned char[16] WXUNUSED(addr))
253 {
254 return true;
255 }
256
257 bool wxIPV6address::Service(const wxString& name)
258 {
259 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
260 }
261
262 bool wxIPV6address::Service(unsigned short port)
263 {
264 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
265 }
266
267 bool wxIPV6address::LocalHost()
268 {
269 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
270 }
271
272 bool wxIPV6address::IsLocalHost() const
273 {
274 return (Hostname() == wxT("localhost") || IPAddress() == wxT("127.0.0.1"));
275 }
276
277 bool wxIPV6address::AnyAddress()
278 {
279 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
280 }
281
282 wxString wxIPV6address::IPAddress() const
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 );
292 }
293
294 wxString wxIPV6address::Hostname() const
295 {
296 char hostname[1024];
297
298 hostname[0] = 0;
299 GAddress_INET_GetHostName(m_address, hostname, 1024);
300 return wxString::FromAscii(hostname);
301 }
302
303 unsigned short wxIPV6address::Service() const
304 {
305 return GAddress_INET_GetPort(m_address);
306 }
307
308 #endif // wxUSE_IPV6
309
310 #if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
311
312 // ---------------------------------------------------------------------------
313 // wxUNIXaddress
314 // ---------------------------------------------------------------------------
315
316 wxUNIXaddress::wxUNIXaddress()
317 : wxSockAddress()
318 {
319 }
320
321 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
322 : wxSockAddress(other)
323 {
324 }
325
326 wxUNIXaddress::~wxUNIXaddress()
327 {
328 }
329
330 void wxUNIXaddress::Filename(const wxString& fname)
331 {
332 GAddress_UNIX_SetPath(m_address, fname.fn_str());
333 }
334
335 wxString wxUNIXaddress::Filename()
336 {
337 char path[1024];
338
339 path[0] = 0;
340 GAddress_UNIX_GetPath(m_address, path, 1024);
341
342 return wxString::FromAscii(path);
343 }
344
345 #endif // __UNIX__
346
347 #endif
348 // wxUSE_SOCKETS