Wake on Lan – [HOW TO] – tutorial C/C++

Wake on Lan, means powering on a computer remotely by sending a data packet known as a magic packet. For this to work, the network card must have enabled the feature: “Power on Lan” or “Power on PCI Device“, which is done by accessing the BIOS of the machine.

MagicPacket format:

6 * 255 or (0xff)
16 * MAC Address of target PC

In C/C++ you can create a packet like this

unsigned char tosend[102];
unsigned char mac[6];

/** first 6 bytes of 255 **/
for(int i = 0; i < 6; i++) {
tosend[i] = 0xFF;
/** store mac address **/
mac[0] = 0x00;
mac[1] = 0x17;
mac[2] = 0x31;
mac[3] = 0x42;
mac[4] = 0x45;
mac[5] = 0x27;
/** append it 16 times to packet **/
for(int i = 1; i <= 16; i++) {
memcpy(&tosend[i * 6], &mac, 6 * sizeof(unsigned char));

How to send

The magic packet must be sent over UDP protocol to port 7 or 9, the ladder being the most used. Sending the packet to a computer in the same subnetwork is pretty easy. You just have to specify the broadcast address of you network, which usually ends with *.255.

int udpSocket;
struct sockaddr_in udpClient, udpServer;
int broadcast = 1;

udpSocket = socket(AF_INET, SOCK_DGRAM, 0);

/** you need to set this so you can broadcast **/
if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) == -1) {
perror(“setsockopt (SO_BROADCAST)”);
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;

bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));

/** …make the packet as shown above **/

/** set server end point (the broadcast addres)**/
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(“”);
udpServer.sin_port = htons(9);

/** send the packet **/
sendto(udpSocket, &tosend, sizeof(unsigned char) * 102, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));

The target computer should now be up and running!

Sending the magic packet over internet behind a NAT is a bit tricky, but simpler to code.
If the target computer sits behind a router which enables port forwarding to the broadcast address, then the process in sending the magic packet resembles the steps above. just replace local broadcast address with the public broadcast address of the router and set up port forwarding to route UDP packets on port 9 to the internal broadcast ip.
If the router doesn’t support the above mentioned feature your not screwed :). For this to work, the target computer must not be turned off, but in standby or sleep mode and with the network card set up as to allow any network activity to bring it back online. This can be easily done by changing the advanced setting of the card in Windows Device Manager.

Forward UDP data on any port to that machine by tweaking the router, and send the magic packet to the public ip of the router on the forwarded port, without the need to set up the socket as to broadcast data.

this should also wake up the machine.

This was written in a hurry. ask questions if something is not understood!.


%d bloggers like this: