CRZbulabula commented on code in PR #11455:
URL: https://github.com/apache/iotdb/pull/11455#discussion_r1379561752


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/NodeUrlUtils.java:
##########
@@ -188,10 +193,62 @@ public static List<TConfigNodeLocation> 
parseTConfigNodeUrls(String configNodeUr
     return parseTConfigNodeUrls(Arrays.asList(configNodeUrls.split(";")));
   }
 
-  public static boolean isLocalAddress(String ip) {
-    if (ip == null) {
+  /**
+   * Detect whether the given addresses or host names(may contain both) point 
to the node itself.
+   *
+   * @param addressesOrHostNames List of the addresses or host name to check
+   * @return true if one of the given strings point to the node itself
+   * @throws UnknownHostException Throw when unable to parse the given 
addresses or host names
+   */
+  public static boolean containsLocalAddress(List<String> addressesOrHostNames)
+      throws UnknownHostException {
+    if (addressesOrHostNames == null) {
       return false;
     }
-    return ip.equals("0.0.0.0") || ip.equals("127.0.0.1") || 
ip.equals("localhost");
+
+    Set<String> selfAddresses = getAllLocalAddresses();
+
+    for (String addressOrHostName : addressesOrHostNames) {
+      if (addressOrHostName == null) {
+        continue;
+      }
+      // Unify address or hostName, converting them to addresses
+      HashSet<String> translatedAddresses =
+          Arrays.stream(InetAddress.getAllByName(addressOrHostName))
+              .map(InetAddress::getHostAddress)
+              .collect(Collectors.toCollection(HashSet::new));
+      translatedAddresses.retainAll(selfAddresses);
+
+      if (!translatedAddresses.isEmpty()) {
+        return true;
+      }
+    }
+
+    return false;
+  }
+
+  /**
+   * Return all the internal, external, IPv4, IPv6 and loopback 
addresses(without hostname) of the
+   * node
+   *
+   * @return the local addresses set
+   * @throws UnknownHostException Throw when unable to self addresses or host 
names (Normally not
+   *     thrown)
+   */
+  public static Set<String> getAllLocalAddresses() throws UnknownHostException 
{
+    // Check internal and external, IPv4 and IPv6 network addresses
+    Set<String> selfAddresses =
+        
Arrays.stream(InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()))
+            .map(InetAddress::getHostAddress)
+            .collect(Collectors.toCollection(HashSet::new));
+    // Check IPv4 and IPv6 loopback addresses 127.0.0.1 and 0.0.0.0.0.0.0.1
+    selfAddresses.addAll(
+        Arrays.stream(InetAddress.getAllByName("localhost"))
+            .map(InetAddress::getHostAddress)
+            .collect(Collectors.toCollection(HashSet::new)));
+    // Check general address 0.0.0.0
+    selfAddresses.add("0.0.0.0");

Review Comment:
   Don't use magic string, using private static constant instead. 



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/NodeUrlUtils.java:
##########
@@ -188,10 +193,62 @@ public static List<TConfigNodeLocation> 
parseTConfigNodeUrls(String configNodeUr
     return parseTConfigNodeUrls(Arrays.asList(configNodeUrls.split(";")));
   }
 
-  public static boolean isLocalAddress(String ip) {
-    if (ip == null) {
+  /**
+   * Detect whether the given addresses or host names(may contain both) point 
to the node itself.
+   *
+   * @param addressesOrHostNames List of the addresses or host name to check
+   * @return true if one of the given strings point to the node itself
+   * @throws UnknownHostException Throw when unable to parse the given 
addresses or host names
+   */
+  public static boolean containsLocalAddress(List<String> addressesOrHostNames)
+      throws UnknownHostException {
+    if (addressesOrHostNames == null) {
       return false;
     }
-    return ip.equals("0.0.0.0") || ip.equals("127.0.0.1") || 
ip.equals("localhost");
+
+    Set<String> selfAddresses = getAllLocalAddresses();
+
+    for (String addressOrHostName : addressesOrHostNames) {
+      if (addressOrHostName == null) {
+        continue;
+      }
+      // Unify address or hostName, converting them to addresses
+      HashSet<String> translatedAddresses =
+          Arrays.stream(InetAddress.getAllByName(addressOrHostName))
+              .map(InetAddress::getHostAddress)
+              .collect(Collectors.toCollection(HashSet::new));
+      translatedAddresses.retainAll(selfAddresses);
+
+      if (!translatedAddresses.isEmpty()) {
+        return true;
+      }
+    }
+
+    return false;
+  }
+
+  /**
+   * Return all the internal, external, IPv4, IPv6 and loopback 
addresses(without hostname) of the
+   * node
+   *
+   * @return the local addresses set
+   * @throws UnknownHostException Throw when unable to self addresses or host 
names (Normally not
+   *     thrown)
+   */
+  public static Set<String> getAllLocalAddresses() throws UnknownHostException 
{
+    // Check internal and external, IPv4 and IPv6 network addresses
+    Set<String> selfAddresses =
+        
Arrays.stream(InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()))
+            .map(InetAddress::getHostAddress)
+            .collect(Collectors.toCollection(HashSet::new));
+    // Check IPv4 and IPv6 loopback addresses 127.0.0.1 and 0.0.0.0.0.0.0.1
+    selfAddresses.addAll(
+        Arrays.stream(InetAddress.getAllByName("localhost"))

Review Comment:
   Don't use magic string, using private static constant instead.



-- 
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]

Reply via email to