怎么打印泰坦之旅数据包放哪里里的数据,packet

& & & 提起发送数据包大家可能会想到使用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){
char errbuf[PCAP_ERRBUF_SIZE];
u_char packet[100];
/* 检查命令行参数的合法性 */
if (argc != 2)
printf("usage: %s interface (e.g. 'rpcap://eth0')", argv[0]);
return -1;
/* 打开输出设备 */
if ( (fp= pcap_open(argv[1],
// 要捕获的部分 (只捕获前100个字节)
PCAP_OPENFLAG_PROMISCUOUS,
// 混杂模式
// 读超时时间
// 远程机器验证
// 错误缓冲
) ) == 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,*
char errbuf[PCAP_ERRBUF_SIZE];
char source[PCAP_BUF_SIZE];
int caplen,
pcap_send_queue *
struct pcap_pkthdr *
const u_char *
float cpu_
u_int npacks = 0;
/* 检查命令行参数的合法性 */
if (argc &= 2 || argc &= 5)
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;
sync = FALSE;
/* 开始捕获 */
/* 根据WinPcap的新语法创建一个源字符串 */
if ( pcap_createsrcstr( source,
// 源字符串
PCAP_SRC_FILE,
// 我们要打开的文件
// 远程主机
// 远程主机的端口
// 我们要打开的文件名
// 错误缓冲
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");
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");
& & & 介绍一个新的数据类型,pcap_send_queue结构体,下面是该结构体的定义。
\brief A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit().*/struct pcap_send_queue{
///& Maximum size of the the queue, in bytes. This variable contains the size of the buffer field.
///& Current size of the queue, in bytes.
///& Buffer containing the packets to be sent.};typedef struct pcap_send_queue pcap_send_
& & & 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,因为这样发送将毫无意义可言。
& & & 最后看一下第二个程序在本机上运行的结果:
阅读(...) 评论()当前位置: >>
3数据包传输的过程
Exploring the Packet Delivery ProcessEthernet LANs? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-1 Layer 2 AddressingUses MAC address Assigned to end devices? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-2 Layer 3 AddressingEach NOS has its own Layer 3 address format. OSI uses NSAP. TCP/IP uses IP.? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-3 Host-to-Host Packet Delivery (1 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-4 Host-to-Host Packet Delivery (2 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-5 Host-to-Host Packet Delivery (3 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-6 Host-to-Host Packet Delivery (4 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-7 Host-to-Host Packet Delivery (5 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-8 Host-to-Host Packet Delivery (6 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-9 Host-to-Host Packet Delivery (7 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-10 Host-to-Host Packet Delivery (8 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-11 Host-to-Host Packet Delivery (9 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-12 Host-to-Host Packet Delivery (10 of 10)? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-13 SummaryLayer 2 addresses are MAC addresses. Layer 3 addresses are IP addresses. If the MAC address is not known, ARP is used to map Layer 2 to Layer 3. Switches learn the MAC addressing to port mapping by monitoring the source addresses of frames. When a switch forwards a frame, it does not change the source or destination Layer 2 address.? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-14 ? 2007 Cisco Systems, Inc. All rights reserved.ICND1 v1.0―2-15
如下图所示,网络站点A发送数据包给B,在数据包经过路由器转发的过程中,封装在数据包3中的目的IP地址和目的MAC地址是()。 A.223.54.9.2和.cc78 B...实验三:数据包结构分析 - 《网络与通信》课程实验报告 实验三:数据包结构分析 姓名 任课教师 实验地点 实验课表现 实验目的: 1. 了解 Sniffer 的工作原理,掌握...发送目的地: 以太网数据包发送源: 以太网数据包内容类型(以太网协议类型): ARP 数据包类型(ARP 的动作): 目标硬件地址:00:16:EC:09:FF:24 步骤 3 的结果...请独立完成实验和报告,实验报告分析和数据要一致。 实验 得分 1 2 3 4 5 6 7 8 9 3.1 UDP 通信过程分析 实验时间: A.上传的文件名是: 客户端 IP: Ssh...正确分析链路层、网络层、运输层各个数据包首部主要字段的 内容 三.实验步骤与...解码分析 IP 报文结构: 《编译原理》实验报告 -3- 45:4 表示 IP 数据报...IP 报头之后的部分为 IP 包中的数据 部分。步骤 3:利用网络协议编辑软件编辑并发送 IP 数据包 1、在本地主机打开网络协议编辑软件,在工具栏选择“添加”,建立...计算机网络与应用实验实验三捕获 TCP 数据包自 94 班 杨磊
实验目的通过实验熟悉 Wireshark 抓包软件的使用方法,理解 TCP 传输过程,以及慢启动、拥塞 ...实验三IP数据包的解析 - 实验三 IP 包的解析 一.实验目的 实验目的: 目的 本次课程设计的目的是设计一个解析 IP 数据包的程序, 并根据分析结果说明 IP 数据...实验3 网络数据包捕获和协议分析_天文/地理_自然科学_专业资料。计算机网络 实验...PASV(被动)方式的连接过程是:客户端向服务器的 FTP 端口(默认是 21)发送连 ...数据包抓包分析 - 数据链路层数据包抓包分析 实验内容 (1)安装 Wireshark 软件。 (2)掌握抓包软件的使用 (3)掌握通过抓包软件抓取帧并进行分析的办法 实验步骤 ...
All rights reserved Powered by
www.tceic.com
copyright &copyright 。文档资料库内容来自网络,如有侵犯请联系客服。Hi,亲爱的小伙伴!
欢迎来到php1.cn社区!
Tools Online | 在线开发工具
RankList | 热门文章
扫码关注 PHP1 官方微信号
Recommend | 推荐阅读
PHP1.CN | 中国最专业的PHP中文社区 |
Copyright (C) 1998 - 2020 PHP1.CN. All Rights Reserved
PHP1.CN 第一PHP社区数据分析――Packet Sniffer_图文_百度文库
您的浏览器Javascript被禁用,需开启后体验完整功能,
享专业文档下载特权
&赠共享文档下载特权
&10W篇文档免费专享
&每天抽奖多种福利
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
数据分析――Packet Sniffer
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,同时保存到云知识,更方便管理
加入VIP
还剩10页未读,
定制HR最喜欢的简历
你可能喜欢packetsniffer可以抓取哪些数据包_百度知道
packetsniffer可以抓取哪些数据包
我有更好的答案
sniffer一般用来抓取局域网中的数据包。因为目前局域网一般采用以太网技术,而以太网是基于广播实现数据收发的。每时每刻网卡都在侦听局域网中的数据包,正常情况下,网卡会自动丢弃掉那些不是发给自己的数据包。而sniffer可以将网卡设为“混杂”...
采纳率:84%
来自团队:
为您推荐:
其他类似问题
您可能关注的内容
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。}

我要回帖

更多关于 影之诗数据包在哪里 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信