public void SendICMPPing(string Address, int Timeout) { PingDataStructure pingDataStructure = new PingDataStructure(); if (!string.IsNullOrEmpty(Address)) { var oof = GetHosts(Address); pingDataStructure.FoundIPV4 = oof.FoundIPV4; pingDataStructure.FoundIPV6 = oof.FoundIPV6; PingReply pingReply = sendPing(Address, Timeout); if (pingReply != null) { pingDataStructure.SourceAddress = GetIPAddresses.GetLocalAddress; pingDataStructure.Protocol = "ICMP"; pingDataStructure.Destination = Address; pingDataStructure.Data = $"{pingReply.Buffer.Length.ToString()} Bytes"; pingDataStructure.Time = $"{pingReply.RoundtripTime} ms"; if (pingReply.Status == IPStatus.Success) { pingDataStructure.SuccessOrUnsuccess = "Success"; } else { pingDataStructure.SuccessOrUnsuccess = "Failed"; } } else { return; } } OnStatusMessage?.Invoke(pingDataStructure); }
public void SendUDPPing(string Address, int Timeout, int Port) { PingDataStructure pds = new PingDataStructure(); var oof = GetHosts(Address); pds.FoundIPV4 = oof.FoundIPV4; pds.FoundIPV6 = oof.FoundIPV6; pds.SuccessOrUnsuccess = "Doesn't work yet :("; Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); Stopwatch sw = new Stopwatch(); sw.Start(); s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, true); try { s.BeginConnect(Address, Port, null, null).AsyncWaitHandle.WaitOne(Timeout); } catch (Exception gg) { ITMessageBox.Show(gg.Message, "Err sending UDP Ping"); } sw.Stop(); //pds.SuccessOrUnsuccess = hasConnected ? "Success" : "Failed"; pds.SourceAddress = GetIPAddresses.GetLocalAddress; pds.Protocol = $"UDP -> {Port}"; pds.Destination = Address; pds.Time = sw.Elapsed.TotalMilliseconds.ToString() + " ms"; pds.Data = $"{s.SendBufferSize.ToString()} Bytes"; OnStatusMessage?.Invoke(pds); }
private void SendArpRequest(IPAddress dst) { byte[] macAddr = new byte[6]; uint macAddrLen = (uint)macAddr.Length; //int uintAddress = BitConverter.ToInt32(dst.GetAddressBytes(), 0); Task.Run(() => { sw.Start(); }); int result = SendARP(BitConverter.ToInt32(dst.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen);; Task.Run(() => { sw.Stop(); }); PingDataStructure pds = new PingDataStructure(); pds.SourceAddress = GetIPAddresses.GetLocalAddress; pds.Data = $"42 Bytes"; pds.Protocol = "TCP"; pds.Destination = dst.ToString(); pds.Time = $"{sw.ElapsedMilliseconds} ms"; if (result == 0) { pds.SuccessOrUnsuccess = "Success"; Task.Run(() => { sw.Restart(); }); } else { pds.SuccessOrUnsuccess = "Failed"; } OnStatusMessage?.Invoke(pds); }
public PingDataStructure GetHosts(string address) { PingDataStructure pingDataStructure = new PingDataStructure(); Task t = Task.Run(() => { HostnameIdentifierM hi = new HostnameIdentifierM(); HostnameIdentifierDataStructure hids = hi.GetHostnames(address); pingDataStructure.FoundIPV4 = hids.IPV4Address; pingDataStructure.FoundIPV6 = hids.IPV6Address; return(Task.CompletedTask); }); while (!t.IsCompleted) { Thread.Sleep(1); } return(pingDataStructure); }
public void SendTCPPing(string Address, int Timeout, int Port) { Task.Run(() => { if (!string.IsNullOrEmpty(Address)) { PingDataStructure pds = new PingDataStructure(); var oof = GetHosts(Address); pds.FoundIPV4 = oof.FoundIPV4; pds.FoundIPV6 = oof.FoundIPV6; Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sock.SendBufferSize = 32; sock.Blocking = true; Stopwatch stopwatch = new Stopwatch(); // Measure the Connect call only stopwatch.Start(); //IAsyncResult results = sock.BeginConnect(address, 80, null, null); bool successs = false; try { successs = sock.BeginConnect(Address, Port, null, null).AsyncWaitHandle.WaitOne(Timeout); } catch { } stopwatch.Stop(); pds.SuccessOrUnsuccess = successs ? "Success" : "Failed"; pds.SourceAddress = GetIPAddresses.GetLocalAddress; pds.Data = $"{sock.SendBufferSize.ToString()} Bytes"; pds.Protocol = $"TCP -> {Port}"; pds.Destination = Address; pds.Time = $"{stopwatch.Elapsed.TotalMilliseconds} ms"; OnStatusMessage?.Invoke(pds); } }); }