public static VirtualAdapter Load(XmlNode AdapterNode) { if (AdapterNode == null) { throw new ArgumentNullException("Adapter XML Node", "Virtual Adapter XML node cannot be empty!"); } VirtualAdapter Adapter = new VirtualAdapter(); XmlNode NameNode = AdapterNode.SelectSingleNode("Name"); if (NameNode != null) { Adapter.Name = NameNode.InnerText; } XmlNode IpNode = AdapterNode.SelectSingleNode("IP"); if (IpNode != null) { Adapter.IP = new IpV4Address(IpNode.InnerText); } XmlNode MacNode = AdapterNode.SelectSingleNode("MAC"); if (MacNode != null) { Adapter.MAC = new MacAddress(MacNode.InnerText); } XmlNode VlanNode = AdapterNode.SelectSingleNode("VLAN"); if (VlanNode != null) { Adapter.VLAN = ushort.Parse(VlanNode.InnerText); } XmlNode ArpServiceNode = AdapterNode.SelectSingleNode("Services/ARP"); if (ArpServiceNode != null && bool.Parse(ArpServiceNode.InnerText)) { Adapter._arp_service = new ArpService(Adapter); } XmlNode IcmpServiceNode = AdapterNode.SelectSingleNode("Services/ICMP"); if (IcmpServiceNode != null && bool.Parse(IcmpServiceNode.InnerText)) { Adapter._icmp_service = new IcmpService(Adapter); } XmlNode TcpServiceNode = AdapterNode.SelectSingleNode("Services/TCP"); if (TcpServiceNode != null && bool.Parse(TcpServiceNode.InnerText)) { Adapter._tcp_service = new TcpService(Adapter); } return(Adapter); }
public VirtualAdapter NewAdapter() { VirtualAdapter Adapter = VirtualAdapter.RandomNewAdapter; _adapters.Add(Adapter); return(Adapter); }
public void RemoveInterface(string MAC, string IP, ushort VLAN) { VirtualAdapter Adapter = GetAdapter(MAC, IP, VLAN); if (Adapter != null) { _adapters.Remove(Adapter); } }
public VirtualAdapter NewAdapter(MacAddress MAC, IpV4Address IP, ushort VLAN) { if (GetAdapter(MAC, IP, VLAN) != null) { throw new Exception("Cannot add virtual adatper, already exist!"); } VirtualAdapter Adapter = new VirtualAdapter(MAC, IP, VLAN); _adapters.Add(Adapter); return(Adapter); }
public static VirtualNetwork Load(string Filename) { string FullFilename = Filename; if (!Path.IsPathRooted(FullFilename)) { FullFilename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), FullFilename); } if (!File.Exists(FullFilename)) { throw new FileNotFoundException("File not exist!", FullFilename); } VirtualNetwork NetworkInstance = VirtualNetwork.NewInstance; NetworkInstance.Filename = Filename; XmlDocument ConfigXml = new XmlDocument(); ConfigXml.Load(FullFilename); XmlNode NetworkNode = ConfigXml.SelectSingleNode("/Network"); if (NetworkNode != null) { XmlNode PhysicalAdapterNode = NetworkNode.SelectSingleNode("PhysicalAdapter"); if (PhysicalAdapterNode != null) { NetworkInstance.PhysicalAdapter = PhysicalAdapterNode.InnerText; } XmlNodeList AdapterNodeList = NetworkNode.SelectNodes("Adapters/VirtualAdapter"); if (AdapterNodeList != null && AdapterNodeList.Count > 0) { foreach (XmlNode AdapterNode in AdapterNodeList) { VirtualAdapter Adapter = VirtualAdapter.Load(AdapterNode); if (Adapter != null) { NetworkInstance._adapters.Add(Adapter); } } } NetworkNode = null; } ConfigXml = null; _instance = NetworkInstance; return(NetworkInstance); }
public IcmpService(VirtualAdapter Adapter) { _adapter = Adapter; _current_state = ICMP_STATE.IDLE; }
public void RemoveInterface(VirtualAdapter Adapter) { _adapters.Remove(Adapter); }
private void PacketProcess(Packet packet) { // Debug.WriteLine(packet.Ethernet.EtherType.ToString() + " : " + packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length); ushort VLAN = 1; if (packet.Ethernet.EtherType == EthernetType.VLanTaggedFrame) { VLAN = packet.Ethernet.VLanTaggedFrame.VLanIdentifier; } if (packet.Ethernet.Destination.Equals(UtilityLib.BroadcastMac)) { foreach (VirtualAdapter Adapter in _adapter_hashtable.Values) { if (Adapter.VLAN.Equals(VLAN)) // 若为广播包,则交给相应VLAN下所有虚拟适配器处理 { Adapter.PacketProcess(packet); } } } else { if (VLAN != 1) { if (packet.Ethernet.VLanTaggedFrame.EtherType == EthernetType.Arp) { VirtualAdapter[] Adapters = GetAdapterByMac(packet.Ethernet.Destination); foreach (VirtualAdapter Adapter in Adapters) { if (Adapter.VLAN.Equals(VLAN)) { Adapter.PacketProcess(packet); } } } else if (packet.Ethernet.VLanTaggedFrame.EtherType == EthernetType.IpV4) // 处理IPv4包 { uint AdapterHashCode = UtilityLib.GetVirtualAdapterHashCode(packet.Ethernet.Destination, packet.Ethernet.VLanTaggedFrame.IpV4.CurrentDestination, VLAN); VirtualAdapter Adapter = (VirtualAdapter)_adapter_hashtable[AdapterHashCode]; if (Adapter != null) { Adapter.PacketProcess(packet); } } } else { if (packet.Ethernet.EtherType == EthernetType.Arp) { VirtualAdapter[] Adapters = GetAdapterByMac(packet.Ethernet.Destination); foreach (VirtualAdapter Adapter in Adapters) { if (Adapter.VLAN.Equals(VLAN)) { Adapter.PacketProcess(packet); } } } else if (packet.Ethernet.EtherType == EthernetType.IpV4) // 处理IPv4包 { uint AdapterHashCode = UtilityLib.GetVirtualAdapterHashCode(packet.Ethernet.Destination, packet.Ethernet.IpV4.CurrentDestination, VLAN); VirtualAdapter Adapter = (VirtualAdapter)_adapter_hashtable[AdapterHashCode]; if (Adapter != null) { Adapter.PacketProcess(packet); } } } } }
public TcpService(VirtualAdapter Adapter) { this._adapter = Adapter; this._tcp_sessions = new Hashtable(); }
public ArpService(VirtualAdapter Adapter) { _adapter = Adapter; _arp_table = new Dictionary <IpV4Address, MacAddress>(); }