-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagwpe.js
701 lines (634 loc) · 16.3 KB
/
agwpe.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
* The AGWPE object implements the AGWPE protocol as supported by direwolf.
* It allows TNC operations, including connected packet.
*
* Basic API:
* var tnc = new AGWPE.TNC('127.0.0.1', 8000); // SYNC
* - Connects to the listening sever at the specified host and port, and creates port objects.
* Additionally, username and password can be included after this to log in (direwolf doesn't use this)
* tnc.ports[0].registerCall('W8BSD-1'); // SYNC
* - Registers a callsign on a port. Calls must be registered before they are the source of data.
* tnc.ports[0].unRegisterCall('W8BSD-1'); // ASYNC
* - Unregisters a callsign on a port.
* var os = tnc.ports[0].askOutstanding(); // SYNC
* - Requests the number of outstanding frames on the port
* tnc.ports[0].sendUNPROTO('W8BSD-1', 'W8BSD-2', 'Hello Deuce!'); // ASYNC
* - Sends an UNPROTO frame.
* tnc.ports[0].sendUNPROTO('W8BSD-1', 'W8BSD-2', ['W8BSD-3', 'W8BSD-4'], 'Hello Deuce!'); // ASYNC
* - Sends an UNPROTO frame using a via path
* tnc.ports[0].sendRaw(data); // ASYNC
* - Sends an UNPROTO frame using a via path
* tnc.ports[0].toggleMonitor(); // ASYNC
* - Toggle if monitor frames are received
* tnc.ports[0].toggleRaw(); // ASYNC
* - Toggle if raw frames are received
* var conn = new tnc.ports[0].connection('W8BSD-1', 'W8BSD-2'); // ASYNC
* - Connects FROM the first callsign TO the second callsign
* var conn2 = new tnc.ports[0].connection('W8BSD-1', 'W8BSD-2', ['W8BSD-3', 'W8BSD-4']); // ASYNC
* - As above but connects via the array of digipeaters
* var conn3 = new tnc.ports[0].connection('W8BSD-1', 'W8BSD-2', 0xCC); // ASYNC
* - As above but connects using a different PID (0xCC in this example)
* var cos = conn.askOutstanding(); // SYNC
* - Requests the number of outstanding frames on the connection
* conn.close(); // ASYNC
* - Closes a connection
* conn.send(data); // ASYNC
* - Sends data on a connection
*/
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.callbacks = {
'G':[],
'R':[
{
func: function(frame) {
if (frame.data.length == 8) {
this.major = ascii(frame.data[0]);
this.major |= ascii(frame.data[1]) << 8;
this.major |= ascii(frame.data[2]) << 16;
this.major |= ascii(frame.data[3]) << 24;
this.minor = ascii(frame.data[4]);
this.minor |= ascii(frame.data[5]) << 8;
this.minor |= ascii(frame.data[6]) << 16;
this.minor |= ascii(frame.data[7]) << 24;
}
else
throw("Invalid Version Response Data Length!");
}
}
]
};
this.host = host;
this.port = port;
this.ports = {};
this.sock = new Socket(SOCK_STREAM, "AGWPE");
this.tnc_port = function(port, name)
{
var pself = this;
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':[],
'X':[],
'H':[],
'M':[
this._packetCallback
],
'K':[
this._packetCallback
],
'S':[
this._packetCallback
],
'U':[
this._packetCallback
],
'T':[
this._packetCallback
],
'I':[
this._packetCallback
],
'pkt':[
]
};
this.connections = {};
this.monitor = false;
this.rawRx = false;
this.frames = [];
/*
* This needs to be here, not in the prototype
* because it uses pself.
*/
this.frame = function(kind)
{
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.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)
{
var cself = this;
var via = [];
var pid = 0xf0;
if (from === undefined)
throw("Connection from undefined callsign");
if (pself.calls.indexOf(from) === -1)
throw("Connection from unregistered callsign");
if (to === undefined)
throw("Connection to undefined call");
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;
this.pid = pid;
this.data = '';
this.callbacks = {
'Y':[],
'C':[
{
func:function(frame) {
this.connected = true;
}
}
],
'D':[
{
func:function(frame) {
this.data += frame.data;
}
}
],
'd':[
{
func:function(frame) {
this.doClose();
}
}
]
};
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)
{
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;
this.from = cself.from;
this.to = cself.to;
this.data = '';
};
};
};
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) {
authf = new port0.frame('P');
authf.data = user;
while (authf.data.length < 255)
authf.data += '\x00';
authf.data += pass;
while (authf.data.length < 510)
authf.data += '\x00';
self.sock.send(authf.bin);
}
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.tnc_port(pn-1, parr[i+1]);
}
}
port0.askVersion();
},
_frameProto:{},
_connProto:{},
_portProto:{}
};
AGWPE.TNC.prototype.cycle = function(timeout)
{
var f;
var c;
if (timeout === undefined)
timeout = 0;
function handle_callbacks(ctx, frame) {
var i;
if (ctx.callbacks[frame.kind] !== undefined) {
for (i = 0; i < ctx.callbacks[frame.kind].length; i++) {
if (ctx.callbacks[frame.kind][i].func.call(ctx, frame)) {
if (ctx.callbacks[frame.kind][i].oneshot !== undefined) {
if (ctx.callbacks[frame.kind][i].oneshot === true) {
ctx.callbacks[frame.kind].splice(i, 1);
i--;
}
}
}
}
}
}
function find_conn(f) {
var i;
for (i in this.ports[f.port].connections) {
// TODO: Sort out the reversal of calls...
if (f.from == this.ports[f.port].connections[i].from && f.to == this.ports[f.port].connections[i].to)
return this.ports[f.port].connections[i];
if (f.from == this.ports[f.port].connections[i].to && f.to == this.ports[f.port].connections[i].from)
return this.ports[f.port].connections[i];
}
throw("Message on unknown connection (from='"+f.from+"' to='"+f.to+"' kind='"+f.kind+"')");
}
if (this.sock.poll(timeout, false)) {
f = this.getFrame();
switch (f.kind) {
// "Global" messages (port doesn't matter)
case 'R': // Reply to Request for Version
case 'G': // Reply to ports request
handle_callbacks(this, f);
break;
// "Port" messages (from/to don't matter)
case 'g': // Reply to capabilities
case 'y': // Outstanding frames on a port
case 'X': // Callsign register response
case 'H': // Callsignes Heard response (not in direwolf)
case 'M': // Monitored Connected Packet
case 'K': // Raw AX.25 frame
case 'S': // Monitored Supervisory Packet
case 'U': // Monitored Unproto Packet
case 'T': // Monitored Own Packet
case 'I': // Monitored Connected Information (not in direwolf)
if (this.ports[f.port] === undefined)
throw("Got message on invalid port "+f.port+"!");
handle_callbacks(this.ports[f.port], f);
break;
// "Connection" messages
case 'Y': // Outstanding frames on a connection
case 'C': // AX.25 connection established
case 'D': // AX.25 Connected Data
case 'd': // Disconnection notice
// Find or create the connection...
c = find_conn.call(this, f);
handle_callbacks(c, f);
break;
default:
throw("Unhandled kind: '"+f.kind+"'");
}
}
};
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 = '';
ret += ascii(this.port);
ret += ascii(0);
ret += ascii(0);
ret += ascii(0);
if (ret.length !== 4)
throw ("Invalid length after port "+ret.length);
ret += this.kind;
ret += ascii(0);
if (ret.length !== 6)
throw ("Invalid length after kind "+ret.length);
ret += ascii(this.pid);
ret += ascii(0);
if (ret.length !== 8)
throw ("Invalid length after PID "+ret.length);
ret += this.from;
while (ret.length < 18)
ret += ascii(0);
ret += this.to;
while (ret.length < 28)
ret += ascii(0);
ret += ascii(this.data.length & 0xff);
ret += ascii((this.data.length >> 8) & 0xff);
ret += ascii((this.data.length >> 16) & 0xff);
ret += ascii((this.data.length >> 24) & 0xff);
ret += ascii(0);
ret += ascii(0);
ret += ascii(0);
ret += ascii(0);
if (ret.length !== 36)
throw ("Invalid length "+ret.length);
ret += this.data;
js.flatten_string(ret);
return ret;
}
});
AGWPE._portProto._packetCallback = {
func:function(frame) {
var i;
this.frames.push(frame);
if (this.callbacks.pkt !== undefined) {
for (i = 0; i < this.callbacks.pkt.length; i++) {
if (this.callbacks.pkt[i].func.call(this, frame)) {
if (this.callbacks.pkt[i].oneshot !== undefined) {
if (this.callbacks.pkt[i].oneshot === true) {
this.callbacks.pkt.splice(i, 1);
i--;
}
}
}
}
}
}
};
AGWPE._portProto.askVersion = function()
{
var f = new this.frame('R');
var ret = {};
var done = false;
this.parent.callbacks.R.push({
oneshot:true,
func:function(frame) {
done = true;
return true;
}
});
this.parent.sock.send(f.bin);
while (!done)
this.parent.cycle(0.01);
ret.major = this.parent.major;
ret.minor = this.parent.minor;
return ret;
};
AGWPE._portProto.askPorts = function()
{
var f = new this.frame('G');
var data;
this.parent.callbacks.G.push({
oneshot:true,
func:function(frame) {
data = frame.data;
return true;
}
});
this.parent.sock.send(f.bin);
while (data === undefined)
this.parent.cycle(0.01);
return data;
};
AGWPE._portProto.registerCall = function(call)
{
var f = new this.frame('X');
var r;
if (this.calls.indexOf(call) !== -1)
return false;
f.from = call;
this.callbacks.X.push({
oneshot:true,
func:function(frame) {
if (frame.data.length !== 1)
throw("Incorrect 'X' frame data length: "+frame.data.length);
r = ascii(frame.data[0]);
return true;
}
});
this.parent.sock.send(f.bin);
while (r === undefined)
this.parent.cycle(0.01);
switch(r) {
case 0:
return false;
case 1:
this.calls.push(call);
return true;
default:
throw("Unexpected registerCall status: "+r);
}
};
AGWPE._portProto.unRegisterCall = function(call)
{
var f = new this.frame('x');
if (this.calls.indexOf(call) == -1)
return;
f.from = call;
this.parent.sock.send(f.bin);
this.calls.splice(this.calls.indexOf(call), 1);
};
AGWPE._portProto.askOutstanding = function()
{
var f = new this.frame('y');
var ret;
this.callbacks.y.push({
oneshot:true,
func:function(frame) {
if (frame.data.length !== 4)
throw("Invalid length in 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.sock.send(f.bin);
while (ret === undefined)
this.parent.cycle(0.01);
return ret;
};
AGWPE._portProto.toggleMonitor = function()
{
var f = new this.frame('m');
var ret;
this.parent.sock.send(f.bin);
this.monitor = !this.monitor;
};
AGWPE._portProto.toggleRaw = function()
{
var f = new this.frame('k');
var ret;
this.parent.sock.send(f.bin);
this.rawRx = !this.rawRx;
};
AGWPE._portProto.sendUNPROTO = function(from, to, arg3, arg4)
{
var f = new this.frame('M');
var via = [];
var data = '';
var head = '';
var i;
if (from === undefined)
throw("sendUNPROTO without from");
if (this.calls.indexOf(from) === -1)
throw("sendUNPROTO from unregistered call '"+from+"'");
f.from = from;
if (to === undefined)
throw("sendUNPROTO without to");
f.to = to;
if (Array.isArray(arg3)) {
via = arg3;
data = arg4;
}
if (data === undefined)
data = '';
if (via.length > 0) {
f.kind = 'V';
head += ascii(via.length);
for (i in via) {
head += via[i];
while ((head.length - 1) % 10)
head += "0x00";
}
data = head + data;
}
f.data = data;
this.parent.sock.send(f.bin);
};
AGWPE._portProto.sendRaw = function(data)
{
var f = new this.frame('K');
if (data === undefined)
data = '';
this.parent.sock.send(f.bin);
};
AGWPE._portProto._connect = function(from, to, pid)
{
var f;
if (pid !== 0xf0)
f = new this.frame('C');
else
f = new this.frame('c');
f.from = from;
f.to = to;
f.pid = pid;
this.parent.sock.send(f.bin);
};
AGWPE._portProto._viaConnect = function(from, to, via)
{
var f = new this.frame('C');
var path = ascii(via.length);
var i;
f.from = from;
f.to = to;
for (i in via) {
path += via[i];
while ((path.length - 1) % 10)
path += '\x00';
}
f.data = path;
this.parent.sock.send(f.bin);
};
AGWPE._connProto.askOutstanding = function()
{
var f = new this.frame('Y');
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.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);