netinet_any.h 11 KB

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