| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2013 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/base/asyncstuntcpsocket.h" | 11 #include "webrtc/p2p/base/asyncstuntcpsocket.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <string.h> |
| 14 | 14 |
| 15 #include "webrtc/p2p/base/stun.h" | 15 #include "webrtc/p2p/base/stun.h" |
| 16 #include "webrtc/base/common.h" | 16 #include "webrtc/base/common.h" |
| 17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 18 | 18 |
| 19 namespace cricket { | 19 namespace cricket { |
| 20 | 20 |
| 21 static const size_t kMaxPacketSize = 64 * 1024; | 21 static const size_t kMaxPacketSize = 64 * 1024; |
| 22 | 22 |
| 23 typedef uint16 PacketLength; | 23 typedef uint16_t PacketLength; |
| 24 static const size_t kPacketLenSize = sizeof(PacketLength); | 24 static const size_t kPacketLenSize = sizeof(PacketLength); |
| 25 static const size_t kPacketLenOffset = 2; | 25 static const size_t kPacketLenOffset = 2; |
| 26 static const size_t kBufSize = kMaxPacketSize + kStunHeaderSize; | 26 static const size_t kBufSize = kMaxPacketSize + kStunHeaderSize; |
| 27 static const size_t kTurnChannelDataHdrSize = 4; | 27 static const size_t kTurnChannelDataHdrSize = 4; |
| 28 | 28 |
| 29 inline bool IsStunMessage(uint16 msg_type) { | 29 inline bool IsStunMessage(uint16_t msg_type) { |
| 30 // The first two bits of a channel data message are 0b01. | 30 // The first two bits of a channel data message are 0b01. |
| 31 return (msg_type & 0xC000) ? false : true; | 31 return (msg_type & 0xC000) ? false : true; |
| 32 } | 32 } |
| 33 | 33 |
| 34 // AsyncStunTCPSocket | 34 // AsyncStunTCPSocket |
| 35 // Binds and connects |socket| and creates AsyncTCPSocket for | 35 // Binds and connects |socket| and creates AsyncTCPSocket for |
| 36 // it. Takes ownership of |socket|. Returns NULL if bind() or | 36 // it. Takes ownership of |socket|. Returns NULL if bind() or |
| 37 // connect() fail (|socket| is destroyed in that case). | 37 // connect() fail (|socket| is destroyed in that case). |
| 38 AsyncStunTCPSocket* AsyncStunTCPSocket::Create( | 38 AsyncStunTCPSocket* AsyncStunTCPSocket::Create( |
| 39 rtc::AsyncSocket* socket, | 39 rtc::AsyncSocket* socket, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 rtc::AsyncSocket* socket) { | 122 rtc::AsyncSocket* socket) { |
| 123 SignalNewConnection(this, new AsyncStunTCPSocket(socket, false)); | 123 SignalNewConnection(this, new AsyncStunTCPSocket(socket, false)); |
| 124 } | 124 } |
| 125 | 125 |
| 126 size_t AsyncStunTCPSocket::GetExpectedLength(const void* data, size_t len, | 126 size_t AsyncStunTCPSocket::GetExpectedLength(const void* data, size_t len, |
| 127 int* pad_bytes) { | 127 int* pad_bytes) { |
| 128 *pad_bytes = 0; | 128 *pad_bytes = 0; |
| 129 PacketLength pkt_len = | 129 PacketLength pkt_len = |
| 130 rtc::GetBE16(static_cast<const char*>(data) + kPacketLenOffset); | 130 rtc::GetBE16(static_cast<const char*>(data) + kPacketLenOffset); |
| 131 size_t expected_pkt_len; | 131 size_t expected_pkt_len; |
| 132 uint16 msg_type = rtc::GetBE16(data); | 132 uint16_t msg_type = rtc::GetBE16(data); |
| 133 if (IsStunMessage(msg_type)) { | 133 if (IsStunMessage(msg_type)) { |
| 134 // STUN message. | 134 // STUN message. |
| 135 expected_pkt_len = kStunHeaderSize + pkt_len; | 135 expected_pkt_len = kStunHeaderSize + pkt_len; |
| 136 } else { | 136 } else { |
| 137 // TURN ChannelData message. | 137 // TURN ChannelData message. |
| 138 expected_pkt_len = kTurnChannelDataHdrSize + pkt_len; | 138 expected_pkt_len = kTurnChannelDataHdrSize + pkt_len; |
| 139 // From RFC 5766 section 11.5 | 139 // From RFC 5766 section 11.5 |
| 140 // Over TCP and TLS-over-TCP, the ChannelData message MUST be padded to | 140 // Over TCP and TLS-over-TCP, the ChannelData message MUST be padded to |
| 141 // a multiple of four bytes in order to ensure the alignment of | 141 // a multiple of four bytes in order to ensure the alignment of |
| 142 // subsequent messages. The padding is not reflected in the length | 142 // subsequent messages. The padding is not reflected in the length |
| 143 // field of the ChannelData message, so the actual size of a ChannelData | 143 // field of the ChannelData message, so the actual size of a ChannelData |
| 144 // message (including padding) is (4 + Length) rounded up to the nearest | 144 // message (including padding) is (4 + Length) rounded up to the nearest |
| 145 // multiple of 4. Over UDP, the padding is not required but MAY be | 145 // multiple of 4. Over UDP, the padding is not required but MAY be |
| 146 // included. | 146 // included. |
| 147 if (expected_pkt_len % 4) | 147 if (expected_pkt_len % 4) |
| 148 *pad_bytes = 4 - (expected_pkt_len % 4); | 148 *pad_bytes = 4 - (expected_pkt_len % 4); |
| 149 } | 149 } |
| 150 return expected_pkt_len; | 150 return expected_pkt_len; |
| 151 } | 151 } |
| 152 | 152 |
| 153 } // namespace cricket | 153 } // namespace cricket |
| OLD | NEW |