Specifying a CIDR mask selects network interfaces that are down
Specifying a CIDR mask selects any interface with that IP, regardless of the status.
Looking at the function in Networking.cs responsible for selecting the network interface by CIDR mask, it doesn't have a check for the interface status:
public static NetworkInterface GetNetworkInterfaceByCIDR(string mask)
{
if(mask.Contains('/'))
{
IPNetwork ipnetwork = IPNetwork.Parse(mask);
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
var unicastAddresses = nic.GetIPProperties().UnicastAddresses;
if(unicastAddresses.Any(ua => ipnetwork.Contains(ua.Address)))
{
return nic;
}
}
}
else
{
IPAddress address = IPAddress.Parse(mask);
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
var unicastAddresses = nic.GetIPProperties().UnicastAddresses;
if(unicastAddresses.Select(ua => ua.Address).Contains(address))
{
return nic;
}
}
}
throw new NetworkInterfaceNotFoundException($"No network interface found for IP address/CIDR Mask {mask}");
}
Running with a CIDR mask of 192.168.0.0/16 results in the following:
On my Windows machine, it selects the "Microsoft Virtual WiFi Miniport Adapter" which is not up and has two Unicast IPs associated with it: 169.254.175.152 and 192.168.137.1 (which are interestingly not shown when running ipconfig /all or in the network properties).
The two IP addresses are then bound to the GRPC server and it fails when Start() is called with the following exception:
Server creation/initialization exception: System.IO.IOException: Failed to bind port at "169.254.175.152:50053"
at Grpc.Core.Server.CheckPortsBoundSuccessfully()
at Grpc.Core.Server.Start()
at sila2.SiLA2Server.StartServer()
The same thing can be reproduced by having a static IP set for your unconnected LAN interface that matches the CIDR mask.
It seems that this can be fixed by adding a check for OperationalStatus.Up without any downfalls - if someone can think of any, please comment.