winpcap学习(三)
来源:互联网 发布:买家怎么取消淘宝客 编辑:程序博客网 时间:2024/05/05 21:41
//发送数据包
bool SendPacket() { pcap_if_t* allAdapters = NULL; // 适配器列表 pcap_if_t* pAdapter = NULL; pcap_t* adapterHandle = NULL; u_char packet[20]; //发送数据包 char errorBuffer[PCAP_ERRBUF_SIZE+1] = {"0"}; if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&allAdapters,errorBuffer) == -1) { fprintf(stderr,"non't excute pcap_findalldevs_ex,errorInfo:%s",errorBuffer); return -1; } if(allAdapters == NULL) { fprintf(stderr," not find the adapter for use!"); pcap_freealldevs(allAdapters); return 0; } int crtAdapter = 0; for(pAdapter = allAdapters; pAdapter;pAdapter = pAdapter->next) { printf("\nthe interface %d:%s",++crtAdapter,pAdapter->name); printf("\n---%s",pAdapter->description); } // 选择适配器 int nadapterNum = 0; printf(" Enter the number of adapter which you want to send packet!"); scanf("%d",&nadapterNum); // 搜索接口 if( nadapterNum < 1 || nadapterNum > crtAdapter ) { printf( "\nAdapter number out of range.\n" ); // 释放适配器列表 pcap_freealldevs( allAdapters ); return -1; } pAdapter = allAdapters; for( crtAdapter = 0; crtAdapter < nadapterNum - 1; crtAdapter++ ) pAdapter = pAdapter->next; // 打开指定适配器 adapterHandle = pcap_open( pAdapter->name, // name of the adapter 65536, // portion of the packet to capture // 65536 guarantees that the whole // packet will be captured PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode 1000, // read timeout - 1 millisecond NULL, // authentication on the remote machine errorBuffer // error buffer ); if( adapterHandle == NULL ) {//指定适配器打开失败 fprintf( stderr, "\nUnable to open the adapter\n", pAdapter->name ); // 释放适配器列表 pcap_freealldevs( allAdapters ); return -1; } pcap_freealldevs( allAdapters );//释放适配器列表 //创建数据封包 // 设置目标的MAC地址为01 : 01 : 01 : 01 : 01 : 01 packet[0] = 0x01; packet[1] = 0x01; packet[2] = 0x01; packet[3] = 0x01; packet[4] = 0x01; packet[5] = 0x01; // 设置源的MAC地址为02 : 02 : 02 : 02 : 02 : 02 packet[6] = 0x02; packet[7] = 0x02; packet[8] = 0x02; packet[9] = 0x02; packet[10] = 0x02; packet[11] = 0x02; // 设置封包其他部分内容 for( int index = 12; index < 20; index++ ) { packet[index] = 0xC4; } //发送数据封包 if( pcap_sendpacket( adapterHandle, // the adapter handle packet, // the packet 20 // the length of the packet ) != 0 ) { fprintf( stderr,"\nError sending the packet: \n", pcap_geterr( adapterHandle ) ); return -1; } return 0; }
0 0
- winpcap学习(三)
- Winpcap学习总结(三)
- 循序渐进学习使用WINPCAP(三)
- winpcap学习(一)
- winpcap学习(二)
- winpcap学习(四)
- winpcap学习(五)
- WinpCap的详解(三)
- Winpcap(三)
- WinPcap学习(三)获取已安装设备的高级信息
- 庖丁解牛-----winpcap源码彻底解密(三)
- WinPcap权威指南(三):ARP协议
- Winpcap学习
- Winpcap学习
- Winpcap学习
- WinPcap学习(十一)底层学习
- 循序渐进学习使用WinPcap(一)
- 循序渐进学习使用WINPCAP(二)
- prim学习
- libevent源码分析--I/O 定时 信号 被处理的安排
- 复制与粘贴
- PHP截取汉字乱码问题
- EBS站点分享
- winpcap学习(三)
- ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php
- 最喜爱的文章与专家
- 毕业设计
- libjpeg的编译
- 查看linux版本的三种常用方法
- 我的第一课:【JSON】====创建 JSON 对象
- D. Yaroslav and Divisors
- Job is deferred. Reason:RMFailure