netchx/netch · error · ArgumentException

DNS format invalid

Error message

DNS format invalid

What it means

SetDns validates the primary DNS via a local VerifyDns that trims and rejects null/empty/whitespace by throwing ArgumentException('DNS format invalid'). Important bug: the secondDns branch on line 82 calls VerifyDns(ref primaryDns) again instead of verifying secondDns, so an invalid secondary DNS is never caught here and only the primary is ever validated. Also, VerifyDns does not actually parse the IP format - it checks non-emptiness only.

Source

Thrown at Netch/Utils/NetworkInterfaceUtils.cs:77

    public static int GetIndex(this NetworkInterface ni)
    {
        var ipProperties = ni.GetIPProperties();
        if (ni.Supports(NetworkInterfaceComponent.IPv4))
            return ipProperties.GetIPv4Properties().Index;

        if (ni.Supports(NetworkInterfaceComponent.IPv6))
            return ipProperties.GetIPv6Properties().Index;

        throw new Exception();
    }

    public static void SetDns(this NetworkInterface ni, string primaryDns, string? secondDns = null)
    {
        void VerifyDns(ref string s)
        {
            s = s.Trim();
            if (primaryDns.IsNullOrEmpty())
                throw new ArgumentException("DNS format invalid", nameof(primaryDns));
        }

        VerifyDns(ref primaryDns);
        if (secondDns != null)
            VerifyDns(ref primaryDns);

        var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
        var mos = wmi.GetInstances().Cast<ManagementObject>();

        var mo = mos.First(m => m["Description"].ToString() == ni.Description);

        var dns = new[] { primaryDns };
        if (secondDns != null)
            dns = dns.Append(secondDns).ToArray();

        var inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
        inPar["DNSServerSearchOrder"] = dns;

View on GitHub (pinned to 9d99eb1c5a)

Solutions

  1. Pass a valid DNS IP string (e.g. '8.8.8.8') or skip the SetDns call entirely when blank.
  2. Fix the bug: change the secondDns branch to VerifyDns(ref secondDns) and make VerifyDns validate with IPAddress.Parse/TryParse.
  3. Validate the IP format with IPAddress.TryParse rather than only checking emptiness.
  4. Skip (no-op) when the value is blank instead of throwing, so the existing DNS is preserved.

Example fix

// before
void VerifyDns(ref string s)
{
    s = s.Trim();
    if (primaryDns.IsNullOrEmpty())
        throw new ArgumentException("DNS format invalid", nameof(primaryDns));
}
VerifyDns(ref primaryDns);
if (secondDns != null)
    VerifyDns(ref primaryDns); // BUG: should verify secondDns
// after - verify each value and its IP format
void VerifyDns(ref string s, string name)
{
    if (string.IsNullOrWhiteSpace(s) || !IPAddress.TryParse(s.Trim(), out _))
        throw new ArgumentException("DNS format invalid", name);
    s = s.Trim();
}
VerifyDns(ref primaryDns, nameof(primaryDns));
if (secondDns != null)
    VerifyDns(ref secondDns, nameof(secondDns));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(primaryDns) || !IPAddress.TryParse(primaryDns.Trim(), out _))
    return; // skip setting DNS, or surface a UI error
ni.SetDns(primaryDns.Trim(), secondDns?.Trim());

Type guard

bool IsValidDns(string? s) => !string.IsNullOrWhiteSpace(s) && IPAddress.TryParse(s!.Trim(), out _);

Try / catch

try { ni.SetDns(primaryDns, secondDns); }
catch (ArgumentException ex) when (ex.Message.Contains("DNS format invalid"))
{
    Log.Warning("Invalid DNS config, skipping: {Primary}", primaryDns);
}

Prevention

When it happens

Trigger: Calling SetDns with primaryDns = null, empty, or whitespace. It does NOT fire for an invalid secondary DNS due to the copy-paste bug; it also does not fire for a syntactically invalid-but-non-empty IP string.

Common situations: User leaves the primary DNS field blank; a config migration nulls the DNS field; a code path setting DNS from an unset setting string.

Related errors


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