vlsmcalc
v2.0.1
Published
Lib to calc Variable Length Subnet Masking
Readme
vlsmcalc
vlsmcalc is a library designed for network administrators and developers to easily calculate Variable Length Subnet Masking (VLSM). It helps you generate optimized subnets for a given major network, considering specific host requirements for each subnet.
Table of Contents
Features
- Dynamically calculates subnets based on host requirements for each subnet.
- Computes subnet masks, network addresses, broadcast addresses, and the first and last usable IPs.
- Validates input major networks and ensures subnet creation is possible.
- Efficient subnetting: Optimizes network allocation by calculating the smallest subnet mask that fits the host requirements for each subnet, reducing IP wastage.
- Scalability: Supports creating subnets for networks with large IP space and varying host requirements, making it suitable for both small and large networks.
- Validation: Automatically checks for errors in input, such as insufficient IP space or invalid CIDR notation.
Prerequisites
Before using vlsmcalc, ensure that you have:
- Node.js (version 12 or higher) installed. Download Node.js
- A package manager such as npm or yarn:
- To install npm, download Node.js from nodejs.org.
- To install yarn, follow the instructions on Yarn's website.
Installation
To install the library, you can use Yarn or npm:
Using Yarn:
yarn add vlsmcalcUsing npm:
npm install vlsmcalcLocal Development Setup
To get started with local development, follow these steps:
- Clone the repository:
git clone https://github.com/yourusername/project-name.git- Install the project dependencies:
yarn install- Run the tests:
yarn testUsage
Here's an example of how to use vlsmcalc:
const Subnet = require('vlsmcalc');
// Define the major network and hosts per subnet
const majorNetwork = '192.168.1.0/24';
const hostsEachSubnet = [50, 20, 10];
// Instantiate the Subnet class with host requirements and major network
const subnetCalc = new Subnet(hostsEachSubnet, majorNetwork);
// Validate if subnetting is possible
if (subnetCalc.isValid()) {
const networks = subnetCalc.getNetworks();
networks.forEach((network, index) => {
console.log(`Subnet ${index + 1}:`);
console.log(` Network Address: ${network.getNetwork()}`);
console.log(` Subnet Mask: ${network.getSubnetMask()}`);
console.log(` Broadcast Address: ${network.getBroadcast()}`);
console.log(` First Usable IP: ${network.getFirstIP()}`);
console.log(` Last Usable IP: ${network.getLastIP()}`);
console.log(` Prefix: /${network.getPrefix()}`);
console.log(` Allocated Size: ${network.getAllocatedSize()} hosts`);
});
} else {
console.log('Invalid major network or insufficient space for requested subnets.');
}
Error Handling
For improved reliability, you can include error handling to catch invalid inputs or unexpected issues:
try {
if (subnetCalc.isValid()) {
const networks = subnetCalc.getNetworks();
// Continue as above...
} else {
console.log('Invalid major network or insufficient space for requested subnets.');
}
} catch (error) {
console.error('Error during subnet calculation:', error);
}Examples
Subnetting Example
const VLSMCalc = require('vlsmcalc');
let hostsEachSubnet = [500, 250, 100, 50]; // Hosts required for each subnet
let majorNetwork = '192.168.0.0/24'; // Major network in CIDR notation
// Create subnet calculation
let subnet = new VLSMCalc(hostsEachSubnet, majorNetwork);
// Get networks (subnets)
let networks = subnet.getNetworks();
// Output network details
networks.forEach(network => {
console.log(`Network: ${network.getNetwork()}`);
console.log(`Subnet Mask: ${network.getSubnetMask()}`);
console.log(`First IP: ${network.getFirstIP()}`);
console.log(`Last IP: ${network.getLastIP()}`);
console.log(`Broadcast: ${network.getBroadcast()}`);
console.log(`---------------------------------`);
});Expected Output:
Network: 192.168.0.0
Subnet Mask: 255.255.255.0
First IP: 192.168.0.1
Last IP: 192.168.0.254
Broadcast: 192.168.0.255
---------------------------------
Network: 192.168.1.0
Subnet Mask: 255.255.255.128
First IP: 192.168.1.1
Last IP: 192.168.1.126
Broadcast: 192.168.1.127
---------------------------------
Network: 192.168.1.128
Subnet Mask: 255.255.255.192
First IP: 192.168.1.129
Last IP: 192.168.1.190
Broadcast: 192.168.1.191
---------------------------------
Network: 192.168.1.192
Subnet Mask: 255.255.255.224
First IP: 192.168.1.193
Last IP: 192.168.1.222
Broadcast: 192.168.1.223
---------------------------------Calculating Next Network
const { Network } = require('vlsmcalc');
let majorNetwork = '192.168.0.0/24';
let network = new Network(500, majorNetwork);
// Get the next available network after allocation
console.log('Next Network:', network.getNextNetwork());Next Network: 192.168.1.0Contributing
We welcome contributions to this project! If you find any issues or have improvements to suggest, feel free to open an issue or submit a pull request.
Before you contribute, please ensure you check the existing issues to avoid duplicates.
To contribute, follow these steps:
- Fork the repository.
- Clone your fork to your local machine.
- Create a new branch for your changes.
- Make your changes and add tests if applicable.
- Run the tests to ensure everything works:
yarn test - Create a pull request with a description of your changes.
Your contributions help improve this project, and we appreciate your effort!
Repository
Find the source code and report issues at: GitHub Repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Created by Marco De Araujo. For more details, visit marcodearaujo.com or connect with me on LinkedIn.
