blob: f1daec48dd5d3a3fccae007d027321d0912bbff1 [file] [log] [blame]
Sergey Ulanovdc305db2016-01-14 17:14:54 -08001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
Sergey Ulanovdc305db2016-01-14 17:14:54 -08003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
Sergey Ulanovdc305db2016-01-14 17:14:54 -08009 */
10
kjellandera96e2d72016-02-04 23:52:28 -080011#include "webrtc/media/base/turnutils.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080012
Sergey Ulanovdc305db2016-01-14 17:14:54 -080013#include "webrtc/p2p/base/stun.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020014#include "webrtc/rtc_base/byteorder.h"
15#include "webrtc/rtc_base/checks.h"
Sergey Ulanovdc305db2016-01-14 17:14:54 -080016
17namespace cricket {
18
19namespace {
20
21const size_t kTurnChannelHeaderLength = 4;
22
23bool IsTurnChannelData(const uint8_t* data, size_t length) {
24 return length >= kTurnChannelHeaderLength && ((*data & 0xC0) == 0x40);
25}
26
27bool IsTurnSendIndicationPacket(const uint8_t* data, size_t length) {
28 if (length < kStunHeaderSize) {
29 return false;
30 }
31
32 uint16_t type = rtc::GetBE16(data);
33 return (type == TURN_SEND_INDICATION);
34}
35
36} // namespace
37
38bool UnwrapTurnPacket(const uint8_t* packet,
39 size_t packet_size,
40 size_t* content_position,
41 size_t* content_size) {
42 if (IsTurnChannelData(packet, packet_size)) {
43 // Turn Channel Message header format.
44 // 0 1 2 3
45 // 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
46 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 // | Channel Number | Length |
48 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 // | |
50 // / Application Data /
51 // / /
52 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 size_t length = rtc::GetBE16(&packet[2]);
54 if (length + kTurnChannelHeaderLength > packet_size) {
55 return false;
56 }
57
58 *content_position = kTurnChannelHeaderLength;
59 *content_size = length;
60 return true;
61 }
62
63 if (IsTurnSendIndicationPacket(packet, packet_size)) {
64 // Validate STUN message length.
65 const size_t stun_message_length = rtc::GetBE16(&packet[2]);
66 if (stun_message_length + kStunHeaderSize != packet_size) {
67 return false;
68 }
69
70 // First skip mandatory stun header which is of 20 bytes.
71 size_t pos = kStunHeaderSize;
72 // Loop through STUN attributes until we find STUN DATA attribute.
73 while (pos < packet_size) {
74 // Keep reading STUN attributes until we hit DATA attribute.
75 // Attribute will be a TLV structure.
76 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77 // | Type | Length |
78 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 // | Value (variable) ....
80 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 // The value in the length field MUST contain the length of the Value
82 // part of the attribute, prior to padding, measured in bytes. Since
83 // STUN aligns attributes on 32-bit boundaries, attributes whose content
84 // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of
85 // padding so that its value contains a multiple of 4 bytes. The
86 // padding bits are ignored, and may be any value.
87 uint16_t attr_type, attr_length;
88 const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length);
89
90 if (packet_size < pos + kAttrHeaderLength) {
91 return false;
92 }
93
94 // Getting attribute type and length.
95 attr_type = rtc::GetBE16(&packet[pos]);
96 attr_length = rtc::GetBE16(&packet[pos + sizeof(attr_type)]);
97
98 pos += kAttrHeaderLength; // Skip STUN_DATA_ATTR header.
99
100 // Checking for bogus attribute length.
101 if (pos + attr_length > packet_size) {
102 return false;
103 }
104
105 if (attr_type == STUN_ATTR_DATA) {
106 *content_position = pos;
107 *content_size = attr_length;
108 return true;
109 }
110
111 pos += attr_length;
112 if ((attr_length % 4) != 0) {
113 pos += (4 - (attr_length % 4));
114 }
115 }
116
117 // There is no data attribute present in the message.
118 return false;
119 }
120
121 // This is not a TURN packet.
122 *content_position = 0;
123 *content_size = packet_size;
124 return true;
125}
126
127} // namespace cricket