Commit af5df00f authored by Benjamin Chelli's avatar Benjamin Chelli

remove the connect function and make it called implicitly by every single...

remove the connect function and make it called implicitly by every single function trying to interact with the share
parent 59423dd3
......@@ -24,26 +24,18 @@ var s = new SMB2({
, password:'password!'
});
// negotiate the connection with the server
s.connect(function(err){
// read the content of the folder
s.readDir('Windows\\System32', function(err, files){
// if an error happen => throw it
if(err) throw err;
// read the content of the folder
s.readDir('Windows\\System32', function(err, files){
// if an error happen => throw it
if(err) throw err;
// display content in the console
console.log(files);
// close the connection
s.close();
});
// display content in the console
console.log(files);
// close the connection
s.close();
});
```
......
......@@ -76,50 +76,6 @@ var SMB = module.exports = function(opt){
var proto = SMB.prototype = {};
/*
* connect
* =======
*
* connect you to the SMB2 server:
*
* - open TCP socket
*
* - negotiation SM2 connection
*
* - setup session / ntlm negotiatiation
*
* - setup session / ntlm authetication
*
* - tree connect
*
*/
proto.connect = function(cb){
var connection = this;
// open TCP socket
connection.socket.connect(port, this.ip);
// SMB2 negotiate connection
SMB2Request('negotiate', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / negotiate ntlm
else SMB2Request('session_setup_step1', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / autheticate with ntlm
else SMB2Request('session_setup_step2', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 tree connect
else SMB2Request('tree_connect', {}, connection, function(err){
if(err) cb && cb(err);
else cb && cb(null);
});
});
});
});
}
/*
* close
* =====
......@@ -150,23 +106,27 @@ proto.close = function(){
proto.readdir = function(path, cb){
var connection = this;
// SMB2 open directory
SMB2Request('open', {path:path}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 query directory
else SMB2Request('query_directory', file, connection, function(err, files){
connect(connection, function(err){
if(err) cb(err);
// SMB2 open directory
else SMB2Request('open', {path:path}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 close directory
else SMB2Request('close', file, connection, function(err){
cb && cb(
err
, files
.map(function(v){ return v.Filename }) // get the filename only
.filter(function(v){ return v!='.' && v!='..' }) // remove '.' and '..' values
);
// SMB2 query directory
else SMB2Request('query_directory', file, connection, function(err, files){
if(err) cb && cb(err);
// SMB2 close directory
else SMB2Request('close', file, connection, function(err){
cb && cb(
err
, files
.map(function(v){ return v.Filename }) // get the filename only
.filter(function(v){ return v!='.' && v!='..' }) // remove '.' and '..' values
);
});
});
});
});
})
}
......@@ -191,64 +151,67 @@ proto.readFile = function(filename, options, cb){
options = {};
}
// SMB2 open file
SMB2Request('open', {path:filename}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 read file content
else {
var fileLength = 0
, offset = new bigint(8)
, stop = false
, nbRemainingPackets = 0
, maxPacketSize = 0x00010000
;
// get file length
for(var i=0;i<file.EndofFile.length;i++){
fileLength |= file.EndofFile[i] << (i*8);
}
var result = new Buffer(fileLength);
// callback manager
function callback(offset){
return function(err, content){
if(stop) return;
if(err) {
cb && cb(err);
stop = true;
} else {
content.copy(result, offset.toNumber());
nbRemainingPackets--;
checkDone();
connect(connection, function(err){
if(err) cb(err);
// SMB2 open file
else SMB2Request('open', {path:filename}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 read file content
else {
var fileLength = 0
, offset = new bigint(8)
, stop = false
, nbRemainingPackets = 0
, maxPacketSize = 0x00010000
;
// get file length
for(var i=0;i<file.EndofFile.length;i++){
fileLength |= file.EndofFile[i] << (i*8);
}
var result = new Buffer(fileLength);
// callback manager
function callback(offset){
return function(err, content){
if(stop) return;
if(err) {
cb && cb(err);
stop = true;
} else {
content.copy(result, offset.toNumber());
nbRemainingPackets--;
checkDone();
}
}
}
}
// callback manager
function checkDone(){
if(stop) return;
createPackets();
if(nbRemainingPackets==0 && offset.ge(fileLength)) {
SMB2Request('close', file, connection, function(err){
cb && cb(err, result.toString());
})
// callback manager
function checkDone(){
if(stop) return;
createPackets();
if(nbRemainingPackets==0 && offset.ge(fileLength)) {
SMB2Request('close', file, connection, function(err){
cb && cb(err, result.toString());
})
}
}
}
// create packets
function createPackets(){
while(nbRemainingPackets<connection.packetConcurrency && offset.lt(fileLength)){
// process packet size
var rest = offset.sub(fileLength).neg();
var packetSize = rest.gt(maxPacketSize) ? maxPacketSize : rest.toNumber();
// generate buffer
SMB2Request('read', {
'FileId':file.FileId
, 'Length':packetSize
, 'Offset':offset.toBuffer()
}, connection, callback(offset));
offset = offset.add(packetSize);
nbRemainingPackets++;
// create packets
function createPackets(){
while(nbRemainingPackets<connection.packetConcurrency && offset.lt(fileLength)){
// process packet size
var rest = offset.sub(fileLength).neg();
var packetSize = rest.gt(maxPacketSize) ? maxPacketSize : rest.toNumber();
// generate buffer
SMB2Request('read', {
'FileId':file.FileId
, 'Length':packetSize
, 'Offset':offset.toBuffer()
}, connection, callback(offset));
offset = offset.add(packetSize);
nbRemainingPackets++;
}
}
checkDone();
}
checkDone();
}
});
});
}
......@@ -355,26 +318,30 @@ proto.writeFile = function(filename, data, options, cb){
checkDone();
}
this.exists(filename, function(err, exists){
if(err) cb && cb(err);
connect(connection, function(err){
if(err) cb(err);
else connection.exists(filename, function(err, exists){
else if(!exists){
if(err) cb && cb(err);
else if(!exists){
createFile(function(){
setFileSize(function(){
writeFile(function(){
closeFile(cb);
createFile(function(){
setFileSize(function(){
writeFile(function(){
closeFile(cb);
});
});
});
});
} else {
} else {
cb(new Error('File already exists'));
cb(new Error('File already exists'));
}
}
});
});
}
......@@ -395,10 +362,13 @@ proto.exists = function(path, cb){
var connection = this;
SMB2Request('open', {path:path}, connection, function(err, file){
if(err) cb && cb(null, false);
else SMB2Request('close', file, connection, function(err){
cb && cb(null, true);
connect(connection, function(err){
if(err) cb(err);
else SMB2Request('open', {path:path}, connection, function(err, file){
if(err) cb && cb(null, false);
else SMB2Request('close', file, connection, function(err){
cb && cb(null, true);
});
});
});
......@@ -421,36 +391,75 @@ proto.exists = function(path, cb){
proto.unlinkFile = function(path, cb){
var connection = this;
this.exists(path, function(err, exists){
connect(connection, function(err){
if(err) cb(err);
else connection.exists(path, function(err, exists){
if(err) cb && cb(err);
if(err) cb && cb(err);
else if(exists){
else if(exists){
// SMB2 open file
SMB2Request('create', {path:path}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 query directory
else SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileDispositionInformation',Buffer:(new bigint(1,1)).toBuffer()}, connection, function(err, files){
// SMB2 open file
SMB2Request('create', {path:path}, connection, function(err, file){
if(err) cb && cb(err);
// SMB2 close directory
else SMB2Request('close', file, connection, function(err){
cb && cb(err, files);
// SMB2 query directory
else SMB2Request('set_info', {FileId:file.FileId, FileInfoClass:'FileDispositionInformation',Buffer:(new bigint(1,1)).toBuffer()}, connection, function(err, files){
if(err) cb && cb(err);
// SMB2 close directory
else SMB2Request('close', file, connection, function(err){
cb && cb(err, files);
});
});
});
});
} else {
} else {
cb(new Error('File does not exists'));
cb(new Error('File does not exists'));
}
}
});
});
}
/*
* PRIVATE FUNCTION TO HANDLE CONNECTION
*/
connect = function(connection, cb){
if(connection.connected){
cb && cb(null);
return;
}
// open TCP socket
connection.socket.connect(port, connection.ip);
// SMB2 negotiate connection
SMB2Request('negotiate', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / negotiate ntlm
else SMB2Request('session_setup_step1', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 setup session / autheticate with ntlm
else SMB2Request('session_setup_step2', {}, connection, function(err){
if(err) cb && cb(err);
// SMB2 tree connect
else SMB2Request('tree_connect', {}, connection, function(err){
if(err) cb && cb(err);
else {
connection.connected = true;
cb && cb(null);
}
});
});
});
});
}
/*
* MESSAGE MANAGMENT
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment