简体   繁体   English

在C ++上创建魔术包

[英]Create magic packet on C++

I'm trying to crate a Magic Packet to apply WOL on a certain PC, I tried to do it in the same format exactly like the packet looks in a Wireshark capture, but the PC doesn't recognize the receiving packet as a magic packet 我试图创建一个魔术包以在特定PC上应用WOL,我尝试以相同的格式进行处理,就像Wireshark捕获中的数据包看起来一样,但是PC无法将接收包识别为魔术包

here is the code: 这是代码:

UINT8*  CTestCase::CreateMagicPacket(IN OUT UINT8* p_buffer, IN int  size, IN MACAddress src, IN MACAddress dst)
{

    UINT8 *my_buff_to_send = new UINT8[size]{ 0 }; //create new buffer with the size we want


    for (int i = 0; i < 6; i++) //broadcast address
    {
        my_buff_to_send[i] = 0xFF;
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
    }

    int m = 5;
    for (size_t i = 6; i < 12; i++) //insert source mac adress to the new buffer
    {

            my_buff_to_send[i] = src.Get(m);
            m_pLog->AddMessage(" |");
            m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
            m--;
    }


    my_buff_to_send[12] = 0x08;
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[12]));
    my_buff_to_send[13] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[13]));
    my_buff_to_send[14] =  0x45;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[14]));
    my_buff_to_send[15] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[15]));
    my_buff_to_send[16] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[16]));
    my_buff_to_send[17] =  0x90;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[17]));
    my_buff_to_send[18] = 0x48;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[18]));
    my_buff_to_send[19] = 0x91;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[19]));
    my_buff_to_send[20] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[20]));
    my_buff_to_send[21] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[21]));
    my_buff_to_send[22] = 0x80;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[22]));
    my_buff_to_send[23] = 0x11;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[23]));
    my_buff_to_send[24] = 0x00;  //insert permanent type to the buffer OR dd
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[24]));
    my_buff_to_send[25] = 0x00;  //insert permanent type to the buffer OR bd
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[25]));
    my_buff_to_send[26] = 0x0a;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[26]));
    my_buff_to_send[27] = 0x0a;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[27]));
    my_buff_to_send[28] = 0x0a;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[28]));
    my_buff_to_send[29] = 0x05;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[29]));
    my_buff_to_send[30] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[30]));
    my_buff_to_send[31] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[31]));
    my_buff_to_send[32] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[32]));
    my_buff_to_send[33] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[33]));
    my_buff_to_send[34] = 0xd0;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[34]));
    my_buff_to_send[35] = 0x28;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[35]));
    my_buff_to_send[36] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[36]));
    my_buff_to_send[37] = 0xff;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[37]));
    my_buff_to_send[38] = 0x00;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[38]));
    my_buff_to_send[39] = 0x7c;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[39]));
    my_buff_to_send[40] = 0x14;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[40]));
    my_buff_to_send[41] = 0x9c;  //insert permanent type to the buffer
    m_pLog->AddMessage(" |");
    m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[41]));



    for (int i = 42; i < 48; i++)
    {
        my_buff_to_send[i] = 0xFF;
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
    }

    int r = 5;
    for (size_t i = 48; i < 54; i++) //insert source mac adress to the new buffer
    {
        my_buff_to_send[i] = dst.Get(r);
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
        r--;
    }

    for (int i = 54; i < 60; i++)
    {
        my_buff_to_send[i] = 0xFF;
        m_pLog->AddMessage(" |");
        m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
    }


    for (size_t j = 0; j < 16; j++)
    {

        for (int k = 5; k >= 0; k--) //insert destination mac adress to the buffer
        {
            int i = 60;
            my_buff_to_send[i] = dst.Get(k);

            m_pLog->AddMessage(" |");
            m_pLog->AddMessage("%d ", UINT32(my_buff_to_send[i]));
            i++;
        }

    }


            return my_buff_to_send;




};

Do you really need to create a raw Ethernet packet on your own? 您真的需要自己创建一个原始的以太网数据包吗?

When I've done this, I've just embedded the magic packet into something like a UDP packet, and broadcast that. 完成此操作后,我将魔术包嵌入了UDP包之类的内容中,并进行了广播。 For example: 例如:

#include "sock2/sock2.h"

int main() { 
    std::string MAC_address("\x00\x00\x00\x00\x00\x00", 6);
    std::string buffer("\xff\xff\xff\xff\xff\xff");

    for (int i=0; i<16; i++)
        buffer.append(MAC_address);

    IP::UDP::socket s;
    s.setopt(SO_BROADCAST);

    IP::address addr(192, 168, 0, 255, 5);
    s.send(buffer, addr);
}

That's pretty minimalist (eg, hard-coded addresses, virtually no error checking), but with appropriate bytes for the MAC address works just fine. 这是非常简单的(例如,硬编码的地址,几乎没有错误检查),但是对于MAC地址使用适当的字节就可以了。 I didn't try to analyze all your code in detail, but it does seem to include quite a lot more than I'd expect should normally be necessary. 我没有尝试详细分析您的所有代码,但是它似乎包含的内容比我通常通常认为需要的要多得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM