Skip to content

Commit

Permalink
fix(ifconfig): add broadcast addr
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Mar 22, 2015
1 parent 468d472 commit 09d6394
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 205 deletions.
9 changes: 9 additions & 0 deletions src/ifconfig.configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
var _ = require('lodash');


module.exports = function (cp) {
return function (f) {

};
};
162 changes: 162 additions & 0 deletions src/ifconfig.interfaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';
var _ = require('lodash');

var MAC = 'HWaddr';
var INET = 'inet';
var BCAST = 'Bcast';

module.exports = function (cp) {
return function (f) {
// @todo add command timeout
cp.exec('ifconfig', function (err, ifConfigOut, stderr) {
if (err) {
return f(err);
}

if (stderr) {
return f(stderr);
}

cp.exec('route', function (err, routeOut, stderr) {
if (err) {
return f(err);
}

if (stderr) {
return f(stderr);
}

f(null, parse(ifConfigOut, routeOut));
});
});
};
};


function parse(ifConfigOut, routeOut) {
return ifConfigOut.split('\n\n').map(function (inface) {
var lines = inface.split('\n');

/**
* Format 1
* link xx:xx HWaddr xx-xx-xx
* link xx:xx HWaddr xx:xx:xx
*
* Format 1
* inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx
*/

return {
name: getInterfaceName(_.first(lines)),
ip: getInterfaceIpAddr(lines[1]),
netmask: getInterfaceNetmaskAddr(lines[1]),
broadcast: getBroadcastAddr(lines[1]),
mac: getInterfaceMacAddr(_.first(lines)),
gateway: getGateway(routeOut)
};
});
}

function getInterfaceName(firstLine) {
return _.first(firstLine.split(' '));
}

/**
* extract mac adress
*
* ifconfig output:
* - link xx:xx HWaddr xx-xx-xx
* - link xx:xx HWaddr xx:xx:xx
*
* @param {string} firstLine
* @return {string} Mac address, format: "xx:xx:xx:xx:xx:xx"
*/
function getInterfaceMacAddr(firstLine) {
if (!_.include(firstLine, MAC)) {
return null;
}

var macAddr = _.last(firstLine.split(MAC)).trim().replace(/-/g, ':');

if (macAddr.split(':').length !== 6) {
return null;
}

return macAddr;
}

/**
* extract ip addr
*
* ifconfig output:
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx
*
* @param {string} line
* @return {string,null} xxx.xxx.xxx.xxx
*/
function getInterfaceIpAddr(line) {
if (!_.include(line, INET)) {
return null;
}
return _.first(line.split(':')[1].split(' '));
}

/**
* extract netmask addr
*
* ifconfig output:
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx
*
* @param {string} line
* @return {string,null} xxx.xxx.xxx.xxx
*/
function getInterfaceNetmaskAddr(line) {
if (!_.include(line, INET)) {
return null;
}
return _.last(line.split(':'));
}

/**
* extract broadcast addr
* @param {string} line
* @return {string,null} xxx.xxx.xxx.xxx
*/
function getBroadcastAddr(line) {
if (!_.include(line, BCAST)) {
return null;
}

// inet adr:1.1.1.77 Bcast:1.1.1.255 Masque:1.1.1.0
return _.chain(line)
.split(BCAST)
.slice(1)
.first()
.value()
.substring(1)
.split(' ')[0];
}


/**
* extract gateway ip
* @param {string} stdout
* @return {string,null} default gateway ip or null
*/
function getGateway(stdout) {
// @todo this is ugly.
return _.chain(stdout)
.split('\n')
.filter(function (line) {
return _.include(line, 'default');
})
.first()
.split(' ')
.rest() // without "default"
.join('')
.split('.')
.first()
.trim()
.replace(/-/g, '.')
.value();
}
104 changes: 2 additions & 102 deletions src/ifconfig.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,8 @@
'use strict';
var _ = require('lodash');

module.exports = function (cp) {
return {
interfaces: function (f) {
cp.exec('ifconfig', function (err, stdout, stderr) {
if (err) {
return f(err);
}

if (stderr) {
return f(stderr);
}

f(null, parse(stdout));
});
}
interfaces: require('./ifconfig.interfaces')(cp),
configure: require('./ifconfig.configure')(cp)
};
};


function parse(stdout) {
return stdout.split('\n\n').map(function (inface) {
var lines = inface.split('\n');

/**
* Format 1
* link xx:xx HWaddr xx-xx-xx
* link xx:xx HWaddr xx:xx:xx
*
* Format 1
* inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx
*/

return {
name: getInterfaceName(_.first(lines)),
ip_address: getInterfaceIpAddr(lines[1]),
netmask: getInterfaceNetmaskAddr(lines[1]),
mac_address: getInterfaceMacAddr(_.first(lines))
};
});
}

function getInterfaceName(firstLine) {
return _.first(firstLine.split(' '));
}

/**
* extract mac adress
*
* ifconfig output:
* - link xx:xx HWaddr xx-xx-xx
* - link xx:xx HWaddr xx:xx:xx
*
* @param {string} firstLine
* @return {string} Mac address, format: "xx:xx:xx:xx:xx:xx"
*/
var MAC = 'HWaddr';

function getInterfaceMacAddr(firstLine) {
if (!_.include(firstLine, MAC)) {
return null;
}

var macAddr = _.last(firstLine.split(MAC)).trim().replace(/-/g, ':');

if (macAddr.split(':').length !== 6) {
return null;
}

return macAddr;
}

/**
* extract ip addr
*
* ifconfig output:
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx
*
* @param {string} line
* @return {string} xxx.xxx.xxx.xxx
*/

var INET = 'inet';

function getInterfaceIpAddr(line) {
if (!_.include(line, INET)) {
return null;
}
return _.first(line.split(':')[1].split(' '));
}

/**
* extract netmask addr
*
* ifconfig output:
* - inet xx:xxx.xxx.xxx.xxx mask|masque|...:xxx.xxx.xxx.xxx
*
* @param {string} line
* @return {string} xxx.xxx.xxx.xxx
*/
function getInterfaceNetmaskAddr(line) {
if (!_.include(line, INET)) {
return null;
}
return _.last(line.split(':'));
}
17 changes: 17 additions & 0 deletions test/fixtures/route_get_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Table de routage IP du noyau
Destination Passerelle Genmask Indic Metric Ref Use Iface
10.21.21.112 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.113 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.114 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.100 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.102 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.103 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.104 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.105 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.107 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.109 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.110 * 255.255.255.255 UH 0 0 0 venet0
10.21.21.111 * 255.255.255.255 UH 0 0 0 venet0
10.10.10.0 * 255.255.255.0 U 0 0 0 vmbr0
10.21.21.0 * 255.255.255.0 U 0 0 0 vmbr2
default 10-10-10-1.re 0.0.0.0 UG 0 0 0 vmbr0
4 changes: 4 additions & 0 deletions test/fixtures/route_get_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Table de routage IP du noyau
Destination Passerelle Genmask Indic Metric Ref Use Iface
default 10-10-10-1.po 0.0.0.0 UG 0 0 0 eth0
10.10.10.0 * 255.255.255.0 U 0 0 0 eth0
Loading

0 comments on commit 09d6394

Please sign in to comment.