]>
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"
30 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
35 #include "wx/object.h"
37 #if defined(__WXMAC__)
38 #include "/wx/mac/macsock.h"
41 #if defined(__WINDOWS__)
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
61 struct hostent
*gethostbyname(const char *name
);
67 #include "wx/sckaddr.h"
69 #define CHECK_ADDRTYPE(var, type)
71 #if !USE_SHARED_LIBRARY
72 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
73 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
75 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
78 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
82 wxIPV4address::wxIPV4address()
84 m_addr
= new sockaddr_in
;
88 wxIPV4address::~wxIPV4address()
92 int wxIPV4address::SockAddrLen()
94 return sizeof(*m_addr
);
97 int wxIPV4address::GetFamily()
102 void wxIPV4address::Clear()
104 memset(m_addr
, 0, sizeof(*m_addr
));
105 m_addr
->sin_family
= AF_INET
;
106 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
110 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
112 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
113 CHECK_ADDRTYPE(addr, wxIPV4address);
114 m_addr = ip_addr->m_addr;
119 bool wxIPV4address::Hostname(const wxString
& name
)
121 struct hostent
*theHostent
;
122 struct in_addr
*addr
;
127 if (!name
.IsNumber()) {
128 if ((theHostent
= gethostbyname(name
.GetData())) == 0) {
133 long len_addr
= inet_addr(name
.GetData()).s_addr
;
135 long len_addr
= inet_addr(name
.GetData());
139 m_addr
->sin_addr
.s_addr
= len_addr
;
143 addr
= (struct in_addr
*) *(theHostent
->h_addr_list
);
145 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
149 bool wxIPV4address::Hostname(unsigned long addr
)
151 m_addr
->sin_addr
.s_addr
= htonl(addr
);
155 bool wxIPV4address::Service(const wxString
& name
)
157 struct servent
*theServent
;
162 if (!name
.IsNumber()) {
163 if ((theServent
= getservbyname(name
, "tcp")) == 0)
166 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
167 m_addr
->sin_port
= htons(atoi(name
));
172 m_addr
->sin_port
= theServent
->s_port
;
176 bool wxIPV4address::Service(unsigned short port
)
178 m_addr
->sin_port
= htons(port
);
182 bool wxIPV4address::LocalHost()
184 static char buf
[256];
186 if (gethostname(buf
, sizeof(buf
)) < 0)
187 return Hostname("localhost");
189 return Hostname(buf
);
192 wxString
wxIPV4address::Hostname()
194 struct hostent
*h_ent
;
196 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
202 return wxString(h_ent
->h_name
);
205 unsigned short wxIPV4address::Service()
207 return ntohs(m_addr
->sin_port
);
210 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
212 addr
= (struct sockaddr
*)m_addr
;
213 len
= sizeof(*m_addr
);
216 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
218 if (len
!= sizeof(*m_addr
))
220 *m_addr
= *(struct sockaddr_in
*)addr
;
225 wxIPV6address::wxIPV6address()
227 m_addr
= new sockaddr_in6
;
231 wxIPV6address::~wxIPV6address()
235 int wxIPV6address::SockAddrLen()
237 return sizeof(*m_addr
);
240 int wxIPV6address::GetFamily()
245 void wxIPV6address::Clear()
247 memset(m_addr
, 0, sizeof(*m_addr
));
248 m_addr
->sin6_family
= AF_INET6
;
249 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
253 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
255 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
257 CHECK_ADDRTYPE(addr, wxIPV6address);
258 m_addr = ip_addr->m_addr;
263 bool wxIPV6address::Hostname(const wxString
& name
)
265 struct hostent
*theHostent
;
266 struct in_addr
*addr
;
271 if (!name
.IsNumber()) {
272 hostent
= gethostbyname2((char*) name
, AF_INET6
);
280 addr
= (struct in6_addr
*) *(theHostent
->h_addr_list
);
282 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
286 bool wxIPV6address::Hostname(unsigned char addr
[16])
288 m_addr
->sin6_addr
.s6_addr
= addr
;
292 bool wxIPV6address::Service(const char *name
)
294 struct servent
*theServent
;
296 if (!name
|| !strlen(name
))
299 if (!isdigit(*name
)) {
300 if ((theServent
= getservbyname((char*) name
, "tcp")) == 0)
303 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
304 m_addr
->sin_port
= htons(atoi(name
));
309 m_addr
->sin_port
= theServent
->s_port
;
313 bool wxIPV6address::Service(unsigned short port
)
315 m_addr
->sin_port
= htons(port
);
319 bool wxIPV6address::LocalHost()
321 static char buf
[256];
323 if (gethostname(buf
, sizeof(buf
)) < 0)
324 return Hostname("localhost");
326 return Hostname(buf
);
329 const wxString
& wxIPV6address::Hostname()
331 struct hostent
*h_ent
;
333 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
335 return wxString(h_ent
->h_name
);
338 unsigned short wxIPV6address::Service()
340 return ntohs(m_addr
->sin_port
);
343 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
345 len
= sizeof(*m_addr
);
349 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
351 if (len
!= sizeof(*m_addr
))
353 *m_addr
= *(struct sockaddr_in6
*)addr
;
361 wxUNIXaddress::wxUNIXaddress()
363 m_addr
= new sockaddr_un
;
367 wxUNIXaddress::~wxUNIXaddress()
371 int wxUNIXaddress::SockAddrLen()
373 return sizeof(*m_addr
);
376 int wxUNIXaddress::GetFamily()
381 void wxUNIXaddress::Clear()
383 memset(m_addr
, 0, sizeof(m_addr
));
384 m_addr
->sun_family
= AF_UNIX
;
388 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
390 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
391 CHECK_ADDRTYPE(addr, wxUNIXaddress);
392 m_addr = unx_addr->m_addr;
397 void wxUNIXaddress::Filename(const wxString
& fname
)
399 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
402 wxString
wxUNIXaddress::Filename()
404 return wxString(m_addr
->sun_path
);
407 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
409 addr
= (struct sockaddr
*)m_addr
;
410 len
= sizeof(*m_addr
);
413 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
415 if (len
!= sizeof(*m_addr
))
417 *m_addr
= *(struct sockaddr_un
*)addr
;