netchx/netch · error · MessageException

GetBestRoute 搜索失败

Error message

GetBestRoute 搜索失败

What it means

Identical mechanism to error [21] but in NetworkInterfaceUtils.GetBest, used to pick the best IPv4 NetworkInterface. It probes GetBestRoute to 114.114.114.114; a non-zero return means no IPv4 route exists, so it throws. Note the surrounding switch: AddressFamily.InterNetworkV6 throws NotImplementedException (not implemented), an unknown family throws InvalidOperationException, and only the IPv4 path reaches this GetBestRoute check. On success it returns the interface whose index matches route.dwForwardIfIndex.

Source

Thrown at Netch/Utils/NetworkInterfaceUtils.cs:28

public static class NetworkInterfaceUtils
{
    public static NetworkInterface GetBest(AddressFamily addressFamily = AddressFamily.InterNetwork)
    {
        string ipAddress;
        switch (addressFamily)
        {
            case AddressFamily.InterNetwork:
                ipAddress = "114.114.114.114";
                break;
            case AddressFamily.InterNetworkV6:
                throw new NotImplementedException();
            default:
                throw new InvalidOperationException();
        }

        if (PInvoke.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0), 0, out var route) != 0)
            throw new MessageException("GetBestRoute 搜索失败");

        return Get((int)route.dwForwardIfIndex);
    }

    public static NetworkInterface Get(int interfaceIndex)
    {
        return NetworkInterface.GetAllNetworkInterfaces().First(n => n.GetIndex() == interfaceIndex);
    }

    public static NetworkInterface Get(Func<NetworkInterface, bool> expression)
    {
        return NetworkInterface.GetAllNetworkInterfaces().First(expression);
    }

    public static void SetInterfaceMetric(int interfaceIndex, int? metric = null)
    {
        var arguments = $"interface ip set interface {interfaceIndex} ";
        if (metric != null)

View on GitHub (pinned to 9d99eb1c5a)

Solutions

  1. Bring up an IPv4 network interface with a default gateway and retry.
  2. Check for a default route: `Get-NetRoute -DestinationPrefix 0.0.0.0/0`.
  3. Do not pass AddressFamily.InterNetworkV6 (IPv6 selection is not implemented).
  4. Restore any default route that a VPN client removed.

Example fix

// before
if (PInvoke.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0), 0, out var route) != 0)
    throw new MessageException("GetBestRoute 搜索失败");
// after - report the destination and cause
if (PInvoke.GetBestRoute(BitConverter.ToUInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0), 0, out var route) != 0)
    throw new MessageException($"No IPv4 route to {ipAddress}. Connect to a network.");
Defensive patterns

Strategy: validation

Validate before calling

// Ensure an Up IPv4 interface with a gateway exists
var hasIpv4 = NetworkInterface.GetAllNetworkInterfaces()
    .Any(n => n.OperationalStatus == OperationalStatus.Up
              && n.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily == AddressFamily.InterNetwork));
if (!hasIpv4)
    throw new MessageException("No active IPv4 interface; cannot select best route.");

Type guard

bool HasIpv4Route() => NetworkInterface.GetAllNetworkInterfaces()
    .Any(n => n.OperationalStatus == OperationalStatus.Up
              && n.GetIPProperties().GatewayAddresses.Any(g => g.Address.AddressFamily == AddressFamily.InterNetwork));

Try / catch

try { var ni = NetworkInterfaceUtils.GetBest(); }
catch (MessageException ex) when (ex.Message.Contains("GetBestRoute"))
{
    Global.MainForm.NotifyTip("No IPv4 network available. Connect and retry.");
    return;
}

Prevention

When it happens

Trigger: Calling GetBest() with no IPv4 default route: all NICs down/disabled, airplane mode, immediately after disabling the main NIC. Passing AddressFamily.InterNetworkV6 throws NotImplementedException instead, so it never reaches this check for IPv6.

Common situations: Selecting the outbound interface at startup with no network; a TUN/VPN having cleared routes; a machine with only IPv6 connectivity; calling GetBest before any interface is up.

Related errors


AI-assisted analysis of netchx/netch@9d99eb1c5a (2026-08-13). Data as JSON: /api/errors/821e48ac7bbe52df. Report an issue: GitHub.