-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SocketTransportHandler.java
219 lines (198 loc) · 7.36 KB
/
SocketTransportHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.bolt.transport;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.function.Function;
import org.neo4j.collection.primitive.PrimitiveLongObjectMap;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import static io.netty.buffer.Unpooled.wrappedBuffer;
/**
* Handles incoming chunks of data for a given client channel. This initially will negotiate a protocol version to use,
* and then delegate future messages to the chosen protocol.
* <p/>
* This class is stateful, one instance is expected per channel.
*/
public class SocketTransportHandler extends ChannelInboundHandlerAdapter
{
private final ProtocolChooser protocolChooser;
private final Log log;
private BoltProtocol protocol;
public SocketTransportHandler( ProtocolChooser protocolChooser, LogProvider logging )
{
this.protocolChooser = protocolChooser;
this.log = logging.getLog( getClass() );
}
@Override
public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception
{
if ( msg instanceof ByteBuf )
{
ByteBuf buffer = (ByteBuf) msg;
if ( protocol == null )
{
chooseProtocolVersion( ctx, buffer );
}
else
{
protocol.handle( ctx, buffer );
}
}
else
{
ctx.fireChannelRead( msg );
}
}
@Override
public void channelInactive( ChannelHandlerContext ctx ) throws Exception
{
close();
}
@Override
public void handlerRemoved( ChannelHandlerContext ctx ) throws Exception
{
close();
}
@Override
public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) throws Exception
{
log.error( "Fatal error occurred when handling a client connection: " + cause.getMessage(), cause );
close();
}
private void close()
{
if(protocol != null)
{
protocol.close();
protocol = null;
}
}
private void chooseProtocolVersion( ChannelHandlerContext ctx, ByteBuf buffer ) throws Exception
{
switch ( protocolChooser.handleVersionHandshakeChunk( buffer, ctx.channel() ) )
{
case PROTOCOL_CHOSEN:
protocol = protocolChooser.chosenProtocol();
ctx.writeAndFlush( ctx.alloc().buffer( 4 ).writeInt( protocol.version() ) );
// If there is more data pending, the client optimistically sent this in its initial payload. It really
// shouldn't be doing that since it can't know which versions we support, but here we are anyway.
// Emulate a second call to channelRead, the remaining data in the buffer will be forwarded to the newly
// selected protocol.
if ( buffer.readableBytes() > 0 )
{
channelRead( ctx, buffer );
}
else
{
buffer.release();
}
return;
case NO_APPLICABLE_PROTOCOL:
buffer.release();
ctx.writeAndFlush( wrappedBuffer( new byte[]{0, 0, 0, 0} ) )
.sync()
.channel()
.close();
return;
case INVALID_HANDSHAKE:
buffer.release();
ctx.close();
return;
case PARTIAL_HANDSHAKE:
}
}
public enum HandshakeOutcome
{
/** Yay! */
PROTOCOL_CHOSEN,
/** Pending more bytes before handshake can complete */
PARTIAL_HANDSHAKE,
/** the client sent an invalid handshake */
INVALID_HANDSHAKE,
/** None of the clients suggested protocol versions are available :( */
NO_APPLICABLE_PROTOCOL
}
/**
* Manages the state for choosing the protocol version to use.
* The protocol opens with the client sending four bytes (0x6060 B017) followed by four suggested protocol
* versions in preference order. All bytes are expected to be big endian, and each of the suggested protocols are
* 4-byte unsigned integers. Since that message could get split up along the way, we first gather the
* 20 bytes of data we need, and then choose a protocol to use.
*/
public static class ProtocolChooser
{
private final PrimitiveLongObjectMap<Function<Channel,BoltProtocol>> availableVersions;
private final ByteBuffer handShake = ByteBuffer.allocateDirect( 5 * 4 ).order( ByteOrder.BIG_ENDIAN );
private static final int MAGIC_PREAMBLE = 0x6060B017;
private BoltProtocol protocol;
/**
* @param availableVersions version -> protocol mapping
*/
public ProtocolChooser( PrimitiveLongObjectMap<Function<Channel,BoltProtocol>> availableVersions )
{
this.availableVersions = availableVersions;
}
public HandshakeOutcome handleVersionHandshakeChunk( ByteBuf buffer, Channel ch )
{
if ( handShake.remaining() > buffer.readableBytes() )
{
handShake.limit( handShake.position() + buffer.readableBytes() );
buffer.readBytes( handShake );
handShake.limit( handShake.capacity() );
}
else
{
buffer.readBytes( handShake );
}
if ( handShake.remaining() == 0 )
{
handShake.flip();
//Check so that handshake starts with 0x606 0B017
if ( handShake.getInt() != MAGIC_PREAMBLE )
{
return HandshakeOutcome.INVALID_HANDSHAKE;
}
else {
for ( int i = 0; i < 4; i++ )
{
long suggestion = handShake.getInt() & 0xFFFFFFFFL;
if ( availableVersions.containsKey( suggestion ) )
{
protocol = availableVersions.get( suggestion ).apply( ch );
return HandshakeOutcome.PROTOCOL_CHOSEN;
}
}
}
// None of the suggested protocol versions are available.
return HandshakeOutcome.NO_APPLICABLE_PROTOCOL;
}
return HandshakeOutcome.PARTIAL_HANDSHAKE;
}
public BoltProtocol chosenProtocol()
{
return protocol;
}
}
}