udp-stream
UDP header serialization object stream.
Example
Reading
var UdpStream = require('udp-stream');
var IpStream = require('ip-stream');
var EtherStream = require('ether-stream');
var PcapStream = require('pcap-stream');
var pstream = new PcapStream(PCAP_FILE);
var estream = new EtherStream();
var ipstream = new IpStream();
var ustream = new UdpStream();
pstream.pipe(estream).pipe(ipstream).pipe(ustream);
upstream.on('readable', function() {
var msg = upstream.read();
msg.ether.src === '12:34:56:65:43:21';
msg.ip.src === '1.1.1.1';
msg.ip.dst === '2.2.2.2';
msg.ip.protocol === 'udp';
msg.udp.srcPort === 5321;
msg.udo.dstPort === 52;
var payload = msg.data;
});
upstream.on('ignored', function(msg) {
console.log('Ignored message [' + msg + ']');
});
upstream.read(0);
Writing
var UdpStream = require('udp-stream');
var IpStream = require('ip-stream');
var EtherStream = require('ether-stream');
var UdpHeader = require('udp-header');
var IpHeader = require('ip-header');
var EtherFrame = require('ether-frame');
var estream = new EtherStream();
var ipstream = new IpStream();
var ustream = new UdpStream();
estream.pipe(ipstream).pipe(ustream);
var input = {
ether: new EtherFrame({dst: '01:02:03:04:05:06'}),
ip: new IpHeader({dst: '1.2.3.4'}),
udp: new UdpHeader({dstPort: 52, dataLength: 500}),
data: new Buffer(8*1024)
};
estream.write(input);
var out = ustream.read();
out.offset === (input.ether.length * input.ip.length + input.udp.length);
test.deepEqual(input.ether, new EtherFrame(out.data, 0));
test.deepEqual(input.ip, new IpHeader(out.data, input.ether.length));
test.deepEqual(input.udp, new UdpHeader(out.data, input.ether.length +
input.ip.length));
input.suffix = new Buffer(500);
ustream2 = new UdpStream({payload: 'udpPayload'});
input.udpPayload = new Buffer(500);