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 | #include "webrtc/media/sctp/sctpdataengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | #include <stdarg.h> |
| 14 | #include <stdio.h> |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 15 | |
| 16 | #include <memory> |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 17 | #include <sstream> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | #include "usrsctplib/usrsctp.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 21 | #include "webrtc/base/arraysize.h" |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 22 | #include "webrtc/base/copyonwritebuffer.h" |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 23 | #include "webrtc/base/criticalsection.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 24 | #include "webrtc/base/helpers.h" |
| 25 | #include "webrtc/base/logging.h" |
Tommi | d44c077 | 2016-03-11 17:12:32 -0800 | [diff] [blame] | 26 | #include "webrtc/base/safe_conversions.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 27 | #include "webrtc/media/base/codec.h" |
kjellander | f475277 | 2016-03-02 05:42:30 -0800 | [diff] [blame] | 28 | #include "webrtc/media/base/mediaconstants.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 29 | #include "webrtc/media/base/streamparams.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 31 | namespace cricket { |
| 32 | // The biggest SCTP packet. Starting from a 'safe' wire MTU value of 1280, |
| 33 | // take off 80 bytes for DTLS/TURN/TCP/IP overhead. |
| 34 | static const size_t kSctpMtu = 1200; |
| 35 | |
| 36 | // The size of the SCTP association send buffer. 256kB, the usrsctp default. |
| 37 | static const int kSendBufferSize = 262144; |
| 38 | |
| 39 | struct SctpInboundPacket { |
| 40 | rtc::CopyOnWriteBuffer buffer; |
| 41 | ReceiveDataParams params; |
| 42 | // The |flags| parameter is used by SCTP to distinguish notification packets |
| 43 | // from other types of packets. |
| 44 | int flags; |
| 45 | }; |
| 46 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 47 | namespace { |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 48 | // Set the initial value of the static SCTP Data Engines reference count. |
| 49 | int g_usrsctp_usage_count = 0; |
| 50 | rtc::GlobalLockPod g_usrsctp_lock_; |
| 51 | |
| 52 | typedef SctpDataMediaChannel::StreamSet StreamSet; |
| 53 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 54 | // Returns a comma-separated, human-readable list of the stream IDs in 's' |
| 55 | std::string ListStreams(const StreamSet& s) { |
| 56 | std::stringstream result; |
| 57 | bool first = true; |
wu@webrtc.org | e00265e | 2014-01-07 19:32:40 +0000 | [diff] [blame] | 58 | for (StreamSet::const_iterator it = s.begin(); it != s.end(); ++it) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 59 | if (!first) { |
| 60 | result << ", " << *it; |
| 61 | } else { |
| 62 | result << *it; |
| 63 | first = false; |
| 64 | } |
| 65 | } |
| 66 | return result.str(); |
| 67 | } |
| 68 | |
| 69 | // Returns a pipe-separated, human-readable list of the SCTP_STREAM_RESET |
| 70 | // flags in 'flags' |
| 71 | std::string ListFlags(int flags) { |
| 72 | std::stringstream result; |
| 73 | bool first = true; |
| 74 | // Skip past the first 12 chars (strlen("SCTP_STREAM_")) |
| 75 | #define MAKEFLAG(X) { X, #X + 12} |
| 76 | struct flaginfo_t { |
| 77 | int value; |
| 78 | const char* name; |
| 79 | } flaginfo[] = { |
| 80 | MAKEFLAG(SCTP_STREAM_RESET_INCOMING_SSN), |
| 81 | MAKEFLAG(SCTP_STREAM_RESET_OUTGOING_SSN), |
| 82 | MAKEFLAG(SCTP_STREAM_RESET_DENIED), |
| 83 | MAKEFLAG(SCTP_STREAM_RESET_FAILED), |
| 84 | MAKEFLAG(SCTP_STREAM_CHANGE_DENIED) |
| 85 | }; |
| 86 | #undef MAKEFLAG |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 87 | for (uint32_t i = 0; i < arraysize(flaginfo); ++i) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 88 | if (flags & flaginfo[i].value) { |
| 89 | if (!first) result << " | "; |
| 90 | result << flaginfo[i].name; |
| 91 | first = false; |
| 92 | } |
| 93 | } |
| 94 | return result.str(); |
| 95 | } |
| 96 | |
| 97 | // Returns a comma-separated, human-readable list of the integers in 'array'. |
| 98 | // All 'num_elems' of them. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 99 | std::string ListArray(const uint16_t* array, int num_elems) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 100 | std::stringstream result; |
| 101 | for (int i = 0; i < num_elems; ++i) { |
| 102 | if (i) { |
| 103 | result << ", " << array[i]; |
| 104 | } else { |
| 105 | result << array[i]; |
| 106 | } |
| 107 | } |
| 108 | return result.str(); |
| 109 | } |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 110 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | typedef rtc::ScopedMessageData<SctpInboundPacket> InboundPacketMessage; |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 112 | typedef rtc::ScopedMessageData<rtc::CopyOnWriteBuffer> OutboundPacketMessage; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | enum { |
| 115 | MSG_SCTPINBOUNDPACKET = 1, // MessageData is SctpInboundPacket |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 116 | MSG_SCTPOUTBOUNDPACKET = 2, // MessageData is rtc:Buffer |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 119 | // Helper for logging SCTP messages. |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 120 | void DebugSctpPrintf(const char* format, ...) { |
| 121 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | char s[255]; |
| 123 | va_list ap; |
| 124 | va_start(ap, format); |
| 125 | vsnprintf(s, sizeof(s), format, ap); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 126 | LOG(LS_INFO) << "SCTP: " << s; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | va_end(ap); |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 128 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | } |
| 130 | |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 131 | // Get the PPID to use for the terminating fragment of this type. |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 132 | SctpDataMediaChannel::PayloadProtocolIdentifier GetPpid(DataMessageType type) { |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 133 | switch (type) { |
| 134 | default: |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 135 | case DMT_NONE: |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 136 | return SctpDataMediaChannel::PPID_NONE; |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 137 | case DMT_CONTROL: |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 138 | return SctpDataMediaChannel::PPID_CONTROL; |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 139 | case DMT_BINARY: |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 140 | return SctpDataMediaChannel::PPID_BINARY_LAST; |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 141 | case DMT_TEXT: |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 142 | return SctpDataMediaChannel::PPID_TEXT_LAST; |
| 143 | }; |
| 144 | } |
| 145 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 146 | bool GetDataMediaType(SctpDataMediaChannel::PayloadProtocolIdentifier ppid, |
| 147 | DataMessageType* dest) { |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 148 | ASSERT(dest != NULL); |
| 149 | switch (ppid) { |
| 150 | case SctpDataMediaChannel::PPID_BINARY_PARTIAL: |
| 151 | case SctpDataMediaChannel::PPID_BINARY_LAST: |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 152 | *dest = DMT_BINARY; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 153 | return true; |
| 154 | |
| 155 | case SctpDataMediaChannel::PPID_TEXT_PARTIAL: |
| 156 | case SctpDataMediaChannel::PPID_TEXT_LAST: |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 157 | *dest = DMT_TEXT; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 158 | return true; |
| 159 | |
| 160 | case SctpDataMediaChannel::PPID_CONTROL: |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 161 | *dest = DMT_CONTROL; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 162 | return true; |
| 163 | |
| 164 | case SctpDataMediaChannel::PPID_NONE: |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 165 | *dest = DMT_NONE; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 166 | return true; |
| 167 | |
| 168 | default: |
| 169 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Lally Singh | 4c277bb | 2015-05-08 14:39:04 -0400 | [diff] [blame] | 173 | // Log the packet in text2pcap format, if log level is at LS_VERBOSE. |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 174 | void VerboseLogPacket(const void* data, size_t length, int direction) { |
Lally Singh | 4c277bb | 2015-05-08 14:39:04 -0400 | [diff] [blame] | 175 | if (LOG_CHECK_LEVEL(LS_VERBOSE) && length > 0) { |
| 176 | char *dump_buf; |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 177 | // Some downstream project uses an older version of usrsctp that expects |
| 178 | // a non-const "void*" as first parameter when dumping the packet, so we |
| 179 | // need to cast the const away here to avoid a compiler error. |
Lally Singh | 4c277bb | 2015-05-08 14:39:04 -0400 | [diff] [blame] | 180 | if ((dump_buf = usrsctp_dumppacket( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 181 | const_cast<void*>(data), length, direction)) != NULL) { |
Lally Singh | 4c277bb | 2015-05-08 14:39:04 -0400 | [diff] [blame] | 182 | LOG(LS_VERBOSE) << dump_buf; |
| 183 | usrsctp_freedumpbuffer(dump_buf); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 188 | // This is the callback usrsctp uses when there's data to send on the network |
| 189 | // that has been wrapped appropriatly for the SCTP protocol. |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 190 | int OnSctpOutboundPacket(void* addr, |
| 191 | void* data, |
| 192 | size_t length, |
| 193 | uint8_t tos, |
| 194 | uint8_t set_df) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 195 | SctpDataMediaChannel* channel = static_cast<SctpDataMediaChannel*>(addr); |
| 196 | LOG(LS_VERBOSE) << "global OnSctpOutboundPacket():" |
| 197 | << "addr: " << addr << "; length: " << length |
| 198 | << "; tos: " << std::hex << static_cast<int>(tos) |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 199 | << "; set_df: " << std::hex << static_cast<int>(set_df); |
Lally Singh | 4c277bb | 2015-05-08 14:39:04 -0400 | [diff] [blame] | 200 | |
deadbeef | e9fc75e | 2016-06-13 17:30:32 -0700 | [diff] [blame] | 201 | VerboseLogPacket(data, length, SCTP_DUMP_OUTBOUND); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | // Note: We have to copy the data; the caller will delete it. |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 203 | auto* msg = new OutboundPacketMessage( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 204 | new rtc::CopyOnWriteBuffer(reinterpret_cast<uint8_t*>(data), length)); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 205 | channel->worker_thread()->Post(RTC_FROM_HERE, channel, MSG_SCTPOUTBOUNDPACKET, |
| 206 | msg); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | // This is the callback called from usrsctp when data has been received, after |
| 211 | // a packet has been interpreted and parsed by usrsctp and found to contain |
| 212 | // payload data. It is called by a usrsctp thread. It is assumed this function |
| 213 | // will free the memory used by 'data'. |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 214 | int OnSctpInboundPacket(struct socket* sock, |
| 215 | union sctp_sockstore addr, |
| 216 | void* data, |
| 217 | size_t length, |
| 218 | struct sctp_rcvinfo rcv, |
| 219 | int flags, |
| 220 | void* ulp_info) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 221 | SctpDataMediaChannel* channel = static_cast<SctpDataMediaChannel*>(ulp_info); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | // Post data to the channel's receiver thread (copying it). |
| 223 | // TODO(ldixon): Unclear if copy is needed as this method is responsible for |
| 224 | // memory cleanup. But this does simplify code. |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 225 | const SctpDataMediaChannel::PayloadProtocolIdentifier ppid = |
| 226 | static_cast<SctpDataMediaChannel::PayloadProtocolIdentifier>( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 227 | rtc::HostToNetwork32(rcv.rcv_ppid)); |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 228 | DataMessageType type = DMT_NONE; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 229 | if (!GetDataMediaType(ppid, &type) && !(flags & MSG_NOTIFICATION)) { |
| 230 | // It's neither a notification nor a recognized data packet. Drop it. |
| 231 | LOG(LS_ERROR) << "Received an unknown PPID " << ppid |
| 232 | << " on an SCTP packet. Dropping."; |
| 233 | } else { |
| 234 | SctpInboundPacket* packet = new SctpInboundPacket; |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 235 | packet->buffer.SetData(reinterpret_cast<uint8_t*>(data), length); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 236 | packet->params.ssrc = rcv.rcv_sid; |
| 237 | packet->params.seq_num = rcv.rcv_ssn; |
| 238 | packet->params.timestamp = rcv.rcv_tsn; |
| 239 | packet->params.type = type; |
| 240 | packet->flags = flags; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 241 | // The ownership of |packet| transfers to |msg|. |
| 242 | InboundPacketMessage* msg = new InboundPacketMessage(packet); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 243 | channel->worker_thread()->Post(RTC_FROM_HERE, channel, |
| 244 | MSG_SCTPINBOUNDPACKET, msg); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 245 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 246 | free(data); |
| 247 | return 1; |
| 248 | } |
| 249 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 250 | void InitializeUsrSctp() { |
| 251 | LOG(LS_INFO) << __FUNCTION__; |
| 252 | // First argument is udp_encapsulation_port, which is not releveant for our |
| 253 | // AF_CONN use of sctp. |
| 254 | usrsctp_init(0, &OnSctpOutboundPacket, &DebugSctpPrintf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 256 | // To turn on/off detailed SCTP debugging. You will also need to have the |
| 257 | // SCTP_DEBUG cpp defines flag. |
| 258 | // usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 259 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 260 | // TODO(ldixon): Consider turning this on/off. |
| 261 | usrsctp_sysctl_set_sctp_ecn_enable(0); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 262 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 263 | // This is harmless, but we should find out when the library default |
| 264 | // changes. |
| 265 | int send_size = usrsctp_sysctl_get_sctp_sendspace(); |
| 266 | if (send_size != kSendBufferSize) { |
| 267 | LOG(LS_ERROR) << "Got different send size than expected: " << send_size; |
| 268 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 270 | // TODO(ldixon): Consider turning this on/off. |
| 271 | // This is not needed right now (we don't do dynamic address changes): |
| 272 | // If SCTP Auto-ASCONF is enabled, the peer is informed automatically |
| 273 | // when a new address is added or removed. This feature is enabled by |
| 274 | // default. |
| 275 | // usrsctp_sysctl_set_sctp_auto_asconf(0); |
| 276 | |
| 277 | // TODO(ldixon): Consider turning this on/off. |
| 278 | // Add a blackhole sysctl. Setting it to 1 results in no ABORTs |
| 279 | // being sent in response to INITs, setting it to 2 results |
| 280 | // in no ABORTs being sent for received OOTB packets. |
| 281 | // This is similar to the TCP sysctl. |
| 282 | // |
| 283 | // See: http://lakerest.net/pipermail/sctp-coders/2012-January/009438.html |
| 284 | // See: http://svnweb.freebsd.org/base?view=revision&revision=229805 |
| 285 | // usrsctp_sysctl_set_sctp_blackhole(2); |
| 286 | |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 287 | // Set the number of default outgoing streams. This is the number we'll |
| 288 | // send in the SCTP INIT message. |
| 289 | usrsctp_sysctl_set_sctp_nr_outgoing_streams_default(kMaxSctpStreams); |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void UninitializeUsrSctp() { |
| 293 | LOG(LS_INFO) << __FUNCTION__; |
| 294 | // usrsctp_finish() may fail if it's called too soon after the channels are |
| 295 | // closed. Wait and try again until it succeeds for up to 3 seconds. |
| 296 | for (size_t i = 0; i < 300; ++i) { |
| 297 | if (usrsctp_finish() == 0) { |
| 298 | return; |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 301 | rtc::Thread::SleepMs(10); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 302 | } |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 303 | LOG(LS_ERROR) << "Failed to shutdown usrsctp."; |
| 304 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 305 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 306 | void IncrementUsrSctpUsageCount() { |
| 307 | rtc::GlobalLockScope lock(&g_usrsctp_lock_); |
| 308 | if (!g_usrsctp_usage_count) { |
| 309 | InitializeUsrSctp(); |
| 310 | } |
| 311 | ++g_usrsctp_usage_count; |
| 312 | } |
| 313 | |
| 314 | void DecrementUsrSctpUsageCount() { |
| 315 | rtc::GlobalLockScope lock(&g_usrsctp_lock_); |
| 316 | --g_usrsctp_usage_count; |
| 317 | if (!g_usrsctp_usage_count) { |
| 318 | UninitializeUsrSctp(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | DataCodec GetSctpDataCodec() { |
| 323 | DataCodec codec(kGoogleSctpDataCodecId, kGoogleSctpDataCodecName); |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 324 | codec.SetParam(kCodecParamPort, kSctpDefaultPort); |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 325 | return codec; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 328 | } // namespace |
jiayl@webrtc.org | f8063d3 | 2014-06-18 21:30:40 +0000 | [diff] [blame] | 329 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 330 | SctpDataEngine::SctpDataEngine() : codecs_(1, GetSctpDataCodec()) {} |
jiayl@webrtc.org | f8063d3 | 2014-06-18 21:30:40 +0000 | [diff] [blame] | 331 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 332 | SctpDataEngine::~SctpDataEngine() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 334 | // Called on the worker thread. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 335 | DataMediaChannel* SctpDataEngine::CreateChannel( |
| 336 | DataChannelType data_channel_type) { |
| 337 | if (data_channel_type != DCT_SCTP) { |
| 338 | return NULL; |
| 339 | } |
tommi | 7391881 | 2015-08-27 04:29:58 -0700 | [diff] [blame] | 340 | return new SctpDataMediaChannel(rtc::Thread::Current()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 343 | // static |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 344 | SctpDataMediaChannel* SctpDataMediaChannel::GetChannelFromSocket( |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 345 | struct socket* sock) { |
| 346 | struct sockaddr* addrs = nullptr; |
| 347 | int naddrs = usrsctp_getladdrs(sock, 0, &addrs); |
| 348 | if (naddrs <= 0 || addrs[0].sa_family != AF_CONN) { |
| 349 | return nullptr; |
| 350 | } |
| 351 | // usrsctp_getladdrs() returns the addresses bound to this socket, which |
| 352 | // contains the SctpDataMediaChannel* as sconn_addr. Read the pointer, |
| 353 | // then free the list of addresses once we have the pointer. We only open |
| 354 | // AF_CONN sockets, and they should all have the sconn_addr set to the |
| 355 | // pointer that created them, so [0] is as good as any other. |
| 356 | struct sockaddr_conn* sconn = |
| 357 | reinterpret_cast<struct sockaddr_conn*>(&addrs[0]); |
| 358 | SctpDataMediaChannel* channel = |
| 359 | reinterpret_cast<SctpDataMediaChannel*>(sconn->sconn_addr); |
| 360 | usrsctp_freeladdrs(addrs); |
| 361 | |
| 362 | return channel; |
| 363 | } |
| 364 | |
| 365 | // static |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 366 | int SctpDataMediaChannel::SendThresholdCallback(struct socket* sock, |
| 367 | uint32_t sb_free) { |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 368 | // Fired on our I/O thread. SctpDataMediaChannel::OnPacketReceived() gets |
| 369 | // a packet containing acknowledgments, which goes into usrsctp_conninput, |
| 370 | // and then back here. |
| 371 | SctpDataMediaChannel* channel = GetChannelFromSocket(sock); |
| 372 | if (!channel) { |
| 373 | LOG(LS_ERROR) << "SendThresholdCallback: Failed to get channel for socket " |
| 374 | << sock; |
| 375 | return 0; |
| 376 | } |
| 377 | channel->OnSendThresholdCallback(); |
| 378 | return 0; |
| 379 | } |
| 380 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 381 | SctpDataMediaChannel::SctpDataMediaChannel(rtc::Thread* thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | : worker_thread_(thread), |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 383 | local_port_(kSctpDefaultPort), |
| 384 | remote_port_(kSctpDefaultPort), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 385 | sock_(NULL), |
| 386 | sending_(false), |
| 387 | receiving_(false), |
| 388 | debug_name_("SctpDataMediaChannel") { |
| 389 | } |
| 390 | |
| 391 | SctpDataMediaChannel::~SctpDataMediaChannel() { |
| 392 | CloseSctpSocket(); |
| 393 | } |
| 394 | |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 395 | void SctpDataMediaChannel::OnSendThresholdCallback() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 396 | RTC_DCHECK(rtc::Thread::Current() == worker_thread_); |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 397 | SignalReadyToSend(true); |
| 398 | } |
| 399 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 400 | sockaddr_conn SctpDataMediaChannel::GetSctpSockAddr(int port) { |
| 401 | sockaddr_conn sconn = {0}; |
| 402 | sconn.sconn_family = AF_CONN; |
| 403 | #ifdef HAVE_SCONN_LEN |
| 404 | sconn.sconn_len = sizeof(sockaddr_conn); |
| 405 | #endif |
| 406 | // Note: conversion from int to uint16_t happens here. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 407 | sconn.sconn_port = rtc::HostToNetwork16(port); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 408 | sconn.sconn_addr = this; |
| 409 | return sconn; |
| 410 | } |
| 411 | |
| 412 | bool SctpDataMediaChannel::OpenSctpSocket() { |
| 413 | if (sock_) { |
| 414 | LOG(LS_VERBOSE) << debug_name_ |
| 415 | << "->Ignoring attempt to re-create existing socket."; |
| 416 | return false; |
| 417 | } |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 418 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 419 | IncrementUsrSctpUsageCount(); |
| 420 | |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 421 | // If kSendBufferSize isn't reflective of reality, we log an error, but we |
| 422 | // still have to do something reasonable here. Look up what the buffer's |
| 423 | // real size is and set our threshold to something reasonable. |
| 424 | const static int kSendThreshold = usrsctp_sysctl_get_sctp_sendspace() / 2; |
| 425 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 426 | sock_ = usrsctp_socket( |
| 427 | AF_CONN, SOCK_STREAM, IPPROTO_SCTP, OnSctpInboundPacket, |
| 428 | &SctpDataMediaChannel::SendThresholdCallback, kSendThreshold, this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | if (!sock_) { |
| 430 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to create SCTP socket."; |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 431 | DecrementUsrSctpUsageCount(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | |
| 435 | // Make the socket non-blocking. Connect, close, shutdown etc will not block |
| 436 | // the thread waiting for the socket operation to complete. |
| 437 | if (usrsctp_set_non_blocking(sock_, 1) < 0) { |
| 438 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP to non blocking."; |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | // This ensures that the usrsctp close call deletes the association. This |
| 443 | // prevents usrsctp from calling OnSctpOutboundPacket with references to |
| 444 | // this class as the address. |
| 445 | linger linger_opt; |
| 446 | linger_opt.l_onoff = 1; |
| 447 | linger_opt.l_linger = 0; |
| 448 | if (usrsctp_setsockopt(sock_, SOL_SOCKET, SO_LINGER, &linger_opt, |
| 449 | sizeof(linger_opt))) { |
| 450 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SO_LINGER."; |
| 451 | return false; |
| 452 | } |
| 453 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 454 | // Enable stream ID resets. |
| 455 | struct sctp_assoc_value stream_rst; |
| 456 | stream_rst.assoc_id = SCTP_ALL_ASSOC; |
| 457 | stream_rst.assoc_value = 1; |
| 458 | if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_ENABLE_STREAM_RESET, |
| 459 | &stream_rst, sizeof(stream_rst))) { |
| 460 | LOG_ERRNO(LS_ERROR) << debug_name_ |
| 461 | << "Failed to set SCTP_ENABLE_STREAM_RESET."; |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | // Nagle. |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 466 | uint32_t nodelay = 1; |
| 467 | if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_NODELAY, &nodelay, |
| 468 | sizeof(nodelay))) { |
| 469 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_NODELAY."; |
| 470 | return false; |
| 471 | } |
| 472 | |
buildbot@webrtc.org | 624a504 | 2014-08-05 22:13:05 +0000 | [diff] [blame] | 473 | // Disable MTU discovery |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 474 | sctp_paddrparams params = {{0}}; |
buildbot@webrtc.org | 624a504 | 2014-08-05 22:13:05 +0000 | [diff] [blame] | 475 | params.spp_assoc_id = 0; |
| 476 | params.spp_flags = SPP_PMTUD_DISABLE; |
| 477 | params.spp_pathmtu = kSctpMtu; |
| 478 | if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, ¶ms, |
| 479 | sizeof(params))) { |
| 480 | LOG_ERRNO(LS_ERROR) << debug_name_ |
| 481 | << "Failed to set SCTP_PEER_ADDR_PARAMS."; |
| 482 | return false; |
| 483 | } |
| 484 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 485 | // Subscribe to SCTP event notifications. |
| 486 | int event_types[] = {SCTP_ASSOC_CHANGE, |
| 487 | SCTP_PEER_ADDR_CHANGE, |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 488 | SCTP_SEND_FAILED_EVENT, |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 489 | SCTP_SENDER_DRY_EVENT, |
| 490 | SCTP_STREAM_RESET_EVENT}; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 491 | struct sctp_event event = {0}; |
| 492 | event.se_assoc_id = SCTP_ALL_ASSOC; |
| 493 | event.se_on = 1; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 494 | for (size_t i = 0; i < arraysize(event_types); i++) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 495 | event.se_type = event_types[i]; |
| 496 | if (usrsctp_setsockopt(sock_, IPPROTO_SCTP, SCTP_EVENT, &event, |
| 497 | sizeof(event)) < 0) { |
| 498 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to set SCTP_EVENT type: " |
| 499 | << event.se_type; |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Register this class as an address for usrsctp. This is used by SCTP to |
| 505 | // direct the packets received (by the created socket) to this class. |
| 506 | usrsctp_register_address(this); |
| 507 | sending_ = true; |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | void SctpDataMediaChannel::CloseSctpSocket() { |
| 512 | sending_ = false; |
| 513 | if (sock_) { |
| 514 | // We assume that SO_LINGER option is set to close the association when |
| 515 | // close is called. This means that any pending packets in usrsctp will be |
| 516 | // discarded instead of being sent. |
| 517 | usrsctp_close(sock_); |
| 518 | sock_ = NULL; |
| 519 | usrsctp_deregister_address(this); |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 520 | |
| 521 | DecrementUsrSctpUsageCount(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | bool SctpDataMediaChannel::Connect() { |
| 526 | LOG(LS_VERBOSE) << debug_name_ << "->Connect()."; |
| 527 | |
| 528 | // If we already have a socket connection, just return. |
| 529 | if (sock_) { |
| 530 | LOG(LS_WARNING) << debug_name_ << "->Connect(): Ignored as socket " |
| 531 | "is already established."; |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | // If no socket (it was closed) try to start it again. This can happen when |
| 536 | // the socket we are connecting to closes, does an sctp shutdown handshake, |
| 537 | // or behaves unexpectedly causing us to perform a CloseSctpSocket. |
| 538 | if (!sock_ && !OpenSctpSocket()) { |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | // Note: conversion from int to uint16_t happens on assignment. |
| 543 | sockaddr_conn local_sconn = GetSctpSockAddr(local_port_); |
| 544 | if (usrsctp_bind(sock_, reinterpret_cast<sockaddr *>(&local_sconn), |
| 545 | sizeof(local_sconn)) < 0) { |
| 546 | LOG_ERRNO(LS_ERROR) << debug_name_ << "->Connect(): " |
| 547 | << ("Failed usrsctp_bind"); |
| 548 | CloseSctpSocket(); |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | // Note: conversion from int to uint16_t happens on assignment. |
| 553 | sockaddr_conn remote_sconn = GetSctpSockAddr(remote_port_); |
| 554 | int connect_result = usrsctp_connect( |
| 555 | sock_, reinterpret_cast<sockaddr *>(&remote_sconn), sizeof(remote_sconn)); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 556 | if (connect_result < 0 && errno != SCTP_EINPROGRESS) { |
| 557 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed usrsctp_connect. got errno=" |
| 558 | << errno << ", but wanted " << SCTP_EINPROGRESS; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 559 | CloseSctpSocket(); |
| 560 | return false; |
| 561 | } |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | void SctpDataMediaChannel::Disconnect() { |
| 566 | // TODO(ldixon): Consider calling |usrsctp_shutdown(sock_, ...)| to do a |
| 567 | // shutdown handshake and remove the association. |
| 568 | CloseSctpSocket(); |
| 569 | } |
| 570 | |
| 571 | bool SctpDataMediaChannel::SetSend(bool send) { |
| 572 | if (!sending_ && send) { |
| 573 | return Connect(); |
| 574 | } |
| 575 | if (sending_ && !send) { |
| 576 | Disconnect(); |
| 577 | } |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | bool SctpDataMediaChannel::SetReceive(bool receive) { |
| 582 | receiving_ = receive; |
| 583 | return true; |
| 584 | } |
| 585 | |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 586 | bool SctpDataMediaChannel::SetSendParameters(const DataSendParameters& params) { |
| 587 | return SetSendCodecs(params.codecs); |
| 588 | } |
| 589 | |
| 590 | bool SctpDataMediaChannel::SetRecvParameters(const DataRecvParameters& params) { |
| 591 | return SetRecvCodecs(params.codecs); |
| 592 | } |
| 593 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 594 | bool SctpDataMediaChannel::AddSendStream(const StreamParams& stream) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 595 | return AddStream(stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 598 | bool SctpDataMediaChannel::RemoveSendStream(uint32_t ssrc) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 599 | return ResetStream(ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 600 | } |
| 601 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 602 | bool SctpDataMediaChannel::AddRecvStream(const StreamParams& stream) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 603 | // SCTP DataChannels are always bi-directional and calling AddSendStream will |
| 604 | // enable both sending and receiving on the stream. So AddRecvStream is a |
| 605 | // no-op. |
| 606 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 609 | bool SctpDataMediaChannel::RemoveRecvStream(uint32_t ssrc) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 610 | // SCTP DataChannels are always bi-directional and calling RemoveSendStream |
| 611 | // will disable both sending and receiving on the stream. So RemoveRecvStream |
| 612 | // is a no-op. |
| 613 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | bool SctpDataMediaChannel::SendData( |
| 617 | const SendDataParams& params, |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 618 | const rtc::CopyOnWriteBuffer& payload, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 619 | SendDataResult* result) { |
| 620 | if (result) { |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 621 | // Preset |result| to assume an error. If SendData succeeds, we'll |
| 622 | // overwrite |*result| once more at the end. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 623 | *result = SDR_ERROR; |
| 624 | } |
| 625 | |
| 626 | if (!sending_) { |
| 627 | LOG(LS_WARNING) << debug_name_ << "->SendData(...): " |
| 628 | << "Not sending packet with ssrc=" << params.ssrc |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 629 | << " len=" << payload.size() << " before SetSend(true)."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
| 632 | |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 633 | if (params.type != DMT_CONTROL && |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 634 | open_streams_.find(params.ssrc) == open_streams_.end()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 635 | LOG(LS_WARNING) << debug_name_ << "->SendData(...): " |
| 636 | << "Not sending data because ssrc is unknown: " |
| 637 | << params.ssrc; |
| 638 | return false; |
| 639 | } |
| 640 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 641 | // |
| 642 | // Send data using SCTP. |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 643 | ssize_t send_res = 0; // result from usrsctp_sendv. |
| 644 | struct sctp_sendv_spa spa = {0}; |
| 645 | spa.sendv_flags |= SCTP_SEND_SNDINFO_VALID; |
| 646 | spa.sendv_sndinfo.snd_sid = params.ssrc; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 647 | spa.sendv_sndinfo.snd_ppid = rtc::HostToNetwork32( |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 648 | GetPpid(params.type)); |
| 649 | |
| 650 | // Ordered implies reliable. |
| 651 | if (!params.ordered) { |
| 652 | spa.sendv_sndinfo.snd_flags |= SCTP_UNORDERED; |
| 653 | if (params.max_rtx_count >= 0 || params.max_rtx_ms == 0) { |
| 654 | spa.sendv_flags |= SCTP_SEND_PRINFO_VALID; |
| 655 | spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_RTX; |
| 656 | spa.sendv_prinfo.pr_value = params.max_rtx_count; |
| 657 | } else { |
| 658 | spa.sendv_flags |= SCTP_SEND_PRINFO_VALID; |
| 659 | spa.sendv_prinfo.pr_policy = SCTP_PR_SCTP_TTL; |
| 660 | spa.sendv_prinfo.pr_value = params.max_rtx_ms; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | // We don't fragment. |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 665 | send_res = usrsctp_sendv( |
| 666 | sock_, payload.data(), static_cast<size_t>(payload.size()), NULL, 0, &spa, |
| 667 | rtc::checked_cast<socklen_t>(sizeof(spa)), SCTP_SENDV_SPA, 0); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 668 | if (send_res < 0) { |
jiayl@webrtc.org | f7026cd | 2014-05-08 16:02:23 +0000 | [diff] [blame] | 669 | if (errno == SCTP_EWOULDBLOCK) { |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 670 | *result = SDR_BLOCK; |
| 671 | LOG(LS_INFO) << debug_name_ << "->SendData(...): EWOULDBLOCK returned"; |
| 672 | } else { |
| 673 | LOG_ERRNO(LS_ERROR) << "ERROR:" << debug_name_ |
| 674 | << "->SendData(...): " |
| 675 | << " usrsctp_sendv: "; |
| 676 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 677 | return false; |
| 678 | } |
| 679 | if (result) { |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 680 | // Only way out now is success. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 681 | *result = SDR_SUCCESS; |
| 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | // Called by network interface when a packet has been received. |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 687 | void SctpDataMediaChannel::OnPacketReceived( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 688 | rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 689 | RTC_DCHECK(rtc::Thread::Current() == worker_thread_); |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 690 | LOG(LS_VERBOSE) << debug_name_ << "->OnPacketReceived(...): " |
| 691 | << " length=" << packet->size() << ", sending: " << sending_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 692 | // Only give receiving packets to usrsctp after if connected. This enables two |
| 693 | // peers to each make a connect call, but for them not to receive an INIT |
| 694 | // packet before they have called connect; least the last receiver of the INIT |
| 695 | // packet will have called connect, and a connection will be established. |
| 696 | if (sending_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 697 | // Pass received packet to SCTP stack. Once processed by usrsctp, the data |
| 698 | // will be will be given to the global OnSctpInboundData, and then, |
| 699 | // marshalled by a Post and handled with OnMessage. |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 700 | VerboseLogPacket(packet->cdata(), packet->size(), SCTP_DUMP_INBOUND); |
| 701 | usrsctp_conninput(this, packet->cdata(), packet->size(), 0); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 702 | } else { |
| 703 | // TODO(ldixon): Consider caching the packet for very slightly better |
| 704 | // reliability. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | void SctpDataMediaChannel::OnInboundPacketFromSctpToChannel( |
| 709 | SctpInboundPacket* packet) { |
| 710 | LOG(LS_VERBOSE) << debug_name_ << "->OnInboundPacketFromSctpToChannel(...): " |
| 711 | << "Received SCTP data:" |
| 712 | << " ssrc=" << packet->params.ssrc |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 713 | << " notification: " << (packet->flags & MSG_NOTIFICATION) |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 714 | << " length=" << packet->buffer.size(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 715 | // Sending a packet with data == NULL (no data) is SCTPs "close the |
| 716 | // connection" message. This sets sock_ = NULL; |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 717 | if (!packet->buffer.size() || !packet->buffer.data()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 718 | LOG(LS_INFO) << debug_name_ << "->OnInboundPacketFromSctpToChannel(...): " |
| 719 | "No data, closing."; |
| 720 | return; |
| 721 | } |
| 722 | if (packet->flags & MSG_NOTIFICATION) { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 723 | OnNotificationFromSctp(packet->buffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 724 | } else { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 725 | OnDataFromSctpToChannel(packet->params, packet->buffer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | void SctpDataMediaChannel::OnDataFromSctpToChannel( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 730 | const ReceiveDataParams& params, const rtc::CopyOnWriteBuffer& buffer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 731 | if (receiving_) { |
| 732 | LOG(LS_VERBOSE) << debug_name_ << "->OnDataFromSctpToChannel(...): " |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 733 | << "Posting with length: " << buffer.size() |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 734 | << " on stream " << params.ssrc; |
| 735 | // Reports all received messages to upper layers, no matter whether the sid |
| 736 | // is known. |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 737 | SignalDataReceived(params, buffer.data<char>(), buffer.size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 738 | } else { |
| 739 | LOG(LS_WARNING) << debug_name_ << "->OnDataFromSctpToChannel(...): " |
| 740 | << "Not receiving packet with sid=" << params.ssrc |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 741 | << " len=" << buffer.size() << " before SetReceive(true)."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 745 | bool SctpDataMediaChannel::AddStream(const StreamParams& stream) { |
| 746 | if (!stream.has_ssrcs()) { |
| 747 | return false; |
| 748 | } |
| 749 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 750 | const uint32_t ssrc = stream.first_ssrc(); |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 751 | if (ssrc > kMaxSctpSid) { |
lally | 27ed3cc | 2016-01-11 10:24:33 -0800 | [diff] [blame] | 752 | LOG(LS_WARNING) << debug_name_ << "->Add(Send|Recv)Stream(...): " |
| 753 | << "Not adding data stream '" << stream.id |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 754 | << "' with sid=" << ssrc << " because sid is too high."; |
lally | 27ed3cc | 2016-01-11 10:24:33 -0800 | [diff] [blame] | 755 | return false; |
| 756 | } else if (open_streams_.find(ssrc) != open_streams_.end()) { |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 757 | LOG(LS_WARNING) << debug_name_ << "->Add(Send|Recv)Stream(...): " |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 758 | << "Not adding data stream '" << stream.id |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 759 | << "' with sid=" << ssrc |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 760 | << " because stream is already open."; |
| 761 | return false; |
| 762 | } else if (queued_reset_streams_.find(ssrc) != queued_reset_streams_.end() |
| 763 | || sent_reset_streams_.find(ssrc) != sent_reset_streams_.end()) { |
| 764 | LOG(LS_WARNING) << debug_name_ << "->Add(Send|Recv)Stream(...): " |
| 765 | << "Not adding data stream '" << stream.id |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 766 | << "' with sid=" << ssrc |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 767 | << " because stream is still closing."; |
| 768 | return false; |
| 769 | } |
| 770 | |
| 771 | open_streams_.insert(ssrc); |
| 772 | return true; |
| 773 | } |
| 774 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 775 | bool SctpDataMediaChannel::ResetStream(uint32_t ssrc) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 776 | // We typically get this called twice for the same stream, once each for |
| 777 | // Send and Recv. |
| 778 | StreamSet::iterator found = open_streams_.find(ssrc); |
| 779 | |
| 780 | if (found == open_streams_.end()) { |
| 781 | LOG(LS_VERBOSE) << debug_name_ << "->ResetStream(" << ssrc << "): " |
| 782 | << "stream not found."; |
| 783 | return false; |
| 784 | } else { |
| 785 | LOG(LS_VERBOSE) << debug_name_ << "->ResetStream(" << ssrc << "): " |
| 786 | << "Removing and queuing RE-CONFIG chunk."; |
| 787 | open_streams_.erase(found); |
| 788 | } |
| 789 | |
| 790 | // SCTP won't let you have more than one stream reset pending at a time, but |
| 791 | // you can close multiple streams in a single reset. So, we keep an internal |
| 792 | // queue of streams-to-reset, and send them as one reset message in |
| 793 | // SendQueuedStreamResets(). |
| 794 | queued_reset_streams_.insert(ssrc); |
| 795 | |
| 796 | // Signal our stream-reset logic that it should try to send now, if it can. |
| 797 | SendQueuedStreamResets(); |
| 798 | |
| 799 | // The stream will actually get removed when we get the acknowledgment. |
| 800 | return true; |
| 801 | } |
| 802 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 803 | void SctpDataMediaChannel::OnNotificationFromSctp( |
| 804 | const rtc::CopyOnWriteBuffer& buffer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 805 | const sctp_notification& notification = |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 806 | reinterpret_cast<const sctp_notification&>(*buffer.data()); |
| 807 | ASSERT(notification.sn_header.sn_length == buffer.size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 808 | |
| 809 | // TODO(ldixon): handle notifications appropriately. |
| 810 | switch (notification.sn_header.sn_type) { |
| 811 | case SCTP_ASSOC_CHANGE: |
| 812 | LOG(LS_VERBOSE) << "SCTP_ASSOC_CHANGE"; |
| 813 | OnNotificationAssocChange(notification.sn_assoc_change); |
| 814 | break; |
| 815 | case SCTP_REMOTE_ERROR: |
| 816 | LOG(LS_INFO) << "SCTP_REMOTE_ERROR"; |
| 817 | break; |
| 818 | case SCTP_SHUTDOWN_EVENT: |
| 819 | LOG(LS_INFO) << "SCTP_SHUTDOWN_EVENT"; |
| 820 | break; |
| 821 | case SCTP_ADAPTATION_INDICATION: |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 822 | LOG(LS_INFO) << "SCTP_ADAPTATION_INDICATION"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 823 | break; |
| 824 | case SCTP_PARTIAL_DELIVERY_EVENT: |
| 825 | LOG(LS_INFO) << "SCTP_PARTIAL_DELIVERY_EVENT"; |
| 826 | break; |
| 827 | case SCTP_AUTHENTICATION_EVENT: |
| 828 | LOG(LS_INFO) << "SCTP_AUTHENTICATION_EVENT"; |
| 829 | break; |
| 830 | case SCTP_SENDER_DRY_EVENT: |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 831 | LOG(LS_VERBOSE) << "SCTP_SENDER_DRY_EVENT"; |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 832 | SignalReadyToSend(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 833 | break; |
| 834 | // TODO(ldixon): Unblock after congestion. |
| 835 | case SCTP_NOTIFICATIONS_STOPPED_EVENT: |
| 836 | LOG(LS_INFO) << "SCTP_NOTIFICATIONS_STOPPED_EVENT"; |
| 837 | break; |
| 838 | case SCTP_SEND_FAILED_EVENT: |
| 839 | LOG(LS_INFO) << "SCTP_SEND_FAILED_EVENT"; |
| 840 | break; |
| 841 | case SCTP_STREAM_RESET_EVENT: |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 842 | OnStreamResetEvent(¬ification.sn_strreset_event); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 843 | break; |
| 844 | case SCTP_ASSOC_RESET_EVENT: |
| 845 | LOG(LS_INFO) << "SCTP_ASSOC_RESET_EVENT"; |
| 846 | break; |
| 847 | case SCTP_STREAM_CHANGE_EVENT: |
| 848 | LOG(LS_INFO) << "SCTP_STREAM_CHANGE_EVENT"; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 849 | // An acknowledgment we get after our stream resets have gone through, |
| 850 | // if they've failed. We log the message, but don't react -- we don't |
| 851 | // keep around the last-transmitted set of SSIDs we wanted to close for |
| 852 | // error recovery. It doesn't seem likely to occur, and if so, likely |
| 853 | // harmless within the lifetime of a single SCTP association. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 854 | break; |
| 855 | default: |
| 856 | LOG(LS_WARNING) << "Unknown SCTP event: " |
| 857 | << notification.sn_header.sn_type; |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | void SctpDataMediaChannel::OnNotificationAssocChange( |
| 863 | const sctp_assoc_change& change) { |
| 864 | switch (change.sac_state) { |
| 865 | case SCTP_COMM_UP: |
| 866 | LOG(LS_VERBOSE) << "Association change SCTP_COMM_UP"; |
| 867 | break; |
| 868 | case SCTP_COMM_LOST: |
| 869 | LOG(LS_INFO) << "Association change SCTP_COMM_LOST"; |
| 870 | break; |
| 871 | case SCTP_RESTART: |
| 872 | LOG(LS_INFO) << "Association change SCTP_RESTART"; |
| 873 | break; |
| 874 | case SCTP_SHUTDOWN_COMP: |
| 875 | LOG(LS_INFO) << "Association change SCTP_SHUTDOWN_COMP"; |
| 876 | break; |
| 877 | case SCTP_CANT_STR_ASSOC: |
| 878 | LOG(LS_INFO) << "Association change SCTP_CANT_STR_ASSOC"; |
| 879 | break; |
| 880 | default: |
| 881 | LOG(LS_INFO) << "Association change UNKNOWN"; |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 886 | void SctpDataMediaChannel::OnStreamResetEvent( |
| 887 | const struct sctp_stream_reset_event* evt) { |
| 888 | // A stream reset always involves two RE-CONFIG chunks for us -- we always |
| 889 | // simultaneously reset a sid's sequence number in both directions. The |
| 890 | // requesting side transmits a RE-CONFIG chunk and waits for the peer to send |
| 891 | // one back. Both sides get this SCTP_STREAM_RESET_EVENT when they receive |
| 892 | // RE-CONFIGs. |
| 893 | const int num_ssrcs = (evt->strreset_length - sizeof(*evt)) / |
| 894 | sizeof(evt->strreset_stream_list[0]); |
| 895 | LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_ |
| 896 | << "): Flags = 0x" |
| 897 | << std::hex << evt->strreset_flags << " (" |
| 898 | << ListFlags(evt->strreset_flags) << ")"; |
| 899 | LOG(LS_VERBOSE) << "Assoc = " << evt->strreset_assoc_id << ", Streams = [" |
| 900 | << ListArray(evt->strreset_stream_list, num_ssrcs) |
| 901 | << "], Open: [" |
| 902 | << ListStreams(open_streams_) << "], Q'd: [" |
| 903 | << ListStreams(queued_reset_streams_) << "], Sent: [" |
| 904 | << ListStreams(sent_reset_streams_) << "]"; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 905 | |
| 906 | // If both sides try to reset some streams at the same time (even if they're |
| 907 | // disjoint sets), we can get reset failures. |
| 908 | if (evt->strreset_flags & SCTP_STREAM_RESET_FAILED) { |
| 909 | // OK, just try again. The stream IDs sent over when the RESET_FAILED flag |
| 910 | // is set seem to be garbage values. Ignore them. |
| 911 | queued_reset_streams_.insert( |
| 912 | sent_reset_streams_.begin(), |
| 913 | sent_reset_streams_.end()); |
| 914 | sent_reset_streams_.clear(); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 915 | |
| 916 | } else if (evt->strreset_flags & SCTP_STREAM_RESET_INCOMING_SSN) { |
| 917 | // Each side gets an event for each direction of a stream. That is, |
| 918 | // closing sid k will make each side receive INCOMING and OUTGOING reset |
| 919 | // events for k. As per RFC6525, Section 5, paragraph 2, each side will |
| 920 | // get an INCOMING event first. |
| 921 | for (int i = 0; i < num_ssrcs; i++) { |
| 922 | const int stream_id = evt->strreset_stream_list[i]; |
| 923 | |
| 924 | // See if this stream ID was closed by our peer or ourselves. |
| 925 | StreamSet::iterator it = sent_reset_streams_.find(stream_id); |
| 926 | |
| 927 | // The reset was requested locally. |
| 928 | if (it != sent_reset_streams_.end()) { |
| 929 | LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_ |
| 930 | << "): local sid " << stream_id << " acknowledged."; |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 931 | sent_reset_streams_.erase(it); |
| 932 | |
| 933 | } else if ((it = open_streams_.find(stream_id)) |
| 934 | != open_streams_.end()) { |
| 935 | // The peer requested the reset. |
| 936 | LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_ |
| 937 | << "): closing sid " << stream_id; |
| 938 | open_streams_.erase(it); |
buildbot@webrtc.org | 1d66be2 | 2014-05-29 22:54:24 +0000 | [diff] [blame] | 939 | SignalStreamClosedRemotely(stream_id); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 940 | |
| 941 | } else if ((it = queued_reset_streams_.find(stream_id)) |
| 942 | != queued_reset_streams_.end()) { |
| 943 | // The peer requested the reset, but there was a local reset |
| 944 | // queued. |
| 945 | LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_ |
| 946 | << "): double-sided close for sid " << stream_id; |
| 947 | // Both sides want the stream closed, and the peer got to send the |
| 948 | // RE-CONFIG first. Treat it like the local Remove(Send|Recv)Stream |
| 949 | // finished quickly. |
| 950 | queued_reset_streams_.erase(it); |
| 951 | |
| 952 | } else { |
| 953 | // This stream is unknown. Sometimes this can be from an |
| 954 | // RESET_FAILED-related retransmit. |
| 955 | LOG(LS_VERBOSE) << "SCTP_STREAM_RESET_EVENT(" << debug_name_ |
| 956 | << "): Unknown sid " << stream_id; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
jiayl@webrtc.org | 1a6c628 | 2014-06-12 21:59:29 +0000 | [diff] [blame] | 961 | // Always try to send the queued RESET because this call indicates that the |
| 962 | // last local RESET or remote RESET has made some progress. |
| 963 | SendQueuedStreamResets(); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 964 | } |
| 965 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 966 | // Puts the specified |param| from the codec identified by |id| into |dest| |
| 967 | // and returns true. Or returns false if it wasn't there, leaving |dest| |
| 968 | // untouched. |
| 969 | static bool GetCodecIntParameter(const std::vector<DataCodec>& codecs, |
| 970 | int id, const std::string& name, |
| 971 | const std::string& param, int* dest) { |
| 972 | std::string value; |
| 973 | Codec match_pattern; |
| 974 | match_pattern.id = id; |
| 975 | match_pattern.name = name; |
| 976 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 977 | if (codecs[i].Matches(match_pattern)) { |
| 978 | if (codecs[i].GetParam(param, &value)) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 979 | *dest = rtc::FromString<int>(value); |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 980 | return true; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | bool SctpDataMediaChannel::SetSendCodecs(const std::vector<DataCodec>& codecs) { |
| 988 | return GetCodecIntParameter( |
| 989 | codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, |
| 990 | &remote_port_); |
| 991 | } |
| 992 | |
| 993 | bool SctpDataMediaChannel::SetRecvCodecs(const std::vector<DataCodec>& codecs) { |
| 994 | return GetCodecIntParameter( |
| 995 | codecs, kGoogleSctpDataCodecId, kGoogleSctpDataCodecName, kCodecParamPort, |
| 996 | &local_port_); |
| 997 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 998 | |
| 999 | void SctpDataMediaChannel::OnPacketFromSctpToNetwork( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 1000 | rtc::CopyOnWriteBuffer* buffer) { |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 1001 | // usrsctp seems to interpret the MTU we give it strangely -- it seems to |
| 1002 | // give us back packets bigger than that MTU, if only by a fixed amount. |
| 1003 | // This is that amount that we've observed. |
| 1004 | const int kSctpOverhead = 76; |
| 1005 | if (buffer->size() > (kSctpOverhead + kSctpMtu)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1006 | LOG(LS_ERROR) << debug_name_ << "->OnPacketFromSctpToNetwork(...): " |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1007 | << "SCTP seems to have made a packet that is bigger " |
Lally Singh | e8386d2 | 2015-08-28 14:54:37 -0400 | [diff] [blame] | 1008 | << "than its official MTU: " << buffer->size() |
| 1009 | << " vs max of " << kSctpMtu |
| 1010 | << " even after adding " << kSctpOverhead |
| 1011 | << " extra SCTP overhead"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1012 | } |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 1013 | MediaChannel::SendPacket(buffer, rtc::PacketOptions()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1016 | bool SctpDataMediaChannel::SendQueuedStreamResets() { |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 1017 | if (!sent_reset_streams_.empty() || queued_reset_streams_.empty()) { |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1018 | return true; |
Tommi | 7d01331 | 2016-05-19 19:58:38 +0200 | [diff] [blame] | 1019 | } |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1020 | |
| 1021 | LOG(LS_VERBOSE) << "SendQueuedStreamResets[" << debug_name_ << "]: Sending [" |
| 1022 | << ListStreams(queued_reset_streams_) << "], Open: [" |
| 1023 | << ListStreams(open_streams_) << "], Sent: [" |
| 1024 | << ListStreams(sent_reset_streams_) << "]"; |
| 1025 | |
| 1026 | const size_t num_streams = queued_reset_streams_.size(); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1027 | const size_t num_bytes = |
| 1028 | sizeof(struct sctp_reset_streams) + (num_streams * sizeof(uint16_t)); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1029 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1030 | std::vector<uint8_t> reset_stream_buf(num_bytes, 0); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1031 | struct sctp_reset_streams* resetp = reinterpret_cast<sctp_reset_streams*>( |
| 1032 | &reset_stream_buf[0]); |
| 1033 | resetp->srs_assoc_id = SCTP_ALL_ASSOC; |
| 1034 | resetp->srs_flags = SCTP_STREAM_RESET_INCOMING | SCTP_STREAM_RESET_OUTGOING; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1035 | resetp->srs_number_streams = rtc::checked_cast<uint16_t>(num_streams); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1036 | int result_idx = 0; |
| 1037 | for (StreamSet::iterator it = queued_reset_streams_.begin(); |
| 1038 | it != queued_reset_streams_.end(); ++it) { |
| 1039 | resetp->srs_stream_list[result_idx++] = *it; |
| 1040 | } |
| 1041 | |
jiayl@webrtc.org | a576faf | 2014-01-29 17:45:53 +0000 | [diff] [blame] | 1042 | int ret = usrsctp_setsockopt( |
| 1043 | sock_, IPPROTO_SCTP, SCTP_RESET_STREAMS, resetp, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1044 | rtc::checked_cast<socklen_t>(reset_stream_buf.size())); |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1045 | if (ret < 0) { |
| 1046 | LOG_ERRNO(LS_ERROR) << debug_name_ << "Failed to send a stream reset for " |
| 1047 | << num_streams << " streams"; |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | // sent_reset_streams_ is empty, and all the queued_reset_streams_ go into |
| 1052 | // it now. |
| 1053 | queued_reset_streams_.swap(sent_reset_streams_); |
| 1054 | return true; |
| 1055 | } |
| 1056 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1057 | void SctpDataMediaChannel::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1058 | switch (msg->message_id) { |
| 1059 | case MSG_SCTPINBOUNDPACKET: { |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 1060 | std::unique_ptr<InboundPacketMessage> pdata( |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1061 | static_cast<InboundPacketMessage*>(msg->pdata)); |
| 1062 | OnInboundPacketFromSctpToChannel(pdata->data().get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | } |
| 1065 | case MSG_SCTPOUTBOUNDPACKET: { |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 1066 | std::unique_ptr<OutboundPacketMessage> pdata( |
wu@webrtc.org | f6d6ed0 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 1067 | static_cast<OutboundPacketMessage*>(msg->pdata)); |
| 1068 | OnPacketFromSctpToNetwork(pdata->data().get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1073 | } // namespace cricket |