private void OnHostedNetworkPeerStateChanged(WLAN_HOSTED_NETWORK_PEER_STATE PeerState) { switch (PeerState.PeerAuthState) { case WLAN_HOSTED_NETWORK_PEER_AUTH_STATE.wlan_hosted_network_peer_state_invalid: UpdateStationStatus(string.Empty, PeerState.PhysicalAddress.ConvertToString(), string.Empty, "Disconnected"); IPhlpapi.DeleteIPNetEntry(WlanMgr.HostedNetworkGuid, PeerState.PhysicalAddress.Reverse()); IPhlpapi.DeleteIPNetEntry(WlanMgr.HostedNetworkGuid, PeerState.PhysicalAddress); break; case WLAN_HOSTED_NETWORK_PEER_AUTH_STATE.wlan_hosted_network_peer_state_authenticated: System.Threading.Tasks.Task.Run(() => { int tries = 0; System.Net.IPAddress PeerIPAddress = System.Net.IPAddress.None; do { tries++; PeerIPAddress = IPhlpapi.ResolveIPNetEntry(WlanMgr.HostedNetworkGuid, PeerState.PhysicalAddress); Trace.WriteLine($"Try count: {tries}"); Thread.Sleep(TimeSpan.FromSeconds(1)); }while (ICSRouter.IsConnected && (PeerIPAddress.Equals(System.Net.IPAddress.None) && tries < 60)); if (!PeerIPAddress.Equals(System.Net.IPAddress.None)) { string HostName, Mac = PeerState.PhysicalAddress.ConvertToString(); try { HostName = System.Net.Dns.GetHostEntry(PeerIPAddress).HostName; } catch { HostName = string.Empty; } UpdateStationStatus(HostName, Mac, PeerIPAddress.ToString(), "Connected"); BlockConnection(PeerState.PhysicalAddress, PeerIPAddress); } }); break; } }
protected override void OnShown(EventArgs e) { base.OnShown(e); SSIDText.Text = Settings.SSID; KeyText.Text = Settings.Key; WlanMgr = new WlanManager(); if (!WlanMgr.IsHostedNetworkSupported) { UnsupportedDevices(); return; } if (WlanMgr.IsHostedNetworkActive) { IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid); WlanMgr.ForceStop(); } NetIList.DropDown += UpdateInterfaceList; NetIList.SelectionChangeCommitted += OnInterfaceSelected; DataGridList.CellContentClick += OnCellContentClick; DataGridList.CellEndEdit += OnCellEndEdit; ShareButton.MouseClick += OnShareButtonClick; WlanMgr.HostedNetworkStateChange += OnHostedNetworkStateChange; WlanMgr.HostedNetworkPeerStateChanged += OnHostedNetworkPeerStateChanged; WlanMgr.WlanSoftwareRadioStateChanged += OnWlanSoftwareRadioStateChanged; OnHostedNetworkStateChange(WlanMgr.HostedNetworkState); System.Threading.Tasks.Task.Run(() => UpdateHostedNetworkStatistic()); System.Threading.Tasks.Task.Run(() => UpdatePeerListInfo()); }
private void OnMainFormClosing(object sender, FormClosingEventArgs e) { if (!(WlanMgr is null)) { _ = IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid); ICSRouter.StopSharing(); _ = WlanMgr.ForceStop(); } }
private void BlockConnection(PhysicalAddress mac, System.Net.IPAddress addr) { Guid InterfaceGuid = WlanMgr.HostedNetworkGuid; if (!Properties.Settings.Default.Whitelist.Contains(mac.ToString())) { PhysicalAddress macaddr = mac.Reverse(); IPhlpapi.SetIpNetEntry(InterfaceGuid, macaddr, addr); Invoke(new Action(() => UpdateBlacklistItems(mac))); Trace.WriteLine($"Blocked: IP: {addr} MAC: {mac}"); } else { Invoke(new Action(() => UpdateWhitelistItems(mac))); Trace.WriteLine($"Allowed: IP: {addr} MAC: {mac}"); } }
private void OnCellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex.Equals(6)) { string mac = DataGridList.Rows[e.RowIndex].Cells[1].Value as string ?? string.Empty; string ip = DataGridList.Rows[e.RowIndex].Cells[2].Value as string ?? string.Empty; mac = mac.Replace(':', '-'); if (System.Net.IPAddress.TryParse(ip, out System.Net.IPAddress address)) { PhysicalAddress macaddr = PhysicalAddress.Parse(mac); switch ((int)DataGridList.Rows[e.RowIndex].Cells[6].Tag) { case 0: DataGridList.Rows[e.RowIndex].Cells[6].Value = "Block"; DataGridList.Rows[e.RowIndex].Cells[6].Tag = 1; IPhlpapi.SetIpNetEntry(WlanMgr.HostedNetworkGuid, macaddr, address); Trace.WriteLine("Allowed: IP: " + ip + " MAC: " + macaddr.ToString()); do { Properties.Settings.Default.Blacklist.Remove(macaddr.ToString()); }while (Properties.Settings.Default.Blacklist.Contains(macaddr.ToString())); Properties.Settings.Default.Whitelist.Add(macaddr.ToString()); Properties.Settings.Default.Save(); break; case 1: PhysicalAddress bmacaddr = macaddr.Reverse(); DataGridList.Rows[e.RowIndex].Cells[6].Value = "Allow"; DataGridList.Rows[e.RowIndex].Cells[6].Tag = 0; IPhlpapi.SetIpNetEntry(WlanMgr.HostedNetworkGuid, bmacaddr, address); Trace.WriteLine("Blocked: IP: " + ip + " MAC: " + macaddr.ToString()); do { Properties.Settings.Default.Whitelist.Remove(macaddr.ToString()); }while (Properties.Settings.Default.Whitelist.Contains(macaddr.ToString())); Properties.Settings.Default.Blacklist.Add(macaddr.ToString()); Properties.Settings.Default.Save(); break; } } } }
internal void StartHostedNetwork(string ssid, string key, Guid InterfaceGuid) { WLAN_HOSTED_NETWORK_REASON WlanHnkReason; WlanMgr.QueryConnectionSettings(out string SSID, out uint MaxNumberOfPeers); WlanMgr.SetConnectionSettings(ssid, MaxNumberOfPeers); WlanMgr.SetSecondaryKey(key, true, true); switch (WlanMgr.HostedNetworkState) { case WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_active: try { IPhlpapi.FlushIpNetTable(WlanMgr.HostedNetworkGuid); ICSRouter.StopSharing(); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { _ = WlanMgr.ForceStop(); } break; case WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_unavailable: if (!(Advapi32.GetPrivileges() && WlanMgr.AllowHostedNetwork(true))) { MessageBox.Show("The hosted network couldn't be started.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); break; } WlanHnkReason = WlanMgr.ForceStart(); if (WlanHnkReason != 0) { MessageBox.Show(ShowHostedNetworkReason(WlanHnkReason), Text); break; } while (WlanHnkReason == 0 && !WlanMgr.IsHostedNetworkActive) { Thread.Sleep(TimeSpan.FromSeconds(1)); } try { ICSRouter.StartSharing(InterfaceGuid, WlanMgr.HostedNetworkGuid); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } break; case WLAN_HOSTED_NETWORK_STATE.wlan_hosted_network_idle: WlanHnkReason = WlanMgr.ForceStart(); if (WlanHnkReason != 0) { MessageBox.Show(ShowHostedNetworkReason(WlanHnkReason), Text); break; } while (WlanHnkReason == 0 && !WlanMgr.IsHostedNetworkActive) { Thread.Sleep(TimeSpan.FromSeconds(1)); } try { ICSRouter.StartSharing(InterfaceGuid, WlanMgr.HostedNetworkGuid); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } break; } }