Merge with Ipv6 contribution
This commit is contained in:
commit
df5da7b015
35 changed files with 2893 additions and 158 deletions
|
@ -0,0 +1,164 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer {
|
||||
|
||||
public struct IPAddress {
|
||||
public Int128 Address;
|
||||
public bool V6 { get; private set; }
|
||||
public bool V4 { get { return !V6 || Null; } }
|
||||
public bool IsSubnet { get; private set; }
|
||||
public bool IsMask { get; private set; }
|
||||
public int Cidr { get; private set; }
|
||||
public bool Null { get; private set; }
|
||||
public IPAddress LastSubnetIP { get { return new IPAddress { Address = (Address | (~((Int128)0) >> (V4 ? Cidr + 64 : Cidr))), Cidr = V4 ? 32 : 128, IsSubnet = false, Null = false, V6 = V6 }; } }
|
||||
public IPAddress FirstSubnetIP { get { return new IPAddress { Address = (Address & ~(~((Int128)0) >> (V4 ? Cidr + 64 : Cidr))) + 1, Cidr = V4 ? 32 : 128, IsSubnet = false, Null = false, V6 = V6 }; } }
|
||||
public Int128 Mask { get { return IsSubnet ? Int128.MinValue >> (Cidr-1) : Address; } }
|
||||
|
||||
const int c = 256*256;
|
||||
|
||||
public static IPAddress Parse(string ip)
|
||||
{
|
||||
IPAddress adr = default(IPAddress);
|
||||
adr.V6 = false;
|
||||
|
||||
if (String.IsNullOrEmpty(ip)) {
|
||||
adr.Address = 0; adr.Null = true; adr.Cidr = 32; adr.IsSubnet = false;
|
||||
return adr;
|
||||
}
|
||||
|
||||
if (ip.Contains('/')) {
|
||||
var tokens = ip.Split('/');
|
||||
ip = tokens[0];
|
||||
adr.IsSubnet = true;
|
||||
adr.Cidr = Utils.ParseInt(tokens[1], -1);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(ip)) {
|
||||
adr.IsMask = true; adr.V6 = true;
|
||||
adr.Address = adr.Mask;
|
||||
} else {
|
||||
|
||||
var ipadr = System.Net.IPAddress.Parse(ip);
|
||||
|
||||
if (adr.V6 = ipadr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
|
||||
byte[] bytes = ipadr.GetAddressBytes();
|
||||
Int128 a = 0;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
a = a * 256 + bytes[i];
|
||||
}
|
||||
adr.Address = a;
|
||||
} else {
|
||||
string[] parts = ip.Split('.');
|
||||
adr.Address = (Int128)(Int32.Parse(parts[3]) +
|
||||
(Int32.Parse(parts[2]) << 8) +
|
||||
(Int32.Parse(parts[1]) << 16) +
|
||||
(Int32.Parse(parts[0]) << 24));
|
||||
}
|
||||
}
|
||||
if (adr.V4 && (adr.Cidr > 32 || 0 > adr.Cidr)) throw new ArgumentOutOfRangeException("Cidr must not be greater than 32 for IPv4 Addresses.");
|
||||
if (adr.V6 && (adr.Cidr > 128 || 0 > adr.Cidr)) throw new ArgumentOutOfRangeException("Cidr must not be greater than 128 for IPv6 Addresses.");
|
||||
return adr;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Null)
|
||||
return "";
|
||||
var s = new System.Text.StringBuilder();
|
||||
if (!V6) {
|
||||
var ipl = (long)Address;
|
||||
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>();
|
||||
int i;
|
||||
Int128 a = Address;
|
||||
for (i = 0; i < 8; i++) {
|
||||
vals.Add((int)(a % c));
|
||||
a = a / c;
|
||||
}
|
||||
|
||||
int index = -1, n = 0, m = 0;
|
||||
for (i = 7; i >= 0; i--) {
|
||||
if (vals[i] == 0) {
|
||||
n++;
|
||||
if (n > m) {
|
||||
index = i;
|
||||
m = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
index += m-1;
|
||||
|
||||
i = 7;
|
||||
while (i >= 0) {
|
||||
if (i == index) {
|
||||
if (m == 8) s.Append("::");
|
||||
else s.Append(":");
|
||||
i -= m;
|
||||
}
|
||||
if (i >= 0) {
|
||||
if (i < 7) s.Append(":");
|
||||
s.Append(vals[i].ToString("x"));
|
||||
}
|
||||
i--;
|
||||
}
|
||||
}
|
||||
if (IsSubnet && !(IsMask && V4)) {
|
||||
s.Append('/'); s.Append(Cidr.ToString());
|
||||
}
|
||||
return s.ToString();
|
||||
}
|
||||
|
||||
public string ToV4MaskString() {
|
||||
V6 = false;
|
||||
IsMask = true;
|
||||
return ToString();
|
||||
}
|
||||
|
||||
public static bool operator ==(IPAddress a, IPAddress b) { return a.Address == b.Address && a.Null == b.Null && (a.Null || !(a.IsSubnet && b.IsSubnet || a.IsMask && b.IsMask) || a.Cidr == b.Cidr); }
|
||||
public static bool operator ==(IPAddress a, long b) { return a.Address == b; }
|
||||
public static bool operator !=(IPAddress a, IPAddress b) { return !(a == b); }
|
||||
public static bool operator !=(IPAddress a, long b) { return a.Address != b; }
|
||||
public static bool operator <(IPAddress a, IPAddress b) { return a.Address < b.Address; }
|
||||
public static bool operator >(IPAddress a, IPAddress b) { return a.Address > b.Address; }
|
||||
public static bool operator <=(IPAddress a, IPAddress b) { return a.Address <= b.Address; }
|
||||
public static bool operator >=(IPAddress a, IPAddress b) { return a.Address >= b.Address; }
|
||||
/*
|
||||
public static IPAddress operator +(IPAddress a, IPAddress b) {
|
||||
if (a.IsSubnet || b.IsSubnet || a.V6 != b.V6) throw new ArgumentException("Arithmetic with subnets or mixed v4 & v6 addresses not supported.");
|
||||
return new IPAddress { Address = a.Address + b.Address, Null = a.Null && b.Null, Cidr = 0, V6 = a.V6 };
|
||||
}*/
|
||||
|
||||
public static Int128 operator -(IPAddress a, IPAddress b) {
|
||||
if (a.IsSubnet || b.IsSubnet || a.V6 != b.V6) throw new ArgumentException("Arithmetic with subnets or mixed v4 & v6 addresses not supported.");
|
||||
return a.Address - b.Address;
|
||||
}
|
||||
public static IPAddress operator +(IPAddress a, Int128 b) {
|
||||
return new IPAddress { Address = a.Address + b, Null = a.Null, Cidr = a.V4 ? 32 : 128, V6 = a.V6 };
|
||||
}
|
||||
public static IPAddress operator -(IPAddress a, Int128 b) {
|
||||
return new IPAddress { Address = a.Address - b, Null = a.Null, Cidr = a.V4 ? 32 : 128, V6 = a.V6 };
|
||||
}
|
||||
public static IPAddress operator |(IPAddress a, IPAddress b) {
|
||||
if (a.V6 != b.V6) throw new ArgumentException("Arithmetic with mixed v4 & v6 addresses not supported.");
|
||||
return new IPAddress { Address = a.Address | b.Address, Cidr = a.V4 ? 32 : 128, Null = false, V6 = a.V6, IsSubnet = false };
|
||||
}
|
||||
public static IPAddress operator &(IPAddress a, IPAddress b) {
|
||||
if (a.V6 != b.V6) throw new ArgumentException("Arithmetic with mixed v4 & v6 addresses not supported.");
|
||||
return new IPAddress { Address = a.Address | b.Address, Cidr = a.V4 ? 32 : 128, Null = false, V6 = a.V6, IsSubnet = false };
|
||||
}
|
||||
public static IPAddress operator ~(IPAddress a) {
|
||||
if (a.Null) return new IPAddress { Address = 0, Null = true, Cidr = a.V4 ? 32 : 128, V6 = true, IsSubnet = false };
|
||||
return new IPAddress { Address = ~a.Address, Cidr = a.Cidr , Null = false, V6 = a.V6, IsSubnet = false };
|
||||
}
|
||||
|
||||
public static implicit operator IPAddress(NullIPAddress a) { return new IPAddress { Null = true, Address = 0, Cidr = -1 }; }
|
||||
}
|
||||
|
||||
public class NullIPAddress { }
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -281,7 +281,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
rr.RecordType = (DnsRecordType)Enum.Parse(typeof(DnsRecordType), record.RecordType, true);
|
||||
rr.RecordName = record.RecordName;
|
||||
|
||||
if (record.RecordType == "A")
|
||||
if (record.RecordType == "A" || record.RecordType == "AAAA")
|
||||
{
|
||||
rr.RecordData = String.IsNullOrEmpty(record.RecordData) ? record.ExternalIP : record.RecordData;
|
||||
rr.RecordData = Utils.ReplaceStringVariable(rr.RecordData, "ip", record.ExternalIP);
|
||||
|
|
|
@ -65,7 +65,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
DnsRecord[] records = ServerController.GetDnsZoneRecords(domainId);
|
||||
foreach (DnsRecord record in records)
|
||||
{
|
||||
if ((record.RecordType == DnsRecordType.A) && (String.Compare(recordName, record.RecordName, true) == 0))
|
||||
if ((record.RecordType == DnsRecordType.A || record.RecordType == DnsRecordType.AAAA) && (String.Compare(recordName, record.RecordName, true) == 0))
|
||||
{
|
||||
CompleteTask(ret, CrmErrorCodes.CANNOT_CREATE_DNS_ZONE, null,
|
||||
string.Format("DNS record already exists. DomainId={0}, RecordName={1}", domainId, recordName));
|
||||
|
@ -73,8 +73,8 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
int res = ServerController.AddDnsZoneRecord(domainId, recordName, DnsRecordType.A, ip, 0, 0, 0, 0);
|
||||
var type = ip.Contains(":") ? DnsRecordType.AAAA : DnsRecordType.A;
|
||||
int res = ServerController.AddDnsZoneRecord(domainId, recordName, type, ip, 0);
|
||||
if (res != 0)
|
||||
{
|
||||
CompleteTask(ret, CrmErrorCodes.CANNOT_CREATE_DNS_ZONE, null,
|
||||
|
@ -373,7 +373,8 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
try
|
||||
{
|
||||
int res = ServerController.DeleteDnsZoneRecord(domainId, recordName, DnsRecordType.A, ip);
|
||||
var type = ip.Contains(":") ? DnsRecordType.AAAA : DnsRecordType.A;
|
||||
int res = ServerController.DeleteDnsZoneRecord(domainId, recordName, type, ip);
|
||||
|
||||
if (res != 0)
|
||||
{
|
||||
|
|
|
@ -948,6 +948,8 @@ namespace WebsitePanel.EnterpriseServer
|
|||
public static ResultObject AddIPAddressesRange(IPAddressPool pool, int serverId,
|
||||
string externalIP, string endIP, string internalIP, string subnetMask, string defaultGateway, string comments)
|
||||
{
|
||||
const int MaxSubnet = 512; // TODO bigger max subnet?
|
||||
|
||||
ResultObject res = new ResultObject();
|
||||
|
||||
#region Check account statuses
|
||||
|
@ -965,7 +967,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
try
|
||||
{
|
||||
if (externalIP == endIP)
|
||||
if (externalIP == endIP)
|
||||
{
|
||||
// add single IP and exit
|
||||
AddIPAddress(pool, serverId, externalIP, internalIP, subnetMask, defaultGateway, comments);
|
||||
|
@ -973,23 +975,28 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return res;
|
||||
}
|
||||
|
||||
long startExternalIP = ConvertIPToLong(externalIP);
|
||||
long startInternalIP = ConvertIPToLong(internalIP);
|
||||
long endExternalIP = ConvertIPToLong(endIP);
|
||||
var startExternalIP = IPAddress.Parse(externalIP);
|
||||
var startInternalIP = IPAddress.Parse(internalIP);
|
||||
var endExternalIP = IPAddress.Parse(endIP);
|
||||
|
||||
// handle CIDR notation IP/Subnet addresses
|
||||
if (startExternalIP.IsSubnet && endExternalIP == null) {
|
||||
endExternalIP = startExternalIP.LastSubnetIP;
|
||||
startExternalIP = startExternalIP.FirstSubnetIP;
|
||||
}
|
||||
|
||||
if (startExternalIP.V6 != startInternalIP.V6 && (startExternalIP.V6 != endExternalIP.V6 && endExternalIP != null)) throw new NotSupportedException("All IP addresses must be either V4 or V6.");
|
||||
|
||||
int i = 0;
|
||||
long step = (endExternalIP < startExternalIP) ? -1 : 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (i > 128)
|
||||
if (i > MaxSubnet)
|
||||
break;
|
||||
|
||||
// add IP address
|
||||
DataProvider.AddIPAddress((int)pool, serverId,
|
||||
ConvertLongToIP(startExternalIP),
|
||||
ConvertLongToIP(startInternalIP),
|
||||
subnetMask, defaultGateway, comments);
|
||||
DataProvider.AddIPAddress((int)pool, serverId, startExternalIP.ToString(), startInternalIP.ToString(), subnetMask, defaultGateway, comments);
|
||||
|
||||
if (startExternalIP == endExternalIP)
|
||||
break;
|
||||
|
@ -2439,26 +2446,78 @@ namespace WebsitePanel.EnterpriseServer
|
|||
#endregion
|
||||
|
||||
#region Private methods
|
||||
public static long ConvertIPToLong(string ip)
|
||||
|
||||
/*
|
||||
const int c = 256*256;
|
||||
|
||||
public static BigInt ConvertIPToInt(string ip, out bool v6)
|
||||
{
|
||||
v6 = false;
|
||||
|
||||
if (String.IsNullOrEmpty(ip))
|
||||
return 0;
|
||||
|
||||
string[] parts = ip.Split('.');
|
||||
return Int32.Parse(parts[3]) +
|
||||
(Int32.Parse(parts[2]) << 8) +
|
||||
(Int32.Parse(parts[1]) << 16) +
|
||||
(Int32.Parse(parts[0]) << 24);
|
||||
var adr = System.Net.IPAddress.Parse(ip);
|
||||
|
||||
if (v6 = adr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
|
||||
|
||||
string[] parts = ip.Split('.');
|
||||
return (BigInt)(Int32.Parse(parts[3]) +
|
||||
(Int32.Parse(parts[2]) << 8) +
|
||||
(Int32.Parse(parts[1]) << 16) +
|
||||
(Int32.Parse(parts[0]) << 24));
|
||||
} else {
|
||||
byte[] bytes = adr.GetAddressBytes();
|
||||
var a = BigInt.Zero;
|
||||
for (int i = 0; i < 16; i--) {
|
||||
a = a*256 + bytes[i];
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ConvertLongToIP(long ip)
|
||||
public static string ConvertIntToIP(BigInt ip, bool v6)
|
||||
{
|
||||
if (ip == 0)
|
||||
if (ip == BigInt.Zero)
|
||||
return "";
|
||||
if (!v6) {
|
||||
var ipl = (long)ip;
|
||||
return String.Format("{0}.{1}.{2}.{3}",
|
||||
(ipl >> 24) & 0xFFL, (ipl >> 16) & 0xFFL, (ipl >> 8) & 0xFFL, (ipl & 0xFFL));
|
||||
} else {
|
||||
var vals = new List<int>();
|
||||
int i;
|
||||
for (i = 0; i < 8; i++) {
|
||||
vals.Add((int)(ip % c));
|
||||
ip = ip / c;
|
||||
}
|
||||
|
||||
return String.Format("{0}.{1}.{2}.{3}",
|
||||
(ip >> 24) & 0xFFL, (ip >> 16) & 0xFFL, (ip >> 8) & 0xFFL, (ip & 0xFFL));
|
||||
int index = -1, n = 0, m = 0;
|
||||
for (i = 7; i >= 0; i++) {
|
||||
if (vals[i] == 0) {
|
||||
n++;
|
||||
if (n > m) {
|
||||
index = i;
|
||||
m = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
var s = new System.Text.StringBuilder();
|
||||
i = 7;
|
||||
while (i >= 0) {
|
||||
if (i != index) {
|
||||
if (i < 7) s.Append(":");
|
||||
s.Append(vals[i].ToString("x"));
|
||||
i--;
|
||||
} else {
|
||||
s.Append(":");
|
||||
while (vals[i] == 0) i--;
|
||||
}
|
||||
}
|
||||
return s.ToString();
|
||||
}
|
||||
}
|
||||
*/
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
@ -339,14 +339,19 @@ namespace WebsitePanel.EnterpriseServer.Code.SharePoint
|
|||
DnsRecord[] records = ServerController.GetDnsZoneRecords(domain.DomainId);
|
||||
foreach (DnsRecord record in records)
|
||||
{
|
||||
if (record.RecordType.Equals(DnsRecordType.A) && (record.RecordName == hostName))
|
||||
var type = record.RecordType;
|
||||
if ((type == DnsRecordType.A || type == DnsRecordType.AAAA) && String.IsNullOrEmpty(record.RecordName))
|
||||
{
|
||||
ServerController.DeleteDnsZoneRecord(domain.DomainId, hostName, DnsRecordType.A, record.RecordData);
|
||||
ServerController.DeleteDnsZoneRecord(domain.DomainId, String.Empty, type, record.RecordData);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ServerController.AddDnsZoneRecord(domain.DomainId, hostName, DnsRecordType.A, hostedSharePointSettings["RootWebApplicationIpAddress"], 0, 0, 0, 0);
|
||||
var ip = hostedSharePointSettings["RootWebApplicationIpAddress"];
|
||||
var type2 = ip.Contains(":") ? DnsRecordType.AAAA : DnsRecordType.A;
|
||||
ServerController.AddDnsZoneRecord(domain.DomainId, String.Empty, type2, ip, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -411,7 +416,26 @@ namespace WebsitePanel.EnterpriseServer.Code.SharePoint
|
|||
DomainInfo domain = ServerController.GetDomain(domainName);
|
||||
if (domain != null)
|
||||
{
|
||||
ServerController.DeleteDnsZoneRecord(domain.DomainId, hostName, DnsRecordType.A, hostedSharePointSettings["RootWebApplicationIpAddress"]);
|
||||
var ip = hostedSharePointSettings["RootWebApplicationIpAddress"];
|
||||
var type = ip.Contains(":") ? DnsRecordType.AAAA : DnsRecordType.A;
|
||||
|
||||
|
||||
ServerController.DeleteDnsZoneRecord(domain.DomainId, String.Empty, type, ip);
|
||||
ServerController.DeleteDnsZoneRecord(domain.DomainId, "www", type, ip);
|
||||
if (!String.IsNullOrEmpty(domain.WebSiteName))
|
||||
{
|
||||
DnsRecord[] records = ServerController.GetDnsZoneRecords(domain.DomainId);
|
||||
foreach (DnsRecord record in records)
|
||||
{
|
||||
type = record.RecordType;
|
||||
if ((type == DnsRecordType.A || type == DnsRecordType.AAAA) && record.RecordName.Equals("www", StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
ServerController.AddDnsZoneRecord(domain.DomainId, String.Empty, DnsRecordType.A,
|
||||
record.RecordData, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2968,15 +2968,17 @@ namespace WebsitePanel.EnterpriseServer
|
|||
if (String.IsNullOrEmpty(networkFormat))
|
||||
{
|
||||
// custom format
|
||||
nic.NetworkFormat = settings["PrivateIPAddress"];
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(settings["PrivateSubnetMask"]);
|
||||
nic.NetworkFormat = settings["PrivateIPAddress"];
|
||||
var v6 = IPAddress.Parse(nic.NetworkFormat).V6;
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(settings["PrivateSubnetMask"], v6);
|
||||
}
|
||||
else
|
||||
{
|
||||
// standard format
|
||||
string[] formatPair = settings["PrivateNetworkFormat"].Split('/');
|
||||
nic.NetworkFormat = formatPair[0];
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(formatPair[1]);
|
||||
var v6 = IPAddress.Parse(nic.NetworkFormat).V6;
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(formatPair[1], v6);
|
||||
}
|
||||
|
||||
nic.SubnetMaskCidr = GetSubnetMaskCidr(nic.SubnetMask);
|
||||
|
@ -3038,7 +3040,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
List<PrivateIPAddress> ips = GetPackagePrivateIPAddresses(vm.PackageId);
|
||||
|
||||
// sort them
|
||||
SortedList<long, string> sortedIps = GetSortedNormalizedIPAddresses(ips, nic.SubnetMask);
|
||||
SortedList<IPAddress, string> sortedIps = GetSortedNormalizedIPAddresses(ips, nic.SubnetMask);
|
||||
|
||||
if (selectRandom)
|
||||
{
|
||||
|
@ -3209,14 +3211,14 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return res;
|
||||
}
|
||||
|
||||
private static string GenerateNextAvailablePrivateIP(SortedList<long, string> ips, string subnetMask, string startIPAddress)
|
||||
private static string GenerateNextAvailablePrivateIP(SortedList<IPAddress, string> ips, string subnetMask, string startIPAddress)
|
||||
{
|
||||
// start IP address
|
||||
long startIp = ServerController.ConvertIPToLong(startIPAddress);
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
var startIp = IPAddress.Parse(startIPAddress);
|
||||
var mask = IPAddress.Parse(subnetMask);
|
||||
|
||||
long lastAddress = (startIp & ~mask) - 1;
|
||||
foreach (long addr in ips.Keys)
|
||||
var lastAddress = (startIp & ~mask) - 1;
|
||||
foreach (var addr in ips.Keys)
|
||||
{
|
||||
if ((addr - lastAddress) > 1)
|
||||
{
|
||||
|
@ -3229,11 +3231,11 @@ namespace WebsitePanel.EnterpriseServer
|
|||
}
|
||||
}
|
||||
|
||||
long genAddr = lastAddress + 1;
|
||||
var genAddr = lastAddress + 1;
|
||||
|
||||
// convert to IP address
|
||||
long ip = startIp & mask | (uint)genAddr;
|
||||
string genIP = ServerController.ConvertLongToIP(ip);
|
||||
var ip = startIp & mask | genAddr;
|
||||
string genIP = ip.ToString();
|
||||
|
||||
// store in cache
|
||||
ips.Add(genAddr, genIP);
|
||||
|
@ -3241,46 +3243,44 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return genIP;
|
||||
}
|
||||
|
||||
private static SortedList<long, string> GetSortedNormalizedIPAddresses(List<PrivateIPAddress> ips, string subnetMask)
|
||||
private static SortedList<IPAddress, string> GetSortedNormalizedIPAddresses(List<PrivateIPAddress> ips, string subnetMask)
|
||||
{
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
SortedList<long, string> sortedIps = new SortedList<long, string>();
|
||||
var mask = IPAddress.Parse(subnetMask);
|
||||
SortedList<IPAddress, string> sortedIps = new SortedList<IPAddress, string>();
|
||||
foreach (PrivateIPAddress ip in ips)
|
||||
{
|
||||
long addr = ~mask & ServerController.ConvertIPToLong(ip.IPAddress);
|
||||
var addr = ~mask & IPAddress.Parse(ip.IPAddress);
|
||||
sortedIps.Add(addr, ip.IPAddress);
|
||||
}
|
||||
return sortedIps;
|
||||
}
|
||||
|
||||
private static string GetPrivateNetworkSubnetMask(string cidr)
|
||||
{
|
||||
int digits = 32 - Utils.ParseInt(cidr, 0);
|
||||
|
||||
long mask = 0xFFFFFFFF;
|
||||
mask = mask << digits;
|
||||
return ServerController.ConvertLongToIP(mask);
|
||||
}
|
||||
|
||||
private static string GetSubnetMaskCidr(string subnetMask)
|
||||
{
|
||||
if (String.IsNullOrEmpty(subnetMask))
|
||||
return subnetMask;
|
||||
|
||||
int cidr = 32;
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
while ((mask & 1) == 0 && cidr > 0)
|
||||
{
|
||||
mask >>= 1;
|
||||
cidr -= 1;
|
||||
}
|
||||
return cidr.ToString();
|
||||
}
|
||||
private static string GetPrivateNetworkSubnetMask(string cidr, bool v6) {
|
||||
if (v6) return "/" + cidr;
|
||||
else return IPAddress.Parse("/" + cidr).ToV4MaskString();
|
||||
}
|
||||
|
||||
private static string GetSubnetMaskCidr(string subnetMask) {
|
||||
if (String.IsNullOrEmpty(subnetMask))
|
||||
return subnetMask;
|
||||
var ip = IPAddress.Parse(subnetMask);
|
||||
if (ip.V4) {
|
||||
int cidr = 32;
|
||||
long mask = (long)ip.Address;
|
||||
while ((mask & 1) == 0 && cidr > 0) {
|
||||
mask >>= 1;
|
||||
cidr -= 1;
|
||||
}
|
||||
return cidr.ToString();
|
||||
} else {
|
||||
return ip.Cidr.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool CheckPrivateIPAddress(string subnetMask, string ipAddress)
|
||||
{
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
long ip = ServerController.ConvertIPToLong(ipAddress);
|
||||
var mask = IPAddress.Parse(subnetMask);
|
||||
var ip = IPAddress.Parse(ipAddress);
|
||||
|
||||
return ((mask & ip) == mask);
|
||||
}
|
||||
|
|
|
@ -3019,14 +3019,16 @@ namespace WebsitePanel.EnterpriseServer
|
|||
{
|
||||
// custom format
|
||||
nic.NetworkFormat = settings["PrivateIPAddress"];
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(settings["PrivateSubnetMask"]);
|
||||
var v6 = IPAddress.Parse(nic.NetworkFormat).V6;
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(settings["PrivateSubnetMask"], v6);
|
||||
}
|
||||
else
|
||||
{
|
||||
// standard format
|
||||
string[] formatPair = settings["PrivateNetworkFormat"].Split('/');
|
||||
nic.NetworkFormat = formatPair[0];
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(formatPair[1]);
|
||||
var v6 = IPAddress.Parse(nic.NetworkFormat).V6;
|
||||
nic.SubnetMask = GetPrivateNetworkSubnetMask(formatPair[1], v6);
|
||||
}
|
||||
|
||||
nic.SubnetMaskCidr = GetSubnetMaskCidr(nic.SubnetMask);
|
||||
|
@ -3088,7 +3090,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
List<PrivateIPAddress> ips = GetPackagePrivateIPAddresses(vm.PackageId);
|
||||
|
||||
// sort them
|
||||
SortedList<long, string> sortedIps = GetSortedNormalizedIPAddresses(ips, nic.SubnetMask);
|
||||
SortedList<IPAddress, string> sortedIps = GetSortedNormalizedIPAddresses(ips, nic.SubnetMask);
|
||||
|
||||
if (selectRandom)
|
||||
{
|
||||
|
@ -3259,14 +3261,14 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return res;
|
||||
}
|
||||
|
||||
private static string GenerateNextAvailablePrivateIP(SortedList<long, string> ips, string subnetMask, string startIPAddress)
|
||||
private static string GenerateNextAvailablePrivateIP(SortedList<IPAddress, string> ips, string subnetMask, string startIPAddress)
|
||||
{
|
||||
// start IP address
|
||||
long startIp = ServerController.ConvertIPToLong(startIPAddress);
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
var startIp = IPAddress.Parse(startIPAddress);
|
||||
var mask = IPAddress.Parse(subnetMask);
|
||||
|
||||
long lastAddress = (startIp & ~mask) - 1;
|
||||
foreach (long addr in ips.Keys)
|
||||
var lastAddress = (startIp & ~mask) - 1;
|
||||
foreach (var addr in ips.Keys)
|
||||
{
|
||||
if ((addr - lastAddress) > 1)
|
||||
{
|
||||
|
@ -3279,11 +3281,11 @@ namespace WebsitePanel.EnterpriseServer
|
|||
}
|
||||
}
|
||||
|
||||
long genAddr = lastAddress + 1;
|
||||
var genAddr = lastAddress + 1;
|
||||
|
||||
// convert to IP address
|
||||
long ip = startIp & mask | (uint)genAddr;
|
||||
string genIP = ServerController.ConvertLongToIP(ip);
|
||||
var ip = startIp & mask | genAddr;
|
||||
string genIP = ip.ToString();
|
||||
|
||||
// store in cache
|
||||
ips.Add(genAddr, genIP);
|
||||
|
@ -3291,46 +3293,45 @@ namespace WebsitePanel.EnterpriseServer
|
|||
return genIP;
|
||||
}
|
||||
|
||||
private static SortedList<long, string> GetSortedNormalizedIPAddresses(List<PrivateIPAddress> ips, string subnetMask)
|
||||
private static SortedList<IPAddress, string> GetSortedNormalizedIPAddresses(List<PrivateIPAddress> ips, string subnetMask)
|
||||
{
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
SortedList<long, string> sortedIps = new SortedList<long, string>();
|
||||
var mask = IPAddress.Parse(subnetMask);
|
||||
SortedList<IPAddress, string> sortedIps = new SortedList<IPAddress, string>();
|
||||
foreach (PrivateIPAddress ip in ips)
|
||||
{
|
||||
long addr = ~mask & ServerController.ConvertIPToLong(ip.IPAddress);
|
||||
var addr = ~mask & IPAddress.Parse(ip.IPAddress);
|
||||
sortedIps.Add(addr, ip.IPAddress);
|
||||
}
|
||||
return sortedIps;
|
||||
}
|
||||
|
||||
private static string GetPrivateNetworkSubnetMask(string cidr)
|
||||
private static string GetPrivateNetworkSubnetMask(string cidr, bool v6)
|
||||
{
|
||||
int digits = 32 - Utils.ParseInt(cidr, 0);
|
||||
|
||||
long mask = 0xFFFFFFFF;
|
||||
mask = mask << digits;
|
||||
return ServerController.ConvertLongToIP(mask);
|
||||
if (v6) return "/" + cidr;
|
||||
else return IPAddress.Parse("/" + cidr).ToV4MaskString();
|
||||
}
|
||||
|
||||
private static string GetSubnetMaskCidr(string subnetMask)
|
||||
{
|
||||
if (String.IsNullOrEmpty(subnetMask))
|
||||
return subnetMask;
|
||||
|
||||
int cidr = 32;
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
while ((mask & 1) == 0 && cidr > 0)
|
||||
{
|
||||
mask >>= 1;
|
||||
cidr -= 1;
|
||||
}
|
||||
return cidr.ToString();
|
||||
}
|
||||
private static string GetSubnetMaskCidr(string subnetMask) {
|
||||
if (String.IsNullOrEmpty(subnetMask))
|
||||
return subnetMask;
|
||||
var ip = IPAddress.Parse(subnetMask);
|
||||
if (ip.V4) {
|
||||
int cidr = 32;
|
||||
long mask = (long)ip.Address;
|
||||
while ((mask & 1) == 0 && cidr > 0) {
|
||||
mask >>= 1;
|
||||
cidr -= 1;
|
||||
}
|
||||
return cidr.ToString();
|
||||
} else {
|
||||
return ip.Cidr.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool CheckPrivateIPAddress(string subnetMask, string ipAddress)
|
||||
{
|
||||
long mask = ServerController.ConvertIPToLong(subnetMask);
|
||||
long ip = ServerController.ConvertIPToLong(ipAddress);
|
||||
var mask = IPAddress.Parse(subnetMask);
|
||||
var ip = IPAddress.Parse(ipAddress);
|
||||
|
||||
return ((mask & ip) == mask);
|
||||
}
|
||||
|
|
|
@ -603,11 +603,12 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
private static void FillWebServerBindings(List<ServerBinding> bindings, List<GlobalDnsRecord> dnsRecords,
|
||||
string ipAddr, string domainName)
|
||||
// TODO test if IPv6 works
|
||||
{
|
||||
int bindingsCount = bindings.Count;
|
||||
foreach (GlobalDnsRecord dnsRecord in dnsRecords)
|
||||
{
|
||||
if (dnsRecord.RecordType == "A" &&
|
||||
if ((dnsRecord.RecordType == "A" || dnsRecord.RecordType == "AAAA") &&
|
||||
dnsRecord.RecordName != "*")
|
||||
{
|
||||
string recordData = dnsRecord.RecordName +
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue