Files
Kevin Krüger bb6c229875 -> IPScanner added
-> PortScanner added
2022-01-17 15:17:34 +01:00

340 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NetCalc;
using PTConverter.Plugin;
namespace Networking.Pages
{
/// <summary>
/// Interaktionslogik für Networking.xaml
/// </summary>
public partial class IPv4 : UserControl, IPage
{
#region Declarations
protected IPTool ip = null;
protected bool fill = false; // lock events wenn updating
protected bool isIP = false;
#endregion
private string _ipAddress = "0.0.0.0";
private string IPAddress { get => tbIPAddressP1.Text + "." + tbIPAddressP2.Text + "." + tbIPAddressP3.Text + "." + tbIPAddressP4.Text; set => _ipAddress = value; }
public IPv4()
{
InitializeComponent();
tbNETClass.ItemsSource = new object[] {
"Class A: 0.0.0.0-127.255.255.255",
"Class B: 128.0.0.0-191.255.255.255",
"Class C: 192.0.0.0-223.255.255.255",
"Class D: 224.0.0.0-239.255.255.255",
"Class E: 240.0.0.0-254.255.255.255"};
tbNETClass.SelectedIndex = 0;
}
public string GetCategory() => "Networking";
public string GetUnderCategory() => "IPv4";
public UserControl GetPage() => new IPv4();
#region Methods Used
protected void newIP()
{
try
{
ip = new IPTool(IPAddress);
fillIPCalc();
}
catch (Exception e)
{
MessageBox.Show(e.StackTrace);
}
//catch (UnknownHostException e)
//{
// main.getJp_Calc().getJtf_IP_Calc().setBackground(java.awt.Color.red);
//}
}
protected void fillIPCalc()
{
if (ip == null || ip.getIp() == null) return;
this.fill = true;
this.fillComboBox_Prefix();
this.fillComboBox_NetworkMask();
// Prefix
int prefix = ip.getNetworkPrefix();
if (prefix >= 8 && prefix <= 30)
{
if (prefix != ip.getNetworkClassPrefix() && !ip.isFirstSubNetBit()) prefix--;
prefix = prefix - ip.getNetworkClassPrefix();
}
else prefix = -1;
prefixComboBox.SelectedIndex = prefix;
// NetMask
netMaskComboBox.SelectedIndex = prefix;
// folgendes nur wenn Netzwerkmaske vorhanden
// nicht der Fall bei Class-D oder E
if (prefix != -1)
{
// max Net Hosts
maxCountHosts.Text = ip.getMaxNetworkHosts() + "";
// Net Class
calcNetwork.Text = ip.getNetworkClassName();
// Broadcast
calcBroadcast.Text = ip.getBroadCast();
// Network
calcNetwork.Text = ip.getNetwork();
// Net-Class
tbNETClass.SelectedIndex = ip.getNetworkClass();
// IP-Range
string[] ipRange = ip.getNetworkIPRange();
calcIPRange.Text = ipRange[0] + " - " + ipRange[1];
// max Sub-Nets
clacMaxNetCount.Text = ip.getMaxSubNets() + "";
}
else // Anzeige löschen
{
// max Net Hosts
maxCountHosts.Text = "";
// Net Class
calcNetwork.Text = "";
// Broadcast
calcBroadcast.Text = "";
// Network
calcNetwork.Text = "";
// IP-Range
calcIPRange.Text = "";
// max Sub-Nets
clacMaxNetCount.Text = "";
}
// Description
string desc = "";
if (ip.get1Byte() >= (byte)64 && ip.getNetworkClass() == (byte)0) // 64.
desc = "reserved for future use";
if (ip.get1Byte() == (byte)127) // localhost
desc = "localhost and loopback-Device - your one computer";
if (ip.get1Byte() == (byte)10) // private 10.0.0.0
desc = "private net - for internal use only, would not be routed in internet";
if (ip.get1Byte() == (byte)192 && ip.get2Byte() == (byte)168) // private 192. - 168.
desc = "private net - for internal use only, would not be routed in internet";
if (ip.get1Byte() == (byte)172 && (ip.get2Byte() >= (byte)16 && ip.get2Byte() <= (byte)31)) //private 172.16 - 172.31
desc = "private net - for internal use only, would not be routed in internet";
if ((ip.get1Byte() == (byte)169) && (ip.get2Byte() == (byte)254)) // 169.254.
desc = "APIPA Automatic Private IP Addressing";
if (ip.get1Byte() == (byte)0) // 0.
desc = "0.0.0.0 = default route";
if (ip.getNetworkClass() == (byte)4) // Class-E
desc = "reserved for future use";
if (ip.getNetworkClass() == (byte)3) // Class-D
desc = "used for multicast";
noticeLabel.Content = "Info stuff: " + desc;
this.fill = false;
}
protected void fillComboBox_Prefix()
{
if (ip == null || ip.getIp() == null) return;
prefixComboBox.Items.Clear();
if (ip.getNetworkClassPrefix() == -1)
{
prefixComboBox.Items.Add("");
return;
}
int startPrefix = ip.getNetworkClassPrefix();
// first is Networkclass
prefixComboBox.Items.Add(startPrefix + "");
startPrefix++;
if (!ip.isFirstSubNetBit()) startPrefix++;
for (int i = startPrefix; i <= 30; i++)
prefixComboBox.Items.Add(i + "");
}
protected void fillComboBox_NetworkMask()
{
if (ip == null || ip.getIp() == null) return;
netMaskComboBox.Items.Clear();
if (ip.getNetworkClassPrefix() == -1)
{
netMaskComboBox.Items.Add("");
return;
}
int startPrefix = ip.getNetworkClassPrefix();
// first is Networkclass
netMaskComboBox.Items.Add(ip.getNetworkMaskByPrefix(startPrefix));
startPrefix++;
if (!ip.isFirstSubNetBit()) startPrefix++;
for (int i = startPrefix; i <= 30; i++)
netMaskComboBox.Items.Add(ip.getNetworkMaskByPrefix(i));
}
#endregion
#region Events
private void ipAddress_Leave(object sender, System.EventArgs e)
{
newIP();
}
private void ipAddress_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
newIP();
}
}
private void prefixComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (ip == null || ip.getIp() == null || fill) return;
if (ip.getNetworkClassPrefix() == -1) return;
int prefix = Int32.Parse((string)prefixComboBox.SelectedItem);
ip.setNetworkPrefix(prefix);
fillIPCalc();
}
private void netMaskComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (ip == null || ip.getIp() == null || fill) return;
if (ip.getNetworkClassPrefix() == -1) return;
int select = netMaskComboBox.SelectedIndex;
int prefix = select + ip.getNetworkClassPrefix();
if (select != 0 && !ip.isFirstSubNetBit()) prefix++;
//int prefix = Integer.parseInt((String)main.getJcb_Calc_NetworkMask().getSelectedItem());
ip.setNetworkPrefix(prefix);
fillIPCalc();
}
private void netClassComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (fill) return;
switch (tbNETClass.SelectedIndex)
{
case 0:
IPAddress = "1.0.0.1";
break;
case 1:
IPAddress = "128.0.0.1";
break;
case 2:
IPAddress = "192.0.0.1";
break;
case 3:
IPAddress = "224.0.0.1";
break;
case 4:
IPAddress = "240.0.0.1";
break;
}
newIP();
}
private void firstBitCheckBox_CheckedChanged(object sender, System.EventArgs e)
{
if (ip == null || ip.getIp() == null || fill) return;
ip.setFirstSubNetBit((bool)firstBitCheckBox.IsChecked);
fillIPCalc();
}
private void nextSubnetButt_Click(object sender, System.EventArgs e)
{
if (ip == null || ip.getIp() == null || fill) return;
///nextNumOfHosts.BackColor = Color.White;
try
{
int hosts = Int32.Parse(nextNumOfHosts.Text);
IPTool newIp = ip.getNextSubNet(hosts);
if (newIp == null)
{
//ToManyHosts
noticeLabel.Content = "Info stuff: Number of hosts in this network-class not possible!";
return;
}
ip = newIp;
IPAddress = ip.getIp();
}
catch (System.FormatException)
{
//nextNumOfHosts.BackColor = Color.Red;
return;
}
catch (Exception e1)
{
MessageBox.Show(e1.StackTrace);
return;
}
fillIPCalc();
}
private void calcButt_Click(object sender, System.EventArgs e)
{
newIP();
}
#endregion
private void btnNextSubnet_Click(object sender, RoutedEventArgs e)
{
if (ip == null || ip.getIp() == null || fill) return;
try
{
int hosts = Int32.Parse(nextNumOfHosts.Text);
IPTool newIp = ip.getNextSubNet(hosts);
if (newIp == null)
{
//ToManyHosts
noticeLabel.Content = "Info stuff: Number of hosts in this network-class not possible!";
return;
}
ip = newIp;
IPAddress = ip.getIp();
}
catch (System.FormatException)
{
//nextNumOfHosts.BackColor = Color.Red;
return;
}
catch (Exception e1)
{
MessageBox.Show(e1.StackTrace);
return;
}
fillIPCalc();
}
}
}