]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Network address manager
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "sckaddr.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/object.h"
35 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
40 #include "wx/gsocket.h"
41 #include "wx/socket.h"
42 #include "wx/sckaddr.h"
44 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
45 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
47 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
49 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
50 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 void wxSockAddress::Init()
59 if ( !wxSocketBase::IsInitialized() )
61 // we must do it before using GAddress_XXX functions
62 (void)wxSocketBase::Initialize();
66 wxSockAddress::wxSockAddress()
70 m_address
= GAddress_new();
73 wxSockAddress::wxSockAddress(const wxSockAddress
& other
)
78 m_address
= GAddress_copy(other
.m_address
);
81 wxSockAddress::~wxSockAddress()
83 GAddress_destroy(m_address
);
86 void wxSockAddress::SetAddress(GAddress
*address
)
88 GAddress_destroy(m_address
);
89 m_address
= GAddress_copy(address
);
92 wxSockAddress
& wxSockAddress::operator=(const wxSockAddress
& addr
)
94 SetAddress(addr
.GetAddress());
98 void wxSockAddress::Clear()
100 GAddress_destroy(m_address
);
101 m_address
= GAddress_new();
104 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 wxIPV4address::wxIPV4address()
112 wxIPV4address::wxIPV4address(const wxIPV4address
& other
)
113 : wxSockAddress(other
)
117 wxIPV4address::~wxIPV4address()
121 bool wxIPV4address::Hostname(const wxString
& name
)
123 // Some people are sometimes fool.
126 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
129 m_origHostname
= name
;
130 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
133 bool wxIPV4address::Hostname(unsigned long addr
)
135 bool rv
= (GAddress_INET_SetHostAddress(m_address
, addr
) == GSOCK_NOERROR
);
137 m_origHostname
= Hostname();
139 m_origHostname
= wxEmptyString
;
143 bool wxIPV4address::Service(const wxString
& name
)
145 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
148 bool wxIPV4address::Service(unsigned short port
)
150 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
153 bool wxIPV4address::LocalHost()
155 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
158 bool wxIPV4address::AnyAddress()
160 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
163 wxString
wxIPV4address::Hostname()
168 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
169 return wxString::FromAscii(hostname
);
172 unsigned short wxIPV4address::Service()
174 return GAddress_INET_GetPort(m_address
);
177 wxSockAddress
*wxIPV4address::Clone() const
179 wxIPV4address
*addr
= new wxIPV4address(*this);
180 addr
->m_origHostname
= m_origHostname
;
184 wxString
wxIPV4address::IPAddress() const
186 unsigned long raw
= GAddress_INET_GetHostAddress(m_address
);
187 return wxString::Format(
189 (unsigned char)(raw
& 0xff),
190 (unsigned char)((raw
>>8) & 0xff),
191 (unsigned char)((raw
>>16) & 0xff),
192 (unsigned char)((raw
>>24) & 0xff)
197 // ---------------------------------------------------------------------------
199 // ---------------------------------------------------------------------------
201 wxIPV6address::wxIPV6address()
206 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
207 : wxSockAddress(other
)
211 wxIPV6address::~wxIPV6address()
215 bool wxIPV6address::Hostname(const wxString
& name
)
217 return (GAddress_INET_SetHostName(m_address
, name
.fn_str()) == GSOCK_NOERROR
);
220 bool wxIPV6address::Hostname(unsigned char addr
[16])
225 bool wxIPV6address::Service(const char *name
)
227 return (GAddress_INET_SetPortName(m_address
, name
.fn_str()) == GSOCK_NOERROR
);
230 bool wxIPV6address::Service(unsigned short port
)
232 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
235 bool wxIPV6address::LocalHost()
237 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
240 const wxString
& wxIPV6address::Hostname()
242 return wxString(GAddress_INET_GetHostName(m_address
));
245 unsigned short wxIPV6address::Service()
247 return GAddress_INET_GetPort(m_address
);
252 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
254 // ---------------------------------------------------------------------------
256 // ---------------------------------------------------------------------------
258 wxUNIXaddress::wxUNIXaddress()
263 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
264 : wxSockAddress(other
)
268 wxUNIXaddress::~wxUNIXaddress()
272 void wxUNIXaddress::Filename(const wxString
& fname
)
274 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
277 wxString
wxUNIXaddress::Filename()
282 GAddress_UNIX_GetPath(m_address
, path
, 1024);
284 return wxString::FromAscii(path
);