{"record":{"id":"350411830879b5a9","repo":"mgth/LittleBigMouse","slug":"enter-a-valid-projector-ipv4-address-networkidentity","errorCode":null,"errorMessage":"Enter a valid projector IPv4 address.","messagePattern":"Enter a valid projector IPv4 address\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LittleBigMouse.Plugins/LittleBigMouse.Plugin.Vcp.Avalonia/HisenseVidaa/NetworkIdentity.cs","lineNumber":14,"sourceCode":"#nullable enable\nusing System.Net;\nusing System.Net.NetworkInformation;\nusing System.Net.Sockets;\nusing LittleBigMouse.Plugin.Vcp.Networking;\n\nnamespace LittleBigMouse.Plugin.Vcp.Avalonia.HisenseVidaa;\n\nstatic class NetworkIdentity\n{\n    public static string ControllerMacFor(string remoteAddress)\n    {\n        if (!Ipv4Address.TryParse(remoteAddress, out var remote))\n            throw new ArgumentException(\"Enter a valid projector IPv4 address.\", nameof(remoteAddress));\n\n        using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);\n        socket.Connect(new IPEndPoint(remote, HisenseVidaaProtocol.MqttPort));\n        var local = ((IPEndPoint)socket.LocalEndPoint!).Address;\n        foreach (var network in NetworkInterface.GetAllNetworkInterfaces())\n        foreach (var address in network.GetIPProperties().UnicastAddresses)\n            if (address.Address.Equals(local))\n                return HisenseVidaaProtocol.NormalizeMac(network.GetPhysicalAddress().ToString());\n\n        throw new InvalidOperationException($\"Could not find the network interface used to reach {remoteAddress}.\");\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/mgth/LittleBigMouse/blob/7a42f01d47d99d223b8ee33ba4019af82adf1c48/LittleBigMouse.Plugins/LittleBigMouse.Plugin.Vcp.Avalonia/HisenseVidaa/NetworkIdentity.cs#L1-L27","documentation":"NetworkIdentity.ControllerMacFor determines which local network interface (and thus which controller MAC) would be used to reach the projector. Before doing so it parses remoteAddress as an IPv4 address with Ipv4Address.TryParse; if parsing fails it throws this ArgumentException naming the remoteAddress parameter. It validates input format up front rather than failing later in socket setup.","triggerScenarios":"Calling ControllerMacFor with a remoteAddress string that is not a parseable IPv4 address: hostname instead of IP, IPv6 string, empty/null string, trailing whitespace or typo like '192.168.1.256'.","commonSituations":"User typed a hostname (e.g. 'projector.local') into the IP field; config value copied with whitespace or port appended ('192.168.1.50:36669'); localization or UI let a malformed address through; IPv6-only network where the projector address is an IPv6 literal.","solutions":["Pass a valid dotted-quad IPv4 string (e.g. \"192.168.1.50\") as remoteAddress.","Resolve any hostname to an IPv4 address before calling, using Dns.GetHostAddresses and picking the IPv4 entry.","Validate the IP field in the UI/config layer with IPAddress.TryParse before storing or passing it.","Trim and normalize user-entered addresses (remove port, whitespace) before calling the API."],"exampleFix":"// before\nvar mac = NetworkIdentity.ControllerMacFor(\"projector.local\");\n\n// after\nif (!IPAddress.TryParse(host, out var addr) || addr.AddressFamily != AddressFamily.InterNetwork)\n    throw new ArgumentException(\"Provide an IPv4 address for the projector.\");\nvar mac = NetworkIdentity.ControllerMacFor(addr.ToString());","handlingStrategy":"validation","validationCode":"if (!IPAddress.TryParse(remoteAddress, out var ip) || ip.AddressFamily != AddressFamily.InterNetwork)\n    throw new ArgumentException(\"Projector address must be a valid IPv4 address.\", nameof(remoteAddress));","typeGuard":"static bool IsIpv4(string? s) =>\n    IPAddress.TryParse(s, out var ip) && ip.AddressFamily == AddressFamily.InterNetwork;","tryCatchPattern":"try\n{\n    var mac = NetworkIdentity.ControllerMacFor(remoteAddress);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"remoteAddress\")\n{\n    // prompt user for a valid dotted-quad IPv4 address\n}","preventionTips":["Validate the IP field with IPAddress.TryParse in the UI before saving.","Resolve hostnames to IPv4 with DNS before calling network APIs.","Trim user input and reject values containing ':' or whitespace."],"tags":["csharp","validation","ipv4","argument"],"backgroundTag":"invalid-argument-format","analyzedSha":"7a42f01d47d99d223b8ee33ba4019af82adf1c48","analyzedAt":"2026-09-16T00:35:00.514Z","contentChangedAt":"2026-09-16T00:35:00.514Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}