]>
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__)
51 #if defined(__FreeBSD__) || defined (__NetBSD__)
52 #include <sys/types.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
64 #include "wx/sckaddr.h"
66 #define CHECK_ADDRTYPE(var, type)
68 #if !USE_SHARED_LIBRARY
69 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
70 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
72 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
75 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
79 wxIPV4address::wxIPV4address()
81 m_addr
= new sockaddr_in
;
85 wxIPV4address::~wxIPV4address()
89 int wxIPV4address::SockAddrLen()
91 return sizeof(*m_addr
);
94 int wxIPV4address::GetFamily()
99 void wxIPV4address::Clear()
101 memset(m_addr
, 0, sizeof(*m_addr
));
102 m_addr
->sin_family
= AF_INET
;
103 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
107 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
109 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
110 CHECK_ADDRTYPE(addr, wxIPV4address);
111 m_addr = ip_addr->m_addr;
116 bool wxIPV4address::Hostname(const wxString
& name
)
118 struct hostent
*theHostent
;
119 struct in_addr
*addr
;
124 if (!name
.IsNumber()) {
125 if ((theHostent
= gethostbyname(name
.GetData())) == 0) {
130 long len_addr
= inet_addr(name
.GetData()).s_addr
;
132 long len_addr
= inet_addr(name
.GetData());
136 m_addr
->sin_addr
.s_addr
= len_addr
;
140 addr
= (struct in_addr
*) *(theHostent
->h_addr_list
);
142 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
146 bool wxIPV4address::Hostname(unsigned long addr
)
148 m_addr
->sin_addr
.s_addr
= htonl(addr
);
152 bool wxIPV4address::Service(const wxString
& name
)
154 struct servent
*theServent
;
159 if (!name
.IsNumber()) {
160 if ((theServent
= getservbyname(name
, "tcp")) == 0)
163 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
164 m_addr
->sin_port
= htons(atoi(name
));
169 m_addr
->sin_port
= theServent
->s_port
;
173 bool wxIPV4address::Service(unsigned short port
)
175 m_addr
->sin_port
= htons(port
);
179 bool wxIPV4address::LocalHost()
181 static char buf
[256];
183 if (gethostname(buf
, sizeof(buf
)) < 0)
184 return Hostname("localhost");
186 return Hostname(buf
);
189 wxString
wxIPV4address::Hostname()
191 struct hostent
*h_ent
;
193 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
195 return wxString(h_ent
->h_name
);
198 unsigned short wxIPV4address::Service()
200 return ntohs(m_addr
->sin_port
);
203 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
205 addr
= (struct sockaddr
*)m_addr
;
206 len
= sizeof(*m_addr
);
209 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
211 if (len
!= sizeof(*m_addr
))
213 *m_addr
= *(struct sockaddr_in
*)addr
;
218 wxIPV6address::wxIPV6address()
220 m_addr
= new sockaddr_in6
;
224 wxIPV6address::~wxIPV6address()
228 int wxIPV6address::SockAddrLen()
230 return sizeof(*m_addr
);
233 int wxIPV6address::GetFamily()
238 void wxIPV6address::Clear()
240 memset(m_addr
, 0, sizeof(*m_addr
));
241 m_addr
->sin6_family
= AF_INET6
;
242 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
246 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
248 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
250 CHECK_ADDRTYPE(addr, wxIPV6address);
251 m_addr = ip_addr->m_addr;
256 bool wxIPV6address::Hostname(const wxString
& name
)
258 struct hostent
*theHostent
;
259 struct in_addr
*addr
;
264 if (!name
.IsNumber()) {
265 hostent
= gethostbyname2((char*) name
, AF_INET6
);
273 addr
= (struct in6_addr
*) *(theHostent
->h_addr_list
);
275 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
279 bool wxIPV6address::Hostname(unsigned char addr
[16])
281 m_addr
->sin6_addr
.s6_addr
= addr
;
285 bool wxIPV6address::Service(const char *name
)
287 struct servent
*theServent
;
289 if (!name
|| !strlen(name
))
292 if (!isdigit(*name
)) {
293 if ((theServent
= getservbyname((char*) name
, "tcp")) == 0)
296 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
297 m_addr
->sin_port
= htons(atoi(name
));
302 m_addr
->sin_port
= theServent
->s_port
;
306 bool wxIPV6address::Service(unsigned short port
)
308 m_addr
->sin_port
= htons(port
);
312 bool wxIPV6address::LocalHost()
314 static char buf
[256];
316 if (gethostname(buf
, sizeof(buf
)) < 0)
317 return Hostname("localhost");
319 return Hostname(buf
);
322 const wxString
& wxIPV6address::Hostname()
324 struct hostent
*h_ent
;
326 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
328 return wxString(h_ent
->h_name
);
331 unsigned short wxIPV6address::Service()
333 return ntohs(m_addr
->sin_port
);
336 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
338 len
= sizeof(*m_addr
);
342 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
344 if (len
!= sizeof(*m_addr
))
346 *m_addr
= *(struct sockaddr_in6
*)addr
;
354 wxUNIXaddress::wxUNIXaddress()
356 m_addr
= new sockaddr_un
;
360 wxUNIXaddress::~wxUNIXaddress()
364 int wxUNIXaddress::SockAddrLen()
366 return sizeof(*m_addr
);
369 int wxUNIXaddress::GetFamily()
374 void wxUNIXaddress::Clear()
376 memset(m_addr
, 0, sizeof(m_addr
));
377 m_addr
->sun_family
= AF_UNIX
;
381 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
383 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
384 CHECK_ADDRTYPE(addr, wxUNIXaddress);
385 m_addr = unx_addr->m_addr;
390 void wxUNIXaddress::Filename(const wxString
& fname
)
392 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
395 wxString
wxUNIXaddress::Filename()
397 return wxString(m_addr
->sun_path
);
400 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
402 addr
= (struct sockaddr
*)m_addr
;
403 len
= sizeof(*m_addr
);
406 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
408 if (len
!= sizeof(*m_addr
))
410 *m_addr
= *(struct sockaddr_un
*)addr
;