init

Alphons van der Heijden <alphons@heijden.com> 22 Nov 2023, 13:35
3d092fd5350770d2b53763d8dd0bbffa60411d5a
3 files changed
  • src/EtherWake.csproj
  • src/EtherWake.sln
  • src/Program.cs
diff --git a/src/EtherWake.csproj b/src/EtherWake.csproj
new file mode 100644
index 0000000..2150e37
--- /dev/null
+++ b/src/EtherWake.csproj
@@ -0,0 +1,10 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net8.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+</Project>
diff --git a/src/EtherWake.sln b/src/EtherWake.sln
new file mode 100644
index 0000000..65c55e4
--- /dev/null
+++ b/src/EtherWake.sln
@@ -0,0 +1,25 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.8.34316.72
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EtherWake", "EtherWake.csproj", "{A9608D0A-2160-4BC6-94B8-75CB92AFC5A6}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {A9608D0A-2160-4BC6-94B8-75CB92AFC5A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A9608D0A-2160-4BC6-94B8-75CB92AFC5A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A9608D0A-2160-4BC6-94B8-75CB92AFC5A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A9608D0A-2160-4BC6-94B8-75CB92AFC5A6}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {CBD52815-7D5E-443C-84ED-AA68DDB5B13B}
+ EndGlobalSection
+EndGlobal
diff --git a/src/Program.cs b/src/Program.cs
new file mode 100644
index 0000000..89ac079
--- /dev/null
+++ b/src/Program.cs
@@ -0,0 +1,48 @@
+using System.Net;
+using System.Net.Sockets;
+
+/*
+ * Author: Alphons van der Heijden
+ * Date: nov 11, 2023
+ * Broadcasting WOL (Wake-On-Lan) packet 0x0842 https://en.wikipedia.org/wiki/EtherType
+ * Payload is mac address
+ *
+ * .net core 8
+ */
+
+var Mac = "DE:AD:BE:EF:12:34";
+
+if(args.Length == 0)
+{
+ Console.WriteLine($"Usage: EtherWake {Mac}");
+ return;
+}
+
+if (args.Length>0)
+ Mac = args[0];
+
+var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+
+await client.ConnectAsync(IPAddress.Broadcast, 0); // random port
+
+client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
+
+byte[] broadcast = [ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff ];
+
+var macaddress = Mac.Split(':').Select(x => Convert.ToByte(x, 16)).ToArray();
+
+byte[] ethertype = [ 0x08, 0x42 ];
+
+var arrays = new List<byte[]> { broadcast, macaddress, ethertype, broadcast };
+
+for(int i=0;i<16;i++)
+ arrays.Add(macaddress);
+
+var buffer = arrays.SelectMany(s => s).ToArray();
+
+int len = await client.SendAsync(buffer);
+
+var success = len == buffer.Length;
+
+Console.WriteLine($"sended WOL {success}");
+