]>
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
);
64 int gethostname(char *name
, int namelen
);
70 #include "wx/sckaddr.h"
72 #define CHECK_ADDRTYPE(var, type)
74 #if !USE_SHARED_LIBRARY
75 IMPLEMENT_ABSTRACT_CLASS(wxSockAddress
, wxObject
)
76 IMPLEMENT_DYNAMIC_CLASS(wxIPV4address
, wxSockAddress
)
78 IMPLEMENT_DYNAMIC_CLASS(wxIPV6address
, wxSockAddress
)
81 IMPLEMENT_DYNAMIC_CLASS(wxUNIXaddress
, wxSockAddress
)
85 wxIPV4address::wxIPV4address()
87 m_addr
= new sockaddr_in
;
91 wxIPV4address::~wxIPV4address()
95 int wxIPV4address::SockAddrLen()
97 return sizeof(*m_addr
);
100 int wxIPV4address::GetFamily()
105 void wxIPV4address::Clear()
107 memset(m_addr
, 0, sizeof(*m_addr
));
108 m_addr
->sin_family
= AF_INET
;
109 m_addr
->sin_addr
.s_addr
= INADDR_ANY
;
113 const wxSockAddress& wxIPV4address::operator =(const wxSockAddress& addr)
115 wxIPV4address *ip_addr = (wxIPV4address *)&addr;
116 CHECK_ADDRTYPE(addr, wxIPV4address);
117 m_addr = ip_addr->m_addr;
122 bool wxIPV4address::Hostname(const wxString
& name
)
124 struct hostent
*theHostent
;
125 struct in_addr
*addr
;
130 if (!name
.IsNumber()) {
131 if ((theHostent
= gethostbyname(name
.GetData())) == 0) {
136 long len_addr
= inet_addr(name
.GetData()).s_addr
;
138 long len_addr
= inet_addr(name
.GetData());
142 m_addr
->sin_addr
.s_addr
= len_addr
;
146 addr
= (struct in_addr
*) *(theHostent
->h_addr_list
);
148 m_addr
->sin_addr
.s_addr
= addr
[0].s_addr
;
152 bool wxIPV4address::Hostname(unsigned long addr
)
154 m_addr
->sin_addr
.s_addr
= htonl(addr
);
158 bool wxIPV4address::Service(const wxString
& name
)
160 struct servent
*theServent
;
165 if (!name
.IsNumber()) {
166 if ((theServent
= getservbyname(name
, "tcp")) == 0)
169 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
170 m_addr
->sin_port
= htons(atoi(name
));
175 m_addr
->sin_port
= theServent
->s_port
;
179 bool wxIPV4address::Service(unsigned short port
)
181 m_addr
->sin_port
= htons(port
);
185 bool wxIPV4address::LocalHost()
187 static char buf
[256];
189 if (gethostname(buf
, sizeof(buf
)) < 0)
190 return Hostname("localhost");
192 return Hostname(buf
);
195 wxString
wxIPV4address::Hostname()
197 struct hostent
*h_ent
;
199 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
205 return wxString(h_ent
->h_name
);
208 unsigned short wxIPV4address::Service()
210 return ntohs(m_addr
->sin_port
);
213 void wxIPV4address::Build(struct sockaddr
*&addr
, size_t& len
)
215 addr
= (struct sockaddr
*)m_addr
;
216 len
= sizeof(*m_addr
);
219 void wxIPV4address::Disassemble(struct sockaddr
*addr
, size_t len
)
221 if (len
!= sizeof(*m_addr
))
223 *m_addr
= *(struct sockaddr_in
*)addr
;
228 wxIPV6address::wxIPV6address()
230 m_addr
= new sockaddr_in6
;
234 wxIPV6address::~wxIPV6address()
238 int wxIPV6address::SockAddrLen()
240 return sizeof(*m_addr
);
243 int wxIPV6address::GetFamily()
248 void wxIPV6address::Clear()
250 memset(m_addr
, 0, sizeof(*m_addr
));
251 m_addr
->sin6_family
= AF_INET6
;
252 m_addr
->sin6_addr
.s_addr
= INADDR_ANY
;
256 const wxSockAddress& wxIPV6address::operator =(const wxSockAddress& addr)
258 wxIPV6address *ip_addr = (wxIPV6address *)&addr;
260 CHECK_ADDRTYPE(addr, wxIPV6address);
261 m_addr = ip_addr->m_addr;
266 bool wxIPV6address::Hostname(const wxString
& name
)
268 struct hostent
*theHostent
;
269 struct in_addr
*addr
;
274 if (!name
.IsNumber()) {
275 hostent
= gethostbyname2((char*) name
, AF_INET6
);
283 addr
= (struct in6_addr
*) *(theHostent
->h_addr_list
);
285 m_addr
->sin6_addr
.s6_addr
= addr
[0].s6_addr
;
289 bool wxIPV6address::Hostname(unsigned char addr
[16])
291 m_addr
->sin6_addr
.s6_addr
= addr
;
295 bool wxIPV6address::Service(const char *name
)
297 struct servent
*theServent
;
299 if (!name
|| !strlen(name
))
302 if (!isdigit(*name
)) {
303 if ((theServent
= getservbyname((char*) name
, "tcp")) == 0)
306 if ((theServent
= getservbyport(atoi(name
), "tcp")) == 0) {
307 m_addr
->sin_port
= htons(atoi(name
));
312 m_addr
->sin_port
= theServent
->s_port
;
316 bool wxIPV6address::Service(unsigned short port
)
318 m_addr
->sin_port
= htons(port
);
322 bool wxIPV6address::LocalHost()
324 static char buf
[256];
326 if (gethostname(buf
, sizeof(buf
)) < 0)
327 return Hostname("localhost");
329 return Hostname(buf
);
332 const wxString
& wxIPV6address::Hostname()
334 struct hostent
*h_ent
;
336 h_ent
= gethostbyaddr((char *)&(m_addr
->sin_addr
), sizeof(m_addr
->sin_addr
),
338 return wxString(h_ent
->h_name
);
341 unsigned short wxIPV6address::Service()
343 return ntohs(m_addr
->sin_port
);
346 void wxIPV6address::Build(struct sockaddr
& *addr
, size_t& len
)
348 len
= sizeof(*m_addr
);
352 void wxIPV6address::Disassemble(struct sockaddr
& *addr
, size_t len
)
354 if (len
!= sizeof(*m_addr
))
356 *m_addr
= *(struct sockaddr_in6
*)addr
;
364 wxUNIXaddress::wxUNIXaddress()
366 m_addr
= new sockaddr_un
;
370 wxUNIXaddress::~wxUNIXaddress()
374 int wxUNIXaddress::SockAddrLen()
376 return sizeof(*m_addr
);
379 int wxUNIXaddress::GetFamily()
384 void wxUNIXaddress::Clear()
386 memset(m_addr
, 0, sizeof(m_addr
));
387 m_addr
->sun_family
= AF_UNIX
;
391 const wxSockAddress& wxUNIXaddress::operator =(const wxSockAddress& addr)
393 wxUNIXaddress *unx_addr = (wxUNIXaddress *)&addr;
394 CHECK_ADDRTYPE(addr, wxUNIXaddress);
395 m_addr = unx_addr->m_addr;
400 void wxUNIXaddress::Filename(const wxString
& fname
)
402 sprintf(m_addr
->sun_path
, "%s", WXSTRINGCAST fname
);
405 wxString
wxUNIXaddress::Filename()
407 return wxString(m_addr
->sun_path
);
410 void wxUNIXaddress::Build(struct sockaddr
*& addr
, size_t& len
)
412 addr
= (struct sockaddr
*)m_addr
;
413 len
= sizeof(*m_addr
);
416 void wxUNIXaddress::Disassemble(struct sockaddr
*addr
, size_t len
)
418 if (len
!= sizeof(*m_addr
))
420 *m_addr
= *(struct sockaddr_un
*)addr
;