예제 #1
0
        /// <summary>
        /// Parses the DATA chunk fields
        /// </summary>
        /// <param name="buffer">The buffer holding the serialised chunk.</param>
        /// <param name="posn">The position to start parsing at.</param>
        public static SctpDataChunk ParseChunk(byte[] buffer, int posn)
        {
            var    dataChunk = new SctpDataChunk();
            ushort chunkLen  = dataChunk.ParseFirstWord(buffer, posn);

            if (chunkLen < FIXED_PARAMETERS_LENGTH)
            {
                throw new ApplicationException($"SCTP data chunk cannot be parsed as buffer too short for fixed parameter fields.");
            }

            dataChunk.Unordered = (dataChunk.ChunkFlags & 0x04) > 0;
            dataChunk.Begining  = (dataChunk.ChunkFlags & 0x02) > 0;
            dataChunk.Ending    = (dataChunk.ChunkFlags & 0x01) > 0;

            int startPosn = posn + SCTP_CHUNK_HEADER_LENGTH;

            dataChunk.TSN          = NetConvert.ParseUInt32(buffer, startPosn);
            dataChunk.StreamID     = NetConvert.ParseUInt16(buffer, startPosn + 4);
            dataChunk.StreamSeqNum = NetConvert.ParseUInt16(buffer, startPosn + 6);
            dataChunk.PPID         = NetConvert.ParseUInt32(buffer, startPosn + 8);

            int userDataPosn = startPosn + FIXED_PARAMETERS_LENGTH;
            int userDataLen  = chunkLen - SCTP_CHUNK_HEADER_LENGTH - FIXED_PARAMETERS_LENGTH;

            if (userDataLen > 0)
            {
                dataChunk.UserData = new byte[userDataLen];
                Buffer.BlockCopy(buffer, userDataPosn, dataChunk.UserData, 0, dataChunk.UserData.Length);
            }

            return(dataChunk);
        }
 internal PrivateChannelMessagePacket(UInt32 channelID, String formattedText)
     : base(Packet.Type.PRIVGRP_MESSAGE)
 {
     this.AddData(NetConvert.HostToNetworkOrder(channelID));
     this.AddData(new NetString(formattedText));
     this.AddData(new NetString("\0"));
 }
예제 #3
0
        public byte[] GetBytes(byte[] reports)
        {
            Reports = reports;
            byte[] payload = new byte[SENDERINFO_BYTES_LENGTH + reports.Length];

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(SenderSyncSource)), 0, payload, 0, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(NTPTimestamp)), 0, payload, 4, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(RTPTimestamp)), 0, payload, 12, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(SenderPacketCount)), 0, payload, 16, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(SenderOctetCount)), 0, payload, 20, 4);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(SenderSyncSource), 0, payload, 0, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NTPTimestamp), 0, payload, 4, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(RTPTimestamp), 0, payload, 12, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(SenderPacketCount), 0, payload, 16, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(SenderOctetCount), 0, payload, 20, 4);
            }

            Buffer.BlockCopy(reports, 0, payload, 24, reports.Length);

            Header.Length = Convert.ToUInt16(payload.Length / 4);
            byte[] header = Header.GetBytes();
            byte[] packet = new byte[header.Length + payload.Length];
            Array.Copy(header, packet, header.Length);
            Array.Copy(payload, 0, packet, header.Length, payload.Length);

            return(packet);
        }
예제 #4
0
        public Item <ushort>[] ReadUShorts(DeviceAddress deviceAddress, ushort length)
        {
            var datas   = readyBytes(deviceAddress, 2 * length);
            var ushorts = UnsafeNetConvert.BytesToUShorts(datas, 0, length, deviceAddress.ByteOrder);

            return(NetConvert.ToItems(ushorts, 0, length));
        }
예제 #5
0
        public override int ToByteBuffer(byte[] buffer, int startIndex)
        {
            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((UInt16)base.AttributeType)), 0,
                                 buffer, startIndex, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(ADDRESS_ATTRIBUTE_LENGTH)), 0, buffer,
                                 startIndex + 2, 2);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes((UInt16)base.AttributeType), 0, buffer, startIndex, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(ADDRESS_ATTRIBUTE_LENGTH), 0, buffer, startIndex + 2, 2);
            }

            buffer[startIndex + 5] = (byte)Family;

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(Convert.ToUInt16(Port))), 0, buffer,
                                 startIndex + 6, 2);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(Convert.ToUInt16(Port)), 0, buffer, startIndex + 6, 2);
            }

            Buffer.BlockCopy(Address.GetAddressBytes(), 0, buffer, startIndex + 8, 4);

            return(STUNATTRIBUTE_HEADER_LENGTH + ADDRESS_ATTRIBUTE_LENGTH);
        }
예제 #6
0
        public Item <bool>[] ReadBools(DeviceAddress deviceAddress, ushort length)
        {
            var datas = readyBytes(deviceAddress, (int)Math.Ceiling((double)(deviceAddress.BitAddress + length) / 8));
            var bools = NetConvert.BytesToBools(datas, deviceAddress.BitAddress, length);

            return(NetConvert.ToItems(bools, 0, length));
        }
예제 #7
0
        public Item <int>[] ReadInts(DeviceAddress deviceAddress, ushort length)
        {
            var datas = readyBytes(deviceAddress, 4 * length);
            var ints  = UnsafeNetConvert.BytesToInts(datas, 0, length, deviceAddress.ByteOrder);

            return(NetConvert.ToItems(ints, 0, length));
        }
예제 #8
0
        /// <summary>
        /// Gets the serialised bytes for this Receiver Report.
        /// </summary>
        /// <returns>A byte array.</returns>
        public byte[] GetBytes()
        {
            int rrCount = (ReceptionReports != null) ? ReceptionReports.Count : 0;

            byte[] buffer = new byte[RTCPHeader.HEADER_BYTES_LENGTH + 4 + rrCount * ReceptionReportSample.PAYLOAD_SIZE];
            Header.SetLength((ushort)(buffer.Length / 4 - 1));

            Buffer.BlockCopy(Header.GetBytes(), 0, buffer, 0, RTCPHeader.HEADER_BYTES_LENGTH);
            int payloadIndex = RTCPHeader.HEADER_BYTES_LENGTH;

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(SSRC)), 0, buffer, payloadIndex, 4);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(SSRC), 0, buffer, payloadIndex, 4);
            }

            int bufferIndex = payloadIndex + 4;

            for (int i = 0; i < rrCount; i++)
            {
                var receptionReportBytes = ReceptionReports[i].GetBytes();
                Buffer.BlockCopy(receptionReportBytes, 0, buffer, bufferIndex, ReceptionReportSample.PAYLOAD_SIZE);
                bufferIndex += ReceptionReportSample.PAYLOAD_SIZE;
            }

            return(buffer);
        }
예제 #9
0
        /// <summary>
        /// Extract and load the RTCP header from an RTCP packet.
        /// </summary>
        /// <param name="packet"></param>
        public RTCPHeader(byte[] packet)
        {
            if (packet.Length < HEADER_BYTES_LENGTH)
            {
                throw new ApplicationException(
                          "The packet did not contain the minimum number of bytes for an RTCP header packet.");
            }

            UInt16 firstWord = BitConverter.ToUInt16(packet, 0);

            if (BitConverter.IsLittleEndian)
            {
                firstWord = NetConvert.DoReverseEndian(firstWord);
                Length    = NetConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 2));
            }
            else
            {
                Length = BitConverter.ToUInt16(packet, 2);
            }

            Version              = Convert.ToInt32(firstWord >> 14);
            PaddingFlag          = Convert.ToInt32((firstWord >> 13) & 0x1);
            ReceptionReportCount = Convert.ToInt32((firstWord >> 8) & 0x1f);
            PacketType           = Convert.ToUInt16(firstWord & 0x00ff);
        }
예제 #10
0
        public Item <float>[] Readfloats(DeviceAddress deviceAddress, ushort length)
        {
            var datas  = readRegister(deviceAddress, 2 * length);
            var bdatas = UnsafeNetConvert.BytesToFloats(datas, 0, length, deviceAddress.ByteOrder);

            return(NetConvert.ToItems(bdatas, 0, length));
        }
예제 #11
0
        public Item <bool>[] ReadBools(DeviceAddress deviceAddress, ushort length)
        {
            var datas  = readBool(deviceAddress, length);
            var bdatas = NetConvert.BytesToBools(datas, length);

            return(NetConvert.ToItems(bdatas, 0, length));
        }
예제 #12
0
 internal PrivateMessagePacket(UInt32 characterID, String formattedText)
     : base(Packet.Type.PRIVATE_MESSAGE)
 {
     this._characterID = characterID;
     this.AddData(NetConvert.HostToNetworkOrder(this._characterID));
     this.AddData(new NetString(formattedText));
     this.AddData(new NetString("\0"));
 }
예제 #13
0
        public byte[] GetBytes()
        {
            byte[] buffer = new byte[RTCPHeader.HEADER_BYTES_LENGTH + SENDER_PAYLOAD_SIZE];
            Header.SetLength((ushort)(buffer.Length / 4 - 1));

            Buffer.BlockCopy(Header.GetBytes(), 0, buffer, 0, RTCPHeader.HEADER_BYTES_LENGTH);
            int payloadIndex = RTCPHeader.HEADER_BYTES_LENGTH;

            // All feedback packets require the Sender and Media SSRC's to be set.
            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(SenderSSRC)), 0, buffer, payloadIndex,
                                 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(MediaSSRC)), 0, buffer,
                                 payloadIndex + 4, 4);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(SenderSSRC), 0, buffer, payloadIndex, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(MediaSSRC), 0, buffer, payloadIndex + 4, 4);
            }

            switch (Header)
            {
            case var x when x.PacketType == RTCPReportTypesEnum.RTPFB &&
                x.FeedbackMessageType == RTCPFeedbackTypesEnum.RTCP_SR_REQ:
                // PLI feedback reports do no have any additional parameters.
                break;

            case var x when x.PacketType == RTCPReportTypesEnum.RTPFB:
                if (BitConverter.IsLittleEndian)
                {
                    Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(PID)), 0, buffer,
                                     payloadIndex + 6, 2);
                    Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(BLP)), 0, buffer,
                                     payloadIndex + 8, 2);
                }
                else
                {
                    Buffer.BlockCopy(BitConverter.GetBytes(PID), 0, buffer, payloadIndex + 6, 2);
                    Buffer.BlockCopy(BitConverter.GetBytes(BLP), 0, buffer, payloadIndex + 8, 2);
                }

                break;

            case var x when x.PacketType == RTCPReportTypesEnum.PSFB &&
                x.PayloadFeedbackMessageType == PSFBFeedbackTypesEnum.PLI:
                break;

            default:
                throw new NotImplementedException(
                          $"Serialisation for feedback report {Header.PacketType} not yet implemented.");
            }

            return(buffer);
        }
예제 #14
0
        /// <summary>
        /// Writes the optional and variable length parameters to a Type-Length-Value (TLV)
        /// parameter list.
        /// </summary>
        /// <returns>A TLV parameter list holding the optional and variable length parameters.</returns>
        private List <SctpTlvChunkParameter> GetVariableParameters()
        {
            List <SctpTlvChunkParameter> varParams = new List <SctpTlvChunkParameter>();

            // Add the optional and variable length parameters as Type-Length-Value (TLV) formatted.
            foreach (var address in Addresses)
            {
                ushort addrParamType = (ushort)(address.AddressFamily == AddressFamily.InterNetwork ?
                                                SctpInitChunkParameterType.IPv4Address : SctpInitChunkParameterType.IPv6Address);
                var addrParam = new SctpTlvChunkParameter(addrParamType, address.GetAddressBytes());
                varParams.Add(addrParam);
            }

            if (CookiePreservative > 0)
            {
                varParams.Add(
                    new SctpTlvChunkParameter((ushort)SctpInitChunkParameterType.CookiePreservative,
                                              NetConvert.GetBytes(CookiePreservative)
                                              ));
            }

            if (!string.IsNullOrEmpty(HostnameAddress))
            {
                varParams.Add(
                    new SctpTlvChunkParameter((ushort)SctpInitChunkParameterType.HostNameAddress,
                                              Encoding.UTF8.GetBytes(HostnameAddress)
                                              ));
            }

            if (SupportedAddressTypes.Count > 0)
            {
                byte[] paramVal     = new byte[SupportedAddressTypes.Count * 2];
                int    paramValPosn = 0;
                foreach (var supAddr in SupportedAddressTypes)
                {
                    NetConvert.ToBuffer((ushort)supAddr, paramVal, paramValPosn);
                    paramValPosn += 2;
                }
                varParams.Add(
                    new SctpTlvChunkParameter((ushort)SctpInitChunkParameterType.SupportedAddressTypes, paramVal));
            }

            if (StateCookie != null)
            {
                varParams.Add(
                    new SctpTlvChunkParameter((ushort)SctpInitChunkParameterType.StateCookie, StateCookie));
            }

            foreach (var unrecognised in UnrecognizedPeerParameters)
            {
                varParams.Add(
                    new SctpTlvChunkParameter((ushort)SctpInitChunkParameterType.UnrecognizedParameter, unrecognised.GetBytes()));
            }

            return(varParams);
        }
예제 #15
0
 internal ChatCommandPacket(UInt32 windowId, params string[] commands)
     : base(Packet.Type.CHAT_COMMAND)
 {
     this.AddData(IPAddress.HostToNetworkOrder((short)commands.Length));
     foreach (string command in commands)
     {
         this.AddData(new NetString(command));
     }
     this.AddData(NetConvert.HostToNetworkOrder(windowId));
 }
예제 #16
0
        public Item <bool> ReadBool(DeviceAddress deviceAddress)
        {
            var datas = readBool(deviceAddress, 1);

            return(datas == null ? Item <bool> .CreateDefault() :
                   new Item <bool>()
            {
                Vaule = NetConvert.ByteToBool(datas[0], 0), UpdateTime = DateTime.Now, Quality = QUALITIES.QUALITY_GOOD
            });
        }
예제 #17
0
        /// <summary>
        /// Method for pulling an unsigned short (2-byte) integer off the array.
        /// </summary>
        /// <param name="data">the byte array</param>
        /// <param name="offset">index where to begin pulling off the array</param>
        /// <returns>an unsigned short (2-byte) integer</returns>
        internal static UInt16 PopUnsignedShort(ref byte[] data, ref Int32 offset)
        {
            if (data.Length - offset < 2)
            {
                return(0);
            }

            UInt16 ret = NetConvert.NetworkToHostOrder(BitConverter.ToUInt16(data, offset));

            offset += 2;
            return(ret);
        }
예제 #18
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));
        }
예제 #19
0
        /// <summary>
        /// Extract and load the RTP header from an RTP packet.
        /// </summary>
        /// <param name="packet"></param>
        public RTPHeader(byte[] packet)
        {
            if (packet.Length < MIN_HEADER_LEN)
            {
                throw new ApplicationException(
                          "The packet did not contain the minimum number of bytes for an RTP header packet.");
            }

            UInt16 firstWord = BitConverter.ToUInt16(packet, 0);

            if (BitConverter.IsLittleEndian)
            {
                firstWord      = NetConvert.DoReverseEndian(firstWord);
                SequenceNumber = NetConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 2));
                Timestamp      = NetConvert.DoReverseEndian(BitConverter.ToUInt32(packet, 4));
                SyncSource     = NetConvert.DoReverseEndian(BitConverter.ToUInt32(packet, 8));
            }
            else
            {
                SequenceNumber = BitConverter.ToUInt16(packet, 2);
                Timestamp      = BitConverter.ToUInt32(packet, 4);
                SyncSource     = BitConverter.ToUInt32(packet, 8);
            }

            Version             = firstWord >> 14;
            PaddingFlag         = (firstWord >> 13) & 0x1;
            HeaderExtensionFlag = (firstWord >> 12) & 0x1;
            CSRCCount           = (firstWord >> 8) & 0xf;
            MarkerBit           = (firstWord >> 7) & 0x1;
            PayloadType         = firstWord & 0x7f;

            int headerAndCSRCLength = 12 + 4 * CSRCCount;

            if (HeaderExtensionFlag == 1 && (packet.Length >= (headerAndCSRCLength + 4)))
            {
                if (BitConverter.IsLittleEndian)
                {
                    ExtensionProfile = NetConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 12 + 4 * CSRCCount));
                    ExtensionLength  = NetConvert.DoReverseEndian(BitConverter.ToUInt16(packet, 14 + 4 * CSRCCount));
                }
                else
                {
                    ExtensionProfile = BitConverter.ToUInt16(packet, 12 + 4 * CSRCCount);
                    ExtensionLength  = BitConverter.ToUInt16(packet, 14 + 4 * CSRCCount);
                }

                if (ExtensionLength > 0 && packet.Length >= (headerAndCSRCLength + 4 + ExtensionLength * 4))
                {
                    ExtensionPayload = new byte[ExtensionLength * 4];
                    Buffer.BlockCopy(packet, headerAndCSRCLength + 4, ExtensionPayload, 0, ExtensionLength * 4);
                }
            }
        }
예제 #20
0
        /// <summary>
        /// Method for pulling an unsigned integer from the byte array.
        /// </summary>
        /// <param name="data">the byte array</param>
        /// <param name="offset">index where to start pulling from the array</param>
        /// <returns>returns a 4-byte unsigned integer</returns>
        internal static UInt32 PopUnsignedInteger(ref byte[] data, ref Int32 offset)
        {
            if (data.Length - offset < 4)
            {
                return(0);
            }

            UInt32 ret = NetConvert.NetworkToHostOrder(BitConverter.ToUInt32(data, offset));

            offset += 4;
            return(ret);
        }
예제 #21
0
        public int WriteTo(byte[] buffer, int posn)
        {
            var len = GetErrorCauseLength(true);

            NetConvert.ToBuffer((ushort)CauseCode, buffer, posn);
            NetConvert.ToBuffer(len, buffer, posn + 2);
            if (NewAddressTLVs != null)
            {
                Buffer.BlockCopy(NewAddressTLVs, 0, buffer, posn + 4, NewAddressTLVs.Length);
            }
            return(len);
        }
예제 #22
0
        public int WriteTo(byte[] buffer, int posn)
        {
            var len = GetErrorCauseLength(true);

            NetConvert.ToBuffer((ushort)CauseCode, buffer, posn);
            NetConvert.ToBuffer(len, buffer, posn + 2);
            if (UnrecognizedParameters != null)
            {
                Buffer.BlockCopy(UnrecognizedParameters, 0, buffer, posn + 4, UnrecognizedParameters.Length);
            }
            return(len);
        }
예제 #23
0
        public STUNAttribute(STUNAttributeTypesEnum attributeType, int value)
        {
            AttributeType = attributeType;

            if (BitConverter.IsLittleEndian)
            {
                Value = BitConverter.GetBytes(NetConvert.DoReverseEndian(Convert.ToUInt32(value)));
            }
            else
            {
                Value = BitConverter.GetBytes(value);
            }
        }
예제 #24
0
        public int WriteTo(byte[] buffer, int posn)
        {
            var len = GetErrorCauseLength(true);

            NetConvert.ToBuffer((ushort)CauseCode, buffer, posn);
            NetConvert.ToBuffer(len, buffer, posn + 2);
            if (!string.IsNullOrEmpty(AdditionalInformation))
            {
                var reasonBuffer = Encoding.UTF8.GetBytes(AdditionalInformation);
                Buffer.BlockCopy(reasonBuffer, 0, buffer, posn + 4, reasonBuffer.Length);
            }
            return(len);
        }
예제 #25
0
        /// <summary>
        /// Gets the chunk bytes for an unsigned int chunk type.
        /// </summary>
        public static byte[] GetBytes(ChunkTypeEnum chunkType, uint val)
        {
            byte[] buf = InitBuffer(chunkType, MINIMUM_CHUNK_LENGTH + 4);

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(val)), 0, buf, MINIMUM_CHUNK_LENGTH, 4);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(val), 0, buf, MINIMUM_CHUNK_LENGTH, 4);
            }
            return(buf);
        }
예제 #26
0
        public STUNAddressAttribute(byte[] attributeValue)
            : base(STUNAttributeTypesEnum.MappedAddress, attributeValue)
        {
            if (BitConverter.IsLittleEndian)
            {
                Port = NetConvert.DoReverseEndian(BitConverter.ToUInt16(attributeValue, 2));
            }
            else
            {
                Port = BitConverter.ToUInt16(attributeValue, 2);
            }

            Address = new IPAddress(new byte[] { attributeValue[4], attributeValue[5], attributeValue[6], attributeValue[7] });
        }
예제 #27
0
        public byte[] GetBytes()
        {
            byte[] payload = new byte[RTCPREPORT_BYTES_LENGTH];

            if (BitConverter.IsLittleEndian)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(SyncSource)), 0, payload, 0, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((ulong)SampleStartTime.Ticks)), 0, payload, 4, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((ulong)SampleEndTime.Ticks)), 0, payload, 12, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(StartSequenceNumber)), 0, payload, 20, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian(EndSequenceNumber)), 0, payload, 22, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)TotalPackets)), 0, payload, 24, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)OutOfOrder)), 0, payload, 28, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)JitterAverage)), 0, payload, 32, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)JitterMaximum)), 0, payload, 36, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)JitterDiscards)), 0, payload, 40, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)PacketsLost)), 0, payload, 44, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)Duplicates)), 0, payload, 48, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((ulong)BytesReceived)), 0, payload, 52, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)TransmissionRate)), 0, payload, 60, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((ulong)Duration)), 0, payload, 64, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)AverageTransitTime)), 0, payload, 72, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)ReportNumber)), 0, payload, 76, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(NetConvert.DoReverseEndian((uint)LastReceivedReportNumber)), 0, payload, 80, 4);
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(SyncSource), 0, payload, 0, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(SampleStartTime.Ticks), 0, payload, 4, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(SampleEndTime.Ticks), 0, payload, 12, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(StartSequenceNumber), 0, payload, 20, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(EndSequenceNumber), 0, payload, 22, 2);
                Buffer.BlockCopy(BitConverter.GetBytes(TotalPackets), 0, payload, 24, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(OutOfOrder), 0, payload, 28, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(JitterAverage), 0, payload, 32, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(JitterMaximum), 0, payload, 36, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(JitterDiscards), 0, payload, 40, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(PacketsLost), 0, payload, 44, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(Duplicates), 0, payload, 48, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(BytesReceived), 0, payload, 52, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(TransmissionRate), 0, payload, 60, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(Duration), 0, payload, 64, 8);
                Buffer.BlockCopy(BitConverter.GetBytes(AverageTransitTime), 0, payload, 72, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(ReportNumber), 0, payload, 76, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(LastReceivedReportNumber), 0, payload, 76, 4);
            }

            return(payload);
        }
예제 #28
0
        /// <summary>
        /// Parses the an SCTP header from a buffer.
        /// </summary>
        /// <param name="buffer">The buffer to parse the SCTP header from.</param>
        /// <param name="posn">The position in the buffer to start parsing the header from.</param>
        /// <returns>A new SCTPHeaer instance.</returns>
        public static SctpHeader Parse(byte[] buffer, int posn)
        {
            if (buffer.Length < SCTP_HEADER_LENGTH)
            {
                throw new ApplicationException("The buffer did not contain the minimum number of bytes for an SCTP header.");
            }

            SctpHeader header = new SctpHeader();

            header.SourcePort      = NetConvert.ParseUInt16(buffer, posn);
            header.DestinationPort = NetConvert.ParseUInt16(buffer, posn + 2);
            header.VerificationTag = NetConvert.ParseUInt32(buffer, posn + 4);
            header.Checksum        = NetConvert.ParseUInt32(buffer, posn + 8);

            return(header);
        }
예제 #29
0
        /// <summary>
        /// The first 32 bits of all chunk parameters represent the type and length. This method
        /// parses those fields and sets them on the current instance.
        /// </summary>
        /// <param name="buffer">The buffer holding the serialised chunk parameter.</param>
        /// <param name="posn">The position in the buffer that indicates the start of the chunk parameter.</param>
        public ushort ParseFirstWord(byte[] buffer, int posn)
        {
            ParameterType = NetConvert.ParseUInt16(buffer, posn);
            ushort paramLen = NetConvert.ParseUInt16(buffer, posn + 2);

            if (paramLen > 0 && buffer.Length < posn + paramLen)
            {
                // The buffer was not big enough to supply the specified chunk parameter.
                int bytesRequired  = paramLen;
                int bytesAvailable = buffer.Length - posn;
                throw new ApplicationException($"The SCTP chunk parameter buffer was too short. " +
                                               $"Required {bytesRequired} bytes but only {bytesAvailable} available.");
            }

            return(paramLen);
        }
예제 #30
0
 public STUNXORAddressAttribute(STUNAttributeTypesEnum attributeType, byte[] attributeValue)
     : base(attributeType, attributeValue)
 {
     if (BitConverter.IsLittleEndian)
     {
         Port = NetConvert.DoReverseEndian(BitConverter.ToUInt16(attributeValue, 2)) ^ (UInt16)(STUNHeader.MAGIC_COOKIE >> 16);
         UInt32 address = NetConvert.DoReverseEndian(BitConverter.ToUInt32(attributeValue, 4)) ^ STUNHeader.MAGIC_COOKIE;
         Address = new IPAddress(NetConvert.DoReverseEndian(address));
     }
     else
     {
         Port = BitConverter.ToUInt16(attributeValue, 2) ^ (UInt16)(STUNHeader.MAGIC_COOKIE >> 16);
         UInt32 address = BitConverter.ToUInt32(attributeValue, 4) ^ STUNHeader.MAGIC_COOKIE;
         Address = new IPAddress(address);
     }
 }