//private int _packetIndex = 1; private void PacketHandle(PPacket ppacket) { //if (ppacket.Packet.Ethernet == null || ppacket.Packet.Ethernet.IpV4 == null || ppacket.Packet.Ethernet.IpV4.Tcp == null) if (ppacket.Ipv4 == null || ppacket.Tcp == null) return; //TcpStreamPacket tcpStreamPacket = new TcpStreamPacket(packet); //TcpConnection tcpConnection = tcpStreamPacket.Connection; //TcpConnection tcpConnection = new TcpConnection(ppacket.Packet); TcpConnection tcpConnection = ppacket.GetTcpConnection(); if (!_tcpStreamList.ContainsKey(tcpConnection)) { TcpStream tcpStream = new TcpStream(tcpConnection); _tcpStreamList.Add(tcpConnection, tcpStream); //WriteTcpStreamToFile writeTcpStreamToFile = new WriteTcpStreamToFile(tcpStream, _directory); //_tcpStreamToFileList.Add(writeTcpStreamToFile); //new CreatePacketTcpStream(tcpStream).TcpPacketHandle = (direction, tcpPacket) => { w.PrintPacketHandler2(_ppacketManager.CreatePPacket(tcpPacket), true); }; //if (_trace) // new TraceTcpStream(tcpStream); tcpStream.zWriteTcpStreamToFile(_directory); //tcpStream.CreatePacketTcpStream((direction, tcpPacket) => { w.PrintPacketHandler2(_ppacketManager.CreatePPacket(tcpPacket), true); }); tcpStream.zTcpStreamReassembledPacket((direction, tcpPacket) => { PrintPacket.PrintPacket1(_ppacketManager.CreatePPacket(tcpPacket), true); }); if (_trace) tcpStream.zTraceTcpStream(_directory); } //Trace.CurrentTrace.WriteLine("packet no {0} {1}:{2} - {3}:{4}", _packetIndex++, tcpConnection.Source.Address, tcpConnection.Source.Port, tcpConnection.Destination.Address, tcpConnection.Destination.Port); //_tcpStreamList[tcpConnection].Add(tcpStreamPacket); //_tcpStreamList[tcpConnection].Add(tcpConnection, ppacket); _tcpStreamList[tcpConnection].Add(ppacket); }
public void Add(PPacket ppacket) { _totalPacketsCount++; if (ppacket.Tcp == null) { return; } _selectionnedPacketsCount++; //IndexedTcpConnection tcpConnection = new IndexedTcpConnection(ppacket.Packet); TcpConnection tcpConnection = ppacket.GetTcpConnection(); int i = _tcpStreamPacketsList.IndexOfKey(tcpConnection); List <PPacket> ppacketList; if (i == -1) { tcpConnection.Index = ++_tcpConnectionIndex; ppacketList = new List <PPacket>(); _tcpStreamPacketsList.Add(tcpConnection, ppacketList); } else { tcpConnection.Index = _tcpStreamPacketsList.Keys[i].Index; ppacketList = _tcpStreamPacketsList.Values[i]; } ppacketList.Add(ppacket); }
//public void Add(TcpStreamPacket tcpStreamPacket) //public void Add(TcpConnection tcpConnection, PPacket ppacket) public void Add(PPacket ppacket) { TcpConnection tcpConnection = ppacket.GetTcpConnection(); if (tcpConnection.Source == _tcpConnection.Source && tcpConnection.Destination == _tcpConnection.Destination) { _streamSourceToDestination.Add(ppacket); } else { _streamDestinationToSource.Add(ppacket); } }
//private static void PrintSortedTcpStreamPacket(IndexedTcpConnection tcpConnection, PPacket ppacket) private static void PrintSortedTcpStreamPacket(PPacket ppacket) { TcpConnection tcpConnection = ppacket.GetTcpConnection(); if (_currentTcpConnection == null || tcpConnection.Index != _currentTcpConnection.Index) { Trace.WriteLine(); Trace.WriteLine("group packet time source dir destination protocol flags data seq next seq ack window urg data"); //_LastStreamNumber = tcpConnection.Index; _currentTcpConnection = tcpConnection; } //TcpDirection direction = TcpDirection.SourceToDestination; //string s = GetTcpStreamPacketString1(tcpConnection, ppacket, _currentTcpConnection.GetTcpDirection(tcpConnection), true); string s = GetTcpStreamPacketString1(ppacket, _currentTcpConnection.GetTcpDirection(tcpConnection), true); //string s2 = null; //foreach (Pib.Pcap.Test.TcpPacketError error in streamPacket.Errors) // s2 = s2.zAddValue(streamPacket.GetErrorMessage(error)); //if (s2 != null) // s2 = " " + s2; //Trace.WriteLine("{0,-162}{1}{2}", s, ppacket.GetTcpDescription(), s2); Trace.WriteLine(s); }
public void Add(PPacket ppacket) { _totalPacketsCount++; if (ppacket.Tcp == null) return; _selectionnedPacketsCount++; //IndexedTcpConnection tcpConnection = new IndexedTcpConnection(ppacket.Packet); TcpConnection tcpConnection = ppacket.GetTcpConnection(); int i = _tcpStreamPacketsList.IndexOfKey(tcpConnection); List<PPacket> ppacketList; if (i == -1) { tcpConnection.Index = ++_tcpConnectionIndex; ppacketList = new List<PPacket>(); _tcpStreamPacketsList.Add(tcpConnection, ppacketList); } else { tcpConnection.Index = _tcpStreamPacketsList.Keys[i].Index; ppacketList = _tcpStreamPacketsList.Values[i]; } ppacketList.Add(ppacket); }
//private int _packetIndex = 1; private void PacketHandle(PPacket ppacket) { //if (ppacket.Packet.Ethernet == null || ppacket.Packet.Ethernet.IpV4 == null || ppacket.Packet.Ethernet.IpV4.Tcp == null) if (ppacket.Ipv4 == null || ppacket.Tcp == null) { return; } //TcpStreamPacket tcpStreamPacket = new TcpStreamPacket(packet); //TcpConnection tcpConnection = tcpStreamPacket.Connection; //TcpConnection tcpConnection = new TcpConnection(ppacket.Packet); TcpConnection tcpConnection = ppacket.GetTcpConnection(); if (!_tcpStreamList.ContainsKey(tcpConnection)) { TcpStream tcpStream = new TcpStream(tcpConnection); _tcpStreamList.Add(tcpConnection, tcpStream); //WriteTcpStreamToFile writeTcpStreamToFile = new WriteTcpStreamToFile(tcpStream, _directory); //_tcpStreamToFileList.Add(writeTcpStreamToFile); //new CreatePacketTcpStream(tcpStream).TcpPacketHandle = (direction, tcpPacket) => { w.PrintPacketHandler2(_ppacketManager.CreatePPacket(tcpPacket), true); }; //if (_trace) // new TraceTcpStream(tcpStream); tcpStream.zWriteTcpStreamToFile(_directory); //tcpStream.CreatePacketTcpStream((direction, tcpPacket) => { w.PrintPacketHandler2(_ppacketManager.CreatePPacket(tcpPacket), true); }); tcpStream.zTcpStreamReassembledPacket((direction, tcpPacket) => { PrintPacket.PrintPacket1(_ppacketManager.CreatePPacket(tcpPacket), true); }); if (_trace) { tcpStream.zTraceTcpStream(_directory); } } //Trace.CurrentTrace.WriteLine("packet no {0} {1}:{2} - {3}:{4}", _packetIndex++, tcpConnection.Source.Address, tcpConnection.Source.Port, tcpConnection.Destination.Address, tcpConnection.Destination.Port); //_tcpStreamList[tcpConnection].Add(tcpStreamPacket); //_tcpStreamList[tcpConnection].Add(tcpConnection, ppacket); _tcpStreamList[tcpConnection].Add(ppacket); }
//public static string GetTcpStreamPacketString1(IndexedTcpConnection tcpConnection, PPacket ppacket, TcpDirection direction, bool printData) public static string GetTcpStreamPacketString1(PPacket ppacket, TcpDirection direction, bool printData) { IpV4Datagram ip = ppacket.Ipv4; TcpDatagram tcp = ppacket.Tcp; string saddr = null; string daddr = null; string mf = null; string offset = null; string id = null; if (ip != null) { saddr = ip.Source.ToString(); daddr = ip.Destination.ToString(); if (tcp != null) { saddr += ":" + tcp.SourcePort.ToString(); daddr += ":" + tcp.DestinationPort.ToString(); } //mf = ip.MoreFragment.ToString(); mf = (ip.Fragmentation.Options == IpV4FragmentationOptions.MoreFragments).ToString(); offset = ip.Fragmentation.Offset.zToHex(); id = ip.Identification.zToHex(); } StringBuilder sb = new StringBuilder(); //direction string dir = null; //if (direction != null) //{ if (direction == TcpDirection.SourceToDestination) dir = "--> "; else { dir = "<-- "; string addr = saddr; saddr = daddr; daddr = addr; } //} //sb.Append(string.Format("{0,5} {1,5} {2,10:0.000000} {3,-21} {4}{5,-21} {6,-7}", packet.gGroupNumber, packet.PacketNumber, packet.RelativeTime.TotalSeconds, saddr, dir, daddr, packet.ProtocolCode)); sb.Append(string.Format("{0,5} {1,5} {2,10:0.000000} {3,-21} {4}{5,-21} {6,-7}", ppacket.GetTcpConnection().Index, ppacket.PacketNumber, ppacket.RelativeTime.TotalSeconds, saddr, dir, daddr, ppacket.IpProtocolCode)); string align = ""; if (tcp != null) { string next_seq; if (tcp.PayloadLength > 0) next_seq = "0x" + (tcp.SequenceNumber + (uint)tcp.PayloadLength).zToHex(); else if (tcp.IsSynchronize) next_seq = "0x" + (tcp.SequenceNumber + 1).zToHex(); else next_seq = " "; string dataLength; if (tcp.PayloadLength > 0) dataLength = "0x" + ((ushort)tcp.PayloadLength).zToHex(); else dataLength = " "; string ack_seq; if (tcp.AcknowledgmentNumber != 0) ack_seq = "0x" + tcp.AcknowledgmentNumber.zToHex(); else ack_seq = " "; string urg_ptr = null; if (tcp.UrgentPointer != 0) urg_ptr = " 0x" + tcp.UrgentPointer.zToHex(); else align += " "; //TCPFlags(tcp) //tcp.GetFlagsString() sb.Append(string.Format(" {0,-20} {1} 0x{2} {3} {4} 0x{5}{6}", ppacket.GetTcpFlagsString(), dataLength, tcp.SequenceNumber.zToHex(), next_seq, ack_seq, tcp.Window.zToHex(), urg_ptr)); } else align += " "; sb.Append(align + " "); HttpDatagram http = tcp.Http; if (http != null) { sb.Append("http "); if (http.IsRequest) sb.Append("request"); else if (http.IsResponse) sb.Append("reply "); else sb.Append("???? "); //sb.Append(http.Version.ToString()); } else sb.Append(" "); if (printData && tcp != null && tcp.PayloadLength > 0) { //sb.Append(align + " "); //byte[] data = tcp.Payload; int i = 0; //int maxDataChar = 100; int maxDataChar = 50; foreach (byte b in tcp.Payload) { if (++i > maxDataChar) break; sb.Append(" "); sb.Append(b.zToHex()); } sb.Append(" "); i = 0; foreach (byte b in tcp.Payload) { if (++i > maxDataChar) break; if (b >= 32 && b <= 126) sb.Append((char)b); else sb.Append('.'); } } return sb.ToString(); }
//public void Add(TcpStreamPacket tcpStreamPacket) //public void Add(TcpConnection tcpConnection, PPacket ppacket) public void Add(PPacket ppacket) { TcpConnection tcpConnection = ppacket.GetTcpConnection(); if (tcpConnection.Source == _tcpConnection.Source && tcpConnection.Destination == _tcpConnection.Destination) _streamSourceToDestination.Add(ppacket); else _streamDestinationToSource.Add(ppacket); }