nodejs实现接收Snmp的Trap消息
var assert = require('assert');
var ASN1 = {
EOC: 0,
Boolean: 1,
Integer: 2,
BitString: 3,
OctetString: 4,
Null: 5,
OID: 6,
ObjectDescriptor: 7,
External: 8,
Real: 9, // float
Enumeration: 10,
PDV: 11,
Utf8String: 12,
RelativeOID: 13,
Sequence: 16,
Set: 17,
NumericString: 18,
PrintableString: 19,
T61String: 20,
VideotexString: 21,
IA5String: 22,
UTCTime: 23,
GeneralizedTime: 24,
GraphicString: 25,
VisibleString: 26,
GeneralString: 28,
UniversalString: 29,
CharacterString: 30,
BMPString: 31,
Constructor: 32,
Context: 128,
TypeError: function(msg) {
var e = new Error();
e.name = 'InvalidAsn1Error';
e.message = msg || '';
return e;
}
};
function Reader(data) {
if (!data || !Buffer.isBuffer(data))
throw new TypeError('data must be a node Buffer');
this._buf = data;
this._size = data.length;
// These hold the "current" state
this._len = 0;
this._offset = 0;
var self = this;
this.__defineGetter__('length', function() { return self._len; });
this.__defineGetter__('offset', function() { return self._offset; });
this.__defineGetter__('remain', function() {
return self._size - self._offset;
});
this.__defineGetter__('buffer', function() {
return self._buf.slice(self._offset);
});
}
/**
* Reads a single byte and advances offset; you can pass in `true` to make this
* a "peek" operation (i.e., get the byte, but don't advance the offset).
*
* @param {Boolean} peek true means don't move offset.
* @return {Number} the next byte, null if not enough data.
*/
Reader.prototype.readByte = function(peek) {
if (this._size - this._offset < 1)
return null;
var b = this._buf[this._offset] & 0xff;
if (!peek)
this._offset += 1;
return b;
};
Reader.prototype.peek = function() {
return this.readByte(true);
};
/**
* Reads a (potentially) variable length off the BER buffer. This call is
* not really meant to be called directly, as callers have to manipulate
* the internal buffer afterwards.
*
* As a result of this call, you can call `Reader.length`, until the
* next thing called that does a readLength.
*
* @return {Number} the amount of offset to advance the buffer.
* @throws {InvalidAsn1Error} on bad ASN.1
*/
Reader.prototype.readLength = function(offset) {
if (offset === undefined)
offset = this._offset;
if (offset >= this._size)
return null;
var lenB = this._buf[offset++] & 0xff;
if (lenB === null)
return null;
if ((lenB & 0x80) == 0x80) {
lenB &= 0x7f;
if (lenB == 0)
throw ASN1.TypeError('Indefinite length not supported');
if (lenB > 4)
throw ASN1.TypeError('encoding too long');
if (this._size - offset < lenB)
return null;
this._len = 0;
for (var i = 0; i < lenB; i++)
this._len = (this._len << 8) + (this._buf[offset++] & 0xff);
} else {
// Wasn't a variable length
this._len = lenB;
}
return offset;
};
/**
* Parses the next sequence in this BER buffer.
*
* To get the length of the sequence, call `Reader.length`.
*
* @return {Number} the sequence's tag.
*/
Reader.prototype.readSequence = function(tag) {
var seq = this.peek();
if (seq === null)
return null;
if (tag !== undefined && tag !== seq)
throw ASN1.TypeError('Expected 0x' + tag.toString(16) + ': got 0x' + seq.toString(16));
var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;
this._offset = o;
return seq;
};
Reader.prototype.readInt = function() {
return this.readTag(ASN1.Integer);
};
Reader.prototype.readBoolean = function() {
return (this.readTag(ASN1.Boolean) === 0 ? false : true);
};
Reader.prototype.readEnumeration = function() {
return this.readTag(ASN1.Enumeration);
};
Reader.prototype.readString = function(tag, retbuf) {
if (!tag)
tag = ASN1.OctetString;
var b = this.peek();
if (b === null)
return null;
if (b !== tag)
throw ASN1.TypeError('Expected 0x' + tag.toString(16) + ': got 0x' + b.toString(16));
var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;
if (this.length > this._size - o)
return null;
this._offset = o;
if (this.length === 0)
return retbuf ? new Buffer(0) : '';
var str = this._buf.slice(this._offset, this._offset + this.length);
this._offset += this.length;
return retbuf ? str : str.toString('utf8');
};
Reader.prototype.readOID = function(tag) {
if (!tag)
tag = ASN1.OID;
var b = this.readString(tag, true);
if (b === null)
return null;
var values = [];
var value = 0;
for (var i = 0; i < b.length; i++) {
var byte = b[i] & 0xff;
value <<= 7;
value += byte & 0x7f;
if ((byte & 0x80) == 0) {
values.push(value);
value = 0;
}
}
value = values.shift();
values.unshift(value % 40);
values.unshift((value / 40) >> 0);
return values.join('.');
};
Reader.prototype.readTag = function(tag) {
assert.ok(tag !== undefined);
var b = this.peek();
if (b === null)
return null;
if (b !== tag)
throw ASN1.TypeError('Expected 0x' + tag.toString(16) + ': got 0x' + b.toString(16));
var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;
if (this.length > 4)
throw ASN1.TypeError('Integer too long: ' + this.length);
if (this.length > this._size - o)
return null;
this._offset = o;
var fb = this._buf[this._offset];
var value = 0;
for (var i = 0; i < this.length; i++) {
value <<= 8;
value |= (this._buf[this._offset++] & 0xff);
}
if ((fb & 0x80) == 0x80 && i !== 4)
value -= (1 << (i * 8));
return value >> 0;
};
var dgram = require("dgram");
function parseTrapPacket(buffer){
var pkt = {};
var reader = new Reader(buffer);
reader.readSequence();
pkt.version = reader.readInt();//02 01 00
pkt.community = reader.readString();//04 06 70 75 62 6c 69 63
pkt.type = reader.readSequence();//a4
pkt.enterprise = reader.readOID()//0x06, 0x0c, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x81, 0x91, 0x28, 0x02, 0x02, 0x47, 0x64
var bytes = reader.readString(64, true);//0x40, 0x04, 0xc0, 0xa8, 0x17, 0x0a,
pkt.agentAddr = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3];
pkt.specific = reader.readInt();// 0x02, 0x01, 0x06,
pkt.generic = reader.readInt();//0x02, 0x01, 0x0a
pkt.upTime = reader.readTag(67);//
pkt.varbinds = readVarbinds(reader);
return pkt;
};
function readVarbinds (reader) {
var vbs = [];
reader.readSequence ();
while (1) {
reader.readSequence();
var oid = reader.readOID (), type = reader.peek (), value = '';
if (type == null) break;
switch(type){
case 1:
value = reader.readBoolean();
break;
case 2:
case 65:
case 66:
case 67:
value = reader.readTag(2);
break;
case 4:
value = reader.readString (null);
break;
case 5:
case 128:
case 129:
case 130:
reader.readByte ();
reader.readByte ();
value = null;
break;
case 6:
value = reader.readOID();
break;
case 64:
var bytes = reader.readString(64, true);
value = bytes.length == 4 ? bytes[0] + '.' + bytes[1] + '.' + bytes[2] + '.' + bytes[3] : '';
break;
case 68:
case 70:
value = reader.readString(type, true);
break;
}
vbs.push ({
oid: oid,
type: type,
value: value
});
}
return vbs;
}
function Receiver(port, onTrap, onError, onStart){
this.port = port;
this.socket = null;
this.isRunning = false;
this.onTrap = onTrap;
this.onError = onError;
this.onStart = onStart;
};
Receiver.prototype.start = function(){
var self = this;
if(self.isRunning) return;
var socket = self.socket = dgram.createSocket('udp4');
socket.on('error', function(err){
socket.close();
self.isRunning = false;
if(self.onError){
self.onError(err);
}
});
socket.on('message', function(msg, remote){
if(self.onTrap){
var pkt = parseTrapPacket(msg);
self.onTrap(remote, pkt);
}
});
socket.on('listening', function(){
self.isRunning = true;
if(self.onStart){
self.onStart(self.port);
}
});
socket.bind(self.port);
};
Receiver.prototype.stop = function(){
var self = this;
if(self.isRunning){
if(self.socket){
self.socket.close();
self.isRunning = false;
}
}
};
var trap = new Receiver(162, function(remote, pkt){
console.log(JSON.stringify(remote), JSON.stringify(pkt));
}, '', function(port){
console.log('begin listen on ' + port);
});
trap.start();
module.exports.TrapReceiver = Receiver;
nodejs实现接收Snmp的Trap消息的更多相关文章
- 关于Snmp的Trap代码开发之坑
最近是被这个snmp的trap给坑了,其实回想起来主要是对这个不了解.特别是对snmp协议研究不够深入, 真的不想看这些协议东西,只想大概知道.结果在开发snmp trap时候被坑了,下面列下自己踩到 ...
- Unity3D与iOS消息交互方法(1)--iOS接收Unity3D发出的消息
跨平台这种事情不管多NB, 总要有些与原生系统交互的方法, 比如 Unity3D与iOS消息交互方法. 一: 建立一个空的Unity工程. File --> New Project 二: 编 ...
- WIN7/8系统下程序接收不到WM_COPYDATA 消息的原因和解决
在WIN7/win8,如果发送消息的程序用户权限低于和接收消息的程序,则消 息无法传递.发送程序必须等于或者等于接收程序的权限.如发送与接收 是同一个用户,或者发送是管理员帐户,接收是是普通用户,这样 ...
- C/C++控制台接收不到鼠标消息-【解决办法】
控制台编程中,使用了鼠标操作,遇到了控制台无法接收到鼠标消息的问题,可尝试一下办法解决 [win10系统] 在控制台标题栏右键-默认值-选项,将一下对勾取消 然后调用如下函数: HANDLE hIn ...
- nodejs之socket.io 私发消息和选择群组发消息
写在前面:其实有的时候忙碌是好的,比如忙碌起来的自己手机可以一天耗费掉只有20%的电,忙碌的自己很专心于一件事情,但是忙碌不等于过度疲劳,本周忙碌有点上脑,迷糊了一天,都在补觉,还是要去平衡下自己一天 ...
- 【招聘App】—— React/Nodejs/MongoDB全栈项目:消息列表
前言:最近在学习Redux+react+Router+Nodejs全栈开发高级课程,这里对实践过程作个记录,方便自己和大家翻阅.最终成果github地址:https://github.com/66We ...
- nodejs数据接收body-parser中间件
给大家翻译一下npm上body-parser的资料 nodejs 的body数据解析中间件 插件作用:对于req.body属性,在操作数据前分析进来的请求体的插件 首先学习解析一个http处理 这篇文 ...
- springboot使用activemq同时接收queue和topic消息
原文链接:https://blog.csdn.net/jia_costa/article/details/79354478 新建springboot项目, pom文件如下 <?xml versi ...
- 解决在 win10 下 vs2017 中创建 MFC 程序拖放文件接收不到 WM_DROPFILES 消息问题
解决方案 这个问题是由于 win10 的安全机制搞的鬼,即使以管理员权限运行也不行,因为它会把 WM_DROPFILES 消息过滤掉,那怎么办呢?只需在窗口初始化 OnInitDialog() 里添加 ...
随机推荐
- 同一DataTable下创建多个结构数据相同的DataView的小问题
昨天在根据经理的要求修改公司后台的时候,遇到了一个很奇怪的问题 DataView dvFocus = ]); DataView dvLook = ]); DataView dvNewUser = ]) ...
- .NET中反射机制的使用与分析
.NET中反射机制的使用与分析 [日期:2008-06-30] 来源: 作者:志伟 .NET反射的定义:审查元数据并收集关于它的类型信息的能力. 元数据是一种二进制信息,用以对存储在公共语言 ...
- 使用VS调试64位应用程序
VS2012开始,就自带IIS Express,相比先前VS的Web调试器的弱智,和IIS的繁琐,Express简直是最佳方式啦. 本机环境是64位Win7 + 64位Oracle Client,调试 ...
- IBM SPSS Modeler 预测建模基础(一)
1.搜索下载IBM SPSS Modeler 14.1 32位 及 IBM SPSS Modeler 14.1 注册文件(破解布丁): 2.下载train.csv 及 test.csv: train. ...
- SQL Server中带事务的存储过程简单举例
先来看一个概念: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完整地执行,要么完全地不执行.那么在存储过程里添加事务,则可以保证该事务里的所 ...
- 解决MySQL中文乱码的问题
遇到中文乱码问题,首先用status命令检查数据库的配置,如下: 上图会显示数据库配置的各项信息. 还可以用 show create database XXX,来显示创建数据库的时候的编码设置. 一般 ...
- Java中关于final关键字
final关键字,大学时一直没有理解,伴随到工作,郁闷......也可能大学真的没认真去学习 首先,final给人一看英语单词 不就是最终的嘛,最终的什么的呢 ? 小张现在就带大家 学习下..... ...
- struts2的注解配置全面解析
以前在用struts2的注解配置时总是要在web.xml中配置一个初始化参数(actionPackages),最近发现不灵了,仔细研究了下发现即使不用在web.xml中配置也能成功,但时灵时不灵的,很 ...
- Laravel 模板引擎Blade中标签详细介绍
这篇文章主要介绍了Laravel模板引擎Blade中section的一些标签的区别介绍,需要的朋友可以来看看. Laravel 框架中的Blade模板引擎很好用,但是官方文档介绍的并不详细,我接下来将 ...
- Yogurt factory(POJ 2393 贪心 or DP)
Yogurt factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8205 Accepted: 4197 De ...