private void scanBtn_Click(object sender, EventArgs e) { DialogResult res = MessageBox.Show(this, "Continuing will flood your subnet with ARP packets to look for any responses" + "\n\nThis wont do any harm, but might get you into trouble or at least look suspicious on networks YOU DO NOT OWN" + "\n\nAre you sure you want to do this?\n\n", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if ("OK".Equals(res.ToString())) { scanBtn.Text = "Scanning..."; string ipWithSubnet = _netCardInfo.OurIpAddress.ToString() + "/24"; IPNetwork net = IPNetwork.Parse(ipWithSubnet); IPNetworkCollection ips = IPNetwork.Subnet(net, 32); _ipsInSubnet = ips.GetIpRange(); outputBox.printIpRangeInfo(ips, ipWithSubnet); if (_netCardInfo != null) { new Thread(() => { ScanResult scanResult = PerformScan(_netCardInfo); _onlineClients = scanResult.OnlineClients; outputBox.PrintArpScanDetails(scanResult.Duration, _onlineClients, _netCardInfo.GatewayIpAddess, _netCardInfo.OurIpAddress); scanBtn.Invoke((MethodInvoker) delegate { scanBtn.Text = "Scan"; }); }).Start(); } } }
/// <summary> /// Parses a comma delimited list of IPv4 addresses in CIDR notation and stores them /// in the IpBlocks db table. Each IpBlock is then enumerated and all of the possible /// IP addresses in the range are stored in the IpAddresses db table. /// </summary> /// <param name="ipBlocks">A comma delimited list of IPv4 addresses in CIDR notation.</param> /// <param name="ipConfigId">The Id of the IpConfig that these IpBlocks belong to.</param> /// <returns></returns> private async Task ProcessIpBlocks(List <string> ipBlocks, long ipConfigId) { HashSet <string> blocks = new HashSet <string>(); for (int i = 0; i < ipBlocks.Count; i++) { // Remove whitespace from string ipBlocks.ElementAt(i).Replace(ipBlocks.ElementAt(i), Regex.Replace(ipBlocks.ElementAt(i), @"\s+", "")); // Check for duplicate IpBlocks if (!blocks.Add(ipBlocks.ElementAt(i))) { // TODO - One of the IpBlocks is a duplicate throw new NotSupportedException(); } } // For each Ipv4 address in CIDR notation, enumerate all the IPs in the range then add them to ipAddresses foreach (string cidr in ipBlocks) { IpBlock ipBlock = new IpBlock { CIDR = cidr, IpConfigId = ipConfigId }; // Store each CIDR IPv4 address in the IpBlocks db table _context.IpBlocks.Add(ipBlock); await _context._SaveChangesAsync(); IPNetwork block = IPNetwork.Parse(cidr); // Adjust the prefix relative to the IPsPerSub byte prefix = (byte)(32 - Math.Log((await _context.IpConfigs.FindAsync(ipConfigId)).IPsPerSub, 2)); // Break the IpBlock into smaller subnets in CIDR IPv4 notation IPNetworkCollection subnets = block.Subnet(prefix); // This list will contain all of the enumerated IP address entities for the ipBlock List <IpAddress> ipAddressEntities = new List <IpAddress>(); foreach (IPNetwork network in subnets) { IpAddress ip = new IpAddress { Value = network.Value, IsAvailable = true, IpBlockId = ipBlock.Id }; ipAddressEntities.Add(ip); } // Store all of the enumerated IP address entities in the IpAddresses db table _context.IpAddresses.AddRange(ipAddressEntities); await _context._SaveChangesAsync(); } }
public void MoveNext1() { IPNetwork ipn = IPNetwork.Parse("192.168.1.0/30"); using (IPNetworkCollection ipns = ipn.Subnet(32)) { bool next = ipns.MoveNext(); Assert.AreEqual(true, next, "next"); } }
public IPAddress this[double i] { get { if (i >= this.Count) { throw new ArgumentOutOfRangeException("i"); } IPNetworkCollection ipn = IPNetwork.Subnet(this._ipnetwork, 32); return(ipn[i].Network); } }
public IPAddress this[BigInteger i] { get { if (i >= this.Count) { throw new ArgumentOutOfRangeException("i"); } byte width = this._ipnetwork.AddressFamily == Sockets.AddressFamily.InterNetwork ? (byte)32 : (byte)128; IPNetworkCollection ipn = IPNetwork.Subnet(this._ipnetwork, width); return(ipn[i].Network); } }
public void TestEnumerator() { IPNetwork ipn = IPNetwork.Parse("192.168.0.0/32"); using (IPNetworkCollection ipns = ipn.Subnet(32)) { var ipnse = (Collections.IEnumerable)ipns; var ee = ipnse.GetEnumerator(); ee.MoveNext(); var ipn0 = ee.Current; Assert.AreEqual("192.168.0.0/32", ipn0.ToString(), "ipn0"); } }
private async void Subnetting() { DisplayStatusMessage = false; IsCalculationRunning = true; SubnetsResult.Clear(); IPNetwork subnet = IPNetwork.Parse(Subnet); byte.TryParse(NewSubnetmaskOrCIDR.TrimStart('/'), out byte newCidr); // Ask the user if there is a large calculation... int baseCidr = subnet.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? 32 : 128; if (65535 < (Math.Pow(2, (baseCidr - subnet.Cidr)) / Math.Pow(2, (baseCidr - newCidr)))) { MetroDialogSettings settings = AppearanceManager.MetroDialog; settings.AffirmativeButtonText = LocalizationManager.GetStringByKey("String_Button_Continue"); settings.NegativeButtonText = LocalizationManager.GetStringByKey("String_Button_Cancel"); settings.DefaultButtonFocus = MessageDialogResult.Affirmative; if (await dialogCoordinator.ShowMessageAsync(this, LocalizationManager.GetStringByKey("String_Header_AreYouSure"), LocalizationManager.GetStringByKey("String_TheProcessCanTakeUpSomeTimeAndResources"), MessageDialogStyle.AffirmativeAndNegative, settings) != MessageDialogResult.Affirmative) { IsCalculationRunning = false; return; } } // This still slows the application / freezes the ui... there are to many updates to the ui thread... await Task.Run(() => { foreach (IPNetwork network in subnet.Subnet(newCidr)) { Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate() { lock (SubnetsResult) SubnetsResult.Add(network); })); } }); IsResultVisible = true; AddSubnetToHistory(Subnet); AddNewSubnetmaskOrCIDRToHistory(NewSubnetmaskOrCIDR); IsCalculationRunning = false; }
public void TestReset1() { IPNetwork ipn = IPNetwork.Parse("192.168.1.0/29"); using (IPNetworkCollection ipns = ipn.Subnet(32)) { var ipn0 = ipns.Current; ipns.MoveNext(); ipns.Reset(); var ipn1 = ipns.Current; Assert.AreEqual(ipn0, ipn1, "reset"); } }
public Subnet() { string rndIP = rnd.Next(0, 256) + "." + rnd.Next(0, 256) + "." + rnd.Next(0, 256) + "." + rnd.Next(0, 256) + "/" + rnd.Next(4, 29); IPNetwork ipnetwork = IPNetwork.Parse(rndIP); IpAdress = rndIP.Substring(0, rndIP.IndexOf('/')); CidrOld = ipnetwork.Cidr; int limit = (CidrOld + 20 < 30) ? CidrOld + 20 : 30; CidrNew = rnd.Next(CidrOld + 1, limit); IPNetworkCollection subneted = ipnetwork.Subnet((byte)CidrNew); subnetCount = (int)subneted.Count; subnetArr = new string[subnetCount]; for (int i = 0; i < subnetCount; i++) { subnetArr[i] = Convert.ToString(subneted[i]); } }
private void Send_pings(object data) { List <string> datapassed = (List <string>)data; string IP_label = datapassed[0]; string subnet = datapassed[1]; bool validsubnet = false; List <string> Addresses = new List <string>(); //Addresses.Add("aalsa007"); try { IPNetwork net = IPNetwork.Parse(subnet); IPNetworkCollection ips = IPNetwork.Subnet(net, 32); validsubnet = true; foreach (IPNetwork ipnetwork in ips) { Addresses.Add(ipnetwork.ToString().Substring(0, ipnetwork.ToString().Length - 3)); } } catch { MessageBox.Show("Invalid Subnet"); } if (validsubnet) { Set_IP_of_label("/ " + Addresses.Count.ToString()); List <Task <PingReply> > pingTasks = new List <Task <PingReply> >(); for (int i = 0; i < Addresses.Count; i++) { pingTasks.Add(PingAsync(Addresses[i])); } List <string> finishedIDs = new List <string>(); List <string> toadd = new List <string>(); finishedIDs.Add("Not an ip"); bool alldone = false; //while (finishedIDs.Count != Addresses.Count) while (!alldone) { alldone = true; foreach (var t in pingTasks.ToArray()) { update_progressbar((float)finishedIDs.Count / (float)Addresses.Count); if (t.IsCompleted) { if (t.Result != null) { bool isShown = false; foreach (var ID in finishedIDs) { if (t.Result.Address.ToString() == ID) { isShown = true; } } if (!isShown) { if (t.Result.Address.ToString() != "0.0.0.0") { //Thread updateUEthread = new Thread(new ParameterizedThreadStart(add_IP)); List <string> results = new List <string>(); //add_IP(t.Result.RoundtripTime.ToString() + " ms", t.Result.Address.ToString(), GetHostName(t.Result.Address.ToString()), RDP); results.Add(t.Result.Address.ToString()); results.Add("-"); results.Add(t.Result.RoundtripTime.ToString() + " ms"); results.Add("-"); results.Add("-"); results.Add("-"); add_IP(results); toadd.Add(t.Result.Address.ToString()); Thread hostname_thread = new Thread(new ParameterizedThreadStart(update_hostname)); hostname_thread.Start(t.Result.Address.ToString()); Thread RDP_thread = new Thread(new ParameterizedThreadStart(update_RDP)); RDP_thread.Start(t.Result.Address.ToString()); Thread SSH_thread = new Thread(new ParameterizedThreadStart(update_SSH)); SSH_thread.Start(t.Result.Address.ToString()); Thread HTTP_thread = new Thread(new ParameterizedThreadStart(update_HTTP)); HTTP_thread.Start(t.Result.Address.ToString()); } } } foreach (var toaddID in toadd) { finishedIDs.Add(toaddID); } toadd.Clear(); //MessageBox.Show(t.Result.RoundtripTime.ToString() + t.Result.Address); } else { alldone = false; } } if (alldone) { update_progressbar(1); } Thread.Sleep(10); } } }
static IPNetwork[] Split(IPNetwork iPNetwork) { return(iPNetwork.Subnet((byte)(iPNetwork.Cidr + 1)).ToArray()); }