From 0e9da42ce020f49c212a8cca8d86a3735a22bcfe Mon Sep 17 00:00:00 2001 From: Simon Hartfield Date: Thu, 4 Oct 2018 21:08:08 +0100 Subject: [PATCH] Allow separate internal and external ports on port forwarding Rename property on delete forwarding function --- Lidgren.Network/NetUPnP.cs | 135 +++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 64 deletions(-) diff --git a/Lidgren.Network/NetUPnP.cs b/Lidgren.Network/NetUPnP.cs index 217602f..ac71115 100644 --- a/Lidgren.Network/NetUPnP.cs +++ b/Lidgren.Network/NetUPnP.cs @@ -160,77 +160,84 @@ namespace Lidgren.Network return false; } return false; - } + } - /// - /// Add a forwarding rule to the router using UPnP - /// - public bool ForwardPort(int port, string description) - { - if (!CheckAvailability()) - return false; + /// + /// Add a forwarding rule to the router using UPnP + /// + /// The external, WAN facing, port + /// A description for the port forwarding rule + /// The port on the client machine to send traffic to + public bool ForwardPort(int externalPort, string description, int internalPort = 0) + { + if (!CheckAvailability()) + return false; - IPAddress mask; - var client = NetUtility.GetMyAddress(out mask); - if (client == null) - return false; + IPAddress mask; + var client = NetUtility.GetMyAddress(out mask); + if (client == null) + return false; - try - { - SOAPRequest(m_serviceUrl, - "" + - "" + - "" + port.ToString() + "" + - "" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "" + - "" + port.ToString() + "" + - "" + client.ToString() + "" + - "1" + - "" + description + "" + - "0" + - "", - "AddPortMapping"); + if (internalPort == 0) + internalPort = externalPort; - m_peer.LogDebug("Sent UPnP port forward request"); - NetUtility.Sleep(50); - } - catch (Exception ex) - { - m_peer.LogWarning("UPnP port forward failed: " + ex.Message); - return false; - } - return true; - } + try + { + SOAPRequest(m_serviceUrl, + "" + + "" + + "" + externalPort.ToString() + "" + + "" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "" + + "" + internalPort.ToString() + "" + + "" + client.ToString() + "" + + "1" + + "" + description + "" + + "0" + + "", + "AddPortMapping"); - /// - /// Delete a forwarding rule from the router using UPnP - /// - public bool DeleteForwardingRule(int port) - { - if (!CheckAvailability()) - return false; + m_peer.LogDebug("Sent UPnP port forward request"); + NetUtility.Sleep(50); + } + catch (Exception ex) + { + m_peer.LogWarning("UPnP port forward failed: " + ex.Message); + return false; + } + return true; + } - try - { - SOAPRequest(m_serviceUrl, - "" + - "" + - "" + - "" + port + "" + - "" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "" + - "", "DeletePortMapping"); - return true; - } - catch (Exception ex) - { - m_peer.LogWarning("UPnP delete forwarding rule failed: " + ex.Message); - return false; - } - } + /// + /// Delete a forwarding rule from the router using UPnP + /// + /// The external, 'internet facing', port + public bool DeleteForwardingRule(int externalPort) + { + if (!CheckAvailability()) + return false; - /// - /// Retrieve the extern ip using UPnP - /// - public IPAddress GetExternalIP() + try + { + SOAPRequest(m_serviceUrl, + "" + + "" + + "" + + "" + externalPort + "" + + "" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "" + + "", "DeletePortMapping"); + return true; + } + catch (Exception ex) + { + m_peer.LogWarning("UPnP delete forwarding rule failed: " + ex.Message); + return false; + } + } + + /// + /// Retrieve the extern ip using UPnP + /// + public IPAddress GetExternalIP() { if (!CheckAvailability()) return null;