public static IAsyncResult BeginDeletePortMap(this INatDevice device, Mapping mapping, AsyncCallback callback, object asyncState) { var result = new TaskAsyncResult(device.DeletePortMapAsync(mapping), callback, asyncState); result.Task.ContinueWith(t => result.Complete(), TaskScheduler.Default); return(result); }
public static async void Shutdown() { if (currentMapping != null && device != null) { await device.DeletePortMapAsync(currentMapping); } Debug.Log("Port Mappings Cleaned Up"); }
private static async void HandleQuit(object sender, ConsoleCancelEventArgs args) { System.Console.WriteLine("quitting"); if (gatewayDevice != null) { await gatewayDevice.DeletePortMapAsync(new Mapping(Protocol.Tcp, PORT, PORT)); System.Console.WriteLine("Port mapping has been removed."); } }
async Task DeletePortMapping(INatDevice device, Mapping mapping) { var map = new Mono.Nat.Mapping( mapping.Protocol == Protocol.Tcp ? Mono.Nat.Protocol.Tcp : Mono.Nat.Protocol.Udp, mapping.PrivatePort, mapping.PublicPort ); try { await device.DeletePortMapAsync(map); } catch { } }
private async void DeviceFound(object sender, DeviceEventArgs args) { try { INatDevice device = args.Device; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Device found"); Console.ResetColor(); Console.WriteLine("Type: {0}", device.GetType().Name); Console.WriteLine("IP: {0}", await device.GetExternalIPAsync()); await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 1500, 1500)); Console.WriteLine("---"); return; Mapping mapping = new Mapping(Protocol.Tcp, 6001, 6001); await device.CreatePortMapAsync(mapping); Console.WriteLine("Create Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); try { Mapping m = await device.GetSpecificMappingAsync(Protocol.Tcp, 6001); Console.WriteLine("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); } catch { Console.WriteLine("Couldnt get specific mapping"); } foreach (Mapping mp in await device.GetAllMappingsAsync()) { Console.WriteLine("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); await device.DeletePortMapAsync(mp); } Console.WriteLine("External IP: {0}", await device.GetExternalIPAsync()); Console.WriteLine("Done..."); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
private async Task RemovePortMappings() { refreshTimer.Stop(); if (natDevice == null) { return; } // Remove all mappings. for (int i = 0; i < 2; i++) { Mapping mapping = activeMappings[i]; if (mapping == null) { continue; } // Delete mapping. Mapping result; try { result = await natDevice.DeletePortMapAsync(mapping); } catch (Exception e) { Debug.WriteLine("Failed to delete port mapping: {0}", e); ShowMessageNoBlock("Failed to remove port mapping", e.Message, MessageBoxIcon.Error); continue; } finally { activeMappings[i] = null; } // Check mapping. if (!mapping.Equals(result)) { Debug.WriteLine("Deleted mapping not equal\n expected: {0}\n actual: {1}", mapping, result); } } }
/// <summary> /// Deletes an existing port map. /// </summary> /// <param name="port">The port to delete.</param> public static void DeletePortMap(int port) { if (!_discoveryComplete) { return; } Mapping mapping; if (_mappings.TryGetValue(port, out mapping)) { try { for (int i = 0; i < 3; i++) { _device.DeletePortMapAsync(mapping); } } catch (MappingException) { } } }
public static Mapping DeletePortMap(this INatDevice device, Mapping mapping) { return(device.DeletePortMapAsync(mapping).GetAwaiter().GetResult()); }
private async void DeviceFound(object sender, DeviceEventArgs args) { await locker.WaitAsync(); try { INatDevice device = args.Device; // Only interact with one device at a time. Some devices support both // upnp and nat-pmp. Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Device found: {0}", device.NatProtocol); Console.ResetColor(); Console.WriteLine("Type: {0}", device.GetType().Name); Console.WriteLine("IP: {0}", await device.GetExternalIPAsync()); Console.WriteLine("---"); //return; /******************************************/ /* Advanced test suite. */ /******************************************/ // Try to create a new port map: var mapping = new Mapping(Protocol.Tcp, 6001, 6011); await device.CreatePortMapAsync(mapping); Console.WriteLine("Create Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); // Try to retrieve confirmation on the port map we just created: try { Mapping m = await device.GetSpecificMappingAsync(Protocol.Tcp, mapping.PublicPort); Console.WriteLine("Specific Mapping: protocol={0}, public={1}, private={2}", m.Protocol, m.PublicPort, m.PrivatePort); } catch { Console.WriteLine("Couldn't get specific mapping"); } // Try retrieving all port maps: try { var mappings = await device.GetAllMappingsAsync(); if (mappings.Length == 0) { Console.WriteLine("No existing uPnP mappings found."); } foreach (Mapping mp in mappings) { Console.WriteLine("Existing Mappings: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); } } catch { Console.WriteLine("Couldn't get all mappings"); } // Try deleting the port we opened before: try { await device.DeletePortMapAsync(mapping); Console.WriteLine("Deleting Mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); } catch { Console.WriteLine("Couldn't delete specific mapping"); } // Try retrieving all port maps: try { var mappings = await device.GetAllMappingsAsync(); if (mappings.Length == 0) { Console.WriteLine("No existing uPnP mappings found."); } foreach (Mapping mp in mappings) { Console.WriteLine("Existing Mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); } } catch { Console.WriteLine("Couldn't get all mappings"); } Console.WriteLine("External IP: {0}", await device.GetExternalIPAsync()); Console.WriteLine("Done..."); } finally { locker.Release(); } }