diff --git a/ISPChk/Controllers/HostController.cs b/ISPChk/Controllers/HostController.cs index bcb2fc4..01e3f32 100644 --- a/ISPChk/Controllers/HostController.cs +++ b/ISPChk/Controllers/HostController.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using ISPChk.Models; +using System.Net; namespace ISPChk.Controllers { @@ -100,6 +101,23 @@ namespace ISPChk.Controllers [HttpPost] public async Task> PostHost(Host host) { + IPAddress ipAddr; + if (!IPAddress.TryParse(host.HostName, out ipAddr)) + { + try { + IPHostEntry hostInfo = Dns.GetHostEntry(host.HostName); + } catch(System.Net.Sockets.SocketException e) + { + return BadRequest(new { errmsg = "Could not resolve hostname." }); + } + } else + { + if(ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && host.IPv6 == true || + ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 && host.IPv6 == false) + { + return BadRequest(new { errmsg = "Given IP was not of correct family." }); + } + } _context.Hosts.Add(host); await _context.SaveChangesAsync(); diff --git a/ISPChk/Models/Host.cs b/ISPChk/Models/Host.cs index c7b926c..32e9690 100644 --- a/ISPChk/Models/Host.cs +++ b/ISPChk/Models/Host.cs @@ -10,6 +10,7 @@ namespace ISPChk.Models public long HostId { get; set; } public string Name { get; set; } public string HostName { get; set; } + public bool IPv6 { get; set; } public bool Ping { get; set; } public bool TCP { get; set; } diff --git a/ISPChk/NetworkTest.cs b/ISPChk/NetworkTest.cs index e16bdb1..ac0dc1b 100644 --- a/ISPChk/NetworkTest.cs +++ b/ISPChk/NetworkTest.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Threading; @@ -13,9 +14,11 @@ namespace ISPChk public class NetworkTest : INetworkTest { private ISPChkContext _context; + private List runningTasks; public NetworkTest() { _context = CreateDbContext(); + System.Diagnostics.Debug.WriteLine("NetworkTest instantiated."); } public async void LoadHostsFromDatabase() @@ -32,7 +35,7 @@ namespace ISPChk var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlite("Data Source=ispchk.db"); var ctx = new ISPChkContext(optionsBuilder.Options); - System.Diagnostics.Debug.WriteLine("NetworkTest instantiated."); + System.Diagnostics.Debug.WriteLine("DbContext created."); ctx.Database.EnsureCreated(); return ctx; } @@ -46,6 +49,30 @@ namespace ISPChk var h = await ctx.Hosts.FindAsync(host.HostId); ctx.Entry(h).Collection(hst => hst.PingItems).Load(); + IPAddress pingAddress; + + + if (!IPAddress.TryParse(h.HostName, out pingAddress)) + { + System.Diagnostics.Debug.WriteLine("Resolving "+h.HostName); + IPHostEntry hostInfo = Dns.GetHostEntry(h.HostName); + foreach(var a in hostInfo.AddressList) + { + if((a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 && h.IPv6 == true) || + a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && h.IPv6 == false) + { + pingAddress = a; + break; + } + } + } + + if(pingAddress == null) + { + System.Diagnostics.Debug.WriteLine("No address to ping found..."); + return; + } + Ping pingSender = new Ping(); // Create a buffer of 32 bytes of data to be transmitted. @@ -61,7 +88,7 @@ namespace ISPChk // cannot be fragmented. PingOptions options = new PingOptions(64, true); - System.Diagnostics.Debug.WriteLine("Pinging " + host.Name); + System.Diagnostics.Debug.WriteLine("Initiate ping to " + pingAddress); while (true) { @@ -73,30 +100,35 @@ namespace ISPChk int failures = 0; for (var i = 0; i < numPings; ++i) { - // Send the request. - PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); + try { + // Send the request. + PingReply reply = pingSender.Send(pingAddress, timeout, buffer, options); - if (reply.Status == IPStatus.Success) - { - successes++; - //System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() + - // ": bytes=" + reply.Buffer.Length + - // " time=" + reply.RoundtripTime + "ms"); - avg += reply.RoundtripTime; - if (reply.RoundtripTime < min) + if (reply.Status == IPStatus.Success) { - min = reply.RoundtripTime; + successes++; + //System.Diagnostics.Debug.WriteLine("Reply from " + reply.Address.ToString() + + // ": bytes=" + reply.Buffer.Length + + // " time=" + reply.RoundtripTime + "ms"); + avg += reply.RoundtripTime; + if (reply.RoundtripTime < min) + { + min = reply.RoundtripTime; + } + if (reply.RoundtripTime > max) + { + max = reply.RoundtripTime; + } + //System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl); + //System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment); } - if (reply.RoundtripTime > max) + else { - max = reply.RoundtripTime; + System.Diagnostics.Debug.WriteLine(reply.Status); + failures++; } - //System.Diagnostics.Debug.WriteLine("Time to live: {0}", reply.Options.Ttl); - //System.Diagnostics.Debug.WriteLine("Don't fragment: {0}", reply.Options.DontFragment); - } - else - { - System.Diagnostics.Debug.WriteLine(reply.Status); + } catch(Exception e) { + System.Diagnostics.Debug.WriteLine("Found exception while pinging"+e); failures++; } Thread.Sleep(500); diff --git a/ISPChk/wwwroot/index.html b/ISPChk/wwwroot/index.html index 2a30908..c095a0e 100644 --- a/ISPChk/wwwroot/index.html +++ b/ISPChk/wwwroot/index.html @@ -22,6 +22,11 @@ +
+ + +
+
@@ -55,7 +60,7 @@
- +
diff --git a/ISPChk/wwwroot/ispchk.js b/ISPChk/wwwroot/ispchk.js index 7bdcb0e..1d82360 100644 --- a/ISPChk/wwwroot/ispchk.js +++ b/ISPChk/wwwroot/ispchk.js @@ -222,11 +222,13 @@ $("#frmHostAdd").submit((event) => { event.preventDefault(); var name = $("#inpNameAdd").val(); var hostname = $("#inpHostNameAdd").val(); - var ping = $('#chkPing').prop('checked') - var tcp = $('#chkPing').prop('checked') + var ipv6 = $('#chkIPv6').prop('checked'); + var ping = $('#chkPing').prop('checked'); + var tcp = $('#chkPing').prop('checked'); var host = { name: name, hostname: hostname, + ipv6: ipv6, ping: ping, tcp: tcp }