var netkit = require('../index.js');
var util = require('util');
var tun0 = netkit.newTunInterfaceRaw();
tun0.ifname = "tun_test";
if(tun0.create()) {
console.log("Interface created: " + tun0.ifname);
console.log(" fd: " + tun0.fd);
setTimeout(function(){
if(tun0.open()) {
console.log("Opened successfully.");
netkit.assignAddress({
ifname:tun0.ifname,
mtu: 1501,
inet6: {
addr: "fe80::1",
add_addr: [ "aaaa::1/64" ]
}
},function(err){
if(err) {
console.log("Error: " + util.inspect(err));
} else {
console.log("assignAddress called successfully.");
}
});
netkit.assignRoute({
add_route6:
[
{
dest: "2003::/16",
via_if: "tun_test",
metric: 2400,
flags: netkit.FLAGS.RT_MODIFIED | netkit.FLAGS.RT_DYNAMIC
}
],
del_route6: []
},function(err){
if(err) {
console.log("Error: " + util.inspect(err));
} else {
console.log("assignRoute called successfully.");
}
});
netkit.setIfFlags(tun0.ifname,netkit.FLAGS.IFF_UP | netkit.FLAGS.IFF_RUNNING);
tun0.stream.on('readable', function() {
var chunk;
while (null !== (chunk = tun0.stream.read())) {
console.log('got %d bytes of data', chunk.length);
console.log('buffer: ' + chunk.toJSON());
if(!tun0.stream.write(chunk)) {
console.log("ERROR: test write failed");
} else
console.log("wrote back " + chunk.length + " bytes.");
}
});
setTimeout(function(){
netkit.assignRoute({
del_route6:
[
{
dest: "2003::/16",
via_if: "tun_test",
metric: 2400,
flags: netkit.FLAGS.RT_GATEWAY | netkit.FLAGS.RT_MODIFIED | netkit.FLAGS.RT_DYNAMIC
}
]
});
}, 5000);
}
},1000);
} else {
console.log("Failed to create interface tun0, error: " + tun0.lastError + " --> " + tun0.lastErrorStr);
}
console.log("You can type 'ifconfig -a' in a terminal to see if the interface exists.");
setTimeout(function(){
console.log("done.");
}, 15000);