263 lines
9.3 KiB
C#
263 lines
9.3 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
|
|
{
|
|
IPAddress WhatToEdit;
|
|
IPAddress DHCPInterface=null;
|
|
bool WellDone = true;
|
|
NetworkDevice ParentDevice = null;
|
|
Point StartLocation = new Point (50,50);
|
|
|
|
|
|
public IPAddressEntry(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();
|
|
IPAddress lastIP = myNet.RetrieveLastIP();
|
|
WhatToEdit = toEdit;
|
|
string hostname = "";
|
|
if (ToEdit != null)
|
|
hostname = ToEdit.hostname;
|
|
if (toEdit.GetIP.ToIpString() == NB.ZeroIPString)
|
|
{
|
|
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);
|
|
Close();
|
|
}
|
|
|
|
public bool Edit()
|
|
{
|
|
Location = StartLocation;
|
|
ShowDialog();
|
|
return WellDone;
|
|
}
|
|
|
|
public bool Edit(NetworkDevice ToEdit, 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)
|
|
{
|
|
UpdateFieldsFromAddress();
|
|
WellDone = false;
|
|
Close();
|
|
}
|
|
|
|
private void tbGateway_Validating(object sender, CancelEventArgs e)
|
|
{
|
|
Network mynet = NB.GetNetwork();
|
|
if (ParentDevice != null)
|
|
{
|
|
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 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)
|
|
{
|
|
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 IPAddress(tbNetmask.Text)))
|
|
{
|
|
e.Cancel = true;
|
|
MessageBox.Show(NB.Translate("IPAE_tbNtmskValEndIPRange"));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void tbIPAddress_Validating(object sender, CancelEventArgs e)
|
|
{
|
|
UInt32 taddress;
|
|
Network mynet = NB.GetNetwork();
|
|
IPAddress tIPAddress = null;
|
|
if (ParentDevice != null)
|
|
{
|
|
tIPAddress = mynet.DNSLookup(ParentDevice, tbIPAddress.Text);
|
|
}
|
|
if(tIPAddress != null)
|
|
{
|
|
taddress = tIPAddress.GetIP;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|