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