Skip to content

Commit 7da530f

Browse files
committed
CSHARP-1462: IPv6 addresses were getting lost in the shuffle.
1 parent db5c4ff commit 7da530f

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
{
@@ -76,16 +76,19 @@ public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings
7676

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

87-
var serverDescription = cluster.Description.Servers.Single(s => s.EndPoint.Equals(endPoints[0]));
88-
serverDescription.EndPoint.Should().Be(endPoints[0]);
91+
cluster.Description.Servers.Select(s => s.EndPoint).Should().Contain(endPoints);
8992

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

src/MongoDB.Driver.Tests/MongoUrlBuilderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ public void TestServer(string host, int? port, string connectionString)
745745
[TestCase(new string[] { "host1", "host2" }, new object[] { 27018, null }, "mongodb://host1:27018,host2")]
746746
[TestCase(new string[] { "host1", "host2" }, new object[] { 27018, 27017 }, "mongodb://host1:27018,host2")]
747747
[TestCase(new string[] { "host1", "host2" }, new object[] { 27018, 27018 }, "mongodb://host1:27018,host2:27018")]
748+
[TestCase(new string[] { "[::1]", "host2" }, new object[] { 27018, 27018 }, "mongodb://[::1]:27018,host2:27018")]
748749
public void TestServers(string[] hosts, object[] ports, string connectionString)
749750
{
750751
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),
@@ -139,6 +140,11 @@ private SslStreamSettings ConfigureSsl(SslStreamSettings settings, ClusterKey cl
139140

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

src/MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,12 @@ public void Parse(string url)
576576
}
577577
else if ((ipEndPoint = endPoint as IPEndPoint) != null)
578578
{
579-
return new MongoServerAddress(ipEndPoint.Address.ToString(), ipEndPoint.Port);
579+
var address = ipEndPoint.Address.ToString();
580+
if (ipEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
581+
{
582+
address = "[" + address + "]";
583+
}
584+
return new MongoServerAddress(address, ipEndPoint.Port);
580585
}
581586
else
582587
{

0 commit comments

Comments
 (0)