private Peer AddPeer(PeerTable table, string addr, PeerOrigin peerOrigin, DateTimeOffset now) { var peer = new Peer(peerOrigin, new NetworkAddress() { Endpoint = new IPEndPoint(IPAddress.Parse(addr), 100), Time = now }); table.WritePeer(peer); return peer; }
private Node AddNode(Peer peer, CancellationToken cancellationToken) { try { var node = new Node(peer, this, cancellationToken); return AddNode(node); } catch(Exception) { return null; } }
public Node GetNodeByPeer(Peer peer, CancellationToken cancellation = default(CancellationToken)) { var node = _Nodes.GetNodeByPeer(peer); if(node != null) return node; return AddNode(peer, cancellation); }
public Node GetNodeByEndpoint(IPEndPoint endpoint, CancellationToken cancellation = default(CancellationToken)) { lock(_Nodes) { endpoint = Utils.EnsureIPv6(endpoint); var node = _Nodes.GetNodeByEndpoint(endpoint); if(node != null) return node; var peer = PeerTable.GetPeer(endpoint); if(peer == null) peer = new Peer(PeerOrigin.Manual, new NetworkAddress() { Endpoint = endpoint, Time = Utils.UnixTimeToDateTime(0) }); return AddNode(peer, cancellation); } }
public VersionPayload CreateVersionPayload(Peer peer, IPEndPoint me, ProtocolVersion? version) { me = Utils.EnsureIPv6(me); return new VersionPayload() { Nonce = Nonce, UserAgent = UserAgent, Version = version == null ? Version : version.Value, StartHeight = 0, Timestamp = DateTimeOffset.UtcNow, AddressReciever = peer.NetworkAddress.Endpoint, AddressFrom = me }; }
private static void CanStorePeer(PeerTableRepository repository) { var peer = new Peer(PeerOrigin.Addr, new NetworkAddress() { Endpoint = new IPEndPoint(IPAddress.Parse("0.0.1.0").MapToIPv6(), 110), Time = DateTimeOffset.UtcNow - TimeSpan.FromMinutes(5) }); repository.WritePeer(peer); var result = repository.GetPeers().ToArray(); Assert.Equal(1, result.Length); Assert.Equal(PeerOrigin.Addr, result[0].Origin); Assert.Equal(IPAddress.Parse("0.0.1.0").MapToIPv6(), result[0].NetworkAddress.Endpoint.Address); Assert.Equal(110, result[0].NetworkAddress.Endpoint.Port); Assert.Equal(peer.NetworkAddress.Time, result[0].NetworkAddress.Time); peer.NetworkAddress.Time = DateTimeOffset.UtcNow + TimeSpan.FromMinutes(5); repository.WritePeer(peer); result = repository.GetPeers().ToArray(); Assert.Equal(1, result.Length); Assert.Equal(peer.NetworkAddress.Time, result[0].NetworkAddress.Time); var peer2 = new Peer(PeerOrigin.Addr, new NetworkAddress() { Endpoint = new IPEndPoint(IPAddress.Parse("0.0.2.0").MapToIPv6(), 110), Time = DateTimeOffset.UtcNow - TimeSpan.FromMinutes(5) }); repository.WritePeers(new Peer[] { peer, peer2 }); Assert.Equal(2, repository.GetPeers().ToArray().Length); peer.NetworkAddress.Time = Utils.UnixTimeToDateTime(0); repository.WritePeer(peer); result = repository.GetPeers().ToArray(); Assert.Equal(1, result.Length); Assert.Equal(IPAddress.Parse("0.0.2.0").MapToIPv6(), result[0].NetworkAddress.Endpoint.Address); }
public void WritePeer(Peer peer) { WritePeers(new Peer[] { peer }); }
public PeerBlob(Peer peer) { Origin = (byte)peer.Origin; Address = peer.NetworkAddress.Endpoint.Address.ToString(); Port = peer.NetworkAddress.Endpoint.Port; LastSeen = peer.NetworkAddress.Time; }
private bool IsFree(Peer p, bool seedsAsFree = true) { if(p == null) return true; var isExpired = p.NetworkAddress.Ago > TimeSpan.FromHours(3.0); var isSeed = p.Origin == PeerOrigin.DNSSeed || p.Origin == PeerOrigin.HardSeed; return isSeed ? seedsAsFree : isExpired; }
public void RemovePeer(Peer peer) { lock(_Peers) { _Peers.Remove(peer.NetworkAddress.Endpoint.ToString()); _PeerSeeds.Remove(peer.NetworkAddress.Endpoint.ToString()); } }