Skip to content
Snippets Groups Projects
Commit b33a5747 authored by deuce's avatar deuce
Browse files

Make the default handler a bit more magical... REJECT files already received

this session, SKIP files that already exist, and ACCEPT all others.

No longer try to append extra data packets to the end of the file to allow
files to grow in transit... the protocol is really not well suited to this
anyway.  Instead, truncate to the advertised length.

If the want_callback changes the file name, still ACK the correct file.

Escape filenames more consistantly.
parent 950007d7
Branches
Tags
No related merge requests found
...@@ -86,7 +86,14 @@ function BinkP(name_ver, inbound, rx_callback) ...@@ -86,7 +86,14 @@ function BinkP(name_ver, inbound, rx_callback)
BinkP.prototype.Frame = function() {}; BinkP.prototype.Frame = function() {};
BinkP.prototype.default_want = function(fobj, fsize, fdate, offset) BinkP.prototype.default_want = function(fobj, fsize, fdate, offset)
{ {
// Accept everything. // Reject duplicate filenames... a more robust callback would rename them.
// Or process the old ones first.
if (this.received_files.indexOf(fobj.name) != -1)
return this.file.REJECT;
// Skip existing files.
if (file_exists(fobj.name))
return this.file.SKIP;
// Accept everything else
return this.file.ACCEPT; return this.file.ACCEPT;
}; };
BinkP.prototype.escapeFileName = function(name) BinkP.prototype.escapeFileName = function(name)
...@@ -163,23 +170,23 @@ BinkP.prototype.faddr_to_inetaddr = function(addr) ...@@ -163,23 +170,23 @@ BinkP.prototype.faddr_to_inetaddr = function(addr)
BinkP.prototype.ack_file = function() BinkP.prototype.ack_file = function()
{ {
if (this.receiving !== undefined) { if (this.receiving !== undefined) {
this.receiving.close();
if (this.receiving.position >= this.receiving_len) { if (this.receiving.position >= this.receiving_len) {
this.receiving.truncate(this.receiving_len);
this.receiving.close();
this.receiving.date = this.receiving_date; this.receiving.date = this.receiving_date;
if (this.rx_callback !== undefined) if (this.rx_callback !== undefined)
this.rx_callback(this.receiving.name); this.rx_callback(this.receiving.name);
if (this.received_files.indexOf(this.receiving.name) == -1) { if (this.sendCmd(this.command.M_GOT, this.escapeFileName(this.receiving_name)+' '+this.receiving.length+' '+this.receiving.date))
if (this.sendCmd(this.command.M_GOT, file_getname(this.receiving.name)+' '+this.receiving.position+' '+this.receiving.date)) { this.received_files.push(this.receiving.name);
this.received_files.push(this.receiving.name);
}
}
} }
else { else {
this.receiving.close();
this.failed_received_files.push(this.receiving.name); this.failed_received_files.push(this.receiving.name);
} }
this.receiving = undefined; this.receiving = undefined;
this.receiving_len = undefined; this.receiving_len = undefined;
this.receiving_date = undefined; this.receiving_date = undefined;
this.receiving_name = undefined;
} }
}; };
BinkP.prototype.getCRAM = function(algo, key) BinkP.prototype.getCRAM = function(algo, key)
...@@ -334,16 +341,13 @@ BinkP.prototype.accept = function(sock, auth_cb) ...@@ -334,16 +341,13 @@ BinkP.prototype.accept = function(sock, auth_cb)
return false; return false;
if (pkt !== null && pkt !== this.partialFrame) { if (pkt !== null && pkt !== this.partialFrame) {
if (pkt.is_cmd) { if (pkt.is_cmd) {
switch(pkt.command) { if (pkt.command === this.command.M_PWD) {
case this.command.M_PWD: if (auth_cb(this.remote_addrs, this.parseArgs(pkt.data), challenge))
if (auth_cb(this.remote_addrs, this.parseArgs(pkt.data), challenge)) this.authenticated = 'secure';
this.authenticated = 'secure'; else
else this.authenticated = 'non-secure';
this.authenticated = 'non-secure'; this.sendCmd(this.command.M_OK, this.authenticated);
this.sendCmd(this.command.M_OK, this.authenticated); break;
break;
default:
break;
} }
} }
} }
...@@ -366,6 +370,7 @@ BinkP.prototype.session = function() ...@@ -366,6 +370,7 @@ BinkP.prototype.session = function()
var pkt; var pkt;
var m; var m;
var tmp; var tmp;
var tmp2;
var ver; var ver;
var args; var args;
var size; var size;
...@@ -386,14 +391,13 @@ BinkP.prototype.session = function() ...@@ -386,14 +391,13 @@ BinkP.prototype.session = function()
// Ignore // Ignore
break; break;
case this.command.M_FILE: case this.command.M_FILE:
this.senteob = false;
this.ack_file(); this.ack_file();
args = this.parseArgs(pkt.data); args = this.parseArgs(pkt.data);
if (args.length < 4) { if (args.length < 4) {
log(LOG_ERROR, "Invalid M_FILE command args: '"+pkt.data+"'"); log(LOG_ERROR, "Invalid M_FILE command args: '"+pkt.data+"'");
this.sendCmd(this.command.M_ERR, "Invalid M_FILE command args: '"+pkt.data+"'"); this.sendCmd(this.command.M_ERR, "Invalid M_FILE command args: '"+pkt.data+"'");
} }
if (this.ver1_1)
this.senteob = false;
tmp = new File(this.inbound + file_getname(args[0])); tmp = new File(this.inbound + file_getname(args[0]));
switch (this.want_callback(tmp, parseInt(args[1], 10), parseInt(args[2], 10), parseInt(args[3], 10))) { switch (this.want_callback(tmp, parseInt(args[1], 10), parseInt(args[2], 10), parseInt(args[3], 10))) {
case this.file.SKIP: case this.file.SKIP:
...@@ -423,10 +427,11 @@ BinkP.prototype.session = function() ...@@ -423,10 +427,11 @@ BinkP.prototype.session = function()
} }
if (!tmp.open(tmp.exists ? "r+b" : "wb")) { if (!tmp.open(tmp.exists ? "r+b" : "wb")) {
log(LOG_ERROR, "Unable to open file "+tmp.name); log(LOG_ERROR, "Unable to open file "+tmp.name);
this.sendCmd(this.command.M_SKIP, args[0]+' '+args[1]+' '+args[2]); this.sendCmd(this.command.M_SKIP, this.escapeFileName(args[0])+' '+args[1]+' '+args[2]);
break; break;
} }
this.receiving = tmp; this.receiving = tmp;
this.receiving_name = args[0];
this.receiving_len = parseInt(args[1], 10); this.receiving_len = parseInt(args[1], 10);
this.receiving_date = parseInt(args[2], 10); this.receiving_date = parseInt(args[2], 10);
} }
...@@ -513,13 +518,7 @@ BinkP.prototype.session = function() ...@@ -513,13 +518,7 @@ BinkP.prototype.session = function()
else { else {
this.receiving.write(pkt.data); this.receiving.write(pkt.data);
// We need to ACK here... // We need to ACK here...
if (this.receiving.position >= this.receiving_len) { this.ack_file();
this.receiving.date = this.receiving_date;
if (this.received_files.indexOf(this.receiving.name) == -1) {
if (this.sendCmd(this.command.M_GOT, file_getname(this.receiving.name)+' '+this.receiving.position+' '+this.receiving.date))
this.received_files.push(this.receiving.name);
}
}
} }
} }
} }
...@@ -531,9 +530,9 @@ BinkP.prototype.session = function() ...@@ -531,9 +530,9 @@ BinkP.prototype.session = function()
else { else {
this.pending_ack.push(file_getname(this.sending.name)); this.pending_ack.push(file_getname(this.sending.name));
if (this.nonreliable) if (this.nonreliable)
this.sendCmd(this.command.M_FILE, file_getname(this.sending.name)+' '+this.sending.length+' '+this.sending.date+' -1'); this.sendCmd(this.command.M_FILE, this.escapeFileName(file_getname(this.sending.name))+' '+this.sending.length+' '+this.sending.date+' -1');
else else
this.sendCmd(this.command.M_FILE, file_getname(this.sending.name)+' '+this.sending.length+' '+this.sending.date+' '+this.sending.position); this.sendCmd(this.command.M_FILE, this.escapeFileName(file_getname(this.sending.name))+' '+this.sending.length+' '+this.sending.date+' '+this.sending.position);
if (this.ver1_1) if (this.ver1_1)
this.goteob = false; this.goteob = false;
} }
...@@ -585,6 +584,8 @@ BinkP.prototype.sendCmd = function(cmd, data) ...@@ -585,6 +584,8 @@ BinkP.prototype.sendCmd = function(cmd, data)
var type; var type;
var tmp; var tmp;
if (this.sock === undefined)
return false;
if (data === undefined) if (data === undefined)
data = ''; data = '';
if (this.debug) { if (this.debug) {
...@@ -624,6 +625,9 @@ BinkP.prototype.sendCmd = function(cmd, data) ...@@ -624,6 +625,9 @@ BinkP.prototype.sendCmd = function(cmd, data)
BinkP.prototype.sendData = function(data) BinkP.prototype.sendData = function(data)
{ {
var len = data.length; var len = data.length;
if (this.sock === undefined)
return false;
if (this.debug) if (this.debug)
log(LOG_DEBUG, "Sending "+data.length+" bytes of data"); log(LOG_DEBUG, "Sending "+data.length+" bytes of data");
if (!this.sock.sendBin(len, 2)) if (!this.sock.sendBin(len, 2))
...@@ -643,6 +647,11 @@ BinkP.prototype.recvFrame = function(timeout) ...@@ -643,6 +647,11 @@ BinkP.prototype.recvFrame = function(timeout)
var ver; var ver;
var avail; var avail;
if (this.sock === undefined) {
this.partialFrame = undefined;
return undefined;
}
if (timeout === undefined) if (timeout === undefined)
timeout = 0; timeout = 0;
...@@ -734,12 +743,11 @@ BinkP.prototype.recvFrame = function(timeout) ...@@ -734,12 +743,11 @@ BinkP.prototype.recvFrame = function(timeout)
}; };
} }
else { else {
switch(args[i]) { if (args[i] === 'NR') {
case 'NR': if (!this.sent_nr)
if (!this.sent_nr) this.sendCmd(this.command.M_NUL, "NR");
this.sendCmd(this.command.M_NUL, "NR"); this.non_reliable = true;
this.non_reliable = true; break;
break;
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment