-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv6.js
50 lines (46 loc) · 1.92 KB
/
ipv6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Calculates an IPv4-mapped IPv6 address.
* @param {string} ipv4 - An IPv4 address in dotted-quad format.
* @return {*} (ipv6Address) - An IPv6 address string or null if a run-time problem was detected.
*/
function getIpv4MappedIpv6Address(ipv4) {
// Initialize return argument
let ipv6Address = null;
// Prepare to derive a Hex version of the dotted-quad decimal IPv4 address.
// Split the IPv4 address into its four parts.
let ipv4Quads = ipv4.split('.');
// Count the number of parts found.
let numIpv4Segments = ipv4Quads.length;
// Verify IPv4 had four parts.
if (numIpv4Segments === 4) {
let validQuads = true;
// Iterate over the IPv4 address parts and verify each segment was a number between 0 and 255.
for(let i=0; i < numIpv4Segments; i++) {
if( isNaN(Number(ipv4Quads[i])) || ipv4Quads[i] < 0 || ipv4Quads[i] > 255 ) {
validQuads = false;
}
}
// Passed IPv4 is valid. Now to derive an IPv4-mapped IPv6 address.
if (validQuads) {
// Hardcode the prefix. During refactor, we might want to make the prefix a const.
ipv6Address = "0:0:0:0:0:ffff:";
// Iterate over the IPv4 parts
for(let i=0; i < numIpv4Segments; i++) {
// Convert part to an integer, then convert to a hex string using method toString()
// with a base 16 (hex) encoding.
let hexString = parseInt(ipv4Quads[i]).toString(16);
// If hex is odd (single digit), prepend a '0'. This is why we wanted to work with a string.
if (hexString.length % 2)
hexString = '0' + hexString;
// Append hex part to evolving variable ipv6Address.
ipv6Address = ipv6Address + hexString;
// Add a colon to split the encoded address and match the IPv6 format.
if(i===1) {
ipv6Address = ipv6Address + ':';
}
}
}
}
return ipv6Address;
}
module.exports.getIpv4MappedIpv6Address = getIpv4MappedIpv6Address;