static void NatUtility_ClosePort(NatDeviceState device, int port) { lock (syncLock) { Mapping mapping = new Mapping(Protocol.Udp, port, port); device.Nat.BeginDeletePortMap(mapping, ar => { lock (syncLock) { try { device.Nat.EndDeletePortMap(ar); ClosePortMapping(device, port); } catch (MappingException exn) { if (exn.ErrorCode == 714) { ClosePortMapping(device, port); } else { BoltLog.Exception(exn); } } catch (Exception exn) { BoltLog.Exception(exn); } } }, null); } }
static void NatUtility_OpenPort(NatDeviceState device, int port) { lock (syncLock) { Mapping mapping = new Mapping(Protocol.Udp, port, port); device.Nat.BeginCreatePortMap(mapping, ar => { lock (syncLock) { try { device.Nat.EndCreatePortMap(ar); // finish this NatUtility_OpenPort_Finish(device, port); } catch (MappingException exn) { if (exn.ErrorCode == 718) { NatUtility_OpenPort_Finish(device, port); } else { BoltLog.Exception(exn); } } catch (Exception exn) { BoltLog.Exception(exn); } } }, null); } }
static void NatUtility_OpenPort_Finish(NatDeviceState device, int port) { try { var natMapping = device.PortMappings.Values.FirstOrDefault(p => p.Internal == port && p.External == port); if (natMapping != null) { // set this port as open natMapping.Status = Bolt.NatPortMappingStatus.Open; // tell user about this portChanges.Enqueue(new NatPortMappingChanged { Device = device, Mapping = natMapping.Clone() }); // meep BoltLog.Info("Changed {0} on {1}", natMapping, device); } else { BoltLog.Warn("Received incorrect port mapping result from {0}", device); } } catch (Exception exn) { BoltLog.Exception(exn); } }
static void NatUtility_FindPublicAddress(NatDeviceState device) { lock (syncLock) { device.Nat.BeginGetExternalIP(ar => { lock (syncLock) { try { device.ExternalAddress = device.Nat.EndGetExternalIP(ar); BoltLog.Info("Found external address of {0}", device); } catch (Exception exn) { BoltLog.Exception(exn); } } }, null); } }
static void NatUtility_DeviceFound(object sender, DeviceEventArgs e) { lock (syncLock) { foreach (var device in deviceList) { if (device.Equals(e.Device)) { return; } } NatDeviceState deviceState; deviceState = new NatDeviceState { Nat = e.Device }; deviceState.PortMappings = new Dictionary <int, NatPortMapping>(); deviceList.Add(deviceState); BoltLog.Info("Found {0}", deviceState); NatUtility_FindPublicAddress(deviceState); } }
static void NatUtility_DeviceFound(object sender, DeviceEventArgs e) { lock (syncLock) { foreach (var device in deviceList) { if (device.Equals(e.Device)) { return; } } NatDeviceState deviceState; deviceState = new NatDeviceState { Nat = e.Device }; deviceState.PortMappings = new Dictionary<int, NatPortMapping>(); deviceList.Add(deviceState); BoltLog.Info("Found {0}", deviceState); NatUtility_FindPublicAddress(deviceState); } }