egametang/ET · error · ArgumentException

Only IPv4 addresses are supported

Error message

Only IPv4 addresses are supported

What it means

Thrown by NetworkHelper.IPStringToInt when the parsed address is not IPv4 (i.e. its byte length is not 4, so it is IPv6 or an unexpected family). The helper packs an address into one 32-bit int, which only fits IPv4.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Helper/NetworkHelper.cs:79

			TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 0);
			try
			{
				tcpListener.Start();
				return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
			}
			finally
			{
				tcpListener.Stop();
			}
		}

		public static int IPStringToInt(string ipString)
		{
			IPAddress ipAddress = IPAddress.Parse(ipString);
			byte[] bytes = ipAddress.GetAddressBytes();
			if (bytes.Length != 4)
			{
				throw new ArgumentException("Only IPv4 addresses are supported");
			}
			return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
		}

		public static string IntToIPString(int ipInt)
		{
			byte[] bytes = new byte[4];
			bytes[0] = (byte)((ipInt >> 24) & 0xFF);
			bytes[1] = (byte)((ipInt >> 16) & 0xFF);
			bytes[2] = (byte)((ipInt >> 8) & 0xFF);
			bytes[3] = (byte)(ipInt & 0xFF);
			return new IPAddress(bytes).ToString();
		}
        
		public static void SetSioUdpConnReset(Socket socket)
		{
			if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
			{

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Use an IPv4 address string (e.g. '127.0.0.1', '10.0.0.1') for any value fed to IPStringToInt.
  2. If you must support IPv6, use IPAddress.GetAddressBytes/long directly instead of this 4-byte helper.
  3. Normalize loopback to '127.0.0.1' rather than '::1' before calling.

Example fix

// before
int ip = NetworkHelper.IPStringToInt("::1");

// after
int ip = NetworkHelper.IPStringToInt("127.0.0.1");
Defensive patterns

Strategy: validation

Validate before calling

var addr = IPAddress.Parse(ipString);
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
    int ip = NetworkHelper.IPStringToInt(ipString);
}

Type guard

static bool IsIPv4(string ip)
    => IPAddress.TryParse(ip, out var a)
       && a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;

Prevention

When it happens

Trigger: Passing an IPv6 literal (e.g. '::1', 'fe80::1') to IPStringToInt; an address resolved from a dual-stack socket that returned IPv6; config/environment giving an IPv6 address.

Common situations: Server configured with IPv6 addresses; modern OS preferring IPv6 for loopback ('::1'); parsing addresses from hosts that are v6-only.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/83bed3babf00e94d. Report an issue: GitHub.