|
|
@ -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<Task> 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<ISPChkContext>(); |
|
|
|
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,8 +100,9 @@ namespace ISPChk |
|
|
|
int failures = 0; |
|
|
|
for (var i = 0; i < numPings; ++i) |
|
|
|
{ |
|
|
|
try { |
|
|
|
// Send the request.
|
|
|
|
PingReply reply = pingSender.Send(host.HostName, timeout, buffer, options); |
|
|
|
PingReply reply = pingSender.Send(pingAddress, timeout, buffer, options); |
|
|
|
|
|
|
|
if (reply.Status == IPStatus.Success) |
|
|
|
{ |
|
|
@ -99,6 +127,10 @@ namespace ISPChk |
|
|
|
System.Diagnostics.Debug.WriteLine(reply.Status); |
|
|
|
failures++; |
|
|
|
} |
|
|
|
} catch(Exception e) { |
|
|
|
System.Diagnostics.Debug.WriteLine("Found exception while pinging"+e); |
|
|
|
failures++; |
|
|
|
} |
|
|
|
Thread.Sleep(500); |
|
|
|
} |
|
|
|
if(successes > 0) |
|
|
|