Giter Club home page Giter Club logo

wake-on-lan's Introduction

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

wake-on-lan's People

Contributors

johnseed avatar nikeee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

wake-on-lan's Issues

Release new version

Hey

I would like to use this library on a .net core 3 console application that can run on any os.
Unfortunately, the latest version on nuget is 6 year old and is targeting the full .net framework.
Is it possible that you release a new version of your really useful library (it could be an alpha or beta version if you want...)?

ARP Requests are Windows-only

Trying to execute them on linux results in the following exception:

System.DllNotFoundException: Unable to load shared library 'iphlpapi.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libiphlpapi.dll: cannot open shared object file: No such file or directory
   at System.Net.NativeMethods.SendARP(Int32 destinationIp, Int32 sourceIp, Byte[] macAddress, Int32& physicalAddrLength)
   at System.Net.ArpRequest.Send(IPAddress destination)
   at System.Net.ArpRequest.<>c__DisplayClass1_0.<SendAsync>b__0()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at WoL.Services.AddressLookupService.GetMac(IPAddress ip)
   at WoL.Pages.AddHost.GetHostByName()
   at WoL.Pages.AddHost.GetHost()
   at WoL.Pages.AddHost.HandleValidSubmit(EditContext context)

While I understand why this is the case it might be nice to provide a different solution for non-windows platforms or at least throwing a platform not supported exception.

MagicPacket.Send invalid method signature

Not sure if it's related to #10 but that doesn't works, byte[] is not a params byte.

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);
            IPAddress.Broadcast.SendWol(0x00, 0x11, 0x22, 0x33, 0x44, 0x55);

If the #10 is about this, just close this issue.

Stackoverflow

Stack overflow.
Repeat 24082 times:

at System.Net.MagicPacket.Send(System.Net.IPEndPoint, System.Net.NetworkInformation.PhysicalAddress)
image
#17

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.