| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/p2p/socket_host.h" | 5 #include "content/browser/renderer_host/p2p/socket_host.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
| 9 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 9 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 10 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" | 10 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" |
| 11 #include "content/browser/renderer_host/p2p/socket_host_udp.h" | 11 #include "content/browser/renderer_host/p2p/socket_host_udp.h" |
| 12 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "crypto/hmac.h" | 14 #include "third_party/libjingle/source/talk/media/base/rtputils.h" |
| 15 #include "third_party/webrtc/base/asyncpacketsocket.h" | 15 #include "third_party/libjingle/source/talk/media/base/turnutils.h" |
| 16 #include "third_party/webrtc/base/byteorder.h" | |
| 17 #include "third_party/webrtc/base/messagedigest.h" | |
| 18 #include "third_party/webrtc/p2p/base/stun.h" | |
| 19 | 16 |
| 20 namespace { | 17 namespace { |
| 21 | 18 |
| 22 const uint32_t kStunMagicCookie = 0x2112A442; | 19 const uint32_t kStunMagicCookie = 0x2112A442; |
| 23 const size_t kMinRtpHeaderLength = 12; | |
| 24 const size_t kMinRtcpHeaderLength = 8; | 20 const size_t kMinRtcpHeaderLength = 8; |
| 25 const size_t kRtpExtensionHeaderLength = 4; | |
| 26 const size_t kDtlsRecordHeaderLength = 13; | 21 const size_t kDtlsRecordHeaderLength = 13; |
| 27 const size_t kTurnChannelHeaderLength = 4; | |
| 28 const size_t kAbsSendTimeExtensionLength = 3; | |
| 29 const size_t kOneByteHeaderLength = 1; | |
| 30 const size_t kMaxRtpPacketLength = 2048; | |
| 31 | |
| 32 // Fake auth tag written by the render process if external authentication is | |
| 33 // enabled. HMAC in packet will be compared against this value before updating | |
| 34 // packet with actual HMAC value. | |
| 35 static const unsigned char kFakeAuthTag[10] = { | |
| 36 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd, 0xba, 0xdd | |
| 37 }; | |
| 38 | |
| 39 bool IsTurnChannelData(const char* data, size_t length) { | |
| 40 return length >= kTurnChannelHeaderLength && ((*data & 0xC0) == 0x40); | |
| 41 } | |
| 42 | 22 |
| 43 bool IsDtlsPacket(const char* data, size_t length) { | 23 bool IsDtlsPacket(const char* data, size_t length) { |
| 44 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); | 24 const uint8_t* u = reinterpret_cast<const uint8_t*>(data); |
| 45 return (length >= kDtlsRecordHeaderLength && (u[0] > 19 && u[0] < 64)); | 25 return (length >= kDtlsRecordHeaderLength && (u[0] > 19 && u[0] < 64)); |
| 46 } | 26 } |
| 47 | 27 |
| 48 bool IsRtcpPacket(const char* data, size_t length) { | 28 bool IsRtcpPacket(const char* data, size_t length) { |
| 49 if (length < kMinRtcpHeaderLength) { | 29 if (length < kMinRtcpHeaderLength) { |
| 50 return false; | 30 return false; |
| 51 } | 31 } |
| 52 | 32 |
| 53 int type = (static_cast<uint8_t>(data[1]) & 0x7F); | 33 int type = (static_cast<uint8_t>(data[1]) & 0x7F); |
| 54 return (type >= 64 && type < 96); | 34 return (type >= 64 && type < 96); |
| 55 } | 35 } |
| 56 | 36 |
| 57 bool IsTurnSendIndicationPacket(const char* data, size_t length) { | |
| 58 if (length < content::P2PSocketHost::kStunHeaderSize) { | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 uint16_t type = rtc::GetBE16(data); | |
| 63 return (type == cricket::TURN_SEND_INDICATION); | |
| 64 } | |
| 65 | |
| 66 bool IsRtpPacket(const char* data, size_t length) { | |
| 67 return (length >= kMinRtpHeaderLength) && ((*data & 0xC0) == 0x80); | |
| 68 } | |
| 69 | |
| 70 // Verifies rtp header and message length. | |
| 71 bool ValidateRtpHeader(const char* rtp, size_t length, size_t* header_length) { | |
| 72 if (header_length) { | |
| 73 *header_length = 0; | |
| 74 } | |
| 75 | |
| 76 if (length < kMinRtpHeaderLength) { | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 size_t cc_count = rtp[0] & 0x0F; | |
| 81 size_t header_length_without_extension = kMinRtpHeaderLength + 4 * cc_count; | |
| 82 if (header_length_without_extension > length) { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 // If extension bit is not set, we are done with header processing, as input | |
| 87 // length is verified above. | |
| 88 if (!(rtp[0] & 0x10)) { | |
| 89 if (header_length) | |
| 90 *header_length = header_length_without_extension; | |
| 91 | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 rtp += header_length_without_extension; | |
| 96 | |
| 97 if (header_length_without_extension + kRtpExtensionHeaderLength > length) { | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 // Getting extension profile length. | |
| 102 // Length is in 32 bit words. | |
| 103 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); | |
| 104 size_t extension_length = extension_length_in_32bits * 4; | |
| 105 | |
| 106 size_t rtp_header_length = extension_length + | |
| 107 header_length_without_extension + | |
| 108 kRtpExtensionHeaderLength; | |
| 109 | |
| 110 // Verify input length against total header size. | |
| 111 if (rtp_header_length > length) { | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 if (header_length) { | |
| 116 *header_length = rtp_header_length; | |
| 117 } | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 void UpdateAbsSendTimeExtensionValue(char* extension_data, | |
| 122 size_t length, | |
| 123 uint32_t abs_send_time) { | |
| 124 // Absolute send time in RTP streams. | |
| 125 // | |
| 126 // The absolute send time is signaled to the receiver in-band using the | |
| 127 // general mechanism for RTP header extensions [RFC5285]. The payload | |
| 128 // of this extension (the transmitted value) is a 24-bit unsigned integer | |
| 129 // containing the sender's current time in seconds as a fixed point number | |
| 130 // with 18 bits fractional part. | |
| 131 // | |
| 132 // The form of the absolute send time extension block: | |
| 133 // | |
| 134 // 0 1 2 3 | |
| 135 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
| 136 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 137 // | ID | len=2 | absolute send time | | |
| 138 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 139 if (length != kAbsSendTimeExtensionLength) { | |
| 140 NOTREACHED(); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 // Now() has resolution ~1-15ms | |
| 145 uint32_t now_second = abs_send_time; | |
| 146 if (!now_second) { | |
| 147 uint64_t now_us = | |
| 148 (base::TimeTicks::Now() - base::TimeTicks()).InMicroseconds(); | |
| 149 // Convert second to 24-bit unsigned with 18 bit fractional part | |
| 150 now_second = | |
| 151 ((now_us << 18) / base::Time::kMicrosecondsPerSecond) & 0x00FFFFFF; | |
| 152 } | |
| 153 // TODO(mallinath) - Add SetBE24 to byteorder.h in libjingle. | |
| 154 extension_data[0] = static_cast<uint8_t>(now_second >> 16); | |
| 155 extension_data[1] = static_cast<uint8_t>(now_second >> 8); | |
| 156 extension_data[2] = static_cast<uint8_t>(now_second); | |
| 157 } | |
| 158 | |
| 159 // Assumes |length| is actual packet length + tag length. Updates HMAC at end of | |
| 160 // the RTP packet. | |
| 161 void UpdateRtpAuthTag(char* rtp, | |
| 162 size_t length, | |
| 163 const rtc::PacketOptions& options) { | |
| 164 // If there is no key, return. | |
| 165 if (options.packet_time_params.srtp_auth_key.empty()) { | |
| 166 return; | |
| 167 } | |
| 168 | |
| 169 size_t tag_length = options.packet_time_params.srtp_auth_tag_len; | |
| 170 | |
| 171 // ROC (rollover counter) is at the beginning of the auth tag. | |
| 172 const size_t kRocLength = 4; | |
| 173 if (tag_length < kRocLength || tag_length > length) { | |
| 174 NOTREACHED(); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
| 179 if (!hmac.Init(reinterpret_cast<const unsigned char*>( | |
| 180 &options.packet_time_params.srtp_auth_key[0]), | |
| 181 options.packet_time_params.srtp_auth_key.size())) { | |
| 182 NOTREACHED(); | |
| 183 return; | |
| 184 } | |
| 185 | |
| 186 if (tag_length > hmac.DigestLength()) { | |
| 187 NOTREACHED(); | |
| 188 return; | |
| 189 } | |
| 190 | |
| 191 char* auth_tag = rtp + (length - tag_length); | |
| 192 | |
| 193 // We should have a fake HMAC value @ auth_tag. | |
| 194 DCHECK_EQ(0, memcmp(auth_tag, kFakeAuthTag, tag_length)); | |
| 195 | |
| 196 // Copy ROC after end of rtp packet. | |
| 197 memcpy(auth_tag, &options.packet_time_params.srtp_packet_index, kRocLength); | |
| 198 // Authentication of a RTP packet will have RTP packet + ROC size. | |
| 199 int auth_required_length = length - tag_length + kRocLength; | |
| 200 | |
| 201 unsigned char output[64]; | |
| 202 if (!hmac.Sign(base::StringPiece(rtp, auth_required_length), | |
| 203 output, sizeof(output))) { | |
| 204 NOTREACHED(); | |
| 205 return; | |
| 206 } | |
| 207 // Copy HMAC from output to packet. This is required as auth tag length | |
| 208 // may not be equal to the actual HMAC length. | |
| 209 memcpy(auth_tag, output, tag_length); | |
| 210 } | |
| 211 | |
| 212 } // namespace | 37 } // namespace |
| 213 | 38 |
| 214 namespace content { | 39 namespace content { |
| 215 | 40 |
| 216 namespace packet_processing_helpers { | |
| 217 | |
| 218 bool ApplyPacketOptions(char* data, | |
| 219 size_t length, | |
| 220 const rtc::PacketOptions& options, | |
| 221 uint32_t abs_send_time) { | |
| 222 DCHECK(data != NULL); | |
| 223 DCHECK(length > 0); | |
| 224 // if there is no valid |rtp_sendtime_extension_id| and |srtp_auth_key| in | |
| 225 // PacketOptions, nothing to be updated in this packet. | |
| 226 if (options.packet_time_params.rtp_sendtime_extension_id == -1 && | |
| 227 options.packet_time_params.srtp_auth_key.empty()) { | |
| 228 return true; | |
| 229 } | |
| 230 | |
| 231 DCHECK(!IsDtlsPacket(data, length)); | |
| 232 DCHECK(!IsRtcpPacket(data, length)); | |
| 233 | |
| 234 // If there is a srtp auth key present then packet must be a RTP packet. | |
| 235 // RTP packet may have been wrapped in a TURN Channel Data or | |
| 236 // TURN send indication. | |
| 237 size_t rtp_start_pos; | |
| 238 size_t rtp_length; | |
| 239 if (!GetRtpPacketStartPositionAndLength( | |
| 240 data, length, &rtp_start_pos, &rtp_length)) { | |
| 241 // This method should never return false. | |
| 242 NOTREACHED(); | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 // Skip to rtp packet. | |
| 247 char* start = data + rtp_start_pos; | |
| 248 // If packet option has non default value (-1) for sendtime extension id, | |
| 249 // then we should parse the rtp packet to update the timestamp. Otherwise | |
| 250 // just calculate HMAC and update packet with it. | |
| 251 if (options.packet_time_params.rtp_sendtime_extension_id != -1) { | |
| 252 UpdateRtpAbsSendTimeExtension( | |
| 253 start, | |
| 254 rtp_length, | |
| 255 options.packet_time_params.rtp_sendtime_extension_id, | |
| 256 abs_send_time); | |
| 257 } | |
| 258 | |
| 259 UpdateRtpAuthTag(start, rtp_length, options); | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 263 bool GetRtpPacketStartPositionAndLength(const char* packet, | |
| 264 size_t length, | |
| 265 size_t* rtp_start_pos, | |
| 266 size_t* rtp_packet_length) { | |
| 267 if (length < kMinRtpHeaderLength || length > kMaxRtpPacketLength) { | |
| 268 return false; | |
| 269 } | |
| 270 | |
| 271 size_t rtp_begin; | |
| 272 size_t rtp_length = 0; | |
| 273 if (IsTurnChannelData(packet, length)) { | |
| 274 // Turn Channel Message header format. | |
| 275 // 0 1 2 3 | |
| 276 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
| 277 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 278 // | Channel Number | Length | | |
| 279 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 280 // | | | |
| 281 // / Application Data / | |
| 282 // / / | |
| 283 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 284 rtp_begin = kTurnChannelHeaderLength; | |
| 285 rtp_length = rtc::GetBE16(&packet[2]); | |
| 286 if (length < rtp_length + kTurnChannelHeaderLength) { | |
| 287 return false; | |
| 288 } | |
| 289 } else if (IsTurnSendIndicationPacket(packet, length)) { | |
| 290 // Validate STUN message length. | |
| 291 const size_t stun_message_length = rtc::GetBE16(&packet[2]); | |
| 292 if (stun_message_length + P2PSocketHost::kStunHeaderSize != length) { | |
| 293 return false; | |
| 294 } | |
| 295 | |
| 296 // First skip mandatory stun header which is of 20 bytes. | |
| 297 rtp_begin = P2PSocketHost::kStunHeaderSize; | |
| 298 // Loop through STUN attributes until we find STUN DATA attribute. | |
| 299 bool data_attr_present = false; | |
| 300 while (rtp_begin < length) { | |
| 301 // Keep reading STUN attributes until we hit DATA attribute. | |
| 302 // Attribute will be a TLV structure. | |
| 303 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 304 // | Type | Length | | |
| 305 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 306 // | Value (variable) .... | |
| 307 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 308 // The value in the length field MUST contain the length of the Value | |
| 309 // part of the attribute, prior to padding, measured in bytes. Since | |
| 310 // STUN aligns attributes on 32-bit boundaries, attributes whose content | |
| 311 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of | |
| 312 // padding so that its value contains a multiple of 4 bytes. The | |
| 313 // padding bits are ignored, and may be any value. | |
| 314 uint16_t attr_type, attr_length; | |
| 315 const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length); | |
| 316 | |
| 317 if (length < rtp_begin + kAttrHeaderLength) { | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 // Getting attribute type and length. | |
| 322 attr_type = rtc::GetBE16(&packet[rtp_begin]); | |
| 323 attr_length = rtc::GetBE16( | |
| 324 &packet[rtp_begin + sizeof(attr_type)]); | |
| 325 | |
| 326 rtp_begin += kAttrHeaderLength; // Skip STUN_DATA_ATTR header. | |
| 327 | |
| 328 // Checking for bogus attribute length. | |
| 329 if (length < rtp_begin + attr_length) { | |
| 330 return false; | |
| 331 } | |
| 332 | |
| 333 if (attr_type != cricket::STUN_ATTR_DATA) { | |
| 334 rtp_begin += attr_length; | |
| 335 if ((attr_length % 4) != 0) { | |
| 336 rtp_begin += (4 - (attr_length % 4)); | |
| 337 } | |
| 338 continue; | |
| 339 } | |
| 340 | |
| 341 data_attr_present = true; | |
| 342 rtp_length = attr_length; | |
| 343 | |
| 344 // We found STUN_DATA_ATTR. We can skip parsing rest of the packet. | |
| 345 break; | |
| 346 } | |
| 347 | |
| 348 if (!data_attr_present) { | |
| 349 // There is no data attribute present in the message. We can't do anything | |
| 350 // with the message. | |
| 351 return false; | |
| 352 } | |
| 353 | |
| 354 } else { | |
| 355 // This is a raw RTP packet. | |
| 356 rtp_begin = 0; | |
| 357 rtp_length = length; | |
| 358 } | |
| 359 | |
| 360 // Making sure we have a valid RTP packet at the end. | |
| 361 if (IsRtpPacket(packet + rtp_begin, rtp_length) && | |
| 362 ValidateRtpHeader(packet + rtp_begin, rtp_length, NULL)) { | |
| 363 *rtp_start_pos = rtp_begin; | |
| 364 *rtp_packet_length = rtp_length; | |
| 365 return true; | |
| 366 } | |
| 367 return false; | |
| 368 } | |
| 369 | |
| 370 // ValidateRtpHeader must be called before this method to make sure, we have | |
| 371 // a sane rtp packet. | |
| 372 bool UpdateRtpAbsSendTimeExtension(char* rtp, | |
| 373 size_t length, | |
| 374 int extension_id, | |
| 375 uint32_t abs_send_time) { | |
| 376 // 0 1 2 3 | |
| 377 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
| 378 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 379 // |V=2|P|X| CC |M| PT | sequence number | | |
| 380 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 381 // | timestamp | | |
| 382 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 383 // | synchronization source (SSRC) identifier | | |
| 384 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ | |
| 385 // | contributing source (CSRC) identifiers | | |
| 386 // | .... | | |
| 387 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 388 | |
| 389 // Return if extension bit is not set. | |
| 390 if (!(rtp[0] & 0x10)) { | |
| 391 return true; | |
| 392 } | |
| 393 | |
| 394 size_t cc_count = rtp[0] & 0x0F; | |
| 395 size_t header_length_without_extension = kMinRtpHeaderLength + 4 * cc_count; | |
| 396 | |
| 397 rtp += header_length_without_extension; | |
| 398 | |
| 399 // Getting extension profile ID and length. | |
| 400 uint16_t profile_id = rtc::GetBE16(rtp); | |
| 401 // Length is in 32 bit words. | |
| 402 uint16_t extension_length_in_32bits = rtc::GetBE16(rtp + 2); | |
| 403 size_t extension_length = extension_length_in_32bits * 4; | |
| 404 | |
| 405 rtp += kRtpExtensionHeaderLength; // Moving past extension header. | |
| 406 | |
| 407 bool found = false; | |
| 408 // WebRTC is using one byte header extension. | |
| 409 // TODO(mallinath) - Handle two byte header extension. | |
| 410 if (profile_id == 0xBEDE) { // OneByte extension header | |
| 411 // 0 | |
| 412 // 0 1 2 3 4 5 6 7 | |
| 413 // +-+-+-+-+-+-+-+-+ | |
| 414 // | ID |length | | |
| 415 // +-+-+-+-+-+-+-+-+ | |
| 416 | |
| 417 // 0 1 2 3 | |
| 418 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
| 419 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 420 // | 0xBE | 0xDE | length=3 | | |
| 421 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 422 // | ID | L=0 | data | ID | L=1 | data... | |
| 423 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 424 // ...data | 0 (pad) | 0 (pad) | ID | L=3 | | |
| 425 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 426 // | data | | |
| 427 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
| 428 const char* extension_start = rtp; | |
| 429 const char* extension_end = extension_start + extension_length; | |
| 430 | |
| 431 while (rtp < extension_end) { | |
| 432 const int id = (*rtp & 0xF0) >> 4; | |
| 433 const size_t length = (*rtp & 0x0F) + 1; | |
| 434 if (rtp + kOneByteHeaderLength + length > extension_end) { | |
| 435 return false; | |
| 436 } | |
| 437 // The 4-bit length is the number minus one of data bytes of this header | |
| 438 // extension element following the one-byte header. | |
| 439 if (id == extension_id) { | |
| 440 UpdateAbsSendTimeExtensionValue( | |
| 441 rtp + kOneByteHeaderLength, length, abs_send_time); | |
| 442 found = true; | |
| 443 break; | |
| 444 } | |
| 445 rtp += kOneByteHeaderLength + length; | |
| 446 // Counting padding bytes. | |
| 447 while ((rtp < extension_end) && (*rtp == 0)) { | |
| 448 ++rtp; | |
| 449 } | |
| 450 } | |
| 451 } | |
| 452 return found; | |
| 453 } | |
| 454 | |
| 455 } // packet_processing_helpers | |
| 456 | |
| 457 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, | 41 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender, |
| 458 int socket_id, | 42 int socket_id, |
| 459 ProtocolType protocol_type) | 43 ProtocolType protocol_type) |
| 460 : message_sender_(message_sender), | 44 : message_sender_(message_sender), |
| 461 id_(socket_id), | 45 id_(socket_id), |
| 462 state_(STATE_UNINITIALIZED), | 46 state_(STATE_UNINITIALIZED), |
| 463 dump_incoming_rtp_packet_(false), | 47 dump_incoming_rtp_packet_(false), |
| 464 dump_outgoing_rtp_packet_(false), | 48 dump_outgoing_rtp_packet_(false), |
| 465 protocol_type_(protocol_type), | 49 protocol_type_(protocol_type), |
| 466 send_packets_delayed_total_(0), | 50 send_packets_delayed_total_(0), |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 | 199 |
| 616 void P2PSocketHost::DumpRtpPacket(const char* packet, | 200 void P2PSocketHost::DumpRtpPacket(const char* packet, |
| 617 size_t length, | 201 size_t length, |
| 618 bool incoming) { | 202 bool incoming) { |
| 619 if (IsDtlsPacket(packet, length) || IsRtcpPacket(packet, length)) { | 203 if (IsDtlsPacket(packet, length) || IsRtcpPacket(packet, length)) { |
| 620 return; | 204 return; |
| 621 } | 205 } |
| 622 | 206 |
| 623 size_t rtp_packet_pos = 0; | 207 size_t rtp_packet_pos = 0; |
| 624 size_t rtp_packet_length = length; | 208 size_t rtp_packet_length = length; |
| 625 if (!packet_processing_helpers::GetRtpPacketStartPositionAndLength( | 209 if (!cricket::UnwrapTurnPacket(reinterpret_cast<const uint8_t*>(packet), |
| 626 packet, length, &rtp_packet_pos, &rtp_packet_length)) { | 210 length, &rtp_packet_pos, &rtp_packet_length)) { |
| 627 return; | 211 return; |
| 628 } | 212 } |
| 629 | 213 |
| 630 packet += rtp_packet_pos; | 214 packet += rtp_packet_pos; |
| 631 | 215 |
| 632 size_t header_length = 0; | 216 size_t header_length = 0; |
| 633 bool valid = ValidateRtpHeader(packet, rtp_packet_length, &header_length); | 217 bool valid = |
| 218 cricket::ValidateRtpHeader(reinterpret_cast<const uint8_t*>(packet), |
| 219 rtp_packet_length, &header_length); |
| 634 if (!valid) { | 220 if (!valid) { |
| 635 DCHECK(false); | 221 NOTREACHED(); |
| 636 return; | 222 return; |
| 637 } | 223 } |
| 638 | 224 |
| 639 scoped_ptr<uint8_t[]> header_buffer(new uint8_t[header_length]); | 225 scoped_ptr<uint8_t[]> header_buffer(new uint8_t[header_length]); |
| 640 memcpy(header_buffer.get(), packet, header_length); | 226 memcpy(header_buffer.get(), packet, header_length); |
| 641 | 227 |
| 642 // Posts to the IO thread as the data members should be accessed on the IO | 228 // Posts to the IO thread as the data members should be accessed on the IO |
| 643 // thread only. | 229 // thread only. |
| 644 BrowserThread::PostTask( | 230 BrowserThread::PostTask( |
| 645 BrowserThread::IO, FROM_HERE, | 231 BrowserThread::IO, FROM_HERE, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 send_bytes_delayed_max_ = send_bytes_delayed_cur_; | 267 send_bytes_delayed_max_ = send_bytes_delayed_cur_; |
| 682 } | 268 } |
| 683 } | 269 } |
| 684 | 270 |
| 685 void P2PSocketHost::DecrementDelayedBytes(uint32_t size) { | 271 void P2PSocketHost::DecrementDelayedBytes(uint32_t size) { |
| 686 send_bytes_delayed_cur_ -= size; | 272 send_bytes_delayed_cur_ -= size; |
| 687 DCHECK_GE(send_bytes_delayed_cur_, 0); | 273 DCHECK_GE(send_bytes_delayed_cur_, 0); |
| 688 } | 274 } |
| 689 | 275 |
| 690 } // namespace content | 276 } // namespace content |
| OLD | NEW |