EduNetworkBuilder/EduNetworkBuilder/IPAddressEntry.cs

310 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class IPAddressEntry : Form
{
NB_IPAddress WhatToEdit;
NB_IPAddress DHCPInterface=null;
bool WellDone = false;
NetworkDevice ParentDevice = null;
Point StartLocation = new Point (50,50);
NB_IPAddress SavedIPAddress = null;
ToolTip myTooltip = new ToolTip();
//variable to hold true if the for is closing
private bool isFormClosing = false;
// Contant for the close message
private const int WM_CLOSE = 16;
//override the WndProc msg to trap the WM_CLOSE message
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE)
isFormClosing = true;
base.WndProc(ref m);
}
public IPAddressEntry(NB_IPAddress toEdit, NetworkDevice ToEdit, Form ParentForm, bool JustPinging=false)
{
InitializeComponent();
LanguagifyComponents();
if(ParentForm != null)
{
Point NewPoint = new Point(ParentForm.Location.X + (ParentForm.Width / 2), ParentForm.Location.Y + (ParentForm.Height / 2));
StartLocation = NewPoint;
}
ParentDevice = ToEdit;
Network myNet = NB.GetNetwork();
NB_IPAddress lastIP = myNet.RetrieveLastIP();
SavedIPAddress = new NB_IPAddress(toEdit);
WhatToEdit = toEdit;
string hostname = "";
if (ToEdit != null)
hostname = ToEdit.hostname;
if (toEdit.IsZeroString())
{
string lIP = lastIP.GetIP.ToIpString();
string lNM = lastIP.GetMask.ToIpString();
string lGW = lastIP.GetGateway.ToIpString();
switch (WhatToEdit.GetAddressType)
{
case IPAddressType.gw:
lNM = NB.ZeroIPString;
lGW = NB.ZeroIPString;
break;
case IPAddressType.ip:
case IPAddressType.ip_only:
if (!lNM.Contains("255"))
lNM = "255.255.255.0";
lGW = NB.ZeroIPString;
break;
case IPAddressType.route:
break;
}
WhatToEdit.Reparse(lIP, lNM, lGW);
}
UpdateFieldsFromAddress();
switch (WhatToEdit.GetAddressType)
{
case IPAddressType.gw:
tbGateway.Enabled = false;
tbGateway.Visible = false; //This just confuses people
tbIPAddress.Enabled = true;
tbNetmask.Enabled = false;
lblGateway.Visible = false;
break;
case IPAddressType.ip:
tbGateway.Enabled = false;
tbIPAddress.Enabled = true;
tbNetmask.Enabled = true;
break;
case IPAddressType.route:
tbGateway.Enabled = true;
tbIPAddress.Enabled = true;
tbNetmask.Enabled = true;
break;
case IPAddressType.ip_only:
tbGateway.Enabled = false;
tbIPAddress.Enabled = true;
tbNetmask.Enabled = false;
break;
}
//Disable anythingthatis locked
switch (WhatToEdit.GetAddressType)
{
case IPAddressType.gw:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockGateway))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
break;
case IPAddressType.ip:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockIP))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
break;
case IPAddressType.route:
if (myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockRoute))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
break;
case IPAddressType.ip_only:
if (!JustPinging && myNet.ItemIsLocked(hostname, tbIPAddress.Text, NetTestType.LockIP))
{
tbIPAddress.Enabled = false;
}
break;
}
}
private void LanguagifyComponents()
{
//ResourceManager RM = NB.GetResource();
//CultureInfo CI = NB.GetCulture();
Text = NB.Translate("IPAE_lblIP");
Text = NB.Translate("IPAE_lblNetmask");
Text = NB.Translate("IPAE_lblGateway");
Text = NB.Translate("_Cancel");
Text = NB.Translate("_OK");
Text = NB.Translate("IPAE_Form");
}
private void UpdateFieldsFromAddress()
{
tbIPAddress.Text = WhatToEdit.GetIP.ToIpString();
tbNetmask.Text = WhatToEdit.GetMask.ToIpString();
tbGateway.Text = WhatToEdit.GetGateway.ToIpString();
}
private void btnOK_Click(object sender, EventArgs e)
{
WhatToEdit.Reparse(tbIPAddress.Text, tbNetmask.Text, tbGateway.Text);
Network myNet = NB.GetNetwork();
myNet.StoreLastIP(WhatToEdit);
WellDone = true;
Close();
}
public bool Edit()
{
Location = StartLocation;
ShowDialog();
return WellDone;
}
public bool Edit(NetworkDevice ToEdit, NB_IPAddress DHCPif)
{
DHCPInterface = DHCPif;
lblIP.Text = NB.Translate("IPAE_EditIf");
lblNetmask.Text = NB.Translate("IPAE_EditStart");
lblGateway.Text = NB.Translate("IPAE_EditEnd");
tbIPAddress.Enabled = false;
tbGateway.Enabled = true;
tbNetmask.Enabled = true;
Network myNet = NB.GetNetwork();
if (myNet.ItemIsLocked(ToEdit.hostname, tbIPAddress.Text, NetTestType.LockDHCP))
{
tbGateway.Enabled = false;
tbIPAddress.Enabled = false;
tbNetmask.Enabled = false;
}
Location = StartLocation;
ShowDialog();
return WellDone;
}
private void btnCancel_Click(object sender, EventArgs e)
{
if(SavedIPAddress != null)
WhatToEdit.Reparse(SavedIPAddress.GetIPString, SavedIPAddress.GetMaskString, SavedIPAddress.GetGateway.ToIpString());
UpdateFieldsFromAddress();
Close();
}
private void tbGateway_Validating(object sender, CancelEventArgs e)
{
if (isFormClosing) return;
Network mynet = NB.GetNetwork();
if (ParentDevice != null)
{
NB_IPAddress tAddress = mynet.DNSLookup(ParentDevice, tbGateway.Text);
}
UInt32 taddress = tbGateway.Text.ParseIp();
UInt32 startaddress = tbNetmask.Text.ParseIp();
tbGateway.Text = taddress.ToIpString();
if (DHCPInterface != null)
{
//We also check to verify that the address is in the correct range
if (!DHCPInterface.IsLocal(new NB_IPAddress(tbGateway.Text)))
{
e.Cancel = true;
MessageBox.Show(NB.Translate("IPAE_tbGateValEndIPRange"));
}
if (taddress < startaddress)
{
e.Cancel = true;
MessageBox.Show(NB.Translate("IPAE_tbGateValIPEqualLarger"));
}
}
}
private void tbNetmask_Validating(object sender, CancelEventArgs e)
{
if (isFormClosing) return;
UInt32 taddress = tbNetmask.Text.ParseIp();
tbNetmask.Text = taddress.ToIpString();
if (DHCPInterface != null)
{
//We also check to verify that the address is in the correct range
if (!DHCPInterface.IsLocal(new NB_IPAddress(tbNetmask.Text)))
{
e.Cancel = true;
MessageBox.Show(NB.Translate("IPAE_tbNtmskValEndIPRange"));
}
}
}
private void tbIPAddress_Validating(object sender, CancelEventArgs e)
{
if (isFormClosing) return;
UInt32 taddress;
Network mynet = NB.GetNetwork();
NB_IPAddress tIPAddress = null;
if (ParentDevice != null)
{
tIPAddress = mynet.DNSLookup(ParentDevice, tbIPAddress.Text);
}
if (tIPAddress != null) //device name used, convert to IP address and netmask
{
taddress = tIPAddress.GetIP;
if (DHCPInterface == null)
tbNetmask.Text = tIPAddress.GetMaskString;
}
else
taddress = tbIPAddress.Text.ParseIp();
tbIPAddress.Text = taddress.ToIpString();
}
public void SetText(string text)
{
this.Text = text;
}
private void IPAddressEntry_Shown(object sender, EventArgs e)
{
Point tLocation = new Point (StartLocation.X - Width, StartLocation.Y - Height);
if (tLocation.X < 0 || tLocation.Y < 0) tLocation = new Point(50, 50);
Location = tLocation;
}
public void ShowAsSubnetGateway()
{
lblIP.Text = NB.Translate("IPE_Network");
Text = NB.Translate("NB_Subnet");
myTooltip.AutoPopDelay = 5000;
myTooltip.InitialDelay = 1000;
myTooltip.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
myTooltip.ShowAlways = true;
myTooltip.SetToolTip(tbIPAddress, NB.Translate("IPE_NetworkTooltip"));
myTooltip.SetToolTip(tbGateway, NB.Translate("IPE_GatewayTooltip"));
myTooltip.SetToolTip(tbNetmask, NB.Translate("IPE_NetmaskTooltip"));
myTooltip.SetToolTip(lblIP, NB.Translate("IPE_NetworkTooltip"));
myTooltip.SetToolTip(lblGateway, NB.Translate("IPE_GatewayTooltip"));
myTooltip.SetToolTip(lblNetmask, NB.Translate("IPE_NetmaskTooltip"));
ShowDialog();
}
}
}