-
Notifications
You must be signed in to change notification settings - Fork 317
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
Comments
@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?
The reason Is explained here. |
@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 ChatGPTThe 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 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 IPv6Here's an updated example that checks for both IPv4 and IPv6 addresses. However, please note that 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
Limitations and Considerations
Advanced Libraries and FrameworksFor more robust and explicit support for IPv6, consider using more advanced libraries or frameworks. Here are some options:
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. |
Confirmed. If you have full IPv6 support (computer, router, and ISP) then STUN binding requests will fail when trying to connect to the server's IPv6 address. Easy fix is to make sure DNS resolve for your STUN server only returns IPv4 addresses as follows. This didn't seem like a big enough deal for a PR. Change this line from: IPEndPoint remoteEndPoint = new IPEndPoint(System.Net.Dns.GetHostAddresses(host)[0], port); to: IPEndPoint remoteEndPoint = new IPEndPoint(System.Net.Dns.GetHostAddresses(host, AddressFamily.InterNetwork)[0], port); |
Good find, what about? // IPEndPoint remoteEndPoint = new IPEndPoint(System.Net.Dns.GetHostAddresses(host)[0], port);
// Get IPv4 and IPv6 addresses
var addresses = Dns.GetHostAddresses(host);
var ipv4Address = addresses.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
var ipv6Address = addresses.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetworkV6);
// Use the IPv4 address if available, otherwise use the IPv6 address
IPAddress selectedAddress = ipv4Address ?? ipv6Address;
if (selectedAddress == null)
{
throw new Exception("No valid IP address found for the host.");
}
IPEndPoint remoteEndPoint = new IPEndPoint(selectedAddress, port);
Console.WriteLine($"Selected IP: {selectedAddress}");
Console.WriteLine($"Remote Endpoint: {remoteEndPoint}"); |
No description provided.
The text was updated successfully, but these errors were encountered: