發送數據包


【實驗代碼】

// sendpacket.cpp : 定義控制檯應用程序的入口點。

//


#include "stdafx.h"



#include <stdlib.h>

#include <stdio.h>


#include <pcap.h>



void main(int argc, char **argv)

{

pcap_t *fp;

pcap_if_t *d;

pcap_if_t *alldevs;

int inum;

char errbuf[PCAP_ERRBUF_SIZE];

u_char packet[100];

int i;

int i2=0;



/* 獲得設備列表 */

    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)

    {

        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);

        exit(1);

    }

    

    /* 打印列表 */

    for(d=alldevs; d; d=d->next)

    {

        printf("%d. %s", ++i2, d->name);

        if (d->description)

            printf(" (%s)\n", d->description);

        else

            printf(" (No description available)\n");

    }


    if(i2==0)

    {

        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");

        return ;

    }

    

    printf("Enter the interface number (1-%d):",i2);

    scanf("%d", &inum);

    

    if(inum < 1 || inum > i2)

    {

        printf("\nInterface number out of range.\n");

        /* 釋放設備列表 */

        pcap_freealldevs(alldevs);

        return ;

    }


    /* 跳轉到已選設備 */

    for(d=alldevs, i2=0; i2< inum-1 ;d=d->next, i2++);

    /* 打開輸出設備 */

    if ( (fp= pcap_open(d->name,            // 設備名

                        100,                // 要捕獲的部分 (只捕獲前100個字節)

                        PCAP_OPENFLAG_PROMISCUOUS,  // 混雜模式

                        1000,               // 讀超時時間

                        NULL,               // 遠程機器驗證

                        errbuf              // 錯誤緩衝

                        ) ) == NULL)

    {

        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);

        return;

    }

/* 釋放設備列表 */

        pcap_freealldevs(alldevs);


    /* 假設在以太網上,設置MAC的目的地址爲 1:1:1:1:1:1 */

    packet[0]=255;

    packet[1]=255;

    packet[2]=255;

    packet[3]=255;

    packet[4]=255;

    packet[5]=255;

    

    /* 設置MAC源地址爲 2:2:2:2:2:2 */

    packet[6]=7;

    packet[7]=7;

    packet[8]=7;

    packet[9]=7;

    packet[10]=7;

    packet[11]=7;

packet[12]=96;

packet[13]=00;

    

    /* 填充剩下的內容 */

    for(i=14;i<100;i++)

    {

        packet[i]=i%256;

    }


    /* 發送數據包 */

    if (pcap_sendpacket(fp, packet, 100 /* size */) != 0)

    {

        fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));

        return;

    }


    return;

}

【實驗結果】


在wireshark中檢索eth.type==9600

就會出現所輸入的地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章