]> git.saurik.com Git - wxWidgets.git/blame - src/common/sckaddr.cpp
Committing in .
[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
f4ada568
GL
25#include <stdio.h>
26#include <stdlib.h>
27#include <ctype.h>
ce3ed50d
JS
28
29#if !defined(__MWERKS__) && !defined(__SALFORDC__)
f4ada568 30#include <memory.h>
469e1e5c 31#endif
f4ada568 32
3096bd2f
VZ
33#include "wx/defs.h"
34#include "wx/object.h"
35#include "wx/log.h"
58c837a4 36#include "wx/intl.h"
3096bd2f
VZ
37#include "wx/gsocket.h"
38#include "wx/sckaddr.h"
f4ada568
GL
39
40#if !USE_SHARED_LIBRARY
41IMPLEMENT_ABSTRACT_CLASS(wxSockAddress, wxObject)
42IMPLEMENT_DYNAMIC_CLASS(wxIPV4address, wxSockAddress)
43#ifdef ENABLE_IPV6
44IMPLEMENT_DYNAMIC_CLASS(wxIPV6address, wxSockAddress)
45#endif
46#ifdef __UNIX__
47IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress, wxSockAddress)
48#endif
49#endif
50
a324a7bc
GL
51// ---------------------------------------------------------------------------
52// wxIPV4address
53// ---------------------------------------------------------------------------
54
55wxSockAddress::wxSockAddress()
69919241 56{
a324a7bc 57 m_address = GAddress_new();
f4ada568
GL
58}
59
a324a7bc 60wxSockAddress::~wxSockAddress()
f4ada568 61{
a324a7bc 62 GAddress_destroy(m_address);
f4ada568
GL
63}
64
a324a7bc 65void wxSockAddress::SetAddress(GAddress *address)
f4ada568 66{
a324a7bc
GL
67 GAddress_destroy(m_address);
68 m_address = GAddress_copy(address);
f4ada568
GL
69}
70
a324a7bc 71const wxSockAddress& wxSockAddress::operator=(const wxSockAddress& addr)
f4ada568 72{
a324a7bc
GL
73 SetAddress(addr.GetAddress());
74 return *this;
f4ada568
GL
75}
76
a324a7bc 77void wxSockAddress::CopyObject(wxObject& dest) const
f4ada568 78{
a324a7bc
GL
79 wxSockAddress *addr = (wxSockAddress *)&dest;
80
81 wxObject::CopyObject(dest);
82 addr->SetAddress(GetAddress());
f4ada568
GL
83}
84
a324a7bc
GL
85void wxSockAddress::Clear()
86{
87 GAddress_destroy(m_address);
88 m_address = GAddress_new();
f4ada568 89}
f4ada568 90
a324a7bc
GL
91// ---------------------------------------------------------------------------
92// wxIPV4address
93// ---------------------------------------------------------------------------
94
95wxIPV4address::wxIPV4address()
96 : wxSockAddress()
f4ada568 97{
a324a7bc 98}
f4ada568 99
a324a7bc
GL
100wxIPV4address::~wxIPV4address()
101{
102}
f4ada568 103
a324a7bc
GL
104bool wxIPV4address::Hostname(const wxString& name)
105{
f61815af 106 // Some people are sometimes fool.
58c837a4
RR
107 if (name == wxT(""))
108 {
109 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
f61815af
GL
110 return FALSE;
111 }
112
a324a7bc 113 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
114}
115
116bool wxIPV4address::Hostname(unsigned long addr)
117{
5a96d2f4 118 return (GAddress_INET_SetHostAddress(m_address, addr) == GSOCK_NOERROR);
f4ada568
GL
119}
120
121bool wxIPV4address::Service(const wxString& name)
122{
5a96d2f4 123 return (GAddress_INET_SetPortName(m_address, name.fn_str(), "tcp") == GSOCK_NOERROR);
f4ada568
GL
124}
125
126bool wxIPV4address::Service(unsigned short port)
127{
a324a7bc 128 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
129}
130
131bool wxIPV4address::LocalHost()
132{
a324a7bc 133 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
134}
135
136wxString wxIPV4address::Hostname()
137{
a324a7bc 138 char hostname[1024];
f4ada568 139
a324a7bc
GL
140 hostname[0] = 0;
141 GAddress_INET_GetHostName(m_address, hostname, 1024);
142 return wxString(hostname);
f4ada568
GL
143}
144
a324a7bc 145unsigned short wxIPV4address::Service()
f4ada568 146{
a324a7bc 147 return GAddress_INET_GetPort(m_address);
f4ada568
GL
148}
149
5a96d2f4 150#if 0
a324a7bc
GL
151// ---------------------------------------------------------------------------
152// wxIPV6address
153// ---------------------------------------------------------------------------
f4ada568
GL
154
155wxIPV6address::wxIPV6address()
a324a7bc 156 : wxSockAddress()
f4ada568 157{
f4ada568
GL
158}
159
160wxIPV6address::~wxIPV6address()
161{
162}
163
f4ada568
GL
164bool wxIPV6address::Hostname(const wxString& name)
165{
a324a7bc 166 return (GAddress_INET_SetHostName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
167}
168
169bool wxIPV6address::Hostname(unsigned char addr[16])
170{
f4ada568
GL
171 return TRUE;
172}
173
174bool wxIPV6address::Service(const char *name)
175{
a324a7bc 176 return (GAddress_INET_SetPortName(m_address, name.fn_str()) == GSOCK_NOERROR);
f4ada568
GL
177}
178
179bool wxIPV6address::Service(unsigned short port)
180{
a324a7bc 181 return (GAddress_INET_SetPort(m_address, port) == GSOCK_NOERROR);
f4ada568
GL
182}
183
184bool wxIPV6address::LocalHost()
185{
a324a7bc 186 return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
f4ada568
GL
187}
188
189const wxString& wxIPV6address::Hostname()
190{
a324a7bc 191 return wxString(GAddress_INET_GetHostName(m_address));
f4ada568
GL
192}
193
194unsigned short wxIPV6address::Service()
195{
a324a7bc 196 return GAddress_INET_GetPort(m_address);
f4ada568
GL
197}
198
199#endif
200
201#ifdef __UNIX__
a324a7bc
GL
202// ---------------------------------------------------------------------------
203// wxUNIXaddress
204// ---------------------------------------------------------------------------
f4ada568
GL
205
206wxUNIXaddress::wxUNIXaddress()
a324a7bc 207 : wxSockAddress()
f4ada568 208{
f4ada568
GL
209}
210
211wxUNIXaddress::~wxUNIXaddress()
212{
213}
214
f4ada568
GL
215void wxUNIXaddress::Filename(const wxString& fname)
216{
a324a7bc 217 GAddress_UNIX_SetPath(m_address, fname.fn_str());
f4ada568
GL
218}
219
220wxString wxUNIXaddress::Filename()
221{
a324a7bc 222 char path[1024];
f4ada568 223
a324a7bc
GL
224 path[0] = 0;
225 GAddress_UNIX_GetPath(m_address, path, 1024);
226 return wxString(path);
f4ada568
GL
227}
228
f4ada568 229#endif
35a4dab7
GL
230
231#endif
232 // wxUSE_SOCKETS