{"record":{"id":"499708a8d38cb2e3","repo":"2dust/v2rayN","slug":"invalid-socks5-udp-packet-too-short","errorCode":null,"errorMessage":"Invalid SOCKS5 UDP packet: too short","messagePattern":"Invalid SOCKS5 UDP packet: too short","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs","lineNumber":104,"sourceCode":"        // RSV (2 bytes) + FRAG (1 byte) - Reserved and Fragment fields\n        ms.WriteByte(0x00);\n        ms.WriteByte(0x00);\n        ms.WriteByte(0x00);\n\n        // Write address (ATYP + address + port)\n        ms.Write(addressData.ToBytes());\n\n        // User data payload\n        ms.Write(data);\n\n        return ms.ToArray();\n    }\n\n    private static (Socks5RemoteEndpoint Remote, byte[] Data) ParseSocks5UdpPacket(byte[] packet)\n    {\n        if (packet.Length < 10) // Minimum length: RSV(2) + FRAG(1) + ATYP(1) + IPv4(4) + Port(2) = 10\n        {\n            throw new ArgumentException(\"Invalid SOCKS5 UDP packet: too short\");\n        }\n\n        var offset = 0;\n\n        // RSV (2 bytes) - Reserved field, skip\n        offset += 2;\n\n        // FRAG (1 byte) - Fragment number, currently only support 0 (no fragmentation)\n        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;","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/2dust/v2rayN/blob/e01717d8326a4f5060b335523590c5fda943fe03/v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs#L86-L122","documentation":"Thrown by ParseSocks5UdpPacket when an incoming SOCKS5 UDP relay packet is under 10 bytes, the minimum legal size for a UDP ASSOCIATE payload (RSV 2 + FRAG 1 + ATYP 1 + IPv4 4 + Port 2). The parser cannot even locate the address-type field, so it refuses to read further. This signals a truncated, corrupt, or non-SOCKS5 datagram arrived on the UDP relay channel.","triggerScenarios":"channel.ReceiveAsync() returns a datagram whose total length is < 10 bytes; typically a truncated relay response, a stray packet from another source hitting the bound UDP port, or a proxy that omitted the SOCKS5 UDP header.","commonSituations":"The SOCKS5 proxy sent a raw (non-wrapped) UDP datagram without the 10-byte SOCKS5 header; the bound local UDP port received unrelated traffic; packet corruption over an unreliable relay; wrong proxy port (connected to a plain UDP forwarder rather than a SOCKS5 UDP ASSOCIATE relay).","solutions":["Confirm the SOCKS5 server actually supports UDP ASSOCIATE (command 0x03) and wraps replies with the RSV/FRAG/ATYP header.","Verify the relay endpoint and port returned by the handshake (EstablishUdpAssociationAsync._relayEndPoint) are correct and that traffic on the local UDP socket is not mixed with unrelated datagrams.","Wrap the ReceiveAsync/parse call in a try-catch for ArgumentException and treat a too-short packet as a failed attempt rather than a fatal error.","Log the raw packet length and bytes to distinguish proxy misbehavior from local noise."],"exampleFix":"// before\nvar (_, receiveResult) = await channel.ReceiveAsync(cancellationToken).ConfigureAwait(false);\n// ...later ParseSocks5UdpPacket throws on a <10 byte packet\n\n// after - validate length before handing to the parser, or catch the malformed-packet family\nvar (_, receiveResult) = await channel.ReceiveAsync(cancellationToken).ConfigureAwait(false);\nif (receiveResult == null || receiveResult.Length < 10)\n{\n    throw new Exception(\"SOCKS5 proxy returned a malformed/truncated UDP packet\");\n}","handlingStrategy":"try-catch","validationCode":"// Validate relay packet shape before parsing\nstatic bool LooksLikeSocks5Udp(byte[] p) => p != null && p.Length >= 10 && p[0] == 0x00 && p[1] == 0x00;\n// usage:\nif (!LooksLikeSocks5Udp(packet)) return; // skip stray/truncated datagrams","typeGuard":"static bool IsPlausibleSocks5UdpPacket(byte[] packet) => packet != null && packet.Length >= 10 && packet[0] == 0x00 && packet[1] == 0x00 && packet[2] == 0x00;","tryCatchPattern":"try\n{\n    var (remote, data) = ParseSocks5UdpPacket(packet);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"too short\"))\n{\n    // malformed/truncated relay packet; treat as a failed attempt, do not abort the test\n    _log?.Warn($\"Dropped short SOCKS5 UDP packet (len={packet?.Length}).\");\n}","preventionTips":["Bind the local UDP socket to an ephemeral high port to avoid receiving unrelated traffic.","Verify the SOCKS5 server supports UDP ASSOCIATE before relying on it.","Treat any parse ArgumentException as a per-attempt failure, not a fatal error."],"tags":["socks5","udp","packet-parsing","network"],"backgroundTag":null,"analyzedSha":"e01717d8326a4f5060b335523590c5fda943fe03","analyzedAt":"2026-08-13T10:10:23.416Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}