Software Networking with Node.js

Marty Jacobs · Feb 21, 2019 00:00 · 575 words · 3 minute read

Software-defined Networking is an emerging field with lots of room for innovation. There has been many companies, cough BitTorrent cough, which have really managed to wrangle this topic to create great Software. Take Twitch for example, a live video gaming streaming company, requiring heavy use of strong computer networks to relay live streaming data. Developers and organisations are getting behind SDN and Node.js to build interoperability between Software applications.

In this post, we will build a Software-defined Networking application, showing how easy the Node.js build process really is. We are blown away by how fast it was to develop the app from idea, right through to delivery. Node.js is an excellent framework choice for a number of reasons, they are:

  • Interoperability between Software Applications
  • Configuration of the Network Layer from the Application Layer
  • Easy to publish updates and allows for Application versioning
  • Increases Build Velocity supporting different types of Applications

Getting Started with Node.js

Get up and running with Node.js by downloading the latest version:

https://nodejs.org/en/download/

Creating a Node Project

Create a empty directory:

mkdir networking-module

Create a new node project:

cd networking-module
npm init

Creating a Networking Module

To create a networking module, we need to write Javascript code and upload it to npm.

Below we have written a simple application to display the TCP/IP Networking Configuration information of a host machine.

This information is quite useful in diagnosing network problems and outages.

It provides information based off the local host machine, such as the IP address, type of Network running, Subnet mask etc.

'use strict';

var os = require('os');
var ifaces = os.networkInterfaces();

exports.displayNetworkInfo = function() {

 Object.keys(ifaces).forEach(function(ifname) {
  var alias = 0;

  ifaces[ifname].forEach(function(iface) {
    if ('IPv4' !== iface.family || iface.internal !== false) {
      return;
    }

    if (alias >= 1) {
        //many hosts
      console.log();
      console.log('Network information...');
      console.log();
      console.log('Network type:' + ifname);
      console.log('Local IP:' + iface.address);
      console.log('IP Version: ' + iface.family);
      console.log('Mac address v6:' + os.networkInterfaces()[ifname][0].address);
      console.log('Subnet Mask:' + os.networkInterfaces()[ifname][1].netmask);
    } else {
        //one host
      console.log();
      console.log('Network information...');
      console.log();
      console.log('Network type: ' + ifname);
      console.log('Local IP: ' + iface.address);
      console.log('IP Version: ' + iface.family);
      console.log('Mac address v6: ' + os.networkInterfaces()[ifname][0].address);
      console.log('Subnet Mask: ' + os.networkInterfaces()[ifname][1].netmask);
    }
    ++alias;
  });
});
}

You can view the full source code here.

4. Publishing a Module to NPM

To upload a Networking Module, an account is required to signup to the NPM registry.

https://www.npmjs.com/signup

Once signed up, we can log in using the terminal to the account created.

npm login

After logging in, you’re ready to publish!

npm publish

5. Testing Modules

When testing NPM modules, you can think of it like someone else is now using your Software Networking application in their project. We will create a test project and “test” the functionality, as if someone else was using your Software.

mkdir TestProject
cd TestProject

Create the test node project:

npm init

Install your created module:

npm install network-info
var testmodule = require('network-info')
testmodule.displayNetworkInfo();

Execute the test script:

node index.js  //inside the TestProject directory

After running the test script, the following output will show:

//Network information...
 
//Network type: _____
//Local IP: _____
//IP Version: _____
//Mac address v6: _____
//Subnet Mask: _____


Thanks for reading! I hope you enjoyed this article on creating a Software-defined Networking module in Node.js. The repo has been published to node registry. We suggest picking up a copy of SDN: Software Defined Networks: An Authoritative Review of Network Programmability Technologies for a detailed guide into Software-defined Networking.

Happy coding!

All the best,