mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +01:00

We now have two downstream users of stun.h, so it appears to be generally usable. I put this in a new dir networking/, but I'm open to suggestions here (maybe some things in api/ should move in there). I checked what our downstream users are actually using, and it's cricket::ComputeStunCredentialHash cricket::<constants> cricket::TurnMessage cricket::GetStunErrorResponseType cricket::StunAttribute::CreateAddress cricket::StunErrorCodeAttribute cricket::StunByteStringAttribute StunAttribute::CreateUnknownAttributes cricket::TurnErrorType cricket::StunMessage I reckoned that was pretty much everything in stun.h, so I didn't bother splitting it up. They don't use every function and constant in there, but all _types_ of functions and constants, so for the sake of coherence I don't think it makes sense to split it. There's some old stuff in there like GTURN which could arguably be split out, but it should likely go away soon anyway, so I don't think it's worth the effort. Steps: 1) land this 2) update downstream to point to the new header and target 3) remove p2p/base:stun_types. Bug: webrtc:11091 Change-Id: I1f05bf06055475d25601197ec6fefb8d3b55e8e3 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/159923 Commit-Queue: Patrik Höglund <phoglund@webrtc.org> Reviewed-by: Niels Moller <nisse@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29822}
126 lines
4.4 KiB
C++
126 lines
4.4 KiB
C++
/*
|
|
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "media/base/turn_utils.h"
|
|
|
|
#include "api/transport/stun.h"
|
|
#include "rtc_base/byte_order.h"
|
|
|
|
namespace cricket {
|
|
|
|
namespace {
|
|
|
|
const size_t kTurnChannelHeaderLength = 4;
|
|
|
|
bool IsTurnChannelData(const uint8_t* data, size_t length) {
|
|
return length >= kTurnChannelHeaderLength && ((*data & 0xC0) == 0x40);
|
|
}
|
|
|
|
bool IsTurnSendIndicationPacket(const uint8_t* data, size_t length) {
|
|
if (length < kStunHeaderSize) {
|
|
return false;
|
|
}
|
|
|
|
uint16_t type = rtc::GetBE16(data);
|
|
return (type == TURN_SEND_INDICATION);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool UnwrapTurnPacket(const uint8_t* packet,
|
|
size_t packet_size,
|
|
size_t* content_position,
|
|
size_t* content_size) {
|
|
if (IsTurnChannelData(packet, packet_size)) {
|
|
// Turn Channel Message header format.
|
|
// 0 1 2 3
|
|
// 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
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
// | Channel Number | Length |
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
// | |
|
|
// / Application Data /
|
|
// / /
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
size_t length = rtc::GetBE16(&packet[2]);
|
|
if (length + kTurnChannelHeaderLength > packet_size) {
|
|
return false;
|
|
}
|
|
|
|
*content_position = kTurnChannelHeaderLength;
|
|
*content_size = length;
|
|
return true;
|
|
}
|
|
|
|
if (IsTurnSendIndicationPacket(packet, packet_size)) {
|
|
// Validate STUN message length.
|
|
const size_t stun_message_length = rtc::GetBE16(&packet[2]);
|
|
if (stun_message_length + kStunHeaderSize != packet_size) {
|
|
return false;
|
|
}
|
|
|
|
// First skip mandatory stun header which is of 20 bytes.
|
|
size_t pos = kStunHeaderSize;
|
|
// Loop through STUN attributes until we find STUN DATA attribute.
|
|
while (pos < packet_size) {
|
|
// Keep reading STUN attributes until we hit DATA attribute.
|
|
// Attribute will be a TLV structure.
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
// | Type | Length |
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
// | Value (variable) ....
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
// The value in the length field MUST contain the length of the Value
|
|
// part of the attribute, prior to padding, measured in bytes. Since
|
|
// STUN aligns attributes on 32-bit boundaries, attributes whose content
|
|
// is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
|
|
// padding so that its value contains a multiple of 4 bytes. The
|
|
// padding bits are ignored, and may be any value.
|
|
uint16_t attr_type, attr_length;
|
|
const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length);
|
|
|
|
if (packet_size < pos + kAttrHeaderLength) {
|
|
return false;
|
|
}
|
|
|
|
// Getting attribute type and length.
|
|
attr_type = rtc::GetBE16(&packet[pos]);
|
|
attr_length = rtc::GetBE16(&packet[pos + sizeof(attr_type)]);
|
|
|
|
pos += kAttrHeaderLength; // Skip STUN_DATA_ATTR header.
|
|
|
|
// Checking for bogus attribute length.
|
|
if (pos + attr_length > packet_size) {
|
|
return false;
|
|
}
|
|
|
|
if (attr_type == STUN_ATTR_DATA) {
|
|
*content_position = pos;
|
|
*content_size = attr_length;
|
|
return true;
|
|
}
|
|
|
|
pos += attr_length;
|
|
if ((attr_length % 4) != 0) {
|
|
pos += (4 - (attr_length % 4));
|
|
}
|
|
}
|
|
|
|
// There is no data attribute present in the message.
|
|
return false;
|
|
}
|
|
|
|
// This is not a TURN packet.
|
|
*content_position = 0;
|
|
*content_size = packet_size;
|
|
return true;
|
|
}
|
|
|
|
} // namespace cricket
|