deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_MEDIA_SCTP_SCTPTRANSPORT_H_ |
| 12 | #define WEBRTC_MEDIA_SCTP_SCTPTRANSPORT_H_ |
| 13 | |
| 14 | #include <errno.h> |
| 15 | |
| 16 | #include <memory> // for unique_ptr. |
| 17 | #include <set> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/asyncinvoker.h" |
| 22 | #include "webrtc/rtc_base/constructormagic.h" |
| 23 | #include "webrtc/rtc_base/copyonwritebuffer.h" |
| 24 | #include "webrtc/rtc_base/sigslot.h" |
| 25 | #include "webrtc/rtc_base/thread.h" |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 26 | // For SendDataParams/ReceiveDataParams. |
| 27 | #include "webrtc/media/base/mediachannel.h" |
| 28 | #include "webrtc/media/sctp/sctptransportinternal.h" |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 29 | |
| 30 | // Defined by "usrsctplib/usrsctp.h" |
| 31 | struct sockaddr_conn; |
| 32 | struct sctp_assoc_change; |
| 33 | struct sctp_stream_reset_event; |
| 34 | // Defined by <sys/socket.h> |
| 35 | struct socket; |
| 36 | namespace cricket { |
| 37 | |
| 38 | // Holds data to be passed on to a channel. |
| 39 | struct SctpInboundPacket; |
| 40 | |
| 41 | // From channel calls, data flows like this: |
| 42 | // [network thread (although it can in princple be another thread)] |
| 43 | // 1. SctpTransport::SendData(data) |
| 44 | // 2. usrsctp_sendv(data) |
| 45 | // [network thread returns; sctp thread then calls the following] |
| 46 | // 3. OnSctpOutboundPacket(wrapped_data) |
| 47 | // [sctp thread returns having async invoked on the network thread] |
| 48 | // 4. SctpTransport::OnPacketFromSctpToNetwork(wrapped_data) |
| 49 | // 5. TransportChannel::SendPacket(wrapped_data) |
| 50 | // 6. ... across network ... a packet is sent back ... |
| 51 | // 7. SctpTransport::OnPacketReceived(wrapped_data) |
| 52 | // 8. usrsctp_conninput(wrapped_data) |
| 53 | // [network thread returns; sctp thread then calls the following] |
| 54 | // 9. OnSctpInboundData(data) |
| 55 | // [sctp thread returns having async invoked on the network thread] |
| 56 | // 10. SctpTransport::OnInboundPacketFromSctpToChannel(inboundpacket) |
| 57 | // 11. SctpTransport::OnDataFromSctpToChannel(data) |
| 58 | // 12. SctpTransport::SignalDataReceived(data) |
| 59 | // [from the same thread, methods registered/connected to |
| 60 | // SctpTransport are called with the recieved data] |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 61 | // TODO(zhihuang): Rename "channel" to "transport" on network-level. |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 62 | class SctpTransport : public SctpTransportInternal, |
| 63 | public sigslot::has_slots<> { |
| 64 | public: |
| 65 | // |network_thread| is where packets will be processed and callbacks from |
| 66 | // this transport will be posted, and is the only thread on which public |
| 67 | // methods can be called. |
| 68 | // |channel| is required (must not be null). |
| 69 | SctpTransport(rtc::Thread* network_thread, |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 70 | rtc::PacketTransportInternal* channel); |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 71 | ~SctpTransport() override; |
| 72 | |
| 73 | // SctpTransportInternal overrides (see sctptransportinternal.h for comments). |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 74 | void SetTransportChannel(rtc::PacketTransportInternal* channel) override; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 75 | bool Start(int local_port, int remote_port) override; |
| 76 | bool OpenStream(int sid) override; |
| 77 | bool ResetStream(int sid) override; |
| 78 | bool SendData(const SendDataParams& params, |
| 79 | const rtc::CopyOnWriteBuffer& payload, |
| 80 | SendDataResult* result = nullptr) override; |
| 81 | bool ReadyToSendData() override; |
| 82 | void set_debug_name_for_testing(const char* debug_name) override { |
| 83 | debug_name_ = debug_name; |
| 84 | } |
| 85 | |
| 86 | // Exposed to allow Post call from c-callbacks. |
| 87 | // TODO(deadbeef): Remove this or at least make it return a const pointer. |
| 88 | rtc::Thread* network_thread() const { return network_thread_; } |
| 89 | |
| 90 | private: |
| 91 | void ConnectTransportChannelSignals(); |
| 92 | void DisconnectTransportChannelSignals(); |
| 93 | |
| 94 | // Creates the socket and connects. |
| 95 | bool Connect(); |
| 96 | |
| 97 | // Returns false when opening the socket failed. |
| 98 | bool OpenSctpSocket(); |
| 99 | // Helpet method to set socket options. |
| 100 | bool ConfigureSctpSocket(); |
| 101 | // Sets |sock_ |to nullptr. |
| 102 | void CloseSctpSocket(); |
| 103 | |
| 104 | // Sends a SCTP_RESET_STREAM for all streams in closing_ssids_. |
| 105 | bool SendQueuedStreamResets(); |
| 106 | |
| 107 | // Sets the "ready to send" flag and fires signal if needed. |
| 108 | void SetReadyToSendData(); |
| 109 | |
| 110 | // Callbacks from DTLS channel. |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 111 | void OnWritableState(rtc::PacketTransportInternal* transport); |
| 112 | virtual void OnPacketRead(rtc::PacketTransportInternal* transport, |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 113 | const char* data, |
| 114 | size_t len, |
| 115 | const rtc::PacketTime& packet_time, |
| 116 | int flags); |
| 117 | |
| 118 | // Methods related to usrsctp callbacks. |
| 119 | void OnSendThresholdCallback(); |
| 120 | sockaddr_conn GetSctpSockAddr(int port); |
| 121 | |
| 122 | // Called using |invoker_| to send packet on the network. |
| 123 | void OnPacketFromSctpToNetwork(const rtc::CopyOnWriteBuffer& buffer); |
| 124 | // Called using |invoker_| to decide what to do with the packet. |
| 125 | // The |flags| parameter is used by SCTP to distinguish notification packets |
| 126 | // from other types of packets. |
| 127 | void OnInboundPacketFromSctpToChannel(const rtc::CopyOnWriteBuffer& buffer, |
| 128 | ReceiveDataParams params, |
| 129 | int flags); |
| 130 | void OnDataFromSctpToChannel(const ReceiveDataParams& params, |
| 131 | const rtc::CopyOnWriteBuffer& buffer); |
| 132 | void OnNotificationFromSctp(const rtc::CopyOnWriteBuffer& buffer); |
| 133 | void OnNotificationAssocChange(const sctp_assoc_change& change); |
| 134 | |
| 135 | void OnStreamResetEvent(const struct sctp_stream_reset_event* evt); |
| 136 | |
| 137 | // Responsible for marshalling incoming data to the channels listeners, and |
| 138 | // outgoing data to the network interface. |
| 139 | rtc::Thread* network_thread_; |
| 140 | // Helps pass inbound/outbound packets asynchronously to the network thread. |
| 141 | rtc::AsyncInvoker invoker_; |
| 142 | // Underlying DTLS channel. |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 143 | rtc::PacketTransportInternal* transport_channel_; |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 144 | bool was_ever_writable_ = false; |
| 145 | int local_port_ = kSctpDefaultPort; |
| 146 | int remote_port_ = kSctpDefaultPort; |
| 147 | struct socket* sock_ = nullptr; // The socket created by usrsctp_socket(...). |
| 148 | |
| 149 | // Has Start been called? Don't create SCTP socket until it has. |
| 150 | bool started_ = false; |
| 151 | // Are we ready to queue data (SCTP socket created, and not blocked due to |
| 152 | // congestion control)? Different than |transport_channel_|'s "ready to |
| 153 | // send". |
| 154 | bool ready_to_send_data_ = false; |
| 155 | |
| 156 | typedef std::set<uint32_t> StreamSet; |
| 157 | // When a data channel opens a stream, it goes into open_streams_. When we |
| 158 | // want to close it, the stream's ID goes into queued_reset_streams_. When |
| 159 | // we actually transmit a RE-CONFIG chunk with that stream ID, the ID goes |
| 160 | // into sent_reset_streams_. When we get a response RE-CONFIG chunk back |
| 161 | // acknowledging the reset, we remove the stream ID from |
| 162 | // sent_reset_streams_. We use sent_reset_streams_ to differentiate |
| 163 | // between acknowledgment RE-CONFIG and peer-initiated RE-CONFIGs. |
| 164 | StreamSet open_streams_; |
| 165 | StreamSet queued_reset_streams_; |
| 166 | StreamSet sent_reset_streams_; |
| 167 | |
| 168 | // A static human-readable name for debugging messages. |
| 169 | const char* debug_name_ = "SctpTransport"; |
| 170 | // Hides usrsctp interactions from this header file. |
| 171 | class UsrSctpWrapper; |
| 172 | |
| 173 | RTC_DISALLOW_COPY_AND_ASSIGN(SctpTransport); |
| 174 | }; |
| 175 | |
| 176 | class SctpTransportFactory : public SctpTransportInternalFactory { |
| 177 | public: |
| 178 | explicit SctpTransportFactory(rtc::Thread* network_thread) |
| 179 | : network_thread_(network_thread) {} |
| 180 | |
| 181 | std::unique_ptr<SctpTransportInternal> CreateSctpTransport( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 182 | rtc::PacketTransportInternal* channel) override { |
deadbeef | 953c2ce | 2017-01-09 14:53:41 -0800 | [diff] [blame] | 183 | return std::unique_ptr<SctpTransportInternal>( |
| 184 | new SctpTransport(network_thread_, channel)); |
| 185 | } |
| 186 | |
| 187 | private: |
| 188 | rtc::Thread* network_thread_; |
| 189 | }; |
| 190 | |
| 191 | } // namespace cricket |
| 192 | |
| 193 | #endif // WEBRTC_MEDIA_SCTP_SCTPTRANSPORT_H_ |