发送数据包

虽然名称WinPcap清楚地表明图书馆的目的是分组捕获,但还提供了其他有用的原始网络功能。其中,用户可以找到一组完整的功能来发送数据包。

请注意,原来的libpcap库目前没有提供任何方式发送数据包,因此这里显示的所有功能都是WinPcap扩展,在Unix下不起作用。

用pcap_sendpacket()发送单个数据包

发送数据包的最简单方式如下面的代码段所示。打开适配器后,调用pcap_sendpacket()发送手工制作的数据包。pcap_sendpacket()将包含要发送的数据的缓冲区的长度和要发送的缓冲区的适配器作为参数。请注意,缓冲区按原样发送到网络,无需任何操作。这意味着应用程序必须创建正确的协议头以发送有意义的东西。

#include <stdlib.h>
#include <stdio.h>#include <pcap.h>void main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
int i;/* Check the validity of the command line */if (argc != 2){printf("usage: %s interface (e.g. 'rpcap://eth0')", argv[0]);return;}/* Open the output device */if ( (fp= pcap_open(argv[1],            // name of the device100,                // portion of the packet to capture (only the first 100 bytes)PCAP_OPENFLAG_PROMISCUOUS,  // promiscuous mode1000,               // read timeoutNULL,               // authentication on the remote machineerrbuf              // error buffer) ) == NULL){fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);return;}/* Supposing to be on ethernet, set mac destination to 1:1:1:1:1:1 */packet[0]=1;packet[1]=1;packet[2]=1;packet[3]=1;packet[4]=1;packet[5]=1;/* set mac source to 2:2:2:2:2:2 */packet[6]=2;packet[7]=2;packet[8]=2;packet[9]=2;packet[10]=2;packet[11]=2;/* Fill the rest of the packet */for(i=12;i<100;i++){packet[i]=(u_char)i;}/* Send down the packet */if (pcap_sendpacket(fp, packet, 100 /* size */) != 0){fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));return;}return;
}

发送队列

虽然pcap_sendpacket()提供了一种简单而直接的方式来发送单个数据包,但发送队列提供了高级,强大和优化的机制来发送数据包集合。发送队列是将发送到网络的可变数量的数据包的容器。它有一个大小,表示它可以存储的最大字节数。

创建一个发送队列,调用pcap_sendqueue_alloc()函数,指定新发送队列的大小。

创建发送队列后,可以使用pcap_sendqueue_queue()将数据包添加到发送队列。此函数使用带有时间戳和长度的pcap_pkthdr以及数据包的数据的缓冲区。这些参数与pcap_next_ex()和pcap_handler()接收的参数相同,因此排队刚从文件捕获或读取的数据包是将这些参数传递给pcap_sendqueue_queue()的问题。

要发送发送队列,WinPcap提供pcap_sendqueue_transmit()函数。注意第三个参数:如果非零,发送将被同步,即将遵守数据包的相对时间戳。此操作需要大量CPU,因为使用“忙等待”循环在内核驱动程序中进行同步。虽然这个操作是CPU密集型的,但是通常会导致非常高的精度数据包传输(通常在几微秒或更短的时间内)。

请注意,使用pcap_sendqueue_transmit()发送发送队列比执行一系列pcap_sendpacket()更有效率,因为发送队列在内核级别缓冲大幅度减少上下文切换的数量。

当不再需要队列时,可以使用pcap_sendqueue_destroy()删除它,从而释放与发送队列关联的所有缓冲区。

下一个程序显示如何使用发送队列。它使用pcap_open_offline()打开一个捕获文件,然后将数据包从文件移动到正确分配的发送队列。在他的点上,它发送队列,如果用户请求,则同步它。

请注意,转储文件的链接层与将使用pcap_datalink()发送数据包的接口进行比较,如果它们不同,则打印警告 - 重要的是捕获文件链接层为与适配器的链路层相同,否则传输是无意义的。

#include <stdlib.h>
#include <stdio.h>#include <pcap.h>void usage();void main(int argc, char **argv)
{pcap_t *indesc,*outdesc;char errbuf[PCAP_ERRBUF_SIZE];char source[PCAP_BUF_SIZE];FILE *capfile;int caplen, sync;u_int res;pcap_send_queue *squeue;struct pcap_pkthdr *pktheader;u_char *pktdata;float cpu_time;u_int npacks = 0;errno_t fopen_error;/* Check the validity of the command line */if (argc <= 2 || argc >= 5){usage();return;}/* Retrieve the length of the capture file */fopen_error = fopen_s(&capfile, argv[1],"rb");if(fopen_error != 0){printf("Error opening the file, errno %d.\n", fopen_error);return;}fseek(capfile , 0, SEEK_END);caplen= ftell(capfile)- sizeof(struct pcap_file_header);fclose(capfile);/* Chek if the timestamps must be respected */if(argc == 4 && argv[3][0] == 's')sync = TRUE;elsesync = FALSE;/* Open the capture *//* Create the source string according to the new WinPcap syntax */if ( pcap_createsrcstr( source,         // variable that will keep the source stringPCAP_SRC_FILE,  // we want to open a fileNULL,           // remote hostNULL,           // port on the remote hostargv[1],        // name of the file we want to openerrbuf          // error buffer) != 0){fprintf(stderr,"\nError creating a source string\n");return;}/* Open the capture file */if ( (indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL){fprintf(stderr,"\nUnable to open the file %s.\n", source);return;}/* Open the output adapter */if ( (outdesc= pcap_open(argv[2], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL){fprintf(stderr,"\nUnable to open adapter %s.\n", source);return;}/* Check the MAC type */if (pcap_datalink(indesc) != pcap_datalink(outdesc)){printf("Warning: the datalink of the capture differs from the one of the selected interface.\n");printf("Press a key to continue, or CTRL+C to stop.\n");getchar();}/* Allocate a send queue */squeue = pcap_sendqueue_alloc(caplen);/* Fill the queue with the packets from the file */while ((res = pcap_next_ex( indesc, &pktheader, &pktdata)) == 1){if (pcap_sendqueue_queue(squeue, pktheader, pktdata) == -1){printf("Warning: packet buffer too small, not all the packets will be sent.\n");break;}npacks++;}if (res == -1){printf("Corrupted input file.\n");pcap_sendqueue_destroy(squeue);return;}/* Transmit the queue */cpu_time = (float)clock ();if ((res = pcap_sendqueue_transmit(outdesc, squeue, sync)) < squeue->len){printf("An error occurred sending the packets: %s. Only %d bytes were sent\n", pcap_geterr(outdesc), res);}cpu_time = (clock() - cpu_time)/CLK_TCK;printf ("\n\nElapsed time: %5.3f\n", cpu_time);printf ("\nTotal packets generated = %d", npacks);printf ("\nAverage packets per second = %d", (int)((double)npacks/cpu_time));printf ("\n");/* free the send queue */pcap_sendqueue_destroy(squeue);/* Close the input file */pcap_close(indesc);/* * lose the output adapter * IMPORTANT: remember to close the adapter, otherwise there will be no guarantee that all the * packets will be sent!*/pcap_close(outdesc);return;
}void usage()
{printf("\nSendcap, sends a libpcap/tcpdump capture file to the net. Copyright (C) 2002 Loris Degioanni.\n");printf("\nUsage:\n");printf("\t sendcap file_name adapter [s]\n");printf("\nParameters:\n");printf("\nfile_name: the name of the dump file that will be sent to the network\n");printf("\nadapter: the device to use. Use \"WinDump -D\" for a list of valid devices\n");printf("\ns: if present, forces the packets to be sent synchronously, i.e. respecting the timestamps in the dump file. This option will work only under Windows NTx.\n\n");exit(0);
}

c++ winpcap开发(8)相关推荐

  1. 在Visual Studio 2005下配置WinPcap开发环境

    在Visual Studio 2005下配置WinPcap开发环境 http://www.winpcap.org/archive/ 4.1beta5_WpdPack.zip http://www.wi ...

  2. winpcap 开发

    Winpcap是一个强大的网络开发库,可以实现许多功能:获取可用的网络适配器:获取指定适配器信息(比如名称和描述信息):捕获指定网卡的数据封包:发送数据封包:过滤捕获的包以获取特定包等. 首先到htt ...

  3. socket编程之DEV C++配置winpcap开发环境并编写网络嗅探器sniffer

    欢迎关注我的个人博客:www.zuzhiang.cn 期末计算机网络课程设计让做一个网络嗅探器,要求可以检测和选择网卡,并打开到混杂模式,监听局域网中的所有数据包并解析出所用网络协议以及首部各个字段的 ...

  4. codeblocks配置winpcap开发环境

    转载请注明出处,谢谢_ (:з」∠)_ 起因 最近作业要用Winpcap开发包分析.pcap文件实现报文字段的识别-本来打算用Visual Studio写,不过想起以前打OJ的时候都用Code::Bl ...

  5. Dev-C++ 配置 WinPcap 开发环境

    VC++ 6.0实在是太老了,自己并不愿意在这个平台上开发,所以转而使用Dev-C++,以下是综合网上的教程和自己的试验总结出的用Dev-C++进行WinPcap网络开发的所需的环境配置工作: 首先是 ...

  6. 一步一步开发sniffer(Winpcap+MFC)(一)工欲善其事,必先配环境——配置winpcap开发环境

    0.说在前面的话 1) 本文将以一个初学者的角度,一步一步几乎是从0开始讲述如何完成一个基于winpcap+MFC的sniffer(嗅探器)当然我指的"0"并不是指连编程都不会,如 ...

  7. 基于winpcap开发的相关资料

    2019独角兽企业重金招聘Python工程师标准>>> http://www.oschina.net/code/snippet_196111_7100 http://wenku.ba ...

  8. c++ winpcap开发(9)

    收集网络流量统计 本课程展示了WinPcap的另一个高级功能:收集网络流量统计信息的能力.统计引擎利用内核级包过滤器对传入的数据包进行有效的分类.如果您想了解更多详细信息,可以参考NPF驱动程序内部手 ...

  9. c++ winpcap开发(7)

    处理离线转储文件 在这个课程中,我们将学习如何处理数据包捕获到一个文件(转储到文件).WinPcap提供广泛的功能来将文件的网络流量保存到文件并读取转储的内容 - 本课将介绍如何使用所有这些功能.我们 ...

最新文章

  1. 经典重温:卡尔曼滤波器介绍与理论分析
  2. jhipster 配置 mysql_JHipster技术栈定制 - JHipster Registry配置信息加密
  3. sql2000 的bcp命令
  4. HEOI2012 朋友圈
  5. 概念辨析:工厂模式 工厂方法模式 简单工厂模式 抽象工厂
  6. MM模块几个移动类型之间的区别
  7. 电脑桌面锁屏怎么设置_华为手机总是莫名多出照片?这两个设置不关闭,内存再大也不够用...
  8. 华为手机asph啥机型_华为正式宣布!19款机型开启新系统内测,你的手机榜首有名吗?...
  9. 小米荣耀互怼:头部高管们神仙打架 到底谁是谁非?
  10. 反恐精英的服务器存在哪个文件夹,反恐精英地图放在哪里 CS1.6地图放置位置详细介绍_游侠网...
  11. 总结vue几种页面刷新方法
  12. linux 上安装 Node.js和npm
  13. python爬虫实战——爬取猫眼电影TOP100并导入excel表
  14. 公务员报考二级专业目录计算机类,国家公务员考试报考专业(公务员考试二级专业目录)...
  15. c语言string函数的用法_C语言让电脑关机?system函数功能够大够硬
  16. 锚具ovm是什么意思_OVM锚具资料
  17. 苹果IOS 10.0.2屏蔽垃圾短信
  18. [我参加NVIDIA Sky Hackathon](模型训练ssd系列)
  19. 在word2015中的条形图在灰度的情况下不明显
  20. python百度地图标注自己的店名_百度地图上怎么显示店名 百度地图标注店名方法...

热门文章

  1. java手工注入bean_java相关:Spring中如何动态注入Bean实例教程
  2. 解读文献里的那些图——流式细胞术
  3. 使用Mfuzz包进行基因表达的时间趋势分析并划分聚类群
  4. 导师要让你学会的“显规则”
  5. 有了它,快速学会RStudio应用
  6. AE物体表面跟踪特效合成高级插件:Lockdown for Mac
  7. 蓝桥杯大赛青少年创意编程 推荐考生阅读 相关书籍的相关视频(部分)
  8. 第十一届蓝桥杯省赛C++组试题 第4题 选择题判定
  9. 1.5 编程基础之循环控制 05 最高的分数
  10. 【docker】第四节:通过docker容器,进行部署fastadmin。