henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_SCTP_SCTPDATAENGINE_H_ |
| 12 | #define WEBRTC_MEDIA_SCTP_SCTPDATAENGINE_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 14 | #include <errno.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | #include <string> |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 16 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 18 | namespace cricket { |
| 19 | // Some ERRNO values get re-#defined to WSA* equivalents in some talk/ |
| 20 | // headers. We save the original ones in an enum. |
| 21 | enum PreservedErrno { |
| 22 | SCTP_EINPROGRESS = EINPROGRESS, |
| 23 | SCTP_EWOULDBLOCK = EWOULDBLOCK |
| 24 | }; |
| 25 | } // namespace cricket |
| 26 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 27 | #include "webrtc/base/copyonwritebuffer.h" |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 28 | #include "webrtc/base/gtest_prod_util.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 29 | #include "webrtc/media/base/codec.h" |
| 30 | #include "webrtc/media/base/mediachannel.h" |
| 31 | #include "webrtc/media/base/mediaengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | |
| 33 | // Defined by "usrsctplib/usrsctp.h" |
| 34 | struct sockaddr_conn; |
| 35 | struct sctp_assoc_change; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 36 | struct sctp_stream_reset_event; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | // Defined by <sys/socket.h> |
| 38 | struct socket; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | namespace cricket { |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 40 | // The number of outgoing streams that we'll negotiate. Since stream IDs (SIDs) |
| 41 | // are 0-based, the highest usable SID is 1023. |
| 42 | // |
| 43 | // It's recommended to use the maximum of 65535 in: |
| 44 | // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.2 |
| 45 | // However, we use 1024 in order to save memory. usrsctp allocates 104 bytes |
| 46 | // for each pair of incoming/outgoing streams (on a 64-bit system), so 65535 |
| 47 | // streams would waste ~6MB. |
| 48 | // |
| 49 | // Note: "max" and "min" here are inclusive. |
| 50 | constexpr uint16_t kMaxSctpStreams = 1024; |
| 51 | constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1; |
| 52 | constexpr uint16_t kMinSctpSid = 0; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 53 | |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 54 | // This is the default SCTP port to use. It is passed along the wire and the |
| 55 | // connectee and connector must be using the same port. It is not related to the |
| 56 | // ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in |
| 57 | // usrsctp.h) |
| 58 | const int kSctpDefaultPort = 5000; |
| 59 | |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 60 | class SctpDataMediaChannel; |
| 61 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | // A DataEngine that interacts with usrsctp. |
| 63 | // |
| 64 | // From channel calls, data flows like this: |
| 65 | // [worker thread (although it can in princple be another thread)] |
| 66 | // 1. SctpDataMediaChannel::SendData(data) |
| 67 | // 2. usrsctp_sendv(data) |
| 68 | // [worker thread returns; sctp thread then calls the following] |
| 69 | // 3. OnSctpOutboundPacket(wrapped_data) |
| 70 | // [sctp thread returns having posted a message for the worker thread] |
| 71 | // 4. SctpDataMediaChannel::OnMessage(wrapped_data) |
| 72 | // 5. SctpDataMediaChannel::OnPacketFromSctpToNetwork(wrapped_data) |
| 73 | // 6. NetworkInterface::SendPacket(wrapped_data) |
| 74 | // 7. ... across network ... a packet is sent back ... |
| 75 | // 8. SctpDataMediaChannel::OnPacketReceived(wrapped_data) |
| 76 | // 9. usrsctp_conninput(wrapped_data) |
| 77 | // [worker thread returns; sctp thread then calls the following] |
| 78 | // 10. OnSctpInboundData(data) |
| 79 | // [sctp thread returns having posted a message fot the worker thread] |
| 80 | // 11. SctpDataMediaChannel::OnMessage(inboundpacket) |
| 81 | // 12. SctpDataMediaChannel::OnInboundPacketFromSctpToChannel(inboundpacket) |
| 82 | // 13. SctpDataMediaChannel::OnDataFromSctpToChannel(data) |
| 83 | // 14. SctpDataMediaChannel::SignalDataReceived(data) |
| 84 | // [from the same thread, methods registered/connected to |
| 85 | // SctpDataMediaChannel are called with the recieved data] |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 86 | class SctpDataEngine : public DataEngineInterface, public sigslot::has_slots<> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | public: |
| 88 | SctpDataEngine(); |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 89 | ~SctpDataEngine() override; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 91 | DataMediaChannel* CreateChannel(DataChannelType data_channel_type) override; |
| 92 | const std::vector<DataCodec>& data_codecs() override { return codecs_; } |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 93 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | private: |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 95 | const std::vector<DataCodec> codecs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | // TODO(ldixon): Make into a special type of TypedMessageData. |
| 99 | // Holds data to be passed on to a channel. |
| 100 | struct SctpInboundPacket; |
| 101 | |
| 102 | class SctpDataMediaChannel : public DataMediaChannel, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 103 | public rtc::MessageHandler { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | public: |
| 105 | // DataMessageType is used for the SCTP "Payload Protocol Identifier", as |
| 106 | // defined in http://tools.ietf.org/html/rfc4960#section-14.4 |
| 107 | // |
| 108 | // For the list of IANA approved values see: |
| 109 | // http://www.iana.org/assignments/sctp-parameters/sctp-parameters.xml |
| 110 | // The value is not used by SCTP itself. It indicates the protocol running |
| 111 | // on top of SCTP. |
| 112 | enum PayloadProtocolIdentifier { |
| 113 | PPID_NONE = 0, // No protocol is specified. |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 114 | // Matches the PPIDs in mozilla source and |
| 115 | // https://datatracker.ietf.org/doc/draft-ietf-rtcweb-data-protocol Sec. 9 |
| 116 | // They're not yet assigned by IANA. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | PPID_CONTROL = 50, |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 118 | PPID_BINARY_PARTIAL = 52, |
| 119 | PPID_BINARY_LAST = 53, |
| 120 | PPID_TEXT_PARTIAL = 54, |
| 121 | PPID_TEXT_LAST = 51 |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 124 | typedef std::set<uint32_t> StreamSet; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 125 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | // Given a thread which will be used to post messages (received data) to this |
| 127 | // SctpDataMediaChannel instance. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 128 | explicit SctpDataMediaChannel(rtc::Thread* thread); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | virtual ~SctpDataMediaChannel(); |
| 130 | |
| 131 | // When SetSend is set to true, connects. When set to false, disconnects. |
| 132 | // Calling: "SetSend(true); SetSend(false); SetSend(true);" will connect, |
| 133 | // disconnect, and reconnect. |
| 134 | virtual bool SetSend(bool send); |
| 135 | // Unless SetReceive(true) is called, received packets will be discarded. |
| 136 | virtual bool SetReceive(bool receive); |
| 137 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 138 | virtual bool SetSendParameters(const DataSendParameters& params); |
| 139 | virtual bool SetRecvParameters(const DataRecvParameters& params); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | virtual bool AddSendStream(const StreamParams& sp); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 141 | virtual bool RemoveSendStream(uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 142 | virtual bool AddRecvStream(const StreamParams& sp); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 143 | virtual bool RemoveRecvStream(uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 144 | |
| 145 | // Called when Sctp gets data. The data may be a notification or data for |
| 146 | // OnSctpInboundData. Called from the worker thread. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 147 | virtual void OnMessage(rtc::Message* msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | // Send data down this channel (will be wrapped as SCTP packets then given to |
| 149 | // sctp that will then post the network interface by OnMessage). |
| 150 | // Returns true iff successful data somewhere on the send-queue/network. |
| 151 | virtual bool SendData(const SendDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 152 | const rtc::CopyOnWriteBuffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | SendDataResult* result = NULL); |
| 154 | // A packet is received from the network interface. Posted to OnMessage. |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 155 | virtual void OnPacketReceived(rtc::CopyOnWriteBuffer* packet, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 156 | const rtc::PacketTime& packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | |
| 158 | // Exposed to allow Post call from c-callbacks. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 159 | rtc::Thread* worker_thread() const { return worker_thread_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | // Many of these things are unused by SCTP, but are needed to fulfill |
| 162 | // the MediaChannel interface. |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 163 | virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 164 | const rtc::PacketTime& packet_time) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | virtual void OnReadyToSend(bool ready) {} |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 166 | virtual void OnTransportOverheadChanged(int transport_overhead_per_packet) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 168 | void OnSendThresholdCallback(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | // Helper for debugging. |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 170 | void set_debug_name_for_testing(const char* debug_name) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | debug_name_ = debug_name; |
| 172 | } |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 173 | const struct socket* socket() const { return sock_; } |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 174 | |
| 175 | private: |
| 176 | FRIEND_TEST_ALL_PREFIXES(SctpDataMediaChannelTest, EngineSignalsRightChannel); |
| 177 | static int SendThresholdCallback(struct socket* sock, uint32_t sb_free); |
| 178 | static SctpDataMediaChannel* GetChannelFromSocket(struct socket* sock); |
| 179 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | private: |
| 181 | sockaddr_conn GetSctpSockAddr(int port); |
| 182 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 183 | bool SetSendCodecs(const std::vector<DataCodec>& codecs); |
| 184 | bool SetRecvCodecs(const std::vector<DataCodec>& codecs); |
| 185 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 186 | // Creates the socket and connects. Sets sending_ to true. |
| 187 | bool Connect(); |
| 188 | // Closes the socket. Sets sending_ to false. |
| 189 | void Disconnect(); |
| 190 | |
| 191 | // Returns false when openning the socket failed; when successfull sets |
| 192 | // sending_ to true |
| 193 | bool OpenSctpSocket(); |
| 194 | // Sets sending_ to false and sock_ to NULL. |
| 195 | void CloseSctpSocket(); |
| 196 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 197 | // Sends a SCTP_RESET_STREAM for all streams in closing_ssids_. |
| 198 | bool SendQueuedStreamResets(); |
| 199 | |
| 200 | // Adds a stream. |
| 201 | bool AddStream(const StreamParams &sp); |
| 202 | // Queues a stream for reset. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 203 | bool ResetStream(uint32_t ssrc); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 204 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 205 | // Called by OnMessage to send packet on the network. |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 206 | void OnPacketFromSctpToNetwork(rtc::CopyOnWriteBuffer* buffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | // Called by OnMessage to decide what to do with the packet. |
| 208 | void OnInboundPacketFromSctpToChannel(SctpInboundPacket* packet); |
| 209 | void OnDataFromSctpToChannel(const ReceiveDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 210 | const rtc::CopyOnWriteBuffer& buffer); |
| 211 | void OnNotificationFromSctp(const rtc::CopyOnWriteBuffer& buffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | void OnNotificationAssocChange(const sctp_assoc_change& change); |
sergeyu@chromium.org | 4b26e2e | 2014-01-15 23:15:54 +0000 | [diff] [blame] | 213 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 214 | void OnStreamResetEvent(const struct sctp_stream_reset_event* evt); |
| 215 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | // Responsible for marshalling incoming data to the channels listeners, and |
| 217 | // outgoing data to the network interface. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 218 | rtc::Thread* worker_thread_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | // The local and remote SCTP port to use. These are passed along the wire |
| 220 | // and the listener and connector must be using the same port. It is not |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 221 | // related to the ports at the IP level. If set to -1, we default to |
| 222 | // kSctpDefaultPort. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 223 | int local_port_; |
| 224 | int remote_port_; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 225 | struct socket* sock_; // The socket created by usrsctp_socket(...). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | |
| 227 | // sending_ is true iff there is a connected socket. |
| 228 | bool sending_; |
| 229 | // receiving_ controls whether inbound packets are thrown away. |
| 230 | bool receiving_; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 231 | |
| 232 | // When a data channel opens a stream, it goes into open_streams_. When we |
| 233 | // want to close it, the stream's ID goes into queued_reset_streams_. When |
| 234 | // we actually transmit a RE-CONFIG chunk with that stream ID, the ID goes |
| 235 | // into sent_reset_streams_. When we get a response RE-CONFIG chunk back |
| 236 | // acknowledging the reset, we remove the stream ID from |
| 237 | // sent_reset_streams_. We use sent_reset_streams_ to differentiate |
| 238 | // between acknowledgment RE-CONFIG and peer-initiated RE-CONFIGs. |
| 239 | StreamSet open_streams_; |
| 240 | StreamSet queued_reset_streams_; |
| 241 | StreamSet sent_reset_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 242 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 243 | // A static human-readable name for debugging messages. |
| 244 | const char* debug_name_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | } // namespace cricket |
| 248 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 249 | #endif // WEBRTC_MEDIA_SCTP_SCTPDATAENGINE_H_ |