This is an automated email from the ASF dual-hosted git repository.

ferdei pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new 84878df61a NIFI-12751 [MiNiFi] Prefer IPv4 address when sending 
NetworkInfo to C2
84878df61a is described below

commit 84878df61ab7340bd7340922f651e862afa74b9a
Author: Ferenc Kis <briansolo1...@gmail.com>
AuthorDate: Wed Feb 7 15:57:12 2024 +0100

    NIFI-12751 [MiNiFi] Prefer IPv4 address when sending NetworkInfo to C2
    
    Signed-off-by: Ferenc Erdei <erdei.feren...@gmail.com>
    This closes #8370
---
 .../nifi/c2/client/service/C2HeartbeatFactory.java | 24 +++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git 
a/c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/C2HeartbeatFactory.java
 
b/c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/C2HeartbeatFactory.java
index 8534536430..c939ab78ab 100644
--- 
a/c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/C2HeartbeatFactory.java
+++ 
b/c2/c2-client-bundle/c2-client-service/src/main/java/org/apache/nifi/c2/client/service/C2HeartbeatFactory.java
@@ -14,6 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.nifi.c2.client.service;
 
 import static org.apache.commons.lang3.StringUtils.isNotBlank;
@@ -21,6 +22,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
 import java.io.File;
 import java.lang.management.ManagementFactory;
 import java.lang.management.OperatingSystemMXBean;
+import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.util.Collections;
@@ -169,14 +171,20 @@ public class C2HeartbeatFactory {
                     logger.debug("Instance has multiple interfaces.  Generated 
information may be non-deterministic.");
                 }
 
+                boolean networkInfoUnset = true;
                 for (NetworkInterface networkInterface : operationIfaces) {
                     Enumeration<InetAddress> inetAddresses = 
networkInterface.getInetAddresses();
-                    if (inetAddresses.hasMoreElements()) {
+                    while (inetAddresses.hasMoreElements()) {
                         InetAddress inetAddress = inetAddresses.nextElement();
-                        networkInfo.setDeviceId(networkInterface.getName());
-                        networkInfo.setHostname(inetAddress.getHostName());
-                        networkInfo.setIpAddress(inetAddress.getHostAddress());
-                        break;
+                        // IPv4 address is preferred over IPv6 as it provides 
more readable information for the user
+                        if (inetAddress instanceof Inet4Address) {
+                            updateNetworkInfo(networkInfo, networkInterface, 
inetAddress);
+                            return networkInfo;
+                        }
+                        if (networkInfoUnset) {
+                            updateNetworkInfo(networkInfo, networkInterface, 
inetAddress);
+                            networkInfoUnset = false;
+                        }
                     }
                 }
             }
@@ -186,6 +194,12 @@ public class C2HeartbeatFactory {
         return networkInfo;
     }
 
+    private void updateNetworkInfo(NetworkInfo networkInfo, NetworkInterface 
networkInterface, InetAddress inetAddress) {
+        networkInfo.setDeviceId(networkInterface.getName());
+        networkInfo.setHostname(inetAddress.getHostName());
+        networkInfo.setIpAddress(inetAddress.getHostAddress());
+    }
+
     private String getDeviceIdentifier(NetworkInfo networkInfo) {
         if (deviceId == null) {
             if (networkInfo.getDeviceId() != null) {

Reply via email to