OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "build/build_config.h" |
| 8 |
| 9 #if defined(OS_WIN) |
| 10 #include <winsock2.h> // for htonl |
| 11 #else |
| 12 #include <arpa/inet.h> |
| 13 #endif |
| 14 |
7 #include "content/browser/renderer_host/p2p_socket_host_udp.h" | 15 #include "content/browser/renderer_host/p2p_socket_host_udp.h" |
8 | 16 |
9 P2PSocketHost::P2PSocketHost(P2PSocketsHost* host, int routing_id, int id) | 17 namespace { |
10 : host_(host), routing_id_(routing_id), id_(id) { | 18 const int kStunHeaderSize = 20; |
| 19 const uint32 kStunMagicCookie = 0x2112A442; |
| 20 } // namespace |
| 21 |
| 22 P2PSocketHost::P2PSocketHost(IPC::Message::Sender* message_sender, |
| 23 int routing_id, int id) |
| 24 : message_sender_(message_sender), routing_id_(routing_id), id_(id) { |
11 } | 25 } |
12 | 26 |
13 P2PSocketHost::~P2PSocketHost() { } | 27 P2PSocketHost::~P2PSocketHost() { } |
14 | 28 |
| 29 // Verifies that the packet |data| has a valid STUN header. |
| 30 bool P2PSocketHost::GetStunPacketType( |
| 31 const char* data, int data_size, StunMessageType* type) { |
| 32 |
| 33 if (data_size < kStunHeaderSize) |
| 34 return false; |
| 35 |
| 36 // TODO(sergeyu): Fix libjingle to format STUN message according to |
| 37 // RFC5389 and validate STUN magic cookie here. |
| 38 // |
| 39 // uint32 cookie = ntohl(*reinterpret_cast<const uint32*>(data + 4)); |
| 40 // if (cookie != kStunMagicCookie) |
| 41 // return false; |
| 42 |
| 43 uint16 length = ntohs(*reinterpret_cast<const uint16*>(data + 2)); |
| 44 if (length != data_size - kStunHeaderSize) |
| 45 return false; |
| 46 |
| 47 int message_type = ntohs(*reinterpret_cast<const uint16*>(data)); |
| 48 |
| 49 // Verify that the type is known: |
| 50 switch (message_type) { |
| 51 case STUN_BINDING_REQUEST: |
| 52 case STUN_BINDING_RESPONSE: |
| 53 case STUN_BINDING_ERROR_RESPONSE: |
| 54 case STUN_SHARED_SECRET_REQUEST: |
| 55 case STUN_SHARED_SECRET_RESPONSE: |
| 56 case STUN_SHARED_SECRET_ERROR_RESPONSE: |
| 57 case STUN_ALLOCATE_REQUEST: |
| 58 case STUN_ALLOCATE_RESPONSE: |
| 59 case STUN_ALLOCATE_ERROR_RESPONSE: |
| 60 case STUN_SEND_REQUEST: |
| 61 case STUN_SEND_RESPONSE: |
| 62 case STUN_SEND_ERROR_RESPONSE: |
| 63 case STUN_DATA_INDICATION: |
| 64 *type = static_cast<StunMessageType>(message_type); |
| 65 return true; |
| 66 |
| 67 default: |
| 68 return false; |
| 69 } |
| 70 } |
| 71 |
15 // static | 72 // static |
16 P2PSocketHost* P2PSocketHost::Create( | 73 P2PSocketHost* P2PSocketHost::Create( |
17 P2PSocketsHost* host, int routing_id, int id, P2PSocketType type) { | 74 IPC::Message::Sender* message_sender, int routing_id, int id, |
| 75 P2PSocketType type) { |
18 switch (type) { | 76 switch (type) { |
19 case P2P_SOCKET_UDP: | 77 case P2P_SOCKET_UDP: |
20 return new P2PSocketHostUdp(host, routing_id, id); | 78 return new P2PSocketHostUdp(message_sender, routing_id, id); |
21 | 79 |
22 case P2P_SOCKET_TCP_SERVER: | 80 case P2P_SOCKET_TCP_SERVER: |
23 // TODO(sergeyu): Implement TCP sockets support. | 81 // TODO(sergeyu): Implement TCP sockets support. |
| 82 NOTIMPLEMENTED(); |
24 return NULL; | 83 return NULL; |
25 | 84 |
26 case P2P_SOCKET_TCP_CLIENT: | 85 case P2P_SOCKET_TCP_CLIENT: |
| 86 NOTIMPLEMENTED(); |
27 return NULL; | 87 return NULL; |
28 } | 88 } |
29 | 89 |
30 NOTREACHED(); | 90 NOTREACHED(); |
31 return NULL; | 91 return NULL; |
32 } | 92 } |
OLD | NEW |