]>
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(__WXMAC__)
37 #include "/wx/mac/macsock.h"
40 #if defined(__WINDOWS__)
50 #if defined(__FreeBSD__) || defined (__NetBSD__)
51 #include <sys/types.h>
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
63 #include "wx/sckaddr.h"
65 #define CHECK_ADDRTYPE(var, type)
67 #if !USE_SHARED_LIBRARY
68 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
69 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
71 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
74 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
78 wxIPV4address::wxIPV4address()
80 m_addr
= new sockaddr_in
;
84 wxIPV4address::~wxIPV4address()
88 int wxIPV4address::SockAddrLen()
90 return sizeof(*m_addr
);
93 int wxIPV4address::GetFamily()
98 void wxIPV4address::Clear()
100 memset(m_addr
, 0, sizeof(*m_addr
));
101 m_addr
->sin_family
= AF_INET
;
102 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
106 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
108 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
109 CHECK_ADDRTYPE(addr, wxIPV4address);
110 m_addr = ip_addr->m_addr;
115 bool wxIPV4address::Hostname(const wxString
& name
)
117 struct hostent
*hostent
;
118 struct in_addr
*addr
;
123 if (!name
.IsNumber()) {
124 if ((hostent
= gethostbyname(name
.GetData())) == 0) {
129 long len_addr
= inet_addr(name
.GetData()).s_addr
;
131 long len_addr
= inet_addr(name
.GetData());
135 m_addr
->sin_addr
.s_addr
= len_addr
;
139 addr
= (struct in_addr
*) *(hostent
->h_addr_list
);
141 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
145 bool wxIPV4address::Hostname(unsigned long addr
)
147 m_addr
->sin_addr
.s_addr
= htonl(addr
);
151 bool wxIPV4address::Service(const wxString
& name
)
153 struct servent
*servent
;
158 if (!name
.IsNumber()) {
159 if ((servent
= getservbyname(name
, "tcp")) == 0)
162 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
163 m_addr
->sin_port
= htons(atoi(name
));
168 m_addr
->sin_port
= servent
->s_port
;
172 bool wxIPV4address::Service(unsigned short port
)
174 m_addr
->sin_port
= htons(port
);
178 bool wxIPV4address::LocalHost()
180 static char buf
[256];
182 if (gethostname(buf
, sizeof(buf
)) < 0)
183 return Hostname("localhost");
185 return Hostname(buf
);
188 wxString
wxIPV4address::Hostname()
190 struct hostent
*h_ent
;
192 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
194 return wxString(h_ent
->h_name
);
197 unsigned short wxIPV4address::Service()
199 return ntohs(m_addr
->sin_port
);
202 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
204 addr
= (struct sockaddr
*)m_addr
;
205 len
= sizeof(*m_addr
);
208 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
210 if (len
!= sizeof(*m_addr
))
212 *m_addr
= *(struct sockaddr_in
*)addr
;
217 wxIPV6address::wxIPV6address()
219 m_addr
= new sockaddr_in6
;
223 wxIPV6address::~wxIPV6address()
227 int wxIPV6address::SockAddrLen()
229 return sizeof(*m_addr
);
232 int wxIPV6address::GetFamily()
237 void wxIPV6address::Clear()
239 memset(m_addr
, 0, sizeof(*m_addr
));
240 m_addr
->sin6_family
= AF_INET6
;
241 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
245 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
247 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
249 CHECK_ADDRTYPE(addr, wxIPV6address);
250 m_addr = ip_addr->m_addr;
255 bool wxIPV6address::Hostname(const wxString
& name
)
257 struct hostent
*hostent
;
258 struct in_addr
*addr
;
263 if (!name
.IsNumber()) {
264 hostent
= gethostbyname2((char*) name
, AF_INET6
);
272 addr
= (struct in6_addr
*) *(hostent
->h_addr_list
);
274 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
278 bool wxIPV6address::Hostname(unsigned char addr
[16])
280 m_addr
->sin6_addr
.s6_addr
= addr
;
284 bool wxIPV6address::Service(const char *name
)
286 struct servent
*servent
;
288 if (!name
|| !strlen(name
))
291 if (!isdigit(*name
)) {
292 if ((servent
= getservbyname((char*) name
, "tcp")) == 0)
295 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
296 m_addr
->sin_port
= htons(atoi(name
));
301 m_addr
->sin_port
= servent
->s_port
;
305 bool wxIPV6address::Service(unsigned short port
)
307 m_addr
->sin_port
= htons(port
);
311 bool wxIPV6address::LocalHost()
313 static char buf
[256];
315 if (gethostname(buf
, sizeof(buf
)) < 0)
316 return Hostname("localhost");
318 return Hostname(buf
);
321 const wxString
& wxIPV6address::Hostname()
323 struct hostent
*h_ent
;
325 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
327 return wxString(h_ent
->h_name
);
330 unsigned short wxIPV6address::Service()
332 return ntohs(m_addr
->sin_port
);
335 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
337 len
= sizeof(*m_addr
);
341 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
343 if (len
!= sizeof(*m_addr
))
345 *m_addr
= *(struct sockaddr_in6
*)addr
;
353 wxUNIXaddress::wxUNIXaddress()
355 m_addr
= new sockaddr_un
;
359 wxUNIXaddress::~wxUNIXaddress()
363 int wxUNIXaddress::SockAddrLen()
365 return sizeof(*m_addr
);
368 int wxUNIXaddress::GetFamily()
373 void wxUNIXaddress::Clear()
375 memset(m_addr
, 0, sizeof(m_addr
));
376 m_addr
->sun_family
= AF_UNIX
;
380 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
382 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
383 CHECK_ADDRTYPE(addr, wxUNIXaddress);
384 m_addr = unx_addr->m_addr;
389 void wxUNIXaddress::Filename(const wxString
& fname
)
391 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
394 wxString
wxUNIXaddress::Filename()
396 return wxString(m_addr
->sun_path
);
399 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
401 addr
= (struct sockaddr
*)m_addr
;
402 len
= sizeof(*m_addr
);
405 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
407 if (len
!= sizeof(*m_addr
))
409 *m_addr
= *(struct sockaddr_un
*)addr
;