/// <summary> /// The UPnP Managed Class /// </summary> /// <remarks></remarks> public UPnP() { _lastInstance = this; //Create the new NAT Class _upnpnat = new UPnPNAT(); //generate the static mappings GetStaticMappings(); }
public PortMappingEntry(uint port, string ip, string name, UPnP.Protocol protocol) { try { Port = port; Ip = ip; Name = name; Protocol = protocol; } catch (Exception) { throw new InvalidCastException("Couldn't create portmapping entry"); } }
private static void GetMappingJob() { try { if (_lastInstance == null) { _lastInstance = new UPnP(); } // Return list List <PortMappingEntry> l = new List <PortMappingEntry>(); // Loop through all the data after a check if (!UpnpEnabled || !_lastInstance._staticEnabled || _lastInstance._staticMapping == null) { return; } foreach (IStaticPortMapping mapping in _lastInstance._staticMapping) { try { l.Add(new PortMappingEntry(Convert.ToUInt32(mapping.InternalPort), mapping.InternalClient, mapping.Description, mapping.Protocol)); } catch (Exception ex) { Logger.Log(LogLevel.Warning, "uPnP", "Couldn't load mapping: " + ex.Message); } } MappingUpdateReceivedEventHandler handler = MappingUpdateReceived; if (handler != null) { handler.Invoke(l); } } catch (Exception exception) { Logger.Log(LogLevel.Warning, "UPnP", "Couldn't get upnp mapping! Probably UPnP is disabled", exception.Message); if (_lastInstance != null) { _lastInstance._staticEnabled = false; // remember that upnp is disabled } } // No return value, use the event to get the list! }
private void UpdateMappingAsync() { if (InvokeRequired) { Invoke((MethodInvoker)(UpdateMappingAsync)); } else { BtnAdd.Enabled = false; BtnRefresh.Enabled = false; UPnP.GetMapping(); // this.lblStatus.Text = "Loading info. This could take a while..."; } }
private void PortForwarder_Load(object sender, EventArgs e) { //if (!UPnP.UpnpEnabled) //{ // MetroMessageBox.Show(Application.OpenForms[0], // Locale.Tr("Port forwarding isn't available") + Constants.vbCrLf + // Locale.Tr("Your network device (router) doesn't support UPnP"), // Locale.Tr("Port forwarding unavailable"), MessageBoxButtons.OK, MessageBoxIcon.Error); //} TxtIp.Text = UPnP.LocalIp(); CBProtocol.SelectedIndex = 0; UpdateMappingAsync(); }
public static bool Forward(string name, uint port, string ip, Protocol protocol = Protocol.TCP, bool @async = true) { try { // lblStatus.Text = "adding port forward..."; if (_lastInstance == null) { _lastInstance = new UPnP(); } if (_lastInstance.Exists(port, protocol)) { MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr("This port is aleady forwarded"), Locale.Tr("Port already in use"), MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); //aLocale.Tready in use } if (!@async) { _lastInstance.Add(ip, port, protocol, name); } else { Thread t = new Thread((() => Forward(new PortMappingEntry(port, ip, name, protocol)))) { Name = "Foward_" + port }; t.SetApartmentState(ApartmentState.MTA); t.Start(); } return(true); } catch (Exception ex) { Logger.Log(LogLevel.Warning, "PortForwarder", "Couldn't forward ports", ex.InnerException.Message); MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr( "This port couldn't be forwarded. Something went wrong while communicating with your router"), Locale.Tr("Can't forward"), MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }
private void BtnAdd_Click(object sender, EventArgs e) { UPnP.Protocol protocol; if (CBProtocol.SelectedIndex == 1) { protocol = UPnP.Protocol.UDP; } else { protocol = UPnP.Protocol.TCP; } if (!Regex.IsMatch(TxtIp.Text, "(\\d{1,3}\\.){3}\\d{1,3}")) { MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr( "The IP address you entered is invalid. It should be something like for example 192.168.1.2"), Locale.Tr("Invalid inpunt"), MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } UPnP.Forward(TxtName.Text, Convert.ToUInt32(NumPort.Value), TxtIp.Text, protocol); }
/// <summary> /// Removes a port mapping from the UPnP enabled device. /// </summary> /// <param name="port">The port to remove.</param> /// <param name="protocol">The protocol of the port [TCP/UDP]</param> /// <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception> /// <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception> /// <exception cref="ArgumentException">This exception is thrown when the port [or protocol] is invalid.</exception> /// <remarks></remarks> public static void Remove(int port, Protocol protocol) { if (_lastInstance == null) { _lastInstance = new UPnP(); } if (!UpnpEnabled) { throw new ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization."); } // Begin utilizing if (!_lastInstance.Exists(port, protocol)) { throw new ArgumentException("This mapping doesn't exist!", port + " : " + protocol); } // Okay, continue on _lastInstance._staticMapping.Remove(port, protocol.ToString()); GetMapping(); //refresh }
public static bool Forward(string name, uint port, string ip, Protocol protocol = Protocol.TCP, bool @async = true) { try { // lblStatus.Text = "adding port forward..."; if (_lastInstance == null) _lastInstance = new UPnP(); if (_lastInstance.Exists(port, protocol)) { MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr("This port is aleady forwarded"), Locale.Tr("Port already in use"), MessageBoxButtons.OK, MessageBoxIcon.Error); return false; //aLocale.Tready in use } if (!@async) { _lastInstance.Add(ip, port, protocol, name); } else { Thread t = new Thread((() => Forward(new PortMappingEntry(port, ip, name, protocol)))) { Name = "Foward_" + port }; t.SetApartmentState(ApartmentState.MTA); t.Start(); } return true; } catch (Exception ex) { Logger.Log(LogLevel.Warning, "PortForwarder", "Couldn't forward ports", ex.InnerException.Message); MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr( "This port couldn't be forwarded. Something went wrong while communicating with your router"), Locale.Tr("Can't forward"), MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
private static void GetMappingJob() { try { if (_lastInstance == null) _lastInstance = new UPnP(); // Return list List<PortMappingEntry> l = new List<PortMappingEntry>(); // Loop through all the data after a check if (!UpnpEnabled || !_lastInstance._staticEnabled || _lastInstance._staticMapping == null) return; foreach (IStaticPortMapping mapping in _lastInstance._staticMapping) { try { l.Add(new PortMappingEntry(Convert.ToUInt32(mapping.InternalPort), mapping.InternalClient, mapping.Description, mapping.Protocol)); } catch (Exception ex) { Logger.Log(LogLevel.Warning, "uPnP", "Couldn't load mapping: " + ex.Message); } } MappingUpdateReceivedEventHandler handler = MappingUpdateReceived; if (handler != null) { handler.Invoke(l); } } catch (Exception exception) { Logger.Log(LogLevel.Warning, "UPnP", "Couldn't get upnp mapping! Probably UPnP is disabled", exception.Message); if (_lastInstance != null) _lastInstance._staticEnabled = false; // remember that upnp is disabled } // No return value, use the event to get the list! }
/// <summary> /// Removes a port mapping from the UPnP enabled device. /// </summary> /// <param name="port">The port to remove.</param> /// <param name="protocol">The protocol of the port [TCP/UDP]</param> /// <exception cref="ApplicationException">This exception is thrown when UPnP is disabled.</exception> /// <exception cref="ObjectDisposedException">This exception is thrown when this class has been disposed.</exception> /// <exception cref="ArgumentException">This exception is thrown when the port [or protocol] is invalid.</exception> /// <remarks></remarks> public static void Remove(int port, Protocol protocol) { if (_lastInstance == null) _lastInstance = new UPnP(); if (!UpnpEnabled) throw new ApplicationException("UPnP is not enabled, or there was an error with UPnP Initialization."); // Begin utilizing if (!_lastInstance.Exists(port, protocol)) throw new ArgumentException("This mapping doesn't exist!", port + " : " + protocol); // Okay, continue on _lastInstance._staticMapping.Remove(port, protocol.ToString()); GetMapping(); //refresh }
public void RemoveFromMapping() { UPnP.Remove(Convert.ToInt32(Port), Protocol); }
public void AddToMapping() { UPnP.Forward(this); }