Skip to content

Commit

Permalink
Merge pull request #2 from rbaumier/master
Browse files Browse the repository at this point in the history
support link-local and prevent crash when recovering gateway
  • Loading branch information
FGRibreau committed Aug 24, 2015
2 parents f484369 + 7d8c4d9 commit e1cdb98
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
21 changes: 9 additions & 12 deletions src/ifconfig.interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var _ = require('lodash');
var MAC = 'HWaddr';
var INET = 'inet';
var BCAST = 'Bcast';
var DESTINATIONS = ['default', 'link-local'];

module.exports = function (cp) {
return function (f) {
Expand Down Expand Up @@ -145,18 +146,14 @@ function getBroadcastAddr(line) {
*/
function getGateway(stdout) {
// @todo yep. this is ugly.
return _.chain(stdout)
return stdout
.split('\n')
.filter(function (line) {
return _.include(line, 'default');
})
.first()
.split(' ')
.rest() // without "default"
.join('')
.split('.')
.first()
.trim()
.replace(/-/g, '.')
.value();
return _.some(DESTINATIONS, function (destination)  {
return _.include(line, destination);
});
})[0]
.split(/\s+/)[1]
.split('.')[0]
.replace(/-/g, '.');
}
3 changes: 2 additions & 1 deletion test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
interfaces_3_out: fs.readFileSync(path.resolve(__dirname, './interfaces_3_out.txt'), 'utf8'),

route_get_1: fs.readFileSync(path.resolve(__dirname, './route_get_1.txt'), 'utf8'),
route_get_2: fs.readFileSync(path.resolve(__dirname, './route_get_2.txt'), 'utf8')
route_get_2: fs.readFileSync(path.resolve(__dirname, './route_get_2.txt'), 'utf8'),
route_get_3: fs.readFileSync(path.resolve(__dirname, './route_get_3.txt'), 'utf8')
};
4 changes: 4 additions & 0 deletions test/fixtures/route_get_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
link-local * 255.255.0.0 U 1000 0 0 eth0
192.168.0.0 * 255.255.255.0 U 0 0 0 eth0
26 changes: 26 additions & 0 deletions test/ifconfig.interfaces.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,31 @@ describe('ifconfig', function () {
done();
});
});

it('should list interfaces', function (done) {
execMock.stdout.push(fixtures.ifconfig_get_1);
execMock.stdout.push(fixtures.route_get_3);

ifconfig.interfaces(function (err, interfaces) {
t.strictEqual(err, null);
t.strictEqual(interfaces.length, 2);
t.deepEqual(interfaces, [{
name: 'eth0',
ip: '1.1.1.77',
netmask: '1.1.1.0',
broadcast: '1.1.1.255',
mac: 'aa:aa:aa:aa:aa:aa',
gateway: '*'
}, {
name: 'lo',
ip: '127.0.0.1',
netmask: '255.0.0.0',
broadcast: null,
mac: null,
gateway: '*'
}]);
done();
});
});
});
});

0 comments on commit e1cdb98

Please sign in to comment.