Nikeee Wake On Lan Save

Sending magic packets and performing IP address operations.

Project README

Wake-On-LAN Matrix Build Status NuGet version

A simple library for sending magic packets and performing IP address operations. The default namespace is System.Net. There is an online documentation available here.

Samples

Sending a Magic Packet

This sample uses 00:11:22:33:44:55 as MAC address.

using System.Net;
// ...

// Using the IPAddess extension
IPAddress.Broadcast.SendWol(0x00, 0x11, 0x22, 0x33, 0x44, 0x55);

// via core MagicPacket class
var endPoint = new IPEndPoint(IPAddress.Broadcast, 7); // You don't have to use Broadcast.
                                                       // Every IP/port-combination is possible.
MagicPacket.Send(endPoint, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55);

// via IPEndPoint extension
endPoint.SendWol(0x00, 0x11, 0x22, 0x33, 0x44, 0x55);

// ...
using System.Net.NetworkInformation;
PhysicalAddress.Parse("00-11-22-33-44-55").SendWol();

Getting Subnet Information

You can also retrieve information about a subnet.

using System.Net;
using System.Net.Topology;
// ...

var someIp = new IPAddress(new byte[] { 192, 168, 1, 23 }); // Some IP address in the subnet
var mask = new NetMask(255, 255, 255, 0); // the network mask of the subnet

// CIDR-notation number of the network mask
int cidr = mask.Cidr;

var networkPrefix = someIp & mask; // bitwise operation to get the network address (192.168.1.0)
networkPrefix = someIp.GetNetworkPrefix(mask); // using the extension method for IPAddress

// retrieve broadcast address of the subnet (192.168.1.255)
var broadcastAddress = someIp.GetBroadcastAddress(mask);

IEnumerable<IPAddress> siblings = someIp.GetSiblings(mask, SiblingOptions.ExcludeUnusable);
// Enumerate through all IP addresses in the subnet, except network prefix and broadcast (RFC 950, 2^n-2)
foreach (IPAddress someIpInNetwork in siblings)
{
    Console.WriteLine(someIpInNetwork.ToString());
}

// Get number of possible siblings without someIp, broadcast and network prefix
int siblingCount = mask.GetSiblingCount(SiblingOptions.ExcludeAll);

ARP Requests

To retrieve the MAC address of a host, there is a functionality for ARP-request built-in. It uses the Windows API method SendArp.

ArpRequestResult res = ArpRequest.Send(someIp);
if(res.Exception != null)
{
    Console.WriteLine("ARP error occurred: " + res.Exception.Message);
}
else
{
    Console.WriteLine($"Host MAC address: {res.Address}");
}

Note that there isn't always an MAC address available although there is a host. The reason for this could be the host is offline and/or the physical address is not cached somewhere. Also, this function uses a p/invoke. This might cause problems when used on platforms other than Windows.

Async/Await

This library also supports the Task-based Asynchronous Pattern (TAP). Every method that can send a magic packet synchronously is available as a TAP method returning a Task.

await IPAddress.Broadcast.SendWolAsync(0x00, 0x11, 0x22, 0x33, 0x44, 0x55);

Further Samples

The System.Net.NetworkInformation.PhysicalAddress class is also supported as it represents a MAC address.

var mac = new PhysicalAddress(new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55});
mac.SendWol(); // via extension method

Install

Install the NuGet package of this library:

# NuGet CLI
Install-Package WakeOnLan
# dotnet CLI
dotnet add package WakeOnLAN

Compatibility

Version 2+ will be available for .NET Standard only. Version 1 supports the following platforms:

  • .NET 2.0 (does not include extension methods and async features)
  • .NET 3.5 Client Profile (does not include async features)
  • .NET 4.0 Client Profile (does not include async features)
  • .NET 4.5
  • .NET 4.5.1

To install a version <2, have a look at all the available versions of the NuGet package and install a specific version. For example:

# NuGet CLI
Install-Package WakeOnLAN -Version 1.6.0
# dotnet CLI
dotnet add package --version 1.6.0 WakeOnLAN
Open Source Agenda is not affiliated with "Nikeee Wake On Lan" Project. README Source: nikeee/wake-on-lan
Stars
58
Open Issues
5
Last Commit
5 months ago
Repository

Open Source Agenda Badge

Open Source Agenda Rating