#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#pragma comment(lib, "wpcap.lib")
SHORT checksum(USHORT* buffer, int size)
{
unsigned long cksum = 0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (USHORT)(~cksum);
}
int main()
{
int a;
pcap_if_t *alldevs;
pcap_if_t *d;
int i = 0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
struct bpf_program fcode;
BYTE packet[100] = {0};
int innum;
/* get local devices */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr, "Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}
printf("当前网络适配器列表如下:\n");
/* print devices list */
for (d = alldevs; d != NULL; d = d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf("(%s)\n", d->description);
else
printf("(No description available)\n");
}
if (i == 0)
{
printf("\nNo interfaces found!\n");
return -1;
}
/*打开适配器*/
printf("Enter the interface number (1-%d):", i);
scanf_s("%d", &innum);
if (innum < 1 || innum > i)
{
printf("\nInterface number out of range.\n");
pcap_freealldevs(alldevs); /* 释放设备列表 */
return -1;
}
for (d = alldevs, i = 0; i < innum - 1; d = d->next, i++); /* 跳转到选中的适配器 */
adhandle = pcap_open_live(d->name, //适配器名字
65536, //捕获包最大字节数
1, //混杂模式
1000, //读取超时时间
errbuf); //错误信息保存
/*********************************发送数据包**************************************/
if ((adhandle = pcap_open_live(d->name, 100, 1, 1000, errbuf)) == NULL)
{
fprintf(stderr, "\nError opening adapter:%s\n", errbuf);
return -1;
}
{//填充以太网头
byte btDstMac[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xfa };
byte btDstSrc[6] = { 0xc4, 0x34, 0x6b, 0x65, 0x91, 0x37 };
memcpy(packet, btDstMac, 6);
memcpy(packet + 6, btDstSrc, 6);
/*填充MAC帧类型*/
packet[12] = 0x8;
packet[13] = 0x00;
}
{//填充IP首部部分
packet[14] = 0x46;//版本+头长度
packet[15] = 0;//区分服务(0x00)
//长度
packet[16] = 0;//总长度前一个字节部分(0x00)
packet[17] = 0X20;//总长度后一个字节部分(0x05)
//
packet[18] = 0x22;//标识前一个字节部分(0x00)
packet[19] = 0xca;//标识后一个字节部分(0x00)
packet[20] = 0;//标志和片偏移的前5bit(0x00)
packet[21] = 0;//片偏移的后8bit(0x00)
packet[22] = 1;//生存时间(0x01)
packet[23] = 2;//协议IGMP
//数据包校验
packet[24] = 00;//首部校验和的前一字节(0xa5)
packet[25] = 00;//首部校验和的后一字节(0xa0)
//本机IP地址
packet[26] = 0xc0;
packet[27] = 0xa8;
packet[28] = 0x02;
packet[29] = 0xc3;
//目的地址 239.255.255.250
packet[30] = 0xef;
packet[31] = 0xff;
packet[32] = 0xff;
packet[33] = 0xfa;
/*附加项*/
packet[34] = 0x94;
packet[35] = 0x04;
packet[36] = 0;
packet[37] = 0;
BYTE bt[24] = { 0 };
memcpy(bt, packet + 14, 24);
SHORT dwSum = checksum((unsigned short*)bt, 24);//求校验和
packet[25] = (dwSum >> 8) & 0xff;//首部校验和的前一字节(0xa5)
packet[24] = dwSum & 0xff;//首部校验和的后一字节(0xa0)
}
{
//填充IGMP包
packet[38] = 0x16;
packet[39] = 0x00;
//igmp校验和
packet[40] = 0;
packet[41] = 0;
//根据抓包,组地址跟目的地址一样
packet[42] = 0xef;
packet[43] = 0xff;
packet[44] = 0xff;
packet[45] = 0xfa;
BYTE bt1[8] = { 0 };
memcpy(bt1, packet + 38, 8);
SHORT dwSum1 = checksum((unsigned short*)bt1, 8);
packet[41] = (dwSum1 >> 8) & 0xff;//首部校验和的前一字节(0xa5)
packet[40] = dwSum1 & 0xff;//首部校验和的后一字节(0xa0)
}
for (int i = 46; i < 60; i++){
packet[i] = 0x00;
}
/* 发送数据包 */
if (pcap_sendpacket(adhandle, packet, 60) == 0)
{
//fprintf(stderr, "\nError sending the packet: %s\n", pcap_geterr(adhandle));
printf("发送数据包成功!\n");
//return -1;
}
scanf_s("%d",&a);
}