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