-
Notifications
You must be signed in to change notification settings - Fork 618
[jdbc-v2] Fixes IPv6 address conversion into IPv4 #2369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.clickhouse.client.api.data_formats.internal; | ||
|
|
||
| import java.net.Inet4Address; | ||
| import java.net.Inet6Address; | ||
| import java.net.InetAddress; | ||
| import java.net.UnknownHostException; | ||
|
|
||
| public class InetAddressConverter { | ||
|
|
||
| /** | ||
| * Converts IPv4 address to IPv6 address. | ||
| * | ||
| * @param value IPv4 address | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Javadoc for |
||
| * @return IPv6 address | ||
| * @throws IllegalArgumentException when failed to convert to IPv6 address | ||
| */ | ||
| public static Inet6Address convertToIpv6(InetAddress value) { | ||
| if (value == null || value instanceof Inet6Address) { | ||
| return (Inet6Address) value; | ||
| } | ||
|
|
||
| // https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses | ||
| byte[] bytes = new byte[16]; | ||
| bytes[10] = (byte) 0xFF; | ||
| bytes[11] = (byte) 0xFF; | ||
| System.arraycopy(value.getAddress(), 0, bytes, 12, 4); | ||
|
|
||
| try { | ||
| return Inet6Address.getByAddress(null, bytes, null); | ||
| } catch (UnknownHostException e) { | ||
| throw new IllegalArgumentException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts IPv6 address to IPv4 address if applicable. | ||
| * | ||
| * @param value IPv6 address | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Javadoc for |
||
| * @return IPv4 address | ||
| * @throws IllegalArgumentException when failed to convert to IPv4 address | ||
| */ | ||
| public static Inet4Address convertToIpv4(InetAddress value) { | ||
| if (value == null || value instanceof Inet4Address) { | ||
| return (Inet4Address) value; | ||
| } | ||
|
|
||
| byte[] bytes = value.getAddress(); | ||
| boolean invalid = false; | ||
| for (int i = 0; i < 10; i++) { | ||
| if (bytes[i] != (byte) 0) { | ||
| invalid = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!invalid) { | ||
| invalid = bytes[10] != 0xFF || bytes[11] != 0xFF; | ||
| } | ||
|
|
||
| if (invalid) { | ||
| throw new IllegalArgumentException("Failed to convert IPv6 to IPv4"); | ||
| } | ||
|
|
||
| byte[] addr = new byte[4]; | ||
| System.arraycopy(bytes, 12, addr, 0, 4); | ||
| try { | ||
| return (Inet4Address) InetAddress.getByAddress(addr); | ||
| } catch (UnknownHostException e) { | ||
| throw new IllegalArgumentException(e); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the project guidelines, tests are required for new functionality. Please include unit tests for this converter class to verify both conversion methods work correctly with various edge cases (null values, valid IPv4/IPv6 addresses, invalid conversions, etc.).