{"record":{"id":"dd8d9aca72918ed4","repo":"2dust/v2rayN","slug":"invalid-socks5-udp-packet-port-incomplete","errorCode":null,"errorMessage":"Invalid SOCKS5 UDP packet: port incomplete","messagePattern":"Invalid SOCKS5 UDP packet: port incomplete","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs","lineNumber":180,"sourceCode":"                {\n                    throw new ArgumentException(\"Invalid SOCKS5 UDP packet: domain incomplete\");\n                }\n\n                host = Encoding.ASCII.GetString(packet, offset, domainLength);\n                addressLength = domainLength;\n                isDomain = true;\n                break;\n\n            default:\n                throw new NotSupportedException($\"Unsupported SOCKS5 address type: {addressType}\");\n        }\n\n        offset += addressLength;\n\n        // Port (2 bytes, big-endian)\n        if (packet.Length < offset + 2)\n        {\n            throw new ArgumentException(\"Invalid SOCKS5 UDP packet: port incomplete\");\n        }\n\n        var port = BinaryPrimitives.ReadUInt16BigEndian(packet.AsSpan(offset, 2));\n        offset += 2;\n\n        // Data (remaining bytes)\n        var dataLength = packet.Length - offset;\n        var data = new byte[dataLength];\n        if (dataLength > 0)\n        {\n            Array.Copy(packet, offset, data, 0, dataLength);\n        }\n\n        // Create remote endpoint without DNS resolution\n        var remote = new Socks5RemoteEndpoint(host, port, isDomain);\n        return (remote, data);\n    }\n","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/2dust/v2rayN/blob/e01717d8326a4f5060b335523590c5fda943fe03/v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs#L162-L198","documentation":"Thrown when fewer than 2 bytes remain after the address for the big-endian port field. The parser needs exactly 2 bytes for the port; a short datagram here means the relay packet was truncated at the very end.","triggerScenarios":"ParseSocks5UdpPacket has consumed RSV/FRAG/ATYP/address, then packet.Length < offset + 2 before reading the port.","commonSituations":"Datagram truncated just before the port field; proxy that omits the 2-byte port; earlier address-length misparse leaving the offset wrong so the port read overruns the buffer.","solutions":["Catch ArgumentException per attempt and continue.","Confirm the proxy always appends a 2-byte big-endian port after the address per RFC 1928.","Re-validate the address-length math (especially for domain type) to ensure offset is correct before the port read.","Log offset and total length at failure to locate the truncation."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check port field availability\nstatic bool HasPortBytes(byte[] p, int offset) => p != null && p.Length >= offset + 2;","typeGuard":"static bool HasCompletePort(byte[] packet, int offset) => packet != null && offset + 2 <= packet.Length;","tryCatchPattern":"try\n{\n    var (remote, data) = ParseSocks5UdpPacket(packet);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"port incomplete\"))\n{\n    // truncated at the port field; mark attempt failed\n}","preventionTips":["Catch the ArgumentException per attempt.","Confirm the proxy always appends a 2-byte big-endian port after the address.","Re-validate address-length math for domain-type packets to keep offset correct."],"tags":["socks5","udp","packet-parsing","port","network"],"backgroundTag":null,"analyzedSha":"e01717d8326a4f5060b335523590c5fda943fe03","analyzedAt":"2026-08-13T10:10:23.416Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}