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