Skip to content

Commit 142cd7e

Browse files
committed
CSHARP-1462: IPv6 addresses were getting lost in the shuffle.
1 parent 873db20 commit 142cd7e

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

src/MongoDB.Driver.Tests/ClusterRegistryTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void Instance_should_return_the_same_instance_every_time()
4242
public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
4343
{
4444
var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
45-
var servers = new[] { new MongoServerAddress("localhost") };
45+
var servers = new[] { new MongoServerAddress("localhost"), new MongoServerAddress("127.0.0.1", 30000), new MongoServerAddress("[::1]", 27018) };
4646

4747
var sslSettings = new SslSettings
4848
{
@@ -77,17 +77,20 @@ public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings
7777

7878
using (var cluster = subject.GetOrCreateCluster(clientSettings.ToClusterKey()))
7979
{
80-
var address = clientSettings.Servers.Single();
81-
var endPoints = new[] { new DnsEndPoint(address.Host, address.Port) };
80+
var endPoints = new EndPoint[]
81+
{
82+
new DnsEndPoint("localhost", 27017),
83+
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000),
84+
new IPEndPoint(IPAddress.Parse("[::1]"), 27018)
85+
};
8286
cluster.Settings.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
8387
cluster.Settings.EndPoints.Equals(endPoints);
8488
cluster.Settings.ReplicaSetName.Should().Be("rs");
8589
cluster.Settings.ServerSelectionTimeout.Should().Be(clientSettings.ServerSelectionTimeout);
8690
cluster.Settings.PostServerSelector.Should().NotBeNull().And.Subject.Should().BeOfType<LatencyLimitingServerSelector>();
8791
cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(20);
8892

89-
var serverDescription = cluster.Description.Servers.Single(s => s.EndPoint.Equals(endPoints[0]));
90-
serverDescription.EndPoint.Should().Be(endPoints[0]);
93+
cluster.Description.Servers.Select(s => s.EndPoint).Should().Contain(endPoints);
9194

9295
// TODO: don't know how to test the rest of the settings because they are all private to the cluster
9396
}

src/MongoDB.Driver.Tests/MongoUrlBuilderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ public void TestServer(string host, int? port, string connectionString)
749749
[TestCase(new string[] { "host1", "host2" }, new object[] { 27018, null }, "mongodb://host1:27018,host2")]
750750
[TestCase(new string[] { "host1", "host2" }, new object[] { 27018, 27017 }, "mongodb://host1:27018,host2")]
751751
[TestCase(new string[] { "host1", "host2" }, new object[] { 27018, 27018 }, "mongodb://host1:27018,host2:27018")]
752+
[TestCase(new string[] { "[::1]", "host2" }, new object[] { 27018, 27018 }, "mongodb://[::1]:27018,host2:27018")]
752753
public void TestServers(string[] hosts, object[] ports, string connectionString)
753754
{
754755
var servers = (hosts == null) ? null : new List<MongoServerAddress>();

src/MongoDB.Driver/ClusterRegistry.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using System.Net;
1919
using System.Net.Security;
20+
using System.Net.Sockets;
2021
using System.Security.Cryptography.X509Certificates;
2122
using MongoDB.Driver.Core.Authentication;
2223
using MongoDB.Driver.Core.Clusters;
@@ -79,7 +80,7 @@ private ICluster CreateCluster(ClusterKey clusterKey)
7980

8081
private ClusterSettings ConfigureCluster(ClusterSettings settings, ClusterKey clusterKey)
8182
{
82-
var endPoints = clusterKey.Servers.Select(s => (EndPoint)new DnsEndPoint(s.Host, s.Port));
83+
var endPoints = clusterKey.Servers.Select(s => EndPointHelper.Parse(s.ToString()));
8384
return settings.With(
8485
connectionMode: clusterKey.ConnectionMode.ToCore(),
8586
endPoints: Optional.Enumerable(endPoints),
@@ -140,6 +141,11 @@ private SslStreamSettings ConfigureSsl(SslStreamSettings settings, ClusterKey cl
140141

141142
private TcpStreamSettings ConfigureTcp(TcpStreamSettings settings, ClusterKey clusterKey)
142143
{
144+
if (clusterKey.IPv6)
145+
{
146+
settings = settings.With(addressFamily: AddressFamily.InterNetworkV6);
147+
}
148+
143149
return settings.With(
144150
connectTimeout: clusterKey.ConnectTimeout,
145151
readTimeout: clusterKey.SocketTimeout,

src/MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,12 @@ public void Parse(string url)
594594
}
595595
else if ((ipEndPoint = endPoint as IPEndPoint) != null)
596596
{
597-
return new MongoServerAddress(ipEndPoint.Address.ToString(), ipEndPoint.Port);
597+
var address = ipEndPoint.Address.ToString();
598+
if (ipEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
599+
{
600+
address = "[" + address + "]";
601+
}
602+
return new MongoServerAddress(address, ipEndPoint.Port);
598603
}
599604
else
600605
{

0 commit comments

Comments
 (0)