]>
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"
32 #include "wx/object.h"
34 #if defined(__WINDOWS__)
44 #if defined(__FreeBSD__) || defined (__NetBSD__)
45 #include <sys/types.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
57 #include "wx/sckaddr.h"
59 #define CHECK_ADDRTYPE(var, type)
61 #if !USE_SHARED_LIBRARY
62 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
63 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
65 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
68 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
72 wxIPV4address::wxIPV4address()
74 m_addr
= new sockaddr_in
;
78 wxIPV4address::~wxIPV4address()
82 int wxIPV4address::SockAddrLen()
84 return sizeof(*m_addr
);
87 int wxIPV4address::GetFamily()
92 void wxIPV4address::Clear()
94 memset(m_addr
, 0, sizeof(*m_addr
));
95 m_addr
->sin_family
= AF_INET
;
96 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
100 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
102 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
103 CHECK_ADDRTYPE(addr, wxIPV4address);
104 m_addr = ip_addr->m_addr;
109 bool wxIPV4address::Hostname(const wxString
& name
)
111 struct hostent
*hostent
;
112 struct in_addr
*addr
;
117 if (!name
.IsNumber()) {
118 if ((hostent
= gethostbyname(name
.GetData())) == 0) {
122 long len_addr
= inet_addr(name
.GetData());
125 m_addr
->sin_addr
.s_addr
= len_addr
;
129 addr
= (struct in_addr
*) *(hostent
->h_addr_list
);
131 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
135 bool wxIPV4address::Hostname(unsigned long addr
)
137 m_addr
->sin_addr
.s_addr
= htonl(addr
);
141 bool wxIPV4address::Service(const wxString
& name
)
143 struct servent
*servent
;
148 if (!name
.IsNumber()) {
149 if ((servent
= getservbyname(name
, "tcp")) == 0)
152 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
153 m_addr
->sin_port
= htons(atoi(name
));
158 m_addr
->sin_port
= servent
->s_port
;
162 bool wxIPV4address::Service(unsigned short port
)
164 m_addr
->sin_port
= htons(port
);
168 bool wxIPV4address::LocalHost()
170 static char buf
[256];
172 if (gethostname(buf
, sizeof(buf
)) < 0)
173 return Hostname("localhost");
175 return Hostname(buf
);
178 wxString
wxIPV4address::Hostname()
180 struct hostent
*h_ent
;
182 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
184 return wxString(h_ent
->h_name
);
187 unsigned short wxIPV4address::Service()
189 return ntohs(m_addr
->sin_port
);
192 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
194 addr
= (struct sockaddr
*)m_addr
;
195 len
= sizeof(*m_addr
);
198 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
200 if (len
!= sizeof(*m_addr
))
202 *m_addr
= *(struct sockaddr_in
*)addr
;
207 wxIPV6address::wxIPV6address()
209 m_addr
= new sockaddr_in6
;
213 wxIPV6address::~wxIPV6address()
217 int wxIPV6address::SockAddrLen()
219 return sizeof(*m_addr
);
222 int wxIPV6address::GetFamily()
227 void wxIPV6address::Clear()
229 memset(m_addr
, 0, sizeof(*m_addr
));
230 m_addr
->sin6_family
= AF_INET6
;
231 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
235 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
237 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
239 CHECK_ADDRTYPE(addr, wxIPV6address);
240 m_addr = ip_addr->m_addr;
245 bool wxIPV6address::Hostname(const wxString
& name
)
247 struct hostent
*hostent
;
248 struct in_addr
*addr
;
253 if (!name
.IsNumber()) {
254 hostent
= gethostbyname2((char*) name
, AF_INET6
);
262 addr
= (struct in6_addr
*) *(hostent
->h_addr_list
);
264 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
268 bool wxIPV6address::Hostname(unsigned char addr
[16])
270 m_addr
->sin6_addr
.s6_addr
= addr
;
274 bool wxIPV6address::Service(const char *name
)
276 struct servent
*servent
;
278 if (!name
|| !strlen(name
))
281 if (!isdigit(*name
)) {
282 if ((servent
= getservbyname((char*) name
, "tcp")) == 0)
285 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
286 m_addr
->sin_port
= htons(atoi(name
));
291 m_addr
->sin_port
= servent
->s_port
;
295 bool wxIPV6address::Service(unsigned short port
)
297 m_addr
->sin_port
= htons(port
);
301 bool wxIPV6address::LocalHost()
303 static char buf
[256];
305 if (gethostname(buf
, sizeof(buf
)) < 0)
306 return Hostname("localhost");
308 return Hostname(buf
);
311 const wxString
& wxIPV6address::Hostname()
313 struct hostent
*h_ent
;
315 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
317 return wxString(h_ent
->h_name
);
320 unsigned short wxIPV6address::Service()
322 return ntohs(m_addr
->sin_port
);
325 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
327 len
= sizeof(*m_addr
);
331 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
333 if (len
!= sizeof(*m_addr
))
335 *m_addr
= *(struct sockaddr_in6
*)addr
;
343 wxUNIXaddress::wxUNIXaddress()
345 m_addr
= new sockaddr_un
;
349 wxUNIXaddress::~wxUNIXaddress()
353 int wxUNIXaddress::SockAddrLen()
355 return sizeof(*m_addr
);
358 int wxUNIXaddress::GetFamily()
363 void wxUNIXaddress::Clear()
365 memset(m_addr
, 0, sizeof(m_addr
));
366 m_addr
->sun_family
= AF_UNIX
;
370 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
372 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
373 CHECK_ADDRTYPE(addr, wxUNIXaddress);
374 m_addr = unx_addr->m_addr;
379 void wxUNIXaddress::Filename(const wxString
& fname
)
381 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
384 wxString
wxUNIXaddress::Filename()
386 return wxString(m_addr
->sun_path
);
389 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
391 addr
= (struct sockaddr
*)m_addr
;
392 len
= sizeof(*m_addr
);
395 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
397 if (len
!= sizeof(*m_addr
))
399 *m_addr
= *(struct sockaddr_un
*)addr
;