-
Notifications
You must be signed in to change notification settings - Fork 0
/
WoLWake.cs
118 lines (102 loc) · 4.05 KB
/
WoLWake.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Net.Sockets;
namespace WoLib
{
public class WoLWake
{
public EthernetMacAddress device_mac_address { get; private set; }
public byte[] magic_packet {get; private set;}
public string ip { get; set; }
public string broadcast_address { get; private set; }
//Constructor with EthernetMacAddress object
public WoLWake(string ip, EthernetMacAddress device_mac_address)
{
this.device_mac_address = device_mac_address;
this.ip = ip;
broadcast_address = IpToBraodcastAddress(this.ip);
magic_packet = new byte[102];
//Fill in six times 0xFF
for (int i = 0; i < 6; i++)
{
magic_packet[i] = 0xff;
}
//Fill in 16x ethernet mac address
for (int i = 0; i < 16; i++)
{
magic_packet[i * 6 + 6] = device_mac_address.mac_address[0];
magic_packet[i * 6 + 7] = device_mac_address.mac_address[1];
magic_packet[i * 6 + 8] = device_mac_address.mac_address[2];
magic_packet[i * 6 + 9] = device_mac_address.mac_address[3];
magic_packet[i * 6 + 10] = device_mac_address.mac_address[4];
magic_packet[i * 6 + 11] = device_mac_address.mac_address[5];
}
}
//Constructor with mac address as byte array
public WoLWake(string ip, byte[] hex_address)
{
if (hex_address.Length > 6)
{
throw new Exception("Hex address too long! Has to be six bytes!");
}
else if (hex_address.Length < 6)
{
throw new Exception("Hex address too short! Has to be six bytes!");
}
device_mac_address = new EthernetMacAddress(hex_address);
this.ip = ip;
broadcast_address = IpToBraodcastAddress(this.ip);
magic_packet = new byte[102];
//Fill in six times 0xFF
for (int i = 0; i < 6; i++)
{
magic_packet[i] = 0xff;
}
//Fill in 16x ethernet mac address
for (int i = 0; i < 16; i++)
{
magic_packet[i * 6 + 6] = hex_address[0];
magic_packet[i * 6 + 7] = hex_address[1];
magic_packet[i * 6 + 8] = hex_address[2];
magic_packet[i * 6 + 9] = hex_address[3];
magic_packet[i * 6 + 10] = hex_address[4];
magic_packet[i * 6 + 11] = hex_address[5];
}
}
//Constructor with mac address as single bytes
public WoLWake(string ip, byte a, byte b, byte c, byte d, byte e, byte f)
{
device_mac_address = new EthernetMacAddress(a,b,c,d,e,f);
this.ip = ip;
broadcast_address = IpToBraodcastAddress(this.ip);
magic_packet = new byte[102];
//Fill in six times 0xFF
for (int i = 0; i < 6; i++)
{
magic_packet[i] = 0xff;
}
//Fill in 16x ethernet mac address
for (int i = 0; i < 16; i++)
{
magic_packet[i * 6 + 6] = a;
magic_packet[i * 6 + 7] = b;
magic_packet[i * 6 + 8] = c;
magic_packet[i * 6 + 9] = d;
magic_packet[i * 6 + 10] = e;
magic_packet[i * 6 + 11] = f;
}
}
public void Wake()
{
UdpClient s = new UdpClient(broadcast_address, 49153);
s.Send(magic_packet, magic_packet.Length);
s.Close();
}
private string IpToBraodcastAddress(string ip)
{
string broadcast_address = "";
int last_dot = ip.LastIndexOf(".");
broadcast_address = ip.Remove(last_dot + 1) + "255";
return broadcast_address;
}
}
}