Skip to content

Instantly share code, notes, and snippets.

@aredridel
Last active January 4, 2016 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aredridel/8642161 to your computer and use it in GitHub Desktop.
Save aredridel/8642161 to your computer and use it in GitHub Desktop.
var dgram = require('dgram');
var srp = require('srp');
var util = require('util');
var DBConn = require('./dbconn');
var proto = require('./proto');
// UDPApp
//
// This object encompasses all functionality dealing with the UDP endpoint of
// charon. More specifically, the socket server lives here.
// Constructor
var UDPApp = function(config) {
"use strict";
if (!("dbfilename" in config)) {
throw Error("Missing dbfilename in UDPApp configuration.");
}
if (!("port" in config)) {
throw Error("Missing port in UDPApp configuration.");
}
// Create database connection
this.dbconn = new DBConn({
filename: config.dbfilename
});
// Create socket server
this.socket = dgram.createSocket('udp4');
this.socket.on('message', this.router());
this.socket.bind(config.port);
};
// Object methods
UDPApp.prototype = {
router: function () {
var routes = this.routes;
return function(msg, rinfo) {
if (msg.length < 4) {
util.log("Message from " + rinfo.address + " is too small, discarding.");
return;
}
var packetType = msg.readUInt32LE(0);
if (packetType in routes) {
router[packetType](msg, rinfo);
} else {
util.log("Message from " + rinfo.address + " has an invalid packet type, discarding.");
}
}
}
// Routes
UDPApp.prototype.routes = {};
UDPApp.prototype.routes[proto.SERVER_NEGOTIATE] = function(msg, rinfo) {
util.log("SERVER_NEGOTIATE not implemented.");
};
UDPApp.prototype.routes[proto.SRP_A] = function(msg, rinfo) {
util.log("SRP_A not implemented.");
};
UDPApp.prototype.routes[proto.SRP_M] = function(msg, rinfo) {
util.log("SRP_M not implemented.");
};
module.exports = UDPApp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment