2dust/v2rayN · error · ArgumentException

Invalid SOCKS5 UDP packet: too short

Error message

Invalid SOCKS5 UDP packet: too short

What it means

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.

Source

Thrown at v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs:104

        // RSV (2 bytes) + FRAG (1 byte) - Reserved and Fragment fields
        ms.WriteByte(0x00);
        ms.WriteByte(0x00);
        ms.WriteByte(0x00);

        // Write address (ATYP + address + port)
        ms.Write(addressData.ToBytes());

        // User data payload
        ms.Write(data);

        return ms.ToArray();
    }

    private static (Socks5RemoteEndpoint Remote, byte[] Data) ParseSocks5UdpPacket(byte[] packet)
    {
        if (packet.Length < 10) // Minimum length: RSV(2) + FRAG(1) + ATYP(1) + IPv4(4) + Port(2) = 10
        {
            throw new ArgumentException("Invalid SOCKS5 UDP packet: too short");
        }

        var offset = 0;

        // RSV (2 bytes) - Reserved field, skip
        offset += 2;

        // FRAG (1 byte) - Fragment number, currently only support 0 (no fragmentation)
        var frag = packet[offset++];
        if (frag != 0x00)
        {
            throw new NotSupportedException("SOCKS5 UDP fragmentation is not supported");
        }

        // ATYP (1 byte) - Address type
        var addressType = packet[offset++];

        string host;

View on GitHub (pinned to e01717d832)

Solutions

  1. Confirm the SOCKS5 server actually supports UDP ASSOCIATE (command 0x03) and wraps replies with the RSV/FRAG/ATYP header.
  2. 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.
  3. 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.
  4. Log the raw packet length and bytes to distinguish proxy misbehavior from local noise.

Example fix

// before
var (_, receiveResult) = await channel.ReceiveAsync(cancellationToken).ConfigureAwait(false);
// ...later ParseSocks5UdpPacket throws on a <10 byte packet

// after - validate length before handing to the parser, or catch the malformed-packet family
var (_, receiveResult) = await channel.ReceiveAsync(cancellationToken).ConfigureAwait(false);
if (receiveResult == null || receiveResult.Length < 10)
{
    throw new Exception("SOCKS5 proxy returned a malformed/truncated UDP packet");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate relay packet shape before parsing
static bool LooksLikeSocks5Udp(byte[] p) => p != null && p.Length >= 10 && p[0] == 0x00 && p[1] == 0x00;
// usage:
if (!LooksLikeSocks5Udp(packet)) return; // skip stray/truncated datagrams

Type guard

static bool IsPlausibleSocks5UdpPacket(byte[] packet) => packet != null && packet.Length >= 10 && packet[0] == 0x00 && packet[1] == 0x00 && packet[2] == 0x00;

Try / catch

try
{
    var (remote, data) = ParseSocks5UdpPacket(packet);
}
catch (ArgumentException ex) when (ex.Message.Contains("too short"))
{
    // malformed/truncated relay packet; treat as a failed attempt, do not abort the test
    _log?.Warn($"Dropped short SOCKS5 UDP packet (len={packet?.Length}).");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of 2dust/v2rayN@e01717d832 (2026-08-13). Data as JSON: /api/errors/499708a8d38cb2e3. Report an issue: GitHub.