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