Program to remotely Power On a PC over the internet using the Wake-on-LAN protocol.

Wake-on-LAN (WoL) is an Ethernet or token ring computer networking standard that allows a computer to be turned on or awakened by a network message.

Principle of operation

In order for Wake-on-LAN to work, parts of the network interface need to stay on. This consumes a small amount of standby power, much less than normal operating power. Disabling wake-on-LAN when not needed, can therefore very slightly reduce power consumption on computers that are switched off but still plugged into a power socket.

Magic Packet Structure
The magic packet is a broadcast frame containing anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal), followed by sixteen repetitions of the target computer’s 48-bit MAC address, for a total of 102 bytes.
Since the magic packet is only scanned for the string above, and not actually parsed by a full protocol stack, it may be sent as any network- and transport-layer protocol, although it is typically sent as a UDP datagram to port 0, 7 or 9, or directly over Ethernet as EtherType 0x0842.

A standard magic packet has the following basic limitations:



  1. Requires destination computer MAC address (also may require a SecureOn password).
  2. Does not provide a delivery confirmation.
  3. May not work outside of the local network.
  4. Requires hardware support of Wake-On-LAN on destination computer.
  5. Most 802.11 wireless interfaces do not maintain a link in low power states and cannot receive a magic packet.

The Wake-on-LAN implementation is designed to be very simple and to be quickly processed by the circuitry present on the network interface card with minimal power requirement. Because Wake-on-LAN operates below the IP protocol layer the MAC address is required and makes IP addresses and DNS names meaningless.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to remotely Power On a PC over the
// internet using the Wake-on-LAN protocol.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
  
int main()
{
    int i;
    unsigned char toSend[102],mac[6];
    struct sockaddr_in udpClient, udpServer;
    int broadcast = 1 ;
  
    // UDP Socket creation
    int udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
  
    // Manipulating the Socket
    if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST,
                  &broadcast, sizeof broadcast) == -1)
    {
        perror("setsockopt (SO_BROADCAST)");
        exit(EXIT_FAILURE);
    }
    udpClient.sin_family = AF_INET;
    udpClient.sin_addr.s_addr = INADDR_ANY;
    udpClient.sin_port = 0;
  
    //Binding the socket
    bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
  
    for (i=0; i<6; i++)
        toSend[i] = 0xFF;
  
    // Let the MAC Address be ab:cd:ef:gh:ij:kl
    mac[0] = 0xab;  // 1st octet of the MAC Address
    mac[1] = 0xcd;  // 2nd octet of the MAC Address
    mac[2] = 0xef;  // 3rd octet of the MAC Address
    mac[3] = 0xgh;  // 4th octet of the MAC Address
    mac[4] = 0xij;  // 5th octet of the MAC Address
    mac[5] = 0xkl;  // 6th octet of the MAC Address
  
    for (i=1; i<=16; i++)
        memcpy(&toSend[i*6], &mac, 6*sizeof(unsigned char));
  
    udpServer.sin_family = AF_INET;
  
    // Braodcast address
    udpServer.sin_addr.s_addr = inet_addr("10.89.255.255");
    udpServer.sin_port = htons(9);
  
    sendto(udpSocket, &toSend, sizeof(unsigned char) * 102, 0,
             (struct sockaddr*)&udpServer, sizeof(udpServer));
    return 0;
}
chevron_right

Output:

This program will power on the switched-off PC
whose MAC Address is used in this program (the 
PC and the Host computer must be connected over
LAN). 

This article is contributed by Kishlay Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Reference :
https://en.wikipedia.org/wiki/Wake-on-LAN

Please write comments if you find anything incorrect, orr you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




Article Tags :
Practice Tags :