2015-08-01 11:58:53 -06:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IPAddressEntry(IPAddress toEdit, NetworkDevice ToEdit)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
LanguagifyComponents();
|
|
|
|
|
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;
|
|
|
|
|
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 (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("IPAE_btnCancel");
|
|
|
|
|
Text = NB.Translate("IPAE_btnOK");
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
ShowDialog();
|
|
|
|
|
return WellDone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Edit(NetworkDevice ToEdit, IPAddress DHCPif)
|
|
|
|
|
{
|
|
|
|
|
DHCPInterface = DHCPif;
|
2015-11-16 14:17:07 -06:00
|
|
|
|
lblIP.Text = NB.Translate("IPAE_EditIf");
|
2015-11-16 14:30:58 -06:00
|
|
|
|
lblNetmask.Text = NB.Translate("IPAE_EditStart");
|
|
|
|
|
lblGateway.Text = NB.Translate("IPAE_EditEnd");
|
2015-08-01 11:58:53 -06:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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;
|
2015-11-16 14:17:07 -06:00
|
|
|
|
MessageBox.Show(NB.Translate("IPAE_tbGateValEndIPRange"));
|
2015-08-01 11:58:53 -06:00
|
|
|
|
}
|
|
|
|
|
if (taddress < startaddress)
|
|
|
|
|
{
|
|
|
|
|
e.Cancel = true;
|
2015-11-16 14:30:58 -06:00
|
|
|
|
MessageBox.Show(NB.Translate("IPAE_tbGateValIPEqualLarger"));
|
2015-08-01 11:58:53 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2015-11-16 14:30:58 -06:00
|
|
|
|
MessageBox.Show(NB.Translate("IPAE_tbNtmskValEndIPRange"));
|
2015-08-01 11:58:53 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|