发送数据包是winpcap的扩展功能,并不是libpcap本身具备的,所以不能工作在Linux下。利用pcap_sendpacket()函数可以发送单个数据包,其函数原型为: int pcap_sendpacket ( pcap_t * p, u_char * buf, int size ) 说明:该函数可以发送一个原始数据包到网络上。buf包含要发送到网络上的数据包的数据(包括协议头)。返回值为0说明数据包已经成功的发送了,否则返回-1。该函数直接将buf里面的内容作为单个数据包发送到网络上,不会经过任何处理。

   另外一个可以发送数据包的函数就是Pcap_sendpacket(),它首先将要发送的数据包放在一个队列里面,然后挨个发送。队列有大小,大小表明了它能够存放的数据包的最大数量。发送队列是通过调用pcap_sendqueue_alloc()函数来创建的,创建时要指定所需创建的队列的大小。一旦队列创建完毕,就可以使用pcap_sendqueue_queue()来存放一个数据包在队列里面。此函数获得一个带有时间戳,长度,数据包缓冲区的pcap_pkthdr。这些参数同样被pcap_next_ex()和函数pcap_handler()接收。因此,把一个刚捕获的或者从文件中读取出来的数据包放到队列中去就是把数据包作为参数传递给pcap_sendqueue_queue()。

       下面的函数,是利用队列来发送的,读取F盘下面的某个pcap文件,然后存放到队列中,然后发送出去。发送的网卡需要自己输入,程序运行后,利用抓包软件就可以看到发出去的所有报文:

#define HOME_REMOTE
#define WPCAP

#include "pcap.h"

void main()
{
pcap_t *indev;     /*文件句柄,输入设备*/
pcap_t *outdev;   /*输出设备*/
    FILE *capFile;    /*指向pcap文件的指针*/
    int caplen;   /*该pcap文件的长度*/
char err[PCAP_ERRBUF_SIZE];
pcap_send_queue *squeue; /*发送队列*/
struct pcap_pkthdr *pktheader;
u_char *pktdata;

    /**获取网卡列表,并指定从哪个网卡发送数据**/
pcap_if_t *temp;
pcap_if_t *d;
if(pcap_findalldevs(&temp,err) == -1)
{
   fprintf(stderr,"error in pcap_findalldevs :%s\n",&err);
   return ;
}

/**指定从哪个网卡发送数据,这里没有对输入的数字进行判断**/
int i;
printf("please input a network to send packet:");
    scanf("%d",&i);

int j = 0;
for(d = temp;j < i-1;j++,d = d->next);


/**打开pcap文件**/
char *pcapFile = "f:\\test.pcap";     /*打开的pcap文件路径*/
capFile = fopen(pcapFile,"rb");
if(capFile ==NULL)
{
   printf("open the pcap file wrong!\n");
   return;
}

/**求pcap文件长度**/
fseek(capFile,0,SEEK_END);
caplen = ftell(capFile) - sizeof(struct pcap_file_header);
fclose(capFile);

/**读取pcap文件的内容**/
if((indev = pcap_open_offline(pcapFile,err)) == NULL)
{
   fprintf(stderr,"\n can't open the pcap file \n");
   return;
}

/**打开输出设备**/
if((outdev = pcap_open_live(d->name,100,1,1000,err)) == NULL)
{
        fprintf(stderr,"\n can't open the output device \n");
   return;
}

/**检测MAC类型是否匹配**/
if(pcap_datalink(indev) != pcap_datalink(outdev))
{
   fprintf(stderr,"\n different device \n");
   return;
}

/**将数据存放到队列中**/
squeue = pcap_sendqueue_alloc(caplen);
u_int res;
while((res = pcap_next_ex(indev,&pktheader,(const unsigned char **)&pktdata)) == 1)
{
        if(pcap_sendqueue_queue(squeue,pktheader,pktdata) == -1)
   {
    printf("\n no pcaket \n");
      return;

   }
}

if(res == -1)
{
   printf("\n input no \n");
   return;
}

/**发送队列数据**/
if((res = pcap_sendqueue_transmit(outdev,squeue,1)) < squeue->len)
{
        printf("\n send wrong \n");
}

pcap_sendqueue_destroy(squeue);

return;
}来自: http://hi.baidu.com/honeyhacker/blog/item/d4e79a55267d8b55d0090662.html

Logo

更多推荐