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