anmolnar commented on code in PR #2280:
URL: https://github.com/apache/zookeeper/pull/2280#discussion_r2285810016
##########
zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java:
##########
@@ -81,6 +97,83 @@ private byte[] v4addr2Bytes(String addr) {
return b;
}
+ /**
+ * Validates an IPv6 address string and converts it into a byte array.
+ *
+ * @param ipv6Addr The IPv6 address string to validate.
+ * @return A byte array representing the IPv6 address if valid, or null if
the address
+ * is invalid or cannot be parsed.
+ */
+ static byte[] v6addr2Bytes(String ipv6Addr) {
+ try {
+ return parseV6addr(ipv6Addr);
+ } catch (IllegalArgumentException e) {
+ LOG.warn("Fail to parse {} as IPv6 address: {}", ipv6Addr,
e.getMessage());
+ return null;
+ }
+ }
+
+ static byte[] parseV6addr(String ipv6Addr) {
+ // Split the address by "::" to handle zero compression, -1 to keep
trailing empty strings
+ String[] parts = ipv6Addr.split("::", -1);
+
+ String[] segments1 = new String[0];
+ String[] segments2 = new String[0];
+
+ // Case 1: No "::" (full address)
+ if (parts.length == 1) {
+ segments1 = parts[0].split(":", -1);
Review Comment:
It doesn't forbid, with `-1` lenght `split()` will produce trailing empty
string in the resulting array which the below check can detect as wrong format.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]