Thread: Constructing a Wake On Lan packet (low level packet)

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    9

    Constructing a Wake On Lan packet (low level packet)

    Hello there!

    As an exercise, I am trying to implement a WOL programme to use inside my local network. As per the specification of a magic packet:

    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.
    Let's say I have a MAC address of AA:BB:CCD and I want to construct a data element to be sent to my network (broadcasted actually) so that my device with said MAC address can accept it.

    If I go and write std::string mac = "AABBCCDD", I am giving the ASCII values of each character to mac, which will be translated (to bytes) 41 41 42 42 43 43 44 44. Would I need to use std::hex in a string stream to accomplice my goal?

    What is the standard practice to create such a low level packet? Should I still use unsigned char arrays?

    Thank you so much for your time!

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    866
    RAW sockets on Level 2...
    Easy to do in Linux... hard to do in everything else...

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    9
    I am gladly on linux. I will definitely look into RAW sockets, but still this another matter, isn't it?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,126
    Code:
    #include <stdio.h>
     
    typedef unsigned char uchar;
     
    int main() {
        uchar packet[102] = {'\xFF', '\xFF', '\xFF', '\xFF', '\xFF', '\xFF'};
        uchar mac[6] = {0, 0, '\xAA', '\xBB', '\xCC', '\xDD'};
     
        // construct packet
        for (int i = 0; i < 16; ++i)
            for (int j = 0; j < 6; ++j)
                packet[i * 6 + j + 6] = mac[j];
     
        // display on terminal
        for (int i = 0; i < 17; ++i) {
            for (int j = 0; j < 6; ++j)
                printf("%02X ", packet[i * 6 + j]);
            putchar('\n');
        }
     
        // write to binary file
        FILE *fout = fopen("packet.bin", "wb");
        fwrite(packet, 1, 102, fout);
        fclose(fout);
     
        return 0;
    }
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constructing and parsing network packet
    By ddd in forum C Programming
    Replies: 5
    Last Post: 04-23-2012, 04:12 PM
  2. How to get packet IP?
    By leetow2003 in forum Linux Programming
    Replies: 6
    Last Post: 07-11-2011, 11:28 AM
  3. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 05:49 AM
  4. TCP/IP packet run-together-ing
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-10-2003, 04:42 PM
  5. LOWEST level packet writing
    By skacy in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2002, 02:51 PM

Tags for this Thread