Skip to content

Commit

Permalink
Remove Ice.PreferIPv6Address and update discovery plugins (zeroc-ice#…
Browse files Browse the repository at this point in the history
  • Loading branch information
externl authored Oct 14, 2020
1 parent 90003e4 commit 3742d03
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 128 deletions.
2 changes: 2 additions & 0 deletions config/PropertyNames.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ generated from the section label.
<property name="Package.[any]" />
<property name="Plugin.[any]" />
<property name="PluginLoadOrder" />
<!-- TODO: remove PreferIPv6Address -->
<property name="PreferIPv6Address" />
<property name="PreloadAssemblies" />
<property name="PrintAdapterReady" />
Expand Down Expand Up @@ -444,6 +445,7 @@ generated from the section label.
<property name="Timeout"/>
<property name="RetryCount"/>
<property name="LatencyMultiplier"/>
<!-- TODO: Remove Address, Port, and Interface -->
<property name="Address"/>
<property name="Port"/>
<property name="Interface"/>
Expand Down
3 changes: 0 additions & 3 deletions csharp/src/Ice/Communicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ public IRouterPrx? DefaultRouter
internal IReadOnlyList<InvocationInterceptor> InvocationInterceptors => _invocationInterceptors;
internal bool IsDisposed => _disposeTask != null;
internal INetworkProxy? NetworkProxy { get; }
internal bool PreferIPv6 { get; }

/// <summary>Gets the maximum number of invocation attempts made to send a request including the original
/// invocation. It must be a number greater than 0.</summary>
Expand Down Expand Up @@ -525,8 +524,6 @@ public Communicator(

_backgroundLocatorCacheUpdates = GetPropertyAsBool("Ice.BackgroundLocatorCacheUpdates") ?? false;

PreferIPv6 = GetPropertyAsBool("Ice.PreferIPv6Address") ?? false;

NetworkProxy = CreateNetworkProxy(Network.EnableBoth);

SslEngine = new SslEngine(this, tlsClientOptions, tlsServerOptions);
Expand Down
2 changes: 0 additions & 2 deletions csharp/src/Ice/INetworkProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ await Network.GetAddressesForClientEndpointAsync(_host,
_port,
ipVersion,
EndpointSelectionType.Random,
false,
cancel).ConfigureAwait(false);
return new SOCKSNetworkProxy(addresses.First());
}
Expand Down Expand Up @@ -165,7 +164,6 @@ await Network.GetAddressesForClientEndpointAsync(_host,
_port,
ipVersion,
EndpointSelectionType.Random,
false,
cancel).ConfigureAwait(false);
return new HTTPNetworkProxy(addresses.First(), ipVersion);
}
Expand Down
4 changes: 1 addition & 3 deletions csharp/src/Ice/IPEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ await Network.GetAddressesForClientEndpointAsync(Host,
Port,
ipVersion,
endptSelection,
Communicator.PreferIPv6,
cancel).ConfigureAwait(false);
return addrs.Select(item => CreateConnector(item, networkProxy));
}
Expand All @@ -141,8 +140,7 @@ public override IEnumerable<Endpoint> ExpandHost(out Endpoint? publish)
IEnumerable<IPEndPoint> addresses = Network.GetAddresses(Host,
Port,
Network.EnableBoth,
EndpointSelectionType.Ordered,
Communicator.PreferIPv6);
EndpointSelectionType.Ordered);

if (addresses.Count() == 1)
{
Expand Down
48 changes: 18 additions & 30 deletions csharp/src/Ice/IceDiscovery/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ZeroC.Ice;

Expand Down Expand Up @@ -42,44 +43,31 @@ public async ValueTask DisposeAsync()

public void Initialize(PluginInitializationContext context)
{
string address = _communicator.GetProperty("IceDiscovery.Address") ??
(_communicator.PreferIPv6 ? "ff15::1" : "239.255.0.1");

int port = _communicator.GetPropertyAsInt("IceDiscovery.Port") ?? 4061;
string intf = _communicator.GetProperty("IceDiscovery.Interface") ?? "";
const string defaultIPv4Endpoint = "udp -h 239.255.0.1 -p 4061";
const string defaultIPv6Endpoint = "udp -h \"ff15::1\" -p 4061";

if (_communicator.GetProperty("IceDiscovery.Multicast.Endpoints") == null)
{
if (intf.Length > 0)
{
_communicator.SetProperty("IceDiscovery.Multicast.Endpoints",
$"udp -h \"{address}\" -p {port} --interface \"{intf}\"");
}
else
{
_communicator.SetProperty("IceDiscovery.Multicast.Endpoints", $"udp -h \"{address}\" -p {port}");
}
_communicator.SetProperty("IceDiscovery.Multicast.Endpoints",
$"{defaultIPv4Endpoint}:{defaultIPv6Endpoint}");
}

string lookupEndpoints = _communicator.GetProperty("IceDiscovery.Lookup") ?? "";
if (lookupEndpoints.Length == 0)
string? lookupEndpoints = _communicator.GetProperty("IceDiscovery.Lookup");
if (lookupEndpoints == null)
{
int ipVersion = _communicator.PreferIPv6 ? Network.EnableIPv6 : Network.EnableIPv4;
List<string> interfaces = Network.GetInterfacesForMulticast(intf, ipVersion);
foreach (string p in interfaces)
{
if (p != interfaces[0])
{
lookupEndpoints += ":";
}
lookupEndpoints += $"udp -h \"{address}\" -p {port} --interface \"{p}\"";
}
List<string> endpoints = new ();
List<string> ipv4Interfaces = Network.GetInterfacesForMulticast("0.0.0.0", Network.EnableIPv4);
List<string> ipv6Interfaces = Network.GetInterfacesForMulticast("::0", Network.EnableIPv6);

endpoints.AddRange(ipv4Interfaces.Select(i => $"{defaultIPv4Endpoint} --interface \"{i}\""));
endpoints.AddRange(ipv6Interfaces.Select(i => $"{defaultIPv6Endpoint} --interface \"{i}\""));

lookupEndpoints = string.Join(":", endpoints);
}

if (_communicator.GetProperty("IceDiscovery.Reply.Endpoints") == null)
{
_communicator.SetProperty("IceDiscovery.Reply.Endpoints",
intf.Length == 0 ? "udp -h *" : $"udp -h \"{intf}\"");
_communicator.SetProperty("IceDiscovery.Reply.Endpoints", "udp -h \"::0\" -p 0");
}

if (_communicator.GetProperty("IceDiscovery.Locator.Endpoints") == null)
Expand All @@ -92,15 +80,15 @@ public void Initialize(PluginInitializationContext context)
_locatorAdapter = _communicator.CreateObjectAdapter("IceDiscovery.Locator");

// Setup locator registry.
var locatorRegistry = new LocatorRegistry();
LocatorRegistry locatorRegistry = new ();
ILocatorRegistryPrx locatorRegistryPrx =
_locatorAdapter.AddWithUUID(locatorRegistry, ILocatorRegistryPrx.Factory);

ILookupPrx lookupPrx =
ILookupPrx.Parse($"IceDiscovery/Lookup -d:{lookupEndpoints}", _communicator).Clone(clearRouter: true);

// Add lookup Ice object
var lookup = new Lookup(locatorRegistry, lookupPrx, _communicator, _replyAdapter);
Lookup lookup = new (locatorRegistry, lookupPrx, _communicator, _replyAdapter);
_multicastAdapter.Add("IceDiscovery/Lookup", lookup);

// Setup locator on the communicator.
Expand Down
27 changes: 14 additions & 13 deletions csharp/src/Ice/IceLocatorDiscovery/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,25 +381,26 @@ public async ValueTask DisposeAsync()

public void Initialize(PluginInitializationContext context)
{
string address = _communicator.GetProperty($"{_name}.Address") ??
(_communicator.PreferIPv6 ? "ff15::1" : "239.255.0.1");
const string defaultIPv4Endpoint = "udp -h 239.255.0.1 -p 4061";
const string defaultIPv6Endpoint = "udp -h \"ff15::1\" -p 4061";

int port = _communicator.GetPropertyAsInt($"{_name}.Port") ?? 4061;
string intf = _communicator.GetProperty($"{_name}.Interface") ?? "";

string lookupEndpoints = _communicator.GetProperty($"{_name}.Lookup") ?? "";
if (lookupEndpoints.Length == 0)
string? lookupEndpoints = _communicator.GetProperty($"{_name}.Lookup");
if (lookupEndpoints == null)
{
int ipVersion = _communicator.PreferIPv6 ? Network.EnableIPv6 : Network.EnableIPv4;
List<string> interfaces = Network.GetInterfacesForMulticast(intf, ipVersion);
lookupEndpoints = string.Join(":", interfaces.Select(
intf => $"udp -h \"{address}\" -p {port} --interface \"{intf}\""));

List<string> endpoints = new ();
List<string> ipv4Interfaces = Network.GetInterfacesForMulticast("0.0.0.0", Network.EnableIPv4);
List<string> ipv6Interfaces = Network.GetInterfacesForMulticast("::0", Network.EnableIPv6);

endpoints.AddRange(ipv4Interfaces.Select(i => $"{defaultIPv4Endpoint} --interface \"{i}\""));
endpoints.AddRange(ipv6Interfaces.Select(i => $"{defaultIPv6Endpoint} --interface \"{i}\""));

lookupEndpoints = string.Join(":", endpoints);
}

if (_communicator.GetProperty($"{_name}.Reply.Endpoints") == null)
{
_communicator.SetProperty($"{_name}.Reply.Endpoints",
intf.Length == 0 ? "udp -h *" : $"udp -h \"{intf}\"");
_communicator.SetProperty($"{_name}.Reply.Endpoints", "udp -h \"::0\" -p 0");
}

if (_communicator.GetProperty($"{_name}.Locator.Endpoints") == null)
Expand Down
50 changes: 15 additions & 35 deletions csharp/src/Ice/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ internal static IEnumerable<IPEndPoint> GetAddresses(
string host,
int port,
int ipVersion,
EndpointSelectionType selType,
bool preferIPv6)
EndpointSelectionType selType)
{
try
{
ValueTask<IEnumerable<IPEndPoint>> task = GetAddressesAsync(host,
port,
ipVersion,
selType,
preferIPv6);
selType);
return task.IsCompleted ? task.Result : task.AsTask().Result;
}
catch (AggregateException ex)
Expand All @@ -120,7 +118,6 @@ internal static async ValueTask<IEnumerable<IPEndPoint>> GetAddressesAsync(
int port,
int ipVersion,
EndpointSelectionType selType,
bool preferIPv6,
CancellationToken cancel = default)
{
Debug.Assert(host.Length > 0);
Expand Down Expand Up @@ -172,17 +169,6 @@ internal static async ValueTask<IEnumerable<IPEndPoint>> GetAddressesAsync(
addrs = addrs.Shuffle();
}

if (ipVersion == EnableBoth)
{
if (preferIPv6)
{
return addrs.OrderByDescending(addr => addr.AddressFamily);
}
else
{
return addrs.OrderBy(addr => addr.AddressFamily);
}
}
return addrs;
}
catch (DNSException)
Expand All @@ -209,15 +195,14 @@ internal static async ValueTask<IEnumerable<IPEndPoint>> GetAddressesForClientEn
int port,
int ipVersion,
EndpointSelectionType selType,
bool preferIPv6,
CancellationToken cancel)
{
Debug.Assert(host.Length > 0);

return await GetAddressesAsync(host, port, ipVersion, selType, preferIPv6, cancel).ConfigureAwait(false);
return await GetAddressesAsync(host, port, ipVersion, selType, cancel).ConfigureAwait(false);
}

internal static IPEndPoint GetAddressForServerEndpoint(string host, int port, int ipVersion, bool preferIPv6)
internal static IPEndPoint GetAddressForServerEndpoint(string host, int port, int ipVersion)
{
// TODO: Fix this method to be asynchronous.

Expand All @@ -229,8 +214,7 @@ internal static IPEndPoint GetAddressForServerEndpoint(string host, int port, in
ValueTask<IEnumerable<IPEndPoint>> task = GetAddressesAsync(host,
port,
ipVersion,
EndpointSelectionType.Ordered,
preferIPv6);
EndpointSelectionType.Ordered);
return (task.IsCompleted ? task.Result : task.AsTask().Result).First();
}
catch (AggregateException ex)
Expand Down Expand Up @@ -264,17 +248,16 @@ internal static List<string> GetHostsForEndpointExpand(string host, int ipVersio
return hosts;
}

internal static List<string> GetInterfacesForMulticast(string intf, int ipVersion)
internal static List<string> GetInterfacesForMulticast(string? intf, int ipVersion)
{
var interfaces = new List<string>();
if (IsWildcard(intf, ipVersion))

if (intf == null || IsWildcard(intf, ipVersion))
{
foreach (IPAddress a in GetLocalAddresses(ipVersion, true, true))
{
interfaces.Add(a.ToString());
}
interfaces.AddRange(GetLocalAddresses(ipVersion, true, true).Select(i => i.ToString()));
}
if (interfaces.Count == 0)

if (intf != null && interfaces.Count == 0)
{
interfaces.Add(intf);
}
Expand Down Expand Up @@ -334,11 +317,11 @@ internal static IPAddress[] GetLocalAddresses(int ipVersion, bool includeLoopbac
{
goto repeatGetHostByName;
}
throw new DNSException("0.0.0.0", ex);
throw new TransportException("error retrieving local network interface IP addresses", ex);
}
catch (Exception ex)
{
throw new DNSException("0.0.0.0", ex);
throw new TransportException("error retrieving local network interface IP addresses", ex);
}

return addresses.ToArray();
Expand Down Expand Up @@ -435,7 +418,7 @@ internal static void SetBufSize(Socket socket, Communicator communicator, Transp
}
}

internal static void SetMulticastGroup(Socket s, IPAddress group, string iface)
internal static void SetMulticastGroup(Socket s, IPAddress group, string? iface)
{
var indexes = new HashSet<int>();
foreach (string intf in GetInterfacesForMulticast(iface, GetIPVersion(group)))
Expand Down Expand Up @@ -546,10 +529,7 @@ internal static string SocketToString(Socket? socket)

private static IPAddress? GetInterfaceAddress(string iface, AddressFamily family)
{
if (iface.Length == 0)
{
return null;
}
Debug.Assert(iface.Length > 0);

// The iface parameter must either be an IP address, an index or the name of an interface. If it's an index
// we just return it. If it's an IP addess we search for an interface which has this IP address. If it's a
Expand Down
3 changes: 1 addition & 2 deletions csharp/src/Ice/TcpAcceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ internal TcpAcceptor(TcpEndpoint endpoint, ObjectAdapter adapter)

_addr = Network.GetAddressForServerEndpoint(endpoint.Host,
endpoint.Port,
Network.EnableBoth,
endpoint.Communicator.PreferIPv6);
Network.EnableBoth);

_socket = Network.CreateServerSocket(endpoint, _addr.AddressFamily);

Expand Down
2 changes: 1 addition & 1 deletion csharp/src/Ice/UdpConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override bool Equals(object? obj)

if (obj is UdpConnector udpConnector)
{
if (!_endpoint.MulticastInterface.Equals(udpConnector._endpoint.MulticastInterface))
if (_endpoint.MulticastInterface != udpConnector._endpoint.MulticastInterface)
{
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions csharp/src/Ice/UdpEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal sealed class UdpEndpoint : IPEndpoint
public override Transport Transport => Transport.UDP;

/// <summary>The local network interface used to send multicast datagrams.</summary>
internal string MulticastInterface { get; } = "";
internal string? MulticastInterface { get; }

/// <summary>The time-to-live of the multicast datagrams, in hops.</summary>
internal int MulticastTtl { get; } = -1;
Expand Down Expand Up @@ -78,8 +78,9 @@ protected internal override void AppendOptions(StringBuilder sb, char optionSepa

base.AppendOptions(sb, optionSeparator);

if (MulticastInterface.Length > 0)
if (MulticastInterface != null)
{
Debug.Assert(MulticastInterface.Length > 0);
bool addQuote = MulticastInterface.IndexOf(':') != -1;
sb.Append(" --interface ");
if (addQuote)
Expand Down Expand Up @@ -205,7 +206,7 @@ internal UdpEndpoint(
{
if (oaEndpoint)
{
MulticastInterface = "";
MulticastInterface = null;
}
else
{
Expand Down
Loading

0 comments on commit 3742d03

Please sign in to comment.