Skip to content

Commit b85d67d

Browse files
Merge pull request #247 from rabbitmq/rabbitmq-dotnet-client-244
Handle the case where a dns lookup returns an address for an
2 parents 855e1e9 + 63e063b commit b85d67d

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

projects/client/RabbitMQ.Client/src/client/impl/TcpClientAdapter.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43
using System.Net.Sockets;
54
using System.Net;
6-
using System.Text;
75
using System.Threading.Tasks;
6+
87
namespace RabbitMQ.Client
98
{
10-
11-
129
/// <summary>
13-
/// Simple wrapper around TcpClient.
10+
/// Simple wrapper around TcpClient.
1411
/// </summary>
1512
public class TcpClientAdapter : ITcpClient
1613
{
@@ -20,14 +17,18 @@ public TcpClientAdapter(Socket socket)
2017
{
2118
if (socket == null)
2219
throw new InvalidOperationException("socket must not be null");
23-
20+
2421
this.sock = socket;
2522
}
2623

2724
public virtual async Task ConnectAsync(string host, int port)
2825
{
2926
var adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
30-
var ep = adds.First();
27+
var ep = adds.FirstOrDefault(a => a.AddressFamily == sock.AddressFamily);
28+
if(ep == default(IPAddress))
29+
{
30+
throw new ArgumentException("No ip address could be resolved for " + host);
31+
}
3132
#if CORECLR
3233
await sock.ConnectAsync(ep, port);
3334
#else
@@ -71,4 +72,4 @@ public virtual int ReceiveTimeout
7172
}
7273
}
7374
}
74-
}
75+
}

projects/client/Unit/src/unit/TestConnectionFactory.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
//---------------------------------------------------------------------------
4040

4141
using System;
42+
using System.Collections.Generic;
4243
using NUnit.Framework;
4344
using RabbitMQ.Client.Exceptions;
4445

@@ -107,7 +108,7 @@ public void TestCreateConnectionWithClientProvidedNameUsesName()
107108
Assert.AreEqual("some_name", conn.ClientProperties["connection_name"]);
108109
}
109110
}
110-
111+
111112
[Test]
112113
public void TestCreateConnectionWithClientProvidedNameAndAutorecoveryUsesName()
113114
{
@@ -186,5 +187,14 @@ public void TestCreateConnectionUsesInvalidAmqpTcpEndpoint()
186187
using(var conn = cf.CreateConnection(new System.Collections.Generic.List<AmqpTcpEndpoint> { ep })) {}
187188
}, Throws.TypeOf<BrokerUnreachableException>());
188189
}
190+
191+
[Test]
192+
public void TestCreateConnectioUsesValidEndpointWhenMultipleSupplied()
193+
{
194+
var cf = new ConnectionFactory();
195+
var invalidEp = new AmqpTcpEndpoint("not_localhost");
196+
var ep = new AmqpTcpEndpoint("localhost");
197+
using(var conn = cf.CreateConnection(new List<AmqpTcpEndpoint> { invalidEp, ep })) {};
198+
}
189199
}
190200
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 1.1.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (c) 2007-2016 Pivotal Software, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v1.1:
23+
//
24+
//---------------------------------------------------------------------------
25+
// The contents of this file are subject to the Mozilla Public License
26+
// Version 1.1 (the "License"); you may not use this file except in
27+
// compliance with the License. You may obtain a copy of the License
28+
// at http://www.mozilla.org/MPL/
29+
//
30+
// Software distributed under the License is distributed on an "AS IS"
31+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
32+
// the License for the specific language governing rights and
33+
// limitations under the License.
34+
//
35+
// The Original Code is RabbitMQ.
36+
//
37+
// The Initial Developer of the Original Code is Pivotal Software, Inc.
38+
// Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using System;
42+
using System.Net.Sockets;
43+
using System.Threading;
44+
using NUnit.Framework;
45+
46+
using RabbitMQ.Client;
47+
48+
namespace RabbitMQ.Client.Unit
49+
{
50+
[TestFixture]
51+
public class TestTcpClientAdapter
52+
{
53+
[Test]
54+
public void ConnectAsyncThrowsArgumentExceptionWhenNoAddressForAddressFamilyCanBeFound()
55+
{
56+
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
57+
var sut = new TcpClientAdapter(socket);
58+
Assert.Throws<ArgumentException>(() =>
59+
{
60+
sut.ConnectAsync("localhost", 5672).GetAwaiter().GetResult();
61+
});
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)