]> git.saurik.com Git - wxWidgets.git/blob - include/wx/private/sckaddr.h
insert missing iclude file
[wxWidgets.git] / include / wx / private / sckaddr.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/sockaddr.h
3 // Purpose: wxSockAddressImpl
4 // Author: Vadim Zeitlin
5 // Created: 2008-12-28
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_PRIVATE_SOCKADDR_H_
12 #define _WX_PRIVATE_SOCKADDR_H_
13
14 #ifdef __WXMSW__
15 #include "wx/msw/wrapwin.h"
16
17 #if wxUSE_IPV6
18 #include <ws2tcpip.h>
19 #endif
20 #elif defined(__VMS__)
21 #include <socket.h>
22
23 struct sockaddr_un
24 {
25 u_char sun_len; /* sockaddr len including null */
26 u_char sun_family; /* AF_UNIX */
27 char sun_path[108]; /* path name (gag) */
28 };
29 #else // generic Unix
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <sys/un.h>
34 #endif // platform
35
36 #include <stdlib.h> // for calloc()
37
38 // this is a wrapper for sockaddr_storage if it's available or just sockaddr
39 // otherwise
40 union wxSockAddressStorage
41 {
42 #if wxUSE_IPV6
43 sockaddr_storage addr_storage;
44 #endif
45 sockaddr addr;
46 };
47
48 // ----------------------------------------------------------------------------
49 // helpers for wxSockAddressImpl
50 // ----------------------------------------------------------------------------
51
52 // helper class mapping sockaddr_xxx types to corresponding AF_XXX values
53 //
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 }; };
58
59 template <> struct AddressFamily<sockaddr_in> { enum { value = AF_INET }; };
60
61 #if wxUSE_IPV6
62 template <> struct AddressFamily<sockaddr_in6> { enum { value = AF_INET6 }; };
63 #endif // wxUSE_IPV6
64
65 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
66 template <> struct AddressFamily<sockaddr_un> { enum { value = AF_UNIX }; };
67 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
68
69 // ----------------------------------------------------------------------------
70 // wxSockAddressImpl
71 // ----------------------------------------------------------------------------
72
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).
77 //
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
85 {
86 public:
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)
89 enum Family
90 {
91 FAMILY_INET = PF_INET,
92 #if wxUSE_IPV6
93 FAMILY_INET6 = PF_INET6,
94 #endif
95 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
96 FAMILY_UNIX = PF_UNIX,
97 #endif
98 FAMILY_UNSPEC = PF_UNSPEC
99 };
100
101 // default ctor creates uninitialized object, use one of CreateXXX() below
102 wxSockAddressImpl()
103 {
104 InitUnspec();
105 }
106
107 // ctor from an existing sockaddr
108 wxSockAddressImpl(const sockaddr& addr, int len)
109 {
110 switch ( addr.sa_family )
111 {
112 case PF_INET:
113 #if wxUSE_IPV6
114 case PF_INET6:
115 #endif
116 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
117 case PF_UNIX:
118 #endif
119 m_family = static_cast<Family>(addr.sa_family);
120 break;
121
122 default:
123 wxFAIL_MSG( "unsupported socket address family" );
124 InitUnspec();
125 return;
126 }
127
128 InitFromSockaddr(addr, len);
129 }
130
131 // copy ctor and assignment operators
132 wxSockAddressImpl(const wxSockAddressImpl& other)
133 {
134 InitFromOther(other);
135 }
136
137 wxSockAddressImpl& operator=(const wxSockAddressImpl& other)
138 {
139 free(m_addr);
140
141 InitFromOther(other);
142
143 return *this;
144 }
145
146 // dtor frees the memory used by m_addr
147 ~wxSockAddressImpl()
148 {
149 free(m_addr);
150 }
151
152
153 // reset the address to the initial uninitialized state
154 void Clear()
155 {
156 free(m_addr);
157
158 InitUnspec();
159 }
160
161 // initialize the address to be of specific address family, it must be
162 // currently uninitialized (you may call Clear() to achieve this)
163 void CreateINET();
164 void CreateINET6();
165 #ifdef wxHAS_UNIX_DOMAIN_SOCKETS
166 void CreateUnix();
167 #endif // wxHAS_UNIX_DOMAIN_SOCKETS
168
169 // simple accessors
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; }
176
177 // accessors for INET or INET6 address families
178 #if wxUSE_IPV6
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()
183 #else
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
187
188 wxString GetHostName() const;
189 bool SetHostName(const wxString& name)
190 {
191 return CALL_IPV4_OR_6(SetHostName, (name));
192 }
193
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);
197
198 bool SetToAnyAddress() { return CALL_IPV4_OR_6_VOID(SetToAnyAddress); }
199
200 #undef CALL_IPV4_OR_6
201
202 // accessors for INET addresses only
203 bool GetHostAddress(wxUint32 *address) const;
204 bool SetHostAddress(wxUint32 address);
205
206 bool SetToBroadcastAddress() { return SetHostAddress(INADDR_BROADCAST); }
207
208 // accessors for INET6 addresses only
209 #if wxUSE_IPV6
210 bool GetHostAddress(in6_addr *address) const;
211 bool SetHostAddress(const in6_addr& address);
212 #endif // wxUSE_IPV6
213
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
219
220 private:
221 void DoAlloc(int len)
222 {
223 m_addr = static_cast<sockaddr *>(calloc(1, len));
224 m_len = len;
225 }
226
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
229 // hide it
230 template <class T>
231 T *Alloc(T *)
232 {
233 DoAlloc(sizeof(T));
234
235 return reinterpret_cast<T *>(m_addr);
236 }
237
238 template <class T>
239 T *Get(T *) const
240 {
241 wxCHECK_MSG( static_cast<int>(m_family) == AddressFamily<T>::value,
242 NULL,
243 "socket address family mismatch" );
244
245 return reinterpret_cast<T *>(m_addr);
246 }
247
248 void InitUnspec()
249 {
250 m_family = FAMILY_UNSPEC;
251 m_addr = NULL;
252 m_len = 0;
253 }
254
255 void InitFromSockaddr(const sockaddr& addr, int len)
256 {
257 DoAlloc(len);
258 memcpy(m_addr, &addr, len);
259 }
260
261 void InitFromOther(const wxSockAddressImpl& other)
262 {
263 m_family = other.m_family;
264
265 if ( other.m_addr )
266 {
267 InitFromSockaddr(*other.m_addr, other.m_len);
268 }
269 else // no address to copy
270 {
271 m_addr = NULL;
272 m_len = 0;
273 }
274 }
275
276 // IPv4/6 implementations of public functions
277 bool SetHostName4(const wxString& name);
278
279 bool SetPort4(wxUint16 port);
280 wxUint16 GetPort4() const;
281
282 bool SetToAnyAddress4() { return SetHostAddress(INADDR_ANY); }
283
284 #if wxUSE_IPV6
285 bool SetHostName6(const wxString& name);
286
287 bool SetPort6(wxUint16 port);
288 wxUint16 GetPort6() const;
289
290 bool SetToAnyAddress6();
291 #endif // wxUSE_IPV6
292
293 Family m_family;
294 sockaddr *m_addr;
295 int m_len;
296 };
297
298 #endif // _WX_PRIVATE_SOCKADDR_H_