Files
WOL-Manager/WoL-Manager v0.1/Magic_Packet.cs
T
2017-10-26 20:11:00 +02:00

88 lines
2.0 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace WoL_Manager
{
class Magic_Packet
{
private byte[] data;
private int size;
public Magic_Packet(string mac_adress)
{
size = 135;
data = new byte[6];
for (int t1 = 0; t1 < 6; t1++)
{
data[t1] = byte.Parse(mac_adress.Substring(t1 * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
}
public int getSerializeLength()
{
return size;
}
public int serialize(ref byte[] buffer, int index)
{
if (buffer.Length - index < size)
{
throw new Exception("ERROR:Buffer-Overflow detected");
}
for (int t = 0; t < 6; t++)
{
buffer[index+t] = 0xFF;
}
for (int t = 0; t < 16; t++)
{
Array.Copy(data, 0, buffer, index + 6 + 6 * t, 6);
}
char[] temp = "\0\0Wake on Lan provided by MatzMan".ToCharArray();
for (int t = 0; t < 33; t++)
{
buffer[index + 102 + t] = Convert.ToByte(temp[t]);
}
return size;
}
public string getPayloadType()
{
return "WoL";
}
public static bool sendPacket(Host host)
{
int temp1;
byte[] temp2;
if (!MainWindow.checkMAC(host.MAC))
{
return false;
}
Magic_Packet packet = new Magic_Packet(host.MAC);
temp1 = packet.getSerializeLength();
temp2 = new byte[temp1];
packet.serialize(ref temp2, 0);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw);
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AcceptConnection, false);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
IPEndPoint ipepServer = new IPEndPoint(host.Broadcast, 0);
EndPoint epServer = (ipepServer);
int temp;
for (int t1 = 0; t1 < 3; t1++)
{
temp = socket.SendTo(temp2, epServer);
}
return true;
}
}
}