]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
8f667937dc82057736a9f2725ed021c94622e437
[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_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
46 #ifdef ENABLE_IPV6
47 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
48 #endif
49 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
50 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
51 #endif
52
53 // ---------------------------------------------------------------------------
54 // wxIPV4address
55 // ---------------------------------------------------------------------------
56
57 void wxSockAddress::Init()
58 {
59 if ( !wxSocketBase::IsInitialized() )
60 {
61 // we must do it before using GAddress_XXX functions
62 (void)wxSocketBase::Initialize();
63 }
64 }
65
66 wxSockAddress::wxSockAddress()
67 {
68 Init();
69
70 m_address = GAddress_new();
71 }
72
73 wxSockAddress::wxSockAddress(const wxSockAddress& other)
74 : wxObject()
75 {
76 Init();
77
78 m_address = GAddress_copy(other.m_address);
79 }
80
81 wxSockAddress::~wxSockAddress()
82 {
83 GAddress_destroy(m_address);
84 }
85
86 void wxSockAddress::SetAddress(GAddress *address)
87 {
88 GAddress_destroy(m_address);
89 m_address = GAddress_copy(address);
90 }
91
92 wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
93 {
94 SetAddress(addr.GetAddress());
95 return *this;
96 }
97
98 void wxSockAddress::Clear()
99 {
100 GAddress_destroy(m_address);
101 m_address = GAddress_new();
102 }
103
104 // ---------------------------------------------------------------------------
105 // wxIPV4address
106 // ---------------------------------------------------------------------------
107
108 wxIPV4address::wxIPV4address()
109 {
110 }
111
112 wxIPV4address::wxIPV4address(const wxIPV4address& other)
113 : wxSockAddress(other)
114 {
115 }
116
117 wxIPV4address::~wxIPV4address()
118 {
119 }
120
121 bool wxIPV4address::Hostname(const wxString& name)
122 {
123 // Some people are sometimes fool.
124 if (name == wxT(""))
125 {
126 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
127 return FALSE;
128 }
129 m_origHostname = name;
130 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
131 }
132
133 bool wxIPV4address::Hostname(unsigned long addr)
134 {
135 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
136 if (rv)
137 m_origHostname = Hostname();
138 else
139 m_origHostname = wxEmptyString;
140 return rv;
141 }
142
143 bool wxIPV4address::Service(const wxString& name)
144 {
145 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
146 }
147
148 bool wxIPV4address::Service(unsigned short port)
149 {
150 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
151 }
152
153 bool wxIPV4address::LocalHost()
154 {
155 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
156 }
157
158 bool wxIPV4address::AnyAddress()
159 {
160 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
161 }
162
163 wxString wxIPV4address::Hostname()
164 {
165 char hostname[1024];
166
167 hostname[0] = 0;
168 GAddress_INET_GetHostName(m_address, hostname, 1024);
169 return wxString::FromAscii(hostname);
170 }
171
172 unsigned short wxIPV4address::Service()
173 {
174 return GAddress_INET_GetPort(m_address);
175 }
176
177 wxSockAddress *wxIPV4address::Clone() const
178 {
179 wxIPV4address *addr = new wxIPV4address(*this);
180 addr->m_origHostname = m_origHostname;
181 return addr;
182 }
183
184 wxString wxIPV4address::IPAddress() const
185 {
186 unsigned long raw = GAddress_INET_GetHostAddress(m_address);
187 return wxString::Format(
188 _T("%u.%u.%u.%u"),
189 (unsigned char)(raw & 0xff),
190 (unsigned char)((raw>>8) & 0xff),
191 (unsigned char)((raw>>16) & 0xff),
192 (unsigned char)((raw>>24) & 0xff)
193 );
194 }
195
196 bool wxIPV4address::operator==(wxIPV4address& addr)
197 {
198 if(Hostname().Cmp(addr.Hostname().c_str()) == 0 && Service() == addr.Service()) return true;
199 return false;
200 }
201
202 #if 0
203 // ---------------------------------------------------------------------------
204 // wxIPV6address
205 // ---------------------------------------------------------------------------
206
207 wxIPV6address::wxIPV6address()
208 : wxSockAddress()
209 {
210 }
211
212 wxIPV6address::wxIPV6address(const wxIPV6address& other)
213 : wxSockAddress(other)
214 {
215 }
216
217 wxIPV6address::~wxIPV6address()
218 {
219 }
220
221 bool wxIPV6address::Hostname(const wxString& name)
222 {
223 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
224 }
225
226 bool wxIPV6address::Hostname(unsigned char addr[16])
227 {
228 return TRUE;
229 }
230
231 bool wxIPV6address::Service(const char *name)
232 {
233 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
234 }
235
236 bool wxIPV6address::Service(unsigned short port)
237 {
238 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
239 }
240
241 bool wxIPV6address::LocalHost()
242 {
243 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
244 }
245
246 const wxString& wxIPV6address::Hostname()
247 {
248 return wxString(GAddress_INET_GetHostName(m_address));
249 }
250
251 unsigned short wxIPV6address::Service()
252 {
253 return GAddress_INET_GetPort(m_address);
254 }
255
256 #endif // 0
257
258 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
259
260 // ---------------------------------------------------------------------------
261 // wxUNIXaddress
262 // ---------------------------------------------------------------------------
263
264 wxUNIXaddress::wxUNIXaddress()
265 : wxSockAddress()
266 {
267 }
268
269 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
270 : wxSockAddress(other)
271 {
272 }
273
274 wxUNIXaddress::~wxUNIXaddress()
275 {
276 }
277
278 void wxUNIXaddress::Filename(const wxString& fname)
279 {
280 GAddress_UNIX_SetPath(m_address, fname.fn_str());
281 }
282
283 wxString wxUNIXaddress::Filename()
284 {
285 char path[1024];
286
287 path[0] = 0;
288 GAddress_UNIX_GetPath(m_address, path, 1024);
289
290 return wxString::FromAscii(path);
291 }
292
293 #endif // __UNIX__
294
295 #endif
296 // wxUSE_SOCKETS