One of the task I have is to work with Intel’s vPro technology. One of the many capabilities is to turn on, off or reset computers. The ability to remotely turn on a computer has been the Watch on LAN,WOL, found on some computers. While the vPro technology provides the power management functionality, WOL only allows you to boot a machine.

The booting of the machine using WOL is fairly simple. The properly equipped computer listens for what is called a ‘Magic Packet’ usually on port 0, 7 or 9. If the ‘Magic Packet’ is targeted for the computer it boots up. What is the ‘Magic Packet’?

It is a broadcast frame that starts with the hexadecimal sequence: FF FF FF FF FF FF. After that the target computer’s mac Address is repeated 16 times in hexadecimal format or it’s equivalent.

There are lots of examples of how to do this in python, perl, php, bat script, VB, C and others. So this is how I did it in C#:

public class WakeOnLan {
/// <summary>
/// Wake on LAN is a power on management that will turn on
/// a computer if the computer is setup properly.
/// </summary>
/// <param name=”macAddress”></param>
public static void WoL(string macAddress) {
try {
UdpClient udpClient = new UdpClient();
udpClient.Connect(IPAddress.Broadcast, 40000);
byte[] magicPacket = generateMagicPacket(macAddress);
udpClient.Send(magicPacket, magicPacket.Length);
} catch (Exception e) {}
}

/// <summary>
/// The MagicPacket is the packet configuration that is specified
/// to trigger a computer on if it is set up for Wake On LAN. The
/// first six bytes are FF FF FF FF FF FF followed by 16 repeats of
/// the mac Address of the target computer.
/// </summary>
/// <param name=”macAddress”></param>
/// <returns></returns>
private static byte[] generateMagicPacket(string macAddress) {
byte[] magicPacket = new byte[17 * 6];
// Create magicPacket prefix
for(int i = 0; i < 6; i++) {
magicPacket[i] = 0xFF;
}
byte[] macAddressBytes = GetMacAddressBytes(macAddress);
// Repeat the mac Address sixteen times
for(int i = 1; i < 17; i++) {
for(int j = 0; j < 6; j++) {
magicPacket[(i * 6) + j] = macAddressBytes[j];
}
}
return magicPacket;
}

/// <summary>
/// This converts a mac Address string like 00:11:09:31:4B:11 into a
/// byte array.
/// </summary>
/// <param name=”macAddress”></param>
/// <returns></returns>
private static byte[] GetMacAddressBytes(string macAddress) {
string[] macAddressString = macAddress.Split(’:');
byte[] macAddressBytes = new byte[6];
for(int i = 0; i < 6; i++) {
macAddressBytes[i] = Convert.ToByte(macAddressString[i], 16);
}
return macAddressBytes;
}
}

The entry point WOL takes a mac Address string. This is the familiar format of xx:xx:xx:xx:xx:xx. This string is parsed into a string array using the .Split function. To convert to the equivalent hexadecimal values Convert.ToByte using base 16 does the trick. This is used with the prefix of six FF’s followed by the 16 repetitions of the mac Address. By using UpdClient this ‘Magic Packet’ can be broadcast on the LAN and if the computer with the mac Address is on the LAN then it will boot.