]>
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 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
53 #include "wx/sckaddr.h"
55 #define CHECK_ADDRTYPE(var, type)
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
59 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
61 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
64 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
68 wxIPV4address::wxIPV4address()
70 m_addr
= new sockaddr_in
;
74 wxIPV4address::~wxIPV4address()
78 int wxIPV4address::SockAddrLen()
80 return sizeof(*m_addr
);
83 int wxIPV4address::GetFamily()
88 void wxIPV4address::Clear()
90 memset(m_addr
, 0, sizeof(*m_addr
));
91 m_addr
->sin_family
= AF_INET
;
92 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
96 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
98 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
99 CHECK_ADDRTYPE(addr, wxIPV4address);
100 m_addr = ip_addr->m_addr;
105 bool wxIPV4address::Hostname(const wxString
& name
)
107 struct hostent
*hostent
;
108 struct in_addr
*addr
;
113 if (!name
.IsNumber()) {
114 if ((hostent
= gethostbyname(name
.GetData())) == 0) {
118 long len_addr
= inet_addr(name
.GetData());
121 m_addr
->sin_addr
.s_addr
= len_addr
;
125 addr
= (struct in_addr
*) *(hostent
->h_addr_list
);
127 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
131 bool wxIPV4address::Hostname(unsigned long addr
)
133 m_addr
->sin_addr
.s_addr
= htonl(addr
);
137 bool wxIPV4address::Service(const wxString
& name
)
139 struct servent
*servent
;
144 if (!name
.IsNumber()) {
145 if ((servent
= getservbyname(name
, "tcp")) == 0)
148 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
149 m_addr
->sin_port
= htons(atoi(name
));
154 m_addr
->sin_port
= servent
->s_port
;
158 bool wxIPV4address::Service(unsigned short port
)
160 m_addr
->sin_port
= htons(port
);
164 bool wxIPV4address::LocalHost()
166 static char buf
[256];
168 if (gethostname(buf
, sizeof(buf
)) < 0)
169 return Hostname("localhost");
171 return Hostname(buf
);
174 wxString
wxIPV4address::Hostname()
176 struct hostent
*h_ent
;
178 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
180 return wxString(h_ent
->h_name
);
183 unsigned short wxIPV4address::Service()
185 return ntohs(m_addr
->sin_port
);
188 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
190 addr
= (struct sockaddr
*)m_addr
;
191 len
= sizeof(*m_addr
);
194 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
196 if (len
!= sizeof(*m_addr
))
198 *m_addr
= *(struct sockaddr_in
*)addr
;
203 wxIPV6address::wxIPV6address()
205 m_addr
= new sockaddr_in6
;
209 wxIPV6address::~wxIPV6address()
213 int wxIPV6address::SockAddrLen()
215 return sizeof(*m_addr
);
218 int wxIPV6address::GetFamily()
223 void wxIPV6address::Clear()
225 memset(m_addr
, 0, sizeof(*m_addr
));
226 m_addr
->sin6_family
= AF_INET6
;
227 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
231 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
233 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
235 CHECK_ADDRTYPE(addr, wxIPV6address);
236 m_addr = ip_addr->m_addr;
241 bool wxIPV6address::Hostname(const wxString
& name
)
243 struct hostent
*hostent
;
244 struct in_addr
*addr
;
249 if (!name
.IsNumber()) {
250 hostent
= gethostbyname2((char*) name
, AF_INET6
);
258 addr
= (struct in6_addr
*) *(hostent
->h_addr_list
);
260 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
264 bool wxIPV6address::Hostname(unsigned char addr
[16])
266 m_addr
->sin6_addr
.s6_addr
= addr
;
270 bool wxIPV6address::Service(const char *name
)
272 struct servent
*servent
;
274 if (!name
|| !strlen(name
))
277 if (!isdigit(*name
)) {
278 if ((servent
= getservbyname((char*) name
, "tcp")) == 0)
281 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
282 m_addr
->sin_port
= htons(atoi(name
));
287 m_addr
->sin_port
= servent
->s_port
;
291 bool wxIPV6address::Service(unsigned short port
)
293 m_addr
->sin_port
= htons(port
);
297 bool wxIPV6address::LocalHost()
299 static char buf
[256];
301 if (gethostname(buf
, sizeof(buf
)) < 0)
302 return Hostname("localhost");
304 return Hostname(buf
);
307 const wxString
& wxIPV6address::Hostname()
309 struct hostent
*h_ent
;
311 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
313 return wxString(h_ent
->h_name
);
316 unsigned short wxIPV6address::Service()
318 return ntohs(m_addr
->sin_port
);
321 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
323 len
= sizeof(*m_addr
);
327 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
329 if (len
!= sizeof(*m_addr
))
331 *m_addr
= *(struct sockaddr_in6
*)addr
;
339 wxUNIXaddress::wxUNIXaddress()
341 m_addr
= new sockaddr_un
;
345 wxUNIXaddress::~wxUNIXaddress()
349 int wxUNIXaddress::SockAddrLen()
351 return sizeof(*m_addr
);
354 int wxUNIXaddress::GetFamily()
359 void wxUNIXaddress::Clear()
361 memset(m_addr
, 0, sizeof(m_addr
));
362 m_addr
->sun_family
= AF_UNIX
;
366 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
368 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
369 CHECK_ADDRTYPE(addr, wxUNIXaddress);
370 m_addr = unx_addr->m_addr;
375 void wxUNIXaddress::Filename(const wxString
& fname
)
377 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
380 wxString
wxUNIXaddress::Filename()
382 return wxString(m_addr
->sun_path
);
385 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
387 addr
= (struct sockaddr
*)m_addr
;
388 len
= sizeof(*m_addr
);
391 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
393 if (len
!= sizeof(*m_addr
))
395 *m_addr
= *(struct sockaddr_un
*)addr
;