]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
made wxSplitterWindow::SplitXXX() virtual
[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__)
f4ada568
GL
46#ifdef VMS
47#include <socket.h>
48#include <in.h>
49#else
23e09f11 50#include <sys/types.h>
f4ada568
GL
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
24178e4a
KB
58#ifdef __SUN__
59extern "C"
60{
61 struct hostent *gethostbyname(const char *name);
62};
63#endif
64
f4ada568
GL
65#endif // __UNIX__
66
67#include "wx/sckaddr.h"
68
f4ada568
GL
69#define CHECK_ADDRTYPE(var, type)
70
71#if !USE_SHARED_LIBRARY
72IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
73IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
74#ifdef ENABLE_IPV6
75IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
76#endif
77#ifdef __UNIX__
78IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
79#endif
80#endif
81
82wxIPV4address::wxIPV4address()
69919241 83{
1eddcfaf 84 m_addr = new sockaddr_in;
f4ada568
GL
85 Clear();
86}
87
88wxIPV4address::~wxIPV4address()
89{
90}
91
92int wxIPV4address::SockAddrLen()
93{
94 return sizeof(*m_addr);
95}
96
97int wxIPV4address::GetFamily()
98{
99 return AF_INET;
100}
101
102void 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/*
110const 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
119bool wxIPV4address::Hostname(const wxString& name)
120{
ce3ed50d 121 struct hostent *theHostent;
f4ada568
GL
122 struct in_addr *addr;
123
124 if (name.IsNull())
125 return FALSE;
126
127 if (!name.IsNumber()) {
ce3ed50d 128 if ((theHostent = gethostbyname(name.GetData())) == 0) {
f4ada568
GL
129 return FALSE;
130 }
131 } else {
17dff81c
SC
132#ifdef __WXMAC__
133 long len_addr = inet_addr(name.GetData()).s_addr ;
134#else
f4ada568 135 long len_addr = inet_addr(name.GetData());
17dff81c 136#endif
f4ada568
GL
137 if (len_addr == -1)
138 return FALSE;
139 m_addr->sin_addr.s_addr = len_addr;
140 return TRUE;
141 }
142
ce3ed50d 143 addr = (struct in_addr *) *(theHostent->h_addr_list);
f4ada568
GL
144
145 m_addr->sin_addr.s_addr = addr[0].s_addr;
146 return TRUE;
147}
148
149bool wxIPV4address::Hostname(unsigned long addr)
150{
151 m_addr->sin_addr.s_addr = htonl(addr);
152 return TRUE;
153}
154
155bool wxIPV4address::Service(const wxString& name)
156{
ce3ed50d 157 struct servent *theServent;
f4ada568
GL
158
159 if (name.IsNull())
160 return FALSE;
161
162 if (!name.IsNumber()) {
ce3ed50d 163 if ((theServent = getservbyname(name, "tcp")) == 0)
f4ada568
GL
164 return FALSE;
165 } else {
ce3ed50d 166 if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
f4ada568
GL
167 m_addr->sin_port = htons(atoi(name));
168 return TRUE;
169 }
170 }
171
ce3ed50d 172 m_addr->sin_port = theServent->s_port;
f4ada568
GL
173 return TRUE;
174}
175
176bool wxIPV4address::Service(unsigned short port)
177{
178 m_addr->sin_port = htons(port);
179 return TRUE;
180}
181
182bool 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
192wxString 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
201unsigned short wxIPV4address::Service()
202{
203 return ntohs(m_addr->sin_port);
204}
205
206void wxIPV4address::Build(struct sockaddr *&addr, size_t& len)
207{
208 addr = (struct sockaddr *)m_addr;
209 len = sizeof(*m_addr);
210}
211
212void 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
221wxIPV6address::wxIPV6address()
222{
223 m_addr = new sockaddr_in6;
224 Clear();
225}
226
227wxIPV6address::~wxIPV6address()
228{
229}
230
231int wxIPV6address::SockAddrLen()
232{
233 return sizeof(*m_addr);
234}
235
236int wxIPV6address::GetFamily()
237{
238 return AF_INET6;
239}
240
241void 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/*
249const 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
259bool wxIPV6address::Hostname(const wxString& name)
260{
ce3ed50d 261 struct hostent *theHostent;
f4ada568
GL
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);
ce3ed50d 269 if (!theHostent)
f4ada568
GL
270 return FALSE;
271 } else {
272 // Don't how to do
273 return FALSE;
274 }
275
ce3ed50d 276 addr = (struct in6_addr *) *(theHostent->h_addr_list);
f4ada568
GL
277
278 m_addr->sin6_addr.s6_addr = addr[0].s6_addr;
279 return TRUE;
280}
281
282bool wxIPV6address::Hostname(unsigned char addr[16])
283{
284 m_addr->sin6_addr.s6_addr = addr;
285 return TRUE;
286}
287
288bool wxIPV6address::Service(const char *name)
289{
ce3ed50d 290 struct servent *theServent;
f4ada568
GL
291
292 if (!name || !strlen(name))
293 return FALSE;
294
295 if (!isdigit(*name)) {
ce3ed50d 296 if ((theServent = getservbyname((char*) name, "tcp")) == 0)
f4ada568
GL
297 return FALSE;
298 } else {
ce3ed50d 299 if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
f4ada568
GL
300 m_addr->sin_port = htons(atoi(name));
301 return TRUE;
302 }
303 }
304
ce3ed50d 305 m_addr->sin_port = theServent->s_port;
f4ada568
GL
306 return TRUE;
307}
308
309bool wxIPV6address::Service(unsigned short port)
310{
311 m_addr->sin_port = htons(port);
312 return TRUE;
313}
314
315bool 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
325const 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
334unsigned short wxIPV6address::Service()
335{
336 return ntohs(m_addr->sin_port);
337}
338
339void wxIPV6address::Build(struct sockaddr& *addr, size_t& len)
340{
341 len = sizeof(*m_addr);
342 addr = m_addr;
343}
344
345void 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
357wxUNIXaddress::wxUNIXaddress()
358{
359 m_addr = new sockaddr_un;
360 Clear();
361}
362
363wxUNIXaddress::~wxUNIXaddress()
364{
365}
366
367int wxUNIXaddress::SockAddrLen()
368{
369 return sizeof(*m_addr);
370}
371
372int wxUNIXaddress::GetFamily()
373{
374 return AF_UNIX;
375}
376
377void wxUNIXaddress::Clear()
378{
379 memset(m_addr, 0, sizeof(m_addr));
380 m_addr->sun_family = AF_UNIX;
381}
382
383/*
384const 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
393void wxUNIXaddress::Filename(const wxString& fname)
394{
395 sprintf(m_addr->sun_path, "%s", WXSTRINGCAST fname);
396}
397
398wxString wxUNIXaddress::Filename()
399{
400 return wxString(m_addr->sun_path);
401}
402
403void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len)
404{
405 addr = (struct sockaddr *)m_addr;
406 len = sizeof(*m_addr);
407}
408
409void 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