]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
fixes for big endiand machines (Chris Elliott, patch 520879)
[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 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
30 #include <memory.h>
31 #endif
32
33 #include "wx/defs.h"
34 #include "wx/object.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/gsocket.h"
38 #include "wx/sckaddr.h"
39
40 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
41 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
42 #ifdef ENABLE_IPV6
43 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
44 #endif
45 #if defined(__UNIX__) && !defined(__WXMAC__)
46 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
47 #endif
48
49 // ---------------------------------------------------------------------------
50 // wxIPV4address
51 // ---------------------------------------------------------------------------
52
53 wxSockAddress::wxSockAddress()
54 {
55 m_address = GAddress_new();
56 }
57
58 wxSockAddress::wxSockAddress(const wxSockAddress& other)
59 {
60 m_address = GAddress_copy(other.m_address);
61 }
62
63 wxSockAddress::~wxSockAddress()
64 {
65 GAddress_destroy(m_address);
66 }
67
68 void wxSockAddress::SetAddress(GAddress *address)
69 {
70 GAddress_destroy(m_address);
71 m_address = GAddress_copy(address);
72 }
73
74 wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
75 {
76 SetAddress(addr.GetAddress());
77 return *this;
78 }
79
80 void wxSockAddress::Clear()
81 {
82 GAddress_destroy(m_address);
83 m_address = GAddress_new();
84 }
85
86 // ---------------------------------------------------------------------------
87 // wxIPV4address
88 // ---------------------------------------------------------------------------
89
90 wxIPV4address::wxIPV4address()
91 {
92 }
93
94 wxIPV4address::wxIPV4address(const wxIPV4address& other)
95 : wxSockAddress(other)
96 {
97 }
98
99 wxIPV4address::~wxIPV4address()
100 {
101 }
102
103 bool wxIPV4address::Hostname(const wxString& name)
104 {
105 // Some people are sometimes fool.
106 if (name == wxT(""))
107 {
108 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
109 return FALSE;
110 }
111 m_origHostname = name;
112 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
113 }
114
115 bool wxIPV4address::Hostname(unsigned long addr)
116 {
117 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
118 if (rv)
119 m_origHostname = Hostname();
120 else
121 m_origHostname = "";
122 return rv;
123 }
124
125 bool wxIPV4address::Service(const wxString& name)
126 {
127 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
128 }
129
130 bool wxIPV4address::Service(unsigned short port)
131 {
132 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
133 }
134
135 bool wxIPV4address::LocalHost()
136 {
137 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
138 }
139
140 bool wxIPV4address::AnyAddress()
141 {
142 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
143 }
144
145 wxString wxIPV4address::Hostname()
146 {
147 char hostname[1024];
148
149 hostname[0] = 0;
150 GAddress_INET_GetHostName(m_address, hostname, 1024);
151 return wxString(hostname);
152 }
153
154 unsigned short wxIPV4address::Service()
155 {
156 return GAddress_INET_GetPort(m_address);
157 }
158
159 wxSockAddress *wxIPV4address::Clone() const
160 {
161 wxIPV4address *addr = new wxIPV4address(*this);
162 addr->m_origHostname = m_origHostname;
163 return addr;
164 }
165
166 #if 0
167 // ---------------------------------------------------------------------------
168 // wxIPV6address
169 // ---------------------------------------------------------------------------
170
171 wxIPV6address::wxIPV6address()
172 : wxSockAddress()
173 {
174 }
175
176 wxIPV6address::wxIPV6address(const wxIPV6address& other)
177 : wxSockAddress(other)
178 {
179 }
180
181 wxIPV6address::~wxIPV6address()
182 {
183 }
184
185 bool wxIPV6address::Hostname(const wxString& name)
186 {
187 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
188 }
189
190 bool wxIPV6address::Hostname(unsigned char addr[16])
191 {
192 return TRUE;
193 }
194
195 bool wxIPV6address::Service(const char *name)
196 {
197 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
198 }
199
200 bool wxIPV6address::Service(unsigned short port)
201 {
202 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
203 }
204
205 bool wxIPV6address::LocalHost()
206 {
207 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
208 }
209
210 const wxString& wxIPV6address::Hostname()
211 {
212 return wxString(GAddress_INET_GetHostName(m_address));
213 }
214
215 unsigned short wxIPV6address::Service()
216 {
217 return GAddress_INET_GetPort(m_address);
218 }
219
220 #endif // 0
221
222 #if defined(__UNIX__) && !defined(__WXMAC__)
223
224 // ---------------------------------------------------------------------------
225 // wxUNIXaddress
226 // ---------------------------------------------------------------------------
227
228 wxUNIXaddress::wxUNIXaddress()
229 : wxSockAddress()
230 {
231 }
232
233 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
234 : wxSockAddress(other)
235 {
236 }
237
238 wxUNIXaddress::~wxUNIXaddress()
239 {
240 }
241
242 void wxUNIXaddress::Filename(const wxString& fname)
243 {
244 GAddress_UNIX_SetPath(m_address, fname.fn_str());
245 }
246
247 wxString wxUNIXaddress::Filename()
248 {
249 char path[1024];
250
251 path[0] = 0;
252 GAddress_UNIX_GetPath(m_address, path, 1024);
253 return wxString(path);
254 }
255
256 #endif // __UNIX__
257
258 #endif
259 // wxUSE_SOCKETS