Skip to content

Commit 167e29c

Browse files
Merge pull request #224 from rabbitmq/rabbitmq-dotnet-client-171
work around mono bug with BeginConnect and IPv6
2 parents d525960 + aaff59c commit 167e29c

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net;
45
using System.Net.Sockets;
56
using System.Text;
67

78
namespace RabbitMQ.Client
89
{
10+
static class IPAddressExt
11+
{
12+
// TODO: This method should already exist on IPAddress
13+
// but for some reason this does to compile against mono 4.4.1
14+
public static IPAddress MapToIPv6(this IPAddress addr)
15+
{
16+
var bytes = addr.GetAddressBytes();
17+
var bytes6 = new byte []
18+
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF,
19+
bytes [0], bytes [1], bytes [2], bytes [3] };
920

21+
return new IPAddress (bytes6);
22+
}
23+
}
1024

1125
/// <summary>
12-
/// Simple wrapper around TcpClient.
26+
/// Simple wrapper around TcpClient.
1327
/// </summary>
1428
public class TcpClientAdapter : ITcpClient
1529
{
@@ -24,8 +38,13 @@ public TcpClientAdapter(TcpClient tcpClient)
2438
public virtual IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state)
2539
{
2640
assertTcpClient();
27-
28-
return _tcpClient.BeginConnect(host, port, requestCallback, state);
41+
var endpoints = Dns.GetHostAddresses(host);
42+
if(_tcpClient.Client.AddressFamily == AddressFamily.InterNetworkV6)
43+
{
44+
endpoints = endpoints.Select(a => a.MapToIPv6()).ToArray();
45+
}
46+
47+
return _tcpClient.BeginConnect(endpoints, port, requestCallback, state);
2948
}
3049

3150
private void assertTcpClient()

0 commit comments

Comments
 (0)