Exemplo n.º 1
0
        /// <summary>
        /// Verifies whether the checksum for a serialised SCTP packet is valid.
        /// </summary>
        /// <param name="buffer">The buffer holding the serialised packet.</param>
        /// <param name="posn">The start position in the buffer.</param>
        /// <param name="length">The length of the packet in the buffer.</param>
        /// <returns>True if the checksum was valid, false if not.</returns>
        public static bool VerifyChecksum(byte[] buffer, int posn, int length)
        {
            uint origChecksum = NetConvert.ParseUInt32(buffer, posn + CHECKSUM_BUFFER_POSITION);

            NetConvert.ToBuffer(0U, buffer, posn + CHECKSUM_BUFFER_POSITION);
            uint calcChecksum = CRC32C.Calculate(buffer, posn, length);

            // Put the original checksum back.
            NetConvert.ToBuffer(origChecksum, buffer, posn + CHECKSUM_BUFFER_POSITION);

            return(origChecksum == NetConvert.EndianFlip(calcChecksum));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serialises an SCTP packet to a byte array.
        /// </summary>
        /// <returns>The byte array containing the serialised SCTP packet.</returns>
        public byte[] GetBytes()
        {
            int chunksLength = Chunks.Sum(x => x.GetChunkLength(true));

            byte[] buffer = new byte[SctpHeader.SCTP_HEADER_LENGTH + chunksLength];

            Header.WriteToBuffer(buffer, 0);

            int writePosn = SctpHeader.SCTP_HEADER_LENGTH;

            foreach (var chunk in Chunks)
            {
                writePosn += chunk.WriteTo(buffer, writePosn);
            }

            NetConvert.ToBuffer(0U, buffer, CHECKSUM_BUFFER_POSITION);
            uint checksum = CRC32C.Calculate(buffer, 0, buffer.Length);

            NetConvert.ToBuffer(NetConvert.EndianFlip(checksum), buffer, CHECKSUM_BUFFER_POSITION);

            return(buffer);
        }