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 { /// /// Interaktionslogik für Networking.xaml /// public partial class Networking_IPv4 : UserControl, IPlugin, IPage { #region Declarations protected IPTool ip = null; protected bool fill = false; // lock events wenn updating protected bool isIP = false; public string Author => "Kevin Krüger"; public string Company => "PeaceToke"; public string PluginName => "Networking"; public string Description => "Calculate networks very easy!"; public string Version => "1.0.0"; public string IconLink => "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.clipartmax.com%2Fpng%2Fmiddle%2F276-2769230_networking-deep-neural-network-icon.png&f=1&nofb=1"; public IPage Page => this; #endregion public Networking_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 Networking_IPv4(); #region Methods Used protected void newIP() { try { ip = new IPTool(tbIPAddress.Text); 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: tbIPAddress.Text = "1.0.0.1"; break; case 1: tbIPAddress.Text = "128.0.0.1"; break; case 2: tbIPAddress.Text = "192.0.0.1"; break; case 3: tbIPAddress.Text = "224.0.0.1"; break; case 4: tbIPAddress.Text = "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; tbIPAddress.Text = 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; tbIPAddress.Text = ip.getIp(); } catch (System.FormatException) { //nextNumOfHosts.BackColor = Color.Red; return; } catch (Exception e1) { MessageBox.Show(e1.StackTrace); return; } fillIPCalc(); } } }