Ejemplo n.º 1
0
        public byte[] Processing(Ipv4 ip4)
        {
            if (ip4.NextProtocol != 0x2f)
            {
                return(ip4.IpData);
            }

            this.Data = ip4.Payload;
            return(this.Payload);
        }
Ejemplo n.º 2
0
        private void AddIp(Ipv4 packet)
        {
            var newList = new Ip
            {
                Indentifivation = packet.Indentification,
                IpSource        = packet.Source,
                IpDestination   = packet.Destination,
                Data            = new List <byte>(packet.Payload),
                NextProtocol    = packet.NextProtocol
            };

            this.ips.Add(newList);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Fragmentation packet in byte mode.
        /// </summary>
        /// <param name="ippacket">
        /// The ippacket.
        /// </param>
        /// <param name="shearchIp">
        /// The shearch Ip.
        /// </param>
        /// <returns>
        /// Fragmentation packet in byte array.
        /// </returns>
        public byte[] Processing(byte[] ippacket, bool shearchIp = true)
        {
            var ipPacket = new Ipv4(ippacket);

            if (ipPacket.IsCorrect())
            {
                if (!this.IsExist(ipPacket.Indentification))
                {
                    if (ipPacket.Fragmentation)
                    {
                        this.AddIp(ipPacket);
                        return(null);
                    }

                    return(ipPacket.IpData);
                }

                if (this.IsExist(ipPacket.Indentification))
                {
                    Ip selected = this.SelectIp(ipPacket.Indentification);
                    if (ipPacket.Fragmentation)
                    {
                        selected.Data.AddRange(ipPacket.Payload);
                        return(null);
                    }

                    if (!ipPacket.Fragmentation)
                    {
                        selected.Data.AddRange(ipPacket.IpData);

                        var result = new Ipv4(this.GenerateIp(selected));

                        this.fragmentedIppacket += 1;

                        Debug.WriteLine("Fragmetation ip packet");

                        return(result.IpData);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        private byte[] GenerateIp(Ip select)
        {
            Ip  conection = select;
            var result    = new byte[conection.Data.Count + 20];

            result[0] = 0x45;
            result[1] = 0x00;
            result[2] = (byte)(result.Length / 256);
            result[3] = (byte)(result.Length - (result[2] * 256));
            result[4] = conection.Indentifivation[0];
            result[5] = conection.Indentifivation[1];
            result[6] = 64;
            result[7] = 0;
            result[8] = 0x39;
            result[9] = conection.NextProtocol;
            Array.Copy(conection.IpSource, 0, result, 12, 4);
            Array.Copy(conection.IpDestination, 0, result, 16, 4);
            byte[] checkSum = Ipv4.CheckSum(result);
            result[10] = checkSum[0];
            result[11] = checkSum[1];
            Array.Copy(select.Data.ToArray(), 0, result, 20, result.Length - 20);
            this.ips.Remove(select);
            return(result);
        }
Ejemplo n.º 5
0
        public List <byte[]> Shearch(byte[] indata)
        {
            if (IsBuffer)
            {
                var buffData = buff.GetData();
                var result   = new byte[indata.Length + buffData.Length];
                System.Buffer.BlockCopy(buffData, 0, result, 0, buffData.Length);
                System.Buffer.BlockCopy(indata, 0, result, buffData.Length, indata.Length);
                indata = result;
            }

            var resultPacket = new List <byte[]>();
            int index        = StartPosition;

            while (index < indata.Length - 20)
            {
                if (indata[index] != 0x45)
                {
                    index += 1;
                    continue;
                }

                if (this.NextProtocol != null)
                {
                    if (!this.NextProtocol.Contains(indata[index + 9]))
                    {
                        index += 1;
                        continue;
                    }
                }

                if (this.CheckChecksum)
                {
                    if (Ipv4.ComputeHeaderIpChecksum(indata, index, 20) != 0)
                    {
                        index += 1;
                        continue;
                    }
                }

                int lengthPacket = indata[index + 2] << 8;
                lengthPacket += indata[index + 3];

                if (lengthPacket > 1600)
                {
                    index += 1;
                    continue;
                }

                if ((lengthPacket + index) > indata.Length)
                {
                    break;
                }

                var result = new byte[lengthPacket];

                Buffer.BlockCopy(indata, index, result, 0, result.Length);

                resultPacket.Add(result);

                index += 20;
            }

            if (this.IsBuffer)
            {
                var delta = new byte[indata.Length - index];
                System.Buffer.BlockCopy(indata, index, delta, 0, delta.Length);
                this.buff.AddData(delta);
            }

            return(resultPacket);
        }