Debian Codegraph @

srtcore/channel.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 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 #ifndef INC_SRT_CHANNEL_H
53 #define INC_SRT_CHANNEL_H
54
55 #include "platform_sys.h"
56 #include "udt.h"
57 #include "packet.h"
58 #include "socketconfig.h"
59 #include "netinet_any.h"
60
61 namespace srt
62 {
63
64 class CChannel
65 {
66 void createSocket(int family);
67
68 public:
69 // XXX There's currently no way to access the socket ID set for
70 // whatever the channel is currently working for. Required to find
71 // some way to do this, possibly by having a "reverse pointer".
72 // Currently just "unimplemented".
73 std::string CONID() const { return ""; }
74
75 CChannel();
76 ~CChannel();
77
78 /// Open a UDP channel.
79 /// @param [in] addr The local address that UDP will use.
80
81 void open(const sockaddr_any& addr);
82
83 void open(int family);
84
85 /// Open a UDP channel based on an existing UDP socket.
86 /// @param [in] udpsock UDP socket descriptor.
87
88 void attach(UDPSOCKET udpsock, const sockaddr_any& adr);
89
90 /// Disconnect and close the UDP entity.
91
92 void close() const;
93
94 /// Get the UDP sending buffer size.
95 /// @return Current UDP sending buffer size.
96
97 int getSndBufSize();
98
99 /// Get the UDP receiving buffer size.
100 /// @return Current UDP receiving buffer size.
101
102 int getRcvBufSize();
103
104 /// Query the socket address that the channel is using.
105 /// @param [out] addr pointer to store the returned socket address.
106
107 void getSockAddr(sockaddr_any& addr) const;
108
109 /// Query the peer side socket address that the channel is connect to.
110 /// @param [out] addr pointer to store the returned socket address.
111
112 void getPeerAddr(sockaddr_any& addr) const;
113
114 /// Send a packet to the given address.
115 /// @param [in] addr pointer to the destination address.
116 /// @param [in] packet reference to a CPacket entity.
117 /// @param [in] src source address to sent on an outgoing packet (if not ANY)
118 /// @return Actual size of data sent.
119
120 int sendto(const sockaddr_any& addr, srt::CPacket& packet, const sockaddr_any& src) 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 void getSocketOption(int level, int sockoptname, char* pw_dataptr, socklen_t& w_len, int& w_status);
132
133 template<class Type>
134 Type sockopt(int level, int sockoptname, Type deflt)
135 {
136 Type retval;
137 socklen_t socklen = sizeof retval;
138 int status;
139 getSocketOption(level, sockoptname, ((char*)&retval), (socklen), (status));
140 if (status == -1)
141 return deflt;
142
143 return retval;
144 }
145
146 /// Get the IP TTL.
147 /// @param [in] ttl IP Time To Live.
148 /// @return TTL.
149
150 int getIpTTL() const;
151
152 /// Get the IP Type of Service.
153 /// @return ToS.
154
155 int getIpToS() const;
156
157 #ifdef SRT_ENABLE_BINDTODEVICE
158 bool getBind(char* dst, size_t len);
159 #endif
160
161 int ioctlQuery(int type) const;
162 int sockoptQuery(int level, int option) const;
163
164 const sockaddr* bindAddress() { return m_BindAddr.get(); }
165 const sockaddr_any& bindAddressAny() { return m_BindAddr; }
166
167 private:
168 void setUDPSockOpt();
169
170 private:
171 UDPSOCKET m_iSocket; // socket descriptor
172
173 // Mutable because when querying original settings
174 // this comprises the cache for extracted values,
175 // although the object itself isn't considered modified.
176 mutable CSrtMuxerConfig m_mcfg; // Note: ReuseAddr is unused and ineffective.
177 sockaddr_any m_BindAddr;
178
179 // This feature is not enabled on Windows, for now.
180 // This is also turned off in case of MinGW
181 #ifdef SRT_ENABLE_PKTINFO
182 bool m_bBindMasked; // True if m_BindAddr is INADDR_ANY. Need for quick check.
183
184 // Calculating the required space is extremely tricky, and whereas on most
185 // platforms it's possible to define it this way:
186 //
187 // size_t s = max( CMSG_SPACE(sizeof(in_pktinfo)), CMSG_SPACE(sizeof(in6_pktinfo)) )
188 //
189 // ...on some platforms however CMSG_SPACE macro can't be resolved as constexpr.
190 //
191 // This structure is exclusively used to determine the required size for
192 // CMSG buffer so that it can be allocated in a solid block with CChannel.
193 // NOT TO BE USED to access any data inside the CMSG message.
194 struct CMSGNodeIPv4
195 {
196 in_pktinfo in4;
197 size_t extrafill;
198 cmsghdr hdr;
199 };
200
201 struct CMSGNodeIPv6
202 {
203 in6_pktinfo in6;
204 size_t extrafill;
205 cmsghdr hdr;
206 };
207
208 sockaddr_any getTargetAddress(const msghdr& msg) const
209 {
210 // Loop through IP header messages
211 cmsghdr* cmsg;
212 for (cmsg = CMSG_FIRSTHDR(&msg);
213 cmsg != NULL;
214 cmsg = CMSG_NXTHDR(((msghdr*)&msg), cmsg))
215 {
216 // This should be safe - this packet contains always either
217 // IPv4 headers or IPv6 headers.
218 if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO)
219 {
220 in_pktinfo dest_ip;
221 memcpy(&dest_ip, CMSG_DATA(cmsg), sizeof(struct in_pktinfo));
222 return sockaddr_any(dest_ip.ipi_addr, 0);
223 }
224
225 if (cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO)
226 {
227 in6_pktinfo dest_ip;
228 memcpy(&dest_ip, CMSG_DATA(cmsg), sizeof(struct in6_pktinfo));
229 return sockaddr_any(dest_ip.ipi6_addr, 0);
230 }
231 }
232
233 // Fallback for an error
234 return sockaddr_any(m_BindAddr.family());
235 }
236
237 // IMPORTANT!!! This function shall be called EXCLUSIVELY just before
238 // calling ::sendmsg function. It uses a static buffer to supply data
239 // for the call, and it's stated that only one thread is trying to
240 // use a CChannel object in sending mode.
241 bool setSourceAddress(msghdr& mh, char *buf, const sockaddr_any& adr) const
242 {
243 // In contrast to an advice followed on the net, there's no case of putting
244 // both IPv4 and IPv6 ancillary data, case we could have them. Only one
245 // IP version is used and it's the version as found in @a adr, which should
246 // be the version used for binding.
247
248 if (adr.family() == AF_INET)
249 {
250 mh.msg_control = (void *) buf;
251 mh.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
252
253 cmsghdr* cmsg_send = CMSG_FIRSTHDR(&mh);
254 cmsg_send->cmsg_level = IPPROTO_IP;
255 cmsg_send->cmsg_type = IP_PKTINFO;
256 cmsg_send->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
257
258 in_pktinfo pktinfo;
259 pktinfo.ipi_ifindex = 0;
260 pktinfo.ipi_spec_dst = adr.sin.sin_addr;
261 memcpy(CMSG_DATA(cmsg_send), &pktinfo, sizeof(in_pktinfo));
262
263 return true;
264 }
265
266 if (adr.family() == AF_INET6)
267 {
268 mh.msg_control = buf;
269 mh.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
270
271 cmsghdr* cmsg_send = CMSG_FIRSTHDR(&mh);
272 cmsg_send->cmsg_level = IPPROTO_IPV6;
273 cmsg_send->cmsg_type = IPV6_PKTINFO;
274 cmsg_send->cmsg_len = CMSG_LEN(sizeof(in6_pktinfo));
275
276 in6_pktinfo* pktinfo = (in6_pktinfo*) CMSG_DATA(cmsg_send);
277 pktinfo->ipi6_ifindex = 0;
278 pktinfo->ipi6_addr = adr.sin6.sin6_addr;
279
280 return true;
281 }
282
283 return false;
284 }
285
286 #endif //SRT_ENABLE_PKTINFO
287
288 };
289
290 } // namespace srt
291
292 #endif