forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitMSSQLCommandMessage.java
More file actions
236 lines (193 loc) · 8.78 KB
/
Copy pathInitMSSQLCommandMessage.java
File metadata and controls
236 lines (193 loc) · 8.78 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.mssqlclient.impl.codec;
import io.netty.buffer.ByteBuf;
import io.vertx.core.net.HostAndPort;
import io.vertx.mssqlclient.MSSQLConnectOptions;
import io.vertx.mssqlclient.impl.MSSQLSocketConnection;
import io.vertx.mssqlclient.impl.protocol.client.login.LoginPacket;
import io.vertx.mssqlclient.impl.utils.Utils;
import io.vertx.sqlclient.spi.connection.Connection;
import io.vertx.sqlclient.spi.protocol.InitCommand;
import java.util.Map;
import static io.vertx.mssqlclient.impl.codec.MessageType.TDS7_LOGIN;
import static io.vertx.mssqlclient.impl.utils.ByteBufUtils.readUnsignedShortLengthString;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import static java.util.Locale.ENGLISH;
class InitMSSQLCommandMessage extends MSSQLCommandMessage<Connection, InitCommand> {
static final Object LOGIN_SENT = new Object();
InitMSSQLCommandMessage(InitCommand cmd) {
super(cmd);
}
@Override
void encode() {
ByteBuf content = tdsMessageCodec.alloc().ioBuffer();
int startIdx = content.writerIndex(); // Length
content.writeZero(4); // set length later by calculating
content.writeInt(LoginPacket.SQL_SERVER_2017_VERSION); // TDSVersion
content.writeIntLE(tdsMessageCodec.encoder().packetSize()); // PacketSize
content.writeZero(12); // ClientProgVer + ClientPID + ConnectionID
content.writeByte(LoginPacket.DEFAULT_OPTION_FLAGS1 |
LoginPacket.OPTION_FLAGS1_ORDER_X86 |
LoginPacket.OPTION_FLAGS1_CHARSET_ASCII |
LoginPacket.OPTION_FLAGS1_FLOAT_IEEE_754 |
LoginPacket.OPTION_FLAGS1_USE_DB_OFF |
LoginPacket.OPTION_FLAGS1_INIT_DB_FATAL |
LoginPacket.OPTION_FLAGS1_SET_LANG_ON
); // OptionFlags1
content.writeByte(LoginPacket.DEFAULT_OPTION_FLAGS2 |
LoginPacket.OPTION_FLAGS2_ODBC_ON
); // OptionFlags2
content.writeByte(LoginPacket.DEFAULT_TYPE_FLAGS); // TypeFlags
content.writeByte(LoginPacket.DEFAULT_OPTION_FLAGS3 | LoginPacket.OPTION_FLAGS3_EXTENSION); // OptionFlags3
content.writeZero(8); // ClientTimeZone + ClientLCID
/*
OffsetLength part:
we set offset by calculating ByteBuf writer indexes diff.
*/
Map<String, String> properties = cmd.properties();
// HostName
String hostName = Utils.getHostName();
int hostNameOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(hostName.length());
// UserName
String userName = cmd.username();
int userNameOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(userName.length());
// Password
String password = cmd.password();
int passwordOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(password.length());
// AppName
CharSequence appName = properties.get("appName");
if (appName == null || appName.length() == 0) {
appName = MSSQLConnectOptions.DEFAULT_APP_NAME;
}
int appNameOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(appName.length());
// ServerName
String serverName = ((MSSQLSocketConnection)cmd.connection()).socket().remoteAddress().host();
int serverNameOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(serverName.length());
// Extension
int extensionOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(4);
// CltIntName
CharSequence interfaceLibraryName = properties.get("clientInterfaceName");
if (interfaceLibraryName == null || interfaceLibraryName.length() == 0) {
interfaceLibraryName = MSSQLConnectOptions.DEFAULT_CLIENT_INTERFACE_NAME;
}
int cltIntNameOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(interfaceLibraryName.length());
// Language
int languageOffsetLengthIdx = content.writerIndex();
content.writeZero(4);
// Database
String database = cmd.database();
int databaseOffsetLengthIdx = content.writerIndex();
content.writeZero(2); // offset
content.writeShortLE(database.length());
// ClientID
// 6 BYTE
content.writeZero(6);
// SSPI
int sspiOffsetLengthIdx = content.writerIndex();
content.writeZero(4);
// AtchDBFile
int atchDbFileOffsetLengthIdx = content.writerIndex();
content.writeZero(4);
// ChangePassword
int changePasswordOffsetLengthIdx = content.writerIndex();
content.writeZero(4);
// SSPILong
content.writeZero(4);
/*
Data part: note we should set offset by calculation before writing data
*/
content.setShortLE(hostNameOffsetLengthIdx, content.writerIndex() - startIdx);
content.writeCharSequence(hostName, UTF_16LE);
content.setShortLE(userNameOffsetLengthIdx, content.writerIndex() - startIdx);
content.writeCharSequence(userName, UTF_16LE);
content.setShortLE(passwordOffsetLengthIdx, content.writerIndex() - startIdx);
writePassword(content, password);
content.setShortLE(appNameOffsetLengthIdx, content.writerIndex() - startIdx);
content.writeCharSequence(appName, UTF_16LE);
content.setShortLE(serverNameOffsetLengthIdx, content.writerIndex() - startIdx);
content.writeCharSequence(serverName, UTF_16LE);
content.setShortLE(extensionOffsetLengthIdx, content.writerIndex() - startIdx);
int featureExtOffsetIdx = content.writerIndex();
content.writeIntLE(0);
content.setShortLE(cltIntNameOffsetLengthIdx, content.writerIndex() - startIdx);
content.writeCharSequence(interfaceLibraryName, UTF_16LE);
content.setShortLE(languageOffsetLengthIdx, content.writerIndex() - startIdx);
content.setShortLE(databaseOffsetLengthIdx, content.writerIndex() - startIdx);
content.writeCharSequence(database, UTF_16LE);
content.setShortLE(sspiOffsetLengthIdx, content.writerIndex() - startIdx);
content.setShortLE(atchDbFileOffsetLengthIdx, content.writerIndex() - startIdx);
content.setShortLE(changePasswordOffsetLengthIdx, content.writerIndex() - startIdx);
content.setIntLE(featureExtOffsetIdx, content.writerIndex() - startIdx);
// TDS_FEATURE_EXT_JSONSUPPORT
content.writeByte(0x0D); // feature ID
content.writeIntLE(1); // data length
content.writeByte(0x01); // JSON support version 1
// FEATUREEXT terminator
content.writeByte(0xFF);
// set length
content.setIntLE(startIdx, content.writerIndex() - startIdx);
tdsMessageCodec.encoder().writeTdsMessage(TDS7_LOGIN, content);
tdsMessageCodec.chctx().pipeline().fireUserEventTriggered(LOGIN_SENT);
}
/*
Before submitting a password from the client to the server,
for every byte in the password buffer starting with the position pointed to by ibPassword or ibChangePassword,
the client SHOULD first swap the four high bits with the four low bits and then do a bit-XOR with 0xA5 (10100101).
After reading a submitted password, for every byte in the password buffer starting with the position pointed to by ibPassword or ibChangePassword,
the server SHOULD first do a bit-XOR with 0xA5 (10100101) and then swap the four high bits with the four low bits.
*/
private void writePassword(ByteBuf payload, String password) {
byte[] bytes = password.getBytes(UTF_16LE);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
bytes[i] = (byte) ((b >> 4 | ((b & 0x0F) << 4)) ^ 0xA5);
}
payload.writeBytes(bytes);
}
@Override
protected void handleLoginAck() {
result = cmd.connection();
}
@Override
protected void handleRouting(ByteBuf payload) {
MSSQLSocketConnection conn = (MSSQLSocketConnection) this.result;
if (conn == null) {
throw new IllegalStateException("Routing ENVCHANGE token MUST be sent after the LOGINACK token in the login response");
}
if (payload.readUnsignedShortLE() <= 5) {
throw new IllegalStateException("RoutingDataValueLength too short");
}
if (payload.readUnsignedByte() != 0) {
throw new IllegalStateException("Protocol MUST be 0, specifying TCP-IP protocol");
}
int port;
if ((port = payload.readUnsignedShortLE()) == 0) {
throw new IllegalStateException("ProtocolProperty value of zero is not allowed when Protocol is TCP-IP");
}
String host = readUnsignedShortLengthString(payload);
conn.setAlternateServer(HostAndPort.create(host.toLowerCase(ENGLISH), port));
}
}