1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/sockaddr.h
3 // Purpose: wxSockAddressImpl
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_PRIVATE_SOCKADDR_H_
12 #define _WX_PRIVATE_SOCKADDR_H_
15 #include "wx/msw/wrapwin.h"
20 #elif defined(__VMS__)
25 u_char sun_len
; /* sockaddr len including null */
26 u_char sun_family
; /* AF_UNIX */
27 char sun_path
[108]; /* path name (gag) */
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
37 #include <stdlib.h> // for calloc()
39 // this is a wrapper for sockaddr_storage if it's available or just sockaddr
41 union wxSockAddressStorage
44 sockaddr_storage addr_storage
;
49 // ----------------------------------------------------------------------------
50 // helpers for wxSockAddressImpl
51 // ----------------------------------------------------------------------------
53 // helper class mapping sockaddr_xxx types to corresponding AF_XXX values
55 // FIXME-VC6: we could leave the template undefined if not for VC6 which
56 // absolutely does need to have a generic version defining the
57 // template "interface" to compile the code below
58 template <class T
> struct AddressFamily
{ enum { value
= AF_UNSPEC
}; };
60 template <> struct AddressFamily
<sockaddr_in
> { enum { value
= AF_INET
}; };
63 template <> struct AddressFamily
<sockaddr_in6
> { enum { value
= AF_INET6
}; };
66 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
67 template <> struct AddressFamily
<sockaddr_un
> { enum { value
= AF_UNIX
}; };
68 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // Represents a socket endpoint, e.g. an (address, port) pair for PF_INET
75 // sockets. It can be initialized from an existing sockaddr struct and also
76 // provides access to sockaddr stored internally so that it can be easily used
77 // with e.g. connect(2).
79 // This class also performs (synchronous, hence potentially long) name lookups
80 // if necessary, i.e. if the host name strings don't contain addresses in
81 // numerical form (quad dotted for IPv4 or standard hexadecimal for IPv6).
82 // Notice that internally the potentially Unicode host names are encoded as
83 // UTF-8 before being passed to the lookup function but the host names should
84 // really be ASCII anyhow.
85 class wxSockAddressImpl
88 // as this is passed to socket() it should be a PF_XXX and not AF_XXX (even
89 // though they're the same in practice)
92 FAMILY_INET
= PF_INET
,
94 FAMILY_INET6
= PF_INET6
,
96 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
97 FAMILY_UNIX
= PF_UNIX
,
99 FAMILY_UNSPEC
= PF_UNSPEC
102 // default ctor creates uninitialized object, use one of CreateXXX() below
108 // ctor from an existing sockaddr
109 wxSockAddressImpl(const sockaddr
& addr
, int len
)
111 switch ( addr
.sa_family
)
117 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
120 m_family
= static_cast<Family
>(addr
.sa_family
);
124 wxFAIL_MSG( "unsupported socket address family" );
129 InitFromSockaddr(addr
, len
);
132 // copy ctor and assignment operators
133 wxSockAddressImpl(const wxSockAddressImpl
& other
)
135 InitFromOther(other
);
138 wxSockAddressImpl
& operator=(const wxSockAddressImpl
& other
)
142 InitFromOther(other
);
147 // dtor frees the memory used by m_addr
154 // reset the address to the initial uninitialized state
162 // initialize the address to be of specific address family, it must be
163 // currently uninitialized (you may call Clear() to achieve this)
166 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
168 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
169 void Create(Family family
)
183 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
187 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
190 wxFAIL_MSG( "unsupported socket address family" );
195 Family
GetFamily() const { return m_family
; }
196 bool Is(Family family
) const { return m_family
== family
; }
197 bool IsOk() const { return m_family
!= FAMILY_UNSPEC
; }
198 const sockaddr
*GetAddr() const { return m_addr
; }
199 sockaddr
*GetWritableAddr() { return m_addr
; }
200 int GetLen() const { return m_len
; }
202 // accessors for INET or INET6 address families
204 #define CALL_IPV4_OR_6(func, args) \
205 Is(FAMILY_INET6) ? func##6(args) : func##4(args)
206 #define CALL_IPV4_OR_6_VOID(func) \
207 Is(FAMILY_INET6) ? func##6() : func##4()
209 #define CALL_IPV4_OR_6(func, args) func##4(args)
210 #define CALL_IPV4_OR_6_VOID(func) func##4()
211 #endif // IPv6 support on/off
213 wxString
GetHostName() const;
214 bool SetHostName(const wxString
& name
)
216 return CALL_IPV4_OR_6(SetHostName
, (name
));
219 wxUint16
GetPort() const { return CALL_IPV4_OR_6_VOID(GetPort
); }
220 bool SetPort(wxUint16 port
) { return CALL_IPV4_OR_6(SetPort
, (port
)); }
221 bool SetPortName(const wxString
& name
, const char *protocol
);
223 bool SetToAnyAddress() { return CALL_IPV4_OR_6_VOID(SetToAnyAddress
); }
225 #undef CALL_IPV4_OR_6
227 // accessors for INET addresses only
228 bool GetHostAddress(wxUint32
*address
) const;
229 bool SetHostAddress(wxUint32 address
);
231 bool SetToBroadcastAddress() { return SetHostAddress(INADDR_BROADCAST
); }
233 // accessors for INET6 addresses only
235 bool GetHostAddress(in6_addr
*address
) const;
236 bool SetHostAddress(const in6_addr
& address
);
239 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
240 // methods valid for Unix address family addresses only
241 bool SetPath(const wxString
& path
);
242 wxString
GetPath() const;
243 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
246 void DoAlloc(int len
)
248 m_addr
= static_cast<sockaddr
*>(calloc(1, len
));
252 // FIXME-VC6: VC6 doesn't grok Foo<T>() call syntax so we need the extra
253 // dummy parameter of type T, use the macros in sckaddr.cpp to
260 return reinterpret_cast<T
*>(m_addr
);
266 wxCHECK_MSG( static_cast<int>(m_family
) == AddressFamily
<T
>::value
,
268 "socket address family mismatch" );
270 return reinterpret_cast<T
*>(m_addr
);
275 m_family
= FAMILY_UNSPEC
;
280 void InitFromSockaddr(const sockaddr
& addr
, int len
)
283 memcpy(m_addr
, &addr
, len
);
286 void InitFromOther(const wxSockAddressImpl
& other
)
288 m_family
= other
.m_family
;
292 InitFromSockaddr(*other
.m_addr
, other
.m_len
);
294 else // no address to copy
301 // IPv4/6 implementations of public functions
302 bool SetHostName4(const wxString
& name
);
304 bool SetPort4(wxUint16 port
);
305 wxUint16
GetPort4() const;
307 bool SetToAnyAddress4() { return SetHostAddress(INADDR_ANY
); }
310 bool SetHostName6(const wxString
& name
);
312 bool SetPort6(wxUint16 port
);
313 wxUint16
GetPort6() const;
315 bool SetToAnyAddress6();
323 #endif // _WX_PRIVATE_SOCKADDR_H_