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