A utility library to manipulate IP addresses.
To install this package, type following command in your terminal:
npm install xrtlibrary-ip-utilities --save
And then, you can import this package in your NodeJS environment with following "require" statement.
var XRTLibIPUtilities = require("xrtlibrary-ip-utilities");
In this library, we provide three API that can load a string-formed IP address, see following:
try {
// Load IPv4 address.
var ip1 = XRTLibIPUtilities.LoadIPv4("192.168.0.1");
// Load IPv6 address.
var ip2 = XRTLibIPUtilities.LoadIPv6("::abcd:efgh");
// Load whatever IPvX (X = 4/6).
var ip3 = XRTLibIPUtilities.LoadAny("127.0.0.1");
var ip4 = XRTLibIPUtilities.LoadAny("::abcd:efgh");
var ip5 = XRTLibIPUtilities.LoadAny("::ffff:192.168.0.1");
var ip6 = XRTLibIPUtilities.LoadAny("255.255.255.0");
} catch(error) {
// If the address is error, an error will be raised.
process.exit()
}
To compare whether two addresses are equal, use "isEqual()". See following:
console.log(ip1.isEqual(ip1)); // true.
console.log(ip1.isEqual(ip2)); // false.
console.log(ip1.isEqual(ip5)); // true.
To stringify a parsed IP address, use "toString()" method. See following:
console.log(ip1.toString());
console.log(ip2.toString());
console.log(ip3.toString());
console.log(ip4.toString());
console.log(ip5.toString());
To get whether an IP address is IPv4 or IPv6, use "getFamily()" method. See following:
if (ip1.getFamily() == XRTLibIPUtilities.Family.AF_INET) {
console.log("IP 1 is IPv4 address.");
} else if (ip1.getFamily() == XRTLibIPUtilities.Family.AF_INET6) {
console.log("IP 1 is IPv6 address.");
} else {
console.log("Unknown address family.");
}
To mask an address with a mask address, use "mask()" method. See following:
console.log(ip1.mask(ip6).toString()); // "192.168.0.0".
To mask an address with a prefix, use "maskByPrefix()" method. See following:
console.log(ip1.maskByPrefix(24).toString()); // "192.168.0.0".
To get the 32-bit hash code of an IP address, use "getHashCode()" method. See following:
console.log(ip1.getHashCode());
To get whether an IP address is bound to a local interface, use "isLocalInterfaceAddress()" method. See following:
console.log(ip3.isLocalInterfaceAddress()); // Generally returns true.
console.log(INetIP.LoadAny("::1").isLocalInterfaceAddress()); // Generally returns true.
console.log(INetIP.LoadAny("0:0::1").isLocalInterfaceAddress()); // The same as previous.
Unstable now, so inspect the API with your Node.JS console. After our API becomes stable, we will release the API table.