]> git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
More compilation fixes for Solaris. :-<
[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 #ifndef WX_PRECOMP
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29
30 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
31 #include <memory.h>
32 #endif
33
34 #include "wx/defs.h"
35 #include "wx/object.h"
36
37 #if defined(__WXMAC__)
38 #include "/wx/mac/macsock.h"
39 #endif
40
41 #if defined(__WINDOWS__)
42 #include <winsock.h>
43 #endif // __WINDOWS__
44
45 #if defined(__UNIX__)
46 #ifdef VMS
47 #include <socket.h>
48 #include <in.h>
49 #else
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #endif
55 #include <unistd.h>
56 #include <netdb.h>
57
58 #ifdef __SUN__
59 extern "C"
60 {
61 struct hostent *gethostbyname(const char *name);
62 };
63 #endif
64
65 #endif // __UNIX__
66
67 #include "wx/sckaddr.h"
68
69 #define CHECK_ADDRTYPE(var, type)
70
71 #if !USE_SHARED_LIBRARY
72 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
73 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
74 #ifdef ENABLE_IPV6
75 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
76 #endif
77 #ifdef __UNIX__
78 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
79 #endif
80 #endif
81
82 wxIPV4address::wxIPV4address()
83 {
84 m_addr = new sockaddr_in;
85 Clear();
86 }
87
88 wxIPV4address::~wxIPV4address()
89 {
90 }
91
92 int wxIPV4address::SockAddrLen()
93 {
94 return sizeof(*m_addr);
95 }
96
97 int wxIPV4address::GetFamily()
98 {
99 return AF_INET;
100 }
101
102 void wxIPV4address::Clear()
103 {
104 memset(m_addr, 0, sizeof(*m_addr));
105 m_addr->sin_family = AF_INET;
106 m_addr->sin_addr.s_addr = INADDR_ANY;
107 }
108
109 /*
110 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
111 {
112 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
113 CHECK_ADDRTYPE(addr, wxIPV4address);
114 m_addr = ip_addr->m_addr;
115 return *this;
116 }
117 */
118
119 bool wxIPV4address::Hostname(const wxString& name)
120 {
121 struct hostent *theHostent;
122 struct in_addr *addr;
123
124 if (name.IsNull())
125 return FALSE;
126
127 if (!name.IsNumber()) {
128 if ((theHostent = gethostbyname(name.GetData())) == 0) {
129 return FALSE;
130 }
131 } else {
132 #ifdef __WXMAC__
133 long len_addr = inet_addr(name.GetData()).s_addr ;
134 #else
135 long len_addr = inet_addr(name.GetData());
136 #endif
137 if (len_addr == -1)
138 return FALSE;
139 m_addr->sin_addr.s_addr = len_addr;
140 return TRUE;
141 }
142
143 addr = (struct in_addr *) *(theHostent->h_addr_list);
144
145 m_addr->sin_addr.s_addr = addr[0].s_addr;
146 return TRUE;
147 }
148
149 bool wxIPV4address::Hostname(unsigned long addr)
150 {
151 m_addr->sin_addr.s_addr = htonl(addr);
152 return TRUE;
153 }
154
155 bool wxIPV4address::Service(const wxString& name)
156 {
157 struct servent *theServent;
158
159 if (name.IsNull())
160 return FALSE;
161
162 if (!name.IsNumber()) {
163 if ((theServent = getservbyname(name, "tcp")) == 0)
164 return FALSE;
165 } else {
166 if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
167 m_addr->sin_port = htons(atoi(name));
168 return TRUE;
169 }
170 }
171
172 m_addr->sin_port = theServent->s_port;
173 return TRUE;
174 }
175
176 bool wxIPV4address::Service(unsigned short port)
177 {
178 m_addr->sin_port = htons(port);
179 return TRUE;
180 }
181
182 bool wxIPV4address::LocalHost()
183 {
184 static char buf[256];
185
186 if (gethostname(buf, sizeof(buf)) < 0)
187 return Hostname("localhost");
188 else
189 return Hostname(buf);
190 }
191
192 wxString wxIPV4address::Hostname()
193 {
194 struct hostent *h_ent;
195
196 h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
197 GetFamily());
198 return wxString(h_ent->h_name);
199 }
200
201 unsigned short wxIPV4address::Service()
202 {
203 return ntohs(m_addr->sin_port);
204 }
205
206 void wxIPV4address::Build(struct sockaddr *&addr, size_t& len)
207 {
208 addr = (struct sockaddr *)m_addr;
209 len = sizeof(*m_addr);
210 }
211
212 void wxIPV4address::Disassemble(struct sockaddr *addr, size_t len)
213 {
214 if (len != sizeof(*m_addr))
215 return;
216 *m_addr = *(struct sockaddr_in *)addr;
217 }
218
219 #ifdef IPV6_ENABLE
220
221 wxIPV6address::wxIPV6address()
222 {
223 m_addr = new sockaddr_in6;
224 Clear();
225 }
226
227 wxIPV6address::~wxIPV6address()
228 {
229 }
230
231 int wxIPV6address::SockAddrLen()
232 {
233 return sizeof(*m_addr);
234 }
235
236 int wxIPV6address::GetFamily()
237 {
238 return AF_INET6;
239 }
240
241 void wxIPV6address::Clear()
242 {
243 memset(m_addr, 0, sizeof(*m_addr));
244 m_addr->sin6_family = AF_INET6;
245 m_addr->sin6_addr.s_addr = INADDR_ANY;
246 }
247
248 /*
249 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
250 {
251 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
252
253 CHECK_ADDRTYPE(addr, wxIPV6address);
254 m_addr = ip_addr->m_addr;
255 return *this;
256 }
257 */
258
259 bool wxIPV6address::Hostname(const wxString& name)
260 {
261 struct hostent *theHostent;
262 struct in_addr *addr;
263
264 if (name.IsNull())
265 return FALSE;
266
267 if (!name.IsNumber()) {
268 hostent = gethostbyname2((char*) name, AF_INET6);
269 if (!theHostent)
270 return FALSE;
271 } else {
272 // Don't how to do
273 return FALSE;
274 }
275
276 addr = (struct in6_addr *) *(theHostent->h_addr_list);
277
278 m_addr->sin6_addr.s6_addr = addr[0].s6_addr;
279 return TRUE;
280 }
281
282 bool wxIPV6address::Hostname(unsigned char addr[16])
283 {
284 m_addr->sin6_addr.s6_addr = addr;
285 return TRUE;
286 }
287
288 bool wxIPV6address::Service(const char *name)
289 {
290 struct servent *theServent;
291
292 if (!name || !strlen(name))
293 return FALSE;
294
295 if (!isdigit(*name)) {
296 if ((theServent = getservbyname((char*) name, "tcp")) == 0)
297 return FALSE;
298 } else {
299 if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
300 m_addr->sin_port = htons(atoi(name));
301 return TRUE;
302 }
303 }
304
305 m_addr->sin_port = theServent->s_port;
306 return TRUE;
307 }
308
309 bool wxIPV6address::Service(unsigned short port)
310 {
311 m_addr->sin_port = htons(port);
312 return TRUE;
313 }
314
315 bool wxIPV6address::LocalHost()
316 {
317 static char buf[256];
318
319 if (gethostname(buf, sizeof(buf)) < 0)
320 return Hostname("localhost");
321 else
322 return Hostname(buf);
323 }
324
325 const wxString& wxIPV6address::Hostname()
326 {
327 struct hostent *h_ent;
328
329 h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
330 GetFamily());
331 return wxString(h_ent->h_name);
332 }
333
334 unsigned short wxIPV6address::Service()
335 {
336 return ntohs(m_addr->sin_port);
337 }
338
339 void wxIPV6address::Build(struct sockaddr& *addr, size_t& len)
340 {
341 len = sizeof(*m_addr);
342 addr = m_addr;
343 }
344
345 void wxIPV6address::Disassemble(struct sockaddr& *addr, size_t len)
346 {
347 if (len != sizeof(*m_addr))
348 return;
349 *m_addr = *(struct sockaddr_in6 *)addr;
350 }
351
352 #endif
353
354 #ifdef __UNIX__
355 #include <sys/un.h>
356
357 wxUNIXaddress::wxUNIXaddress()
358 {
359 m_addr = new sockaddr_un;
360 Clear();
361 }
362
363 wxUNIXaddress::~wxUNIXaddress()
364 {
365 }
366
367 int wxUNIXaddress::SockAddrLen()
368 {
369 return sizeof(*m_addr);
370 }
371
372 int wxUNIXaddress::GetFamily()
373 {
374 return AF_UNIX;
375 }
376
377 void wxUNIXaddress::Clear()
378 {
379 memset(m_addr, 0, sizeof(m_addr));
380 m_addr->sun_family = AF_UNIX;
381 }
382
383 /*
384 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
385 {
386 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
387 CHECK_ADDRTYPE(addr, wxUNIXaddress);
388 m_addr = unx_addr->m_addr;
389 return *this;
390 }
391 */
392
393 void wxUNIXaddress::Filename(const wxString& fname)
394 {
395 sprintf(m_addr->sun_path, "%s", WXSTRINGCAST fname);
396 }
397
398 wxString wxUNIXaddress::Filename()
399 {
400 return wxString(m_addr->sun_path);
401 }
402
403 void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len)
404 {
405 addr = (struct sockaddr *)m_addr;
406 len = sizeof(*m_addr);
407 }
408
409 void wxUNIXaddress::Disassemble(struct sockaddr *addr, size_t len)
410 {
411 if (len != sizeof(*m_addr))
412 return;
413 *m_addr = *(struct sockaddr_un *)addr;
414 }
415 #endif