{"record":{"id":"c2f6e07e04e65c12","repo":"2dust/v2rayN","slug":"socks5-address-type-addresstype-not-supported","errorCode":null,"errorMessage":"SOCKS5 address type {AddressType} not supported.","messagePattern":"SOCKS5 address type (.+?) not supported\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs","lineNumber":331,"sourceCode":"                        ms.Write(domainBytes);\n                    }\n\n                    break;\n\n                case AddrTypeIPv6:\n                    if (IPAddress.TryParse(Host, out var ip6) && ip6.AddressFamily == AddressFamily.InterNetworkV6)\n                    {\n                        ms.Write(ip6.GetAddressBytes(), 0, 16);\n                    }\n                    else\n                    {\n                        ms.Write(new byte[16]);\n                    }\n\n                    break;\n\n                default:\n                    throw new NotSupportedException($\"SOCKS5 address type {AddressType} not supported.\");\n            }\n\n            var portBytes = new byte[2];\n            BinaryPrimitives.WriteUInt16BigEndian(portBytes, Port);\n            ms.Write(portBytes);\n            return ms.ToArray();\n        }\n\n        public static async Task<Socks5AddressData?> ParseAsync(Stream stream, CancellationToken ct)\n        {\n            var addr = new Socks5AddressData();\n            var typeByte = new byte[1];\n            try\n            {\n                if (await stream.ReadAsync(typeByte.AsMemory(0, 1), ct).ConfigureAwait(false) < 1)\n                {\n                    return null;\n                }","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/2dust/v2rayN/blob/e01717d8326a4f5060b335523590c5fda943fe03/v2rayN/ServiceLib.UdpTest/Socks5UdpChannel.cs#L313-L349","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure AddressType is always one of AddrTypeIPv4, AddrTypeIPv6, or AddrTypeDomain before calling ToBytes().","Add a unit test that serializes every defined AddressType value to catch missing switch arms at build time.","Make AddressType a constrained enum so only handled values are assignable.","Throw an ArgumentException with the offending value at construction/set time rather than deep inside serialization."],"exampleFix":"// before - default throws deep in ToBytes if a caller sets an unknown type\nvar addr = new Socks5AddressData { AddressType = 0x09, Host = \"x\", Port = 1 };\nvar bytes = addr.ToBytes(); // NotSupportedException\n\n// after - validate at construction\npublic byte AddressType\n{\n    get => _addressType;\n    set => _addressType = value is AddrTypeIPv4 or AddrTypeIPv6 or AddrTypeDomain\n        ? value\n        : throw new ArgumentOutOfRangeException(nameof(value));\n}","handlingStrategy":"validation","validationCode":"// Validate AddressType before serializing\nstatic bool IsSupportedAtyp(byte t) => t is Socks5AddressData.AddrTypeIPv4 or Socks5AddressData.AddrTypeIPv6 or Socks5AddressData.AddrTypeDomain;\n// usage:\nif (!IsSupportedAtyp(addr.AddressType)) throw new ArgumentOutOfRangeException(nameof(addr.AddressType));","typeGuard":"static bool IsSerializableAddressType(byte addressType) => addressType is Socks5AddressData.AddrTypeIPv4 or Socks5AddressData.AddrTypeIPv6 or Socks5AddressData.AddrTypeDomain;","tryCatchPattern":"try\n{\n    var bytes = addr.ToBytes();\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"address type\"))\n{\n    // caller set an AddressType with no serializer; fix the caller\n    throw new InvalidOperationException(\"Socks5AddressData.AddressType must be IPv4, IPv6, or domain.\", ex);\n}","preventionTips":["Constrain AddressType to the three handled values (enum or setter validation).","Add a unit test serializing every defined AddressType value.","Validate at construction time, not deep inside ToBytes."],"tags":["socks5","serialization","address-type","validation"],"backgroundTag":null,"analyzedSha":"e01717d8326a4f5060b335523590c5fda943fe03","analyzedAt":"2026-08-13T10:10:23.416Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}