]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
don't try to remove -g flag from CFLAGS, it is not necessary and doesn't work correct...
[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
0cbb9297 49#if defined(__UNIX__) && !defined(__WXMAC__)
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
VZ
73wxSockAddress::wxSockAddress(const wxSockAddress& other)
74{
6c0d0845
VZ
75 Init();
76
1539c2f5
VZ
77 m_address = GAddress_copy(other.m_address);
78}
79
a324a7bc 80wxSockAddress::~wxSockAddress()
f4ada568 81{
a324a7bc 82 GAddress_destroy(m_address);
f4ada568
GL
83}
84
a324a7bc 85void wxSockAddress::SetAddress(GAddress *address)
f4ada568 86{
a324a7bc
GL
87 GAddress_destroy(m_address);
88 m_address = GAddress_copy(address);
f4ada568
GL
89}
90
1539c2f5 91wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
f4ada568 92{
a324a7bc
GL
93 SetAddress(addr.GetAddress());
94 return *this;
f4ada568
GL
95}
96
a324a7bc 97void wxSockAddress::Clear()
acd15a3f 98{
a324a7bc
GL
99 GAddress_destroy(m_address);
100 m_address = GAddress_new();
f4ada568 101}
f4ada568 102
a324a7bc
GL
103// ---------------------------------------------------------------------------
104// wxIPV4address
105// ---------------------------------------------------------------------------
106
107wxIPV4address::wxIPV4address()
1539c2f5
VZ
108{
109}
110
111wxIPV4address::wxIPV4address(const wxIPV4address& other)
112 : wxSockAddress(other)
f4ada568 113{
a324a7bc 114}
f4ada568 115
a324a7bc
GL
116wxIPV4address::~wxIPV4address()
117{
118}
f4ada568 119
a324a7bc
GL
120bool wxIPV4address::Hostname(const wxString& name)
121{
f61815af 122 // Some people are sometimes fool.
acd15a3f 123 if (name == wxT(""))
58c837a4
RR
124 {
125 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
f61815af
GL
126 return FALSE;
127 }
ce22d615 128 m_origHostname = name;
f6bcfd97 129 return (GAddress_INET_SetHostName(m_address, name.mb_str()) == GSOCK_NOERROR);
f4ada568
GL
130}
131
132bool wxIPV4address::Hostname(unsigned long addr)
133{
ce22d615
RD
134 bool rv = (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
135 if (rv)
136 m_origHostname = Hostname();
137 else
138 m_origHostname = "";
139 return rv;
f4ada568
GL
140}
141
142bool wxIPV4address::Service(const wxString& name)
143{
f6bcfd97 144 return (GAddress_INET_SetPortName(m_address, name.mb_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
145}
146
147bool wxIPV4address::Service(unsigned short port)
148{
a324a7bc 149 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
150}
151
152bool wxIPV4address::LocalHost()
153{
a324a7bc 154 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
155}
156
d3ea6527
GRG
157bool wxIPV4address::AnyAddress()
158{
159 return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
160}
161
f4ada568
GL
162wxString wxIPV4address::Hostname()
163{
a324a7bc 164 char hostname[1024];
f4ada568 165
a324a7bc
GL
166 hostname[0] = 0;
167 GAddress_INET_GetHostName(m_address, hostname, 1024);
168 return wxString(hostname);
f4ada568
GL
169}
170
a324a7bc 171unsigned short wxIPV4address::Service()
f4ada568 172{
acd15a3f 173 return GAddress_INET_GetPort(m_address);
f4ada568
GL
174}
175
ce22d615
RD
176wxSockAddress *wxIPV4address::Clone() const
177{
178 wxIPV4address *addr = new wxIPV4address(*this);
179 addr->m_origHostname = m_origHostname;
180 return addr;
181}
182
5a96d2f4 183#if 0
a324a7bc
GL
184// ---------------------------------------------------------------------------
185// wxIPV6address
186// ---------------------------------------------------------------------------
f4ada568
GL
187
188wxIPV6address::wxIPV6address()
a324a7bc 189 : wxSockAddress()
f4ada568 190{
f4ada568
GL
191}
192
1539c2f5
VZ
193wxIPV6address::wxIPV6address(const wxIPV6address& other)
194 : wxSockAddress(other)
195{
196}
197
f4ada568
GL
198wxIPV6address::~wxIPV6address()
199{
200}
201
f4ada568
GL
202bool wxIPV6address::Hostname(const wxString& name)
203{
a324a7bc 204 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
205}
206
207bool wxIPV6address::Hostname(unsigned char addr[16])
208{
f4ada568
GL
209 return TRUE;
210}
211
212bool wxIPV6address::Service(const char *name)
213{
a324a7bc 214 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
215}
216
217bool wxIPV6address::Service(unsigned short port)
218{
a324a7bc 219 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
220}
221
222bool wxIPV6address::LocalHost()
223{
a324a7bc 224 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
225}
226
227const wxString& wxIPV6address::Hostname()
228{
a324a7bc 229 return wxString(GAddress_INET_GetHostName(m_address));
f4ada568
GL
230}
231
232unsigned short wxIPV6address::Service()
233{
acd15a3f 234 return GAddress_INET_GetPort(m_address);
f4ada568
GL
235}
236
1539c2f5 237#endif // 0
f4ada568 238
0cbb9297 239#if defined(__UNIX__) && !defined(__WXMAC__)
1539c2f5 240
a324a7bc
GL
241// ---------------------------------------------------------------------------
242// wxUNIXaddress
243// ---------------------------------------------------------------------------
f4ada568
GL
244
245wxUNIXaddress::wxUNIXaddress()
1539c2f5
VZ
246 : wxSockAddress()
247{
248}
249
250wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress& other)
251 : wxSockAddress(other)
f4ada568 252{
f4ada568
GL
253}
254
255wxUNIXaddress::~wxUNIXaddress()
256{
257}
258
f4ada568
GL
259void wxUNIXaddress::Filename(const wxString& fname)
260{
a324a7bc 261 GAddress_UNIX_SetPath(m_address, fname.fn_str());
f4ada568
GL
262}
263
264wxString wxUNIXaddress::Filename()
265{
a324a7bc 266 char path[1024];
f4ada568 267
a324a7bc
GL
268 path[0] = 0;
269 GAddress_UNIX_GetPath(m_address, path, 1024);
270 return wxString(path);
f4ada568
GL
271}
272
1539c2f5 273#endif // __UNIX__
35a4dab7 274
acd15a3f 275#endif
35a4dab7 276 // wxUSE_SOCKETS