Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #78 from jordansjones/support-ipv6
Browse files Browse the repository at this point in the history
Attempt to support IPv6 addresses
  • Loading branch information
Aaronontheweb committed May 17, 2016
2 parents 2469f5c + 5c302bd commit 8333f2f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Helios/Net/Connections/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private void InitClient(Socket client)

private void InitClient()
{
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
_client = new Socket(Binding.Host.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
{
ReceiveTimeout = Timeout.Seconds,
SendTimeout = Timeout.Seconds,
Expand Down
2 changes: 1 addition & 1 deletion src/Helios/Net/Connections/UdpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ protected override void SendInternal(byte[] buffer, int index, int length, INode

protected void InitClient()
{
Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Client = new Socket(Binding.Host.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
{
MulticastLoopback = false
};
Expand Down
2 changes: 1 addition & 1 deletion src/Helios/Reactor/ReactorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected ReactorBase(IPAddress localAddress, int localPort, NetworkEventLoop ev
Encoder = encoder;
Allocator = allocator;
LocalEndpoint = new IPEndPoint(localAddress, localPort);
Listener = new Socket(AddressFamily.InterNetwork, socketType, protocol);
Listener = new Socket(LocalEndpoint.AddressFamily, socketType, protocol);
if (protocol == ProtocolType.Tcp)
{
Transport = TransportType.Tcp;
Expand Down
2 changes: 1 addition & 1 deletion src/Helios/Reactor/Tcp/TcpProxyReactor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TcpProxyReactor(IPAddress localAddress, int localPort, NetworkEventLoop e
bufferSize)
{
LocalEndpoint = new IPEndPoint(localAddress, localPort);
Listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Listener = new Socket(LocalEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}

public override bool IsActive { get; protected set; }
Expand Down
7 changes: 5 additions & 2 deletions src/Helios/Topology/NodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public static INode Host(this INode n, string host)
if (!IPAddress.TryParse(host, out parseIp))
{
var hostentry = Dns.GetHostEntry(host);
parseIp = hostentry.AddressList.First(x => x.AddressFamily == AddressFamily.InterNetwork);
//first IPv4 address
parseIp = hostentry.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork); // first IPv4 address
if (parseIp == null)
{
parseIp = hostentry.AddressList.First(x => x.AddressFamily == AddressFamily.InterNetworkV6); // first IPv6 address
}
}

return Host(n, parseIp);
Expand Down
13 changes: 12 additions & 1 deletion src/Helios/Topology/NodeUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using Helios.Util;

Expand Down Expand Up @@ -33,7 +34,7 @@ protected NodeUri(SerializationInfo serializationInfo, StreamingContext streamin
public static string GetUriStringForNode(INode node)
{
if (node.IsEmpty()) return string.Empty;
return string.Format("{0}://{1}:{2}", GetProtocolStringForTransportType(node.TransportType), node.Host,
return string.Format("{0}://{1}:{2}", GetProtocolStringForTransportType(node.TransportType), GetHostStringForAddress(node.Host),
node.Port);
}

Expand All @@ -50,6 +51,16 @@ public static string GetProtocolStringForTransportType(TransportType transport)
}
}

public static string GetHostStringForAddress(IPAddress address)
{
var host = address.ToString();
if (address.AddressFamily == AddressFamily.InterNetworkV6)
{
host = string.Format("[{0}]", host);
}
return host;
}

#if !NET35 && !NET40
public static INode GetNodeFromUri(Uri uri)
{
Expand Down
22 changes: 22 additions & 0 deletions tests/Helios.Tests/Net/TcpConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ public void Should_throw_exception_when_connecting_to_unreachable_node()
Assert.True(boolDisconnected);
}

[Fact]
public void Should_not_throw_exception_when_connecting_via_ipv6()
{
// arrange
var node = NodeBuilder.BuildNode().Host(IPAddress.IPv6Loopback).WithPort(11111);
var connection = node.GetConnection();
var boolDisconnected = false;
var resetEvent = new AutoResetEvent(false);
connection.OnDisconnection += delegate
{
boolDisconnected = true;
resetEvent.Set();
};

// act
connection.Open();
resetEvent.WaitOne();

// assert
Assert.True(boolDisconnected);
}

#endregion

#region Setup / Teardown
Expand Down
17 changes: 17 additions & 0 deletions tests/Helios.Tests/Topology/NodeUriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ public void Should_convert_valid_tcp_INode_to_NodeUri()
Assert.True(nodeUri.IsLoopback);
}

[Fact]
public void Should_convert_valid_ipv6_tcp_INode_to_NodeUri()
{
//arrange
var testNode =
NodeBuilder.BuildNode().Host(IPAddress.IPv6Loopback).WithPort(1337).WithTransportType(TransportType.Tcp);

//act
var nodeUri = new NodeUri(testNode);

//assert
Assert.Equal(testNode.Port, nodeUri.Port);
Assert.Equal(string.Format("[{0}]", testNode.Host), nodeUri.Host);
Assert.Equal("tcp", nodeUri.Scheme);
Assert.True(nodeUri.IsLoopback);
}

[Fact]
public void Should_convert_valid_tcp_NodeUri_to_INode()
{
Expand Down

0 comments on commit 8333f2f

Please sign in to comment.