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) */
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
36 #include <stdlib.h> // for calloc()
38 // this is a wrapper for sockaddr_storage if it's available or just sockaddr
40 union wxSockAddressStorage
43 sockaddr_storage addr_storage
;
48 // ----------------------------------------------------------------------------
49 // helpers for wxSockAddressImpl
50 // ----------------------------------------------------------------------------
52 // helper class mapping sockaddr_xxx types to corresponding AF_XXX values
54 // FIXME-VC6: we could leave the template undefined if not for VC6 which
55 // absolutely does need to have a generic version defining the
56 // template "interface" to compile the code below
57 template <class T
> struct AddressFamily
{ enum { value
= AF_UNSPEC
}; };
59 template <> struct AddressFamily
<sockaddr_in
> { enum { value
= AF_INET
}; };
62 template <> struct AddressFamily
<sockaddr_in6
> { enum { value
= AF_INET6
}; };
65 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
66 template <> struct AddressFamily
<sockaddr_un
> { enum { value
= AF_UNIX
}; };
67 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // Represents a socket endpoint, e.g. an (address, port) pair for PF_INET
74 // sockets. It can be initialized from an existing sockaddr struct and also
75 // provides access to sockaddr stored internally so that it can be easily used
76 // with e.g. connect(2).
78 // This class also performs (synchronous, hence potentially long) name lookups
79 // if necessary, i.e. if the host name strings don't contain addresses in
80 // numerical form (quad dotted for IPv4 or standard hexadecimal for IPv6).
81 // Notice that internally the potentially Unicode host names are encoded as
82 // UTF-8 before being passed to the lookup function but the host names should
83 // really be ASCII anyhow.
84 class wxSockAddressImpl
87 // as this is passed to socket() it should be a PF_XXX and not AF_XXX (even
88 // though they're the same in practice)
91 FAMILY_INET
= PF_INET
,
93 FAMILY_INET6
= PF_INET6
,
95 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
96 FAMILY_UNIX
= PF_UNIX
,
98 FAMILY_UNSPEC
= PF_UNSPEC
101 // default ctor creates uninitialized object, use one of CreateXXX() below
107 // ctor from an existing sockaddr
108 wxSockAddressImpl(const sockaddr
& addr
, int len
)
110 switch ( addr
.sa_family
)
116 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
119 m_family
= static_cast<Family
>(addr
.sa_family
);
123 wxFAIL_MSG( "unsupported socket address family" );
128 InitFromSockaddr(addr
, len
);
131 // copy ctor and assignment operators
132 wxSockAddressImpl(const wxSockAddressImpl
& other
)
134 InitFromOther(other
);
137 wxSockAddressImpl
& operator=(const wxSockAddressImpl
& other
)
141 InitFromOther(other
);
146 // dtor frees the memory used by m_addr
153 // reset the address to the initial uninitialized state
161 // initialize the address to be of specific address family, it must be
162 // currently uninitialized (you may call Clear() to achieve this)
165 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
167 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
170 Family
GetFamily() const { return m_family
; }
171 bool Is(Family family
) const { return m_family
== family
; }
172 bool IsOk() const { return m_family
!= FAMILY_UNSPEC
; }
173 const sockaddr
*GetAddr() const { return m_addr
; }
174 sockaddr
*GetWritableAddr() { return m_addr
; }
175 int GetLen() const { return m_len
; }
177 // accessors for INET or INET6 address families
179 #define CALL_IPV4_OR_6(func, args) \
180 Is(FAMILY_INET6) ? func##6(args) : func##4(args)
181 #define CALL_IPV4_OR_6_VOID(func) \
182 Is(FAMILY_INET6) ? func##6() : func##4()
184 #define CALL_IPV4_OR_6(func, args) func##4(args)
185 #define CALL_IPV4_OR_6_VOID(func) func##4()
186 #endif // IPv6 support on/off
188 wxString
GetHostName() const;
189 bool SetHostName(const wxString
& name
)
191 return CALL_IPV4_OR_6(SetHostName
, (name
));
194 wxUint16
GetPort() const { return CALL_IPV4_OR_6_VOID(GetPort
); }
195 bool SetPort(wxUint16 port
) { return CALL_IPV4_OR_6(SetPort
, (port
)); }
196 bool SetPortName(const wxString
& name
, const char *protocol
);
198 bool SetToAnyAddress() { return CALL_IPV4_OR_6_VOID(SetToAnyAddress
); }
200 #undef CALL_IPV4_OR_6
202 // accessors for INET addresses only
203 bool GetHostAddress(wxUint32
*address
) const;
204 bool SetHostAddress(wxUint32 address
);
206 bool SetToBroadcastAddress() { return SetHostAddress(INADDR_BROADCAST
); }
208 // accessors for INET6 addresses only
210 bool GetHostAddress(in6_addr
*address
) const;
211 bool SetHostAddress(const in6_addr
& address
);
214 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
215 // methods valid for Unix address family addresses only
216 bool SetPath(const wxString
& path
);
217 wxString
GetPath() const;
218 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
221 void DoAlloc(int len
)
223 m_addr
= static_cast<sockaddr
*>(calloc(1, len
));
227 // FIXME-VC6: VC6 doesn't grok Foo<T>() call syntax so we need the extra
228 // dummy parameter of type T, use the macros in sckaddr.cpp to
235 return reinterpret_cast<T
*>(m_addr
);
241 wxCHECK_MSG( static_cast<int>(m_family
) == AddressFamily
<T
>::value
,
243 "socket address family mismatch" );
245 return reinterpret_cast<T
*>(m_addr
);
250 m_family
= FAMILY_UNSPEC
;
255 void InitFromSockaddr(const sockaddr
& addr
, int len
)
258 memcpy(m_addr
, &addr
, len
);
261 void InitFromOther(const wxSockAddressImpl
& other
)
263 m_family
= other
.m_family
;
267 InitFromSockaddr(*other
.m_addr
, other
.m_len
);
269 else // no address to copy
276 // IPv4/6 implementations of public functions
277 bool SetHostName4(const wxString
& name
);
279 bool SetPort4(wxUint16 port
);
280 wxUint16
GetPort4() const;
282 bool SetToAnyAddress4() { return SetHostAddress(INADDR_ANY
); }
285 bool SetHostName6(const wxString
& name
);
287 bool SetPort6(wxUint16 port
);
288 wxUint16
GetPort6() const;
290 bool SetToAnyAddress6();
298 #endif // _WX_PRIVATE_SOCKADDR_H_