Example #1
0
        // FIXME: this method is not safe from amplification attack
        // maybe ping/pong/ping/pong is required
        private async Task ReceiveFindPeerAsync(FindNeighbors findNeighbors)
        {
            if (!(findNeighbors.Remote is null))
            {
                await UpdateAsync(findNeighbors.Remote);
            }

            List <BoundPeer> found = _routing.Neighbors(findNeighbors.Target, BucketSize).ToList();

            Neighbors neighbors = new Neighbors(found)
            {
                Identity = findNeighbors.Identity,
            };

            _swarm.ReplyMessage(neighbors);
        }
Example #2
0
        private async Task <IEnumerable <BoundPeer> > GetNeighbors(
            BoundPeer addressee,
            Address target,
            TimeSpan?timeout,
            CancellationToken cancellationToken)
        {
            var findPeer = new FindNeighbors(target);

            try
            {
                Message reply = await _transport.SendMessageWithReplyAsync(
                    addressee,
                    findPeer,
                    timeout,
                    cancellationToken
                    );

                if (!(reply is Neighbors neighbors))
                {
                    throw new InvalidMessageException(
                              $"Reply to {nameof(FindNeighbors)} is invalid.",
                              reply
                              );
                }

                return(neighbors.Found);
            }
            catch (InvalidTimestampException)
            {
                _logger.Debug($"Reply of {nameof(GetNeighbors)}'s timestamp is stale.");
                return(ImmutableArray <BoundPeer> .Empty);
            }
            catch (TimeoutException)
            {
                RemovePeer(addressee);
                return(ImmutableArray <BoundPeer> .Empty);
            }
        }