Exemplo n.º 1
0
        /// <summary>
        /// creates response to request and sends the response
        /// </summary>
        /// <param name="localPeerId">optional local test node ID, is sent by server to new peers who dont know server's PeerId</param>
        internal static void Respond(PeerHelloPacket requestPacket, PeerHelloRequestStatus status, PeerId localPeerId,
                                     SocketWithReceiver socket, IPEndPoint remoteEndPoint, bool thisPeerRoleAsUser = false)
        {
            var responseData = new PeerHelloPacket(requestPacket, status, localPeerId, thisPeerRoleAsUser).Encode();

            socket.UdpSocket.Send(responseData, responseData.Length, remoteEndPoint);
        }
Exemplo n.º 2
0
        public PeerHelloPacket(byte[] packetUdpPayloadData)
        {
            //  if (packetUdpPayloadData.Length < MinEncodedSize) throw new ArgumentException(nameof(packetUdpPayloadData));
            var index = P2ptpCommon.HeaderSize;

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

            ExtensionIds = new string[extensionIdsLength];
            for (byte i = 0; i < extensionIdsLength; i++)
            {
                ExtensionIds[i] = PacketProcedures.DecodeString1ASCII(packetUdpPayloadData, ref index);
            }
            if (index < packetUdpPayloadData.Length)
            { // after version 190608
                RequestSequenceNumber = PacketProcedures.DecodeUInt16(packetUdpPayloadData, ref index);
                ResponseCpuDelayMs    = PacketProcedures.DecodeUInt16(packetUdpPayloadData, ref index);
                RequestedFromIp       = PacketProcedures.DecodeString1ASCII(packetUdpPayloadData, ref index);
                var reader = PacketProcedures.CreateBinaryReader(packetUdpPayloadData, index);
                if (FlagIshareMyIpLocation)
                {
                    IpLocationData = IpLocationData.Decode(reader);
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// creates packet for response
 /// </summary>
 private PeerHelloPacket(PeerHelloPacket requestPacket, PeerHelloRequestStatus status, PeerId localPeerId, bool thisPeerRoleAsUser)
 {
     LibraryVersion  = CompilationInfo.CompilationDateTimeUtc_uint32;
     ProtocolVersion = P2ptpCommon.ProtocolVersion;
     FromPeerId      = localPeerId ?? requestPacket.ToPeerId;
     ToPeerId        = requestPacket.FromPeerId;
     StreamId        = requestPacket.StreamId;
     Status          = status;
     RequestTime32   = requestPacket.RequestTime32;
     RoleFlags       = thisPeerRoleAsUser ? (byte)0x01 : (byte)0x00;
 }
Exemplo n.º 4
0
        public readonly string[] ExtensionIds; // nullable  // not null only for requests

        /// <summary>
        /// creates packet for transmission to peer
        /// </summary>
        /// <param name="connectedPeer">destination</param>
        /// <param name="stream">destination</param>
        public PeerHelloPacket(LocalPeer localPeer, ConnectedPeer connectedPeer, ConnectedPeerStream stream, PeerHelloRequestStatus status)
        {
            LibraryVersion  = CompilationInfo.CompilationDateTimeUtc_uint32;
            ProtocolVersion = P2ptpCommon.ProtocolVersion;
            FromPeerId      = localPeer.LocalPeerId;
            ExtensionIds    = localPeer.Configuration.Extensions?.Select(x => x.ExtensionId).ToArray();
            StreamId        = stream.StreamId;
            ToPeerId        = connectedPeer.RemotePeerId;
            Status          = status;
            RequestTime32   = localPeer.Time32;
            RoleFlags       = localPeer.Configuration.RoleAsUser ? (byte)0x01 : (byte)0x00;
        }
Exemplo n.º 5
0
        /// <summary>
        /// creates response to request and sends the response
        /// </summary>
        /// <param name="localPeerId">optional local test node ID, is sent by server to new peers who dont know server's PeerId</param>
        internal static void Respond(PeerHelloPacket requestPacket, PeerHelloRequestStatus status, PeerId localPeerId,
                                     SocketWithReceiver socket, IPEndPoint remoteEndPoint, ushort?responseCpuDelayMs = null, bool thisPeerRoleAsUser = false, IpLocationData localIpLocationData = null)
        {
            var responsePacket = new PeerHelloPacket(requestPacket, status, localPeerId, thisPeerRoleAsUser, responseCpuDelayMs, remoteEndPoint.Address.ToString());

            if (localIpLocationData != null && requestPacket.FlagIwantToGetYourIpLocation)
            {
                responsePacket.IpLocationData = localIpLocationData;
            }
            var responseData = responsePacket.Encode();

            socket.UdpSocket.Send(responseData, responseData.Length, remoteEndPoint);
        }
Exemplo n.º 6
0
 /// <summary>
 /// creates packet for response
 /// </summary>
 private PeerHelloPacket(PeerHelloPacket requestPacket, PeerHelloRequestStatus status, PeerId localPeerId, bool thisPeerRoleAsUser, ushort?responseCpuDelayMs, string requestedFromIp)
 {
     LibraryVersion        = MiscProcedures.CompilationDateTimeUtc_uint32Seconds;
     ProtocolVersion       = P2ptpCommon.ProtocolVersion;
     FromPeerId            = localPeerId ?? requestPacket.ToPeerId;
     ToPeerId              = requestPacket.FromPeerId;
     StreamId              = requestPacket.StreamId;
     Status                = status;
     RequestTime32         = requestPacket.RequestTime32;
     Flags                 = thisPeerRoleAsUser ? (byte)0x01 : (byte)0x00;
     RequestSequenceNumber = requestPacket.RequestSequenceNumber;
     ResponseCpuDelayMs    = responseCpuDelayMs;
     RequestedFromIp       = requestedFromIp;
 }
Exemplo n.º 7
0
        }                                                          // not null if FlagIshareMyIpLocation == true

        /// <summary>
        /// creates request, for transmission to peer
        /// </summary>
        /// <param name="connectedPeer">destination</param>
        /// <param name="stream">destination</param>
        public PeerHelloPacket(LocalPeer localPeer, ConnectedPeer connectedPeer, ConnectedPeerStream stream, PeerHelloRequestStatus status, bool requestIpLocation)
        {
            LibraryVersion  = MiscProcedures.CompilationDateTimeUtc_uint32Seconds;
            ProtocolVersion = P2ptpCommon.ProtocolVersion;
            FromPeerId      = localPeer.LocalPeerId;
            ExtensionIds    = localPeer.Configuration.Extensions?.Select(x => x.ExtensionId).ToArray();
            StreamId        = stream.StreamId;
            ToPeerId        = connectedPeer.RemotePeerId;
            Status          = status;
            RequestTime32   = localPeer.Time32;
            if (localPeer.Configuration.RoleAsUser)
            {
                Flags |= (byte)0x01;
            }
            if (requestIpLocation)
            {
                Flags |= (byte)0x04;
            }
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
 public static bool IsSetupOrPing(this PeerHelloRequestStatus s) => (s == PeerHelloRequestStatus.ping || s == PeerHelloRequestStatus.setup);