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 Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
13 All rights reserved.
14 
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
18 
19 * Redistributions of source code must retain the above
20   copyright notice, this list of conditions and the
21   following disclaimer.
22 
23 * Redistributions in binary form must reproduce the
24   above copyright notice, this list of conditions
25   and the following disclaimer in the documentation
26   and/or other materials provided with the distribution.
27 
28 * Neither the name of the University of Illinois
29   nor the names of its contributors may be used to
30   endorse or promote products derived from this
31   software without specific prior written permission.
32 
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
45 
46 /*****************************************************************************
47 written by
48    Yunhong Gu, last updated 01/27/2011
49 modified by
50    Haivision Systems Inc.
51 *****************************************************************************/
52 
53 #ifndef INC_SRT_CHANNEL_H
54 #define INC_SRT_CHANNEL_H
55 
56 #include "platform_sys.h"
57 #include "udt.h"
58 #include "packet.h"
59 #include "socketconfig.h"
60 #include "netinet_any.h"
61 
62 namespace srt
63 {
64 
65 class CChannel
66 {
67     void createSocket(int family);
68 
69 public:
70     // XXX There's currently no way to access the socket ID set for
71     // whatever the channel is currently working for. Required to find
72     // some way to do this, possibly by having a "reverse pointer".
73     // Currently just "unimplemented".
CONID()74     std::string CONID() const { return ""; }
75 
76     CChannel();
77     ~CChannel();
78 
79     /// Open a UDP channel.
80     /// @param [in] addr The local address that UDP will use.
81 
82     void open(const sockaddr_any& addr);
83 
84     void open(int family);
85 
86     /// Open a UDP channel based on an existing UDP socket.
87     /// @param [in] udpsock UDP socket descriptor.
88 
89     void attach(UDPSOCKET udpsock, const sockaddr_any& adr);
90 
91     /// Disconnect and close the UDP entity.
92 
93     void close() const;
94 
95     /// Get the UDP sending buffer size.
96     /// @return Current UDP sending buffer size.
97 
98     int getSndBufSize();
99 
100     /// Get the UDP receiving buffer size.
101     /// @return Current UDP receiving buffer size.
102 
103     int getRcvBufSize();
104 
105     /// Query the socket address that the channel is using.
106     /// @param [out] addr pointer to store the returned socket address.
107 
108     void getSockAddr(sockaddr_any& addr) const;
109 
110     /// Query the peer side socket address that the channel is connect to.
111     /// @param [out] addr pointer to store the returned socket address.
112 
113     void getPeerAddr(sockaddr_any& addr) const;
114 
115     /// Send a packet to the given address.
116     /// @param [in] addr pointer to the destination address.
117     /// @param [in] packet reference to a CPacket entity.
118     /// @return Actual size of data sent.
119 
120     int sendto(const sockaddr_any& addr, srt::CPacket& packet) const;
121 
122     /// Receive a packet from the channel and record the source address.
123     /// @param [in] addr pointer to the source address.
124     /// @param [in] packet reference to a CPacket entity.
125     /// @return Actual size of data received.
126 
127     EReadStatus recvfrom(sockaddr_any& addr, srt::CPacket& packet) const;
128 
129     void setConfig(const CSrtMuxerConfig& config);
130 
131     /// Get the IP TTL.
132     /// @param [in] ttl IP Time To Live.
133     /// @return TTL.
134 
135     int getIpTTL() const;
136 
137     /// Get the IP Type of Service.
138     /// @return ToS.
139 
140     int getIpToS() const;
141 
142 #ifdef SRT_ENABLE_BINDTODEVICE
143     bool getBind(char* dst, size_t len);
144 #endif
145 
146     int ioctlQuery(int type) const;
147     int sockoptQuery(int level, int option) const;
148 
bindAddress()149     const sockaddr*     bindAddress() { return m_BindAddr.get(); }
bindAddressAny()150     const sockaddr_any& bindAddressAny() { return m_BindAddr; }
151 
152 private:
153     void setUDPSockOpt();
154 
155 private:
156     UDPSOCKET m_iSocket; // socket descriptor
157 
158     // Mutable because when querying original settings
159     // this comprises the cache for extracted values,
160     // although the object itself isn't considered modified.
161     mutable CSrtMuxerConfig m_mcfg; // Note: ReuseAddr is unused and ineffective.
162     sockaddr_any            m_BindAddr;
163 };
164 
165 } // namespace srt
166 
167 #endif
168