Sleds/K2Proxy/k2_udp.js

38 lines
977 B
JavaScript

/*
Copyright (c) 2014 IOnU Security Inc. All rights reserved
Created May 2014 by Kendrick Webster
K2Proxy/k2_udp.js - UDP interface supporting the k2.js module,
sends/receives to/from the K2Daemon instance that this proxy is
configured to use.
*/
"use strict";
var dgram = require("dgram"),
config = require("./config");
function trace(m) { // comment or un-comment as needed for debugging:
// console.log(m);
}
var receiver;
var udp_socket = dgram.createSocket('udp4', function (msg, rinfo) {
trace('udp recv from ' + rinfo.address + ':' + rinfo.port + ', ' + msg.length + ' bytes');
if (typeof receiver === 'function') {
receiver(msg);
}
});
function send(packet) {
udp_socket.send(packet, 0, packet.length, config.daemonPort, config.daemonHost);
trace('udp send ' + packet.length + ' bytes');
}
function set_receiver(callback) {
receiver = callback;
}
exports.send = send;
exports.set_receiver = set_receiver;