]>
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"
34 #include "wx/object.h"
36 #if defined(__WINDOWS__)
46 #if defined(__FreeBSD__) || defined (__NetBSD__)
47 #include <sys/types.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
59 #include "wx/sckaddr.h"
61 #define CHECK_ADDRTYPE(var, type)
63 #if !USE_SHARED_LIBRARY
64 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
67 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
70 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
74 wxIPV4address::wxIPV4address()
76 m_addr
= new sockaddr_in
;
80 wxIPV4address::~wxIPV4address()
84 int wxIPV4address::SockAddrLen()
86 return sizeof(*m_addr
);
89 int wxIPV4address::GetFamily()
94 void wxIPV4address::Clear()
96 memset(m_addr
, 0, sizeof(*m_addr
));
97 m_addr
->sin_family
= AF_INET
;
98 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
102 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
104 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
105 CHECK_ADDRTYPE(addr, wxIPV4address);
106 m_addr = ip_addr->m_addr;
111 bool wxIPV4address::Hostname(const wxString
& name
)
113 struct hostent
*hostent
;
114 struct in_addr
*addr
;
119 if (!name
.IsNumber()) {
120 if ((hostent
= gethostbyname(name
.GetData())) == 0) {
124 long len_addr
= inet_addr(name
.GetData());
127 m_addr
->sin_addr
.s_addr
= len_addr
;
131 addr
= (struct in_addr
*) *(hostent
->h_addr_list
);
133 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
137 bool wxIPV4address::Hostname(unsigned long addr
)
139 m_addr
->sin_addr
.s_addr
= htonl(addr
);
143 bool wxIPV4address::Service(const wxString
& name
)
145 struct servent
*servent
;
150 if (!name
.IsNumber()) {
151 if ((servent
= getservbyname(name
, "tcp")) == 0)
154 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
155 m_addr
->sin_port
= htons(atoi(name
));
160 m_addr
->sin_port
= servent
->s_port
;
164 bool wxIPV4address::Service(unsigned short port
)
166 m_addr
->sin_port
= htons(port
);
170 bool wxIPV4address::LocalHost()
172 static char buf
[256];
174 if (gethostname(buf
, sizeof(buf
)) < 0)
175 return Hostname("localhost");
177 return Hostname(buf
);
180 wxString
wxIPV4address::Hostname()
182 struct hostent
*h_ent
;
184 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
186 return wxString(h_ent
->h_name
);
189 unsigned short wxIPV4address::Service()
191 return ntohs(m_addr
->sin_port
);
194 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
196 addr
= (struct sockaddr
*)m_addr
;
197 len
= sizeof(*m_addr
);
200 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
202 if (len
!= sizeof(*m_addr
))
204 *m_addr
= *(struct sockaddr_in
*)addr
;
209 wxIPV6address::wxIPV6address()
211 m_addr
= new sockaddr_in6
;
215 wxIPV6address::~wxIPV6address()
219 int wxIPV6address::SockAddrLen()
221 return sizeof(*m_addr
);
224 int wxIPV6address::GetFamily()
229 void wxIPV6address::Clear()
231 memset(m_addr
, 0, sizeof(*m_addr
));
232 m_addr
->sin6_family
= AF_INET6
;
233 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
237 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
239 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
241 CHECK_ADDRTYPE(addr, wxIPV6address);
242 m_addr = ip_addr->m_addr;
247 bool wxIPV6address::Hostname(const wxString
& name
)
249 struct hostent
*hostent
;
250 struct in_addr
*addr
;
255 if (!name
.IsNumber()) {
256 hostent
= gethostbyname2((char*) name
, AF_INET6
);
264 addr
= (struct in6_addr
*) *(hostent
->h_addr_list
);
266 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
270 bool wxIPV6address::Hostname(unsigned char addr
[16])
272 m_addr
->sin6_addr
.s6_addr
= addr
;
276 bool wxIPV6address::Service(const char *name
)
278 struct servent
*servent
;
280 if (!name
|| !strlen(name
))
283 if (!isdigit(*name
)) {
284 if ((servent
= getservbyname((char*) name
, "tcp")) == 0)
287 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
288 m_addr
->sin_port
= htons(atoi(name
));
293 m_addr
->sin_port
= servent
->s_port
;
297 bool wxIPV6address::Service(unsigned short port
)
299 m_addr
->sin_port
= htons(port
);
303 bool wxIPV6address::LocalHost()
305 static char buf
[256];
307 if (gethostname(buf
, sizeof(buf
)) < 0)
308 return Hostname("localhost");
310 return Hostname(buf
);
313 const wxString
& wxIPV6address::Hostname()
315 struct hostent
*h_ent
;
317 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
319 return wxString(h_ent
->h_name
);
322 unsigned short wxIPV6address::Service()
324 return ntohs(m_addr
->sin_port
);
327 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
329 len
= sizeof(*m_addr
);
333 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
335 if (len
!= sizeof(*m_addr
))
337 *m_addr
= *(struct sockaddr_in6
*)addr
;
345 wxUNIXaddress::wxUNIXaddress()
347 m_addr
= new sockaddr_un
;
351 wxUNIXaddress::~wxUNIXaddress()
355 int wxUNIXaddress::SockAddrLen()
357 return sizeof(*m_addr
);
360 int wxUNIXaddress::GetFamily()
365 void wxUNIXaddress::Clear()
367 memset(m_addr
, 0, sizeof(m_addr
));
368 m_addr
->sun_family
= AF_UNIX
;
372 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
374 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
375 CHECK_ADDRTYPE(addr, wxUNIXaddress);
376 m_addr = unx_addr->m_addr;
381 void wxUNIXaddress::Filename(const wxString
& fname
)
383 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
386 wxString
wxUNIXaddress::Filename()
388 return wxString(m_addr
->sun_path
);
391 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
393 addr
= (struct sockaddr
*)m_addr
;
394 len
= sizeof(*m_addr
);
397 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
399 if (len
!= sizeof(*m_addr
))
401 *m_addr
= *(struct sockaddr_un
*)addr
;