private int searchMac(LinkedList <WpfApplication1.MacTable> tabulka, String hladanaMac, int portNumber) { foreach (var i in tabulka) { if (i.sourceMac.Equals(hladanaMac)) { i.port = portNumber; i.timer = timeInTimer; this.pomNode = i; return(1); } } return(0); }
private void device_OnPacketArrival1(object sender, CaptureEventArgs packet) { int portNumber = -1; if (((WinPcapDevice)packet.Device).MacAddress == this.dev1.MacAddress) { // System.Diagnostics.Trace.WriteLine("I am port 1"); portNumber = 1; } else { // System.Diagnostics.Trace.WriteLine("I am port 2"); portNumber = 2; } String data = packet.Packet.ToString(); var packet2 = PacketDotNet.Packet.ParsePacket(packet.Packet.LinkLayerType, packet.Packet.Data); var ethernetPacket = (PacketDotNet.EthernetPacket)packet2; if (ethernetPacket != null) { lock (locker) { // countIn1++; // print source mac address on standard output - works // System.Diagnostics.Trace.WriteLine(ethernetPacket.Type.ToString()); var lldp = (PacketDotNet.LLDPPacket)ethernetPacket.Extract(typeof(PacketDotNet.LLDPPacket)); if (lldp != null) { // Console.WriteLine("Nasiel som LLDP"); readLLDP(lldp, portNumber); return; } if (searchMac(tabulka, ethernetPacket.SourceHwAddress.ToString(), portNumber) == 1) { // Console.WriteLine("Zaznam exituje"); // Console.WriteLine(pomNode.sourceMac + " " + pomNode.port + " " + pomNode.timer); } else { WpfApplication1.MacTable newNode = new WpfApplication1.MacTable(); // Console.WriteLine("Zaznam neexistuje treba ho pridat " + ethernetPacket.SourceHwAddress.ToString()); newNode.sourceMac = ethernetPacket.SourceHwAddress.ToString(); newNode.port = 1; newNode.timer = timeInTimer; tabulka.AddLast(newNode); } string protokol = findProtocol(ethernetPacket); if (protokol != null && protokol.Equals("ARP")) { var ArpPacket = (PacketDotNet.ARPPacket)ethernetPacket.Extract(typeof(PacketDotNet.ARPPacket)); if (ArpPacket != null && checkFilter(portNumber, "In", ethernetPacket.SourceHwAddress.ToString() , ethernetPacket.DestinationHwAddress.ToString(), ArpPacket.SenderProtocolAddress.ToString(), ArpPacket.TargetProtocolAddress.ToString(), protokol) == 1) { return; } } else { var IpPacket = (PacketDotNet.IpPacket)ethernetPacket.Extract(typeof(PacketDotNet.IpPacket)); // Console.WriteLine("ja som " + protokol + "\n"); if (IpPacket != null && checkFilter(portNumber, "In", ethernetPacket.SourceHwAddress.ToString() , ethernetPacket.DestinationHwAddress.ToString(), IpPacket.SourceAddress.ToString(), IpPacket.DestinationAddress.ToString(), protokol) == 1) { return; } } incrementStatsIn(portNumber, protokol); // Console.WriteLine("------- Ja som destinacia " + ethernetPacket.DestinationHwAddress.ToString() // + "---------"); int destPort = searchMactoSend(tabulka, ethernetPacket.DestinationHwAddress.ToString()); if (protokol != null && protokol.Equals("ARP")) { var ArpPacket = (PacketDotNet.ARPPacket)ethernetPacket.Extract(typeof(PacketDotNet.ARPPacket)); if (ArpPacket != null && checkFilter(destPort, "Out", ethernetPacket.SourceHwAddress.ToString() , ethernetPacket.DestinationHwAddress.ToString(), ArpPacket.SenderProtocolAddress.ToString(), ArpPacket.TargetProtocolAddress.ToString(), protokol) == 1) { return; } } else { var IpPacket = (PacketDotNet.IpPacket)ethernetPacket.Extract(typeof(PacketDotNet.IpPacket)); // Console.WriteLine("ja som " + protokol + "\n"); if (IpPacket != null && checkFilter(destPort, "Out", ethernetPacket.SourceHwAddress.ToString() , ethernetPacket.DestinationHwAddress.ToString(), IpPacket.SourceAddress.ToString(), IpPacket.DestinationAddress.ToString(), protokol) == 1) { return; } } int odoslane = sendMsg(portNumber, destPort, packet2); if (odoslane != -1) { incrementStatsOut(odoslane, protokol); } } } }