Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Timer's lock with wrong dns #1358

Merged
merged 21 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 3 additions & 49 deletions src/neo/Network/P2P/LocalNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;

Expand Down Expand Up @@ -79,53 +78,6 @@ private void BroadcastMessage(Message message)
Connections.Tell(message);
}

private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, int port)
{
if (IPAddress.TryParse(hostNameOrAddress, out IPAddress ipAddress))
return new IPEndPoint(ipAddress, port);
IPHostEntry entry;
try
{
entry = Dns.GetHostEntry(hostNameOrAddress);
}
catch (SocketException)
{
return null;
}
ipAddress = entry.AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork || p.IsIPv6Teredo);
if (ipAddress == null) return null;
return new IPEndPoint(ipAddress, port);
}

/// <summary>
/// Return an amount of random seeds nodes from the default SeedList file defined on <see cref="ProtocolSettings"/>.
/// </summary>
/// <param name="seedsToTake">Limit of random seed nodes to be obtained, also limited by the available seeds from file.</param>
private static IEnumerable<IPEndPoint> GetIPEndPointsFromSeedList(int seedsToTake)
{
if (seedsToTake > 0)
{
Random rand = new Random();
foreach (string hostAndPort in ProtocolSettings.Default.SeedList.OrderBy(p => rand.Next()))
{
if (seedsToTake == 0) break;
string[] p = hostAndPort.Split(':');
IPEndPoint seed;
try
{
seed = GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
}
catch (AggregateException)
{
continue;
}
if (seed == null) continue;
seedsToTake--;
yield return seed;
}
}
}

public IEnumerable<RemoteNode> GetRemoteNodes()
{
return RemoteNodes.Values;
Expand Down Expand Up @@ -153,7 +105,9 @@ protected override void NeedMorePeers(int count)
{
// Will call AddPeers with default SeedList set cached on <see cref="ProtocolSettings"/>.
// It will try to add those, sequentially, to the list of currently uncconected ones.
AddPeers(GetIPEndPointsFromSeedList(count));

Random rand = new Random();
AddPeers(ProtocolSettings.Default.SeedList.OrderBy(p => rand.Next()).Take(count));
}
}

Expand Down
52 changes: 45 additions & 7 deletions src/neo/ProtocolSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Neo
Expand All @@ -10,7 +12,7 @@ public class ProtocolSettings
public uint Magic { get; }
public byte AddressVersion { get; }
public string[] StandbyValidators { get; }
public string[] SeedList { get; }
public IPEndPoint[] SeedList { get; }
public uint MillisecondsPerBlock { get; }
public int MemoryPoolMaxTransactions { get; }

Expand Down Expand Up @@ -61,18 +63,54 @@ private ProtocolSettings(IConfigurationSection section)
};
IConfigurationSection section_sl = section.GetSection("SeedList");
if (section_sl.Exists())
this.SeedList = section_sl.GetChildren().Select(p => p.Get<string>()).ToArray();
this.SeedList = section_sl.GetChildren()
.AsParallel()
.Select(p => GetIpEndPoint(p.Get<string>()))
.Where(u => u != null)
.ToArray();
else
this.SeedList = new[]
{
"seed1.neo.org:10333",
"seed2.neo.org:10333",
"seed3.neo.org:10333",
"seed4.neo.org:10333",
"seed5.neo.org:10333"
GetIpEndPoint("seed1.neo.org:10333"),
GetIpEndPoint("seed2.neo.org:10333"),
GetIpEndPoint("seed3.neo.org:10333"),
GetIpEndPoint("seed4.neo.org:10333"),
GetIpEndPoint("seed5.neo.org:10333")
};
this.MillisecondsPerBlock = section.GetValue("MillisecondsPerBlock", 15000u);
this.MemoryPoolMaxTransactions = Math.Max(1, section.GetValue("MemoryPoolMaxTransactions", 50_000));
}

private static IPEndPoint GetIPEndpointFromHostPort(string hostNameOrAddress, int port)
{
if (IPAddress.TryParse(hostNameOrAddress, out IPAddress ipAddress))
return new IPEndPoint(ipAddress, port);
IPHostEntry entry;
try
{
entry = Dns.GetHostEntry(hostNameOrAddress);
}
catch (SocketException)
{
return null;
}
ipAddress = entry.AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork || p.IsIPv6Teredo);
if (ipAddress == null) return null;
return new IPEndPoint(ipAddress, port);
}

internal static IPEndPoint GetIpEndPoint(string hostAndPort)
{
if (string.IsNullOrEmpty(hostAndPort)) return null;

try
{
string[] p = hostAndPort.Split(':');
return GetIPEndpointFromHostPort(p[0], int.Parse(p[1]));
}
catch { }

return null;
}
}
}
10 changes: 9 additions & 1 deletion tests/neo.UnitTests/UT_ProtocolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ public void TestGetMillisecondsPerBlock()
[TestMethod]
public void TestGetSeedList()
{
ProtocolSettings.Default.SeedList.Should().BeEquivalentTo(new string[] { "seed1.neo.org:10333", "seed2.neo.org:10333", "seed3.neo.org:10333", "seed4.neo.org:10333", "seed5.neo.org:10333", });
ProtocolSettings.Default.SeedList.Should().BeEquivalentTo(
new IPEndPoint[]
{
ProtocolSettings.GetIpEndPoint("seed1.neo.org:10333"),
ProtocolSettings.GetIpEndPoint("seed2.neo.org:10333"),
ProtocolSettings.GetIpEndPoint("seed3.neo.org:10333"),
ProtocolSettings.GetIpEndPoint("seed4.neo.org:10333"),
ProtocolSettings.GetIpEndPoint("seed5.neo.org:10333"),
});
}
}
}