Skip to content

Instantly share code, notes, and snippets.

@FrankGrimm
Created August 19, 2010 12:30
Show Gist options
  • Save FrankGrimm/537759 to your computer and use it in GitHub Desktop.
Save FrankGrimm/537759 to your computer and use it in GitHub Desktop.
/*
* syslog-server.js
*
* @version 0.1.0
* @author Frank Grimm (http://frankgrimm.net)
*
*/
exports.getServer = function(bind_port, bind_ip, callback) {
// bind parameters
var bindip = bind_ip || undefined;
var bindport = bind_port || 514;
// default callback
var callthis = function(msgobject) {
console.log(require("sys").inspect(msgobject));
}
if (typeof callback == "function") {
callthis = callback;
}
var dgram = require("dgram");
var syslog = require("./syslog-messages");
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
syslog.decodeMessage(msg.toString('ascii'), function(receivedMsg) {
// attach connection information to the received message
receivedMsg.rinfo = rinfo;
callthis(receivedMsg);
});
}).on("listening", function () {
var address = server.address();
console.log("[INFO] syslog server listening " +
address.address + ":" + address.port);
}).bind(bindport, bindip); // bind 514:UDP on all interfaces
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment