2dust/v2rayN · error · NotSupportedException

SOCKS5 address type {AddressType} not supported.

Error message

SOCKS5 address type {AddressType} not supported.

What it means

Thrown on the serialization side (Socks5AddressData.ToBytes) when AddressType is not one of the handled types (IPv4, IPv6, domain). Unlike the parse-side errors, this fires while building an outgoing SOCKS5 frame, before any network I/O.

Source

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

                        ms.Write(domainBytes);
                    }

                    break;

                case AddrTypeIPv6:
                    if (IPAddress.TryParse(Host, out var ip6) && ip6.AddressFamily == AddressFamily.InterNetworkV6)
                    {
                        ms.Write(ip6.GetAddressBytes(), 0, 16);
                    }
                    else
                    {
                        ms.Write(new byte[16]);
                    }

                    break;

                default:
                    throw new NotSupportedException($"SOCKS5 address type {AddressType} not supported.");
            }

            var portBytes = new byte[2];
            BinaryPrimitives.WriteUInt16BigEndian(portBytes, Port);
            ms.Write(portBytes);
            return ms.ToArray();
        }

        public static async Task<Socks5AddressData?> ParseAsync(Stream stream, CancellationToken ct)
        {
            var addr = new Socks5AddressData();
            var typeByte = new byte[1];
            try
            {
                if (await stream.ReadAsync(typeByte.AsMemory(0, 1), ct).ConfigureAwait(false) < 1)
                {
                    return null;
                }

View on GitHub (pinned to e01717d832)

Solutions

  1. Ensure AddressType is always one of AddrTypeIPv4, AddrTypeIPv6, or AddrTypeDomain before calling ToBytes().
  2. Add a unit test that serializes every defined AddressType value to catch missing switch arms at build time.
  3. Make AddressType a constrained enum so only handled values are assignable.
  4. Throw an ArgumentException with the offending value at construction/set time rather than deep inside serialization.

Example fix

// before - default throws deep in ToBytes if a caller sets an unknown type
var addr = new Socks5AddressData { AddressType = 0x09, Host = "x", Port = 1 };
var bytes = addr.ToBytes(); // NotSupportedException

// after - validate at construction
public byte AddressType
{
    get => _addressType;
    set => _addressType = value is AddrTypeIPv4 or AddrTypeIPv6 or AddrTypeDomain
        ? value
        : throw new ArgumentOutOfRangeException(nameof(value));
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate AddressType before serializing
static bool IsSupportedAtyp(byte t) => t is Socks5AddressData.AddrTypeIPv4 or Socks5AddressData.AddrTypeIPv6 or Socks5AddressData.AddrTypeDomain;
// usage:
if (!IsSupportedAtyp(addr.AddressType)) throw new ArgumentOutOfRangeException(nameof(addr.AddressType));

Type guard

static bool IsSerializableAddressType(byte addressType) => addressType is Socks5AddressData.AddrTypeIPv4 or Socks5AddressData.AddrTypeIPv6 or Socks5AddressData.AddrTypeDomain;

Try / catch

try
{
    var bytes = addr.ToBytes();
}
catch (NotSupportedException ex) when (ex.Message.Contains("address type"))
{
    // caller set an AddressType with no serializer; fix the caller
    throw new InvalidOperationException("Socks5AddressData.AddressType must be IPv4, IPv6, or domain.", ex);
}

Prevention

When it happens

Trigger: Code constructs a Socks5AddressData with an AddressType value not covered by the ToBytes switch, then calls ToBytes(); the default branch aborts serialization. In the channel it is exercised by EstablishUdpAssociationAsync, which always sets AddrTypeIPv4, so a live trigger implies a caller set an invalid AddressType.

Common situations: Programming error: caller assigns an out-of-range or future enum value to AddressType; a newly added address type whose serialization was not implemented; corrupt/incomplete object initialization.

Related errors


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