调用winpcap发送路由器公告

#include <stdlib.h>
#include <stdio.h>

#include <pcap.h>

#pragma comment(lib, "packet.lib")
#pragma comment(lib, "wpcap.lib")


int main(int argc, char **argv)
{
    pcap_t *fp;
    char errbuf[PCAP_ERRBUF_SIZE];
    u_char packet[200];
    int i;
    
    /* Check the validity of the command line */
    if (argc != 2)
    {
        printf("usage: %s interface", argv[0]);
        return 1;
    }
    
    /* Open the adapter */
    if ((fp = pcap_open_live(argv[1],        // name of the device
                             65536,            // portion of the packet to capture. It doesn't matter in this case 
                             1,                // promiscuous mode (nonzero means promiscuous)
                             1000,            // read timeout
                             errbuf            // error buffer
                             )) == NULL)
    {
        fprintf(stderr,"
Unable to open the adapter. %s is not supported by WinPcap
", argv[1]);
        return 2;
    }

    /* Supposing to be on ethernet, set mac destination to 1:1:1:1:1:1 */
    packet[0]= 0xbd;
    packet[1]= 0xbd;
    packet[2]= 0xbd;
    packet[3]= 0xbd;
    packet[4]= 0xbd;
    packet[5]= 0xbd;
    
    /* set mac source to 2:2:2:2:2:2 */
    packet[6]= 0x00;
    packet[7]= 0x0e;
    packet[8]= 0xc6;
    packet[9]= 0xd6;
    packet[10]= 0x80;
    packet[11]= 0x6c;

    // Eth type
    packet[12] = 0x86;
    packet[13] = 0xdd;

    //IPv6 header
    packet[14] = 0x60;
    packet[15] = 0x00;
    packet[16] = 0x00;
    packet[17] = 0x00;

    packet[18] = 0x00;
    packet[19] = 0x24;
    packet[20] = 0x3a;    //next header
    packet[21] = 0x08;

    packet[22] = 0xfe;    // source addr
    packet[23] = 0x80;
    packet[24] = 0x00;
    packet[25] = 0x00;

    packet[26] = 0x00;
    packet[27] = 0x00;
    packet[28] = 0x00;
    packet[29] = 0x00;

    packet[30] = 0x8c;
    packet[31] = 0x79;
    packet[32] = 0xe4;
    packet[33] = 0xf9;

    packet[34] = 0xf5;
    packet[35] = 0xa3;
    packet[36] = 0xe5;
    packet[37] = 0xeb;

    packet[38] = 0xff;    // dest addr
    packet[39] = 0x02;
    packet[40] = 0x00;
    packet[41] = 0x00;

    packet[42] = 0x00;
    packet[43] = 0x00;
    packet[44] = 0x00;
    packet[45] = 0x00;

    packet[46] = 0x00;
    packet[47] = 0x00;
    packet[48] = 0x00;
    packet[49] = 0x00;

    packet[50] = 0x00;
    packet[51] = 0x00;
    packet[52] = 0x00;
    packet[53] = 0x01;

    packet[54] = 0x86;    // icmp v6 header type
    packet[55] = 0x00;    // code
    packet[56] = 0xe6;    // check sum
    packet[57] = 0x43;

    packet[58] = 0x08;
    packet[59] = 0x00;
    packet[60] = 0x01;    // router lifetime
    packet[61] = 0x10;

    packet[62] = 0x00;    // reachable time
    packet[63] = 0x00;
    packet[64] = 0x00;
    packet[65] = 0x00;

    packet[66] = 0x00;    // retrans timer
    packet[67] = 0x00;
    packet[68] = 0x00;
    packet[69] = 0x00;

    packet[70] = 0x03;    // Option-type
    packet[71] = 0x04;    // Option-length
    packet[72] = 0x40;    // Option-prefix length
    packet[73] = 0xc0;    // Option-L A

    packet[74] = 0xff;    // Option-valid lifetime
    packet[75] = 0xff;    // Option-valid lifetime
    packet[76] = 0xff;    // Option-valid lifetime
    packet[77] = 0xff;    // Option-valid lifetime

    packet[78] = 0xff;    // Option-prefered lifetime
    packet[79] = 0xff;    // Option-prefered lifetime
    packet[80] = 0xff;    // Option-prefered lifetime
    packet[81] = 0xff;    // Option-prefered lifetime

    packet[82] = 0x00;    // Option-reserved
    packet[83] = 0x00;    // Option-reserved
    packet[84] = 0x00;    // Option-reserved
    packet[85] = 0x00;    // Option-reserved

    packet[86] = 0xfc;    // Option-prefix
    packet[87] = 0x00;    // Option-prefix
    packet[88] = 0x00;    // Option-prefix
    packet[89] = 0x00;    // Option-prefix

    packet[90] = 0x00;    // Option-prefix
    packet[91] = 0x00;    // Option-prefix
    packet[92] = 0xf8;    // Option-prefix
    packet[93] = 0x02;    // Option-prefix

    packet[94] = 0x00;    // Option-prefix
    packet[95] = 0x00;    // Option-prefix
    packet[96] = 0x00;    // Option-prefix
    packet[97] = 0x00;    // Option-prefix

    packet[98] = 0x00;    // Option-prefix
    packet[99] = 0x00;    // Option-prefix
    packet[100] = 0x00;    // Option-prefix
    packet[101] = 0x00;    // Option-prefix


    packet[102] = 0x2c;    // crc
    packet[103] = 0x6a;    // crc
    packet[104] = 0x46;    // crc
    packet[105] = 0xb5;    // crc


    
    ///* Fill the rest of the packet */
    //for(i=102;i<200;i++)
    //{
    //    packet[i]= (u_char)i;
    //}

    /* Send down the packet */
    if (pcap_sendpacket(fp,    // Adapter
        packet,                // buffer with the packet
        106                    // size
        ) != 0)
    {
        fprintf(stderr,"
Error sending the packet: %s
", pcap_geterr(fp));
        return 3;
    }

    pcap_close(fp);    
    return 0;
}

原文地址:https://www.cnblogs.com/yanhc/p/8419896.html