]>
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(__WXMAC__)
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
)
77 m_address
= GAddress_copy(other
.m_address
);
80 wxSockAddress::~wxSockAddress()
82 GAddress_destroy(m_address
);
85 void wxSockAddress::SetAddress(GAddress
*address
)
87 GAddress_destroy(m_address
);
88 m_address
= GAddress_copy(address
);
91 wxSockAddress
& wxSockAddress::operator=(const wxSockAddress
& addr
)
93 SetAddress(addr
.GetAddress());
97 void wxSockAddress::Clear()
99 GAddress_destroy(m_address
);
100 m_address
= GAddress_new();
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 wxIPV4address::wxIPV4address()
111 wxIPV4address::wxIPV4address(const wxIPV4address
& other
)
112 : wxSockAddress(other
)
116 wxIPV4address::~wxIPV4address()
120 bool wxIPV4address::Hostname(const wxString
& name
)
122 // Some people are sometimes fool.
125 wxLogWarning( _("Trying to solve a NULL hostname: giving up") );
128 m_origHostname
= name
;
129 return (GAddress_INET_SetHostName(m_address
, name
.mb_str()) == GSOCK_NOERROR
);
132 bool wxIPV4address::Hostname(unsigned long addr
)
134 bool rv
= (GAddress_INET_SetHostAddress(m_address
, addr
) == GSOCK_NOERROR
);
136 m_origHostname
= Hostname();
142 bool wxIPV4address::Service(const wxString
& name
)
144 return (GAddress_INET_SetPortName(m_address
, name
.mb_str(), "tcp") == GSOCK_NOERROR
);
147 bool wxIPV4address::Service(unsigned short port
)
149 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
152 bool wxIPV4address::LocalHost()
154 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
157 bool wxIPV4address::AnyAddress()
159 return (GAddress_INET_SetAnyAddress(m_address
) == GSOCK_NOERROR
);
162 wxString
wxIPV4address::Hostname()
167 GAddress_INET_GetHostName(m_address
, hostname
, 1024);
168 return wxString(hostname
);
171 unsigned short wxIPV4address::Service()
173 return GAddress_INET_GetPort(m_address
);
176 wxSockAddress
*wxIPV4address::Clone() const
178 wxIPV4address
*addr
= new wxIPV4address(*this);
179 addr
->m_origHostname
= m_origHostname
;
184 // ---------------------------------------------------------------------------
186 // ---------------------------------------------------------------------------
188 wxIPV6address::wxIPV6address()
193 wxIPV6address::wxIPV6address(const wxIPV6address
& other
)
194 : wxSockAddress(other
)
198 wxIPV6address::~wxIPV6address()
202 bool wxIPV6address::Hostname(const wxString
& name
)
204 return (GAddress_INET_SetHostName(m_address
, name
.fn_str()) == GSOCK_NOERROR
);
207 bool wxIPV6address::Hostname(unsigned char addr
[16])
212 bool wxIPV6address::Service(const char *name
)
214 return (GAddress_INET_SetPortName(m_address
, name
.fn_str()) == GSOCK_NOERROR
);
217 bool wxIPV6address::Service(unsigned short port
)
219 return (GAddress_INET_SetPort(m_address
, port
) == GSOCK_NOERROR
);
222 bool wxIPV6address::LocalHost()
224 return (GAddress_INET_SetHostName(m_address
, "localhost") == GSOCK_NOERROR
);
227 const wxString
& wxIPV6address::Hostname()
229 return wxString(GAddress_INET_GetHostName(m_address
));
232 unsigned short wxIPV6address::Service()
234 return GAddress_INET_GetPort(m_address
);
239 #if defined(__UNIX__) && !defined(__WXMAC__)
241 // ---------------------------------------------------------------------------
243 // ---------------------------------------------------------------------------
245 wxUNIXaddress::wxUNIXaddress()
250 wxUNIXaddress::wxUNIXaddress(const wxUNIXaddress
& other
)
251 : wxSockAddress(other
)
255 wxUNIXaddress::~wxUNIXaddress()
259 void wxUNIXaddress::Filename(const wxString
& fname
)
261 GAddress_UNIX_SetPath(m_address
, fname
.fn_str());
264 wxString
wxUNIXaddress::Filename()
269 GAddress_UNIX_GetPath(m_address
, path
, 1024);
270 return wxString(path
);