Exemplo n.º 1
0
        /// <summary>
        /// Load TCP Stream from WireShark TCPStreamFormat
        /// </summary>
        /// <param name="file">File</param>
        public static TcpStream FromFile(string file)
        {
            TcpStream tcp = null;

            if (!string.IsNullOrEmpty(file))
            {
                string[] sp = File.ReadAllLines(file);

                IPEndPoint empty = new IPEndPoint(IPAddress.None, IPEndPoint.MinPort);

                foreach (string line in sp)
                {
                    string l = line.TrimStart().Replace(":", "");

                    ETcpEmisor em = line.StartsWith("\t") ? ETcpEmisor.Server : ETcpEmisor.Client;
                    if (tcp == null)
                    {
                        tcp = new TcpStream(null, em, PhysicalAddress.None, PhysicalAddress.None, empty, empty, null, DateTime.Now);
                    }

                    if (l.Length >= 9 && !l.Substring(0, 8).Contains(" "))
                    {
                        // remove offset
                        l = l.Substring(8, l.Length - 8).Trim();
                    }

                    if (l.Length > 48)
                    {
                        l = l.Substring(0, 48);
                    }
                    l = l.Replace(" ", "");

                    byte[] data = HexHelper.FromHexString(l);

                    if (tcp._Last == null)
                    {
                        tcp._Last = new TcpStreamMessage(DateTime.Now, data, em, null);
                        tcp._InternalList.Add(tcp._Last);
                    }
                    else
                    {
                        // Check if its the same
                        if (tcp._Last.Emisor == em)
                        {
                            tcp._Last.AddData(data, 0, data.Length);
                        }
                        else
                        {
                            // New Packet
                            tcp._Last = new TcpStreamMessage(DateTime.Now, data, em, tcp._Last);
                            tcp._InternalList.Add(tcp._Last);
                        }
                    }
                }
            }

            return(tcp);
        }
Exemplo n.º 2
0
        bool IsTimeouted(TcpStream v, DateTime date)
        {
            TcpStreamMessage st = v.LastStream;

            if (st == null)
            {
                return(date < v.StartDate.Add(TimeoutSync));
            }

            return(date < st.Date.Add(Timeout));
        }
Exemplo n.º 3
0
        public bool GetStream(ResumePacket packet, EStartTcpStreamMethod startTcpStreamMethod, out TcpStream stream)
        {
            ETcpEmisor em;

            if (!TryGetValue(packet.IpSource, packet.IpDest, out stream, out em))
            {
                bool syn = packet.Tcp.Syn;
                bool ack = packet.Tcp.Ack;

                // No data or no Sync
                switch (startTcpStreamMethod)
                {
                case EStartTcpStreamMethod.Sync:
                {
                    if (!syn || ack)
                    {
                        stream = null;
                        return(false);
                    }
                    break;
                }

                case EStartTcpStreamMethod.SyncAck:
                {
                    if (!syn || !ack)
                    {
                        stream = null;
                        return(false);
                    }
                    break;
                }
                }

                lock (_TcpStreams)
                {
                    stream = new TcpStream(this, syn && ack ? ETcpEmisor.Server : ETcpEmisor.Client, packet.HwSource, packet.HwDest, packet.IpSource, packet.IpDest, packet.Tcp, packet.Date);
                    _TcpStreams.Add(stream.Key, stream);
                }
                return(true);
            }

            if (!stream.IsClossed)
            {
                stream.Add(packet.Date, em, packet.Tcp);
            }

            return(false);
        }
Exemplo n.º 4
0
        public bool GetStream(PhysicalAddress hwSource, PhysicalAddress hwDest, IPEndPoint ipSource, IPEndPoint ipDest, TcpPacket tcp, DateTime date, EStartTcpStreamMethod startTcpStreamMethod, out TcpStream stream)
        {
            ETcpEmisor em;

            if (!TryGetValue(ipSource, ipDest, out stream, out em))
            {
                bool syn = tcp.Syn;
                bool ack = tcp.Ack;

                // No data or no Sync
                switch (startTcpStreamMethod)
                {
                case EStartTcpStreamMethod.Sync:
                {
                    if (!syn || ack)
                    {
                        stream = null;
                        return(false);
                    }
                    break;
                }

                case EStartTcpStreamMethod.SyncAck:
                {
                    if (!syn || !ack)
                    {
                        stream = null;
                        return(false);
                    }
                    break;
                }
                }

                lock (_TcpStreams)
                {
                    stream = new TcpStream(this, syn && ack ? ETcpEmisor.Server : ETcpEmisor.Client, hwSource, hwDest, ipSource, ipDest, tcp, date);
                    _TcpStreams.Add(stream.Key, stream);
                }
                return(true);
            }

            if (!stream.IsClossed)
            {
                stream.Add(date, em, tcp);
            }

            return(false);
        }
Exemplo n.º 5
0
        public bool TryGetValue(IPEndPoint source, IPEndPoint dest, out TcpStream ret, out ETcpEmisor em)
        {
            em = dest.Port < 49152 ? ETcpEmisor.Client : ETcpEmisor.Server;

            if (!_TcpStreams.TryGetValue(GetKey(source, dest, em == ETcpEmisor.Server), out ret))
            {
                em = em == ETcpEmisor.Client ? ETcpEmisor.Server : ETcpEmisor.Client;

                if (!_TcpStreams.TryGetValue(GetKey(source, dest, em == ETcpEmisor.Server), out ret))
                {
                    em = ETcpEmisor.Client;
                    return(false);
                }
            }

            return(true);
        }