Sergey Ulanov | dc305db | 2016-01-14 17:14:54 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2016 Google Inc. |
| 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/base/turnutils.h" |
| 29 | |
| 30 | #include "webrtc/base/byteorder.h" |
| 31 | #include "webrtc/base/checks.h" |
| 32 | #include "webrtc/p2p/base/stun.h" |
| 33 | |
| 34 | namespace cricket { |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | const size_t kTurnChannelHeaderLength = 4; |
| 39 | |
| 40 | bool IsTurnChannelData(const uint8_t* data, size_t length) { |
| 41 | return length >= kTurnChannelHeaderLength && ((*data & 0xC0) == 0x40); |
| 42 | } |
| 43 | |
| 44 | bool IsTurnSendIndicationPacket(const uint8_t* data, size_t length) { |
| 45 | if (length < kStunHeaderSize) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | uint16_t type = rtc::GetBE16(data); |
| 50 | return (type == TURN_SEND_INDICATION); |
| 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
| 55 | bool UnwrapTurnPacket(const uint8_t* packet, |
| 56 | size_t packet_size, |
| 57 | size_t* content_position, |
| 58 | size_t* content_size) { |
| 59 | if (IsTurnChannelData(packet, packet_size)) { |
| 60 | // Turn Channel Message header format. |
| 61 | // 0 1 2 3 |
| 62 | // 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 |
| 63 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 64 | // | Channel Number | Length | |
| 65 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 66 | // | | |
| 67 | // / Application Data / |
| 68 | // / / |
| 69 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 70 | size_t length = rtc::GetBE16(&packet[2]); |
| 71 | if (length + kTurnChannelHeaderLength > packet_size) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | *content_position = kTurnChannelHeaderLength; |
| 76 | *content_size = length; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | if (IsTurnSendIndicationPacket(packet, packet_size)) { |
| 81 | // Validate STUN message length. |
| 82 | const size_t stun_message_length = rtc::GetBE16(&packet[2]); |
| 83 | if (stun_message_length + kStunHeaderSize != packet_size) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // First skip mandatory stun header which is of 20 bytes. |
| 88 | size_t pos = kStunHeaderSize; |
| 89 | // Loop through STUN attributes until we find STUN DATA attribute. |
| 90 | while (pos < packet_size) { |
| 91 | // Keep reading STUN attributes until we hit DATA attribute. |
| 92 | // Attribute will be a TLV structure. |
| 93 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 94 | // | Type | Length | |
| 95 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 96 | // | Value (variable) .... |
| 97 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 98 | // The value in the length field MUST contain the length of the Value |
| 99 | // part of the attribute, prior to padding, measured in bytes. Since |
| 100 | // STUN aligns attributes on 32-bit boundaries, attributes whose content |
| 101 | // is not a multiple of 4 bytes are padded with 1, 2, or 3 bytes of |
| 102 | // padding so that its value contains a multiple of 4 bytes. The |
| 103 | // padding bits are ignored, and may be any value. |
| 104 | uint16_t attr_type, attr_length; |
| 105 | const int kAttrHeaderLength = sizeof(attr_type) + sizeof(attr_length); |
| 106 | |
| 107 | if (packet_size < pos + kAttrHeaderLength) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // Getting attribute type and length. |
| 112 | attr_type = rtc::GetBE16(&packet[pos]); |
| 113 | attr_length = rtc::GetBE16(&packet[pos + sizeof(attr_type)]); |
| 114 | |
| 115 | pos += kAttrHeaderLength; // Skip STUN_DATA_ATTR header. |
| 116 | |
| 117 | // Checking for bogus attribute length. |
| 118 | if (pos + attr_length > packet_size) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (attr_type == STUN_ATTR_DATA) { |
| 123 | *content_position = pos; |
| 124 | *content_size = attr_length; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | pos += attr_length; |
| 129 | if ((attr_length % 4) != 0) { |
| 130 | pos += (4 - (attr_length % 4)); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // There is no data attribute present in the message. |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // This is not a TURN packet. |
| 139 | *content_position = 0; |
| 140 | *content_size = packet_size; |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | } // namespace cricket |