]>
git.saurik.com Git - wxWidgets.git/blob - src/common/sckaddr.cpp
4c73c3c1e608a3eba2ed9c7e38a015642f45e1a0
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
),
198 return wxString(h_ent
->h_name
);
201 unsigned short wxIPV4address::Service()
203 return ntohs(m_addr
->sin_port
);
206 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
208 addr
= (struct sockaddr
*)m_addr
;
209 len
= sizeof(*m_addr
);
212 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
214 if (len
!= sizeof(*m_addr
))
216 *m_addr
= *(struct sockaddr_in
*)addr
;
221 wxIPV6address::wxIPV6address()
223 m_addr
= new sockaddr_in6
;
227 wxIPV6address::~wxIPV6address()
231 int wxIPV6address::SockAddrLen()
233 return sizeof(*m_addr
);
236 int wxIPV6address::GetFamily()
241 void wxIPV6address::Clear()
243 memset(m_addr
, 0, sizeof(*m_addr
));
244 m_addr
->sin6_family
= AF_INET6
;
245 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
249 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
251 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
253 CHECK_ADDRTYPE(addr, wxIPV6address);
254 m_addr = ip_addr->m_addr;
259 bool wxIPV6address::Hostname(const wxString
& name
)
261 struct hostent
*theHostent
;
262 struct in_addr
*addr
;
267 if (!name
.IsNumber()) {
268 hostent
= gethostbyname2((char*) name
, AF_INET6
);
276 addr
= (struct in6_addr
*) *(theHostent
->h_addr_list
);
278 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
282 bool wxIPV6address::Hostname(unsigned char addr
[16])
284 m_addr
->sin6_addr
.s6_addr
= addr
;
288 bool wxIPV6address::Service(const char *name
)
290 struct servent
*theServent
;
292 if (!name
|| !strlen(name
))
295 if (!isdigit(*name
)) {
296 if ((theServent
= getservbyname((char*) name
, "tcp")) == 0)
299 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
300 m_addr
->sin_port
= htons(atoi(name
));
305 m_addr
->sin_port
= theServent
->s_port
;
309 bool wxIPV6address::Service(unsigned short port
)
311 m_addr
->sin_port
= htons(port
);
315 bool wxIPV6address::LocalHost()
317 static char buf
[256];
319 if (gethostname(buf
, sizeof(buf
)) < 0)
320 return Hostname("localhost");
322 return Hostname(buf
);
325 const wxString
& wxIPV6address::Hostname()
327 struct hostent
*h_ent
;
329 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
331 return wxString(h_ent
->h_name
);
334 unsigned short wxIPV6address::Service()
336 return ntohs(m_addr
->sin_port
);
339 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
341 len
= sizeof(*m_addr
);
345 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
347 if (len
!= sizeof(*m_addr
))
349 *m_addr
= *(struct sockaddr_in6
*)addr
;
357 wxUNIXaddress::wxUNIXaddress()
359 m_addr
= new sockaddr_un
;
363 wxUNIXaddress::~wxUNIXaddress()
367 int wxUNIXaddress::SockAddrLen()
369 return sizeof(*m_addr
);
372 int wxUNIXaddress::GetFamily()
377 void wxUNIXaddress::Clear()
379 memset(m_addr
, 0, sizeof(m_addr
));
380 m_addr
->sun_family
= AF_UNIX
;
384 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
386 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
387 CHECK_ADDRTYPE(addr, wxUNIXaddress);
388 m_addr = unx_addr->m_addr;
393 void wxUNIXaddress::Filename(const wxString
& fname
)
395 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
398 wxString
wxUNIXaddress::Filename()
400 return wxString(m_addr
->sun_path
);
403 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
405 addr
= (struct sockaddr
*)m_addr
;
406 len
= sizeof(*m_addr
);
409 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
411 if (len
!= sizeof(*m_addr
))
413 *m_addr
= *(struct sockaddr_un
*)addr
;