OLD | NEW |
| (Empty) |
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 | |
21 #include "webrtc/base/asyncinvoker.h" | |
22 #include "webrtc/base/constructormagic.h" | |
23 #include "webrtc/base/copyonwritebuffer.h" | |
24 #include "webrtc/base/sigslot.h" | |
25 #include "webrtc/base/thread.h" | |
26 // For SendDataParams/ReceiveDataParams. | |
27 #include "webrtc/media/base/mediachannel.h" | |
28 #include "webrtc/media/sctp/sctptransportinternal.h" | |
29 #include "webrtc/p2p/base/transportchannel.h" | |
30 | |
31 // Defined by "usrsctplib/usrsctp.h" | |
32 struct sockaddr_conn; | |
33 struct sctp_assoc_change; | |
34 struct sctp_stream_reset_event; | |
35 // Defined by <sys/socket.h> | |
36 struct socket; | |
37 namespace cricket { | |
38 | |
39 // Holds data to be passed on to a channel. | |
40 struct SctpInboundPacket; | |
41 | |
42 // From channel calls, data flows like this: | |
43 // [network thread (although it can in princple be another thread)] | |
44 // 1. SctpTransport::SendData(data) | |
45 // 2. usrsctp_sendv(data) | |
46 // [network thread returns; sctp thread then calls the following] | |
47 // 3. OnSctpOutboundPacket(wrapped_data) | |
48 // [sctp thread returns having async invoked on the network thread] | |
49 // 4. SctpTransport::OnPacketFromSctpToNetwork(wrapped_data) | |
50 // 5. TransportChannel::SendPacket(wrapped_data) | |
51 // 6. ... across network ... a packet is sent back ... | |
52 // 7. SctpTransport::OnPacketReceived(wrapped_data) | |
53 // 8. usrsctp_conninput(wrapped_data) | |
54 // [network thread returns; sctp thread then calls the following] | |
55 // 9. OnSctpInboundData(data) | |
56 // [sctp thread returns having async invoked on the network thread] | |
57 // 10. SctpTransport::OnInboundPacketFromSctpToChannel(inboundpacket) | |
58 // 11. SctpTransport::OnDataFromSctpToChannel(data) | |
59 // 12. SctpTransport::SignalDataReceived(data) | |
60 // [from the same thread, methods registered/connected to | |
61 // SctpTransport are called with the recieved data] | |
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, | |
70 cricket::TransportChannel* channel); | |
71 ~SctpTransport() override; | |
72 | |
73 // SctpTransportInternal overrides (see sctptransportinternal.h for comments). | |
74 void SetTransportChannel(cricket::TransportChannel* channel) override; | |
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. | |
111 void OnWritableState(rtc::PacketTransportInterface* transport); | |
112 virtual void OnPacketRead(rtc::PacketTransportInterface* transport, | |
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. | |
143 TransportChannel* transport_channel_; | |
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( | |
182 TransportChannel* channel) override { | |
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_ | |
OLD | NEW |