private async Task AddPortMapping() { this.ClearOutputAction(); this.PrintEntry(); PortMappingEntry entry = new PortMappingEntry(); this.PrintOutputAction("Remote host:"); if (IPAddress.TryParse(this.GetInputFunc(), out IPAddress remote)) { entry.RemoteHost = remote; } this.PrintOutputAction("External port:"); entry.ExternalPort = UInt16.TryParse(this.GetInputFunc(), out UInt16 res) ? res : (ushort)1; this.PrintOutputAction("Internal host:"); entry.InternalHost = IPAddress.TryParse(this.GetInputFunc(), out IPAddress internalHost) ? internalHost : IPAddress.None; this.PrintOutputAction("internal port:"); entry.InternalPort = UInt16.TryParse(this.GetInputFunc(), out UInt16 res2) ? res2 : (ushort)1; this.PrintOutputAction("Protocol (UDP, TCP, ESP, GRE)"); entry.PortMappingProtocol = (PortMappingProtocol)Enum.Parse(typeof(PortMappingProtocol), this.GetInputFunc()); entry.Enabled = true; this.PrintOutputAction("Description:"); entry.Description = this.GetInputFunc(); await this._client.AddPortMappingAsync(entry); this.PrintOutputAction("Port mapping added"); }
bool getMappingEntryWithIndex(int index, out PortMappingEntry pm) { pm = new PortMappingEntry(); XmlDocument xResponse = null; string txtError = null; string SOAPbody = "<u:GetGenericPortMappingEntry xmlns:u=\"urn:schemas-upnp-org:service:" + ServiceName + ":1\">" + "<NewPortMappingIndex>" + index.ToString().Trim() + "</NewPortMappingIndex>" + "</u:GetGenericPortMappingEntry>"; string SOAPfunction = "GetGenericPortMappingEntry"; if (TrySubmitSOAPRequest(SOAPbody, SOAPfunction, ref txtError, ref xResponse) != NatHelperReponseCodes.OK) { return(false); // end of entries? } try { XmlNamespaceManager nsMgr = new XmlNamespaceManager(xResponse.NameTable); nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0"); XmlNode nd = null; int i = 0; // External Port nd = xResponse.SelectSingleNode("//NewExternalPort/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { if (Int32.TryParse(nd.Value, out i)) { pm.ExternalPort = i; } } // Internal Port nd = xResponse.SelectSingleNode("//NewInternalPort/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { if (Int32.TryParse(nd.Value, out i)) { pm.InternalPort = i; } } // Internal Client nd = xResponse.SelectSingleNode("//NewInternalClient/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { pm.InternalClient = nd.Value; } // Description nd = xResponse.SelectSingleNode("//NewPortMappingDescription/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { pm.Description = nd.Value; } // Protocol nd = xResponse.SelectSingleNode("//NewProtocol/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { PortMappingEntry.PortMappingEntryProtocolTypes result = 0; if (Enum.TryParse <PortMappingEntry.PortMappingEntryProtocolTypes>(nd.Value, out result)) { pm.Protocol = result; } } // Enabled nd = xResponse.SelectSingleNode("//NewEnabled/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { bool b = false; // ENUM PARSE if (bool.TryParse(nd.Value, out b)) { pm.Enabled = b; } else if (Int32.TryParse(nd.Value, out i)) { pm.Enabled = (i == 1); } } // Lease nd = xResponse.SelectSingleNode("//NewLeaseDuration/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { if (Int32.TryParse(nd.Value, out i)) { pm.LeaseDuration = i; } } return(true); } catch { Functions.WriteLineToLogFileIfAdvanced("NATHelper: SOAP: Couldn't get or parse port mapping entry. InnerXML: " + xResponse.InnerXml); return(false); } }
bool getMappingEntryWithIndex(int index, out PortMappingEntry pm) { pm = new PortMappingEntry(); XmlDocument xResponse = null; string txtError = null; string SOAPbody = "<u:GetGenericPortMappingEntry xmlns:u=\"urn:schemas-upnp-org:service:" + ServiceName + ":1\">" + "<NewPortMappingIndex>" + index.ToString().Trim() + "</NewPortMappingIndex>" + "</u:GetGenericPortMappingEntry>"; string SOAPfunction = "GetGenericPortMappingEntry"; if (TrySubmitSOAPRequest(SOAPbody, SOAPfunction, ref txtError, ref xResponse) != NatHelperReponseCodes.OK) { return false; // end of entries? } try { XmlNamespaceManager nsMgr = new XmlNamespaceManager(xResponse.NameTable); nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0"); XmlNode nd = null; int i = 0; // External Port nd = xResponse.SelectSingleNode("//NewExternalPort/text()", nsMgr); if ( (nd != null) && (!(string.IsNullOrWhiteSpace( nd.Value ))) ) { if (Int32.TryParse(nd.Value, out i)) pm.ExternalPort = i; } // Internal Port nd = xResponse.SelectSingleNode("//NewInternalPort/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { if (Int32.TryParse(nd.Value, out i)) pm.InternalPort = i; } // Internal Client nd = xResponse.SelectSingleNode("//NewInternalClient/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) pm.InternalClient = nd.Value; // Description nd = xResponse.SelectSingleNode("//NewPortMappingDescription/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) pm.Description = nd.Value; // Protocol nd = xResponse.SelectSingleNode("//NewProtocol/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { PortMappingEntry.PortMappingEntryProtocolTypes result = 0; if (Enum.TryParse<PortMappingEntry.PortMappingEntryProtocolTypes>(nd.Value, out result)) pm.Protocol = result; } // Enabled nd = xResponse.SelectSingleNode("//NewEnabled/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { bool b = false; // ENUM PARSE if (bool.TryParse(nd.Value, out b)) pm.Enabled = b; else if (Int32.TryParse(nd.Value, out i)) pm.Enabled = (i == 1); } // Lease nd = xResponse.SelectSingleNode("//NewLeaseDuration/text()", nsMgr); if ((nd != null) && (!(string.IsNullOrWhiteSpace(nd.Value)))) { if (Int32.TryParse(nd.Value, out i)) pm.LeaseDuration = i; } return true; } catch { Functions.WriteLineToLogFileIfAdvanced("NATHelper: SOAP: Couldn't get or parse port mapping entry. InnerXML: " + xResponse.InnerXml); return false; } }
private void UPnPTimerCallback(object state) { try { DefaultNetworkInfo defaultNetworkInfo = NetUtilities.GetDefaultNetworkInfo(); if (defaultNetworkInfo == null) { //no internet available; _upnpStatus = UPnPStatus.NoInternetConnection; return; } if (defaultNetworkInfo.IsPublicIP) { //public ip so no need to do port forwarding _upnpStatus = UPnPStatus.PortForwardingNotRequired; return; } IPEndPoint LocalNetworkEP = new IPEndPoint(defaultNetworkInfo.LocalIP, ((IPEndPoint)_tcpListener.LocalEndpoint).Port); try { if ((_upnp == null) || (!_upnp.NetworkBroadcastAddress.Equals(defaultNetworkInfo.BroadcastIP))) { _upnp = InternetGatewayDevice.Discover(defaultNetworkInfo.BroadcastIP, 30000); } } catch { _upnpStatus = UPnPStatus.UPnPDeviceNotFound; throw; } //find external ip from router try { IPAddress externalIP = _upnp.GetExternalIPAddress(); if (!_externalSelfEP.Address.Equals(externalIP)) { _externalSelfEP = new IPEndPoint(externalIP, _externalSelfEP.Port); } } catch { } int externalPort = LocalNetworkEP.Port; bool isTCPMapped = false; try { int loopCount = 0; while (true) { PortMappingEntry portMap = _upnp.GetSpecificPortMappingEntry(ProtocolType.Tcp, externalPort); if (portMap == null) { break; //port available } if (portMap.InternalEP.Equals(LocalNetworkEP)) { //port already mapped with us isTCPMapped = true; _upnpStatus = UPnPStatus.PortForwarded; break; } //find new port for mapping if (externalPort < ushort.MaxValue) { externalPort++; } else { externalPort = 1024; } if (loopCount > ushort.MaxValue) { return; } loopCount++; } } catch { } if (!isTCPMapped) { try { _upnp.AddPortMapping(ProtocolType.Tcp, externalPort, LocalNetworkEP, "BitChat - TCP"); if (_externalSelfEP.Port != externalPort) { _externalSelfEP = new IPEndPoint(_externalSelfEP.Address, externalPort); } _upnpStatus = UPnPStatus.PortForwarded; Debug.Write("BitChatClient.UPnPTimerCallback", "tcp port mapped " + externalPort); } catch { try { _upnp.DeletePortMapping(ProtocolType.Tcp, externalPort); _upnp.AddPortMapping(ProtocolType.Tcp, externalPort, LocalNetworkEP, "BitChat - TCP"); if (_externalSelfEP.Port != externalPort) { _externalSelfEP = new IPEndPoint(_externalSelfEP.Address, externalPort); } _upnpStatus = UPnPStatus.PortForwarded; Debug.Write("BitChat.UPnPTimerCallback", "tcp port mapped " + externalPort); } catch { } } } } catch (Exception ex) { Debug.Write("BitChat.UPnPTimerCallback", ex); } finally { if (_upnpTimer != null) { switch (_upnpStatus) { case Connections.UPnPStatus.UPnPDeviceNotFound: _upnpTimer.Change(10000, Timeout.Infinite); break; default: _upnpTimer.Change(UPNP_TIMER_INTERVAL, Timeout.Infinite); break; } } } }