110 lines
4.0 KiB
C#
110 lines
4.0 KiB
C#
using IPScanner;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Networking.Pages
|
|
{
|
|
public partial class WF_IPScanner : UserControl
|
|
{
|
|
NetworkScanner scanner = new NetworkScanner();
|
|
Timer timer = new Timer();
|
|
List<IPScanResult> results;
|
|
public WF_IPScanner()
|
|
{
|
|
InitializeComponent();
|
|
List<Tuple<IPAddress, IPAddress>> ipRanges = IPRanges.GetOperationalIPRanges();
|
|
if (ipRanges.Count > 0)
|
|
{
|
|
//tbIPRange.Text = ipRanges[0].Item1.GetAddressBytes().ToString();
|
|
tbIPRange.Text = IPtoString(ipRanges[0].Item1.GetAddressBytes()) + "-" + ipRanges[0].Item2.GetAddressBytes()[3].ToString();
|
|
// ipTo.IPAddress = ipRanges[0].Item2;
|
|
}
|
|
}
|
|
|
|
string IPtoString(byte[] array)
|
|
{
|
|
//
|
|
// Concatenate all the elements into a StringBuilder.
|
|
//
|
|
StringBuilder strinbuilder = new StringBuilder();
|
|
for (int i = 0; i < array.Count(); i++)
|
|
{
|
|
|
|
strinbuilder.Append(array[i]);
|
|
if (i != array.Count() - 1)
|
|
strinbuilder.Append('.');
|
|
}
|
|
return strinbuilder.ToString();
|
|
}
|
|
|
|
private string GetPingTime(IPScanResult result)
|
|
{
|
|
if (result.ping > -1)
|
|
return result.ping + " ms";
|
|
return "N/A";
|
|
}
|
|
|
|
private void PopulateListView()
|
|
{
|
|
bool itemModified = false;
|
|
for (int i = 0; i < results.Count; i++)
|
|
{
|
|
IPScanResult result = results[i];
|
|
if (result.status == ScanStatus.Complete || result.status == ScanStatus.Partial)
|
|
{
|
|
string ip = result.ip.ToString();
|
|
ListViewItem[] matchedItems = lvIPList.Items.Find(ip, false);
|
|
if (matchedItems.Length > 0)
|
|
{
|
|
matchedItems[0].Tag = result.response;
|
|
matchedItems[0].SubItems[0].Text = result.ip.ToString();
|
|
matchedItems[0].SubItems[1].Text = GetPingTime(result);
|
|
matchedItems[0].SubItems[2].Text = result.host;
|
|
matchedItems[0].SubItems[3].Text = result.identification;
|
|
}
|
|
else
|
|
{
|
|
ListViewItem lvi = new ListViewItem(new string[] { result.ip.ToString(), GetPingTime(result), result.host, result.identification });
|
|
lvi.Name = ip;
|
|
lvIPList.Items.Add(lvi);
|
|
}
|
|
itemModified = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
void timer_Tick(object sender, EventArgs e)
|
|
{
|
|
PopulateListView();
|
|
}
|
|
|
|
private void btnScan_Click(object sender, EventArgs e)
|
|
{
|
|
timer.Stop();
|
|
lvIPList.Items.Clear();
|
|
|
|
IPAddress ipFrom = IPAddress.Parse(tbIPRange.Text.Split(new Char[] { '-' })[0]);
|
|
|
|
IPAddress ipTo = IPAddress.Parse(ipFrom.GetAddressBytes()[0] + "." +
|
|
ipFrom.GetAddressBytes()[1] + "." +
|
|
ipFrom.GetAddressBytes()[2] + "." +
|
|
byte.Parse(tbIPRange.Text.Split(new Char[] { '-' })[1].ToString()));
|
|
results = scanner.BeginScan(ipFrom, ipTo);
|
|
timer.Interval = 1000;
|
|
timer.Tick += timer_Tick;
|
|
timer.Start();
|
|
}
|
|
}
|
|
}
|