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