]>
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 license
10 /////////////////////////////////////////////////////////////////////////////
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
;
185 // ---------------------------------------------------------------------------
187 // ---------------------------------------------------------------------------
189 wxIPV6address::wxIPV6address()
194 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
195 : wxSockAddress(other
)
199 wxIPV6address::~wxIPV6address()
203 bool wxIPV6address::Hostname(const wxString
& name
)
205 return (GAddress_INET_SetHostName(m_address
, name
.fn_str()) == GSOCK_NOERROR
);
208 bool wxIPV6address::Hostname(unsigned char addr
[16])
213 bool wxIPV6address::Service(const char *name
)
215 return (GAddress_INET_SetPortName(m_address
, name
.fn_str()) == GSOCK_NOERROR
);
218 bool wxIPV6address::Service(unsigned short port
)
220 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
223 bool wxIPV6address::LocalHost()
225 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
228 const wxString
& wxIPV6address::Hostname()
230 return wxString(GAddress_INET_GetHostName(m_address
));
233 unsigned short wxIPV6address::Service()
235 return GAddress_INET_GetPort(m_address
);
240 #if defined(__UNIX__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
242 // ---------------------------------------------------------------------------
244 // ---------------------------------------------------------------------------
246 wxUNIXaddress::wxUNIXaddress()
251 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
252 : wxSockAddress(other
)
256 wxUNIXaddress::~wxUNIXaddress()
260 void wxUNIXaddress::Filename(const wxString
& fname
)
262 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
265 wxString
wxUNIXaddress::Filename()
270 GAddress_UNIX_GetPath(m_address
, path
, 1024);
272 return wxString::FromAscii(path
);