kezhuw commented on code in PR #2280:
URL: https://github.com/apache/zookeeper/pull/2280#discussion_r2273698828


##########
zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java:
##########
@@ -81,6 +96,121 @@ 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.
+     */
+    public static byte[] v6addr2Bytes(String ipv6Addr) {
+        // Split the address by "::" to handle zero compression, -1 to keep 
trailing empty strings
+        String[] parts = ipv6Addr.trim().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(":");
+            if (segments1.length != IPV6_SEGMENT_COUNT) {
+                LOG.info("IPv6 address without '::' must have " + 
IPV6_SEGMENT_COUNT + " segments: {}", ipv6Addr);
+                return null;
+            }
+        } else if (parts.length == 2) {
+            // Case 2: "::" is present
+            // Handle cases like "::1" or "1::"
+            if (!parts[0].isEmpty()) {
+                segments1 = parts[0].split(":");
+            }
+            if (!parts[1].isEmpty()) {
+                segments2 = parts[1].split(":");
+            }
+
+            // Check if the total number of explicit segments exceeds 8
+            if (segments1.length + segments2.length >= IPV6_SEGMENT_COUNT) {
+                LOG.info("Too many segments in IPv6 address with '::': {}", 
ipv6Addr);
+                return null;
+            }
+        } else {
+            // Case 3: Invalid number of parts after splitting by "::" (should 
be 1 or 2)
+            LOG.warn("Invalid IPv6 address format (unexpected '::' split 
result):  {}", ipv6Addr);

Review Comment:
   I think it might be good to introduce a parse method to throw exception for 
`v6addr2Bytes` to catch and log. This way we can unify the log statements in a 
consistent manner, say:
   
   `LOG.warn("Fail to parse {} as IPv6 address: {}", ipv6Addr, reason)`.



-- 
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: notifications-unsubscr...@zookeeper.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to