private void DeviceListView_EncryptionButton_Click(object sender, RoutedEventArgs e) { PhysicalAddressSerializable clickMac = PhysicalAddressSerializable.Parse(((Button)sender).Tag.ToString()); Device device = Settings.SettingsData.GetDevice(clickMac); if (device.EncryptionEnabled) { Settings.ConfirmationDialog dialog = new Settings.ConfirmationDialog("Remove encryption?", $"Do you want to remove encryption for communication with '{device.Name}'?", "Yes", "Cancel"); dialog.Owner = Application.Current.MainWindow; if (dialog.ShowDialog() ?? false) { Settings.SettingsData.SetDeviceEncryptionPassword(clickMac, null); } } else { EncryptionPasswordDialog dialog = new EncryptionPasswordDialog(device.Name); dialog.Owner = Application.Current.MainWindow; if (dialog.ShowDialog() ?? false) { Settings.SettingsData.SetDeviceEncryptionPassword(clickMac, dialog.Password); } } }
/// <summary> /// Creates a presence buffer with device's Mac address, username and device type. /// </summary> public static void GeneratePresenceBuffer(PhysicalAddressSerializable mac, out byte[] buffer, TTInstruction instruction, string name = "", DeviceType type = DeviceType.Unknown) { if ((int)instruction < 10 || (int)instruction > 12) { new Exception("Presence buffer instruction out of range: " + instruction.ToString()); } var macBytes = mac.GetAddressBytes(); if (instruction == TTInstruction.Discovery_Bye) { buffer = new byte[7]; Array.Copy(macBytes, buffer, 6); buffer[6] = (byte)instruction; } else { var deviceTypeByte = (byte)type; var nameBytes = Encoding.UTF8.GetBytes(Properties.Settings.Default.Name); buffer = new byte[7 + 1 + nameBytes.Length]; Array.Copy(macBytes, buffer, 6); buffer[6] = (byte)instruction; buffer[7] = deviceTypeByte; Array.Copy(nameBytes, 0, buffer, 8, nameBytes.Length); } }
public override bool Equals(object comparand) { PhysicalAddressSerializable address = comparand as PhysicalAddressSerializable; if (address == null) { PhysicalAddress ad = comparand as PhysicalAddress; if (ad == null) { return(false); } else { address = (PhysicalAddressSerializable)ad; } } if (this.address.Length != address.address.Length) { return(false); } for (int i = 0; i < address.address.Length; i++) { if (this.address[i] != address.address[i]) { return(false); } } return(true); }
public Device(string name, IPAddress ipAddress, PhysicalAddressSerializable macAddress, DeviceType type) { this.name = name; this.ip = ipAddress; this.mac = macAddress; this.type = type; this.receiveMode = DeviceReceiveMode.Deny; }
public Device() { name = null; ip = null; mac = null; type = DeviceType.Unknown; receiveMode = DeviceReceiveMode.Deny; }
/// <summary> /// Set this device to be online through next timeout. /// </summary> public static void SetOnline(PhysicalAddressSerializable mac) { if (!macAddresses.Contains(mac)) { macAddresses.Add(mac); lastPresenceTimes.Add(DateTime.UtcNow); Settings.SettingsData.GetDevice(mac).NotifyPropertyChanged(); } else { lastPresenceTimes[macAddresses.IndexOf(mac)] = DateTime.UtcNow; } }
/// <summary> /// Go though all online devices and remove any, which haven't sent a presence packet in the designated time interval. /// </summary> public static void TimeoutDevices() { DateTime currentTimeUTC = DateTime.UtcNow; for (int i = 0; i < macAddresses.Count; i++) { PhysicalAddressSerializable removedMac = macAddresses[i]; TimeSpan difference = currentTimeUTC.Subtract(lastPresenceTimes[i]); if (difference.TotalMilliseconds > Settings.SettingsData.PresenceSendPeriod + Settings.SettingsData.MaxNetworkPingMs) { macAddresses.RemoveAt(i); lastPresenceTimes.RemoveAt(i); i--; } Settings.SettingsData.GetDevice(removedMac).NotifyPropertyChanged(); } }
// General /// <summary> /// Returns true if data in buffer is in correct format, unpacks mac, instruction and data into variables. /// </summary> /// <param name="buffer">Data received by client</param> /// <param name="macAddress">MAC Address of remote host</param> /// <param name="instruction">Received TTransfer network instruction</param> /// <param name="data">Data remaining in buffer</param> /// <returns></returns> public static bool UnpackBuffer(byte[] buffer, ref PhysicalAddressSerializable macAddress, ref TTInstruction instruction, ref List <byte> data) { List <byte> b = new List <byte>(buffer); if (b.Count < 7 || !Enum.IsDefined(typeof(TTInstruction), instruction)) { return(false); } macAddress = new PhysicalAddressSerializable(b.GetRange(0, 6).ToArray()); instruction = (TTInstruction)b[6]; data = b.GetRange(7, b.Count - 7); return(true); }
private void DeviceListView_Remove_Click(object sender, RoutedEventArgs e) { PhysicalAddressSerializable clickMac = PhysicalAddressSerializable.Parse(((Button)sender).Tag.ToString()); Settings.SettingsData.RemoveDevice(clickMac); }
// General private void InitializeNetworkInfo() { IPAddress internetIp; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect("8.8.8.8", 65530); IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; internetIp = endPoint.Address; } // Mac - this will either select the interface based on the selected mac address or the interface which will connect to internet MacAddress = null; PhysicalAddress backupMac = null; IPAddress backupIp = null; IPAddress backupMask = null; var nics = TTNet.GetUsableNetworkInterfaces(); foreach (NetworkInterface nic in nics) { var interNetwork = nic.GetIPProperties().UnicastAddresses.Where(u => u.Address.AddressFamily == AddressFamily.InterNetwork); IPAddress interfaceIp = interNetwork.Select(u => u.Address).FirstOrDefault(); IPAddress interfaceMask = interNetwork.Select(u => u.IPv4Mask).FirstOrDefault(); if (interfaceIp == null) { continue; } PhysicalAddress interfaceMac = nic.GetPhysicalAddress(); if (Settings.SettingsData.InterfaceMac != null && Settings.SettingsData.InterfaceMac.Equals(interfaceMac)) { MacAddress = interfaceMac; IPAddress = interfaceIp; IPMask = interfaceMask; break; } if (interfaceIp.ToString() == internetIp.ToString()) { backupMac = interfaceMac; backupIp = interfaceIp; backupMask = interfaceMask; } } // If interface with selected mac doesn't exist, select the one which connect to internet if (MacAddress == null) { if (backupMac != null) { MacAddress = backupMac; IPAddress = internetIp; IPMask = backupMask; Settings.SettingsData.InterfaceMac = MacAddress; } else { throw new Exception("Could not find a usable network interface."); } } // Broadcast endpoint PresenceEndPoint = new IPEndPoint(TTNet.GetBroadcastAddress(IPAddress, IPMask), Settings.SettingsData.NetworkPresencePort); }
/// <summary> /// Set this device to be set offline during next timeout. /// </summary> public static void SetOffline(PhysicalAddressSerializable mac) { lastPresenceTimes[macAddresses.IndexOf(mac)] = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1)); }
/// <summary> /// Returns true if device with this Mac is online. /// </summary> public static bool IsOnline(PhysicalAddressSerializable mac) { return(macAddresses.Contains(mac)); }