andante20 / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

The UPnP port forwarding fail on Mac Mono #182

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello!
I'm trying to use UPnP feature on the Mac with Mono 3.10.
Unfortunately, it seems the required .NET API feature is not supported:

Exception This platform is not supported
   -------------------------stack-------------------------
  at System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties () [0x00000] in <filename unknown>:0 
  at Lidgren.Network.NetUtility.GetNetworkInterface () [0x00000] in <filename unknown>:0 
  at Lidgren.Network.NetUtility.GetMyAddress (System.Net.IPAddress& mask) [0x00000] in <filename unknown>:0 
  at Lidgren.Network.NetUPnP.ForwardPort (Int32 port, System.String description) [0x00000] in <filename unknown>:0

Is there any workaround for this issue?

Original issue reported on code.google.com by vladimir...@gmail.com on 19 Dec 2014 at 3:33

GoogleCodeExporter commented 8 years ago
The library uses this to determine the local IPv4 address. I have not used Mono 
so I don't know any workaround, if you find one please post it here.

Original comment by lidg...@gmail.com on 19 Dec 2014 at 3:48

GoogleCodeExporter commented 8 years ago
I found some solutions on StackOverflow 
http://stackoverflow.com/questions/6803073/get-local-ip-address-c-sharp
the last solution works fine (tested on .NET 3.5 on Windows 8 and Mono 2.10 on 
Mac & Linux), even with multiple network adapters enabled:

        // class NetUtility.cs
        /// <summary>
        /// Gets my local IPv4 address (not necessarily external) and subnet mask
        /// </summary>
        public static IPAddress GetMyAddress(out IPAddress mask)
        {
            mask = null;
#if __ANDROID__
            try
            {
                Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.App.Activity.WifiService);
                if (!wifi.IsWifiEnabled) return null;
                var dhcp = wifi.DhcpInfo;

                int addr = dhcp.IpAddress;
                byte[] quads = new byte[4];
                for (int k = 0; k < 4; k++)
                {
                    quads[k] = (byte) ((addr >> k * 8) & 0xFF);
                }           
                return new IPAddress(quads);
            }
            catch // Catch Access Denied errors
            {
                return null;
            }

#endif      
            // acquire local IP address by creating connection and taking it's address 
            // (which is preferred outbound IP address)
            // http://stackoverflow.com/questions/6803073/get-local-ip-address-c-sharp
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
            {
                socket.Connect("10.0.2.4", 65530);
                IPEndPoint endPoint = (IPEndPoint)socket.LocalEndPoint;
                return endPoint.Address;
            }
        }

Original comment by vladimir...@gmail.com on 22 Dec 2014 at 3:50

GoogleCodeExporter commented 8 years ago
The portforwarding on Mono is still not working.
The solution posted above by me is works fine.

Original comment by vladimir...@gmail.com on 3 Apr 2015 at 4:50

GoogleCodeExporter commented 8 years ago
Which Platform*.cs file gets used in your solution? Are there any global 
defines I can use to determine correct platform file? Are you using unity (so 
UNITY_STANDALONE_OSX is defined?)

Original comment by lidg...@gmail.com on 3 Apr 2015 at 9:15

GoogleCodeExporter commented 8 years ago
I'm sorry, I did mention not the latest version of source, where you not yet 
implemented multi-platform feature.
The platform is Mono, we're using UPNP port forwarding on Mac and Linux 
platforms. We using it not only in Unity but in a standalone Mono application 
also.
Regards!

Original comment by vladimir...@gmail.com on 3 Apr 2015 at 9:22