blob: 179505902cc80beb9b2fbc595e409994ed5cbbcd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle SCTP
3 * Copyright 2012 Google Inc, and Robin Seggelmann
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_MEDIA_SCTP_SCTPDATAENGINE_H_
29#define TALK_MEDIA_SCTP_SCTPDATAENGINE_H_
30
henrike@webrtc.org28654cb2013-07-22 21:07:49 +000031#include <errno.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <string>
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000033#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
henrike@webrtc.org28654cb2013-07-22 21:07:49 +000035namespace cricket {
36// Some ERRNO values get re-#defined to WSA* equivalents in some talk/
37// headers. We save the original ones in an enum.
38enum PreservedErrno {
39 SCTP_EINPROGRESS = EINPROGRESS,
40 SCTP_EWOULDBLOCK = EWOULDBLOCK
41};
42} // namespace cricket
43
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044#include "webrtc/base/buffer.h"
45#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/media/base/codec.h"
47#include "talk/media/base/mediachannel.h"
48#include "talk/media/base/mediaengine.h"
49
50// Defined by "usrsctplib/usrsctp.h"
51struct sockaddr_conn;
52struct sctp_assoc_change;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000053struct sctp_stream_reset_event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054// Defined by <sys/socket.h>
55struct socket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace cricket {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000057// The highest stream ID (Sid) that SCTP allows, and the number of streams we
58// tell SCTP we're going to use.
wu@webrtc.org97077a32013-10-25 21:18:33 +000059const uint32 kMaxSctpSid = 1023;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000060
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000061// This is the default SCTP port to use. It is passed along the wire and the
62// connectee and connector must be using the same port. It is not related to the
63// ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in
64// usrsctp.h)
65const int kSctpDefaultPort = 5000;
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067// A DataEngine that interacts with usrsctp.
68//
69// From channel calls, data flows like this:
70// [worker thread (although it can in princple be another thread)]
71// 1. SctpDataMediaChannel::SendData(data)
72// 2. usrsctp_sendv(data)
73// [worker thread returns; sctp thread then calls the following]
74// 3. OnSctpOutboundPacket(wrapped_data)
75// [sctp thread returns having posted a message for the worker thread]
76// 4. SctpDataMediaChannel::OnMessage(wrapped_data)
77// 5. SctpDataMediaChannel::OnPacketFromSctpToNetwork(wrapped_data)
78// 6. NetworkInterface::SendPacket(wrapped_data)
79// 7. ... across network ... a packet is sent back ...
80// 8. SctpDataMediaChannel::OnPacketReceived(wrapped_data)
81// 9. usrsctp_conninput(wrapped_data)
82// [worker thread returns; sctp thread then calls the following]
83// 10. OnSctpInboundData(data)
84// [sctp thread returns having posted a message fot the worker thread]
85// 11. SctpDataMediaChannel::OnMessage(inboundpacket)
86// 12. SctpDataMediaChannel::OnInboundPacketFromSctpToChannel(inboundpacket)
87// 13. SctpDataMediaChannel::OnDataFromSctpToChannel(data)
88// 14. SctpDataMediaChannel::SignalDataReceived(data)
89// [from the same thread, methods registered/connected to
90// SctpDataMediaChannel are called with the recieved data]
91class SctpDataEngine : public DataEngineInterface {
92 public:
93 SctpDataEngine();
94 virtual ~SctpDataEngine();
95
96 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type);
97
98 virtual const std::vector<DataCodec>& data_codecs() { return codecs_; }
99
100 private:
101 static int usrsctp_engines_count;
102 std::vector<DataCodec> codecs_;
103};
104
105// TODO(ldixon): Make into a special type of TypedMessageData.
106// Holds data to be passed on to a channel.
107struct SctpInboundPacket;
108
109class SctpDataMediaChannel : public DataMediaChannel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000110 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 public:
112 // DataMessageType is used for the SCTP "Payload Protocol Identifier", as
113 // defined in http://tools.ietf.org/html/rfc4960#section-14.4
114 //
115 // For the list of IANA approved values see:
116 // http://www.iana.org/assignments/sctp-parameters/sctp-parameters.xml
117 // The value is not used by SCTP itself. It indicates the protocol running
118 // on top of SCTP.
119 enum PayloadProtocolIdentifier {
120 PPID_NONE = 0, // No protocol is specified.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000121 // Matches the PPIDs in mozilla source and
122 // https://datatracker.ietf.org/doc/draft-ietf-rtcweb-data-protocol Sec. 9
123 // They're not yet assigned by IANA.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 PPID_CONTROL = 50,
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000125 PPID_BINARY_PARTIAL = 52,
126 PPID_BINARY_LAST = 53,
127 PPID_TEXT_PARTIAL = 54,
128 PPID_TEXT_LAST = 51
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 };
130
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000131 typedef std::set<uint32> StreamSet;
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 // Given a thread which will be used to post messages (received data) to this
134 // SctpDataMediaChannel instance.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000135 explicit SctpDataMediaChannel(rtc::Thread* thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 virtual ~SctpDataMediaChannel();
137
138 // When SetSend is set to true, connects. When set to false, disconnects.
139 // Calling: "SetSend(true); SetSend(false); SetSend(true);" will connect,
140 // disconnect, and reconnect.
141 virtual bool SetSend(bool send);
142 // Unless SetReceive(true) is called, received packets will be discarded.
143 virtual bool SetReceive(bool receive);
144
145 virtual bool AddSendStream(const StreamParams& sp);
146 virtual bool RemoveSendStream(uint32 ssrc);
147 virtual bool AddRecvStream(const StreamParams& sp);
148 virtual bool RemoveRecvStream(uint32 ssrc);
149
150 // Called when Sctp gets data. The data may be a notification or data for
151 // OnSctpInboundData. Called from the worker thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000152 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 // Send data down this channel (will be wrapped as SCTP packets then given to
154 // sctp that will then post the network interface by OnMessage).
155 // Returns true iff successful data somewhere on the send-queue/network.
156 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000157 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 SendDataResult* result = NULL);
159 // A packet is received from the network interface. Posted to OnMessage.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000160 virtual void OnPacketReceived(rtc::Buffer* packet,
161 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
163 // Exposed to allow Post call from c-callbacks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
166 // TODO(ldixon): add a DataOptions class to mediachannel.h
167 virtual bool SetOptions(int options) { return false; }
168 virtual int GetOptions() const { return 0; }
169
170 // Many of these things are unused by SCTP, but are needed to fulfill
171 // the MediaChannel interface.
172 // TODO(pthatcher): Cleanup MediaChannel interface, or at least
173 // don't try calling these and return false. Right now, things
174 // don't work if we return false.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000175 virtual bool SetStartSendBandwidth(int bps) { return true; }
176 virtual bool SetMaxSendBandwidth(int bps) { return true; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 virtual bool SetRecvRtpHeaderExtensions(
178 const std::vector<RtpHeaderExtension>& extensions) { return true; }
179 virtual bool SetSendRtpHeaderExtensions(
180 const std::vector<RtpHeaderExtension>& extensions) { return true; }
wu@webrtc.org78187522013-10-07 23:32:02 +0000181 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs);
182 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 virtual void OnRtcpReceived(rtc::Buffer* packet,
184 const rtc::PacketTime& packet_time) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 virtual void OnReadyToSend(bool ready) {}
186
187 // Helper for debugging.
188 void set_debug_name(const std::string& debug_name) {
189 debug_name_ = debug_name;
190 }
191 const std::string& debug_name() const { return debug_name_; }
192
193 private:
194 sockaddr_conn GetSctpSockAddr(int port);
195
196 // Creates the socket and connects. Sets sending_ to true.
197 bool Connect();
198 // Closes the socket. Sets sending_ to false.
199 void Disconnect();
200
201 // Returns false when openning the socket failed; when successfull sets
202 // sending_ to true
203 bool OpenSctpSocket();
204 // Sets sending_ to false and sock_ to NULL.
205 void CloseSctpSocket();
206
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000207 // Sends a SCTP_RESET_STREAM for all streams in closing_ssids_.
208 bool SendQueuedStreamResets();
209
210 // Adds a stream.
211 bool AddStream(const StreamParams &sp);
212 // Queues a stream for reset.
213 bool ResetStream(uint32 ssrc);
214
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 // Called by OnMessage to send packet on the network.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000216 void OnPacketFromSctpToNetwork(rtc::Buffer* buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 // Called by OnMessage to decide what to do with the packet.
218 void OnInboundPacketFromSctpToChannel(SctpInboundPacket* packet);
219 void OnDataFromSctpToChannel(const ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 rtc::Buffer* buffer);
221 void OnNotificationFromSctp(rtc::Buffer* buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 void OnNotificationAssocChange(const sctp_assoc_change& change);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000223
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000224 void OnStreamResetEvent(const struct sctp_stream_reset_event* evt);
225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 // Responsible for marshalling incoming data to the channels listeners, and
227 // outgoing data to the network interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000228 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 // The local and remote SCTP port to use. These are passed along the wire
230 // and the listener and connector must be using the same port. It is not
wu@webrtc.org78187522013-10-07 23:32:02 +0000231 // related to the ports at the IP level. If set to -1, we default to
232 // kSctpDefaultPort.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 int local_port_;
234 int remote_port_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000235 struct socket* sock_; // The socket created by usrsctp_socket(...).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
237 // sending_ is true iff there is a connected socket.
238 bool sending_;
239 // receiving_ controls whether inbound packets are thrown away.
240 bool receiving_;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000241
242 // When a data channel opens a stream, it goes into open_streams_. When we
243 // want to close it, the stream's ID goes into queued_reset_streams_. When
244 // we actually transmit a RE-CONFIG chunk with that stream ID, the ID goes
245 // into sent_reset_streams_. When we get a response RE-CONFIG chunk back
246 // acknowledging the reset, we remove the stream ID from
247 // sent_reset_streams_. We use sent_reset_streams_ to differentiate
248 // between acknowledgment RE-CONFIG and peer-initiated RE-CONFIGs.
249 StreamSet open_streams_;
250 StreamSet queued_reset_streams_;
251 StreamSet sent_reset_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
253 // A human-readable name for debugging messages.
254 std::string debug_name_;
255};
256
257} // namespace cricket
258
259#endif // TALK_MEDIA_SCTP_SCTPDATAENGINE_H_