public void ProcessTCP(IpV4Datagram packet) { IpV4Datagram ip = packet; TcpDatagram tcp = packet.Tcp; uint TcpSessionHashCode = UtilityLib.GetTcpSessionHashCode(ip.CurrentDestination, tcp.DestinationPort, ip.Source, tcp.SourcePort); ITcpSession session = (ITcpSession)_tcp_sessions[TcpSessionHashCode]; if (session != null) { session.ProcessTCP(packet); } }
public static uint GetTcpSessionHashCode(IpV4Address LocalIP, ushort LocalPort, IpV4Address RemoteIP, ushort RemotePort) { byte[] LocalAdress = UtilityLib.ByteArrayJoin(LocalIP.ToBytes(), BitConverter.GetBytes(LocalPort)); byte[] RemoteAdress = UtilityLib.ByteArrayJoin(RemoteIP.ToBytes(), BitConverter.GetBytes(RemotePort)); return(Hash(UtilityLib.ByteArrayJoin(LocalAdress, RemoteAdress))); }
public static uint GetVirtualAdapterHashCode(MacAddress MAC, IpV4Address IP, ushort VLAN) { return(Hash(UtilityLib.ByteArrayJoin(UtilityLib.ByteArrayJoin(MAC.ToBytes(), IP.ToBytes()), BitConverter.GetBytes(VLAN)))); }
public static uint GetVirtualAdapterHashCode(string MAC, string IP, ushort VLAN) { return(Hash(UtilityLib.ByteArrayJoin(UtilityLib.ByteArrayJoin(new MacAddress(MAC).ToBytes(), new IpV4Address(IP).ToBytes()), BitConverter.GetBytes(VLAN)))); }
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); } } } } }