-
Notifications
You must be signed in to change notification settings - Fork 8
Description
IPv4-mapped IPv6 addresses do not work properly with LiteNetLibTransport. An example of an address like this:
::ffff:192.168.1.6
Right now LiteNetLibTransport opens up two sockets, one for IPv4 and one for IPv6. An IPv4-mapped IPv6 address is treated as an IPv6 address, so it is routed to the IPv6 socket. Because an IPv4-mapped IPv6 address is fundamentally an IPv4 address, it requires an IPv4 socket. In C# this is handled through something called a dual-mode socket, which encapsulates both an IPv4 and IPv6 socket into one package. Non-dual mode IPv6 sockets (aka IPv6 only) sockets seem to be incapable of handling IPv4-mapped IPv6 addresses and will throw an exception if you try to send to an address like this. This makes perfect sense, since the program actually needs to use IPv4 under the hood.
If you try to use this type of address right now in LiteNetLib an exception is thrown:
[S]System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context.
at System.Net.Sockets.Socket.SendTo (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP) [0x00082] in <d2957de1c3fd4781a43d89572183136c>:0
at LiteNetLib.NetSocket.SendTo (System.Byte[] data, System.Int32 offset, System.Int32 size, System.Net.IPEndPoint remoteEndPoint, System.Net.Sockets.SocketError& errorCode) [0x0003e] in D:\Caleb\holosproject\holos-desktop\Holos\Assets\LiteNetLibTransport\LiteNetLib\NetSocket.cs:427
UnityEngine.Debug:Log(Object)
LiteNetLib.NetDebug:WriteLogic(NetLogLevel, String, Object[]) (at Assets/LiteNetLibTransport/LiteNetLib/NetDebug.cs:51)
LiteNetLib.NetDebug:WriteError(String, Object[]) (at Assets/LiteNetLibTransport/LiteNetLib/NetDebug.cs:89)
LiteNetLib.NetSocket:SendTo(Byte[], Int32, Int32, IPEndPoint, SocketError&) (at Assets/LiteNetLibTransport/LiteNetLib/NetSocket.cs:441)
LiteNetLib.NetManager:SendRaw(Byte[], Int32, Int32, IPEndPoint) (at Assets/LiteNetLibTransport/LiteNetLib/NetManager.cs:469)
LiteNetLib.NetManager:SendRaw(NetPacket, IPEndPoint) (at Assets/LiteNetLibTransport/LiteNetLib/NetManager.cs:448)
LiteNetLib.NetPeer:.ctor(NetManager, IPEndPoint, Int32, Byte, NetDataWriter) (at Assets/LiteNetLibTransport/LiteNetLib/NetPeer.cs:281)
LiteNetLib.NetManager:Connect(IPEndPoint, NetDataWriter) (at Assets/LiteNetLibTransport/LiteNetLib/NetManager.cs:1430)
LiteNetLib.NetManager:Connect(String, Int32, NetDataWriter) (at Assets/LiteNetLibTransport/LiteNetLib/NetManager.cs:1379)
LiteNetLib.NetManager:Connect(String, Int32, String) (at Assets/LiteNetLibTransport/LiteNetLib/NetManager.cs:1356)
LiteNetLibMirror.Client:Connect(String) (at Assets/LiteNetLibTransport/Client.cs:64)
Mirror.LiteNetLibTransport:ClientConnect(String) (at Assets/LiteNetLibTransport/LiteNetLibTransport.cs:177)
Mirror.NetworkClient:Connect(String) (at Assets/Mirror/Runtime/NetworkClient.cs:73)
Mirror.NetworkManager:StartClient() (at Assets/Mirror/Runtime/NetworkManager.cs:411)
TuringVR.Networking.HolosNetworkManager:NetworkDiscovery_OnDiscoveredServer(String, String) (at Assets/_Holos/Scripts/Networking/HolosNetworkManager.cs:186)
UnityEngine.Events.UnityEvent`2:Invoke(String, String)
TuringVR.Networking.LANNetworkDiscovery:OnReceivedBroadcast(String, String) (at Assets/_Holos/Scripts/Networking/LANNetworkDiscovery.cs:19)
UnityEngine.Networking.NetworkDiscovery:Update()
because the packet is being routed through the non-dual mode IPv6 socket. There are one of two possible fixes: use dual mode sockets, or handle IPv4-mapped IPv6 addresses as a special case and ensure they get routed through the IPv4 socket.
The fix for switching to dual mode is to change the IPv6Enabled to have value IPv6Mode.DualMode instead of IPv6Mode.SeparateSocket at https://github.com/MirrorNetworking/LiteNetLibTransport/blob/master/source/LiteNetLib/NetManager.cs#L294
Alternatively the IPv4-mapped IPv6 address can be parsed specially using https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.isipv4mappedtoipv6?view=netcore-3.1 and https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.maptoipv4?view=netcore-3.1