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