Skip to content

Commit 5715081

Browse files
authored
Merge pull request #1960 from murgatroid99/grpc-js_channelz_ipv6_fix
grpc-js: channelz: Fix algorithm for representing an IPv6 address in binary
2 parents 78466ac + b1be84a commit 5715081

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

packages/grpc-js/src/channelz.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,31 @@ export function unregisterChannelzRef(ref: ChannelRef | SubchannelRef | ServerRe
392392
}
393393
}
394394

395+
/**
396+
* Parse a single section of an IPv6 address as two bytes
397+
* @param addressSection A hexadecimal string of length up to 4
398+
* @returns The pair of bytes representing this address section
399+
*/
400+
function parseIPv6Section(addressSection: string): [number, number] {
401+
const numberValue = Number.parseInt(addressSection, 16);
402+
return [numberValue / 256 | 0, numberValue % 256];
403+
}
404+
405+
/**
406+
* Parse a chunk of an IPv6 address string to some number of bytes
407+
* @param addressChunk Some number of segments of up to 4 hexadecimal
408+
* characters each, joined by colons.
409+
* @returns The list of bytes representing this address chunk
410+
*/
411+
function parseIPv6Chunk(addressChunk: string): number[] {
412+
if (addressChunk === '') {
413+
return [];
414+
}
415+
const bytePairs = addressChunk.split(':').map(section => parseIPv6Section(section));
416+
const result: number[] = [];
417+
return result.concat(...bytePairs);
418+
}
419+
395420
/**
396421
* Converts an IPv4 or IPv6 address from string representation to binary
397422
* representation
@@ -403,17 +428,17 @@ function ipAddressStringToBuffer(ipAddress: string): Buffer | null {
403428
return Buffer.from(Uint8Array.from(ipAddress.split('.').map(segment => Number.parseInt(segment))));
404429
} else if (isIPv6(ipAddress)) {
405430
let leftSection: string;
406-
let rightSection: string | null;
431+
let rightSection: string;
407432
const doubleColonIndex = ipAddress.indexOf('::');
408433
if (doubleColonIndex === -1) {
409434
leftSection = ipAddress;
410-
rightSection = null;
435+
rightSection = '';
411436
} else {
412437
leftSection = ipAddress.substring(0, doubleColonIndex);
413438
rightSection = ipAddress.substring(doubleColonIndex + 2);
414439
}
415-
const leftBuffer = Uint8Array.from(leftSection.split(':').map(segment => Number.parseInt(segment, 16)));
416-
const rightBuffer = rightSection ? Uint8Array.from(rightSection.split(':').map(segment => Number.parseInt(segment, 16))) : new Uint8Array();
440+
const leftBuffer = Buffer.from(parseIPv6Chunk(leftSection));
441+
const rightBuffer = Buffer.from(parseIPv6Chunk(rightSection));
417442
const middleBuffer = Buffer.alloc(16 - leftBuffer.length - rightBuffer.length, 0);
418443
return Buffer.concat([leftBuffer, middleBuffer, rightBuffer]);
419444
} else {

0 commit comments

Comments
 (0)