Skip to content

Commit 0070c25

Browse files
author
sidneyfilho
committed
Login Repsonse
1 parent dc06b6a commit 0070c25

File tree

5 files changed

+129
-59
lines changed

5 files changed

+129
-59
lines changed

example.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var mssql = require('./lib/mssql');
22

33
//try a connection
4-
var connectionString = { Server: "127.0.0.1", Port: 1433, Database: "master", Login: "sa", Password: "yyy", Timeout: 15000 };
4+
var connectionString = { Server: "127.0.0.1", Port: 1433, Database: "master", Login: "sa", Password: "", Timeout: 15000 };
55
var conn = new mssql.SqlConnection(connectionString, function() {
66
//this.Open(function() { console.log(this.Version); this.Close(); });
77
});

lib/mssql.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ var util = require('util');
66
var assert = require('assert');
77
var TdsBuilder = require('../lib/tds.js').TdsBuilder
88

9+
10+
/*
11+
** Default Connection String
12+
*/
913
var connectionStringDefault = { Server: '127.0.0.1', Port: '1433', Database: 'master', Login: 'sa', Password: '', Timeout: 10000 };
1014

15+
16+
/*
17+
** SqlConnection
18+
*/
1119
var SqlConnection = function(connString, callback) {
1220
assert.ok(arguments.length !== 0, "Connection String is missing!");
1321

@@ -25,30 +33,33 @@ var SqlConnection = function(connString, callback) {
2533
for (var key in connectionStringDefault)
2634
it.ConnectionString[key] = it.ConnectionString[key] || connectionStringDefault[key];
2735

28-
2936
//
3037
// Create a Connection to get MS SQL Version
31-
//
32-
33-
net.createConnection(it.ConnectionString.Port, it.ConnectionString.Server, function() {
34-
var buffer = TdsBuilder.HandshakeRequest();
35-
var res = it.socket.end(buffer);
36-
}).on("data", function(data) {
37-
it.Version = TdsBuilder.HandshakeResponse(data).Version;
38-
console.log("My Version is " + util.inspect(it.Version));
39-
});
38+
//
39+
it.socket = new net.Socket({ allowHalfOpen: true }).on("connect", function() {
4040

41-
//
42-
// Create other Connection
43-
//
44-
it.socket = net.createConnection(it.ConnectionString.Port, it.ConnectionString.Server, function() {
41+
//var buffer = TdsBuilder.HandshakeRequest();
4542
var buffer = TdsBuilder.LoginRequest(it.ConnectionString.Login, it.ConnectionString.Password, it.ConnectionString.Database);
46-
var res = it.socket.write(buffer);
47-
}).on("data", function(data) {
48-
console.log("Open(callback) End " + data.length);
49-
callback && callback.call(it);
43+
var res = this.write(buffer);
44+
console.log("CLIENT sent LOGIN7 ");
45+
46+
}).once("data", function(data) {
47+
var response = TdsBuilder.LoginResponse(data);
48+
//it.Version = response.Version;
49+
//console.log("CLIENT received PRELOGIN: " + util.inspect(response) + data.length);
50+
51+
//var buffer = TdsBuilder.LoginRequest(it.ConnectionString.Login, it.ConnectionString.Password, it.ConnectionString.Database);
52+
//var res = it.socket.write(buffer);
53+
//console.log("CLIENT sent LOGIN7 ");
54+
55+
//it.socket.on("data", function(data) {
56+
// console.log("CLIENT received LOGIN7 " + data.length);
57+
// callback && callback.call(it);
58+
//});
59+
5060
});
5161

62+
5263
// TIMEOUT
5364
it.socket.setTimeout(it.ConnectionString.Timeout);
5465
it.socket.on("timeout", function() { console.log("timeout"); it.Close(); });
@@ -61,13 +72,15 @@ var SqlConnection = function(connString, callback) {
6172
//this.socket.on("close", function(data) { console.log("close"); });
6273
//this.socket.on("end", function(data) { console.log("end"); });
6374
//this.socket.on("connect", function(data) { console.log("connect"); });
64-
//this.socket.connect(this.ConnectionString.Port, this.ConnectionString.Server);
75+
this.socket.connect(this.ConnectionString.Port, this.ConnectionString.Server);
6576

6677

6778

6879
return it;
6980
}
7081

82+
83+
7184
SqlConnection.prototype.Close = function() {
7285
console.log("Close()");
7386
this.socket.end();

lib/tds.js

+60-6
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ TdsBuilder.HandshakeRequest = function(useMars) {
164164
msg.addData([0xFF]); // Terminator
165165

166166
var buf = msg.toBuffer();
167-
buf[3] = buf.length;
167+
168+
buf.writeUInt8(buf.length, 3, true);
168169

169170
//return new Buffer([0x12, 0x01, 0x00, 0x2F, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1A, 0x00, 0x06, 0x01, 0x00, 0x20,0x00, 0x01, 0x02, 0x00, 0x21, 0x00, 0x01, 0x03, 0x00, 0x22, 0x00, 0x04, 0x04, 0x00, 0x26, 0x00,0x01, 0xFF, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0xB8, 0x0D, 0x00, 0x00, 0x01]);
170171

@@ -198,7 +199,8 @@ TdsBuilder.HandshakeResponse = function(buffer) {
198199
Major: tmp.readUInt8(0, true),
199200
Minor: tmp.readUInt8(1, true),
200201
Revision: tmp.readUInt16(2, true) // 6->0x6, 64->0x40 = 0x640 = 1600
201-
}
202+
},
203+
UseEncrypt: tmp.readUInt16(4, true)
202204
};
203205
}
204206

@@ -225,10 +227,10 @@ TdsBuilder.LoginRequest = function(username, password, database/* , hostname, ap
225227
// 2008 -> 0x03000A73
226228
// 2008 -> 0x03000B73
227229
// SQL Server Denali -> 0x04000074
228-
msg.addData([0x02, 0, 0x09, 0x72]); // 2005 TDS version
230+
msg.addData([0x03, 0, 0x0A, 0x73]); // 2008 TDS version
229231

230232
msg.addData([0, 0x10, 0, 0]); // PacketSize
231-
msg.addData([0, 0, 0, 0x1]); // Client Prog ver
233+
msg.addData([0, 0, 0, 0x7]); // Client Prog ver
232234
msg.addData([0, 0x1, 0, 0]); // Client Process ID
233235
msg.addData([0, 0, 0, 0]); // Connection ID
234236

@@ -288,18 +290,70 @@ TdsBuilder.LoginRequest = function(username, password, database/* , hostname, ap
288290
msg.addData([0x00, 0x00, 0x00, 0x00]);
289291

290292
var buf = msg.toBuffer();
291-
buf[3] = buf.length;
292-
buf[8] = buf[3] - 8;
293+
294+
buf.writeUInt8(buf.length, 3, false);
295+
buf.writeUInt32(buf.length - 8, 8, false);
296+
293297

294298
//return new Buffer([0x10, 0x01, 0x00, 0x90, 0x00, 0x00, 0x01, 0x00, 0x88, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x72, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x03, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x09, 0x04, 0x00, 0x00, 0x5E, 0x00, 0x08, 0x00, 0x6E, 0x00, 0x02, 0x00, 0x72, 0x00, 0x00, 0x00, 0x72, 0x00, 0x07, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04, 0x00, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x50, 0x8B, 0xE2, 0xB7, 0x8F, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00, 0x6B, 0x00, 0x6F, 0x00, 0x73, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x76, 0x00, 0x31, 0x00, 0x73, 0x00, 0x61, 0x00, 0x4F, 0x00, 0x53, 0x00, 0x51, 0x00, 0x4C, 0x00, 0x2D, 0x00, 0x33, 0x00, 0x32, 0x00, 0x4F, 0x00, 0x44, 0x00, 0x42, 0x00, 0x43, 0x00]);
295299

296300
return buf;
297301
}
298302

299303

304+
/*
305+
** Login Response is a token stream, then needs read the token and read your content, read next token and so on
306+
*/
300307
TdsBuilder.LoginResponse = function(buffer) {
301308
assert.ok(arguments.length === 1, "The argument 'buffer' is missing!");
302309
assert.ok(buffer[0] === 0x04, "This buffer is not Server Response or type bit not is 0x04!");
310+
311+
var offset = 8; /* header length*/
312+
var response = {
313+
envchange: [],
314+
info: []
315+
};
316+
317+
while (offset < buffer.length) {
318+
319+
var token = buffer.readUInt8(offset, false); offset += 1;
320+
321+
//
322+
// ENVCHANGE
323+
//
324+
if (token === 0xE3) {
325+
var length = buffer.readUInt16(offset, false); offset += 2;
326+
var type = buffer.readUInt8(offset, false); //offset += 1;
327+
328+
var data = buffer.slice(offset, offset + length); offset += length;
329+
330+
response.envchange.push({ type: type, offset: offset, length: length, data: data });
331+
}
332+
333+
334+
335+
//
336+
// INFO
337+
//
338+
if (token === 0xAB) {
339+
var info = {};
340+
info.length = buffer.readUInt16(offset, false); offset += 2;
341+
info.number = buffer.readUInt32(offset, false); //offset += 4;
342+
info.state = buffer.readUInt8(offset, false); //offset += 1;
343+
info.classType = buffer.readUInt8(offset, false); //offset += 1;
344+
info.msgLength = buffer.readUInt16(offset, false); //offset += 2;
345+
info.msg = buffer.toString('utf8', offset + 4 + 1 + 1 + 2, offset + 4 + 1 + 1 + 2 + info.length).replace(/\u0000/g,''); offset += info.length;
346+
347+
response.info.push(info);
348+
}
349+
350+
351+
//return response;
352+
}
353+
354+
355+
console.log(response);
356+
return response;
303357
}
304358

305359
exports.ExecuteQuery = function ExecuteQuery(query) {

tests/test-tds.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ assert.equal(builder.toBuffer().length, 8 /*header */
7878
builder = new TdsBuilder();
7979
builder.addPacketData([1, 2, 3, 4, 5, 6]);
8080
assert.equal(builder.toBuffer().length, 8 /*header */
81-
+ 2 /*offset byte*/ + 2 /*length byte*/
81+
+ 2 /*offset byte*/ + 2 /*length byte*/
8282
+ 6 /*token data*/);
8383
builder = new TdsBuilder();
8484
builder.addHeaderPacket(0x12);
@@ -87,7 +87,7 @@ builder.addPacketData([9, 9]);
8787
assert.equal(builder.toBuffer().length, 8 /*header */
8888
+ 4 /*token 1*/
8989
+ 4 /*token 2*/
90-
90+
9191
+ 6 /*token 1 data*/
9292
+ 2 /*token 2 data*/);
9393

@@ -145,27 +145,28 @@ http://msdn.microsoft.com/en-us/library/dd358344(v=PROT.13).aspx
145145
*/
146146
var expected = new Buffer([
147147
0x10, 0x01, 0x00, 0x90, 0x00, 0x00, 0x01, 0x00,
148-
0x88, 0x00, 0x00, 0x00, 0x02, 0x00, 0x09, 0x72,
149-
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
148+
0x88, 0x00, 0x00, 0x00, 0x03, 0x00, 0x0A, 0x73,
149+
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
150150
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151-
0xE0, 0x03, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00,
151+
0xE0, 0x03, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00,
152152
0x09, 0x04, 0x00, 0x00, 0x5E, 0x00, 0x08, 0x00,
153-
0x6E, 0x00, 0x02, 0x00, 0x72, 0x00, 0x00, 0x00,
153+
0x6E, 0x00, 0x02, 0x00, 0x72, 0x00, 0x00, 0x00,
154154
0x72, 0x00, 0x07, 0x00, 0x80, 0x00, 0x00, 0x00,
155-
0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04, 0x00,
155+
0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x04, 0x00,
156156
0x88, 0x00, 0x00, 0x00, 0x88, 0x00, 0x00, 0x00,
157-
0x00, 0x50, 0x8B, 0xE2, 0xB7, 0x8F, 0x88, 0x00,
157+
0x00, 0x50, 0x8B, 0xE2, 0xB7, 0x8F, 0x88, 0x00,
158158
0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x88, 0x00,
159-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00,
159+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00,
160160
0x6B, 0x00, 0x6F, 0x00, 0x73, 0x00, 0x74, 0x00,
161-
0x6F, 0x00, 0x76, 0x00, 0x31, 0x00, 0x73, 0x00,
161+
0x6F, 0x00, 0x76, 0x00, 0x31, 0x00, 0x73, 0x00,
162162
0x61, 0x00, 0x4F, 0x00, 0x53, 0x00, 0x51, 0x00,
163-
0x4C, 0x00, 0x2D, 0x00, 0x33, 0x00, 0x32, 0x00,
163+
0x4C, 0x00, 0x2D, 0x00, 0x33, 0x00, 0x32, 0x00,
164164
0x4F, 0x00, 0x44, 0x00, 0x42, 0x00, 0x43, 0x00
165165
]);
166166
var atual = TdsBuilder.LoginRequest("sa", "", "", "skostov1", "OSQL-32");
167167

168168
assert.equal(atual.length, expected.length);
169+
for (var i = 0; i < expected.length; i++) if (expected[i] !== atual[i]) assert.fail(atual[i], expected[i], "i = " + i);
169170
assert.equal(atual.inspect(), expected.inspect());
170171

171172

@@ -177,29 +178,30 @@ http://msdn.microsoft.com/en-us/library/dd303700(v=PROT.13).aspx
177178
178179
*/
179180

180-
var expected = new Buffer([
181-
0x04, 0x01, 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0xE3, 0x1B, 0x00, 0x01, 0x06, 0x6D, 0x00, 0x61,
182-
0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, 0x72, 0x00, 0x06, 0x6D, 0x00, 0x61, 0x00, 0x73, 0x00,
183-
0x74, 0x00, 0x65, 0x00, 0x72, 0x00, 0xAB, 0x58, 0x00, 0x45, 0x16, 0x00, 0x00, 0x02, 0x00, 0x25,
184-
0x00, 0x43, 0x00, 0x68, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x65, 0x00, 0x64, 0x00, 0x20,
185-
0x00, 0x64, 0x00, 0x61, 0x00, 0x74, 0x00, 0x61, 0x00, 0x62, 0x00, 0x61, 0x00, 0x73, 0x00, 0x65,
186-
0x00, 0x20, 0x00, 0x63, 0x00, 0x6F, 0x00, 0x6E, 0x00, 0x74, 0x00, 0x65, 0x00, 0x78, 0x00, 0x74,
187-
0x00, 0x20, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x20, 0x00, 0x27, 0x00, 0x6D, 0x00, 0x61, 0x00, 0x73,
181+
var buffer = new Buffer([
182+
0x04, 0x01, 0x01, 0x61, 0x00, 0x00, 0x01, 0x00, 0xE3, 0x1B, 0x00, 0x01, 0x06, 0x6D, 0x00, 0x61,
183+
0x00, 0x73, 0x00, 0x74, 0x00, 0x65, 0x00, 0x72, 0x00, 0x06, 0x6D, 0x00, 0x61, 0x00, 0x73, 0x00,
184+
0x74, 0x00, 0x65, 0x00, 0x72, 0x00, 0xAB, 0x58, 0x00, 0x45, 0x16, 0x00, 0x00, 0x02, 0x00, 0x25,
185+
0x00, 0x43, 0x00, 0x68, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x65, 0x00, 0x64, 0x00, 0x20,
186+
0x00, 0x64, 0x00, 0x61, 0x00, 0x74, 0x00, 0x61, 0x00, 0x62, 0x00, 0x61, 0x00, 0x73, 0x00, 0x65,
187+
0x00, 0x20, 0x00, 0x63, 0x00, 0x6F, 0x00, 0x6E, 0x00, 0x74, 0x00, 0x65, 0x00, 0x78, 0x00, 0x74,
188+
0x00, 0x20, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x20, 0x00, 0x27, 0x00, 0x6D, 0x00, 0x61, 0x00, 0x73,
188189
0x00, 0x74, 0x00, 0x65, 0x00, 0x72, 0x00, 0x27, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
189-
0x00, 0xE3, 0x08, 0x00, 0x07, 0x05, 0x09, 0x04, 0xD0, 0x00, 0x34, 0x00, 0xE3, 0x17, 0x00, 0x02,
190-
0x0A, 0x75, 0x00, 0x73, 0x00, 0x5F, 0x00, 0x65, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x6C, 0x00, 0x69,
191-
0x00, 0x73, 0x00, 0x68, 0x00, 0x00, 0xE3, 0x13, 0x00, 0x04, 0x04, 0x34, 0x00, 0x30, 0x00, 0x39,
192-
0x00, 0x36, 0x00, 0x04, 0x34, 0x00, 0x30, 0x00, 0x39, 0x00, 0x36, 0x00, 0xAB, 0x5C, 0x00, 0x47,
193-
0x16, 0x00, 0x00, 0x01, 0x00, 0x27, 0x00, 0x43, 0x00, 0x68, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x67,
194-
0x00, 0x65, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6C, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x75,
195-
0x00, 0x61, 0x00, 0x67, 0x00, 0x65, 0x00, 0x20, 0x00, 0x73, 0x00, 0x65, 0x00, 0x74, 0x00, 0x74,
196-
0x00, 0x69, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x20, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x20, 0x00, 0x75,
197-
0x00, 0x73, 0x00, 0x5F, 0x00, 0x65, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x6C, 0x00, 0x69, 0x00, 0x73,
190+
0x00, 0xE3, 0x08, 0x00, 0x07, 0x05, 0x09, 0x04, 0xD0, 0x00, 0x34, 0x00, 0xE3, 0x17, 0x00, 0x02,
191+
0x0A, 0x75, 0x00, 0x73, 0x00, 0x5F, 0x00, 0x65, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x6C, 0x00, 0x69,
192+
0x00, 0x73, 0x00, 0x68, 0x00, 0x00, 0xE3, 0x13, 0x00, 0x04, 0x04, 0x34, 0x00, 0x30, 0x00, 0x39,
193+
0x00, 0x36, 0x00, 0x04, 0x34, 0x00, 0x30, 0x00, 0x39, 0x00, 0x36, 0x00, 0xAB, 0x5C, 0x00, 0x47,
194+
0x16, 0x00, 0x00, 0x01, 0x00, 0x27, 0x00, 0x43, 0x00, 0x68, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x67,
195+
0x00, 0x65, 0x00, 0x64, 0x00, 0x20, 0x00, 0x6C, 0x00, 0x61, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x75,
196+
0x00, 0x61, 0x00, 0x67, 0x00, 0x65, 0x00, 0x20, 0x00, 0x73, 0x00, 0x65, 0x00, 0x74, 0x00, 0x74,
197+
0x00, 0x69, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x20, 0x00, 0x74, 0x00, 0x6F, 0x00, 0x20, 0x00, 0x75,
198+
0x00, 0x73, 0x00, 0x5F, 0x00, 0x65, 0x00, 0x6E, 0x00, 0x67, 0x00, 0x6C, 0x00, 0x69, 0x00, 0x73,
198199
0x00, 0x68, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAD, 0x36, 0x00, 0x01, 0x72,
199-
0x09, 0x00, 0x02, 0x16, 0x4D, 0x00, 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6F, 0x00, 0x73, 0x00,
200-
0x6F, 0x00, 0x66, 0x00, 0x74, 0x00, 0x20, 0x00, 0x53, 0x00, 0x51, 0x00, 0x4C, 0x00, 0x20, 0x00,
200+
0x09, 0x00, 0x02, 0x16, 0x4D, 0x00, 0x69, 0x00, 0x63, 0x00, 0x72, 0x00, 0x6F, 0x00, 0x73, 0x00,
201+
0x6F, 0x00, 0x66, 0x00, 0x74, 0x00, 0x20, 0x00, 0x53, 0x00, 0x51, 0x00, 0x4C, 0x00, 0x20, 0x00,
201202
0x53, 0x00, 0x65, 0x00, 0x72, 0x00, 0x76, 0x00, 0x65, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
202-
0x00, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203+
0x00, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203204
0x00
204205
]);
205-
var atual = TdsBuilder.LoginRequest("sa", "", "");
206+
207+
assert.equal(TdsBuilder.LoginResponse(buffer).envchange, "");

tests/test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
require("./test-tds");
1+
require("./test-tds");
2+
require("./test-mssql");

0 commit comments

Comments
 (0)