Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stun server doesnt work / ipv6 ? #59

Open
Osiris-Team opened this issue Mar 2, 2024 · 3 comments
Open

Stun server doesnt work / ipv6 ? #59

Osiris-Team opened this issue Mar 2, 2024 · 3 comments

Comments

@Osiris-Team
Copy link
Contributor

No description provided.

@Osiris-Team
Copy link
Contributor Author

Osiris-Team commented Mar 11, 2024

@miroslavpejic85 we could switch to ipv6 where each device has an unique ip anyways, and skip stun servers altogether.

@Osiris-Team Osiris-Team changed the title Stun server doesnt work Stun server doesnt work / ipv6 ? Mar 11, 2024
@miroslavpejic85
Copy link
Owner

miroslavpejic85 commented Jun 11, 2024

@miroslavpejic85 we could switch to ipv6 where each device has an unique ip anyways, and skip stun servers altogether.

Are you able to test it out?

Stun server doesn't work

The reason Is explained here.

@Osiris-Team
Copy link
Contributor Author

Osiris-Team commented Jun 11, 2024

@miroslavpejic85 I have no idea how STUN really works, after a bit of research I'm not sure IPv6 would even make a difference since there still is the issue of all ports being closed in the router firewall and actual pc firewall (if we talk about a regular home network). IPv6 would skip NAT, and thus easify identifying the actual device we want to talk to, but not fix the port issue.

I just have a basic understanding of networking, so take this with a grain of salt.

I did some research and UPnP seems to be promissing, just checked my router and it had UPnP enabled by default too:

View C# implementation details according to ChatGPT

The provided example primarily deals with IPv4 addresses. For UPnP to work with IPv6, you need to ensure that the UPnP implementation on both the router and your application supports IPv6. However, the NATUPNPLib library and many consumer routers traditionally focus on IPv4.

UPnP-IGD (Internet Gateway Device) is the protocol for managing port forwarding on routers, and its support for IPv6 can vary. For full IPv6 support, you might need to look into more advanced libraries or frameworks that explicitly support IPv6.

Example Using NATUPNPLib with IPv6

Here's an updated example that checks for both IPv4 and IPv6 addresses. However, please note that NATUPNPLib itself might not support IPv6 out-of-the-box on all systems or routers.

using System;
using System.Net;
using NATUPNPLib;

namespace UPnPExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Open a port
            OpenPort(8080, "TCP", "Test Port");

            // Wait for user input
            Console.WriteLine("Press any key to close the port...");
            Console.ReadKey();

            // Close the port
            ClosePort(8080, "TCP");

            Console.WriteLine("Port closed. Press any key to exit...");
            Console.ReadKey();
        }

        static void OpenPort(int port, string protocol, string description)
        {
            try
            {
                UPnPNAT upnpnat = new UPnPNAT();
                IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

                if (mappings != null)
                {
                    string localIp = GetLocalIPAddress();
                    mappings.Add(port, protocol, port, localIp, true, description);
                    Console.WriteLine($"Port {port}/{protocol} opened successfully for IP {localIp}.");
                }
                else
                {
                    Console.WriteLine("Unable to retrieve port mappings.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to open port {port}/{protocol}: {ex.Message}");
            }
        }

        static void ClosePort(int port, string protocol)
        {
            try
            {
                UPnPNAT upnpnat = new UPnPNAT();
                IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

                if (mappings != null)
                {
                    mappings.Remove(port, protocol);
                    Console.WriteLine($"Port {port}/{protocol} closed successfully.");
                }
                else
                {
                    Console.WriteLine("Unable to retrieve port mappings.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to close port {port}/{protocol}: {ex.Message}");
            }
        }

        static string GetLocalIPAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork || 
                    ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                {
                    return ip.ToString();
                }
            }
            throw new Exception("No network adapters with an IPv4 or IPv6 address in the system!");
        }
    }
}

Explanation

  • GetLocalIPAddress Method: This method now checks for both IPv4 and IPv6 addresses. It returns the first valid IP address it finds, whether IPv4 or IPv6.
  • UPnPNAT and IStaticPortMappingCollection: These are used to interact with the UPnP functionalities. Note that actual IPv6 support may depend on the specific implementation of NATUPNPLib and your router's UPnP capabilities.

Limitations and Considerations

  • Router and Library Support: Ensure that both your router and the UPnP library you are using support IPv6. Many consumer routers and the NATUPNPLib might only support IPv4.
  • Security: UPnP can introduce security risks, especially if it is not properly managed. Always ensure your network is secure.
  • Testing: Testing in a controlled environment is crucial to verify functionality with your specific setup.

Advanced Libraries and Frameworks

For more robust and explicit support for IPv6, consider using more advanced libraries or frameworks. Here are some options:

  • Mono.Nat: A .NET library that supports NAT traversal, including some support for UPnP.
  • MiniUPnP: A library that provides a UPnP IGD client, which might be used in .NET applications with P/Invoke or through a managed wrapper.

These libraries may provide more granular control and better support for IPv6, depending on your needs and environment.

So the optimal case would be if UPnP together with IPv6 is supported, this would skip NAT, open a port and thus allow direct communication from peer-to-peer without any extra complicated steps.
Otherwise UPnP with IPv4 would also work, however this would still require a STUN server inbetween if I understood it correctly, but this would fix the ports/firewall issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants