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_udp.h" | 5 #include "content/browser/renderer_host/p2p_socket_host_udp.h" |
6 | 6 |
7 #include "content/browser/renderer_host/p2p_sockets_host.h" | |
8 #include "content/common/p2p_messages.h" | 7 #include "content/common/p2p_messages.h" |
9 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
11 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
12 | 11 |
13 namespace { | 12 namespace { |
14 | 13 |
14 // UDP packets cannot be bigger than 64k. | |
15 const int kReadBufferSize = 65536; | 15 const int kReadBufferSize = 65536; |
16 | 16 |
17 } // namespace | 17 } // namespace |
18 | 18 |
19 P2PSocketHostUdp::P2PSocketHostUdp(P2PSocketsHost* host, int routing_id, int id) | 19 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Message::Sender* message_sender, |
20 : P2PSocketHost(host, routing_id, id), | 20 int routing_id, int id) |
Alpha Left Google
2011/04/09 00:44:32
nit: indentation.
Sergey Ulanov
2011/04/09 00:49:10
err, isn't it correct?
| |
21 : P2PSocketHost(message_sender, routing_id, id), | |
21 state_(STATE_UNINITIALIZED), | 22 state_(STATE_UNINITIALIZED), |
23 socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), | |
22 send_pending_(false), | 24 send_pending_(false), |
23 ALLOW_THIS_IN_INITIALIZER_LIST( | 25 ALLOW_THIS_IN_INITIALIZER_LIST( |
24 recv_callback_(this, &P2PSocketHostUdp::OnRecv)), | 26 recv_callback_(this, &P2PSocketHostUdp::OnRecv)), |
25 ALLOW_THIS_IN_INITIALIZER_LIST( | 27 ALLOW_THIS_IN_INITIALIZER_LIST( |
26 send_callback_(this, &P2PSocketHostUdp::OnSend)) { | 28 send_callback_(this, &P2PSocketHostUdp::OnSend)) { |
27 } | 29 } |
28 | 30 |
29 P2PSocketHostUdp::~P2PSocketHostUdp() { | 31 P2PSocketHostUdp::~P2PSocketHostUdp() { |
30 if (state_ == STATE_OPEN) { | 32 if (state_ == STATE_OPEN) { |
31 DCHECK(socket_.get()); | 33 DCHECK(socket_.get()); |
32 socket_.reset(); | 34 socket_.reset(); |
33 } | 35 } |
34 } | 36 } |
35 | 37 |
36 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address) { | 38 bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address) { |
37 net::UDPServerSocket* socket = new net::UDPServerSocket( | 39 DCHECK_EQ(state_, STATE_UNINITIALIZED); |
38 NULL, net::NetLog::Source()); | |
39 socket_.reset(socket); | |
40 | 40 |
41 int result = socket_->Listen(local_address); | 41 int result = socket_->Listen(local_address); |
42 if (result < 0) { | 42 if (result < 0) { |
43 LOG(ERROR) << "bind() failed: " << result; | 43 LOG(ERROR) << "bind() failed: " << result; |
44 OnError(); | 44 OnError(); |
45 return false; | 45 return false; |
46 } | 46 } |
47 | 47 |
48 net::IPEndPoint address; | 48 net::IPEndPoint address; |
49 result = socket_->GetLocalAddress(&address); | 49 result = socket_->GetLocalAddress(&address); |
50 if (result < 0) { | 50 if (result < 0) { |
51 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " | 51 LOG(ERROR) << "P2PSocket::Init(): unable to get local address: " |
52 << result; | 52 << result; |
53 OnError(); | 53 OnError(); |
54 return false; | 54 return false; |
55 } | 55 } |
56 | 56 |
57 VLOG(1) << "Local address: " << address.ToString(); | 57 VLOG(1) << "Local address: " << address.ToString(); |
58 | 58 |
59 state_ = STATE_OPEN; | 59 state_ = STATE_OPEN; |
60 | 60 |
61 recv_buffer_ = new net::IOBuffer(kReadBufferSize); | 61 recv_buffer_ = new net::IOBuffer(kReadBufferSize); |
62 DoRead(); | 62 DoRead(); |
63 | 63 |
64 host_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address)); | 64 message_sender_->Send(new P2PMsg_OnSocketCreated(routing_id_, id_, address)); |
65 | 65 |
66 return true; | 66 return true; |
67 } | 67 } |
68 | 68 |
69 void P2PSocketHostUdp::OnError() { | 69 void P2PSocketHostUdp::OnError() { |
70 socket_.reset(); | 70 socket_.reset(); |
71 | 71 |
72 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) | 72 if (state_ == STATE_UNINITIALIZED || state_ == STATE_OPEN) |
73 host_->Send(new P2PMsg_OnError(routing_id_, id_)); | 73 message_sender_->Send(new P2PMsg_OnError(routing_id_, id_)); |
74 | 74 |
75 state_ = STATE_ERROR; | 75 state_ = STATE_ERROR; |
76 } | 76 } |
77 | 77 |
78 void P2PSocketHostUdp::DoRead() { | 78 void P2PSocketHostUdp::DoRead() { |
79 int result; | 79 int result; |
80 do { | 80 do { |
81 result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_, | 81 result = socket_->RecvFrom(recv_buffer_, kReadBufferSize, &recv_address_, |
82 &recv_callback_); | 82 &recv_callback_); |
83 DidCompleteRead(result); | 83 DidCompleteRead(result); |
84 } while (result > 0); | 84 } while (result > 0); |
85 } | 85 } |
86 | 86 |
87 void P2PSocketHostUdp::OnRecv(int result) { | 87 void P2PSocketHostUdp::OnRecv(int result) { |
88 DidCompleteRead(result); | 88 DidCompleteRead(result); |
89 if (state_ == STATE_OPEN) { | 89 if (state_ == STATE_OPEN) { |
90 DoRead(); | 90 DoRead(); |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 void P2PSocketHostUdp::DidCompleteRead(int result) { | 94 void P2PSocketHostUdp::DidCompleteRead(int result) { |
95 DCHECK_EQ(state_, STATE_OPEN); | 95 DCHECK_EQ(state_, STATE_OPEN); |
96 | 96 |
97 if (result > 0) { | 97 if (result > 0) { |
98 std::vector<char> data(recv_buffer_->data(), recv_buffer_->data() + result); | 98 std::vector<char> data(recv_buffer_->data(), recv_buffer_->data() + result); |
99 host_->Send(new P2PMsg_OnDataReceived(routing_id_, id_, | 99 |
100 recv_address_, data)); | 100 if (authorized_peers_.find(recv_address_) == authorized_peers_.end()) { |
Alpha Left Google
2011/04/09 00:44:32
Authorization based on peer seems to be enough. Ho
| |
101 int type = GetStunPacketType(&*data.begin(), data.size()); | |
102 if (type == STUN_BINDING_REQUEST || type == STUN_BINDING_RESPONSE) { | |
103 authorized_peers_.insert(recv_address_); | |
104 } else if (type != STUN_BINDING_ERROR) { | |
105 LOG(ERROR) << "Received unexpected packet which is not a STUN binding " | |
106 "request or response from " << recv_address_.ToString() | |
107 << " before STUN binding is finished."; | |
108 return; | |
109 } | |
110 } | |
111 | |
112 message_sender_->Send(new P2PMsg_OnDataReceived(routing_id_, id_, | |
113 recv_address_, data)); | |
101 } else if (result < 0 && result != net::ERR_IO_PENDING) { | 114 } else if (result < 0 && result != net::ERR_IO_PENDING) { |
102 LOG(ERROR) << "Error when reading from UDP socket: " << result; | 115 LOG(ERROR) << "Error when reading from UDP socket: " << result; |
103 OnError(); | 116 OnError(); |
104 } | 117 } |
105 } | 118 } |
106 | 119 |
107 void P2PSocketHostUdp::Send(const net::IPEndPoint& socket_address, | 120 void P2PSocketHostUdp::Send(const net::IPEndPoint& to, |
108 const std::vector<char>& data) { | 121 const std::vector<char>& data) { |
109 if (send_pending_) { | 122 if (send_pending_) { |
110 // Silently drop packet if previous send hasn't finished. | 123 // Silently drop packet if previous send hasn't finished. |
111 VLOG(1) << "Dropping UDP packet."; | 124 VLOG(1) << "Dropping UDP packet."; |
112 return; | 125 return; |
113 } | 126 } |
114 | 127 |
128 if (authorized_peers_.find(to) == authorized_peers_.end()) { | |
129 int type = GetStunPacketType(&*data.begin(), data.size()); | |
130 if (type != STUN_BINDING_REQUEST && type != STUN_BINDING_RESPONSE && | |
131 type != STUN_BINDING_ERROR) { | |
132 LOG(ERROR) << "Page tried to send an UDP packet which is not a " | |
133 "STUN message to " << to.ToString() | |
134 << " before STUN binding is finished."; | |
135 OnError(); | |
136 return; | |
137 } | |
138 } | |
139 | |
115 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data.size()); | 140 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(data.size()); |
116 memcpy(buffer->data(), &data.begin()[0], data.size()); | 141 memcpy(buffer->data(), &data.begin()[0], data.size()); |
117 int result = socket_->SendTo(buffer, data.size(), socket_address, | 142 int result = socket_->SendTo(buffer, data.size(), to, &send_callback_); |
118 &send_callback_); | |
119 if (result == net::ERR_IO_PENDING) { | 143 if (result == net::ERR_IO_PENDING) { |
120 send_pending_ = true; | 144 send_pending_ = true; |
121 } else if (result < 0) { | 145 } else if (result < 0) { |
122 LOG(ERROR) << "Error when sending data in UDP socket: " << result; | 146 LOG(ERROR) << "Error when sending data in UDP socket: " << result; |
123 OnError(); | 147 OnError(); |
124 } | 148 } |
125 } | 149 } |
126 | 150 |
127 void P2PSocketHostUdp::OnSend(int result) { | 151 void P2PSocketHostUdp::OnSend(int result) { |
128 DCHECK(send_pending_); | 152 DCHECK(send_pending_); |
129 DCHECK_NE(result, net::ERR_IO_PENDING); | 153 DCHECK_NE(result, net::ERR_IO_PENDING); |
130 | 154 |
131 send_pending_ = false; | 155 send_pending_ = false; |
132 if (result < 0) | 156 if (result < 0) |
133 OnError(); | 157 OnError(); |
134 } | 158 } |
OLD | NEW |