From a53897d51e2e9d9b26a00dd3ef67f0f79a3c6386 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 03 Oct 2007 12:15:23 +0000
Subject: [PATCH] Fix for issue 2407: dsreplication enable : can't configure several instances when using the same "source". The problem was that the code (if the skip port check was not present) did check that the provided replication port was available in the *local* host.  If it was not the case, the error message appeared.  The fix consists of performing this check only if the user specified to configure the local server (the only host on which we can actually perform the check).

---
 opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java |   40 ++++++++++++++++++++++++++++++++++++++--
 1 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java b/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
index 7cb75ee..aec0c4b 100644
--- a/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
+++ b/opends/src/guitools/org/opends/guitools/replicationcli/ReplicationCliMain.java
@@ -38,6 +38,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -619,7 +620,7 @@
                 INFO_REPLICATION_ENABLE_REPLICATIONPORT1_PROMPT.get(),
                 argParser.getDefaultReplicationPort1(), false);
           }
-          if (!argParser.skipReplicationPortCheck())
+          if (!argParser.skipReplicationPortCheck() && isLocalHost(host1))
           {
             if (!SetupUtils.canUseAsPort(replicationPort1))
             {
@@ -829,7 +830,7 @@
                 INFO_REPLICATION_ENABLE_REPLICATIONPORT2_PROMPT.get(),
                 argParser.getDefaultReplicationPort2(), false);
           }
-          if (!argParser.skipReplicationPortCheck())
+          if (!argParser.skipReplicationPortCheck() && isLocalHost(host2))
           {
             if (!SetupUtils.canUseAsPort(replicationPort2))
             {
@@ -2387,6 +2388,7 @@
         if (!hasReplicationPort1)
         {
           if (!argParser.skipReplicationPortCheck() &&
+              isLocalHost(host1) &&
               !SetupUtils.canUseAsPort(replPort1))
           {
             errorMessages.add(getCannotBindToPortError(replPort1));
@@ -2395,6 +2397,7 @@
         if (!hasReplicationPort2)
         {
           if (!argParser.skipReplicationPortCheck() &&
+              isLocalHost(host2) &&
               !SetupUtils.canUseAsPort(replPort2))
           {
             errorMessages.add(getCannotBindToPortError(replPort2));
@@ -5200,4 +5203,37 @@
     }
     return mb.toMessage();
   }
+
+  /**
+   * Basic method to know if the host is local or not.  This is only used to
+   * know if we can perform a port check or not.
+   * @param host the host to analyze.
+   * @return <CODE>true</CODE> if it is the local host and <CODE>false</CODE>
+   * otherwise.
+   */
+  private boolean isLocalHost(String host)
+  {
+    boolean isLocalHost = false;
+    if (!"localhost".equalsIgnoreCase(host))
+    {
+      try
+      {
+        InetAddress localAddress = InetAddress.getLocalHost();
+        InetAddress[] addresses = InetAddress.getAllByName(host);
+        for (int i=0; i<addresses.length && !isLocalHost; i++)
+        {
+          isLocalHost = localAddress.equals(addresses[i]);
+        }
+      }
+      catch (Throwable t)
+      {
+        LOG.log(Level.WARNING, "Failing checking host names: "+t, t);
+      }
+    }
+    else
+    {
+      isLocalHost = true;
+    }
+    return isLocalHost;
+  }
 }

--
Gitblit v1.10.0