コード例 #1
0
ファイル: SCTPPacket.cs プロジェクト: sgf/SCTP
        /// <summary>
        /// Creates an SCTP packet.
        /// </summary>
        /// <param name="sourcePort">The source port.</param>
        /// <param name="destinationPort">The destination port.</param>
        /// <param name="verificationTag">The verification tag.</param>
        /// <param name="chunks">The packets chunks.</param>
        /// <returns>An <see cref="SCTPPacket"/> instance.</returns>
        public static SCTPPacket Create(ushort sourcePort, ushort destinationPort, uint verificationTag, params Chunk[] chunks)
        {
            SCTPPacket packet = new SCTPPacket();

            packet.Header.SrcPort = sourcePort;
            packet.Header.DstPort = destinationPort;
            packet.Header.Chksum  = 0;
            packet.Header.VerTag  = verificationTag;

            if (chunks != null &&
                chunks.Any())
            {
                packet.AddChunks(chunks);
                packet.CalculateSize();
            }

            return(packet);
        }
コード例 #2
0
ファイル: SCTPPacket.cs プロジェクト: sgf/SCTP
        /// <summary>
        /// Reads a packet from a given positin in a byte array.
        /// </summary>
        /// <param name="buffer">The byte array.</param>
        /// <param name="offset">The position to start reading from.</param>
        /// <param name="length">The number of bytes to read for.</param>
        /// <returns>The <c>SCTP</c> packet.</returns>
        public static SCTPPacket FromArray(byte[] buffer, int offset, int length, bool throwOnCRCFailure = false)
        {
            if (offset + 12 < length)
            {
                if (CheckCRC(buffer, offset, length) == false)
                {
                    if (throwOnCRCFailure == true)
                    {
                        throw new Exception("CRC Check failed");
                    }

                    return(null);
                }

                SCTPPacket packet = new SCTPPacket();
                int        start  = offset;

                offset += packet.Header.FromArray(buffer, offset);

                while (offset + 4 <= length)
                {
                    Chunk chunk = Chunk.Create(buffer, offset, out int chunkLength);
                    if (chunk == null)
                    {
                        break;
                    }

                    packet.Chunks.Add(chunk);
                    offset += chunkLength;
                }

                packet.CalculateSize();

                return(packet);
            }

            return(null);
        }