]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
Added size event for status bar
[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());
2a29700e
RR
198
199 if (!h_ent)
200 return wxString("");
201 else
202 return wxString(h_ent->h_name);
f4ada568
GL
203}
204
205unsigned short wxIPV4address::Service()
206{
207 return ntohs(m_addr->sin_port);
208}
209
210void wxIPV4address::Build(struct sockaddr *&addr, size_t& len)
211{
212 addr = (struct sockaddr *)m_addr;
213 len = sizeof(*m_addr);
214}
215
216void wxIPV4address::Disassemble(struct sockaddr *addr, size_t len)
217{
218 if (len != sizeof(*m_addr))
219 return;
220 *m_addr = *(struct sockaddr_in *)addr;
221}
222
223#ifdef IPV6_ENABLE
224
225wxIPV6address::wxIPV6address()
226{
227 m_addr = new sockaddr_in6;
228 Clear();
229}
230
231wxIPV6address::~wxIPV6address()
232{
233}
234
235int wxIPV6address::SockAddrLen()
236{
237 return sizeof(*m_addr);
238}
239
240int wxIPV6address::GetFamily()
241{
242 return AF_INET6;
243}
244
245void wxIPV6address::Clear()
246{
247 memset(m_addr, 0, sizeof(*m_addr));
248 m_addr->sin6_family = AF_INET6;
249 m_addr->sin6_addr.s_addr = INADDR_ANY;
250}
251
252/*
253const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
254{
255 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
256
257 CHECK_ADDRTYPE(addr, wxIPV6address);
258 m_addr = ip_addr->m_addr;
259 return *this;
260}
261*/
262
263bool wxIPV6address::Hostname(const wxString& name)
264{
ce3ed50d 265 struct hostent *theHostent;
f4ada568
GL
266 struct in_addr *addr;
267
268 if (name.IsNull())
269 return FALSE;
270
271 if (!name.IsNumber()) {
272 hostent = gethostbyname2((char*) name, AF_INET6);
ce3ed50d 273 if (!theHostent)
f4ada568
GL
274 return FALSE;
275 } else {
276 // Don't how to do
277 return FALSE;
278 }
279
ce3ed50d 280 addr = (struct in6_addr *) *(theHostent->h_addr_list);
f4ada568
GL
281
282 m_addr->sin6_addr.s6_addr = addr[0].s6_addr;
283 return TRUE;
284}
285
286bool wxIPV6address::Hostname(unsigned char addr[16])
287{
288 m_addr->sin6_addr.s6_addr = addr;
289 return TRUE;
290}
291
292bool wxIPV6address::Service(const char *name)
293{
ce3ed50d 294 struct servent *theServent;
f4ada568
GL
295
296 if (!name || !strlen(name))
297 return FALSE;
298
299 if (!isdigit(*name)) {
ce3ed50d 300 if ((theServent = getservbyname((char*) name, "tcp")) == 0)
f4ada568
GL
301 return FALSE;
302 } else {
ce3ed50d 303 if ((theServent = getservbyport(atoi(name), "tcp")) == 0) {
f4ada568
GL
304 m_addr->sin_port = htons(atoi(name));
305 return TRUE;
306 }
307 }
308
ce3ed50d 309 m_addr->sin_port = theServent->s_port;
f4ada568
GL
310 return TRUE;
311}
312
313bool wxIPV6address::Service(unsigned short port)
314{
315 m_addr->sin_port = htons(port);
316 return TRUE;
317}
318
319bool wxIPV6address::LocalHost()
320{
321 static char buf[256];
322
323 if (gethostname(buf, sizeof(buf)) < 0)
324 return Hostname("localhost");
325 else
326 return Hostname(buf);
327}
328
329const wxString& wxIPV6address::Hostname()
330{
331 struct hostent *h_ent;
332
333 h_ent = gethostbyaddr((char *)&(m_addr->sin_addr), sizeof(m_addr->sin_addr),
334 GetFamily());
335 return wxString(h_ent->h_name);
336}
337
338unsigned short wxIPV6address::Service()
339{
340 return ntohs(m_addr->sin_port);
341}
342
343void wxIPV6address::Build(struct sockaddr& *addr, size_t& len)
344{
345 len = sizeof(*m_addr);
346 addr = m_addr;
347}
348
349void wxIPV6address::Disassemble(struct sockaddr& *addr, size_t len)
350{
351 if (len != sizeof(*m_addr))
352 return;
353 *m_addr = *(struct sockaddr_in6 *)addr;
354}
355
356#endif
357
358#ifdef __UNIX__
359#include <sys/un.h>
360
361wxUNIXaddress::wxUNIXaddress()
362{
363 m_addr = new sockaddr_un;
364 Clear();
365}
366
367wxUNIXaddress::~wxUNIXaddress()
368{
369}
370
371int wxUNIXaddress::SockAddrLen()
372{
373 return sizeof(*m_addr);
374}
375
376int wxUNIXaddress::GetFamily()
377{
378 return AF_UNIX;
379}
380
381void wxUNIXaddress::Clear()
382{
383 memset(m_addr, 0, sizeof(m_addr));
384 m_addr->sun_family = AF_UNIX;
385}
386
387/*
388const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
389{
390 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
391 CHECK_ADDRTYPE(addr, wxUNIXaddress);
392 m_addr = unx_addr->m_addr;
393 return *this;
394}
395*/
396
397void wxUNIXaddress::Filename(const wxString& fname)
398{
399 sprintf(m_addr->sun_path, "%s", WXSTRINGCAST fname);
400}
401
402wxString wxUNIXaddress::Filename()
403{
404 return wxString(m_addr->sun_path);
405}
406
407void wxUNIXaddress::Build(struct sockaddr*& addr, size_t& len)
408{
409 addr = (struct sockaddr *)m_addr;
410 len = sizeof(*m_addr);
411}
412
413void wxUNIXaddress::Disassemble(struct sockaddr *addr, size_t len)
414{
415 if (len != sizeof(*m_addr))
416 return;
417 *m_addr = *(struct sockaddr_un *)addr;
418}
419#endif