Debian Codegraph @

srtcore/netinet_any.h

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 /*****************************************************************************
12 written by
13 Haivision Systems Inc.
14 *****************************************************************************/
15
16 #ifndef INC_SRT_NETINET_ANY_H
17 #define INC_SRT_NETINET_ANY_H
18
19 #include <cstring> // memcmp
20 #include <string>
21 #include <sstream>
22 #include "platform_sys.h"
23
24 // This structure should replace every use of sockaddr and its currently
25 // used specializations, sockaddr_in and sockaddr_in6. This is to simplify
26 // the use of the original BSD API that relies on type-violating type casts.
27 // You can use the instances of sockaddr_any in every place where sockaddr is
28 // required.
29
30 namespace srt
31 {
32
33 struct sockaddr_any
34 {
35 union
36 {
37 sockaddr_in sin;
38 sockaddr_in6 sin6;
39 sockaddr sa;
40 };
41
42 // The type is intended to be the same as the length
43 // parameter in ::accept, ::bind and ::connect functions.
44
45 // This is the type used by SRT.
46 typedef int len_t;
47
48 // This is the type used by system functions
49 #ifdef _WIN32
50 typedef int syslen_t;
51 #else
52 typedef socklen_t syslen_t;
53 #endif
54
55 // Note: by having `len_t` type here the usage in
56 // API functions is here limited to SRT. For system
57 // functions you can pass the address here as (socklen_t*)&sa.len,
58 // but just do it on your own risk, as there's no guarantee
59 // that sizes of `int` and `socklen_t` do not differ. The safest
60 // way seems to be using an intermediate proxy to be written
61 // back here from the value of `syslen_t`.
62 len_t len;
63
64 struct SysLenWrapper
65 {
66 syslen_t syslen;
67 len_t& backwriter;
68 syslen_t* operator&() { return &syslen; }
69
70 SysLenWrapper(len_t& source): syslen(source), backwriter(source)
71 {
72 }
73
74 ~SysLenWrapper()
75 {
76 backwriter = syslen;
77 }
78 };
79
80 // Usage:
81 // ::accept(lsn_sock, sa.get(), &sa.syslen());
82 SysLenWrapper syslen()
83 {
84 return SysLenWrapper(len);
85 }
86
87 static size_t storage_size()
88 {
89 typedef union
90 {
91 sockaddr_in sin;
92 sockaddr_in6 sin6;
93 sockaddr sa;
94 } ucopy;
95 return sizeof (ucopy);
96 }
97
98 void reset()
99 {
100 // sin6 is the largest field
101 memset((&sin6), 0, sizeof sin6);
102 len = 0;
103 }
104
105 // Default domain is unspecified, and
106 // in this case the size is 0.
107 // Note that AF_* (and alias PF_*) types have
108 // many various values, of which only
109 // AF_INET and AF_INET6 are handled here.
110 // Others make the same effect as unspecified.
111 explicit sockaddr_any(int domain = AF_UNSPEC)
112 {
113 // Default domain is "unspecified", 0
114 reset();
115
116 // Overriding family as required in the parameters
117 // and the size then accordingly.
118 sa.sa_family = domain == AF_INET || domain == AF_INET6 ? domain : AF_UNSPEC;
119 switch (domain)
120 {
121 case AF_INET:
122 len = len_t(sizeof (sockaddr_in));
123 break;
124
125 // Use size of sin6 as the default size
126 // len must be properly set so that the
127 // family-less sockaddr is passed to bind/accept
128 default:
129 len = len_t(sizeof (sockaddr_in6));
130 break;
131 }
132 }
133
134 sockaddr_any(const sockaddr_storage& stor)
135 {
136 // Here the length isn't passed, so just rely on family.
137 set((const sockaddr*)&stor);
138 }
139
140 sockaddr_any(const sockaddr* source, len_t namelen = 0)
141 {
142 if (namelen == 0)
143 set(source);
144 else
145 set(source, namelen);
146 }
147
148 void set(const sockaddr* source)
149 {
150 // Less safe version, simply trust the caller that the
151 // memory at 'source' is also large enough to contain
152 // all data required for particular family.
153 if (source->sa_family == AF_INET)
154 {
155 memcpy((&sin), source, sizeof sin);
156 len = sizeof sin;
157 }
158 else if (source->sa_family == AF_INET6)
159 {
160 memcpy((&sin6), source, sizeof sin6);
161 len = sizeof sin6;
162 }
163 else
164 {
165 // Error fallback: no other families than IP are regarded.
166 // Note: socket set up this way isn't intended to be used
167 // for bind/accept.
168 sa.sa_family = AF_UNSPEC;
169 len = 0;
170 }
171 }
172
173 void set(const sockaddr* source, syslen_t namelen)
174 {
175 // It's not safe to copy it directly, so check.
176 if (source->sa_family == AF_INET && namelen >= syslen_t(sizeof sin))
177 {
178 memcpy((&sin), source, sizeof sin);
179 len = sizeof sin;
180 }
181 else if (source->sa_family == AF_INET6 && namelen >= syslen_t(sizeof sin6))
182 {
183 // Note: this isn't too safe, may crash for stupid values
184 // of source->sa_family or any other data
185 // in the source structure, so make sure it's correct first.
186 memcpy((&sin6), source, sizeof sin6);
187 len = sizeof sin6;
188 }
189 else
190 {
191 reset();
192 }
193 }
194
195 void set(const sockaddr_in& in4)
196 {
197 memcpy((&sin), &in4, sizeof in4);
198 len = sizeof in4;
199 }
200
201 void set(const sockaddr_in6& in6)
202 {
203 memcpy((&sin6), &in6, sizeof in6);
204 len = sizeof in6;
205 }
206
207 sockaddr_any(const in_addr& i4_adr, uint16_t port)
208 {
209 // Some cases require separately IPv4 address passed as in_addr,
210 // so port is given separately.
211 sa.sa_family = AF_INET;
212 sin.sin_addr = i4_adr;
213 sin.sin_port = htons(port);
214 len = sizeof sin;
215 }
216
217 sockaddr_any(const in6_addr& i6_adr, uint16_t port)
218 {
219 sa.sa_family = AF_INET6;
220 sin6.sin6_addr = i6_adr;
221 sin6.sin6_port = htons(port);
222 len = sizeof sin6;
223 }
224
225 static len_t size(int family)
226 {
227 switch (family)
228 {
229 case AF_INET:
230 return len_t(sizeof (sockaddr_in));
231
232 case AF_INET6:
233 return len_t(sizeof (sockaddr_in6));
234
235 default:
236 return 0; // fallback
237 }
238 }
239
240 bool empty() const
241 {
242 bool isempty = true; // unspec-family address is always empty
243
244 if (sa.sa_family == AF_INET)
245 {
246 isempty = (sin.sin_port == 0
247 && sin.sin_addr.s_addr == 0);
248 }
249 else if (sa.sa_family == AF_INET6)
250 {
251 isempty = (sin6.sin6_port == 0
252 && memcmp(&sin6.sin6_addr, &in6addr_any, sizeof in6addr_any) == 0);
253 }
254 // otherwise isempty stays with default false
255 return isempty;
256 }
257
258 len_t size() const
259 {
260 return size(sa.sa_family);
261 }
262
263 int family() const { return sa.sa_family; }
264 void family(int val)
265 {
266 sa.sa_family = val;
267 len = size();
268 }
269
270 // port is in exactly the same location in both sin and sin6
271 // and has the same size. This is actually yet another common
272 // field, just not mentioned in the sockaddr structure.
273 uint16_t& r_port() { return sin.sin_port; }
274 uint16_t r_port() const { return sin.sin_port; }
275 int hport() const { return ntohs(sin.sin_port); }
276
277 void hport(int value)
278 {
279 // Port is fortunately located at the same position
280 // in both sockaddr_in and sockaddr_in6 and has the
281 // same size.
282 sin.sin_port = htons(value);
283 }
284
285 sockaddr* get() { return &sa; }
286 const sockaddr* get() const { return &sa; }
287
288 // Sometimes you need to get the address
289 // the way suitable for e.g. inet_ntop.
290 const void* get_addr() const
291 {
292 if (sa.sa_family == AF_INET)
293 return &sin.sin_addr.s_addr;
294
295 if (sa.sa_family == AF_INET6)
296 return &sin6.sin6_addr;
297
298 return NULL;
299 }
300
301 void* get_addr()
302 {
303 const sockaddr_any* that = this;
304 return (void*)that->get_addr();
305 }
306
307 template <int> struct TypeMap;
308
309 template <int af_domain>
310 typename TypeMap<af_domain>::type& get();
311
312 struct Equal
313 {
314 bool operator()(const sockaddr_any& c1, const sockaddr_any& c2)
315 {
316 if (c1.family() != c2.family())
317 return false;
318
319 // Cannot use memcmp due to having in some systems
320 // another field like sockaddr_in::sin_len. This exists
321 // in some BSD-derived systems, but is not required by POSIX.
322 // Therefore sockaddr_any class cannot operate with it,
323 // as in this situation it would be safest to state that
324 // particular implementations may have additional fields
325 // of different purpose beside those required by POSIX.
326 //
327 // The only reliable way to compare two underlying sockaddr
328 // object is then to compare the port value and the address
329 // value.
330 //
331 // Fortunately the port is 16-bit and located at the same
332 // offset in both sockaddr_in and sockaddr_in6.
333
334 return c1.sin.sin_port == c2.sin.sin_port
335 && c1.equal_address(c2);
336 }
337 };
338
339 struct EqualAddress
340 {
341 bool operator()(const sockaddr_any& c1, const sockaddr_any& c2)
342 {
343 if ( c1.sa.sa_family == AF_INET )
344 {
345 return c1.sin.sin_addr.s_addr == c2.sin.sin_addr.s_addr;
346 }
347
348 if ( c1.sa.sa_family == AF_INET6 )
349 {
350 return memcmp(&c1.sin6.sin6_addr, &c2.sin6.sin6_addr, sizeof (in6_addr)) == 0;
351 }
352
353 return false;
354 }
355
356 };
357
358 bool equal_address(const sockaddr_any& rhs) const
359 {
360 return EqualAddress()(*this, rhs);
361 }
362
363 struct Less
364 {
365 bool operator()(const sockaddr_any& c1, const sockaddr_any& c2)
366 {
367 return memcmp(&c1, &c2, sizeof(c1)) < 0;
368 }
369 };
370
371 // Tests if the current address is the "any" wildcard.
372 bool isany() const
373 {
374 if (sa.sa_family == AF_INET)
375 return sin.sin_addr.s_addr == INADDR_ANY;
376
377 if (sa.sa_family == AF_INET6)
378 return memcmp(&sin6.sin6_addr, &in6addr_any, sizeof in6addr_any) == 0;
379
380 return false;
381 }
382
383 // Debug support
384 std::string str() const
385 {
386 if (family() != AF_INET && family() != AF_INET6)
387 return "unknown:0";
388
389 std::ostringstream output;
390 char hostbuf[1024];
391 int flags;
392
393 #if ENABLE_GETNAMEINFO
394 flags = NI_NAMEREQD;
395 #else
396 flags = NI_NUMERICHOST | NI_NUMERICSERV;
397 #endif
398
399 if (!getnameinfo(get(), size(), hostbuf, 1024, NULL, 0, flags))
400 {
401 output << hostbuf;
402 }
403
404 output << ":" << hport();
405 return output.str();
406 }
407
408 bool operator==(const sockaddr_any& other) const
409 {
410 return Equal()(*this, other);
411 }
412
413 bool operator!=(const sockaddr_any& other) const { return !(*this == other); }
414 };
415
416 template<> struct sockaddr_any::TypeMap<AF_INET> { typedef sockaddr_in type; };
417 template<> struct sockaddr_any::TypeMap<AF_INET6> { typedef sockaddr_in6 type; };
418
419 template <>
420 inline sockaddr_any::TypeMap<AF_INET>::type& sockaddr_any::get<AF_INET>() { return sin; }
421 template <>
422 inline sockaddr_any::TypeMap<AF_INET6>::type& sockaddr_any::get<AF_INET6>() { return sin6; }
423
424 } // namespace srt
425
426 #endif