]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
moving string conversions at one place
[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__
2df7be7f 13 #pragma implementation "sckaddr.h"
f4ada568
GL
14#endif
15
fcc6dddd
JS
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
2df7be7f 20 #pragma hdrstop
fcc6dddd
JS
21#endif
22
35a4dab7
GL
23#if wxUSE_SOCKETS
24
6c0d0845
VZ
25#ifndef WX_PRECOMP
26 #include "wx/defs.h"
27 #include "wx/object.h"
28 #include "wx/log.h"
29 #include "wx/intl.h"
ce3ed50d 30
6c0d0845
VZ
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
36 #include <memory.h>
37 #endif
38#endif // !WX_PRECOMP
f4ada568 39
3096bd2f 40#include "wx/gsocket.h"
6c0d0845 41#include "wx/socket.h"
3096bd2f 42#include "wx/sckaddr.h"
f4ada568 43
f4ada568
GL
44IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
45IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
46#ifdef ENABLE_IPV6
47IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
48#endif
5283098e 49#if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
f4ada568
GL
50IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
51#endif
f4ada568 52
a324a7bc
GL
53// ---------------------------------------------------------------------------
54// wxIPV4address
55// ---------------------------------------------------------------------------
56
6c0d0845
VZ
57void wxSockAddress::Init()
58{
59 if ( !wxSocketBase::IsInitialized() )
60 {
61 // we must do it before using GAddress_XXX functions
62 (void)wxSocketBase::Initialize();
63 }
64}
65
a324a7bc 66wxSockAddress::wxSockAddress()
69919241 67{
6c0d0845
VZ
68 Init();
69
70 m_address = GAddress_new();
f4ada568
GL
71}
72
1539c2f5 73wxSockAddress::wxSockAddress(const wxSockAddress& other)
01babda3 74 : wxObject()
1539c2f5 75{
6c0d0845
VZ
76 Init();
77
1539c2f5
VZ
78 m_address = GAddress_copy(other.m_address);
79}
80
a324a7bc 81wxSockAddress::~wxSockAddress()
f4ada568 82{
a324a7bc 83 GAddress_destroy(m_address);
f4ada568
GL
84}
85
a324a7bc 86void wxSockAddress::SetAddress(GAddress *address)
f4ada568 87{
a324a7bc
GL
88 GAddress_destroy(m_address);
89 m_address = GAddress_copy(address);
f4ada568
GL
90}
91
1539c2f5 92wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
f4ada568 93{
a324a7bc
GL
94 SetAddress(addr.GetAddress());
95 return *this;
f4ada568
GL
96}
97
a324a7bc 98void wxSockAddress::Clear()
acd15a3f 99{
a324a7bc
GL
100 GAddress_destroy(m_address);
101 m_address = GAddress_new();
f4ada568 102}
f4ada568 103
a324a7bc
GL
104// ---------------------------------------------------------------------------
105// wxIPV4address
106// ---------------------------------------------------------------------------
107
108wxIPV4address::wxIPV4address()
1539c2f5
VZ
109{
110}
111
112wxIPV4address::wxIPV4address(const wxIPV4address& other)
113 : wxSockAddress(other)
f4ada568 114{
a324a7bc 115}
f4ada568 116
a324a7bc
GL
117wxIPV4address::~wxIPV4address()
118{
119}
f4ada568 120
a324a7bc
GL
121bool wxIPV4address::Hostname(const wxString& name)
122{
f61815af 123 // Some people are sometimes fool.
acd15a3f 124 if (name == wxT(""))
58c837a4
RR
125 {
126 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
f61815af
GL
127 return FALSE;
128 }
ce22d615 129 m_origHostname = name;
f6bcfd97 130 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
131}
132
133bool wxIPV4address::Hostname(unsigned long addr)
134{
ce22d615
RD
135 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
136 if (rv)
137 m_origHostname = Hostname();
138 else
2b5f62a0 139 m_origHostname = wxEmptyString;
ce22d615 140 return rv;
f4ada568
GL
141}
142
143bool wxIPV4address::Service(const wxString& name)
144{
f6bcfd97 145 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
146}
147
148bool wxIPV4address::Service(unsigned short port)
149{
a324a7bc 150 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
151}
152
153bool wxIPV4address::LocalHost()
154{
a324a7bc 155 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
156}
157
d3ea6527
GRG
158bool wxIPV4address::AnyAddress()
159{
160 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
161}
162
f4ada568
GL
163wxString wxIPV4address::Hostname()
164{
a324a7bc 165 char hostname[1024];
f4ada568 166
a324a7bc
GL
167 hostname[0] = 0;
168 GAddress_INET_GetHostName(m_address, hostname, 1024);
2b5f62a0 169 return wxString::FromAscii(hostname);
f4ada568
GL
170}
171
a324a7bc 172unsigned short wxIPV4address::Service()
f4ada568 173{
acd15a3f 174 return GAddress_INET_GetPort(m_address);
f4ada568
GL
175}
176
ce22d615
RD
177wxSockAddress *wxIPV4address::Clone() const
178{
179 wxIPV4address *addr = new wxIPV4address(*this);
180 addr->m_origHostname = m_origHostname;
181 return addr;
182}
183
5a96d2f4 184#if 0
a324a7bc
GL
185// ---------------------------------------------------------------------------
186// wxIPV6address
187// ---------------------------------------------------------------------------
f4ada568
GL
188
189wxIPV6address::wxIPV6address()
a324a7bc 190 : wxSockAddress()
f4ada568 191{
f4ada568
GL
192}
193
1539c2f5
VZ
194wxIPV6address::wxIPV6address(const wxIPV6address& other)
195 : wxSockAddress(other)
196{
197}
198
f4ada568
GL
199wxIPV6address::~wxIPV6address()
200{
201}
202
f4ada568
GL
203bool wxIPV6address::Hostname(const wxString& name)
204{
a324a7bc 205 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
206}
207
208bool wxIPV6address::Hostname(unsigned char addr[16])
209{
f4ada568
GL
210 return TRUE;
211}
212
213bool wxIPV6address::Service(const char *name)
214{
a324a7bc 215 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
216}
217
218bool wxIPV6address::Service(unsigned short port)
219{
a324a7bc 220 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
221}
222
223bool wxIPV6address::LocalHost()
224{
a324a7bc 225 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
226}
227
228const wxString& wxIPV6address::Hostname()
229{
a324a7bc 230 return wxString(GAddress_INET_GetHostName(m_address));
f4ada568
GL
231}
232
233unsigned short wxIPV6address::Service()
234{
acd15a3f 235 return GAddress_INET_GetPort(m_address);
f4ada568
GL
236}
237
1539c2f5 238#endif // 0
f4ada568 239
5283098e 240#if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
1539c2f5 241
a324a7bc
GL
242// ---------------------------------------------------------------------------
243// wxUNIXaddress
244// ---------------------------------------------------------------------------
f4ada568
GL
245
246wxUNIXaddress::wxUNIXaddress()
1539c2f5
VZ
247 : wxSockAddress()
248{
249}
250
251wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
252 : wxSockAddress(other)
f4ada568 253{
f4ada568
GL
254}
255
256wxUNIXaddress::~wxUNIXaddress()
257{
258}
259
f4ada568
GL
260void wxUNIXaddress::Filename(const wxString& fname)
261{
a324a7bc 262 GAddress_UNIX_SetPath(m_address, fname.fn_str());
f4ada568
GL
263}
264
265wxString wxUNIXaddress::Filename()
266{
a324a7bc 267 char path[1024];
f4ada568 268
a324a7bc
GL
269 path[0] = 0;
270 GAddress_UNIX_GetPath(m_address, path, 1024);
2b5f62a0
VZ
271
272 return wxString::FromAscii(path);
f4ada568
GL
273}
274
1539c2f5 275#endif // __UNIX__
35a4dab7 276
acd15a3f 277#endif
35a4dab7 278 // wxUSE_SOCKETS