UNPKG

ip-navigator

Version:

A tool for IP address manipulation and calculation

166 lines 7.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSubnetInfo = exports.calculateAvailableIPs = exports.calculateBroadcastAddress = exports.calculateNetworkAddress = void 0; var conversion_1 = require("../conversion"); var conversion_2 = require("../conversion"); var validation_1 = require("../validation"); var conversion_3 = require("../conversion"); /** * Calculates the network address based on an IP address and subnet mask. * * This function performs a bitwise AND operation between the IP address and subnet mask * to determine the network address. * * @param {string} ipAddress - The IP address. * @param {string} subnetMask - The subnet mask. * @returns {string} The network address. * * @example * calculateNetworkAddress('192.168.1.100', '255.255.255.0'); // returns '192.168.1.0' * * @remarks * - The function assumes both inputs are valid IPv4 addresses in dotted decimal notation. * - It performs a bitwise AND operation between each octet of the IP and subnet mask. * * */ var calculateNetworkAddress = function (ipAddress, subnetMask) { if (!(0, validation_1.isValidIPAddress)(ipAddress) || !(0, validation_1.isValidSubnetMask)(subnetMask)) { throw new Error("Invalid IP address or subnet mask"); } var ipBinaryOctets = (0, conversion_3.ipToBinary)(ipAddress).split("."); var maskBinaryOctets = (0, conversion_3.ipToBinary)(subnetMask).split("."); var networkBinary = ipBinaryOctets .map(function (octet, index) { var number_1 = parseInt(octet, 2); var number_2 = parseInt(maskBinaryOctets[index], 2); return (number_1 & number_2).toString(2).padStart(8, "0"); }) .join("."); return (0, conversion_3.binaryToIP)(networkBinary); }; exports.calculateNetworkAddress = calculateNetworkAddress; /** * Calculates the broadcast address based on an IP address and subnet mask. * * This function determines the broadcast address by setting all host bits to 1 * in the network portion defined by the subnet mask. * * @param {string} ipAddress - The IP address. * @param {string} subnetMask - The subnet mask. * @returns {string} The broadcast address. * * @example * calculateBroadcastAddress('192.168.1.100', '255.255.255.0'); // returns '192.168.1.255' * * @remarks * - The function assumes both inputs are valid IPv4 addresses in dotted decimal notation. * - It calculates the network address and then sets all host bits to 1. */ var calculateBroadcastAddress = function (ipAddress, subnetMask) { if (!(0, validation_1.isValidIPAddress)(ipAddress) || !(0, validation_1.isValidSubnetMask)(subnetMask)) { throw new Error("Invalid IP address or subnet mask"); } var networkAddress = (0, exports.calculateNetworkAddress)(ipAddress, subnetMask); var networkBinary = (0, conversion_3.ipToBinary)(networkAddress); var maskBinary = (0, conversion_3.ipToBinary)(subnetMask); var broadcastBinary = networkBinary .split(".") .map(function (octet, index) { var maskOctet = maskBinary.split(".")[index]; return parseInt(octet, 2) | (parseInt(maskOctet, 2) ^ 255); }) .map(function (num) { return num.toString(2).padStart(8, "0"); }) .join("."); return (0, conversion_3.binaryToIP)(broadcastBinary); }; exports.calculateBroadcastAddress = calculateBroadcastAddress; /** * Calculates the available IP addresses in a subnet. * * This function returns an array of all usable IP addresses in the subnet, * excluding the network address and broadcast address. * * @param {string} networkAddress - The network address of the subnet. * @param {string} subnetMask - The subnet mask. * @returns {string[]} An array of available IP addresses. * * @example * calculateAvailableIPs('192.168.1.0', '255.255.255.252'); * // returns ['192.168.1.1', '192.168.1.2'] * * @remarks * - The function assumes both inputs are valid IPv4 addresses in dotted decimal notation. * - It excludes the network address (first address) and broadcast address (last address) from the results. * - For very large subnets, be aware of potential memory usage when generating the full list. */ var calculateAvailableIPs = function (networkAddress, subnetMask) { if (!(0, validation_1.isValidIPAddress)(networkAddress) || !(0, validation_1.isValidSubnetMask)(subnetMask)) { throw new Error("Invalid network address or subnet mask"); } var broadcastAddress = (0, exports.calculateBroadcastAddress)(networkAddress, subnetMask); var startIP = (0, conversion_2.ipToInteger)(networkAddress) + 1; var endIP = (0, conversion_2.ipToInteger)(broadcastAddress) - 1; var availableIPs = []; for (var i = startIP; i <= endIP; i++) { availableIPs.push((0, conversion_2.integerToIP)(i)); } return availableIPs; }; exports.calculateAvailableIPs = calculateAvailableIPs; /** * Retrieves comprehensive information about a subnet. * * This function calculates various details about a subnet given an IP address and subnet mask. * * @param {string} ipAddress - An IP address within the subnet. * @param {string} subnetMask - The subnet mask. * @returns {Object} An object containing subnet information. * @returns {string} .networkAddress - The network address of the subnet. * @returns {string} .broadcastAddress - The broadcast address of the subnet. * @returns {number} .totalHosts - The total number of host addresses in the subnet. * @returns {number} .usableHosts - The number of usable host addresses in the subnet. * @returns {string} .firstUsableHost - The first usable host address in the subnet. * @returns {string} .lastUsableHost - The last usable host address in the subnet. * * @example * getSubnetInfo('192.168.1.100', '255.255.255.0'); * // returns { * // networkAddress: '192.168.1.0', * // broadcastAddress: '192.168.1.255', * // totalHosts: 256, * // usableHosts: 254, * // firstUsableHost: '192.168.1.1', * // lastUsableHost: '192.168.1.254' * // } * * @remarks * - The function assumes both inputs are valid IPv4 addresses in dotted decimal notation. * - Total hosts include the network and broadcast addresses, while usable hosts exclude these. * - For subnets smaller than /31, firstUsableHost and lastUsableHost will be the same as networkAddress and broadcastAddress. */ var getSubnetInfo = function (ipAddress, subnetMask) { if (!(0, validation_1.isValidIPAddress)(ipAddress) || !(0, validation_1.isValidSubnetMask)(subnetMask)) { throw new Error("Invalid IP address or subnet mask"); } var networkAddress = (0, exports.calculateNetworkAddress)(ipAddress, subnetMask); var broadcastAddress = (0, exports.calculateBroadcastAddress)(ipAddress, subnetMask); var cidr = (0, conversion_1.subnetMaskToCIDR)(subnetMask); var totalHosts = Math.pow(2, 32 - cidr); var usableHosts = cidr === 31 ? 2 : totalHosts > 2 ? totalHosts - 2 : 0; var availableIPs = (0, exports.calculateAvailableIPs)(networkAddress, subnetMask); var firstUsableHost = availableIPs.length > 0 ? availableIPs[0] : networkAddress; var lastUsableHost = availableIPs.length > 0 ? availableIPs[availableIPs.length - 1] : broadcastAddress; return { networkAddress: networkAddress, broadcastAddress: broadcastAddress, totalHosts: totalHosts, usableHosts: usableHosts, firstUsableHost: firstUsableHost, lastUsableHost: lastUsableHost, }; }; exports.getSubnetInfo = getSubnetInfo; //# sourceMappingURL=index.js.map