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