发送数据包

  提起发送数据包大家可能会想到使用SOCKET编程来实现,但其实WinPcap也提供了发送数据包的API,尽管从名字上来看它应该是用来数据捕捉的。值得注意的是,libpcap不支持发送数据包的功能,因此下面提到的函数都是WinPcap的扩展,在UNIX平台下是不支持的。下面这个实例程序正是利用了WinPcap中的pcap_sendpacket()来发送单个数据包。

复制代码
#include <stdlib.h>
#include <stdio.h>
#define HAVE_REMOTE
#include <pcap.h>


int main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
int i;

/* 检查命令行参数的合法性 */
if (argc != 2)
{
printf("usage: %s interface (e.g. 'rpcap://eth0')", argv[0]);
return -1;
}

/* 打开输出设备 */
if ( (fp= pcap_open(argv[1], // 设备名
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 -1;
}

/* 假设在以太网上,设置MAC的目的地址为 1:1:1:1:1:1 */
packet[0]=1;
packet[1]=1;
packet[2]=1;
packet[3]=1;
packet[4]=1;
packet[5]=1;

/* 设置MAC源地址为 2:2:2:2:2:2 */
packet[6]=2;
packet[7]=2;
packet[8]=2;
packet[9]=2;
packet[10]=2;
packet[11]=2;

/* 填充剩下的内容 */
for(i=12; i<100; i++)
{
packet[i]=i%256;
}

/* 发送数据包 */
if (pcap_sendpacket(fp, packet, 100 /* size */) != 0)
{
fprintf(stderr,"\nError sending the packet: %s\n", pcap_geterr(fp));
return -1;
}

return 0;
}
复制代码

      下面是pcap_sendpacket()函数的声明:

      int pcap_sendpacket(pcap_t *, const u_char *, int);

      这三个参数的含义分别是发送数据包的适配器、要发送的数据和缓冲的长度。需要注意的是,缓冲直接发送至网络,不会有任何的控制和加工。这就意味着程序需要构造正确的协议头,使得数据包更有意义。该函数的返回值为0表示发送成功,-1表示发送失败。

      但是我们知道,在真正的应用程序中我们往往需要发送多个数据包,事实上WinPcap也支持发送多个数据包,而且WinPcap的这种方式会更高级、更强大,结构会更优。它支持发送队列的方法,即利用队列作为将要发送至网络的数据包的容器。我们来看看下面这个例子。

复制代码
#include <stdlib.h>
#include <stdio.h>

#define HAVE_REMOTE
#define WPCAP
#include <pcap.h>

void usage();

int 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;
const u_char *pktdata;
float cpu_time;
u_int npacks = 0;

/* 检查命令行参数的合法性 */
if (argc <= 2 || argc >= 5)
{
usage();
return -1;
}

/* 获取捕获文件长度 */
capfile=fopen(argv[1],"rb");
if(!capfile)
{
printf("Capture file not found!\n");
return -1;
}

fseek(capfile , 0, SEEK_END);
caplen= ftell(capfile)- sizeof(struct pcap_file_header);
fclose(capfile);

/* 检查时间戳是否合法 */
if(argc == 4 && argv[3][0] == 's')
sync = TRUE;
else
sync = FALSE;

/* 开始捕获 */
/* 根据WinPcap的新语法创建一个源字符串 */
if ( pcap_createsrcstr( source, // 源字符串
PCAP_SRC_FILE, // 我们要打开的文件
NULL, // 远程主机
NULL, // 远程主机的端口
argv[1], // 我们要打开的文件名
errbuf // 错误缓冲
) != 0)
{
fprintf(stderr,"\nError creating a source string\n");
return -1;
}

/* 打开捕获文件 */
if ( (indesc= pcap_open(source, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
{
fprintf(stderr,"\nUnable to open the file %s.\n", source);
return -1;
}

/* 打开要输出的适配器 */
if ( (outdesc= pcap_open(argv[2], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
{
fprintf(stderr,"\nUnable to open adapter %s.\n", source);
return -1;
}

/* 检查MAC的类型 */
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();
}

/* 分配发送队列 */
squeue = pcap_sendqueue_alloc(caplen);

/* 从文件中将数据包填充到发送队列 */
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 -1;
}

/* 发送队列 */

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");

/* 释放发送队列 */
pcap_sendqueue_destroy(squeue);

/* 关闭输入文件 */
pcap_close(indesc);

/*
* 释放输出适配器
* IMPORTANT: 记得一定要关闭适配器,不然就不能保证
* 所有的数据包都回被发送出去
*/
pcap_close(outdesc);


return 0;
}


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);
}
复制代码

      介绍一个新的数据类型,pcap_send_queue结构体,下面是该结构体的定义。

复制代码
/*!
\brief A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit().
*/
struct pcap_send_queue
{
u_int maxlen; ///< Maximum size of the the queue, in bytes. This variable contains the size of the buffer field.
u_int len; ///< Current size of the queue, in bytes.
char *buffer; ///< Buffer containing the packets to be sent.
};

typedef struct pcap_send_queue pcap_send_queue;
复制代码

      pcap_send_queue结构体正如其名,它是发送数据队列,具体字段的含义源码中已有说明。
      我们再来看看这个发送队列的生命周期~~

      1. 创建发送队列。通过调用pcap_sendqueue_alloc()函数:

          pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
         其中memsize表示申请队列的大小,返回的是队列结构体指针。

      2. 向队列加入要发送的数据包。通过调用pcap_sendqueue_queue()函数:

           int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);

          queue表示指向发送队列的指针;pkt_header表示指向pcap_pkthdr结构体(dump文件中数据包的首部)的指针;pkt_data即为将要发送的数据。

          本例中是从文件中读取pkt_header和pkt_data后再将数据包添加至队列中的。

      3. 发送数据队列。通过调用pcap_sendqueue_transmit()函数:

           u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);

          这里只需要注意第三个参数sync。如果非0,则发送是同步进行的,也就是说,时间戳相符的数据包才会被处理。当然这样会消耗大量的CPU资源,因为同步操作由内核驱动中“忙等”循环实现。尽管这个操作对CPU要求很高,但它对数据包的处理结果通常是很精确的。(通常在数微秒左右,或更少)

          需要注意的是利用pcap_sendqueue_transmit()发送数据队列比通过send_packet()发送一系列数据包要高效得多,这是因为发送队列保存在内核级的缓冲区,减少了上下文交换的次数。

      4. 释放数据队列。通过调用pcap_sendqueue_destroy()函数:

           void pcap_sendqueue_destroy(pcap_send_queue* queue);

      另外需要注意到的一点是,程序中比较了dump文件的链路层和发送数据包的适配器的链路层,如果不同则会打印warning,因为这样发送将毫无意义可言。

      最后看一下第二个程序在本机上运行的结果:

作者:黑剑 
出处:http://www.cnblogs.com/blacksword/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值