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"
16 #elif defined(__VMS__)
21 u_char sun_len
; /* sockaddr len including null */
22 u_char sun_family
; /* AF_UNIX */
23 char sun_path
[108]; /* path name (gag) */
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
32 #include <stdlib.h> // for calloc()
34 // this is a wrapper for sockaddr_storage if it's available or just sockaddr
36 union wxSockAddressStorage
39 sockaddr_storage addr_storage
;
44 // ----------------------------------------------------------------------------
45 // helpers for wxSockAddressImpl
46 // ----------------------------------------------------------------------------
48 // helper class mapping sockaddr_xxx types to corresponding AF_XXX values
50 // FIXME-VC6: we could leave the template undefined if not for VC6 which
51 // absolutely does need to have a generic version defining the
52 // template "interface" to compile the code below
53 template <class T
> struct AddressFamily
{ enum { value
= AF_UNSPEC
}; };
55 template <> struct AddressFamily
<sockaddr_in
> { enum { value
= AF_INET
}; };
58 template <> struct AddressFamily
<sockaddr_in6
> { enum { value
= AF_INET6
}; };
61 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
62 template <> struct AddressFamily
<sockaddr_un
> { enum { value
= AF_UNIX
}; };
63 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // Represents a socket endpoint, e.g. an (address, port) pair for PF_INET
70 // sockets. It can be initialized from an existing sockaddr struct and also
71 // provides access to sockaddr stored internally so that it can be easily used
72 // with e.g. connect(2).
74 // This class also performs (synchronous, hence potentially long) name lookups
75 // if necessary, i.e. if the host name strings don't contain addresses in
76 // numerical form (quad dotted for IPv4 or standard hexadecimal for IPv6).
77 // Notice that internally the potentially Unicode host names are encoded as
78 // UTF-8 before being passed to the lookup function but the host names should
79 // really be ASCII anyhow.
80 class wxSockAddressImpl
83 // as this is passed to socket() it should be a PF_XXX and not AF_XXX (even
84 // though they're the same in practice)
87 FAMILY_INET
= PF_INET
,
89 FAMILY_INET6
= PF_INET6
,
91 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
92 FAMILY_UNIX
= PF_UNIX
,
94 FAMILY_UNSPEC
= PF_UNSPEC
97 // default ctor creates uninitialized object, use one of CreateXXX() below
103 // ctor from an existing sockaddr
104 wxSockAddressImpl(const sockaddr
& addr
, int len
)
106 switch ( addr
.sa_family
)
112 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
115 m_family
= static_cast<Family
>(addr
.sa_family
);
119 wxFAIL_MSG( "unsupported socket address family" );
124 InitFromSockaddr(addr
, len
);
127 // copy ctor and assignment operators
128 wxSockAddressImpl(const wxSockAddressImpl
& other
)
130 InitFromOther(other
);
133 wxSockAddressImpl
& operator=(const wxSockAddressImpl
& other
)
137 InitFromOther(other
);
142 // dtor frees the memory used by m_addr
149 // reset the address to the initial uninitialized state
157 // initialize the address to be of specific address family, it must be
158 // currently uninitialized (you may call Clear() to achieve this)
161 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
163 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
166 Family
GetFamily() const { return m_family
; }
167 bool Is(Family family
) const { return m_family
== family
; }
168 bool IsOk() const { return m_family
!= FAMILY_UNSPEC
; }
169 const sockaddr
*GetAddr() const { return m_addr
; }
170 sockaddr
*GetWritableAddr() { return m_addr
; }
171 int GetLen() const { return m_len
; }
173 // accessors for INET or INET6 address families
175 #define CALL_IPV4_OR_6(func, args) \
176 Is(FAMILY_INET6) ? func##6(args) : func##4(args)
177 #define CALL_IPV4_OR_6_VOID(func) \
178 Is(FAMILY_INET6) ? func##6() : func##4()
180 #define CALL_IPV4_OR_6(func, args) func##4(args)
181 #define CALL_IPV4_OR_6_VOID(func) func##4()
182 #endif // IPv6 support on/off
184 wxString
GetHostName() const;
185 bool SetHostName(const wxString
& name
)
187 return CALL_IPV4_OR_6(SetHostName
, (name
));
190 wxUint16
GetPort() const { return CALL_IPV4_OR_6_VOID(GetPort
); }
191 bool SetPort(wxUint16 port
) { return CALL_IPV4_OR_6(SetPort
, (port
)); }
192 bool SetPortName(const wxString
& name
, const char *protocol
);
194 bool SetToAnyAddress() { return CALL_IPV4_OR_6_VOID(SetToAnyAddress
); }
196 #undef CALL_IPV4_OR_6
198 // accessors for INET addresses only
199 bool GetHostAddress(wxUint32
*address
) const;
200 bool SetHostAddress(wxUint32 address
);
202 bool SetToBroadcastAddress() { return SetHostAddress(INADDR_BROADCAST
); }
204 // accessors for INET6 addresses only
206 bool GetHostAddress(in6_addr
*address
) const;
207 bool SetHostAddress(const in6_addr
& address
);
210 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
211 // methods valid for Unix address family addresses only
212 bool SetPath(const wxString
& path
);
213 wxString
GetPath() const;
214 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
217 void DoAlloc(int len
)
219 m_addr
= static_cast<sockaddr
*>(calloc(1, len
));
223 // FIXME-VC6: VC6 doesn't grok Foo<T>() call syntax so we need the extra
224 // dummy parameter of type T, use the macros in sckaddr.cpp to
231 return reinterpret_cast<T
*>(m_addr
);
237 wxCHECK_MSG( static_cast<int>(m_family
) == AddressFamily
<T
>::value
,
239 "socket address family mismatch" );
241 return reinterpret_cast<T
*>(m_addr
);
246 m_family
= FAMILY_UNSPEC
;
251 void InitFromSockaddr(const sockaddr
& addr
, int len
)
254 memcpy(m_addr
, &addr
, len
);
257 void InitFromOther(const wxSockAddressImpl
& other
)
259 m_family
= other
.m_family
;
263 InitFromSockaddr(*other
.m_addr
, other
.m_len
);
265 else // no address to copy
272 // IPv4/6 implementations of public functions
273 bool SetHostName4(const wxString
& name
);
275 bool SetPort4(wxUint16 port
);
276 wxUint16
GetPort4() const;
278 bool SetToAnyAddress4() { return SetHostAddress(INADDR_ANY
); }
281 bool SetHostName6(const wxString
& name
);
283 bool SetPort6(wxUint16 port
);
284 wxUint16
GetPort6() const;
286 bool SetToAnyAddress6();
294 #endif // _WX_PRIVATE_SOCKADDR_H_