blob: aae3d93d072e95194941ed0c362e06f5416d4870 [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#include "talk/media/sctp/sctpdataengine.h"
29
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030#include <stdarg.h>
31#include <stdio.h>
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000032#include <sstream>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#include <vector>
34
35#include "talk/base/buffer.h"
36#include "talk/base/helpers.h"
37#include "talk/base/logging.h"
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +000038#include "talk/base/safe_conversions.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/media/base/codec.h"
40#include "talk/media/base/constants.h"
41#include "talk/media/base/streamparams.h"
42#include "usrsctplib/usrsctp.h"
43
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000044namespace {
45typedef cricket::SctpDataMediaChannel::StreamSet StreamSet;
46// Returns a comma-separated, human-readable list of the stream IDs in 's'
47std::string ListStreams(const StreamSet& s) {
48 std::stringstream result;
49 bool first = true;
wu@webrtc.orge00265e2014-01-07 19:32:40 +000050 for (StreamSet::const_iterator it = s.begin(); it != s.end(); ++it) {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000051 if (!first) {
52 result << ", " << *it;
53 } else {
54 result << *it;
55 first = false;
56 }
57 }
58 return result.str();
59}
60
61// Returns a pipe-separated, human-readable list of the SCTP_STREAM_RESET
62// flags in 'flags'
63std::string ListFlags(int flags) {
64 std::stringstream result;
65 bool first = true;
66 // Skip past the first 12 chars (strlen("SCTP_STREAM_"))
67#define MAKEFLAG(X) { X, #X + 12}
68 struct flaginfo_t {
69 int value;
70 const char* name;
71 } flaginfo[] = {
72 MAKEFLAG(SCTP_STREAM_RESET_INCOMING_SSN),
73 MAKEFLAG(SCTP_STREAM_RESET_OUTGOING_SSN),
74 MAKEFLAG(SCTP_STREAM_RESET_DENIED),
75 MAKEFLAG(SCTP_STREAM_RESET_FAILED),
76 MAKEFLAG(SCTP_STREAM_CHANGE_DENIED)
77 };
78#undef MAKEFLAG
79 for (int i = 0; i < ARRAY_SIZE(flaginfo); ++i) {
80 if (flags & flaginfo[i].value) {
81 if (!first) result << " | ";
82 result << flaginfo[i].name;
83 first = false;
84 }
85 }
86 return result.str();
87}
88
89// Returns a comma-separated, human-readable list of the integers in 'array'.
90// All 'num_elems' of them.
91std::string ListArray(const uint16* array, int num_elems) {
92 std::stringstream result;
93 for (int i = 0; i < num_elems; ++i) {
94 if (i) {
95 result << ", " << array[i];
96 } else {
97 result << array[i];
98 }
99 }
100 return result.str();
101}
102} // namespace
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104namespace cricket {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000105typedef talk_base::ScopedMessageData<SctpInboundPacket> InboundPacketMessage;
106typedef talk_base::ScopedMessageData<talk_base::Buffer> OutboundPacketMessage;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108// This is the SCTP port to use. It is passed along the wire and the listener
109// and connector must be using the same port. It is not related to the ports at
110// the IP level. (Corresponds to: sockaddr_conn.sconn_port in usrsctp.h)
111//
112// TODO(ldixon): Allow port to be set from higher level code.
113static const int kSctpDefaultPort = 5001;
114// TODO(ldixon): Find where this is defined, and also check is Sctp really
115// respects this.
116static const size_t kSctpMtu = 1280;
117
118enum {
119 MSG_SCTPINBOUNDPACKET = 1, // MessageData is SctpInboundPacket
120 MSG_SCTPOUTBOUNDPACKET = 2, // MessageData is talk_base:Buffer
121};
122
123struct SctpInboundPacket {
124 talk_base::Buffer buffer;
125 ReceiveDataParams params;
126 // The |flags| parameter is used by SCTP to distinguish notification packets
127 // from other types of packets.
128 int flags;
129};
130
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000131// Helper for logging SCTP messages.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132static void debug_sctp_printf(const char *format, ...) {
133 char s[255];
134 va_list ap;
135 va_start(ap, format);
136 vsnprintf(s, sizeof(s), format, ap);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000137 LOG(LS_INFO) << "SCTP: " << s;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 va_end(ap);
139}
140
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000141// Get the PPID to use for the terminating fragment of this type.
142static SctpDataMediaChannel::PayloadProtocolIdentifier GetPpid(
143 cricket::DataMessageType type) {
144 switch (type) {
145 default:
146 case cricket::DMT_NONE:
147 return SctpDataMediaChannel::PPID_NONE;
148 case cricket::DMT_CONTROL:
149 return SctpDataMediaChannel::PPID_CONTROL;
150 case cricket::DMT_BINARY:
151 return SctpDataMediaChannel::PPID_BINARY_LAST;
152 case cricket::DMT_TEXT:
153 return SctpDataMediaChannel::PPID_TEXT_LAST;
154 };
155}
156
157static bool GetDataMediaType(
158 SctpDataMediaChannel::PayloadProtocolIdentifier ppid,
159 cricket::DataMessageType *dest) {
160 ASSERT(dest != NULL);
161 switch (ppid) {
162 case SctpDataMediaChannel::PPID_BINARY_PARTIAL:
163 case SctpDataMediaChannel::PPID_BINARY_LAST:
164 *dest = cricket::DMT_BINARY;
165 return true;
166
167 case SctpDataMediaChannel::PPID_TEXT_PARTIAL:
168 case SctpDataMediaChannel::PPID_TEXT_LAST:
169 *dest = cricket::DMT_TEXT;
170 return true;
171
172 case SctpDataMediaChannel::PPID_CONTROL:
173 *dest = cricket::DMT_CONTROL;
174 return true;
175
176 case SctpDataMediaChannel::PPID_NONE:
177 *dest = cricket::DMT_NONE;
178 return true;
179
180 default:
181 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183}
184
185// This is the callback usrsctp uses when there's data to send on the network
186// that has been wrapped appropriatly for the SCTP protocol.
187static int OnSctpOutboundPacket(void* addr, void* data, size_t length,
188 uint8_t tos, uint8_t set_df) {
189 SctpDataMediaChannel* channel = static_cast<SctpDataMediaChannel*>(addr);
190 LOG(LS_VERBOSE) << "global OnSctpOutboundPacket():"
191 << "addr: " << addr << "; length: " << length
192 << "; tos: " << std::hex << static_cast<int>(tos)
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000193 << "; set_df: " << std::hex << static_cast<int>(set_df);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 // Note: We have to copy the data; the caller will delete it.
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000195 OutboundPacketMessage* msg =
196 new OutboundPacketMessage(new talk_base::Buffer(data, length));
197 channel->worker_thread()->Post(channel, MSG_SCTPOUTBOUNDPACKET, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return 0;
199}
200
201// This is the callback called from usrsctp when data has been received, after
202// a packet has been interpreted and parsed by usrsctp and found to contain
203// payload data. It is called by a usrsctp thread. It is assumed this function
204// will free the memory used by 'data'.
205static int OnSctpInboundPacket(struct socket* sock, union sctp_sockstore addr,
206 void* data, size_t length,
207 struct sctp_rcvinfo rcv, int flags,
208 void* ulp_info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 SctpDataMediaChannel* channel = static_cast<SctpDataMediaChannel*>(ulp_info);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 // Post data to the channel's receiver thread (copying it).
211 // TODO(ldixon): Unclear if copy is needed as this method is responsible for
212 // memory cleanup. But this does simplify code.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000213 const SctpDataMediaChannel::PayloadProtocolIdentifier ppid =
214 static_cast<SctpDataMediaChannel::PayloadProtocolIdentifier>(
215 talk_base::HostToNetwork32(rcv.rcv_ppid));
216 cricket::DataMessageType type = cricket::DMT_NONE;
217 if (!GetDataMediaType(ppid, &type) && !(flags & MSG_NOTIFICATION)) {
218 // It's neither a notification nor a recognized data packet. Drop it.
219 LOG(LS_ERROR) << "Received an unknown PPID " << ppid
220 << " on an SCTP packet. Dropping.";
221 } else {
222 SctpInboundPacket* packet = new SctpInboundPacket;
223 packet->buffer.SetData(data, length);
224 packet->params.ssrc = rcv.rcv_sid;
225 packet->params.seq_num = rcv.rcv_ssn;
226 packet->params.timestamp = rcv.rcv_tsn;
227 packet->params.type = type;
228 packet->flags = flags;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000229 // The ownership of |packet| transfers to |msg|.
230 InboundPacketMessage* msg = new InboundPacketMessage(packet);
231 channel->worker_thread()->Post(channel, MSG_SCTPINBOUNDPACKET, msg);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000232 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 free(data);
234 return 1;
235}
236
237// Set the initial value of the static SCTP Data Engines reference count.
238int SctpDataEngine::usrsctp_engines_count = 0;
239
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000240void SctpDataEngine::AddRefEngine() {
241 LOG(LS_VERBOSE) << "usrsctp_engines_count:" << usrsctp_engines_count;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 if (usrsctp_engines_count == 0) {
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000243 LOG(LS_INFO) << "SctpDataEngine: Initializing usrsctp";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 // First argument is udp_encapsulation_port, which is not releveant for our
245 // AF_CONN use of sctp.
246 usrsctp_init(0, cricket::OnSctpOutboundPacket, debug_sctp_printf);
247
248 // To turn on/off detailed SCTP debugging. You will also need to have the
249 // SCTP_DEBUG cpp defines flag.
250 // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
251
252 // TODO(ldixon): Consider turning this on/off.
253 usrsctp_sysctl_set_sctp_ecn_enable(0);
254
255 // TODO(ldixon): Consider turning this on/off.
256 // This is not needed right now (we don't do dynamic address changes):
257 // If SCTP Auto-ASCONF is enabled, the peer is informed automatically
258 // when a new address is added or removed. This feature is enabled by
259 // default.
260 // usrsctp_sysctl_set_sctp_auto_asconf(0);
261
262 // TODO(ldixon): Consider turning this on/off.
263 // Add a blackhole sysctl. Setting it to 1 results in no ABORTs
264 // being sent in response to INITs, setting it to 2 results
265 // in no ABORTs being sent for received OOTB packets.
266 // This is similar to the TCP sysctl.
267 //
268 // See: http://lakerest.net/pipermail/sctp-coders/2012-January/009438.html
269 // See: http://svnweb.freebsd.org/base?view=revision&revision=229805
270 // usrsctp_sysctl_set_sctp_blackhole(2);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000271
272 // Set the number of default outgoing streams. This is the number we'll
273 // send in the SCTP INIT message. The 'appropriate default' in the
274 // second paragraph of
275 // http://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-05#section-6.2
276 // is cricket::kMaxSctpSid.
277 usrsctp_sysctl_set_sctp_nr_outgoing_streams_default(
278 cricket::kMaxSctpSid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 }
280 usrsctp_engines_count++;
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000281}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000283void SctpDataEngine::ReleaseEngine() {
284 usrsctp_engines_count--;
285 if (usrsctp_engines_count == 0) {
286 LOG(LS_INFO) << "SctpDataEngine: Shutting down";
287 if (usrsctp_finish() != 0) {
288 LOG_ERRNO(LS_ERROR) << "SctpDataEngine: usrsctp_finish failed: ";
289 }
290 }
291 LOG(LS_VERBOSE) << "usrsctp_engines_count:" << usrsctp_engines_count;
292}
293
294SctpDataEngine::SctpDataEngine() {
295 AddRefEngine();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296}
297
298SctpDataEngine::~SctpDataEngine() {
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000299 ReleaseEngine();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300}
301
302DataMediaChannel* SctpDataEngine::CreateChannel(
303 DataChannelType data_channel_type) {
304 if (data_channel_type != DCT_SCTP) {
305 return NULL;
306 }
307 return new SctpDataMediaChannel(talk_base::Thread::Current());
308}
309
310SctpDataMediaChannel::SctpDataMediaChannel(talk_base::Thread* thread)
311 : worker_thread_(thread),
wu@webrtc.org78187522013-10-07 23:32:02 +0000312 local_port_(-1),
313 remote_port_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 sock_(NULL),
315 sending_(false),
316 receiving_(false),
317 debug_name_("SctpDataMediaChannel") {
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000318 SctpDataEngine::AddRefEngine();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319}
320
321SctpDataMediaChannel::~SctpDataMediaChannel() {
322 CloseSctpSocket();
xians@webrtc.orge749c9e2014-02-13 15:09:40 +0000323 SctpDataEngine::ReleaseEngine();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324}
325
326sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) {
327 sockaddr_conn sconn = {0};
328 sconn.sconn_family = AF_CONN;
329#ifdef HAVE_SCONN_LEN
330 sconn.sconn_len = sizeof(sockaddr_conn);
331#endif
332 // Note: conversion from int to uint16_t happens here.
333 sconn.sconn_port = talk_base::HostToNetwork16(port);
334 sconn.sconn_addr = this;
335 return sconn;
336}
337
338bool SctpDataMediaChannel::OpenSctpSocket() {
339 if (sock_) {
340 LOG(LS_VERBOSE) << debug_name_
341 << "->Ignoring attempt to re-create existing socket.";
342 return false;
343 }
344 sock_ = usrsctp_socket(AF_CONN, SOCK_STREAM, IPPROTO_SCTP,
345 cricket::OnSctpInboundPacket, NULL, 0, this);
346 if (!sock_) {
347 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket.";
348 return false;
349 }
350
351 // Make the socket non-blocking. Connect, close, shutdown etc will not block
352 // the thread waiting for the socket operation to complete.
353 if (usrsctp_set_non_blocking(sock_, 1) < 0) {
354 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking.";
355 return false;
356 }
357
358 // This ensures that the usrsctp close call deletes the association. This
359 // prevents usrsctp from calling OnSctpOutboundPacket with references to
360 // this class as the address.
361 linger linger_opt;
362 linger_opt.l_onoff = 1;
363 linger_opt.l_linger = 0;
364 if (usrsctp_setsockopt(sock_, SOL_SOCKET, SO_LINGER, &linger_opt,
365 sizeof(linger_opt))) {
366 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SO_LINGER.";
367 return false;
368 }
369
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000370 // Enable stream ID resets.
371 struct sctp_assoc_value stream_rst;
372 stream_rst.assoc_id = SCTP_ALL_ASSOC;
373 stream_rst.assoc_value = 1;
374 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET,
375 &stream_rst, sizeof(stream_rst))) {
376 LOG_ERRNO(LS_ERROR) << debug_name_
377 << "Failed to set SCTP_ENABLE_STREAM_RESET.";
378 return false;
379 }
380
381 // Nagle.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000382 uint32_t nodelay = 1;
383 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay,
384 sizeof(nodelay))) {
385 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY.";
386 return false;
387 }
388
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 // Subscribe to SCTP event notifications.
390 int event_types[] = {SCTP_ASSOC_CHANGE,
391 SCTP_PEER_ADDR_CHANGE,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000392 SCTP_SEND_FAILED_EVENT,
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000393 SCTP_SENDER_DRY_EVENT,
394 SCTP_STREAM_RESET_EVENT};
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 struct sctp_event event = {0};
396 event.se_assoc_id = SCTP_ALL_ASSOC;
397 event.se_on = 1;
398 for (size_t i = 0; i < ARRAY_SIZE(event_types); i++) {
399 event.se_type = event_types[i];
400 if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_EVENT, &event,
401 sizeof(event)) < 0) {
402 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_EVENT type: "
403 << event.se_type;
404 return false;
405 }
406 }
407
408 // Register this class as an address for usrsctp. This is used by SCTP to
409 // direct the packets received (by the created socket) to this class.
410 usrsctp_register_address(this);
411 sending_ = true;
412 return true;
413}
414
415void SctpDataMediaChannel::CloseSctpSocket() {
416 sending_ = false;
417 if (sock_) {
418 // We assume that SO_LINGER option is set to close the association when
419 // close is called. This means that any pending packets in usrsctp will be
420 // discarded instead of being sent.
421 usrsctp_close(sock_);
422 sock_ = NULL;
423 usrsctp_deregister_address(this);
424 }
425}
426
427bool SctpDataMediaChannel::Connect() {
428 LOG(LS_VERBOSE) << debug_name_ << "->Connect().";
wu@webrtc.org78187522013-10-07 23:32:02 +0000429 if (remote_port_ < 0) {
430 remote_port_ = kSctpDefaultPort;
431 }
432 if (local_port_ < 0) {
433 local_port_ = kSctpDefaultPort;
434 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435
436 // If we already have a socket connection, just return.
437 if (sock_) {
438 LOG(LS_WARNING) << debug_name_ << "->Connect(): Ignored as socket "
439 "is already established.";
440 return true;
441 }
442
443 // If no socket (it was closed) try to start it again. This can happen when
444 // the socket we are connecting to closes, does an sctp shutdown handshake,
445 // or behaves unexpectedly causing us to perform a CloseSctpSocket.
446 if (!sock_ && !OpenSctpSocket()) {
447 return false;
448 }
449
450 // Note: conversion from int to uint16_t happens on assignment.
451 sockaddr_conn local_sconn = GetSctpSockAddr(local_port_);
452 if (usrsctp_bind(sock_, reinterpret_cast<sockaddr *>(&local_sconn),
453 sizeof(local_sconn)) < 0) {
454 LOG_ERRNO(LS_ERROR) << debug_name_ << "->Connect(): "
455 << ("Failed usrsctp_bind");
456 CloseSctpSocket();
457 return false;
458 }
459
460 // Note: conversion from int to uint16_t happens on assignment.
461 sockaddr_conn remote_sconn = GetSctpSockAddr(remote_port_);
462 int connect_result = usrsctp_connect(
463 sock_, reinterpret_cast<sockaddr *>(&remote_sconn), sizeof(remote_sconn));
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000464 if (connect_result < 0 && errno != SCTP_EINPROGRESS) {
465 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed usrsctp_connect. got errno="
466 << errno << ", but wanted " << SCTP_EINPROGRESS;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 CloseSctpSocket();
468 return false;
469 }
470 return true;
471}
472
473void SctpDataMediaChannel::Disconnect() {
474 // TODO(ldixon): Consider calling |usrsctp_shutdown(sock_, ...)| to do a
475 // shutdown handshake and remove the association.
476 CloseSctpSocket();
477}
478
479bool SctpDataMediaChannel::SetSend(bool send) {
480 if (!sending_ && send) {
481 return Connect();
482 }
483 if (sending_ && !send) {
484 Disconnect();
485 }
486 return true;
487}
488
489bool SctpDataMediaChannel::SetReceive(bool receive) {
490 receiving_ = receive;
491 return true;
492}
493
494bool SctpDataMediaChannel::AddSendStream(const StreamParams& stream) {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000495 return AddStream(stream);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496}
497
498bool SctpDataMediaChannel::RemoveSendStream(uint32 ssrc) {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000499 return ResetStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500}
501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502bool SctpDataMediaChannel::AddRecvStream(const StreamParams& stream) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000503 // SCTP DataChannels are always bi-directional and calling AddSendStream will
504 // enable both sending and receiving on the stream. So AddRecvStream is a
505 // no-op.
506 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507}
508
509bool SctpDataMediaChannel::RemoveRecvStream(uint32 ssrc) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000510 // SCTP DataChannels are always bi-directional and calling RemoveSendStream
511 // will disable both sending and receiving on the stream. So RemoveRecvStream
512 // is a no-op.
513 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514}
515
516bool SctpDataMediaChannel::SendData(
517 const SendDataParams& params,
518 const talk_base::Buffer& payload,
519 SendDataResult* result) {
520 if (result) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000521 // Preset |result| to assume an error. If SendData succeeds, we'll
522 // overwrite |*result| once more at the end.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 *result = SDR_ERROR;
524 }
525
526 if (!sending_) {
527 LOG(LS_WARNING) << debug_name_ << "->SendData(...): "
528 << "Not sending packet with ssrc=" << params.ssrc
529 << " len=" << payload.length() << " before SetSend(true).";
530 return false;
531 }
532
wu@webrtc.org91053e72013-08-10 07:18:04 +0000533 if (params.type != cricket::DMT_CONTROL &&
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000534 open_streams_.find(params.ssrc) == open_streams_.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 LOG(LS_WARNING) << debug_name_ << "->SendData(...): "
536 << "Not sending data because ssrc is unknown: "
537 << params.ssrc;
538 return false;
539 }
540
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 //
542 // Send data using SCTP.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000543 ssize_t send_res = 0; // result from usrsctp_sendv.
544 struct sctp_sendv_spa spa = {0};
545 spa.sendv_flags |= SCTP_SEND_SNDINFO_VALID;
546 spa.sendv_sndinfo.snd_sid = params.ssrc;
547 spa.sendv_sndinfo.snd_ppid = talk_base::HostToNetwork32(
548 GetPpid(params.type));
549
550 // Ordered implies reliable.
551 if (!params.ordered) {
552 spa.sendv_sndinfo.snd_flags |= SCTP_UNORDERED;
553 if (params.max_rtx_count >= 0 || params.max_rtx_ms == 0) {
554 spa.sendv_flags |= SCTP_SEND_PRINFO_VALID;
555 spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_RTX;
556 spa.sendv_prinfo.pr_value = params.max_rtx_count;
557 } else {
558 spa.sendv_flags |= SCTP_SEND_PRINFO_VALID;
559 spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL;
560 spa.sendv_prinfo.pr_value = params.max_rtx_ms;
561 }
562 }
563
564 // We don't fragment.
565 send_res = usrsctp_sendv(sock_, payload.data(),
566 static_cast<size_t>(payload.length()),
567 NULL, 0, &spa,
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000568 talk_base::checked_cast<socklen_t>(sizeof(spa)),
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000569 SCTP_SENDV_SPA, 0);
570 if (send_res < 0) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000571 if (errno == EWOULDBLOCK) {
572 *result = SDR_BLOCK;
573 LOG(LS_INFO) << debug_name_ << "->SendData(...): EWOULDBLOCK returned";
574 } else {
575 LOG_ERRNO(LS_ERROR) << "ERROR:" << debug_name_
576 << "->SendData(...): "
577 << " usrsctp_sendv: ";
578 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 return false;
580 }
581 if (result) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000582 // Only way out now is success.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 *result = SDR_SUCCESS;
584 }
585 return true;
586}
587
588// Called by network interface when a packet has been received.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000589void SctpDataMediaChannel::OnPacketReceived(
590 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000591 LOG(LS_VERBOSE) << debug_name_ << "->OnPacketReceived(...): " << " length="
592 << packet->length() << ", sending: " << sending_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 // Only give receiving packets to usrsctp after if connected. This enables two
594 // peers to each make a connect call, but for them not to receive an INIT
595 // packet before they have called connect; least the last receiver of the INIT
596 // packet will have called connect, and a connection will be established.
597 if (sending_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 // Pass received packet to SCTP stack. Once processed by usrsctp, the data
599 // will be will be given to the global OnSctpInboundData, and then,
600 // marshalled by a Post and handled with OnMessage.
601 usrsctp_conninput(this, packet->data(), packet->length(), 0);
602 } else {
603 // TODO(ldixon): Consider caching the packet for very slightly better
604 // reliability.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606}
607
608void SctpDataMediaChannel::OnInboundPacketFromSctpToChannel(
609 SctpInboundPacket* packet) {
610 LOG(LS_VERBOSE) << debug_name_ << "->OnInboundPacketFromSctpToChannel(...): "
611 << "Received SCTP data:"
612 << " ssrc=" << packet->params.ssrc
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 << " notification: " << (packet->flags & MSG_NOTIFICATION)
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000614 << " length=" << packet->buffer.length();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 // Sending a packet with data == NULL (no data) is SCTPs "close the
616 // connection" message. This sets sock_ = NULL;
617 if (!packet->buffer.length() || !packet->buffer.data()) {
618 LOG(LS_INFO) << debug_name_ << "->OnInboundPacketFromSctpToChannel(...): "
619 "No data, closing.";
620 return;
621 }
622 if (packet->flags & MSG_NOTIFICATION) {
623 OnNotificationFromSctp(&packet->buffer);
624 } else {
625 OnDataFromSctpToChannel(packet->params, &packet->buffer);
626 }
627}
628
629void SctpDataMediaChannel::OnDataFromSctpToChannel(
630 const ReceiveDataParams& params, talk_base::Buffer* buffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 if (receiving_) {
632 LOG(LS_VERBOSE) << debug_name_ << "->OnDataFromSctpToChannel(...): "
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000633 << "Posting with length: " << buffer->length()
634 << " on stream " << params.ssrc;
635 // Reports all received messages to upper layers, no matter whether the sid
636 // is known.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 SignalDataReceived(params, buffer->data(), buffer->length());
638 } else {
639 LOG(LS_WARNING) << debug_name_ << "->OnDataFromSctpToChannel(...): "
640 << "Not receiving packet with sid=" << params.ssrc
641 << " len=" << buffer->length()
642 << " before SetReceive(true).";
643 }
644}
645
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000646bool SctpDataMediaChannel::AddStream(const StreamParams& stream) {
647 if (!stream.has_ssrcs()) {
648 return false;
649 }
650
651 const uint32 ssrc = stream.first_ssrc();
652 if (open_streams_.find(ssrc) != open_streams_.end()) {
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000653 LOG(LS_WARNING) << debug_name_ << "->Add(Send|Recv)Stream(...): "
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000654 << "Not adding data stream '" << stream.id
655 << "' with ssrc=" << ssrc
656 << " because stream is already open.";
657 return false;
658 } else if (queued_reset_streams_.find(ssrc) != queued_reset_streams_.end()
659 || sent_reset_streams_.find(ssrc) != sent_reset_streams_.end()) {
660 LOG(LS_WARNING) << debug_name_ << "->Add(Send|Recv)Stream(...): "
661 << "Not adding data stream '" << stream.id
662 << "' with ssrc=" << ssrc
663 << " because stream is still closing.";
664 return false;
665 }
666
667 open_streams_.insert(ssrc);
668 return true;
669}
670
671bool SctpDataMediaChannel::ResetStream(uint32 ssrc) {
672 // We typically get this called twice for the same stream, once each for
673 // Send and Recv.
674 StreamSet::iterator found = open_streams_.find(ssrc);
675
676 if (found == open_streams_.end()) {
677 LOG(LS_VERBOSE) << debug_name_ << "->ResetStream(" << ssrc << "): "
678 << "stream not found.";
679 return false;
680 } else {
681 LOG(LS_VERBOSE) << debug_name_ << "->ResetStream(" << ssrc << "): "
682 << "Removing and queuing RE-CONFIG chunk.";
683 open_streams_.erase(found);
684 }
685
686 // SCTP won't let you have more than one stream reset pending at a time, but
687 // you can close multiple streams in a single reset. So, we keep an internal
688 // queue of streams-to-reset, and send them as one reset message in
689 // SendQueuedStreamResets().
690 queued_reset_streams_.insert(ssrc);
691
692 // Signal our stream-reset logic that it should try to send now, if it can.
693 SendQueuedStreamResets();
694
695 // The stream will actually get removed when we get the acknowledgment.
696 return true;
697}
698
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000699void SctpDataMediaChannel::OnNotificationFromSctp(talk_base::Buffer* buffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 const sctp_notification& notification =
701 reinterpret_cast<const sctp_notification&>(*buffer->data());
702 ASSERT(notification.sn_header.sn_length == buffer->length());
703
704 // TODO(ldixon): handle notifications appropriately.
705 switch (notification.sn_header.sn_type) {
706 case SCTP_ASSOC_CHANGE:
707 LOG(LS_VERBOSE) << "SCTP_ASSOC_CHANGE";
708 OnNotificationAssocChange(notification.sn_assoc_change);
709 break;
710 case SCTP_REMOTE_ERROR:
711 LOG(LS_INFO) << "SCTP_REMOTE_ERROR";
712 break;
713 case SCTP_SHUTDOWN_EVENT:
714 LOG(LS_INFO) << "SCTP_SHUTDOWN_EVENT";
715 break;
716 case SCTP_ADAPTATION_INDICATION:
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000717 LOG(LS_INFO) << "SCTP_ADAPTATION_INDICATION";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 break;
719 case SCTP_PARTIAL_DELIVERY_EVENT:
720 LOG(LS_INFO) << "SCTP_PARTIAL_DELIVERY_EVENT";
721 break;
722 case SCTP_AUTHENTICATION_EVENT:
723 LOG(LS_INFO) << "SCTP_AUTHENTICATION_EVENT";
724 break;
725 case SCTP_SENDER_DRY_EVENT:
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000726 LOG(LS_VERBOSE) << "SCTP_SENDER_DRY_EVENT";
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000727 SignalReadyToSend(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 break;
729 // TODO(ldixon): Unblock after congestion.
730 case SCTP_NOTIFICATIONS_STOPPED_EVENT:
731 LOG(LS_INFO) << "SCTP_NOTIFICATIONS_STOPPED_EVENT";
732 break;
733 case SCTP_SEND_FAILED_EVENT:
734 LOG(LS_INFO) << "SCTP_SEND_FAILED_EVENT";
735 break;
736 case SCTP_STREAM_RESET_EVENT:
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000737 OnStreamResetEvent(&notification.sn_strreset_event);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 break;
739 case SCTP_ASSOC_RESET_EVENT:
740 LOG(LS_INFO) << "SCTP_ASSOC_RESET_EVENT";
741 break;
742 case SCTP_STREAM_CHANGE_EVENT:
743 LOG(LS_INFO) << "SCTP_STREAM_CHANGE_EVENT";
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000744 // An acknowledgment we get after our stream resets have gone through,
745 // if they've failed. We log the message, but don't react -- we don't
746 // keep around the last-transmitted set of SSIDs we wanted to close for
747 // error recovery. It doesn't seem likely to occur, and if so, likely
748 // harmless within the lifetime of a single SCTP association.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 break;
750 default:
751 LOG(LS_WARNING) << "Unknown SCTP event: "
752 << notification.sn_header.sn_type;
753 break;
754 }
755}
756
757void SctpDataMediaChannel::OnNotificationAssocChange(
758 const sctp_assoc_change& change) {
759 switch (change.sac_state) {
760 case SCTP_COMM_UP:
761 LOG(LS_VERBOSE) << "Association change SCTP_COMM_UP";
762 break;
763 case SCTP_COMM_LOST:
764 LOG(LS_INFO) << "Association change SCTP_COMM_LOST";
765 break;
766 case SCTP_RESTART:
767 LOG(LS_INFO) << "Association change SCTP_RESTART";
768 break;
769 case SCTP_SHUTDOWN_COMP:
770 LOG(LS_INFO) << "Association change SCTP_SHUTDOWN_COMP";
771 break;
772 case SCTP_CANT_STR_ASSOC:
773 LOG(LS_INFO) << "Association change SCTP_CANT_STR_ASSOC";
774 break;
775 default:
776 LOG(LS_INFO) << "Association change UNKNOWN";
777 break;
778 }
779}
780
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000781void SctpDataMediaChannel::OnStreamResetEvent(
782 const struct sctp_stream_reset_event* evt) {
783 // A stream reset always involves two RE-CONFIG chunks for us -- we always
784 // simultaneously reset a sid's sequence number in both directions. The
785 // requesting side transmits a RE-CONFIG chunk and waits for the peer to send
786 // one back. Both sides get this SCTP_STREAM_RESET_EVENT when they receive
787 // RE-CONFIGs.
788 const int num_ssrcs = (evt->strreset_length - sizeof(*evt)) /
789 sizeof(evt->strreset_stream_list[0]);
790 LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_
791 << "): Flags = 0x"
792 << std::hex << evt->strreset_flags << " ("
793 << ListFlags(evt->strreset_flags) << ")";
794 LOG(LS_VERBOSE) << "Assoc = " << evt->strreset_assoc_id << ", Streams = ["
795 << ListArray(evt->strreset_stream_list, num_ssrcs)
796 << "], Open: ["
797 << ListStreams(open_streams_) << "], Q'd: ["
798 << ListStreams(queued_reset_streams_) << "], Sent: ["
799 << ListStreams(sent_reset_streams_) << "]";
800 bool local_stream_reset_acknowledged = false;
801
802 // If both sides try to reset some streams at the same time (even if they're
803 // disjoint sets), we can get reset failures.
804 if (evt->strreset_flags & SCTP_STREAM_RESET_FAILED) {
805 // OK, just try again. The stream IDs sent over when the RESET_FAILED flag
806 // is set seem to be garbage values. Ignore them.
807 queued_reset_streams_.insert(
808 sent_reset_streams_.begin(),
809 sent_reset_streams_.end());
810 sent_reset_streams_.clear();
811 local_stream_reset_acknowledged = true;
812
813 } else if (evt->strreset_flags & SCTP_STREAM_RESET_INCOMING_SSN) {
814 // Each side gets an event for each direction of a stream. That is,
815 // closing sid k will make each side receive INCOMING and OUTGOING reset
816 // events for k. As per RFC6525, Section 5, paragraph 2, each side will
817 // get an INCOMING event first.
818 for (int i = 0; i < num_ssrcs; i++) {
819 const int stream_id = evt->strreset_stream_list[i];
820
821 // See if this stream ID was closed by our peer or ourselves.
822 StreamSet::iterator it = sent_reset_streams_.find(stream_id);
823
824 // The reset was requested locally.
825 if (it != sent_reset_streams_.end()) {
826 LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_
827 << "): local sid " << stream_id << " acknowledged.";
828 local_stream_reset_acknowledged = true;
829 sent_reset_streams_.erase(it);
830
831 } else if ((it = open_streams_.find(stream_id))
832 != open_streams_.end()) {
833 // The peer requested the reset.
834 LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_
835 << "): closing sid " << stream_id;
836 open_streams_.erase(it);
837 SignalStreamClosed(stream_id);
838
839 } else if ((it = queued_reset_streams_.find(stream_id))
840 != queued_reset_streams_.end()) {
841 // The peer requested the reset, but there was a local reset
842 // queued.
843 LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_
844 << "): double-sided close for sid " << stream_id;
845 // Both sides want the stream closed, and the peer got to send the
846 // RE-CONFIG first. Treat it like the local Remove(Send|Recv)Stream
847 // finished quickly.
848 queued_reset_streams_.erase(it);
849
850 } else {
851 // This stream is unknown. Sometimes this can be from an
852 // RESET_FAILED-related retransmit.
853 LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_
854 << "): Unknown sid " << stream_id;
855 }
856 }
857 }
858
859 if (local_stream_reset_acknowledged) {
860 // This message acknowledges the last stream-reset request we sent out
861 // (only one can be outstanding at a time). Send out the next one.
862 SendQueuedStreamResets();
863 }
864}
865
wu@webrtc.org78187522013-10-07 23:32:02 +0000866// Puts the specified |param| from the codec identified by |id| into |dest|
867// and returns true. Or returns false if it wasn't there, leaving |dest|
868// untouched.
869static bool GetCodecIntParameter(const std::vector<DataCodec>& codecs,
870 int id, const std::string& name,
871 const std::string& param, int* dest) {
872 std::string value;
873 Codec match_pattern;
874 match_pattern.id = id;
875 match_pattern.name = name;
876 for (size_t i = 0; i < codecs.size(); ++i) {
877 if (codecs[i].Matches(match_pattern)) {
878 if (codecs[i].GetParam(param, &value)) {
879 *dest = talk_base::FromString<int>(value);
880 return true;
881 }
882 }
883 }
884 return false;
885}
886
887bool SctpDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) {
888 return GetCodecIntParameter(
889 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort,
890 &remote_port_);
891}
892
893bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) {
894 return GetCodecIntParameter(
895 codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort,
896 &local_port_);
897}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898
899void SctpDataMediaChannel::OnPacketFromSctpToNetwork(
900 talk_base::Buffer* buffer) {
901 if (buffer->length() > kSctpMtu) {
902 LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): "
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000903 << "SCTP seems to have made a packet that is bigger "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 "than its official MTU.";
905 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000906 MediaChannel::SendPacket(buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907}
908
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000909bool SctpDataMediaChannel::SendQueuedStreamResets() {
910 if (!sent_reset_streams_.empty() || queued_reset_streams_.empty())
911 return true;
912
913 LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending ["
914 << ListStreams(queued_reset_streams_) << "], Open: ["
915 << ListStreams(open_streams_) << "], Sent: ["
916 << ListStreams(sent_reset_streams_) << "]";
917
918 const size_t num_streams = queued_reset_streams_.size();
919 const size_t num_bytes = sizeof(struct sctp_reset_streams)
920 + (num_streams * sizeof(uint16));
921
922 std::vector<uint8> reset_stream_buf(num_bytes, 0);
923 struct sctp_reset_streams* resetp = reinterpret_cast<sctp_reset_streams*>(
924 &reset_stream_buf[0]);
925 resetp->srs_assoc_id = SCTP_ALL_ASSOC;
926 resetp->srs_flags = SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_OUTGOING;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000927 resetp->srs_number_streams = talk_base::checked_cast<uint16_t>(num_streams);
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000928 int result_idx = 0;
929 for (StreamSet::iterator it = queued_reset_streams_.begin();
930 it != queued_reset_streams_.end(); ++it) {
931 resetp->srs_stream_list[result_idx++] = *it;
932 }
933
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000934 int ret = usrsctp_setsockopt(
935 sock_, IPPROTO_SCTP, SCTP_RESET_STREAMS, resetp,
936 talk_base::checked_cast<socklen_t>(reset_stream_buf.size()));
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000937 if (ret < 0) {
938 LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to send a stream reset for "
939 << num_streams << " streams";
940 return false;
941 }
942
943 // sent_reset_streams_ is empty, and all the queued_reset_streams_ go into
944 // it now.
945 queued_reset_streams_.swap(sent_reset_streams_);
946 return true;
947}
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949void SctpDataMediaChannel::OnMessage(talk_base::Message* msg) {
950 switch (msg->message_id) {
951 case MSG_SCTPINBOUNDPACKET: {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000952 talk_base::scoped_ptr<InboundPacketMessage> pdata(
953 static_cast<InboundPacketMessage*>(msg->pdata));
954 OnInboundPacketFromSctpToChannel(pdata->data().get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 break;
956 }
957 case MSG_SCTPOUTBOUNDPACKET: {
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000958 talk_base::scoped_ptr<OutboundPacketMessage> pdata(
959 static_cast<OutboundPacketMessage*>(msg->pdata));
960 OnPacketFromSctpToNetwork(pdata->data().get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 break;
962 }
963 }
964}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965} // namespace cricket