public SubmitIPAddressCommand(IPAddressViewModel viewModel) { _viewModel = viewModel; }
public List <IPAddressPool> GenerateIPBeforeLast(string ipSource, int bitCount) { var bitMod = bitCount % 8; var heads = new List <int>(); var headCount = (int)Math.Pow(2, bitMod); var headBase = (int)Math.Pow(2, 8 - bitMod); for (var i = 0; i < headCount; i++) { heads.Add(headBase * i); } var ipTemplate = new IPAddressViewModel(ipSource); var result = new List <IPAddressPool>(); var subnetMask = GenerateSubnetMask(bitCount); var count = 0; var startIndex = 0; var compareValue = ipTemplate.Fourth; for (var i = 0; i < heads.Count - 1; i++) { if (heads[i] <= compareValue && compareValue < heads[i + 1]) { startIndex = heads[i]; break; } else if (heads[i] <= compareValue && compareValue <= 255) { startIndex = heads[i + 1]; } } for (var i = startIndex; i < 256 && count < 2; i++) { if (heads.Contains(i)) { count++; if (count < 2) { ipTemplate.Fourth = i; var ip = new IPAddressPool { IPAddress = ipTemplate.ToString(), Subnetmask = subnetMask }; result.Add(ip); } } else { ipTemplate.Fourth = i; var ip = new IPAddressPool { IPAddress = ipTemplate.ToString(), Subnetmask = subnetMask }; result.Add(ip); } } int k = result.Count - 2; for (int i = 0; i < result.Count; i++) { result[i].Gateway = result[k].IPAddress; result[i].NetworkIP = result[0].IPAddress; } return(result); }