channel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are
  15. met:
  16. * Redistributions of source code must retain the above
  17. copyright notice, this list of conditions and the
  18. following disclaimer.
  19. * Redistributions in binary form must reproduce the
  20. above copyright notice, this list of conditions
  21. and the following disclaimer in the documentation
  22. and/or other materials provided with the distribution.
  23. * Neither the name of the University of Illinois
  24. nor the names of its contributors may be used to
  25. endorse or promote products derived from this
  26. software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *****************************************************************************/
  39. /*****************************************************************************
  40. written by
  41. Yunhong Gu, last updated 01/27/2011
  42. modified by
  43. Haivision Systems Inc.
  44. *****************************************************************************/
  45. #ifndef INC_SRT_CHANNEL_H
  46. #define INC_SRT_CHANNEL_H
  47. #include "platform_sys.h"
  48. #include "udt.h"
  49. #include "packet.h"
  50. #include "socketconfig.h"
  51. #include "netinet_any.h"
  52. namespace srt
  53. {
  54. class CChannel
  55. {
  56. void createSocket(int family);
  57. public:
  58. // XXX There's currently no way to access the socket ID set for
  59. // whatever the channel is currently working for. Required to find
  60. // some way to do this, possibly by having a "reverse pointer".
  61. // Currently just "unimplemented".
  62. std::string CONID() const { return ""; }
  63. CChannel();
  64. ~CChannel();
  65. /// Open a UDP channel.
  66. /// @param [in] addr The local address that UDP will use.
  67. void open(const sockaddr_any& addr);
  68. void open(int family);
  69. /// Open a UDP channel based on an existing UDP socket.
  70. /// @param [in] udpsock UDP socket descriptor.
  71. void attach(UDPSOCKET udpsock, const sockaddr_any& adr);
  72. /// Disconnect and close the UDP entity.
  73. void close() const;
  74. /// Get the UDP sending buffer size.
  75. /// @return Current UDP sending buffer size.
  76. int getSndBufSize();
  77. /// Get the UDP receiving buffer size.
  78. /// @return Current UDP receiving buffer size.
  79. int getRcvBufSize();
  80. /// Query the socket address that the channel is using.
  81. /// @param [out] addr pointer to store the returned socket address.
  82. void getSockAddr(sockaddr_any& addr) const;
  83. /// Query the peer side socket address that the channel is connect to.
  84. /// @param [out] addr pointer to store the returned socket address.
  85. void getPeerAddr(sockaddr_any& addr) const;
  86. /// Send a packet to the given address.
  87. /// @param [in] addr pointer to the destination address.
  88. /// @param [in] packet reference to a CPacket entity.
  89. /// @param [in] src source address to sent on an outgoing packet (if not ANY)
  90. /// @return Actual size of data sent.
  91. int sendto(const sockaddr_any& addr, srt::CPacket& packet, const sockaddr_any& src) const;
  92. /// Receive a packet from the channel and record the source address.
  93. /// @param [in] addr pointer to the source address.
  94. /// @param [in] packet reference to a CPacket entity.
  95. /// @return Actual size of data received.
  96. EReadStatus recvfrom(sockaddr_any& addr, srt::CPacket& packet) const;
  97. void setConfig(const CSrtMuxerConfig& config);
  98. void getSocketOption(int level, int sockoptname, char* pw_dataptr, socklen_t& w_len, int& w_status);
  99. template<class Type>
  100. Type sockopt(int level, int sockoptname, Type deflt)
  101. {
  102. Type retval;
  103. socklen_t socklen = sizeof retval;
  104. int status;
  105. getSocketOption(level, sockoptname, ((char*)&retval), (socklen), (status));
  106. if (status == -1)
  107. return deflt;
  108. return retval;
  109. }
  110. /// Get the IP TTL.
  111. /// @param [in] ttl IP Time To Live.
  112. /// @return TTL.
  113. int getIpTTL() const;
  114. /// Get the IP Type of Service.
  115. /// @return ToS.
  116. int getIpToS() const;
  117. #ifdef SRT_ENABLE_BINDTODEVICE
  118. bool getBind(char* dst, size_t len);
  119. #endif
  120. int ioctlQuery(int type) const;
  121. int sockoptQuery(int level, int option) const;
  122. const sockaddr* bindAddress() { return m_BindAddr.get(); }
  123. const sockaddr_any& bindAddressAny() { return m_BindAddr; }
  124. private:
  125. void setUDPSockOpt();
  126. private:
  127. UDPSOCKET m_iSocket; // socket descriptor
  128. // Mutable because when querying original settings
  129. // this comprises the cache for extracted values,
  130. // although the object itself isn't considered modified.
  131. mutable CSrtMuxerConfig m_mcfg; // Note: ReuseAddr is unused and ineffective.
  132. sockaddr_any m_BindAddr;
  133. // This feature is not enabled on Windows, for now.
  134. // This is also turned off in case of MinGW
  135. #ifdef SRT_ENABLE_PKTINFO
  136. bool m_bBindMasked; // True if m_BindAddr is INADDR_ANY. Need for quick check.
  137. // Calculating the required space is extremely tricky, and whereas on most
  138. // platforms it's possible to define it this way:
  139. //
  140. // size_t s = max( CMSG_SPACE(sizeof(in_pktinfo)), CMSG_SPACE(sizeof(in6_pktinfo)) )
  141. //
  142. // ...on some platforms however CMSG_SPACE macro can't be resolved as constexpr.
  143. //
  144. // This structure is exclusively used to determine the required size for
  145. // CMSG buffer so that it can be allocated in a solid block with CChannel.
  146. // NOT TO BE USED to access any data inside the CMSG message.
  147. struct CMSGNodeIPv4
  148. {
  149. in_pktinfo in4;
  150. size_t extrafill;
  151. cmsghdr hdr;
  152. };
  153. struct CMSGNodeIPv6
  154. {
  155. in6_pktinfo in6;
  156. size_t extrafill;
  157. cmsghdr hdr;
  158. };
  159. // This is 'mutable' because it's a utility buffer defined here
  160. // to avoid unnecessary re-allocations.
  161. mutable char m_acCmsgRecvBuffer [sizeof (CMSGNodeIPv4) + sizeof (CMSGNodeIPv6)]; // Reserved space for ancillary data with pktinfo
  162. mutable char m_acCmsgSendBuffer [sizeof (CMSGNodeIPv4) + sizeof (CMSGNodeIPv6)]; // Reserved space for ancillary data with pktinfo
  163. // IMPORTANT!!! This function shall be called EXCLUSIVELY just after
  164. // calling ::recvmsg function. It uses a static buffer to supply data
  165. // for the call, and it's stated that only one thread is trying to
  166. // use a CChannel object in receiving mode.
  167. sockaddr_any getTargetAddress(const msghdr& msg) const
  168. {
  169. // Loop through IP header messages
  170. cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
  171. for (cmsg = CMSG_FIRSTHDR(&msg);
  172. cmsg != NULL;
  173. cmsg = CMSG_NXTHDR(((msghdr*)&msg), cmsg))
  174. {
  175. // This should be safe - this packet contains always either
  176. // IPv4 headers or IPv6 headers.
  177. if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
  178. {
  179. in_pktinfo *dest_ip_ptr = (in_pktinfo*)CMSG_DATA(cmsg);
  180. return sockaddr_any(dest_ip_ptr->ipi_addr, 0);
  181. }
  182. if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO)
  183. {
  184. in6_pktinfo* dest_ip_ptr = (in6_pktinfo*)CMSG_DATA(cmsg);
  185. return sockaddr_any(dest_ip_ptr->ipi6_addr, 0);
  186. }
  187. }
  188. // Fallback for an error
  189. return sockaddr_any(m_BindAddr.family());
  190. }
  191. // IMPORTANT!!! This function shall be called EXCLUSIVELY just before
  192. // calling ::sendmsg function. It uses a static buffer to supply data
  193. // for the call, and it's stated that only one thread is trying to
  194. // use a CChannel object in sending mode.
  195. bool setSourceAddress(msghdr& mh, const sockaddr_any& adr) const
  196. {
  197. // In contrast to an advice followed on the net, there's no case of putting
  198. // both IPv4 and IPv6 ancillary data, case we could have them. Only one
  199. // IP version is used and it's the version as found in @a adr, which should
  200. // be the version used for binding.
  201. if (adr.family() == AF_INET)
  202. {
  203. mh.msg_control = m_acCmsgSendBuffer;
  204. mh.msg_controllen = CMSG_SPACE(sizeof(in_pktinfo));
  205. cmsghdr* cmsg_send = CMSG_FIRSTHDR(&mh);
  206. // after initializing msghdr & control data to CMSG_SPACE(sizeof(struct in_pktinfo))
  207. cmsg_send->cmsg_level = IPPROTO_IP;
  208. cmsg_send->cmsg_type = IP_PKTINFO;
  209. cmsg_send->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
  210. in_pktinfo* pktinfo = (in_pktinfo*) CMSG_DATA(cmsg_send);
  211. pktinfo->ipi_ifindex = 0;
  212. pktinfo->ipi_spec_dst = adr.sin.sin_addr;
  213. return true;
  214. }
  215. if (adr.family() == AF_INET6)
  216. {
  217. mh.msg_control = m_acCmsgSendBuffer;
  218. mh.msg_controllen = CMSG_SPACE(sizeof(in6_pktinfo));
  219. cmsghdr* cmsg_send = CMSG_FIRSTHDR(&mh);
  220. cmsg_send->cmsg_level = IPPROTO_IPV6;
  221. cmsg_send->cmsg_type = IPV6_PKTINFO;
  222. cmsg_send->cmsg_len = CMSG_LEN(sizeof(in6_pktinfo));
  223. in6_pktinfo* pktinfo = (in6_pktinfo*) CMSG_DATA(cmsg_send);
  224. pktinfo->ipi6_ifindex = 0;
  225. pktinfo->ipi6_addr = adr.sin6.sin6_addr;
  226. return true;
  227. }
  228. return false;
  229. }
  230. #endif // SRT_ENABLE_PKTINFO
  231. };
  232. } // namespace srt
  233. #endif