]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
define wxFRAME_FLOAT_ON_PARENT as wxFRAME_TOOL_WINDOW
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
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(__WXMAC__)
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 {
75 Init();
76
77 m_address = GAddress_copy(other.m_address);
78 }
79
80 wxSockAddress::~wxSockAddress()
81 {
82 GAddress_destroy(m_address);
83 }
84
85 void wxSockAddress::SetAddress(GAddress *address)
86 {
87 GAddress_destroy(m_address);
88 m_address = GAddress_copy(address);
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 // wxIPV4address
105 // ---------------------------------------------------------------------------
106
107 wxIPV4address::wxIPV4address()
108 {
109 }
110
111 wxIPV4address::wxIPV4address(const wxIPV4address& other)
112 : wxSockAddress(other)
113 {
114 }
115
116 wxIPV4address::~wxIPV4address()
117 {
118 }
119
120 bool wxIPV4address::Hostname(const wxString& name)
121 {
122 // Some people are sometimes fool.
123 if (name == wxT(""))
124 {
125 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
126 return FALSE;
127 }
128 m_origHostname = name;
129 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
130 }
131
132 bool wxIPV4address::Hostname(unsigned long addr)
133 {
134 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
135 if (rv)
136 m_origHostname = Hostname();
137 else
138 m_origHostname = "";
139 return rv;
140 }
141
142 bool wxIPV4address::Service(const wxString& name)
143 {
144 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
145 }
146
147 bool wxIPV4address::Service(unsigned short port)
148 {
149 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
150 }
151
152 bool wxIPV4address::LocalHost()
153 {
154 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
155 }
156
157 bool wxIPV4address::AnyAddress()
158 {
159 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
160 }
161
162 wxString wxIPV4address::Hostname()
163 {
164 char hostname[1024];
165
166 hostname[0] = 0;
167 GAddress_INET_GetHostName(m_address, hostname, 1024);
168 return wxString(hostname);
169 }
170
171 unsigned short wxIPV4address::Service()
172 {
173 return GAddress_INET_GetPort(m_address);
174 }
175
176 wxSockAddress *wxIPV4address::Clone() const
177 {
178 wxIPV4address *addr = new wxIPV4address(*this);
179 addr->m_origHostname = m_origHostname;
180 return addr;
181 }
182
183 #if 0
184 // ---------------------------------------------------------------------------
185 // wxIPV6address
186 // ---------------------------------------------------------------------------
187
188 wxIPV6address::wxIPV6address()
189 : wxSockAddress()
190 {
191 }
192
193 wxIPV6address::wxIPV6address(const wxIPV6address& other)
194 : wxSockAddress(other)
195 {
196 }
197
198 wxIPV6address::~wxIPV6address()
199 {
200 }
201
202 bool wxIPV6address::Hostname(const wxString& name)
203 {
204 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
205 }
206
207 bool wxIPV6address::Hostname(unsigned char addr[16])
208 {
209 return TRUE;
210 }
211
212 bool wxIPV6address::Service(const char *name)
213 {
214 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
215 }
216
217 bool wxIPV6address::Service(unsigned short port)
218 {
219 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
220 }
221
222 bool wxIPV6address::LocalHost()
223 {
224 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
225 }
226
227 const wxString& wxIPV6address::Hostname()
228 {
229 return wxString(GAddress_INET_GetHostName(m_address));
230 }
231
232 unsigned short wxIPV6address::Service()
233 {
234 return GAddress_INET_GetPort(m_address);
235 }
236
237 #endif // 0
238
239 #if defined(__UNIX__) && !defined(__WXMAC__)
240
241 // ---------------------------------------------------------------------------
242 // wxUNIXaddress
243 // ---------------------------------------------------------------------------
244
245 wxUNIXaddress::wxUNIXaddress()
246 : wxSockAddress()
247 {
248 }
249
250 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
251 : wxSockAddress(other)
252 {
253 }
254
255 wxUNIXaddress::~wxUNIXaddress()
256 {
257 }
258
259 void wxUNIXaddress::Filename(const wxString& fname)
260 {
261 GAddress_UNIX_SetPath(m_address, fname.fn_str());
262 }
263
264 wxString wxUNIXaddress::Filename()
265 {
266 char path[1024];
267
268 path[0] = 0;
269 GAddress_UNIX_GetPath(m_address, path, 1024);
270 return wxString(path);
271 }
272
273 #endif // __UNIX__
274
275 #endif
276 // wxUSE_SOCKETS