Data model class that describes the IP packets.
Inheritance: IIpPacket
Exemple #1
0
        protected override bool TryParse(IpPacket packet, out IEnvelope envelope)
        {
            envelope = null;

            if (!packet.PacketHeader.DestinationIpAddress.Equals(this.ListenEndPoint.Address))
            {
                return(false);
            }

            var upacket = packet.ToUdpDatagram();

            var isValid = upacket.UdpDatagramHeader.DestinationPort == this.ListenEndPoint.Port;

            if (isValid)
            {
                envelope = new UdpEnvelope
                {
                    OccurrenceTime  = packet.ReceivedTime,
                    ReceivedTime    = packet.ReceivedTime,
                    Payload         = packet.PacketData.Array,
                    PayloadInstance = upacket,
                };
            }

            return(isValid);
        }
Exemple #2
0
 /// <summary>
 /// Produces a IpPacket based on input
 /// </summary>
 /// <param name="ReceivedPacket">IpPacket to copy to a new instance</param>
 /// <remarks> This method copies all data from the ReceivedPacket into a new packet, including byte arrays.</remarks>
 public IpPacket(IpPacket ReceivedPacket)
     : this()
 {
     ReceivedTime             = ReceivedPacket.ReceivedTime;
     IpVersion                = ReceivedPacket.IpVersion;
     InternetHeaderLength     = ReceivedPacket.InternetHeaderLength;
     DscpValue                = ReceivedPacket.DscpValue;
     ExplicitCongestionNotice = ReceivedPacket.ExplicitCongestionNotice;
     IpPacketLength           = ReceivedPacket.IpPacketLength;
     FragmentGroupId          = ReceivedPacket.FragmentGroupId;
     IpHeaderFlags            = ReceivedPacket.IpHeaderFlags;
     FragmentOffset           = ReceivedPacket.FragmentOffset;
     TimeToLive               = ReceivedPacket.TimeToLive;
     ProtocolNumber           = ReceivedPacket.ProtocolNumber;
     Protocol             = ReceivedPacket.Protocol;
     PacketHeaderChecksum = ReceivedPacket.PacketHeaderChecksum;
     SourceIpAddress      = new IPAddress(ReceivedPacket.SourceIpAddress.GetAddressBytes());
     DestinationIpAddress = new IPAddress(ReceivedPacket.DestinationIpAddress.GetAddressBytes());
     if (ReceivedPacket.IpOptions != null)
     {
         IpOptions = new byte[ReceivedPacket.IpOptions.Length];
         Array.Copy(ReceivedPacket.IpOptions, IpOptions, ReceivedPacket.IpOptions.Length);
     }
     if (ReceivedPacket.PacketData != null)
     {
         PacketData = new byte[ReceivedPacket.PacketData.Length];
         Array.Copy(ReceivedPacket.PacketData, PacketData, ReceivedPacket.PacketData.Length);
     }
     if (ReceivedPacket.DataBuffer != null)
     {
         DataBuffer = new byte[ReceivedPacket.DataBuffer.Length];
         Array.Copy(ReceivedPacket.DataBuffer, DataBuffer, ReceivedPacket.DataBuffer.Length);
     }
 }
 /// <summary>
 /// Takes an IpPacket object and encodes it for transmission on the network in a byte array. Includes computing checksums.
 /// </summary>
 /// <param name="Packet">Packet object for encoding.</param>
 /// <returns>Network order byte array with IP checksum included ready to send on a raw socket.</returns>
 public static byte[] ToWireBytes(this IpPacket Packet)
 {
     using (var builderStream = new MemoryStream())
     {
         var header = PacketHeaderToWireBytes(Packet);
         builderStream.Write(header, 0, header.Length);
         builderStream.Write(Packet.PacketData, 0, Packet.PacketData.Length);
         return(builderStream.ToArray());
     }
 }
        /// <summary>
        /// Takes an IpPacket object and encodes the IP header for transmission on the network in a byte array. Includes computing checksums.
        /// </summary>
        /// <param name="Packet">Packet object for encoding.</param>
        /// <returns>The IP header in network order byte array with checksums included.</returns>
        public static byte[] PacketHeaderToWireBytes(this IpPacket Packet)
        {
            using (var builderStream = new MemoryStream())
            {
                //ipVers  bits 0 to 3
                //InternetHeaderLength  bits 4 to 7
                var ipversion = Packet.IpVersion == NetworkInterfaceComponent.IPv4 ? 4 : 6;
                var byte0     = (byte)BitConverter.GetBytes((ipversion << 4) + Packet.InternetHeaderLength)[0];
                builderStream.WriteByte(byte0);

                //DscpValue 8 to 13
                //ExplicitCongestionNotice 14 to 15
                var byte1 = (byte)BitConverter.GetBytes(((int)Packet.DscpValue << 2) + Packet.ExplicitCongestionNotice)[0];
                builderStream.WriteByte(byte1);

                //IpPacketLength 16 to 31
                var byte23 = BitConverter.GetBytes(NetworkOrderUshort(Packet.IpPacketLength));
                builderStream.Write(byte23, 0, 2);

                //FragmentGroupId 32 to 47
                var byte45 = BitConverter.GetBytes(NetworkOrderUshort(Packet.FragmentGroupId));
                builderStream.Write(byte45, 0, 2);

                //IpHeaderFlags 48 to 50
                //FragmentOffset  51 to 63
                var byte67 = BitConverter.GetBytes(((int)Packet.IpHeaderFlags << 13) + Packet.FragmentOffset);
                builderStream.WriteByte(byte67[0]);
                builderStream.WriteByte(byte67[1]);

                //TimeToLive 64 to 71
                builderStream.WriteByte(Packet.TimeToLive);

                //ProtocolNumber 72 to 79
                builderStream.WriteByte(Packet.ProtocolNumber);

                //PacketHeaderChecksum  80 to 95
                builderStream.Write(BitConverter.GetBytes(0), 0, 2); //put all zeros in here and calculate it below.

                //SourceIpAddress 96 to 127
                builderStream.Write(Packet.SourceIpAddress.GetAddressBytes(), 0, 4);

                //DestinationIpAddress 128 to 160
                builderStream.Write(Packet.DestinationIpAddress.GetAddressBytes(), 0, 4);

                if (Packet.IpOptions != null)
                {
                    builderStream.Write(Packet.IpOptions, 0, Packet.IpOptions.Length);
                }
                var headerBytes = builderStream.ToArray();
                var sum         = GetInternetChecksum(headerBytes);
                Array.Copy(BitConverter.GetBytes(sum), 0, headerBytes, 10, 2);

                return(headerBytes);
            }
        }
Exemple #5
0
        /// <summary>
        /// Decodes a UdpDatagram Datagram
        /// </summary>
        public UdpDatagram(IpPacket ReceivedPacket) : base(ReceivedPacket)
        {

            if (Protocol != ProtocolType.Udp) throw new ArgumentException("Received Packet is not UDP");

            IsUdp = true;
            UdpData = new byte[PacketData.Length - 8];
            Array.Copy(PacketData, 8, UdpData, 0, PacketData.Length - 8);

            if (PacketData.Length > 8)
            {
                SourcePort = PacketData.ReadNetOrderUShort(0);
                DestinationPort = PacketData.ReadNetOrderUShort(2);
                UdpLength = PacketData.ReadNetOrderUShort(4);
                UdpCheckSum = PacketData.ReadNetOrderUShort(6);
            }
        }
Exemple #6
0
        /// <summary>
        /// Decodes a UdpDatagram Datagram
        /// </summary>
        public UdpDatagram(IpPacket ReceivedPacket) : base(ReceivedPacket)
        {
            if (Protocol != ProtocolType.Udp)
            {
                throw new ArgumentException("Received Packet is not UDP");
            }

            IsUdp   = true;
            UdpData = new byte[PacketData.Length - 8];
            Array.Copy(PacketData, 8, UdpData, 0, PacketData.Length - 8);

            if (PacketData.Length > 8)
            {
                SourcePort      = PacketData.ReadNetOrderUShort(0);
                DestinationPort = PacketData.ReadNetOrderUShort(2);
                UdpLength       = PacketData.ReadNetOrderUShort(4);
                UdpCheckSum     = PacketData.ReadNetOrderUShort(6);
            }
        }
Exemple #7
0
        protected override bool TryParse(IpPacket packet, out IUdpDatagram result)
        {
            result = null;

            if (!packet.PacketHeader.DestinationIpAddress.Equals(this.ListenEndPoint.Address))
            {
                return(false);
            }

            var upacket = packet.ToUdpDatagram();

            var isValid = upacket.UdpDatagramHeader.DestinationPort == this.ListenEndPoint.Port;

            if (isValid)
            {
                result = upacket;
            }

            return(isValid);
        }
Exemple #8
0
        /// <summary>
        /// Used to create a primitive packet or datagram for encoding to the network.
        /// </summary>
        /// <param name="Packet">A source IpPacket Object that contains a UDP datagram</param>
        /// <param name="SourcePort">Source port for the datagram.</param>
        /// <param name="DestinationPort">Destination port for the datagram</param>
        /// <param name="UdpLength">The length of the UDP datagram including the UDP header</param>
        /// <param name="UdpData">The data following the UDP header. If this is null the Packet object's packet data is used.</param>
        /// <remarks>The IP Header and UDP Checksum is set by calling the ToWirebytes method to encode the packet to the wire.</remarks>
        public UdpDatagram(IpPacket Packet, ushort SourcePort, ushort DestinationPort, ushort UdpLength, byte[] UdpData)
            : base(Packet)
        {
            if (Packet.Protocol != ProtocolType.Udp)
            {
                throw new InvalidDataException("Input Packet must be of protocol type UDP");
            }

            this.SourcePort      = SourcePort;
            this.DestinationPort = DestinationPort;
            this.UdpLength       = UdpLength;
            this.UdpCheckSum     = 0;
            this.UdpData         = new byte[UdpLength - 8];
            if (UdpData == null)
            {
                Array.Copy(Packet.PacketData, Packet.InternetHeaderLength * 4 + 8, this.UdpData, 0, UdpLength - 8);
            }
            else
            {
                Array.Copy(UdpData, this.UdpData, UdpLength - 8);
            }
            IsUdp = true;
        }
Exemple #9
0
 /// <summary>
 /// Produces a IpPacket based on input
 /// </summary>
 /// <param name="ReceivedPacket">IpPacket to copy to a new instance</param>
 /// <remarks> This method copies all data from the ReceivedPacket into a new packet, including byte arrays.</remarks>
 public IpPacket(IpPacket ReceivedPacket): this()
 {
     ReceivedTime = ReceivedPacket.ReceivedTime;
     IpVersion = ReceivedPacket.IpVersion;
     InternetHeaderLength = ReceivedPacket.InternetHeaderLength;
     DscpValue = ReceivedPacket.DscpValue;
     ExplicitCongestionNotice = ReceivedPacket.ExplicitCongestionNotice;
     IpPacketLength = ReceivedPacket.IpPacketLength;
     FragmentGroupId = ReceivedPacket.FragmentGroupId;
     IpHeaderFlags = ReceivedPacket.IpHeaderFlags;
     FragmentOffset = ReceivedPacket.FragmentOffset;
     TimeToLive = ReceivedPacket.TimeToLive;
     ProtocolNumber = ReceivedPacket.ProtocolNumber;
     Protocol = ReceivedPacket.Protocol;
     PacketHeaderChecksum = ReceivedPacket.PacketHeaderChecksum;
     SourceIpAddress = new IPAddress(ReceivedPacket.SourceIpAddress.GetAddressBytes());
     DestinationIpAddress = new IPAddress(ReceivedPacket.DestinationIpAddress.GetAddressBytes());
     if (ReceivedPacket.IpOptions != null) {
         IpOptions = new byte[ReceivedPacket.IpOptions.Length];
         Array.Copy(ReceivedPacket.IpOptions, IpOptions, ReceivedPacket.IpOptions.Length);
     }
     if (ReceivedPacket.PacketData != null) {
         PacketData =new byte[ReceivedPacket.PacketData.Length];
         Array.Copy(ReceivedPacket.PacketData, PacketData, ReceivedPacket.PacketData.Length);
     }
     if (ReceivedPacket.DataBuffer != null)
     {
         DataBuffer = new byte[ReceivedPacket.DataBuffer.Length];
         Array.Copy(ReceivedPacket.DataBuffer, DataBuffer, ReceivedPacket.DataBuffer.Length);
     }
 }
Exemple #10
0
 protected abstract bool TryParse(IpPacket packet, out T envelope);
Exemple #11
0
 /// <summary>
 /// Creates an instance of a Log object.
 /// </summary>
 /// <param name="ReceivedPacket">An IP object generated on data received on a Raw socket. </param>
 /// <param name="Parser">A regular expression that will be used to parse the internal message of the Log.</param>
 public Syslog(IpPacket ReceivedPacket, Regex Parser)
     : this(new UdpDatagram(ReceivedPacket), Parser)
 {
 }
Exemple #12
0
 /// <summary>
 /// Creates an instance of a Log object.
 /// </summary>
 /// <param name="ReceivedPacket">An IP object generated on data received on a Raw socket. </param>
 /// <param name="Parser">A regular expression that will be used to parse the internal message of the Log.</param>
 public Syslog(IpPacket ReceivedPacket, Regex Parser) : this(new UdpDatagram(ReceivedPacket), Parser) { }