Skip to content

Commit 1b2ded3

Browse files
authored
Merge pull request #19 from NikVanT/EndpointMapper_Scan
[Suggestion] ping, add choice between IPv4 and IPv6 #15
2 parents ff97493 + 1cb1cdb commit 1b2ded3

File tree

1 file changed

+123
-16
lines changed

1 file changed

+123
-16
lines changed

ADReplStatusForm.cs

Lines changed: 123 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System.IO;
1818
using System.Reflection;
1919
using System.Threading;
20+
using System.Net.Sockets;
21+
using System.Net;
2022

2123
namespace ADReplStatus
2224
{
@@ -538,37 +540,142 @@ private void diagnosticMenuSelector(object sender, ToolStripItemClickedEventArgs
538540
private void diagnosticPing(object sender, ToolStripItemClickedEventArgs e)
539541
{
540542
string destination = this.treeListView1.SelectedItem.Text;
541-
542-
if(destination != "")
543+
544+
if (destination != "")
543545
{
544-
try
546+
using (var dialog = new Form())
545547
{
546-
Ping p = new Ping();
547-
PingReply r;
548-
r = p.Send(destination);
549-
if (gLoggingEnabled)
548+
//Set up the ping test window
549+
dialog.Text = "Ping Test";
550+
dialog.StartPosition = FormStartPosition.CenterParent;
551+
dialog.MaximizeBox = false;
552+
dialog.MinimizeBox = false;
553+
dialog.FormBorderStyle = FormBorderStyle.FixedDialog;
554+
dialog.ShowInTaskbar = false;
555+
dialog.Width = 290;
556+
dialog.Height = 150;
557+
558+
var ipv4Button = new Button();
559+
ipv4Button.Text = "IPv4";
560+
ipv4Button.Location = new Point(10, 20);
561+
ipv4Button.Click += (s, ev) => RunPing(destination, AddressFamily.InterNetwork, dialog);
562+
563+
var ipv6Button = new Button();
564+
ipv6Button.Text = "IPv6";
565+
ipv6Button.Location = new Point(180, 20);
566+
ipv6Button.Click += (s, ev) => RunPing(destination, AddressFamily.InterNetworkV6, dialog);
567+
568+
var statusTextBox = new TextBox();
569+
statusTextBox.Multiline = true;
570+
statusTextBox.ReadOnly = true;
571+
statusTextBox.Location = new Point(10, 60);
572+
statusTextBox.Width = dialog.Width - 45;
573+
statusTextBox.Height = dialog.Height - 110;
574+
statusTextBox.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
575+
576+
dialog.Controls.Add(ipv4Button);
577+
dialog.Controls.Add(ipv6Button);
578+
dialog.Controls.Add(statusTextBox);
579+
580+
//Add support for dark mode
581+
if (ADReplStatusForm.gDarkMode == true)
550582
{
551-
System.IO.File.AppendAllText(gLogfileName, $"[{DateTime.Now}] Ping to {this.treeListView1.SelectedItem.Text}\nIPAddress:{r.Address.ToString()} is successful!\n");
583+
dialog.BackColor = Color.FromArgb(32, 32, 32);
584+
foreach (var control in dialog.Controls)
585+
{
586+
if (control is Label)
587+
{
588+
((Label)control).BackColor = Color.FromArgb(32, 32, 32);
589+
((Label)control).ForeColor = Color.White;
590+
}
591+
else if (control is TextBox)
592+
{
593+
((TextBox)control).BackColor = Color.FromArgb(32, 32, 32);
594+
((TextBox)control).ForeColor = Color.White;
595+
}
596+
else if (control is Button)
597+
{
598+
((Button)control).BackColor = Color.FromArgb(32, 32, 32);
599+
((Button)control).ForeColor = Color.White;
600+
}
601+
else if (control is CheckBox)
602+
{
603+
((CheckBox)control).BackColor = Color.FromArgb(32, 32, 32);
604+
((CheckBox)control).ForeColor = Color.White;
605+
}
606+
else if (control is RadioButton)
607+
{
608+
((RadioButton)control).BackColor = Color.FromArgb(32, 32, 32);
609+
((RadioButton)control).ForeColor = Color.White;
610+
}
611+
else if (control is ListBox)
612+
{
613+
((ListBox)control).BackColor = Color.FromArgb(32, 32, 32);
614+
((ListBox)control).ForeColor = Color.White;
615+
}
616+
}
552617
}
553618

554-
string successMessage = $"Success:\nDCName: {this.treeListView1.SelectedItem.Text}\nIPAddress: {r.Address.ToString()}\nProtocol: ICMP";
555-
556-
new Thread(() => System.Windows.Forms.MessageBox.Show(successMessage, "Ping Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)).Start();
619+
dialog.ShowDialog(this);
557620
}
558-
catch (Exception ex)
621+
}
622+
}
623+
624+
625+
private async void RunPing(string destination, AddressFamily addressFamily, Form dialog)
626+
{
627+
try
628+
{
629+
IPAddress address;
630+
if (!IPAddress.TryParse(destination, out address))
559631
{
560-
string errorMessage = $"ERROR: Ping to {this.treeListView1.SelectedItem.Text} failed!\n{ex.Message}\n";
632+
var entry = await Dns.GetHostEntryAsync(destination);
633+
address = entry.AddressList.FirstOrDefault(a => a.AddressFamily == addressFamily);
634+
if (address == null)
635+
{
636+
throw new Exception($"No {addressFamily} address found for {destination}");
637+
}
638+
}
561639

562-
new Thread(() => System.Windows.Forms.MessageBox.Show(errorMessage, "Ping Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)).Start();
640+
using (var p = new Ping())
641+
{
642+
var reply = await p.SendPingAsync(address, 5000, new byte[1], new PingOptions(64, true));
643+
if (reply.Status == IPStatus.Success)
644+
{
645+
string protocol = addressFamily == AddressFamily.InterNetwork ? "IPv4" : "IPv6";
646+
string successMessage = $"Success:\nDCName: {destination} ({reply.Address.ToString()})\nProtocol: {protocol}";
647+
var statusTextBox = (TextBox)dialog.Controls[2];
648+
statusTextBox.Clear();
649+
statusTextBox.AppendText($"Ping to {destination} using {protocol} ({reply.Address.ToString()}) successful.\n");
563650

564-
if (gLoggingEnabled)
651+
if (gLoggingEnabled)
652+
{
653+
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {statusTextBox.Text}");
654+
}
655+
}
656+
else
565657
{
566-
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}\n");
658+
throw new Exception(reply.Status.ToString());
567659
}
568660
}
569661
}
662+
catch (Exception ex)
663+
{
664+
dialog.Invoke(new Action(() =>
665+
{
666+
string errorMessage = $"Ping failed!\n{ex.Message}\n";
667+
var statusTextBox = (TextBox)dialog.Controls[2];
668+
statusTextBox.Clear();
669+
statusTextBox.AppendText($"{errorMessage}\n");
670+
if (gLoggingEnabled)
671+
{
672+
System.IO.File.AppendAllText(ADReplStatusForm.gLogfileName, $"[{DateTime.Now}] {errorMessage}");
673+
}
674+
}));
675+
}
570676
}
571677

678+
572679
private void diagnosticRdp(object sender, ToolStripItemClickedEventArgs e)
573680
{
574681
try

0 commit comments

Comments
 (0)