[lxc-devel] [lxd/master] lxd/util: Tweak NetworkInterfaceAddress to only return global

stgraber on Github lxc-bot at linuxcontainers.org
Mon Apr 20 23:01:59 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200420/c4740046/attachment.bin>
-------------- next part --------------
From 02e840161e36153063224390e2ab56366f8e5c2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Mon, 20 Apr 2020 19:01:10 -0400
Subject: [PATCH] lxd/util: Tweak NetworkInterfaceAddress to only return global
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/util/net.go | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/lxd/util/net.go b/lxd/util/net.go
index af99fc98cd..ebf0c1af2b 100644
--- a/lxd/util/net.go
+++ b/lxd/util/net.go
@@ -114,23 +114,31 @@ func NetworkInterfaceAddress() string {
 	if err != nil {
 		return ""
 	}
+
 	for _, iface := range ifaces {
-		if shared.IsLoopback(&iface) {
-			continue
-		}
 		addrs, err := iface.Addrs()
 		if err != nil {
 			continue
 		}
+
 		if len(addrs) == 0 {
 			continue
 		}
-		addr, ok := addrs[0].(*net.IPNet)
-		if !ok {
-			continue
+
+		for _, addr := range addrs {
+			ipNet, ok := addr.(*net.IPNet)
+			if !ok {
+				continue
+			}
+
+			if !ipNet.IP.IsGlobalUnicast() {
+				continue
+			}
+
+			return ipNet.IP.String()
 		}
-		return addr.IP.String()
 	}
+
 	return ""
 }
 


More information about the lxc-devel mailing list