Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"test": "test"
},
"scripts": {
"test": "lab -v -m 20000 -p"
"test": "lab -v -m 60000 -p"
},
"keywords": [
"traceroute",
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ describe('Traceroute', () => {
done();
});
});

it('traces a fake route and quits after 5 hops in a row', (done) => {

Traceroute.trace('127.0.0.127', (err, hops) => {

expect(err).to.not.exist();
expect(hops).to.exist();
for (let hop of hops) {
expect(hop).to.equal(false);
}
done();
});
});
});
25 changes: 22 additions & 3 deletions traceroute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Net = require('net');
const Os = require('os');
const Util = require('util');


const defaultMaxNullHops = 5;
const internals = {};


Expand All @@ -18,10 +18,16 @@ internals.isWin = /^win/.test(Os.platform());
module.exports = internals.Traceroute = {};


internals.Traceroute.trace = function (host, callback) {
internals.Traceroute.trace = function (host, maxNullHops, callback) {

const Emitter = function () {
if (typeof maxNullHops === 'function' && callback === undefined) {
callback = maxNullHops;
maxNullHops = defaultMaxNullHops;
} else if (maxNullHops === undefined) {
maxNullHops = defaultMaxNullHops;
}

const Emitter = function () {
EventEmitter.call(this);
};
Util.inherits(Emitter, EventEmitter);
Expand All @@ -39,6 +45,7 @@ internals.Traceroute.trace = function (host, callback) {

const hops = [];
let counter = 0;
let nullHops = 0;
traceroute.stdout.on('data', (data) => {

++counter;
Expand All @@ -54,6 +61,18 @@ internals.Traceroute.trace = function (host, callback) {
const hop = internals.parseHop(result);
hops.push(hop);
emitter.emit('hop', hop);

if (hop === false) {
// count every null hop received
nullHops++;
} else {
// reset nullHops counter when a valid hop is received
nullHops = 0;
}
// send a sigint to kill the traceroute process when it reach $maxNullHops hops in a row
if (maxNullHops && nullHops >= maxNullHops) {
traceroute.kill('SIGINT');
}
});

traceroute.on('close', (code) => {
Expand Down