]>
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/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
54 #include "wx/sckaddr.h"
56 #define CHECK_ADDRTYPE(var, type)
58 #if !USE_SHARED_LIBRARY
59 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
60 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
62 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
65 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
69 wxIPV4address::wxIPV4address()
71 m_addr
= new sockaddr_in
;
75 wxIPV4address::~wxIPV4address()
79 int wxIPV4address::SockAddrLen()
81 return sizeof(*m_addr
);
84 int wxIPV4address::GetFamily()
89 void wxIPV4address::Clear()
91 memset(m_addr
, 0, sizeof(*m_addr
));
92 m_addr
->sin_family
= AF_INET
;
93 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
97 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
99 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
100 CHECK_ADDRTYPE(addr, wxIPV4address);
101 m_addr = ip_addr->m_addr;
106 bool wxIPV4address::Hostname(const wxString
& name
)
108 struct hostent
*hostent
;
109 struct in_addr
*addr
;
114 if (!name
.IsNumber()) {
115 if ((hostent
= gethostbyname(name
.GetData())) == 0) {
119 long len_addr
= inet_addr(name
.GetData());
122 m_addr
->sin_addr
.s_addr
= len_addr
;
126 addr
= (struct in_addr
*) *(hostent
->h_addr_list
);
128 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
132 bool wxIPV4address::Hostname(unsigned long addr
)
134 m_addr
->sin_addr
.s_addr
= htonl(addr
);
138 bool wxIPV4address::Service(const wxString
& name
)
140 struct servent
*servent
;
145 if (!name
.IsNumber()) {
146 if ((servent
= getservbyname(name
, "tcp")) == 0)
149 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
150 m_addr
->sin_port
= htons(atoi(name
));
155 m_addr
->sin_port
= servent
->s_port
;
159 bool wxIPV4address::Service(unsigned short port
)
161 m_addr
->sin_port
= htons(port
);
165 bool wxIPV4address::LocalHost()
167 static char buf
[256];
169 if (gethostname(buf
, sizeof(buf
)) < 0)
170 return Hostname("localhost");
172 return Hostname(buf
);
175 wxString
wxIPV4address::Hostname()
177 struct hostent
*h_ent
;
179 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
181 return wxString(h_ent
->h_name
);
184 unsigned short wxIPV4address::Service()
186 return ntohs(m_addr
->sin_port
);
189 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
191 addr
= (struct sockaddr
*)m_addr
;
192 len
= sizeof(*m_addr
);
195 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
197 if (len
!= sizeof(*m_addr
))
199 *m_addr
= *(struct sockaddr_in
*)addr
;
204 wxIPV6address::wxIPV6address()
206 m_addr
= new sockaddr_in6
;
210 wxIPV6address::~wxIPV6address()
214 int wxIPV6address::SockAddrLen()
216 return sizeof(*m_addr
);
219 int wxIPV6address::GetFamily()
224 void wxIPV6address::Clear()
226 memset(m_addr
, 0, sizeof(*m_addr
));
227 m_addr
->sin6_family
= AF_INET6
;
228 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
232 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
234 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
236 CHECK_ADDRTYPE(addr, wxIPV6address);
237 m_addr = ip_addr->m_addr;
242 bool wxIPV6address::Hostname(const wxString
& name
)
244 struct hostent
*hostent
;
245 struct in_addr
*addr
;
250 if (!name
.IsNumber()) {
251 hostent
= gethostbyname2((char*) name
, AF_INET6
);
259 addr
= (struct in6_addr
*) *(hostent
->h_addr_list
);
261 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
265 bool wxIPV6address::Hostname(unsigned char addr
[16])
267 m_addr
->sin6_addr
.s6_addr
= addr
;
271 bool wxIPV6address::Service(const char *name
)
273 struct servent
*servent
;
275 if (!name
|| !strlen(name
))
278 if (!isdigit(*name
)) {
279 if ((servent
= getservbyname((char*) name
, "tcp")) == 0)
282 if ((servent
= getservbyport(atoi(name
), "tcp")) == 0) {
283 m_addr
->sin_port
= htons(atoi(name
));
288 m_addr
->sin_port
= servent
->s_port
;
292 bool wxIPV6address::Service(unsigned short port
)
294 m_addr
->sin_port
= htons(port
);
298 bool wxIPV6address::LocalHost()
300 static char buf
[256];
302 if (gethostname(buf
, sizeof(buf
)) < 0)
303 return Hostname("localhost");
305 return Hostname(buf
);
308 const wxString
& wxIPV6address::Hostname()
310 struct hostent
*h_ent
;
312 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
314 return wxString(h_ent
->h_name
);
317 unsigned short wxIPV6address::Service()
319 return ntohs(m_addr
->sin_port
);
322 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
324 len
= sizeof(*m_addr
);
328 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
330 if (len
!= sizeof(*m_addr
))
332 *m_addr
= *(struct sockaddr_in6
*)addr
;
340 wxUNIXaddress::wxUNIXaddress()
342 m_addr
= new sockaddr_un
;
346 wxUNIXaddress::~wxUNIXaddress()
350 int wxUNIXaddress::SockAddrLen()
352 return sizeof(*m_addr
);
355 int wxUNIXaddress::GetFamily()
360 void wxUNIXaddress::Clear()
362 memset(m_addr
, 0, sizeof(m_addr
));
363 m_addr
->sun_family
= AF_UNIX
;
367 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
369 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
370 CHECK_ADDRTYPE(addr, wxUNIXaddress);
371 m_addr = unx_addr->m_addr;
376 void wxUNIXaddress::Filename(const wxString
& fname
)
378 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
381 wxString
wxUNIXaddress::Filename()
383 return wxString(m_addr
->sun_path
);
386 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
388 addr
= (struct sockaddr
*)m_addr
;
389 len
= sizeof(*m_addr
);
392 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
394 if (len
!= sizeof(*m_addr
))
396 *m_addr
= *(struct sockaddr_un
*)addr
;