]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/sckaddr.cpp
Right-o, wxConv_local does cause some weird crashes, but things
[wxWidgets.git] / src / common / sckaddr.cpp
... / ...
CommitLineData
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
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#if wxUSE_SOCKETS
24
25#ifndef WX_PRECOMP
26#endif
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <ctype.h>
31
32#if !defined(__MWERKS__) && !defined(__SALFORDC__)
33#include <memory.h>
34#endif
35
36#include "wx/defs.h"
37#include "wx/object.h"
38
39#if defined(__WXMAC__)
40#include "/wx/mac/macsock.h"
41#endif
42
43#if defined(__WINDOWS__)
44#include <winsock.h>
45#endif // __WINDOWS__
46
47#if defined(__UNIX__)
48#ifdef VMS
49#include <socket.h>
50#include <in.h>
51#else
52#include <sys/types.h>
53#include <sys/socket.h>
54#include <netinet/in.h>
55#include <arpa/inet.h>
56#endif
57#include <unistd.h>
58#include <netdb.h>
59
60#ifdef __SUN__
61extern "C"
62{
63 struct hostent *gethostbyname(const char *name);
64 int gethostname(char *name, int namelen);
65};
66#endif
67
68#endif // __UNIX__
69
70#include "wx/sckaddr.h"
71
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()
86{
87 m_addr = new sockaddr_in;
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{
124 struct hostent *theHostent;
125 struct in_addr *addr;
126
127 if (name.IsNull())
128 return FALSE;
129
130 if (!name.IsNumber()) {
131 if ((theHostent = gethostbyname(name.fn_str())) == 0) {
132 return FALSE;
133 }
134 } else {
135#ifdef __WXMAC__
136 long len_addr = inet_addr(name.fn_str()).s_addr ;
137#else
138 long len_addr = inet_addr(name.fn_str());
139#endif
140 if (len_addr == -1)
141 return FALSE;
142 m_addr->sin_addr.s_addr = len_addr;
143 return TRUE;
144 }
145
146 addr = (struct in_addr *) *(theHostent->h_addr_list);
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{
160 struct servent *theServent;
161
162 if (name.IsNull())
163 return FALSE;
164
165 if (!name.IsNumber()) {
166 if ((theServent = getservbyname(name.fn_str(), "tcp")) == 0)
167 return FALSE;
168 } else {
169 if ((theServent = getservbyport(wxAtoi(name), "tcp")) == 0) {
170 m_addr->sin_port = htons(wxAtoi(name));
171 return TRUE;
172 }
173 }
174
175 m_addr->sin_port = theServent->s_port;
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
202 if (!h_ent)
203 return wxString("");
204 else
205 return wxString(h_ent->h_name);
206}
207
208unsigned short wxIPV4address::Service()
209{
210 return ntohs(m_addr->sin_port);
211}
212
213void wxIPV4address::Build(struct sockaddr *&addr, size_t& len)
214{
215 addr = (struct sockaddr *)m_addr;
216 len = sizeof(*m_addr);
217}
218
219void wxIPV4address::Disassemble(struct sockaddr *addr, size_t len)
220{
221 if (len != sizeof(*m_addr))
222 return;
223 *m_addr = *(struct sockaddr_in *)addr;
224}
225
226#ifdef IPV6_ENABLE
227
228wxIPV6address::wxIPV6address()
229{
230 m_addr = new sockaddr_in6;
231 Clear();
232}
233
234wxIPV6address::~wxIPV6address()
235{
236}
237
238int wxIPV6address::SockAddrLen()
239{
240 return sizeof(*m_addr);
241}
242
243int wxIPV6address::GetFamily()
244{
245 return AF_INET6;
246}
247
248void wxIPV6address::Clear()
249{
250 memset(m_addr, 0, sizeof(*m_addr));
251 m_addr->sin6_family = AF_INET6;
252 m_addr->sin6_addr.s_addr = INADDR_ANY;
253}
254
255/*
256const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
257{
258 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
259
260 CHECK_ADDRTYPE(addr, wxIPV6address);
261 m_addr = ip_addr->m_addr;
262 return *this;
263}
264*/
265
266bool wxIPV6address::Hostname(const wxString& name)
267{
268 struct hostent *theHostent;
269 struct in_addr *addr;
270
271 if (name.IsNull())
272 return FALSE;
273
274 if (!name.IsNumber()) {
275 hostent = gethostbyname2((char*) name, AF_INET6);
276 if (!theHostent)
277 return FALSE;
278 } else {
279 // Don't how to do
280 return FALSE;
281 }
282
283 addr = (struct in6_addr *) *(theHostent->h_addr_list);
284
285 m_addr->sin6_addr.s6_addr = addr[0].s6_addr;
286 return TRUE;
287}
288
289bool wxIPV6address::Hostname(unsigned char addr[16])
290{
291 m_addr->sin6_addr.s6_addr = addr;
292 return TRUE;
293}
294
295bool wxIPV6address::Service(const char *name)
296{
297 struct servent *theServent;
298
299 if (!name || !strlen(name))
300 return FALSE;
301
302 if (!isdigit(*name)) {
303 if ((theServent = getservbyname((char*) name, "tcp")) == 0)
304 return FALSE;
305 } else {
306 if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
307 m_addr->sin_port = htons(atoi(name));
308 return TRUE;
309 }
310 }
311
312 m_addr->sin_port = theServent->s_port;
313 return TRUE;
314}
315
316bool wxIPV6address::Service(unsigned short port)
317{
318 m_addr->sin_port = htons(port);
319 return TRUE;
320}
321
322bool wxIPV6address::LocalHost()
323{
324 static char buf[256];
325
326 if (gethostname(buf, sizeof(buf)) < 0)
327 return Hostname("localhost");
328 else
329 return Hostname(buf);
330}
331
332const wxString& wxIPV6address::Hostname()
333{
334 struct hostent *h_ent;
335
336 h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
337 GetFamily());
338 return wxString(h_ent->h_name);
339}
340
341unsigned short wxIPV6address::Service()
342{
343 return ntohs(m_addr->sin_port);
344}
345
346void wxIPV6address::Build(struct sockaddr& *addr, size_t& len)
347{
348 len = sizeof(*m_addr);
349 addr = m_addr;
350}
351
352void wxIPV6address::Disassemble(struct sockaddr& *addr, size_t len)
353{
354 if (len != sizeof(*m_addr))
355 return;
356 *m_addr = *(struct sockaddr_in6 *)addr;
357}
358
359#endif
360
361#ifdef __UNIX__
362#include <sys/un.h>
363
364wxUNIXaddress::wxUNIXaddress()
365{
366 m_addr = new sockaddr_un;
367 Clear();
368}
369
370wxUNIXaddress::~wxUNIXaddress()
371{
372}
373
374int wxUNIXaddress::SockAddrLen()
375{
376 return sizeof(*m_addr);
377}
378
379int wxUNIXaddress::GetFamily()
380{
381 return AF_UNIX;
382}
383
384void wxUNIXaddress::Clear()
385{
386 memset(m_addr, 0, sizeof(m_addr));
387 m_addr->sun_family = AF_UNIX;
388}
389
390/*
391const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
392{
393 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
394 CHECK_ADDRTYPE(addr, wxUNIXaddress);
395 m_addr = unx_addr->m_addr;
396 return *this;
397}
398*/
399
400void wxUNIXaddress::Filename(const wxString& fname)
401{
402 sprintf(m_addr->sun_path, "%s", MBSTRINGCAST fname.mb_str());
403}
404
405wxString wxUNIXaddress::Filename()
406{
407 return wxString(m_addr->sun_path);
408}
409
410void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len)
411{
412 addr = (struct sockaddr *)m_addr;
413 len = sizeof(*m_addr);
414}
415
416void wxUNIXaddress::Disassemble(struct sockaddr *addr, size_t len)
417{
418 if (len != sizeof(*m_addr))
419 return;
420 *m_addr = *(struct sockaddr_un *)addr;
421}
422#endif
423
424#endif
425 // wxUSE_SOCKETS