private static IPAddress[] GetLocalAddresses() { IPHostEntry hostAddress = null; // Get all addresses assocatied with this computer try { hostAddress = Dns.GetHostEntry(Dns.GetHostName()); } catch (Exception e) { ConsoleDisplay.Error($"Could not fetch local addresses ({e.GetType().ToString().Split('.').Last()})"); } // Only get IPv4 address List <IPAddress> addresses = new List <IPAddress>(); foreach (IPAddress address in hostAddress.AddressList) { if (address.AddressFamily == AddressFamily.InterNetwork) { addresses.Add(address); } } return(addresses.ToArray()); }
/// <summary> /// Prints and error message and then exits with exit code 1 /// </summary> /// <param name="msg">Error message to print</param> /// <param name="pause">Wait for user input before exitingt</param> public static void ErrorAndExit(string msg) { ConsoleDisplay.Error(msg); if (RequireInput) { Helper.WaitForUserInput(); } Environment.Exit(1); }
public static void ListenForICMPOnAddress(IPAddress address) { Socket listeningSocket = null; PingResults results = new PingResults(); int bufferSize = 4096; // Create listener socket try { listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); listeningSocket.Bind(new IPEndPoint(address, 0)); listeningSocket.IOControl(IOControlCode.ReceiveAll, new byte[] { 1, 0, 0, 0 }, new byte[] { 1, 0, 0, 0 }); // Set SIO_RCVALL flag to socket IO control listeningSocket.ReceiveBufferSize = bufferSize; } catch (Exception e) { ConsoleDisplay.Error($"Exception occured while trying to create listening socket for {address.ToString()} ({e.GetType().ToString().Split('.').Last()})"); return; } ConsoleDisplay.ListenIntroMsg(address.ToString()); // Listening loop while (true) { byte[] buffer = new byte[bufferSize]; try { // Recieve any incoming ICMPv4 packets EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); int bytesRead = listeningSocket.ReceiveFrom(buffer, ref remoteEndPoint); ICMP response = new ICMP(buffer, bytesRead); // Display captured packet ConsoleDisplay.CapturedPacket(address.ToString(), response, remoteEndPoint.ToString(), DateTime.Now.ToString("h:mm:ss.ff tt"), bytesRead); // Store results results.CountPacketType(response.Type); results.IncrementReceivedPackets(); } catch (OperationCanceledException) { } catch (SocketException) { ConsoleDisplay.Error("Could not read packet from socket"); } } listeningSocket.Close(); }
public void Append(string line) { if (_fileStream != null && _fileStream.CanWrite) { _fileStream.Write(_asciiEncoder.GetBytes(line + Environment.NewLine)); try { _fileStream.Flush(); } catch (Exception ex) { ConsoleDisplay.Error($"Error writing to log file ({_filePath})", ex); } } }
public void Create(string filePath) { string?path = ""; try { if (filePath.Contains(Path.DirectorySeparatorChar)) { // Check the directory we want to write to exits path = Path.GetDirectoryName(filePath); if (path != null && !Directory.Exists(path)) { Directory.CreateDirectory(path); } } } catch (Exception e) { ConsoleDisplay.Error($"Cannot create file at {path} changing to {Directory.GetCurrentDirectory()}", e); // Change file to be written to current directory // when we can't create our first directory choice filePath = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(filePath)); } try { // Create the file _fileStream = File.Create(filePath); } catch (Exception ex) { ConsoleDisplay.Error($"Cannot write to log file ({filePath})", ex); _fileStream = null; } }