private void btnScan_Click_1(object sender, EventArgs e) { if (_hostScanner.IsScanning) { _hostScanner.Cancel(); ChangeScanState(ScanState.Cancel); } else { List <IpV4Address> range; if (rbtnCustomRange.Checked) { try { range = NetworkUtilities.GetIpRange(tbIpRange.Text); } catch (FormatException) { MessageBox.Show("IP range string is invalid.", "Scan Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { range = _networkInfo.SubnetRange; } ChangeScanState(ScanState.Scan); _hostScanner.Scan(range); } }
private NetworkInformation ParseNetworkInformation() { var errorMessage = new Action <string, string>((title, msg) => MetroMessageBox.Show(this, msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error, 120)); IpV4Address gatewayIp; MacAddress gatewayMac; IpV4Address netmask; List <IpV4Address> subnetRange; IntPtr winDivertHandle; if (_currentInterface == null) { errorMessage("Interface Error", "No network interface selected."); return(null); } try { gatewayIp = new IpV4Address(tbGatewayIp.Text); } catch (Exception) { errorMessage("Value Error", "Invalid gateway IP."); return(null); } try { gatewayMac = new MacAddress(tbGatewayMac.Text); } catch (Exception) { errorMessage("Value Error", "Invalid gateway MAC."); return(null); } try { netmask = new IpV4Address(tbNetmask.Text); } catch (Exception) { errorMessage("Value Error", "Invalid netmask."); return(null); } try { subnetRange = NetworkUtilities.GetIpRange(string.Format("{0}/{1}", gatewayIp.ToString(), netmask.ToString())); } catch (Exception) { errorMessage("Value Error", "Invalid netmask."); return(null); } winDivertHandle = WinDivert.WinDivertOpen("true", WinDivertLayer.Forward, 0, WinDivertOpenFlags.None); if (winDivertHandle == new IntPtr(-1)) { errorMessage("WinDivert Error", string.Format("WinDivert handle could not be opened.\nError Code: {0}", Marshal.GetLastWin32Error())); return(null); } return(new NetworkInformation() { Interface = _currentInterface, Communicator = _packetCommunicator, InterfaceAddress = _interfaces[_currentInterface], GatewayIp = gatewayIp, GatewayMac = gatewayMac, Netmask = netmask, SubnetRange = subnetRange, WinDivertHandle = winDivertHandle, }); }