Skip to content

Commit

Permalink
add better logging of network devices when sending WOL
Browse files Browse the repository at this point in the history
  • Loading branch information
Maassoft committed Feb 5, 2021
1 parent 91fd44d commit 5268e66
Show file tree
Hide file tree
Showing 25 changed files with 1,915 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ColorControl/ColorControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<ProductName>ColorControl</ProductName>
<PublisherName>Maassoft</PublisherName>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.5.3.0</ApplicationVersion>
<ApplicationVersion>1.5.4.0</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
Expand Down
4 changes: 2 additions & 2 deletions ColorControl/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.3.0")]
[assembly: AssemblyFileVersion("1.5.3.0")]
[assembly: AssemblyVersion("1.5.4.0")]
[assembly: AssemblyFileVersion("1.5.4.0")]
19 changes: 14 additions & 5 deletions ColorControl/WOL.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using NLog;
using System;
using System.Globalization;
using PacketDotNet;
using System.Net.NetworkInformation;
using SharpPcap;
using System.Diagnostics;
using SharpPcap.Npcap;
using System.Linq;
using System.Net;

namespace ColorControl
{
class WOL
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

//You need SharpPcap for this to work
public static void WakeFunction2(string macAddress)
{
//PhysicalAddress.Parse(macAddress).SendWol();
}

//You need SharpPcap for this to work
public static void WakeFunction(string macAddress)
{
/* Retrieve the device list */
Expand All @@ -40,7 +42,14 @@ public static void WakeFunction(string macAddress)
// }
//}

Logger.Debug("Opening network device: " + device.Name);
if (device is NpcapDevice npcapDevice)
{
Logger.Debug($"Opening network device (NpcapDevice): FriendlyName: {npcapDevice.Interface.FriendlyName}, Description: {npcapDevice.Interface.Description}");
}
else
{
Logger.Debug($"Opening network device (NOT A NpcapDevice!): {device.Name}, Description: {device.Description}");
}

//Open the device
device.Open();
Expand Down
39 changes: 39 additions & 0 deletions WakeOnLan/ArpRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace System.Net
{
/// <summary>Stellt Methoden für das Senden von Anfragen über das ARP-Protokoll bereit.</summary>
public static class ArpRequest
{
/// <summary>
/// Sendet eine Anfrage über das ARP-Protokoll, um eine IP-Adresse in die Physikalische Adresse aufzulösen. Falls sich die physikalische Adresse bereits im Cache des Hosts befindet, wird diese zurückgegeben.
/// </summary>
/// <param name="destination">Destination <see cref="IPAddress"/>.</param>
/// <returns>Eine <see cref="T:System.Net.ArpRequestResult">ArpRequestResult</see>-Instanz, welche die Ergebnisse der Anfrage enthält.</returns>
public static ArpRequestResult Send(IPAddress destination)
{
if (destination == null)
throw new ArgumentNullException(nameof(destination));

int destIp = BitConverter.ToInt32(destination.GetAddressBytes(), 0);

var addr = new byte[6];
var len = addr.Length;

var res = NativeMethods.SendARP(destIp, 0, addr, ref len);

if (res == 0)
return new ArpRequestResult(new PhysicalAddress(addr));
return new ArpRequestResult(new Win32Exception(res));
}

/// <summary>
/// Sendet eine Anfrage über das ARP-Protokoll, um eine IP-Adresse in die Physikalische Adresse aufzulösen. Falls sich die physikalische Adresse bereits im Cache des Hosts befindet, wird diese zurückgegeben.
/// </summary>
/// <param name="destination">Destination <see cref="IPAddress"/>.</param>
/// <returns>Ein asynchroner Task, welcher einen ARP-Request sendet.</returns>
public static Task<ArpRequestResult> SendAsync(IPAddress destination) => Task.Run(() => Send(destination));
}
}
57 changes: 57 additions & 0 deletions WakeOnLan/ArpRequestResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Net.NetworkInformation;
using System.Text;

namespace System.Net
{
// TODO: rethink the whole exception thing

/// <summary>
/// Enthält die Rückgabewerte der ArpRequest.Send-Funktion.
/// </summary>
public class ArpRequestResult
{
/// <summary>Falls Fehler bei der Protokollanfrage auftreten, werden diese in dieser Eigenschaft abgelegt. Andernfalls null.</summary>
public Exception Exception { get; }

/// <summary>Die aufgelöste physikalische Adresse.</summary>
public PhysicalAddress Address { get; }

/// <summary>Erstellt eine neue ArpRequestResult-Instanz</summary>
/// <param name="address">Die physikalische Adresse</param>
public ArpRequestResult(PhysicalAddress address)
{
this.Exception = null;
Address = address;
}

/// <summary>Erstellt eine neue ArpRequestResult-Instanz</summary>
/// <param name="exception">Der aufgetretene Fehler</param>
public ArpRequestResult(Exception exception)
{
this.Exception = exception;
Address = null;
}

/// <summary>Konvertiert ARP-Rückgabewerte in eine Zeichenfolge.</summary>
public override string ToString()
{
var sb = new StringBuilder();
if (Address == null)
sb.Append("no address");
else
{
sb.Append("address: ");
sb.Append(Address);
}
sb.Append(", ");
if (Exception == null)
sb.Append("no exception");
else
{
sb.Append("exception: ");
sb.Append(Exception.Message);
}
return sb.ToString();
}
}
}
Loading

0 comments on commit 5268e66

Please sign in to comment.