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 uint16 kStunBindingRequest = 0x0001; |
| 20 const uint16 kStunBindingResponse = 0x0102; |
| 21 const uint16 kStunBindingError = 0x0111; |
| 22 const uint32 kStunMagicCookie = 0x2112A442; |
| 23 } // namespace |
| 24 |
| 25 P2PSocketHost::P2PSocketHost(IPC::Message::Sender* message_sender, |
| 26 int routing_id, int id) |
| 27 : message_sender_(message_sender), routing_id_(routing_id), id_(id) { |
11 } | 28 } |
12 | 29 |
13 P2PSocketHost::~P2PSocketHost() { } | 30 P2PSocketHost::~P2PSocketHost() { } |
14 | 31 |
| 32 // Verifies that the packet |data| has a valid STUN header. |
| 33 P2PSocketHost::StunPacketType P2PSocketHost::GetStunPacketType( |
| 34 const char* data, int data_size) { |
| 35 |
| 36 if (data_size < kStunHeaderSize) |
| 37 return STUN_INVALID_PACKET; |
| 38 |
| 39 // TODO(sergeyu): Fix libjingle to format STUN message according to |
| 40 // RFC5389 and validate STUN magic cookie here. |
| 41 // |
| 42 // uint32 cookie = ntohl(*reinterpret_cast<const uint32*>(data + 4)); |
| 43 // if (cookie != kStunMagicCookie) |
| 44 // return STUN_INVALID_PACKET; |
| 45 |
| 46 uint16 type = ntohs(*reinterpret_cast<const uint16*>(data)); |
| 47 |
| 48 uint16 length = ntohs(*reinterpret_cast<const uint16*>(data + 2)); |
| 49 if (length != data_size - kStunHeaderSize) |
| 50 return STUN_INVALID_PACKET; |
| 51 |
| 52 switch (type) { |
| 53 case kStunBindingRequest: |
| 54 return STUN_BINDING_REQUEST; |
| 55 case kStunBindingResponse: |
| 56 return STUN_BINDING_RESPONSE; |
| 57 case kStunBindingError: |
| 58 return STUN_BINDING_ERROR; |
| 59 default: |
| 60 return STUN_UNKNOWN_TYPE; |
| 61 } |
| 62 } |
| 63 |
15 // static | 64 // static |
16 P2PSocketHost* P2PSocketHost::Create( | 65 P2PSocketHost* P2PSocketHost::Create( |
17 P2PSocketsHost* host, int routing_id, int id, P2PSocketType type) { | 66 IPC::Message::Sender* message_sender, int routing_id, int id, |
| 67 P2PSocketType type) { |
18 switch (type) { | 68 switch (type) { |
19 case P2P_SOCKET_UDP: | 69 case P2P_SOCKET_UDP: |
20 return new P2PSocketHostUdp(host, routing_id, id); | 70 return new P2PSocketHostUdp(message_sender, routing_id, id); |
21 | 71 |
22 case P2P_SOCKET_TCP_SERVER: | 72 case P2P_SOCKET_TCP_SERVER: |
23 // TODO(sergeyu): Implement TCP sockets support. | 73 // TODO(sergeyu): Implement TCP sockets support. |
| 74 NOTIMPLEMENTED(); |
24 return NULL; | 75 return NULL; |
25 | 76 |
26 case P2P_SOCKET_TCP_CLIENT: | 77 case P2P_SOCKET_TCP_CLIENT: |
| 78 NOTIMPLEMENTED(); |
27 return NULL; | 79 return NULL; |
28 } | 80 } |
29 | 81 |
30 NOTREACHED(); | 82 NOTREACHED(); |
31 return NULL; | 83 return NULL; |
32 } | 84 } |
OLD | NEW |