]>
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 #if !defined(__MWERKS__) && !defined(__SALFORDC__)
37 #include "wx/object.h"
39 #if defined(__WXMAC__)
40 #include "/wx/mac/macsock.h"
43 #if defined(__WINDOWS__)
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
63 struct hostent
*gethostbyname(const char *name
);
69 #include "wx/sckaddr.h"
71 #define CHECK_ADDRTYPE(var, type)
73 #if !USE_SHARED_LIBRARY
74 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
75 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
77 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
80 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
84 wxIPV4address::wxIPV4address()
86 m_addr
= new sockaddr_in
;
90 wxIPV4address::~wxIPV4address()
94 int wxIPV4address::SockAddrLen()
96 return sizeof(*m_addr
);
99 int wxIPV4address::GetFamily()
104 void wxIPV4address::Clear()
106 memset(m_addr
, 0, sizeof(*m_addr
));
107 m_addr
->sin_family
= AF_INET
;
108 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
112 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
114 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
115 CHECK_ADDRTYPE(addr, wxIPV4address);
116 m_addr = ip_addr->m_addr;
121 bool wxIPV4address::Hostname(const wxString
& name
)
123 struct hostent
*theHostent
;
124 struct in_addr
*addr
;
129 if (!name
.IsNumber()) {
130 if ((theHostent
= gethostbyname(name
.GetData())) == 0) {
135 long len_addr
= inet_addr(name
.GetData()).s_addr
;
137 long len_addr
= inet_addr(name
.GetData());
141 m_addr
->sin_addr
.s_addr
= len_addr
;
145 addr
= (struct in_addr
*) *(theHostent
->h_addr_list
);
147 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
151 bool wxIPV4address::Hostname(unsigned long addr
)
153 m_addr
->sin_addr
.s_addr
= htonl(addr
);
157 bool wxIPV4address::Service(const wxString
& name
)
159 struct servent
*theServent
;
164 if (!name
.IsNumber()) {
165 if ((theServent
= getservbyname(name
, "tcp")) == 0)
168 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
169 m_addr
->sin_port
= htons(atoi(name
));
174 m_addr
->sin_port
= theServent
->s_port
;
178 bool wxIPV4address::Service(unsigned short port
)
180 m_addr
->sin_port
= htons(port
);
184 bool wxIPV4address::LocalHost()
186 static char buf
[256];
188 if (gethostname(buf
, sizeof(buf
)) < 0)
189 return Hostname("localhost");
191 return Hostname(buf
);
194 wxString
wxIPV4address::Hostname()
196 struct hostent
*h_ent
;
198 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
204 return wxString(h_ent
->h_name
);
207 unsigned short wxIPV4address::Service()
209 return ntohs(m_addr
->sin_port
);
212 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
214 addr
= (struct sockaddr
*)m_addr
;
215 len
= sizeof(*m_addr
);
218 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
220 if (len
!= sizeof(*m_addr
))
222 *m_addr
= *(struct sockaddr_in
*)addr
;
227 wxIPV6address::wxIPV6address()
229 m_addr
= new sockaddr_in6
;
233 wxIPV6address::~wxIPV6address()
237 int wxIPV6address::SockAddrLen()
239 return sizeof(*m_addr
);
242 int wxIPV6address::GetFamily()
247 void wxIPV6address::Clear()
249 memset(m_addr
, 0, sizeof(*m_addr
));
250 m_addr
->sin6_family
= AF_INET6
;
251 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
255 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
257 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
259 CHECK_ADDRTYPE(addr, wxIPV6address);
260 m_addr = ip_addr->m_addr;
265 bool wxIPV6address::Hostname(const wxString
& name
)
267 struct hostent
*theHostent
;
268 struct in_addr
*addr
;
273 if (!name
.IsNumber()) {
274 hostent
= gethostbyname2((char*) name
, AF_INET6
);
282 addr
= (struct in6_addr
*) *(theHostent
->h_addr_list
);
284 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
288 bool wxIPV6address::Hostname(unsigned char addr
[16])
290 m_addr
->sin6_addr
.s6_addr
= addr
;
294 bool wxIPV6address::Service(const char *name
)
296 struct servent
*theServent
;
298 if (!name
|| !strlen(name
))
301 if (!isdigit(*name
)) {
302 if ((theServent
= getservbyname((char*) name
, "tcp")) == 0)
305 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
306 m_addr
->sin_port
= htons(atoi(name
));
311 m_addr
->sin_port
= theServent
->s_port
;
315 bool wxIPV6address::Service(unsigned short port
)
317 m_addr
->sin_port
= htons(port
);
321 bool wxIPV6address::LocalHost()
323 static char buf
[256];
325 if (gethostname(buf
, sizeof(buf
)) < 0)
326 return Hostname("localhost");
328 return Hostname(buf
);
331 const wxString
& wxIPV6address::Hostname()
333 struct hostent
*h_ent
;
335 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
337 return wxString(h_ent
->h_name
);
340 unsigned short wxIPV6address::Service()
342 return ntohs(m_addr
->sin_port
);
345 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
347 len
= sizeof(*m_addr
);
351 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
353 if (len
!= sizeof(*m_addr
))
355 *m_addr
= *(struct sockaddr_in6
*)addr
;
363 wxUNIXaddress::wxUNIXaddress()
365 m_addr
= new sockaddr_un
;
369 wxUNIXaddress::~wxUNIXaddress()
373 int wxUNIXaddress::SockAddrLen()
375 return sizeof(*m_addr
);
378 int wxUNIXaddress::GetFamily()
383 void wxUNIXaddress::Clear()
385 memset(m_addr
, 0, sizeof(m_addr
));
386 m_addr
->sun_family
= AF_UNIX
;
390 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
392 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
393 CHECK_ADDRTYPE(addr, wxUNIXaddress);
394 m_addr = unx_addr->m_addr;
399 void wxUNIXaddress::Filename(const wxString
& fname
)
401 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
404 wxString
wxUNIXaddress::Filename()
406 return wxString(m_addr
->sun_path
);
409 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
411 addr
= (struct sockaddr
*)m_addr
;
412 len
= sizeof(*m_addr
);
415 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
417 if (len
!= sizeof(*m_addr
))
419 *m_addr
= *(struct sockaddr_un
*)addr
;