GenerateNextAvailableIP fixed
This commit is contained in:
parent
f3ece2fafc
commit
b199054bcf
2 changed files with 42 additions and 23 deletions
|
@ -5,7 +5,7 @@ using System.Web;
|
||||||
|
|
||||||
namespace WebsitePanel.EnterpriseServer {
|
namespace WebsitePanel.EnterpriseServer {
|
||||||
|
|
||||||
public struct IPAddress {
|
public struct IPAddress : IComparable {
|
||||||
public Int128 Address;
|
public Int128 Address;
|
||||||
public bool V6 { get; private set; }
|
public bool V6 { get; private set; }
|
||||||
public bool V4 { get { return !V6 || Null; } }
|
public bool V4 { get { return !V6 || Null; } }
|
||||||
|
@ -68,10 +68,19 @@ namespace WebsitePanel.EnterpriseServer {
|
||||||
if (Null)
|
if (Null)
|
||||||
return "";
|
return "";
|
||||||
var s = new System.Text.StringBuilder();
|
var s = new System.Text.StringBuilder();
|
||||||
if (!V6) {
|
if (!V6)
|
||||||
var ipl = Address;
|
{
|
||||||
s.Append(String.Format("{0}.{1}.{2}.{3}", (ipl >> 24) & 0xFFL, (ipl >> 16) & 0xFFL, (ipl >> 8) & 0xFFL, (ipl & 0xFFL)));
|
var ipl = Address;
|
||||||
} else if (!IsMask) {
|
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>();
|
var vals = new List<int>();
|
||||||
int i;
|
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 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 { }
|
public class NullIPAddress { }
|
||||||
|
|
||||||
|
|
|
@ -3235,40 +3235,31 @@ namespace WebsitePanel.EnterpriseServer
|
||||||
Trace.TraceInformation("Param - subnetMask: {0}", subnetMask);
|
Trace.TraceInformation("Param - subnetMask: {0}", subnetMask);
|
||||||
|
|
||||||
// start IP address
|
// start IP address
|
||||||
var startIp = IPAddress.Parse(startIPAddress);
|
var ip = IPAddress.Parse(startIPAddress) - 1;
|
||||||
var mask = IPAddress.Parse(subnetMask);
|
|
||||||
|
|
||||||
Trace.TraceInformation("Calculate initial value for lastAddress");
|
|
||||||
var lastAddress = (startIp & ~mask) - 1;
|
|
||||||
Trace.TraceInformation("Initial lastAddress: {0}", lastAddress.ToString());
|
|
||||||
|
|
||||||
Trace.TraceInformation("Start looking for next available IP");
|
Trace.TraceInformation("Start looking for next available IP");
|
||||||
foreach (var addr in ips.Keys)
|
foreach (var addr in ips.Keys)
|
||||||
{
|
{
|
||||||
if ((addr - lastAddress) > 1)
|
if ((addr - ip) > 1)
|
||||||
{
|
{
|
||||||
// it is a gap
|
// it is a gap
|
||||||
Trace.TraceInformation("Gap");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
lastAddress = addr;
|
ip = addr;
|
||||||
Trace.TraceInformation("New lastAddress: {0}", lastAddress.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var genAddr = lastAddress + 1;
|
// final IP found
|
||||||
Trace.TraceInformation("Generated address: {0}", genAddr.ToString());
|
ip = ip + 1;
|
||||||
|
|
||||||
// convert to IP address
|
|
||||||
var ip = startIp & mask | genAddr;
|
|
||||||
string genIP = ip.ToString();
|
string genIP = ip.ToString();
|
||||||
Trace.TraceInformation("Generated IP: {0}", genIP);
|
Trace.TraceInformation("Generated IP: {0}", genIP);
|
||||||
|
|
||||||
// store in cache
|
// store in cache
|
||||||
Trace.TraceInformation("Adding to sorted list");
|
Trace.TraceInformation("Adding to sorted list");
|
||||||
ips.Add(genAddr, genIP);
|
ips.Add(ip, genIP);
|
||||||
|
|
||||||
Trace.TraceInformation("Leaving GenerateNextAvailablePrivateIP()");
|
Trace.TraceInformation("Leaving GenerateNextAvailablePrivateIP()");
|
||||||
return genIP;
|
return genIP;
|
||||||
|
@ -3292,8 +3283,14 @@ namespace WebsitePanel.EnterpriseServer
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetPrivateNetworkSubnetMask(string cidr, bool v6) {
|
private static string GetPrivateNetworkSubnetMask(string cidr, bool v6) {
|
||||||
if (v6) return "/" + cidr;
|
if (v6)
|
||||||
else return IPAddress.Parse("/" + cidr).ToV4MaskString();
|
{
|
||||||
|
return "/" + cidr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return IPAddress.Parse("/" + cidr).ToV4MaskString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetSubnetMaskCidr(string subnetMask) {
|
private static string GetSubnetMaskCidr(string subnetMask) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue