]>
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"
22 #include "wx/object.h"
24 #if defined(__WINDOWS__)
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
43 #include "wx/sckaddr.h"
49 #define CHECK_ADDRTYPE(var, type)
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
53 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
55 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
58 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
62 wxIPV4address::wxIPV4address()
64 m_addr
= new sockaddr_in
;
68 wxIPV4address::~wxIPV4address()
72 int wxIPV4address::SockAddrLen()
74 return sizeof(*m_addr
);
77 int wxIPV4address::GetFamily()
82 void wxIPV4address::Clear()
84 memset(m_addr
, 0, sizeof(*m_addr
));
85 m_addr
->sin_family
= AF_INET
;
86 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
90 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
92 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
93 CHECK_ADDRTYPE(addr, wxIPV4address);
94 m_addr = ip_addr->m_addr;
99 bool wxIPV4address::Hostname(const wxString
& name
)
101 struct hostent
*hostent
;
102 struct in_addr
*addr
;
107 if (!name
.IsNumber()) {
108 if ((hostent
= gethostbyname(name
.GetData())) == 0) {
112 long len_addr
= inet_addr(name
.GetData());
115 m_addr
->sin_addr
.s_addr
= len_addr
;
119 addr
= (struct in_addr
*) *(hostent
->h_addr_list
);
121 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
125 bool wxIPV4address::Hostname(unsigned long addr
)
127 m_addr
->sin_addr
.s_addr
= htonl(addr
);
131 bool wxIPV4address::Service(const wxString
& name
)
133 struct servent
*servent
;
138 if (!name
.IsNumber()) {
139 if ((servent
= getservbyname(name
, "tcp")) == 0)
142 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
143 m_addr
->sin_port
= htons(atoi(name
));
148 m_addr
->sin_port
= servent
->s_port
;
152 bool wxIPV4address::Service(unsigned short port
)
154 m_addr
->sin_port
= htons(port
);
158 bool wxIPV4address::LocalHost()
160 static char buf
[256];
162 if (gethostname(buf
, sizeof(buf
)) < 0)
163 return Hostname("localhost");
165 return Hostname(buf
);
168 wxString
wxIPV4address::Hostname()
170 struct hostent
*h_ent
;
172 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
174 return wxString(h_ent
->h_name
);
177 unsigned short wxIPV4address::Service()
179 return ntohs(m_addr
->sin_port
);
182 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
184 addr
= (struct sockaddr
*)m_addr
;
185 len
= sizeof(*m_addr
);
188 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
190 if (len
!= sizeof(*m_addr
))
192 *m_addr
= *(struct sockaddr_in
*)addr
;
197 wxIPV6address::wxIPV6address()
199 m_addr
= new sockaddr_in6
;
203 wxIPV6address::~wxIPV6address()
207 int wxIPV6address::SockAddrLen()
209 return sizeof(*m_addr
);
212 int wxIPV6address::GetFamily()
217 void wxIPV6address::Clear()
219 memset(m_addr
, 0, sizeof(*m_addr
));
220 m_addr
->sin6_family
= AF_INET6
;
221 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
225 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
227 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
229 CHECK_ADDRTYPE(addr, wxIPV6address);
230 m_addr = ip_addr->m_addr;
235 bool wxIPV6address::Hostname(const wxString
& name
)
237 struct hostent
*hostent
;
238 struct in_addr
*addr
;
243 if (!name
.IsNumber()) {
244 hostent
= gethostbyname2((char*) name
, AF_INET6
);
252 addr
= (struct in6_addr
*) *(hostent
->h_addr_list
);
254 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
258 bool wxIPV6address::Hostname(unsigned char addr
[16])
260 m_addr
->sin6_addr
.s6_addr
= addr
;
264 bool wxIPV6address::Service(const char *name
)
266 struct servent
*servent
;
268 if (!name
|| !strlen(name
))
271 if (!isdigit(*name
)) {
272 if ((servent
= getservbyname((char*) name
, "tcp")) == 0)
275 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
276 m_addr
->sin_port
= htons(atoi(name
));
281 m_addr
->sin_port
= servent
->s_port
;
285 bool wxIPV6address::Service(unsigned short port
)
287 m_addr
->sin_port
= htons(port
);
291 bool wxIPV6address::LocalHost()
293 static char buf
[256];
295 if (gethostname(buf
, sizeof(buf
)) < 0)
296 return Hostname("localhost");
298 return Hostname(buf
);
301 const wxString
& wxIPV6address::Hostname()
303 struct hostent
*h_ent
;
305 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
307 return wxString(h_ent
->h_name
);
310 unsigned short wxIPV6address::Service()
312 return ntohs(m_addr
->sin_port
);
315 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
317 len
= sizeof(*m_addr
);
321 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
323 if (len
!= sizeof(*m_addr
))
325 *m_addr
= *(struct sockaddr_in6
*)addr
;
333 wxUNIXaddress::wxUNIXaddress()
335 m_addr
= new sockaddr_un
;
339 wxUNIXaddress::~wxUNIXaddress()
343 int wxUNIXaddress::SockAddrLen()
345 return sizeof(*m_addr
);
348 int wxUNIXaddress::GetFamily()
353 void wxUNIXaddress::Clear()
355 memset(m_addr
, 0, sizeof(m_addr
));
356 m_addr
->sun_family
= AF_UNIX
;
360 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
362 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
363 CHECK_ADDRTYPE(addr, wxUNIXaddress);
364 m_addr = unx_addr->m_addr;
369 void wxUNIXaddress::Filename(const wxString
& fname
)
371 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
374 wxString
wxUNIXaddress::Filename()
376 return wxString(m_addr
->sun_path
);
379 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
381 addr
= (struct sockaddr
*)m_addr
;
382 len
= sizeof(*m_addr
);
385 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
387 if (len
!= sizeof(*m_addr
))
389 *m_addr
= *(struct sockaddr_un
*)addr
;