[lxc-devel] [lxd/master] lxd/util: IsAddressCovered takes into account host names

freeekanayaka on Github lxc-bot at linuxcontainers.org
Tue Jan 14 15:50:45 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 377 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200114/d588f414/attachment-0001.bin>
-------------- next part --------------
From f506d831b5cd7c11fe1d214992519e6a399f4d6f Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Tue, 14 Jan 2020 15:47:50 +0000
Subject: [PATCH] lxd/util: IsAddressCovered takes into account host names

Fixes #6682.

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/util/net.go      | 20 ++++++++++++++++++--
 lxd/util/net_test.go |  1 +
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/lxd/util/net.go b/lxd/util/net.go
index 51b192d1ae..154a0d98f5 100644
--- a/lxd/util/net.go
+++ b/lxd/util/net.go
@@ -158,11 +158,27 @@ func IsAddressCovered(address1, address2 string) bool {
 		return false
 	}
 
+	// If address2 contains a host name, let's try to resolve it, in order
+	// to compare the actual IPs.
+	if host2 != "" {
+		ip2 := net.ParseIP(host2)
+		if ip2 == nil {
+			ips, err := net.LookupHost(host2)
+			if err == nil && len(ips) > 0 {
+				host2 = ips[0]
+			}
+		}
+	}
+
+	if host2 == host1 {
+		return true
+	}
+
 	// If address2 is using an IPv4 wildcard for the host, then address2 is
 	// only covered if it's an IPv4 address.
 	if host2 == "0.0.0.0" {
-		ip := net.ParseIP(host1)
-		if ip != nil && ip.To4() != nil {
+		ip1 := net.ParseIP(host1)
+		if ip1 != nil && ip1.To4() != nil {
 			return true
 		}
 		return false
diff --git a/lxd/util/net_test.go b/lxd/util/net_test.go
index 8509a76cb9..ee7845a90a 100644
--- a/lxd/util/net_test.go
+++ b/lxd/util/net_test.go
@@ -69,6 +69,7 @@ func TestIsAddressCovered(t *testing.T) {
 		{"[::1]:8443", ":8443", true},
 		{":8443", "[::]:8443", true},
 		{"0.0.0.0:8443", "[::]:8443", true},
+		{"127.0.0.1:8443", "localhost:8443", true},
 	}
 
 	for _, c := range cases {


More information about the lxc-devel mailing list