Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

Commit 8963eb57 authored by deuce's avatar deuce

More cleanup stuff.

parent f54c38b7
......@@ -41,18 +41,21 @@ require('sockdefs.js', 'SOCK_STREAM');
var AGWPE = {
TNC:function(host, port, user, pass) {
var self = this;
var pinfo;
var port0;
var authf;
var parr;
var i;
var m;
var pn;
if (host === undefined)
host = "127.0.0.1";
if (port === undefined)
port = 8000;
this.host = host;
this.port = port;
this.sock = new Socket(SOCK_STREAM, "AGWPE");
if (!this.sock.connect(this.host, this.port, 10))
throw("Unable to connect to AGWPE server");
this.ports = {};
this.callbacks = {
'G':[],
'R':[
{
func: function(frame) {
......@@ -70,22 +73,26 @@ var AGWPE = {
throw("Invalid Version Response Data Length!");
}
}
],
'G':[]
]
};
this.host = host;
this.port = port;
this.ports = {};
this.sock = new Socket(SOCK_STREAM, "AGWPE");
this.port = function(port)
this.tnc_port = function(port, name)
{
this.parent = self;
var pself = this;
this.__proto__ = AGWPE._portProto;
if (port === undefined)
throw("No port specified for port constructor");
if (name === undefined)
name = "Port "+(port+1);
this.__proto__ = AGWPE._portProto;
this.parent = self;
this.port = port;
this.calls = [];
this.callbacks = {
'g':[], // Garbage from DireWolf
'y':[],
......@@ -117,29 +124,34 @@ var AGWPE = {
this.rawRx = false;
this.frames = [];
/*
* This needs to be here, not in the prototype
* because it uses pself.
*/
this.frame = function(kind)
{
this.__proto__ = AGWPE._frameProto;
this.parent = pself;
if (kind === undefined)
throw("Frame being created with no kind");
this.__proto__ = AGWPE._frameProto;
this.parent = pself;
this.port = pself.port;
this.kind = kind;
this.pid = 0xf0; // I-frame
this.pid = 0xf0; // I-frame
this.from = '';
this.to = '';
this.data = '';
};
/*
* This needs to be here, not in the prototype
* because it uses pself.
*/
this.connection = function(from, to, via_pid)
{
this.__proto__ = AGWPE._connProto;
this.parent = pself;
var cself = this;
var via;
var pid;
var r;
var via = [];
var pid = 0xf0;
if (from === undefined)
throw("Connection from undefined callsign");
......@@ -147,17 +159,17 @@ var AGWPE = {
throw("Connection from unregistered callsign");
if (to === undefined)
throw("Connection to undefined call");
if (via_pid === undefined) {
via = [];
pid = 0xf0;
}
else if (Array.isArray(via_pid)) {
via = via_pid;
pid = 0xf0;
}
else {
pid = parseInt(via_pid, 10);
if (via_pid !== undefined) {
if (Array.isArray(via_pid))
via = via_pid;
else
pid = parseInt(via_pid, 10);
}
if (via.length > 7)
throw("Connect via path too long: "+via.length);
this.__proto__ = AGWPE._connProto;
this.parent = pself;
this.from = from;
this.to = to;
this.via = via;
......@@ -187,25 +199,29 @@ var AGWPE = {
}
]
};
if (via.length > 7)
throw("Connect via path too long: "+via.length);
this.connected = false;
this.disconnected = false;
pself.connections[from+"\x00"+to] = this;
if (via.length === 0)
pself._connect(from, to, pid);
else
pself._viaConnect(from, to, via);
/*
* This needs to be here, not in the prototype
* because it uses cself.
*/
this.frame = function(kind)
{
this.__proto__ = AGWPE._frameProto;
this.parent = cself;
if (kind === undefined)
throw("Frame being created with no kind");
this.__proto__ = AGWPE._frameProto;
this.parent = cself;
this.port = pself.port;
this.kind = kind;
this.pid = cself.pid; // I-frame
this.pid = cself.pid;
this.from = cself.from;
this.to = cself.to;
this.data = '';
......@@ -213,9 +229,12 @@ var AGWPE = {
};
};
var port0 = new this.port(0);
if (!this.sock.connect(this.host, this.port, 10))
throw("Unable to connect to AGWPE server");
// Do global things on port 0... this is hacky.
port0 = new this.tnc_port(0);
if (user !== undefined && pass !== undefined) {
var authf = new port0.frame('P');
authf = new port0.frame('P');
authf.data = user;
while (authf.data.length < 255)
authf.data += '\x00';
......@@ -224,40 +243,22 @@ var AGWPE = {
authf.data += '\x00';
self.sock.send(authf.bin);
}
var pinfo = port0.askPorts();
var parr = pinfo.split(/;/);
var i;
var m;
var pn;
pinfo = port0.askPorts();
parr = pinfo.split(/;/);
for (i=0; i<parseInt(parr[0]); i++) {
m = parr[i+1].match(/^Port([0-9]+)/);
if (m !== null) {
pn = parseInt(m[1]);
this.ports[pn-1] = new this.port(pn-1);
this.ports[pn-1] = new this.tnc_port(pn-1, parr[i+1]);
}
}
var pver = port0.askVersion();
this.ver = pver;
port0.askVersion();
},
_frameProto:{},
_connProto:{},
_portProto:{}
};
AGWPE.TNC.prototype.frame = function(kind)
{
this.__proto__ = AGWPE._frameProto;
if (kind === undefined)
throw("Frame being created with no kind");
this.port = 0;
this.kind = kind;
this.pid = 0xf0;
this.from = '';
this.to = '';
this.data = '';
};
AGWPE.TNC.prototype.cycle = function(timeout)
{
var f;
......@@ -265,6 +266,7 @@ AGWPE.TNC.prototype.cycle = function(timeout)
if (timeout === undefined)
timeout = 0;
function handle_callbacks(ctx, frame) {
var i;
if (ctx.callbacks[frame.kind] !== undefined) {
......@@ -334,6 +336,38 @@ AGWPE.TNC.prototype.cycle = function(timeout)
}
};
AGWPE.TNC.prototype.frame = function(kind)
{
this.__proto__ = AGWPE._frameProto;
if (kind === undefined)
throw("Frame being created with no kind");
this.port = 0;
this.kind = kind;
this.pid = 0xf0;
this.from = '';
this.to = '';
this.data = '';
};
AGWPE.TNC.prototype.getFrame = function()
{
var resp = this.sock.recv(36);
var len = ascii(resp[28]);
var ret = new this.frame('\x00');
ret.port = ascii(resp[0]);
ret.kind = resp[4];
ret.pid = ascii(resp[6]);
ret.from = resp.substr(8,10).split(/\x00/)[0];
ret.to = resp.substr(18,10).split(/\x00/)[0];
len |= ascii(resp[29] << 8);
len |= ascii(resp[30] << 16);
len |= ascii(resp[31] << 24);
ret.data = this.sock.recv(len);
return ret;
};
Object.defineProperty(AGWPE._frameProto, "bin", {
get: function bin() {
var ret = '';
......@@ -374,7 +408,6 @@ Object.defineProperty(AGWPE._frameProto, "bin", {
}
});
AGWPE._portProto._packetCallback = {
func:function(frame) {
var i;
......@@ -398,7 +431,6 @@ AGWPE._portProto._packetCallback = {
AGWPE._portProto.askVersion = function()
{
var f = new this.frame('R');
var resp;
var ret = {};
var done = false;
......@@ -421,7 +453,6 @@ AGWPE._portProto.askVersion = function()
AGWPE._portProto.askPorts = function()
{
var f = new this.frame('G');
var resp;
var data;
this.parent.callbacks.G.push({
......@@ -437,18 +468,9 @@ AGWPE._portProto.askPorts = function()
return data;
};
// 'g' command (port capabilities - just garbage on direwolf)
// TODO: 'k' command (RX raw AX25 frames)
// TODO: 'm' command (RX Monitor AX25 frames)
// 'H' command (recently heard - not implemented on direwolf)
AGWPE._portProto.registerCall = function(call)
{
var f = new this.frame('X');
var resp;
var r;
if (this.calls.indexOf(call) !== -1)
......@@ -473,14 +495,13 @@ AGWPE._portProto.registerCall = function(call)
this.calls.push(call);
return true;
default:
throw("Unexpected registerCall status: "+ascii(resp.data[0]));
throw("Unexpected registerCall status: "+r);
}
};
AGWPE._portProto.unRegisterCall = function(call)
{
var f = new this.frame('x');
var resp;
if (this.calls.indexOf(call) == -1)
return;
......@@ -492,7 +513,6 @@ AGWPE._portProto.unRegisterCall = function(call)
AGWPE._portProto.askOutstanding = function()
{
var f = new this.frame('y');
var resp;
var ret;
this.callbacks.y.push({
......@@ -507,7 +527,7 @@ AGWPE._portProto.askOutstanding = function()
return true;
}
});
this.sock.send(f.bin);
this.parent.sock.send(f.bin);
while (ret === undefined)
this.parent.cycle(0.01);
return ret;
......@@ -516,7 +536,6 @@ AGWPE._portProto.askOutstanding = function()
AGWPE._portProto.toggleMonitor = function()
{
var f = new this.frame('m');
var resp;
var ret;
this.parent.sock.send(f.bin);
......@@ -526,79 +545,15 @@ AGWPE._portProto.toggleMonitor = function()
AGWPE._portProto.toggleRaw = function()
{
var f = new this.frame('k');
var resp;
var ret;
this.parent.sock.send(f.bin);
this.rawRx = !this.rawRx;
};
AGWPE._connProto.askOutstanding = function()
{
var f = new this.frame('Y');
var resp;
var ret;
this.callbacks.Y.push({
oneshot:true,
func:function(frame) {
if (frame.data.length !== 4)
throw("Invalid length in connection askOutstanding reply: "+frame.data.length);
ret = ascii(frame.data[0]);
ret |= ascii(frame.data[1]) << 8;
ret |= ascii(frame.data[2]) << 16;
ret |= ascii(frame.data[3]) << 24;
return true;
}
});
this.sock.send(f.bin);
while (ret === undefined)
this.parent.cycle(0.01);
return ret;
};
AGWPE._connProto.doClose = function()
{
var i;
this.connected = false;
for (i in this.parent.connections) {
if (this.parent.connections[i].connected == false)
delete this.parent.connections[i];
}
};
AGWPE._connProto.close = function()
{
var f = new this.frame('d');
var resp;
var ret;
var i;
if (!this.connected)
return;
this.parent.parent.sock.send(f.bin);
};
AGWPE._connProto.send = function(data)
{
var f = new this.frame('D');
var resp;
var ret;
if (!this.connected)
throw("send on unconnected connection");
if (data === undefined)
throw("send with undefined data");
f.data = data;
this.parent.parent.sock.send(f.bin);
};
AGWPE._portProto.sendUNPROTO = function(from, to, arg3, arg4)
{
var f = new this.frame('M');
var resp;
var ret;
var via = [];
var data = '';
var head = '';
......@@ -644,7 +599,6 @@ AGWPE._portProto.sendRaw = function(data)
AGWPE._portProto._connect = function(from, to, pid)
{
var f;
var resp;
if (pid !== 0xf0)
f = new this.frame('C');
......@@ -674,33 +628,74 @@ AGWPE._portProto._viaConnect = function(from, to, via)
this.parent.sock.send(f.bin);
};
AGWPE.TNC.prototype.getFrame = function()
AGWPE._connProto.askOutstanding = function()
{
var resp = this.sock.recv(36);
var len = ascii(resp[28]);
var ret = new this.frame('\x00');
var f = new this.frame('Y');
var ret;
ret.port = ascii(resp[0]);
ret.kind = resp[4];
ret.pid = ascii(resp[6]);
ret.from = resp.substr(8,10).split(/\x00/)[0];
ret.to = resp.substr(18,10).split(/\x00/)[0];
len |= ascii(resp[29] << 8);
len |= ascii(resp[30] << 16);
len |= ascii(resp[31] << 24);
ret.data = this.sock.recv(len);
this.callbacks.Y.push({
oneshot:true,
func:function(frame) {
if (frame.data.length !== 4)
throw("Invalid length in connection askOutstanding reply: "+frame.data.length);
ret = ascii(frame.data[0]);
ret |= ascii(frame.data[1]) << 8;
ret |= ascii(frame.data[2]) << 16;
ret |= ascii(frame.data[3]) << 24;
return true;
}
});
this.parent.parent.sock.send(f.bin);
while (ret === undefined)
this.parent.parent.cycle(0.01);
return ret;
};
AGWPE._connProto.doClose = function()
{
var i;
this.connected = false;
this.disconnected = true;
for (i in this.parent.connections) {
if (this.parent.connections[i].disconnected == true)
delete this.parent.connections[i];
}
};
AGWPE._connProto.close = function()
{
var f = new this.frame('d');
if (this.disconnected)
return;
this.parent.parent.sock.send(f.bin);
};
AGWPE._connProto.send = function(data)
{
var f = new this.frame('D');
if (!this.connected)
throw("send on unconnected connection");
if (data === undefined)
throw("send with undefined data");
f.data = data;
this.parent.parent.sock.send(f.bin);
};
var tnc = new AGWPE.TNC('127.0.0.1', 8000);
tnc.ports[0].toggleMonitor();
tnc.ports[0].callbacks.pkt.push({
func:function(frame) {
var cleaned = frame.data.replace(/[\x00-\x1f]/g, function (match) {
return format("<0x%02x>", ascii(match));
});
print("Port "+frame.port+" Got '"+frame.kind+"' frame PID: "+frame.pid+"\nFrom: \""+frame.from+"\"\nTo: \""+frame.to+"\"\nData: \""+frame.data+"\"");
this.frames.shift();
}
});
while(1)
tnc.cycle(1);
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