Skip to content

Commit

Permalink
Represent IPv6-mapped IPv4 addresses as IPv4 in channelz
Browse files Browse the repository at this point in the history
  • Loading branch information
murgatroid99 committed Feb 25, 2025
1 parent 822af68 commit 36c9a4f
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/grpc-js/src/channelz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,23 @@ function parseIPv6Chunk(addressChunk: string): number[] {
return result.concat(...bytePairs);
}

function isIPv6MappedIPv4(ipAddress: string) {
return isIPv6(ipAddress) && ipAddress.toLowerCase().startsWith('::ffff:') && isIPv4(ipAddress.substring(7));
}

/**
* Prerequisite: isIPv4(ipAddress)
* @param ipAddress
* @returns
*/
function ipv4AddressStringToBuffer(ipAddress: string): Buffer {
return Buffer.from(
Uint8Array.from(
ipAddress.split('.').map(segment => Number.parseInt(segment))
)
);
}

/**
* Converts an IPv4 or IPv6 address from string representation to binary
* representation
Expand All @@ -468,11 +485,9 @@ function parseIPv6Chunk(addressChunk: string): number[] {
*/
function ipAddressStringToBuffer(ipAddress: string): Buffer | null {
if (isIPv4(ipAddress)) {
return Buffer.from(
Uint8Array.from(
ipAddress.split('.').map(segment => Number.parseInt(segment))
)
);
return ipv4AddressStringToBuffer(ipAddress);
} else if (isIPv6MappedIPv4(ipAddress)) {
return ipv4AddressStringToBuffer(ipAddress.substring(7));
} else if (isIPv6(ipAddress)) {
let leftSection: string;
let rightSection: string;
Expand Down

0 comments on commit 36c9a4f

Please sign in to comment.