Skip to content

Commit

Permalink
Support for Macos (#7)
Browse files Browse the repository at this point in the history
* Fix for network interface 'utun0' on MacOS

On MacOS an "Unknown" network interface gives us problems.
Let's avoid it all together.

* Handle unavailable network interface mtu

On MacOS the "Mtu" seems not to be available (it is zero).
So we ignore it and keep the maxPackageSize as is.
  • Loading branch information
MarLoe authored and richardschneider committed Jun 29, 2018
1 parent 2d118f1 commit 79ed572
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/MulticastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ public MulticastService()
/// <seealso cref="NetworkInterfaceDiscovered"/>
TimeSpan NetworkInterfaceDiscoveryInterval { get; set; } = TimeSpan.FromMinutes(2);


/// <summary>
/// Get the network interfaces that are useable to us
/// </summary>
/// <returns>
/// An enumerable of <see cref="NetworkInterface"/>.
/// </returns>
protected static IEnumerable<NetworkInterface> GetNetworkInterfaces()
{
return NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback && nic.NetworkInterfaceType != NetworkInterfaceType.Unknown)
.Where(nic => nic.SupportsMulticast);
}


/// <summary>
/// Get the IP addresses of the local machine.
/// </summary>
Expand All @@ -132,10 +148,7 @@ public MulticastService()
/// </returns>
public static IEnumerable<IPAddress> GetIPAddresses()
{
return NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Where(nic => nic.SupportsMulticast)
return GetNetworkInterfaces()
.SelectMany(nic => nic.GetIPProperties().UnicastAddresses)
.Select(u => u.Address);
}
Expand Down Expand Up @@ -186,10 +199,7 @@ async void PollNetworkInterfaces()

void FindNetworkInterfaces()
{
var nics = NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up)
.Where(nic => nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Where(nic => nic.SupportsMulticast)
var nics = GetNetworkInterfaces()
.Where(nic => !knownNics.Any(k => k.Id == nic.Id))
.ToArray();
foreach (var nic in nics)
Expand All @@ -202,23 +212,33 @@ void FindNetworkInterfaces()
IPInterfaceProperties properties = nic.GetIPProperties();
if (ip6)
{
var interfaceIndex = properties.GetIPv6Properties().Index;
var ipProperties = properties.GetIPv6Properties();
var interfaceIndex = ipProperties.Index;
var mopt = new IPv6MulticastOption(MulticastAddressIp6, interfaceIndex);
socket.SetSocketOption(
SocketOptionLevel.IPv6,
SocketOptionName.AddMembership,
mopt);
maxPacketSize = Math.Min(maxPacketSize, properties.GetIPv6Properties().Mtu - packetOverhead);
if (ipProperties.Mtu > packetOverhead)
{
// Only change maxPacketSize if Mtu is available (and it that is not the case on MacOS)
maxPacketSize = Math.Min(maxPacketSize, ipProperties.Mtu - packetOverhead);
}
}
else
{
var interfaceIndex = properties.GetIPv4Properties().Index;
var ipProperties = properties.GetIPv4Properties();
var interfaceIndex = ipProperties.Index;
var mopt = new MulticastOption(MulticastAddressIp4, interfaceIndex);
socket.SetSocketOption(
SocketOptionLevel.IP,
SocketOptionName.AddMembership,
mopt);
maxPacketSize = Math.Min(maxPacketSize, properties.GetIPv4Properties().Mtu - packetOverhead);
if (ipProperties.Mtu > packetOverhead)
{
// Only change maxPacketSize if Mtu is available (and it that is not the case on MacOS)
maxPacketSize = Math.Min(maxPacketSize, ipProperties.Mtu - packetOverhead);
}
}
knownNics.Add(nic);
}
Expand Down

0 comments on commit 79ed572

Please sign in to comment.