//Функция вызываемая при получении private void OnReceive(IAsyncResult ar) { int nReceived = 0; //if (mainSocket.) nReceived = mainSocket.EndReceive(ar); //ParseData(byteData, nReceived); //Создали аргументы для события приёма пакетов PacketRawInfo args = new PacketRawInfo(); args.data = byteData; args.time = DateTime.Now; args.size = nReceived; args.linkLayer = LinkLayers.Null; ReceiveEvent(this, args); if (bContinueCapturing) { byteData = new byte[4096]; mainSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); } }
public void OnRecieve(object sender, CaptureEventArgs e) { PacketRawInfo args = new PacketRawInfo(); args.data = e.Packet.Data; args.size = e.Packet.Data.Length; args.time = e.Packet.Timeval.Date; args.linkLayer = LinkLayers.Ethernet; PacketsInfo.AddToQueue(args); }
public void ParseNetworkPacket(PacketRawInfo args) { IPHeader ipHeader = null; IPv6Header ipv6Header = null; EthernetHeader ethernetHeader = null; EthernetPacketType ethernetPackeType = EthernetPacketType.IpV4; PacketInfo packetInfo = new PacketInfo(); packetInfo.protocolE = PacketType.UNKNOWN; packetInfo.count = 1; if (args.linkLayer == LinkLayers.Ethernet) { ethernetHeader = new EthernetHeader(args.data, args.size); ethernetPackeType = ethernetHeader.EtherType; packetInfo.hardwareSource = ethernetHeader.SourceMac; packetInfo.harwareDestination = ethernetHeader.DestinationMac; packetInfo.protocol = ((Protocol)ethernetHeader.EtherType).ToString(); if (ethernetHeader.EtherType == EthernetPacketType.IpV4) { args.data = ethernetHeader.Payload; args.size = ethernetHeader.Payload.Length; } } else { packetInfo.protocol = Protocol.Iplt.ToString(); } switch (ethernetPackeType) { case EthernetPacketType.IpV4: ipHeader = new IPHeader(args.data, args.size); packetInfo.ipDestination = ipHeader.DestinationAddress.ToString(); packetInfo.ipSource = ipHeader.SourceAddress.ToString(); packetInfo.size = ipHeader.MessageLength; if (packetInfo.ipSource == incomingIP) { packetInfo.incoming = true; } Statistics.AddToIPPacketsStats(packetInfo.size, packetInfo.incoming); break; case EthernetPacketType.IpV6: ipv6Header = new IPv6Header(args.data, args.size); packetInfo.ipDestination = ipv6Header.DestinationAddress.ToString(); packetInfo.ipSource = ipv6Header.SourceAddress.ToString(); if (packetInfo.ipSource == incomingIP) { packetInfo.incoming = true; } Statistics.AddToIPPacketsStats(packetInfo.size, packetInfo.incoming); break; case EthernetPacketType.Arp: ARPHeader arpHeader = new ARPHeader(ethernetHeader.Payload, ethernetHeader.Payload.Length); packetInfo.ipDestination = arpHeader.TPA; packetInfo.ipSource = arpHeader.SPA; packetInfo.desription = arpHeader.Description; if (packetInfo.ipSource == incomingIP) { packetInfo.incoming = true; } packetInfo.size = arpHeader.Size; packetInfo.protocol = "ARP"; packetInfo.protocolE = PacketType.ARP; Statistics.AddToARPPacketsStats(packetInfo.size, packetInfo.incoming); break; case EthernetPacketType.WakeOnLan: break; default: packetInfo.desription = "неизвестный Ethernet протокол"; break; } if (ipHeader != null) { switch (ipHeader.ProtocolType) { case Protocol.Udp: NetworkShow.Network.Packets.UDPHeader udpheader = new UDPHeader(ipHeader.Data, ipHeader.Data.Length); packetInfo.portDestination = udpheader.DestinationPort; packetInfo.portSource = udpheader.SourcePort; packetInfo.size = udpheader.Length; packetInfo.protocol = "UDP"; packetInfo.protocolE = PacketType.UDP; packetInfo.active = "T"; Statistics.AddToUDPPacketsStats(packetInfo.size, packetInfo.incoming); break; case Protocol.Tcp: NetworkShow.Network.Packets.TCPHeader tcpheader = new TCPHeader(ipHeader.Data, ipHeader.Data.Length); packetInfo.portDestination = tcpheader.DestinationPort; packetInfo.portSource = tcpheader.SourcePort; packetInfo.size = tcpheader.MessageLength + tcpheader.HeaderLength; packetInfo.active = "T"; packetInfo.protocol = "TCP"; packetInfo.protocolE = PacketType.TCP; Statistics.AddToTCPPacketsStats(packetInfo.size, packetInfo.incoming); break; case Protocol.InternetControlMessageProtocol: NetworkShow.Network.Packets.ICMPHeader icmpheader = new ICMPHeader(ipHeader.Data, ipHeader.Data.Length); packetInfo.desription = icmpheader.Type.ToString(); packetInfo.size = icmpheader.Size; packetInfo.protocol = "ICMP"; packetInfo.protocolE = PacketType.ICMP; Statistics.AddToICMPPacketsStats(packetInfo.size, packetInfo.incoming); break; case Protocol.InternetGroupManagementProtocol: NetworkShow.Network.Packets.IGMPHeader igmpheader = new IGMPHeader(ipHeader.Data, ipHeader.Data.Length); packetInfo.desription = igmpheader.GroupAddress + " " + igmpheader.Type + " " + igmpheader.Version; packetInfo.size = igmpheader.Size; packetInfo.protocol = "IGMP"; packetInfo.protocolE = PacketType.IGMP; Statistics.AddToIGMPPacketsStats(packetInfo.size, packetInfo.incoming); break; default: packetInfo.desription = "неизвестный IP протокол"; packetInfo.protocol = ipHeader.ProtocolType.ToString(); packetInfo.protocolE = PacketType.UNKNOWN; packetInfo.size = ipHeader.MessageLength; break; } } //PacketsInfo.AddToQueue(packetInfo); int pos = -1; if ((packetInfo.protocol == "TCP") || (packetInfo.protocol == "UDP")) { //pos = FindConnection(packetInfo); pos = FindConnection(packetInfo); if (pos >= 0) { packetInfo = UpdateConnection(pos, packetInfo); UpdateInfoEvent(packetInfo.protocol); db.UpdateConnection(packetInfo.pos, packetInfo); } else { packetInfo.pos = db.SaveNewPacket(packetInfo); AddConnection(packetInfo); ChangeRowsCountEvent(packetInfo.protocolE, packetInfo.pos, packetInfo.portSource, packetInfo.portSource); } } else { int position = db.SaveNewPacket(packetInfo); ChangeRowsCountEvent(packetInfo.protocolE, position, packetInfo.portSource, packetInfo.portSource); } }
public void OnRecieve(object obj, PacketRawInfo args) { PacketsInfo.AddToQueue(args); //AddToQueue(args); }