diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Common/IPAddress.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Common/IPAddress.cs index 0d1b104f..b283060f 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Common/IPAddress.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Common/IPAddress.cs @@ -5,7 +5,7 @@ using System.Web; namespace WebsitePanel.EnterpriseServer { - public struct IPAddress { + public struct IPAddress : IComparable { public Int128 Address; public bool V6 { get; private set; } public bool V4 { get { return !V6 || Null; } } @@ -68,10 +68,19 @@ namespace WebsitePanel.EnterpriseServer { if (Null) return ""; var s = new System.Text.StringBuilder(); - if (!V6) { - var ipl = Address; - s.Append(String.Format("{0}.{1}.{2}.{3}", (ipl >> 24) & 0xFFL, (ipl >> 16) & 0xFFL, (ipl >> 8) & 0xFFL, (ipl & 0xFFL))); - } else if (!IsMask) { + if (!V6) + { + var ipl = Address; + if (IsMask) + { + int digits = 32 - Cidr; + ipl = (Int128.MaxValue << 1) | 0x1; // remove left sign bit + ipl = ipl << digits; + } + s.Append(String.Format("{0}.{1}.{2}.{3}", (ipl >> 24) & 0xFFL, (ipl >> 16) & 0xFFL, (ipl >> 8) & 0xFFL, (ipl & 0xFFL))); + } + else if (!IsMask) + { var vals = new List(); int i; @@ -181,7 +190,20 @@ namespace WebsitePanel.EnterpriseServer { } public static implicit operator IPAddress(NullIPAddress a) { return new IPAddress { Null = true, Address = 0, Cidr = -1 }; } - } + + public int CompareTo(object obj) + { + var a = this.Address; + var b = ((IPAddress)obj).Address; + + if (a < b) + return 1; + else if (a > b) + return -1; + else + return 0; + } + } public class NullIPAddress { } diff --git a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Virtualization/VirtualizationServerController.cs b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Virtualization/VirtualizationServerController.cs index d07c176f..e9eb4862 100644 --- a/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Virtualization/VirtualizationServerController.cs +++ b/WebsitePanel/Sources/WebsitePanel.EnterpriseServer/Code/Virtualization/VirtualizationServerController.cs @@ -3235,40 +3235,31 @@ namespace WebsitePanel.EnterpriseServer Trace.TraceInformation("Param - subnetMask: {0}", subnetMask); // start IP address - var startIp = IPAddress.Parse(startIPAddress); - var mask = IPAddress.Parse(subnetMask); - - Trace.TraceInformation("Calculate initial value for lastAddress"); - var lastAddress = (startIp & ~mask) - 1; - Trace.TraceInformation("Initial lastAddress: {0}", lastAddress.ToString()); + var ip = IPAddress.Parse(startIPAddress) - 1; Trace.TraceInformation("Start looking for next available IP"); foreach (var addr in ips.Keys) { - if ((addr - lastAddress) > 1) + if ((addr - ip) > 1) { // it is a gap - Trace.TraceInformation("Gap"); break; } else { - lastAddress = addr; - Trace.TraceInformation("New lastAddress: {0}", lastAddress.ToString()); + ip = addr; } } - var genAddr = lastAddress + 1; - Trace.TraceInformation("Generated address: {0}", genAddr.ToString()); + // final IP found + ip = ip + 1; - // convert to IP address - var ip = startIp & mask | genAddr; string genIP = ip.ToString(); Trace.TraceInformation("Generated IP: {0}", genIP); // store in cache Trace.TraceInformation("Adding to sorted list"); - ips.Add(genAddr, genIP); + ips.Add(ip, genIP); Trace.TraceInformation("Leaving GenerateNextAvailablePrivateIP()"); return genIP; @@ -3292,8 +3283,14 @@ namespace WebsitePanel.EnterpriseServer } private static string GetPrivateNetworkSubnetMask(string cidr, bool v6) { - if (v6) return "/" + cidr; - else return IPAddress.Parse("/" + cidr).ToV4MaskString(); + if (v6) + { + return "/" + cidr; + } + else + { + return IPAddress.Parse("/" + cidr).ToV4MaskString(); + } } private static string GetSubnetMaskCidr(string subnetMask) {