EduNetworkBuilder/EduNetworkBuilder/NetworkInterface.cs
2015-09-01 15:52:08 -05:00

126 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Text.RegularExpressions;
namespace EduNetworkBuilder
{
[Serializable]
public class NetworkInterface
{
public IPAddress myIP;
public string nic_name; //eth0, eth0:0, etc
public HostNicID AttachedToHostNic;
//We should have a feature: none, IP_in_IP_Tunnel, encrypted_vpn, vlan, etc
public NetworkInterface(string name, string IP, string Mask, HostNicID AttachedTo)
{
nic_name = name;
myIP = new IPAddress(IP, Mask,IPAddressType.ip);
AttachedToHostNic = AttachedTo;
}
public NetworkInterface(XmlNode theNode, HostNicID AttachedTo)
{
AttachedToHostNic = AttachedTo;
foreach (XmlNode Individual in theNode.ChildNodes)
{
XmlNodeType myNodetype = Individual.NodeType;
if (myNodetype == XmlNodeType.Element)
{
switch (Individual.Name.ToLower())
{
case "myip":
myIP = new IPAddress(Individual);
break;
case "nicname":
nic_name = Individual.InnerText;
break;
}
}
}
}
public void Save(XmlWriter writer)
{
writer.WriteStartElement("interface");
writer.WriteElementString("nicname", nic_name);
myIP.Save(writer, "myip");
writer.WriteEndElement();
}
public string InterfaceString(bool UseCidr = false)
{
string tstring = myIP.GetIP.ToIpString();
if (!UseCidr)
{
tstring += " - " + myIP.GetMask.ToIpString();
}
else
{
string mask = myIP.GetMask.ToBitString();
mask = Regex.Replace(mask, "0", "");
int count = mask.Length;
tstring += "/" + count.ToString();
}
return tstring;
}
public string RouteString(string nic_name, string GW)
{
string tstring = myIP.IPFormat(GW); ;
return tstring;
}
/// <summary>
/// Pop up a window to edit this address.
/// </summary>
public void EditAddress()
{
Network myNet = NB.GetNetwork();
NetworkDevice ND = myNet.GetDeviceFromID(AttachedToHostNic);
IPAddressEntry ipe = new IPAddressEntry(myIP,ND);
ipe.ShowDialog();
}
public bool isLocal(IPAddress tIp, bool AllowZeroMatch = true)
{
if (tIp == null) return false;
if (!AllowZeroMatch && (myIP == null || myIP.GetIP == 0))
return false;
if (myIP.IsLocal(tIp))
return true;
return false;
}
/// <summary>
/// We are sending the packet out this interface.
/// </summary>
/// <param name="tPacket"></param>
public void ProcessOutboundPacket(Packet tPacket)
{
if (tPacket.sourceIP == null || tPacket.sourceIP.GetIP.ToIpString() == NB.ZeroIPString)
{
//This happens if we are starting a new packet. We should also do this if we are masquerading.
tPacket.sourceIP = new IPAddress(myIP.GetIP.ToIpString(), "", IPAddressType.ip_only); //We only want the IP address
}
}
public void ProcessInboundPacket(Packet tPacket)
{
bool isgood = false;
if (tPacket.destIP != null && tPacket.destIP.GetIP == myIP.GetIP) isgood = true;
if (tPacket.destIP != null && myIP.IsLocal(tPacket.destIP)) isgood = true;
if (myIP.NetworkAddress == myIP.GetIP) isgood = true;
if(isgood)
{
//anything we should do here?
//Mainly vlan if we are a vlan.
}
}
}
}