IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host.cc
blob4b45e99adad087881aa5f9bff69a1004e727a39f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_host.h"
7 #include "base/sys_byteorder.h"
8 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
9 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
10 #include "content/browser/renderer_host/p2p/socket_host_udp.h"
12 namespace {
13 const uint32 kStunMagicCookie = 0x2112A442;
14 } // namespace
16 namespace content {
18 P2PSocketHost::P2PSocketHost(IPC::Sender* message_sender,
19 int id)
20 : message_sender_(message_sender),
21 id_(id),
22 state_(STATE_UNINITIALIZED) {
25 P2PSocketHost::~P2PSocketHost() { }
27 // Verifies that the packet |data| has a valid STUN header.
28 // static
29 bool P2PSocketHost::GetStunPacketType(
30 const char* data, int data_size, StunMessageType* type) {
32 if (data_size < kStunHeaderSize)
33 return false;
35 uint32 cookie = base::NetToHost32(*reinterpret_cast<const uint32*>(data + 4));
36 if (cookie != kStunMagicCookie)
37 return false;
39 uint16 length = base::NetToHost16(*reinterpret_cast<const uint16*>(data + 2));
40 if (length != data_size - kStunHeaderSize)
41 return false;
43 int message_type = base::NetToHost16(*reinterpret_cast<const uint16*>(data));
45 // Verify that the type is known:
46 switch (message_type) {
47 case STUN_BINDING_REQUEST:
48 case STUN_BINDING_RESPONSE:
49 case STUN_BINDING_ERROR_RESPONSE:
50 case STUN_SHARED_SECRET_REQUEST:
51 case STUN_SHARED_SECRET_RESPONSE:
52 case STUN_SHARED_SECRET_ERROR_RESPONSE:
53 case STUN_ALLOCATE_REQUEST:
54 case STUN_ALLOCATE_RESPONSE:
55 case STUN_ALLOCATE_ERROR_RESPONSE:
56 case STUN_SEND_REQUEST:
57 case STUN_SEND_RESPONSE:
58 case STUN_SEND_ERROR_RESPONSE:
59 case STUN_DATA_INDICATION:
60 *type = static_cast<StunMessageType>(message_type);
61 return true;
63 default:
64 return false;
68 // static
69 bool P2PSocketHost::IsRequestOrResponse(StunMessageType type) {
70 return type == STUN_BINDING_REQUEST || type == STUN_BINDING_RESPONSE ||
71 type == STUN_ALLOCATE_REQUEST || type == STUN_ALLOCATE_RESPONSE;
74 // static
75 P2PSocketHost* P2PSocketHost::Create(
76 IPC::Sender* message_sender, int id, P2PSocketType type,
77 net::URLRequestContextGetter* url_context,
78 P2PMessageThrottler* throttler) {
79 switch (type) {
80 case P2P_SOCKET_UDP:
81 return new P2PSocketHostUdp(message_sender, id, throttler);
82 case P2P_SOCKET_TCP_SERVER:
83 return new P2PSocketHostTcpServer(
84 message_sender, id, P2P_SOCKET_TCP_CLIENT);
86 case P2P_SOCKET_STUN_TCP_SERVER:
87 return new P2PSocketHostTcpServer(
88 message_sender, id, P2P_SOCKET_STUN_TCP_CLIENT);
90 case P2P_SOCKET_TCP_CLIENT:
91 case P2P_SOCKET_SSLTCP_CLIENT:
92 case P2P_SOCKET_TLS_CLIENT:
93 return new P2PSocketHostTcp(message_sender, id, type, url_context);
95 case P2P_SOCKET_STUN_TCP_CLIENT:
96 case P2P_SOCKET_STUN_SSLTCP_CLIENT:
97 case P2P_SOCKET_STUN_TLS_CLIENT:
98 return new P2PSocketHostStunTcp(message_sender, id, type, url_context);
101 NOTREACHED();
102 return NULL;
105 } // namespace content