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);
        }
Exemplo n.º 2
0
        public VirtualAdapter NewAdapter()
        {
            VirtualAdapter Adapter = VirtualAdapter.RandomNewAdapter;

            _adapters.Add(Adapter);
            return(Adapter);
        }
Exemplo n.º 3
0
        public void RemoveInterface(string MAC, string IP, ushort VLAN)
        {
            VirtualAdapter Adapter = GetAdapter(MAC, IP, VLAN);

            if (Adapter != null)
            {
                _adapters.Remove(Adapter);
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 public IcmpService(VirtualAdapter Adapter)
 {
     _adapter       = Adapter;
     _current_state = ICMP_STATE.IDLE;
 }
Exemplo n.º 7
0
 public void RemoveInterface(VirtualAdapter Adapter)
 {
     _adapters.Remove(Adapter);
 }
Exemplo n.º 8
0
        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);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
 public TcpService(VirtualAdapter Adapter)
 {
     this._adapter      = Adapter;
     this._tcp_sessions = new Hashtable();
 }
Exemplo n.º 10
0
 public ArpService(VirtualAdapter Adapter)
 {
     _adapter   = Adapter;
     _arp_table = new Dictionary <IpV4Address, MacAddress>();
 }