11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_ci/* eslint-disable no-param-reassign */ 31cb0ef41Sopenharmony_civar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { 41cb0ef41Sopenharmony_ci if (k2 === undefined) k2 = k; 51cb0ef41Sopenharmony_ci var desc = Object.getOwnPropertyDescriptor(m, k); 61cb0ef41Sopenharmony_ci if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { 71cb0ef41Sopenharmony_ci desc = { enumerable: true, get: function() { return m[k]; } }; 81cb0ef41Sopenharmony_ci } 91cb0ef41Sopenharmony_ci Object.defineProperty(o, k2, desc); 101cb0ef41Sopenharmony_ci}) : (function(o, m, k, k2) { 111cb0ef41Sopenharmony_ci if (k2 === undefined) k2 = k; 121cb0ef41Sopenharmony_ci o[k2] = m[k]; 131cb0ef41Sopenharmony_ci})); 141cb0ef41Sopenharmony_civar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { 151cb0ef41Sopenharmony_ci Object.defineProperty(o, "default", { enumerable: true, value: v }); 161cb0ef41Sopenharmony_ci}) : function(o, v) { 171cb0ef41Sopenharmony_ci o["default"] = v; 181cb0ef41Sopenharmony_ci}); 191cb0ef41Sopenharmony_civar __importStar = (this && this.__importStar) || function (mod) { 201cb0ef41Sopenharmony_ci if (mod && mod.__esModule) return mod; 211cb0ef41Sopenharmony_ci var result = {}; 221cb0ef41Sopenharmony_ci if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); 231cb0ef41Sopenharmony_ci __setModuleDefault(result, mod); 241cb0ef41Sopenharmony_ci return result; 251cb0ef41Sopenharmony_ci}; 261cb0ef41Sopenharmony_ciObject.defineProperty(exports, "__esModule", { value: true }); 271cb0ef41Sopenharmony_ciexports.Address4 = void 0; 281cb0ef41Sopenharmony_ciconst common = __importStar(require("./common")); 291cb0ef41Sopenharmony_ciconst constants = __importStar(require("./v4/constants")); 301cb0ef41Sopenharmony_ciconst address_error_1 = require("./address-error"); 311cb0ef41Sopenharmony_ciconst jsbn_1 = require("jsbn"); 321cb0ef41Sopenharmony_ciconst sprintf_js_1 = require("sprintf-js"); 331cb0ef41Sopenharmony_ci/** 341cb0ef41Sopenharmony_ci * Represents an IPv4 address 351cb0ef41Sopenharmony_ci * @class Address4 361cb0ef41Sopenharmony_ci * @param {string} address - An IPv4 address string 371cb0ef41Sopenharmony_ci */ 381cb0ef41Sopenharmony_ciclass Address4 { 391cb0ef41Sopenharmony_ci constructor(address) { 401cb0ef41Sopenharmony_ci this.groups = constants.GROUPS; 411cb0ef41Sopenharmony_ci this.parsedAddress = []; 421cb0ef41Sopenharmony_ci this.parsedSubnet = ''; 431cb0ef41Sopenharmony_ci this.subnet = '/32'; 441cb0ef41Sopenharmony_ci this.subnetMask = 32; 451cb0ef41Sopenharmony_ci this.v4 = true; 461cb0ef41Sopenharmony_ci /** 471cb0ef41Sopenharmony_ci * Returns true if the address is correct, false otherwise 481cb0ef41Sopenharmony_ci * @memberof Address4 491cb0ef41Sopenharmony_ci * @instance 501cb0ef41Sopenharmony_ci * @returns {Boolean} 511cb0ef41Sopenharmony_ci */ 521cb0ef41Sopenharmony_ci this.isCorrect = common.isCorrect(constants.BITS); 531cb0ef41Sopenharmony_ci /** 541cb0ef41Sopenharmony_ci * Returns true if the given address is in the subnet of the current address 551cb0ef41Sopenharmony_ci * @memberof Address4 561cb0ef41Sopenharmony_ci * @instance 571cb0ef41Sopenharmony_ci * @returns {boolean} 581cb0ef41Sopenharmony_ci */ 591cb0ef41Sopenharmony_ci this.isInSubnet = common.isInSubnet; 601cb0ef41Sopenharmony_ci this.address = address; 611cb0ef41Sopenharmony_ci const subnet = constants.RE_SUBNET_STRING.exec(address); 621cb0ef41Sopenharmony_ci if (subnet) { 631cb0ef41Sopenharmony_ci this.parsedSubnet = subnet[0].replace('/', ''); 641cb0ef41Sopenharmony_ci this.subnetMask = parseInt(this.parsedSubnet, 10); 651cb0ef41Sopenharmony_ci this.subnet = `/${this.subnetMask}`; 661cb0ef41Sopenharmony_ci if (this.subnetMask < 0 || this.subnetMask > constants.BITS) { 671cb0ef41Sopenharmony_ci throw new address_error_1.AddressError('Invalid subnet mask.'); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci address = address.replace(constants.RE_SUBNET_STRING, ''); 701cb0ef41Sopenharmony_ci } 711cb0ef41Sopenharmony_ci this.addressMinusSuffix = address; 721cb0ef41Sopenharmony_ci this.parsedAddress = this.parse(address); 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci static isValid(address) { 751cb0ef41Sopenharmony_ci try { 761cb0ef41Sopenharmony_ci // eslint-disable-next-line no-new 771cb0ef41Sopenharmony_ci new Address4(address); 781cb0ef41Sopenharmony_ci return true; 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci catch (e) { 811cb0ef41Sopenharmony_ci return false; 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci } 841cb0ef41Sopenharmony_ci /* 851cb0ef41Sopenharmony_ci * Parses a v4 address 861cb0ef41Sopenharmony_ci */ 871cb0ef41Sopenharmony_ci parse(address) { 881cb0ef41Sopenharmony_ci const groups = address.split('.'); 891cb0ef41Sopenharmony_ci if (!address.match(constants.RE_ADDRESS)) { 901cb0ef41Sopenharmony_ci throw new address_error_1.AddressError('Invalid IPv4 address.'); 911cb0ef41Sopenharmony_ci } 921cb0ef41Sopenharmony_ci return groups; 931cb0ef41Sopenharmony_ci } 941cb0ef41Sopenharmony_ci /** 951cb0ef41Sopenharmony_ci * Returns the correct form of an address 961cb0ef41Sopenharmony_ci * @memberof Address4 971cb0ef41Sopenharmony_ci * @instance 981cb0ef41Sopenharmony_ci * @returns {String} 991cb0ef41Sopenharmony_ci */ 1001cb0ef41Sopenharmony_ci correctForm() { 1011cb0ef41Sopenharmony_ci return this.parsedAddress.map((part) => parseInt(part, 10)).join('.'); 1021cb0ef41Sopenharmony_ci } 1031cb0ef41Sopenharmony_ci /** 1041cb0ef41Sopenharmony_ci * Converts a hex string to an IPv4 address object 1051cb0ef41Sopenharmony_ci * @memberof Address4 1061cb0ef41Sopenharmony_ci * @static 1071cb0ef41Sopenharmony_ci * @param {string} hex - a hex string to convert 1081cb0ef41Sopenharmony_ci * @returns {Address4} 1091cb0ef41Sopenharmony_ci */ 1101cb0ef41Sopenharmony_ci static fromHex(hex) { 1111cb0ef41Sopenharmony_ci const padded = hex.replace(/:/g, '').padStart(8, '0'); 1121cb0ef41Sopenharmony_ci const groups = []; 1131cb0ef41Sopenharmony_ci let i; 1141cb0ef41Sopenharmony_ci for (i = 0; i < 8; i += 2) { 1151cb0ef41Sopenharmony_ci const h = padded.slice(i, i + 2); 1161cb0ef41Sopenharmony_ci groups.push(parseInt(h, 16)); 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci return new Address4(groups.join('.')); 1191cb0ef41Sopenharmony_ci } 1201cb0ef41Sopenharmony_ci /** 1211cb0ef41Sopenharmony_ci * Converts an integer into a IPv4 address object 1221cb0ef41Sopenharmony_ci * @memberof Address4 1231cb0ef41Sopenharmony_ci * @static 1241cb0ef41Sopenharmony_ci * @param {integer} integer - a number to convert 1251cb0ef41Sopenharmony_ci * @returns {Address4} 1261cb0ef41Sopenharmony_ci */ 1271cb0ef41Sopenharmony_ci static fromInteger(integer) { 1281cb0ef41Sopenharmony_ci return Address4.fromHex(integer.toString(16)); 1291cb0ef41Sopenharmony_ci } 1301cb0ef41Sopenharmony_ci /** 1311cb0ef41Sopenharmony_ci * Return an address from in-addr.arpa form 1321cb0ef41Sopenharmony_ci * @memberof Address4 1331cb0ef41Sopenharmony_ci * @static 1341cb0ef41Sopenharmony_ci * @param {string} arpaFormAddress - an 'in-addr.arpa' form ipv4 address 1351cb0ef41Sopenharmony_ci * @returns {Adress4} 1361cb0ef41Sopenharmony_ci * @example 1371cb0ef41Sopenharmony_ci * var address = Address4.fromArpa(42.2.0.192.in-addr.arpa.) 1381cb0ef41Sopenharmony_ci * address.correctForm(); // '192.0.2.42' 1391cb0ef41Sopenharmony_ci */ 1401cb0ef41Sopenharmony_ci static fromArpa(arpaFormAddress) { 1411cb0ef41Sopenharmony_ci // remove ending ".in-addr.arpa." or just "." 1421cb0ef41Sopenharmony_ci const leader = arpaFormAddress.replace(/(\.in-addr\.arpa)?\.$/, ''); 1431cb0ef41Sopenharmony_ci const address = leader.split('.').reverse().join('.'); 1441cb0ef41Sopenharmony_ci return new Address4(address); 1451cb0ef41Sopenharmony_ci } 1461cb0ef41Sopenharmony_ci /** 1471cb0ef41Sopenharmony_ci * Converts an IPv4 address object to a hex string 1481cb0ef41Sopenharmony_ci * @memberof Address4 1491cb0ef41Sopenharmony_ci * @instance 1501cb0ef41Sopenharmony_ci * @returns {String} 1511cb0ef41Sopenharmony_ci */ 1521cb0ef41Sopenharmony_ci toHex() { 1531cb0ef41Sopenharmony_ci return this.parsedAddress.map((part) => (0, sprintf_js_1.sprintf)('%02x', parseInt(part, 10))).join(':'); 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci /** 1561cb0ef41Sopenharmony_ci * Converts an IPv4 address object to an array of bytes 1571cb0ef41Sopenharmony_ci * @memberof Address4 1581cb0ef41Sopenharmony_ci * @instance 1591cb0ef41Sopenharmony_ci * @returns {Array} 1601cb0ef41Sopenharmony_ci */ 1611cb0ef41Sopenharmony_ci toArray() { 1621cb0ef41Sopenharmony_ci return this.parsedAddress.map((part) => parseInt(part, 10)); 1631cb0ef41Sopenharmony_ci } 1641cb0ef41Sopenharmony_ci /** 1651cb0ef41Sopenharmony_ci * Converts an IPv4 address object to an IPv6 address group 1661cb0ef41Sopenharmony_ci * @memberof Address4 1671cb0ef41Sopenharmony_ci * @instance 1681cb0ef41Sopenharmony_ci * @returns {String} 1691cb0ef41Sopenharmony_ci */ 1701cb0ef41Sopenharmony_ci toGroup6() { 1711cb0ef41Sopenharmony_ci const output = []; 1721cb0ef41Sopenharmony_ci let i; 1731cb0ef41Sopenharmony_ci for (i = 0; i < constants.GROUPS; i += 2) { 1741cb0ef41Sopenharmony_ci const hex = (0, sprintf_js_1.sprintf)('%02x%02x', parseInt(this.parsedAddress[i], 10), parseInt(this.parsedAddress[i + 1], 10)); 1751cb0ef41Sopenharmony_ci output.push((0, sprintf_js_1.sprintf)('%x', parseInt(hex, 16))); 1761cb0ef41Sopenharmony_ci } 1771cb0ef41Sopenharmony_ci return output.join(':'); 1781cb0ef41Sopenharmony_ci } 1791cb0ef41Sopenharmony_ci /** 1801cb0ef41Sopenharmony_ci * Returns the address as a BigInteger 1811cb0ef41Sopenharmony_ci * @memberof Address4 1821cb0ef41Sopenharmony_ci * @instance 1831cb0ef41Sopenharmony_ci * @returns {BigInteger} 1841cb0ef41Sopenharmony_ci */ 1851cb0ef41Sopenharmony_ci bigInteger() { 1861cb0ef41Sopenharmony_ci return new jsbn_1.BigInteger(this.parsedAddress.map((n) => (0, sprintf_js_1.sprintf)('%02x', parseInt(n, 10))).join(''), 16); 1871cb0ef41Sopenharmony_ci } 1881cb0ef41Sopenharmony_ci /** 1891cb0ef41Sopenharmony_ci * Helper function getting start address. 1901cb0ef41Sopenharmony_ci * @memberof Address4 1911cb0ef41Sopenharmony_ci * @instance 1921cb0ef41Sopenharmony_ci * @returns {BigInteger} 1931cb0ef41Sopenharmony_ci */ 1941cb0ef41Sopenharmony_ci _startAddress() { 1951cb0ef41Sopenharmony_ci return new jsbn_1.BigInteger(this.mask() + '0'.repeat(constants.BITS - this.subnetMask), 2); 1961cb0ef41Sopenharmony_ci } 1971cb0ef41Sopenharmony_ci /** 1981cb0ef41Sopenharmony_ci * The first address in the range given by this address' subnet. 1991cb0ef41Sopenharmony_ci * Often referred to as the Network Address. 2001cb0ef41Sopenharmony_ci * @memberof Address4 2011cb0ef41Sopenharmony_ci * @instance 2021cb0ef41Sopenharmony_ci * @returns {Address4} 2031cb0ef41Sopenharmony_ci */ 2041cb0ef41Sopenharmony_ci startAddress() { 2051cb0ef41Sopenharmony_ci return Address4.fromBigInteger(this._startAddress()); 2061cb0ef41Sopenharmony_ci } 2071cb0ef41Sopenharmony_ci /** 2081cb0ef41Sopenharmony_ci * The first host address in the range given by this address's subnet ie 2091cb0ef41Sopenharmony_ci * the first address after the Network Address 2101cb0ef41Sopenharmony_ci * @memberof Address4 2111cb0ef41Sopenharmony_ci * @instance 2121cb0ef41Sopenharmony_ci * @returns {Address4} 2131cb0ef41Sopenharmony_ci */ 2141cb0ef41Sopenharmony_ci startAddressExclusive() { 2151cb0ef41Sopenharmony_ci const adjust = new jsbn_1.BigInteger('1'); 2161cb0ef41Sopenharmony_ci return Address4.fromBigInteger(this._startAddress().add(adjust)); 2171cb0ef41Sopenharmony_ci } 2181cb0ef41Sopenharmony_ci /** 2191cb0ef41Sopenharmony_ci * Helper function getting end address. 2201cb0ef41Sopenharmony_ci * @memberof Address4 2211cb0ef41Sopenharmony_ci * @instance 2221cb0ef41Sopenharmony_ci * @returns {BigInteger} 2231cb0ef41Sopenharmony_ci */ 2241cb0ef41Sopenharmony_ci _endAddress() { 2251cb0ef41Sopenharmony_ci return new jsbn_1.BigInteger(this.mask() + '1'.repeat(constants.BITS - this.subnetMask), 2); 2261cb0ef41Sopenharmony_ci } 2271cb0ef41Sopenharmony_ci /** 2281cb0ef41Sopenharmony_ci * The last address in the range given by this address' subnet 2291cb0ef41Sopenharmony_ci * Often referred to as the Broadcast 2301cb0ef41Sopenharmony_ci * @memberof Address4 2311cb0ef41Sopenharmony_ci * @instance 2321cb0ef41Sopenharmony_ci * @returns {Address4} 2331cb0ef41Sopenharmony_ci */ 2341cb0ef41Sopenharmony_ci endAddress() { 2351cb0ef41Sopenharmony_ci return Address4.fromBigInteger(this._endAddress()); 2361cb0ef41Sopenharmony_ci } 2371cb0ef41Sopenharmony_ci /** 2381cb0ef41Sopenharmony_ci * The last host address in the range given by this address's subnet ie 2391cb0ef41Sopenharmony_ci * the last address prior to the Broadcast Address 2401cb0ef41Sopenharmony_ci * @memberof Address4 2411cb0ef41Sopenharmony_ci * @instance 2421cb0ef41Sopenharmony_ci * @returns {Address4} 2431cb0ef41Sopenharmony_ci */ 2441cb0ef41Sopenharmony_ci endAddressExclusive() { 2451cb0ef41Sopenharmony_ci const adjust = new jsbn_1.BigInteger('1'); 2461cb0ef41Sopenharmony_ci return Address4.fromBigInteger(this._endAddress().subtract(adjust)); 2471cb0ef41Sopenharmony_ci } 2481cb0ef41Sopenharmony_ci /** 2491cb0ef41Sopenharmony_ci * Converts a BigInteger to a v4 address object 2501cb0ef41Sopenharmony_ci * @memberof Address4 2511cb0ef41Sopenharmony_ci * @static 2521cb0ef41Sopenharmony_ci * @param {BigInteger} bigInteger - a BigInteger to convert 2531cb0ef41Sopenharmony_ci * @returns {Address4} 2541cb0ef41Sopenharmony_ci */ 2551cb0ef41Sopenharmony_ci static fromBigInteger(bigInteger) { 2561cb0ef41Sopenharmony_ci return Address4.fromInteger(parseInt(bigInteger.toString(), 10)); 2571cb0ef41Sopenharmony_ci } 2581cb0ef41Sopenharmony_ci /** 2591cb0ef41Sopenharmony_ci * Returns the first n bits of the address, defaulting to the 2601cb0ef41Sopenharmony_ci * subnet mask 2611cb0ef41Sopenharmony_ci * @memberof Address4 2621cb0ef41Sopenharmony_ci * @instance 2631cb0ef41Sopenharmony_ci * @returns {String} 2641cb0ef41Sopenharmony_ci */ 2651cb0ef41Sopenharmony_ci mask(mask) { 2661cb0ef41Sopenharmony_ci if (mask === undefined) { 2671cb0ef41Sopenharmony_ci mask = this.subnetMask; 2681cb0ef41Sopenharmony_ci } 2691cb0ef41Sopenharmony_ci return this.getBitsBase2(0, mask); 2701cb0ef41Sopenharmony_ci } 2711cb0ef41Sopenharmony_ci /** 2721cb0ef41Sopenharmony_ci * Returns the bits in the given range as a base-2 string 2731cb0ef41Sopenharmony_ci * @memberof Address4 2741cb0ef41Sopenharmony_ci * @instance 2751cb0ef41Sopenharmony_ci * @returns {string} 2761cb0ef41Sopenharmony_ci */ 2771cb0ef41Sopenharmony_ci getBitsBase2(start, end) { 2781cb0ef41Sopenharmony_ci return this.binaryZeroPad().slice(start, end); 2791cb0ef41Sopenharmony_ci } 2801cb0ef41Sopenharmony_ci /** 2811cb0ef41Sopenharmony_ci * Return the reversed ip6.arpa form of the address 2821cb0ef41Sopenharmony_ci * @memberof Address4 2831cb0ef41Sopenharmony_ci * @param {Object} options 2841cb0ef41Sopenharmony_ci * @param {boolean} options.omitSuffix - omit the "in-addr.arpa" suffix 2851cb0ef41Sopenharmony_ci * @instance 2861cb0ef41Sopenharmony_ci * @returns {String} 2871cb0ef41Sopenharmony_ci */ 2881cb0ef41Sopenharmony_ci reverseForm(options) { 2891cb0ef41Sopenharmony_ci if (!options) { 2901cb0ef41Sopenharmony_ci options = {}; 2911cb0ef41Sopenharmony_ci } 2921cb0ef41Sopenharmony_ci const reversed = this.correctForm().split('.').reverse().join('.'); 2931cb0ef41Sopenharmony_ci if (options.omitSuffix) { 2941cb0ef41Sopenharmony_ci return reversed; 2951cb0ef41Sopenharmony_ci } 2961cb0ef41Sopenharmony_ci return (0, sprintf_js_1.sprintf)('%s.in-addr.arpa.', reversed); 2971cb0ef41Sopenharmony_ci } 2981cb0ef41Sopenharmony_ci /** 2991cb0ef41Sopenharmony_ci * Returns true if the given address is a multicast address 3001cb0ef41Sopenharmony_ci * @memberof Address4 3011cb0ef41Sopenharmony_ci * @instance 3021cb0ef41Sopenharmony_ci * @returns {boolean} 3031cb0ef41Sopenharmony_ci */ 3041cb0ef41Sopenharmony_ci isMulticast() { 3051cb0ef41Sopenharmony_ci return this.isInSubnet(new Address4('224.0.0.0/4')); 3061cb0ef41Sopenharmony_ci } 3071cb0ef41Sopenharmony_ci /** 3081cb0ef41Sopenharmony_ci * Returns a zero-padded base-2 string representation of the address 3091cb0ef41Sopenharmony_ci * @memberof Address4 3101cb0ef41Sopenharmony_ci * @instance 3111cb0ef41Sopenharmony_ci * @returns {string} 3121cb0ef41Sopenharmony_ci */ 3131cb0ef41Sopenharmony_ci binaryZeroPad() { 3141cb0ef41Sopenharmony_ci return this.bigInteger().toString(2).padStart(constants.BITS, '0'); 3151cb0ef41Sopenharmony_ci } 3161cb0ef41Sopenharmony_ci /** 3171cb0ef41Sopenharmony_ci * Groups an IPv4 address for inclusion at the end of an IPv6 address 3181cb0ef41Sopenharmony_ci * @returns {String} 3191cb0ef41Sopenharmony_ci */ 3201cb0ef41Sopenharmony_ci groupForV6() { 3211cb0ef41Sopenharmony_ci const segments = this.parsedAddress; 3221cb0ef41Sopenharmony_ci return this.address.replace(constants.RE_ADDRESS, (0, sprintf_js_1.sprintf)('<span class="hover-group group-v4 group-6">%s</span>.<span class="hover-group group-v4 group-7">%s</span>', segments.slice(0, 2).join('.'), segments.slice(2, 4).join('.'))); 3231cb0ef41Sopenharmony_ci } 3241cb0ef41Sopenharmony_ci} 3251cb0ef41Sopenharmony_ciexports.Address4 = Address4; 3261cb0ef41Sopenharmony_ci//# sourceMappingURL=ipv4.js.map