Fixed a bug in IPv6 parser: The machine architecture will no longer effect IPv6 address parsing
This commit is contained in:
@@ -242,14 +242,13 @@ export function parseIPv4(d) {
|
|||||||
*
|
*
|
||||||
* @param {string} d IP address
|
* @param {string} d IP address
|
||||||
*
|
*
|
||||||
* @returns {Uint16Array} Parsed IPv6 Address
|
* @returns {Uint8Array} Parsed IPv6 Address
|
||||||
*
|
*
|
||||||
* @throws {Exception} When the given ip address was not an IPv6 addr
|
* @throws {Exception} When the given ip address was not an IPv6 addr
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export function parseIPv6(d) {
|
export function parseIPv6(d) {
|
||||||
const addrSeg = 8;
|
const addrSeg = 8;
|
||||||
|
|
||||||
let s = d.split(":");
|
let s = d.split(":");
|
||||||
|
|
||||||
if (s.length > addrSeg || s.length <= 1) {
|
if (s.length > addrSeg || s.length <= 1) {
|
||||||
@@ -258,41 +257,33 @@ export function parseIPv6(d) {
|
|||||||
|
|
||||||
if (s[0].charAt(0) === "[") {
|
if (s[0].charAt(0) === "[") {
|
||||||
s[0] = s[0].substring(1, s[0].length);
|
s[0] = s[0].substring(1, s[0].length);
|
||||||
|
|
||||||
let end = s.length - 1;
|
let end = s.length - 1;
|
||||||
|
|
||||||
if (s[end].charAt(s[end].length - 1) !== "]") {
|
if (s[end].charAt(s[end].length - 1) !== "]") {
|
||||||
throw new Exception("Invalid address");
|
throw new Exception("Invalid address");
|
||||||
}
|
}
|
||||||
|
|
||||||
s[end] = s[end].substring(0, s[end].length - 1);
|
s[end] = s[end].substring(0, s[end].length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let r = new Uint16Array(addrSeg),
|
let r = new Uint8Array(addrSeg * 2),
|
||||||
rIndexShift = 0;
|
rIndexShift = 0;
|
||||||
|
|
||||||
for (let i = 0; i < s.length; i++) {
|
for (let i = 0; i < s.length; i++) {
|
||||||
if (s[i].length <= 0) {
|
if (s[i].length <= 0) {
|
||||||
rIndexShift = addrSeg - s.length;
|
rIndexShift = addrSeg - s.length;
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isHex(s[i])) {
|
if (!isHex(s[i])) {
|
||||||
throw new Exception("Invalid address");
|
throw new Exception("Invalid address");
|
||||||
}
|
}
|
||||||
|
|
||||||
let ii = parseInt(s[i], 16); // Only support hex
|
let ii = parseInt(s[i], 16); // Only support hex
|
||||||
|
|
||||||
if (isNaN(ii)) {
|
if (isNaN(ii)) {
|
||||||
throw new Exception("Invalid address");
|
throw new Exception("Invalid address");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ii > 0xffff) {
|
if (ii > 0xffff) {
|
||||||
throw new Exception("Invalid address");
|
throw new Exception("Invalid address");
|
||||||
}
|
}
|
||||||
|
let j = (rIndexShift + i) * 2;
|
||||||
r[rIndexShift + i] = ii;
|
r[j] = ii >> 8;
|
||||||
|
r[j + 1] = ii & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
|
|||||||
@@ -77,46 +77,65 @@ describe("Common", () => {
|
|||||||
{
|
{
|
||||||
sample: "2001:db8:1f70:0:999:de8:7648:6e8",
|
sample: "2001:db8:1f70:0:999:de8:7648:6e8",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([
|
expected: new Uint8Array([
|
||||||
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
|
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
|
||||||
|
0x76, 0x48, 0x6, 0xe8,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "2001:db8:85a3::8a2e:370:7334",
|
sample: "2001:db8:85a3::8a2e:370:7334",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([
|
expected: new Uint8Array([
|
||||||
0x2001, 0xdb8, 0x85a3, 0x0, 0x0, 0x8a2e, 0x370, 0x7334,
|
0x20, 0x01, 0xd, 0xb8, 0x85, 0xa3, 0x0, 0x0, 0x0, 0x0, 0x8a, 0x2e,
|
||||||
|
0x3, 0x70, 0x73, 0x34,
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
sample: "fdef:90fb:4138::8ca",
|
||||||
|
expectingFailure: false,
|
||||||
|
expected: new Uint8Array([
|
||||||
|
0xfd, 0xef, 0x90, 0xfb, 0x41, 0x38, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||||
|
0x0, 0x8, 0xca,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "::1",
|
sample: "::1",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x01]),
|
expected: new Uint8Array([
|
||||||
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||||
|
0x0, 0x1,
|
||||||
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "::",
|
sample: "::",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x00]),
|
expected: new Uint8Array([
|
||||||
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||||
|
0x0, 0x0,
|
||||||
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "2001:db8:1f70::999:de8:7648:6e8",
|
sample: "2001:db8:1f70::999:de8:7648:6e8",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([
|
expected: new Uint8Array([
|
||||||
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
|
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
|
||||||
|
0x76, 0x48, 0x6, 0xe8,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "2001:0db8:ac10:fe01::",
|
sample: "2001:0db8:ac10:fe01::",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([
|
expected: new Uint8Array([
|
||||||
0x2001, 0x0db8, 0xac10, 0xfe01, 0x0, 0x0, 0x0, 0x0,
|
0x20, 0x01, 0x0d, 0xb8, 0xac, 0x10, 0xfe, 0x01, 0x0, 0x0, 0x0, 0x0,
|
||||||
|
0x0, 0x0, 0x0, 0x0,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "::7f00:1",
|
sample: "::7f00:1",
|
||||||
expectingFailure: false,
|
expectingFailure: false,
|
||||||
expected: new Uint16Array([
|
expected: new Uint8Array([
|
||||||
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x7f00, 0x0001,
|
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7f,
|
||||||
|
0x00, 0x00, 0x01,
|
||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -199,21 +218,19 @@ describe("Common", () => {
|
|||||||
{
|
{
|
||||||
sample: "2001:db8:1f70::999:de8:7648:6e8",
|
sample: "2001:db8:1f70::999:de8:7648:6e8",
|
||||||
expectedType: "IPv6",
|
expectedType: "IPv6",
|
||||||
expectedAddr: new Uint8Array(
|
expectedAddr: new Uint8Array([
|
||||||
new Uint16Array([
|
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
|
||||||
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
|
0x76, 0x48, 0x6, 0xe8,
|
||||||
]).buffer
|
]),
|
||||||
),
|
|
||||||
expectedPort: 22,
|
expectedPort: 22,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sample: "[2001:db8:1f70::999:de8:7648:6e8]:100",
|
sample: "[2001:db8:1f70::999:de8:7648:6e8]:100",
|
||||||
expectedType: "IPv6",
|
expectedType: "IPv6",
|
||||||
expectedAddr: new Uint8Array(
|
expectedAddr: new Uint8Array([
|
||||||
new Uint16Array([
|
0x20, 0x01, 0xd, 0xb8, 0x1f, 0x70, 0x0, 0x0, 0x9, 0x99, 0xd, 0xe8,
|
||||||
0x2001, 0xdb8, 0x1f70, 0x0, 0x999, 0xde8, 0x7648, 0x6e8,
|
0x76, 0x48, 0x6, 0xe8,
|
||||||
]).buffer
|
]),
|
||||||
),
|
|
||||||
expectedPort: 100,
|
expectedPort: 100,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user