{"record":{"id":"41e8bcab489ee25c","repo":"2dust/v2rayN","slug":"invalid-socks5-udp-packet-ipv4-address-incomplete","errorCode":null,"errorMessage":"Invalid SOCKS5 UDP packet: IPv4 address incomplete","messagePattern":"Invalid SOCKS5 UDP packet: IPv4 address incomplete","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs","lineNumber":131,"sourceCode":"        var frag = packet[offset++];\n        if (frag != 0x00)\n        {\n            throw new NotSupportedException(\"SOCKS5 UDP fragmentation is not supported\");\n        }\n\n        // ATYP (1 byte) - Address type\n        var addressType = packet[offset++];\n\n        string host;\n        int addressLength;\n        bool isDomain;\n\n        switch (addressType)\n        {\n            case Socks5AddressData.AddrTypeIPv4:\n                if (packet.Length < offset + 4)\n                {\n                    throw new ArgumentException(\"Invalid SOCKS5 UDP packet: IPv4 address incomplete\");\n                }\n\n                var ipv4Bytes = new byte[4];\n                Array.Copy(packet, offset, ipv4Bytes, 0, 4);\n                host = new IPAddress(ipv4Bytes).ToString();\n                addressLength = 4;\n                isDomain = false;\n                break;\n\n            case Socks5AddressData.AddrTypeIPv6:\n                if (packet.Length < offset + 16)\n                {\n                    throw new ArgumentException(\"Invalid SOCKS5 UDP packet: IPv6 address incomplete\");\n                }\n\n                var ipv6Bytes = new byte[16];\n                Array.Copy(packet, offset, ipv6Bytes, 0, 16);\n                host = new IPAddress(ipv6Bytes).ToString();","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/2dust/v2rayN/blob/e01717d8326a4f5060b335523590c5fda943fe03/v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs#L113-L149","documentation":"Thrown when ATYP indicates IPv4 (0x01) but fewer than 4 bytes remain in the packet for the address. The parser needs exactly 4 octets for an IPv4 address and refuses to read past the buffer.","triggerScenarios":"ParseSocks5UdpPacket reads ATYP == AddrTypeIPv4 then finds packet.Length < offset + 4; i.e. the header claimed IPv4 but the datagram was truncated inside the address field.","commonSituations":"Corrupted/truncated relay packet; proxy that mis-encodes the address length; packet loss on an unreliable relay leaving a short datagram.","solutions":["Treat as a failed attempt: catch ArgumentException in the per-attempt loop and continue rather than aborting the whole test.","Verify the SOCKS5 server builds UDP headers per RFC 1928 (ATYP 0x01 followed by exactly 4 IPv4 octets).","Log offset and total length at failure to confirm the truncation point.","Test against a known-good SOCKS5 server to isolate whether the proxy or the target is the source of malformed packets."],"exampleFix":"// before - parsing throws ArgumentException mid-test, aborting\nvar (remote, receiveResult) = ParseSocks5UdpPacket(packet);\n\n// after - guard the parse per attempt\ntry\n{\n    var (remote, receiveResult) = ParseSocks5UdpPacket(packet);\n    udpReceiveResult = receiveResult;\n}\ncatch (ArgumentException)\n{\n    // malformed packet from proxy; counts as a failed attempt\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check IPv4 address availability\nstatic bool HasFullIPv4(byte[] p, int offset) => p != null && p.Length >= offset + 4;\n// combined with ATYP==0x01 at offset 3","typeGuard":"static bool IsCompleteIPv4Packet(byte[] packet, int offset) => packet != null && offset + 4 + 2 <= packet.Length;","tryCatchPattern":"try\n{\n    var (remote, data) = ParseSocks5UdpPacket(packet);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"IPv4 address incomplete\"))\n{\n    // truncated relay packet; retry or mark attempt failed\n}","preventionTips":["Catch ArgumentException in the per-attempt loop so a truncated packet does not abort the whole test.","Confirm the proxy encodes IPv4 with ATYP 0x01 + 4 octets per RFC 1928.","Log offset/length on failure to confirm truncation vs proxy mis-encoding."],"tags":["socks5","udp","packet-parsing","ipv4","network"],"backgroundTag":null,"analyzedSha":"e01717d8326a4f5060b335523590c5fda943fe03","analyzedAt":"2026-08-13T10:10:23.416Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}