private void PopulateScreen(PackageDetail package) { if (package == null) { MessageBox.Show("Package does not exist! Enter a valid package.", null, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (package.TcpPacket != null) { packageType.Text = "TCP PACKET"; headerText.Text += SetHeader(package.TcpPacket.Header); checksumText.Text += package.TcpPacket.Checksum.ToString() + " - is " + BooleanToString(package.TcpPacket.ValidChecksum, 1); windowsSizeText.Text += package.TcpPacket.WindowSize.ToString(); sequenceNumberText.Text += package.TcpPacket.SequenceNumber.ToString(); acknowledgmentNumberText.Text += package.TcpPacket.AcknowledgmentNumber.ToString(); dataOffsetText.Text += package.TcpPacket.DataOffset.ToString(); sourceAndDestinationText.Text += SetAddress(package.IpPacket, package.TcpPacket); flagsText.Text += "\n" + BooleanToString(package.TcpPacket.CWR) + " -> Congestion Window Reduced (CWR)"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.ECN) + " -> ECN-Echo"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.Urg) + " -> Urgent"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.Ack) + " -> Acknowledgment"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.Psh) + " -> Push"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.Rst) + " -> Reset"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.Syn) + " -> Syn"; flagsText.Text += "\n" + BooleanToString(package.TcpPacket.Fin) + " -> Fin"; } else if (package.UdpPacket != null) { packageType.Text = "UDP PACKET"; headerText.Text += SetHeader(package.UdpPacket.Header); checksumText.Text += package.UdpPacket.Checksum.ToString() + " - is " + BooleanToString(package.UdpPacket.ValidChecksum, 1); sourceAndDestinationText.Text += SetAddress(package.IpPacket, package.UdpPacket); windowsSizeText.Text = sequenceNumberText.Text = acknowledgmentNumberText.Text = dataOffsetText.Text = flagsText.Text = ""; } }
private void device_OnPacketArrival(object sender, CaptureEventArgs packet) { Packet pack = Packet.ParsePacket(packet.Packet.LinkLayerType, packet.Packet.Data); TcpPacket tcpPacket = (TcpPacket)pack.Extract(typeof(TcpPacket)); DateTime time = packet.Packet.Timeval.Date; int len = packet.Packet.Data.Length; if (tcpPacket != null) { IpPacket ipPacket = (IpPacket)tcpPacket.ParentPacket; var packageDetail = new PackageDetail(tcpPacket, null, ipPacket); packageDetailList.Add(packageDetail); var srcIp = ipPacket.SourceAddress; var dstIp = ipPacket.DestinationAddress; var srcPort = tcpPacket.SourcePort; var dstPort = tcpPacket.DestinationPort; writeLine = string.Format("ID: {9} - {0}:{1}:{2},{3} - TCP Packet: {5}:{6} -> {7}:{8}\n\n", time.Hour, time.Minute, time.Second, time.Millisecond, len, srcIp, srcPort, dstIp, dstPort, packageDetail.Id); Invoke(new MethodInvoker(updateLog)); } else { UdpPacket udpPacket = (UdpPacket)pack.Extract(typeof(UdpPacket)); time = packet.Packet.Timeval.Date; len = packet.Packet.Data.Length; if (udpPacket != null) { IpPacket ipPacket = (IpPacket)udpPacket.ParentPacket; var packageDetail = new PackageDetail(null, udpPacket, ipPacket); packageDetailList.Add(packageDetail); IPAddress srcIp = ipPacket.SourceAddress; IPAddress dstIp = ipPacket.DestinationAddress; ushort srcPort = udpPacket.SourcePort; ushort dstPort = udpPacket.DestinationPort; writeLine = (string.Format("ID: {9} - {0}:{1}:{2},{3} - UDP Packet: {5}:{6} -> {7}:{8}\n", time.Hour, time.Minute, time.Second, time.Millisecond, len, srcIp, srcPort, dstIp, dstPort, packageDetail.Id)); Invoke(new MethodInvoker(updateLog)); if (decodeMode == true) { byte[] packetBytes = udpPacket.PayloadData; int version = SnmpPacket.GetProtocolVersion(packetBytes, packetBytes.Length); switch (version) { case (int)SnmpVersion.Ver1: SnmpV1Packet snmpPacket = new SnmpV1Packet(); try { snmpPacket.decode(packetBytes, packetBytes.Length); writeLine = "SNMP.V1 Packet: " + snmpPacket.ToString(); } catch (Exception e) { writeLine = e.ToString(); } break; case (int)SnmpVersion.Ver2: SnmpV2Packet snmp2Packet = new SnmpV2Packet(); try { snmp2Packet.decode(packetBytes, packetBytes.Length); writeLine = "SNMP.V2 Packet: " + snmp2Packet.ToString(); } catch (Exception e) { writeLine = e.ToString(); } break; case (int)SnmpVersion.Ver3: SnmpV3Packet snmp3Packet = new SnmpV3Packet(); try { snmp3Packet.decode(packetBytes, packetBytes.Length); writeLine = "SNMP.V3 Packet: " + snmp3Packet.ToString(); } catch (Exception e) { writeLine = e.ToString(); } break; } Invoke(new MethodInvoker(updateLog)); } } } }