1 // Copyright 2014 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 "components/devtools_bridge/socket_tunnel_packet_handler.h"
9 #include "components/devtools_bridge/socket_tunnel_connection.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
13 namespace devtools_bridge
{
15 static const int kControlConnectionId
=
16 SocketTunnelConnection::kControlConnectionId
;
17 static const int kMinConnectionId
= SocketTunnelConnection::kMinConnectionId
;
18 static const int kMaxConnectionId
= SocketTunnelConnection::kMaxConnectionId
;
19 static const int kControlPacketSizeBytes
=
20 SocketTunnelConnection::kControlPacketSizeBytes
;
22 void SocketTunnelPacketHandler::DecodePacket(const void* data
, size_t length
) {
23 const unsigned char* bytes
= static_cast<const unsigned char*>(data
);
25 DLOG(ERROR
) << "Empty packet";
26 HandleProtocolError();
29 int connection_id
= bytes
[0];
30 if (connection_id
!= kControlConnectionId
) {
31 if (connection_id
< kMinConnectionId
||
32 connection_id
> kMaxConnectionId
) {
33 DLOG(ERROR
) << "Invalid connection ID: " << connection_id
;
34 HandleProtocolError();
38 int connection_index
= connection_id
- kMinConnectionId
;
39 scoped_refptr
<net::IOBufferWithSize
> packet(
40 new net::IOBufferWithSize(length
- 1));
41 memcpy(packet
->data(), bytes
+ 1, length
- 1);
42 HandleDataPacket(connection_index
, packet
);
43 } else if (length
>= kControlPacketSizeBytes
) {
44 static_assert(kControlPacketSizeBytes
== 3,
45 "kControlPacketSizeBytes should equal 3");
47 int op_code
= bytes
[1];
48 connection_id
= bytes
[2];
49 if (connection_id
< kMinConnectionId
||
50 connection_id
> kMaxConnectionId
) {
51 DLOG(ERROR
) << "Invalid connection ID: " << connection_id
;
52 HandleProtocolError();
55 int connection_index
= connection_id
- kMinConnectionId
;
56 HandleControlPacket(connection_index
, op_code
);
58 DLOG(ERROR
) << "Invalid packet";
59 HandleProtocolError();
64 } // namespace devtools_bridge