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