Esempio n. 1
0
                                   1;      // extensions length
        public byte[] Encode()
        {
            var size = MinEncodedSize;

            if (ExtensionIds != null)
            {
                foreach (var extensionId in ExtensionIds)
                {
                    size += 1 + extensionId.Length;
                }
            }

            byte[] data = new byte[size];
            P2ptpCommon.EncodeHeader(data, PacketType.hello);
            int index = P2ptpCommon.HeaderSize;

            PeerId.Encode(FromPeerId, data, ref index);
            StreamId.Encode(StreamId, data, ref index);
            PeerId.Encode(ToPeerId, data, ref index);
            P2ptpCommon.EncodeUInt32(data, ref index, LibraryVersion);
            P2ptpCommon.EncodeUInt16(data, ref index, ProtocolVersion);
            data[index++] = (byte)Status;
            P2ptpCommon.EncodeUInt32(data, ref index, RequestTime32);
            data[index++] = RoleFlags;
            data[index++] = (byte)(ExtensionIds?.Length ?? 0);
            if (ExtensionIds != null)
            {
                foreach (var extensionId in ExtensionIds)
                {
                    P2ptpCommon.EncodeString1ASCII(data, ref index, extensionId);
                }
            }
            return(data);
        }
Esempio n. 2
0
 internal static PeersListPacket_SharedPeerIpv4 Decode(byte[] data, ref int index)
 {
     return(new PeersListPacket_SharedPeerIpv4(
                StreamId.Decode(data, ref index),
                PeerId.Decode(data, ref index),
                new IPEndPoint(new IPAddress(P2ptpCommon.DecodeUInt32(data, ref index)), P2ptpCommon.DecodeUInt16(data, ref index))
                ));
 }
        internal static (PeerId fromPeerId, PeerId toPeerId, StreamId streamId, string extensionId) ParseExtensionSignalingPacket(BinaryReader reader)
        {
            var fromPeerId  = PeerId.Decode(reader);
            var toPeerId    = PeerId.Decode(reader);
            var streamId    = StreamId.Decode(reader);
            var extensionId = P2ptpCommon.DecodeString1ASCII(reader);

            return(fromPeerId, toPeerId, streamId, extensionId);
        }
Esempio n. 4
0
        internal void Encode(byte[] data, ref int index)
        {
            StreamId.Encode(FromSocketAtStreamId, data, ref index);
            PeerId.Encode(ToPeerId, data, ref index);
            if (ToEndPoint.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
            {
                throw new NotSupportedException("only IPv4 is supported");
            }
#pragma warning disable CS0618 // Type or member is obsolete
            P2ptpCommon.EncodeUInt32(data, ref index, (uint)ToEndPoint.Address.Address);
#pragma warning restore CS0618 // Type or member is obsolete
            P2ptpCommon.EncodeUInt16(data, ref index, (ushort)ToEndPoint.Port);
        }
Esempio n. 5
0
        public static StreamId Decode(BinaryReader reader)
        {
            var id = P2ptpCommon.DecodeUInt32(reader);

            if (id == 0)
            {
                return(null);
            }
            else
            {
                return(new StreamId(id));
            }
        }
Esempio n. 6
0
        public static StreamId Decode(byte[] data, ref int index)
        {
            var id = P2ptpCommon.DecodeUInt32(data, ref index);

            if (id == 0)
            {
                return(null);
            }
            else
            {
                return(new StreamId(id));
            }
        }
Esempio n. 7
0
        public byte[] Encode()
        {
            var data = new byte[EncodedSizeMinimum + SharedPeers.Length * PeersListPacket_SharedPeerIpv4.EncodedSize];

            P2ptpCommon.EncodeHeader(data, PacketTypes.peersListIpv4);
            var index = P2ptpCommon.HeaderSize;

            PeerId.Encode(FromPeerId, data, ref index);
            StreamId.Encode(StreamId, data, ref index);

            data[index++] = (byte)SharedPeers.Length;
            foreach (var peer in SharedPeers)
            {
                peer.Encode(data, ref index);
            }

            return(data);
        }
Esempio n. 8
0
        //const int MinEncodedSize = P2ptpCommon.HeaderSize +
        //        PeerId.EncodedSize + StreamId.EncodedSize +
        //        PeerId.EncodedSize +
        //        4 + 2 + // library, protocol version
        //        1 + // status
        //        4 + // requesttime
        //        1 + // role flags
        //        1; // extensions length
        //const int OptionalEncodedSize = 2 + // RequestSequenceNumber
        //    2 +//ResponseCpuDelayMs
        //    1; // RequestedFromIp length
        public byte[] Encode()
        {
            //  var size = MinEncodedSize;
            //    if (ExtensionIds != null)
            //        foreach (var extensionId in ExtensionIds)
            //           size += 1 + extensionId.Length;
            var requestedFromIp = RequestedFromIp ?? "";

            FlagIshareMyIpLocation = (IpLocationData != null);
            //    size += OptionalEncodedSize + requestedFromIp.Length;

            //  byte[] data = new byte[size];
            PacketProcedures.CreateBinaryWriter(out var ms, out var writer);
            P2ptpCommon.EncodeHeader(writer, PacketTypes.hello);
            //  int index = P2ptpCommon.HeaderSize;
            PeerId.Encode(writer, FromPeerId);
            StreamId.Encode(writer, StreamId);
            PeerId.Encode(writer, ToPeerId);
            writer.Write(LibraryVersion);
            writer.Write(ProtocolVersion);
            writer.Write((byte)Status);
            writer.Write(RequestTime32);
            writer.Write(Flags);
            writer.Write((byte)(ExtensionIds?.Length ?? 0));
            if (ExtensionIds != null)
            {
                foreach (var extensionId in ExtensionIds)
                {
                    PacketProcedures.EncodeString1ASCII(writer, extensionId);
                }
            }

            writer.Write(RequestSequenceNumber ?? 0);
            writer.Write(ResponseCpuDelayMs ?? 0);
            PacketProcedures.EncodeString1ASCII(writer, requestedFromIp);

            if (FlagIshareMyIpLocation)
            {
                IpLocationData.Encode(writer);
            }

            return(ms.ToArray());
        }
 public static void InitializeExtensionSignalingPacket(BinaryWriter writer, PeerId fromPeerId, PeerId toPeerId, StreamId streamId, string extensionId)
 {
     if (fromPeerId == null)
     {
         throw new ArgumentNullException(nameof(fromPeerId));
     }
     if (toPeerId == null)
     {
         throw new ArgumentNullException(nameof(toPeerId));
     }
     if (streamId == null)
     {
         throw new ArgumentNullException(nameof(streamId));
     }
     P2ptpCommon.EncodeHeader(writer, PacketType.extensionSignaling);
     PeerId.Encode(writer, fromPeerId);
     PeerId.Encode(writer, toPeerId);
     StreamId.Encode(writer, streamId);
     P2ptpCommon.EncodeString1ASCII(writer, extensionId);
 }
Esempio n. 10
0
        public PeerHelloPacket(byte[] data)
        {
            if (data.Length < MinEncodedSize)
            {
                throw new ArgumentException(nameof(data));
            }
            var index = P2ptpCommon.HeaderSize;

            FromPeerId      = PeerId.Decode(data, ref index);
            StreamId        = StreamId.Decode(data, ref index);
            ToPeerId        = PeerId.Decode(data, ref index);
            LibraryVersion  = P2ptpCommon.DecodeUInt32(data, ref index);
            ProtocolVersion = P2ptpCommon.DecodeUInt16(data, ref index);
            Status          = (PeerHelloRequestStatus)data[index++];
            RequestTime32   = P2ptpCommon.DecodeUInt32(data, ref index);
            RoleFlags       = data[index++];
            var extensionIdsLength = data[index++];

            ExtensionIds = new string[extensionIdsLength];
            for (byte i = 0; i < extensionIdsLength; i++)
            {
                ExtensionIds[i] = P2ptpCommon.DecodeString1ASCII(data, ref index);
            }
        }
Esempio n. 11
0
 public static void Encode(BinaryWriter writer, StreamId streamId)
 {
     P2ptpCommon.EncodeUInt32(writer, streamId?.Id ?? 0);
 }
Esempio n. 12
0
 public static void Encode(StreamId streamId, byte[] data, ref int index)
 {
     P2ptpCommon.EncodeUInt32(data, ref index, streamId?.Id ?? 0);
 }