WinPcap发送接收裸包(二)

上一篇说了如何发送裸包,下面看如何接收数据包。

代码很简单,就不多介绍了。

#ifdef _MSC_VER
/*
 * we do not want the warnings about the old deprecated and unsecure CRT functions
 * since these examples can be compiled under *nix as well
 */
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "pcap.h"


typedef struct eth_header
{
	unsigned char DestMac[6];
	unsigned char SrcMac[6];
	unsigned short Etype;
}eth_header;

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
void formatMACToStr(LPSTR lpHWAddrStr,const unsigned char *HWAddr);

int main()
{
	pcap_if_t *alldevs;
	pcap_if_t *d;
	int inum;
	int i=0;
	pcap_t *adhandle;
	char errbuf[PCAP_ERRBUF_SIZE];
	u_int netmask;
	char packet_filter[] ="ether src 28:d2:44:28:74:f9" /*"ip and udp"*/;
	struct bpf_program fcode;
	
	/* Retrieve the device list */
	if(pcap_findalldevs(&alldevs, errbuf) == -1)
	{
		fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
		exit(1);
	}
	
	/* Print the list */
	for(d=alldevs; d; 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! Make sure WinPcap is installed.\n");
		return -1;
	}
	
	printf("Enter the interface number (1-%d):",i);
	scanf("%d", &inum);
	
	/* Check if the user specified a valid adapter */
	if(inum < 1 || inum > i)
	{
		printf("\nAdapter number out of range.\n");
		
		/* Free the device list */
		pcap_freealldevs(alldevs);
		return -1;
	}

	/* Jump to the selected adapter */
	for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
	
	/* Open the adapter */
	if ((adhandle= pcap_open_live(d->name,	// name of the device
							 65536,			// portion of the packet to capture. 
											// 65536 grants that the whole packet will be captured on all the MACs.
							 1,				// promiscuous mode (nonzero means promiscuous)
							 1000,			// read timeout
							 errbuf			// error buffer
							 )) == NULL)
	{
		fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
		/* Free the device list */
		pcap_freealldevs(alldevs);
		return -1;
	}
	
	/* Check the link layer. We support only Ethernet for simplicity. */
	if(pcap_datalink(adhandle) != DLT_EN10MB)
	{
		fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
		/* Free the device list */
		pcap_freealldevs(alldevs);
		return -1;
	}
	
	if(d->addresses != NULL)
		/* Retrieve the mask of the first address of the interface */
		netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
	else
		/* If the interface is without addresses we suppose to be in a C class network */
		netmask=0xffffff; 


	//compile the filter
	if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
	{
		fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
		/* Free the device list */
		pcap_freealldevs(alldevs);
		return -1;
	}
	
	//set the filter
	if (pcap_setfilter(adhandle, &fcode)<0)
	{
		fprintf(stderr,"\nError setting the filter.\n");
		/* Free the device list */
		pcap_freealldevs(alldevs);
		return -1;
	}
	
	printf("\nlistening on %s...\n", d->description);
	
	/* At this point, we don't need any more the device list. Free it */
	pcap_freealldevs(alldevs);
	
	/* start the capture */
	pcap_loop(adhandle, 0, packet_handler, NULL);
	
	return 0;
}

/* Callback function invoked by libpcap for every incoming packet */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
	struct tm *ltime;
	char timestr[16];
	time_t local_tv_sec;
	eth_header *pEthHeader;
	char strMac[50];
	char *pRecBuffer;

	/*
	 * unused parameter
	 */
	(VOID)(param);

	/* convert the timestamp to readable format */
	local_tv_sec = header->ts.tv_sec;
	ltime=localtime(&local_tv_sec);
	strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

	/* print timestamp and length of the packet */

	pEthHeader=(eth_header *)pkt_data;
	formatMACToStr(strMac,pEthHeader->SrcMac);
	printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
	printf("\nSrcMac is:%s-->",strMac);
	formatMACToStr(strMac,pEthHeader->DestMac);
	printf("destMac is:%s\n",strMac);
	printf("eth type is:\t%d\n",ntohs(pEthHeader->Etype));

    pRecBuffer=(char *)(pkt_data+14);
	printf("data is:\t%s\n",pRecBuffer);
	
}

/****************************************************************************
*	Name & Params::
*			formatMACToStr
*			(
*				LPSTR lpHWAddrStr :		 要显示到list上的字符串
*				unsigned char *HWAddr :  传入的MAC字符串
*			)
*	Purpose:
*			将用户输入的MAC地址字符转成相应格式
****************************************************************************/
void formatMACToStr(LPSTR lpHWAddrStr,const unsigned char *HWAddr)
{
	int i;
	short temp;
	char szStr[3];
	
	strcpy(lpHWAddrStr, "");
	for (i=0; i<6; ++i)
	{
		temp = (short)(*(HWAddr + i));
		_itoa(temp, szStr, 16);
		if (strlen(szStr) == 1)	strcat(lpHWAddrStr, "0");
		strcat(lpHWAddrStr, szStr);
		if (i<5)	strcat(lpHWAddrStr, ":");         // 加上 - 
	}
}

WinPcap是一款在Windows操作系统上的网络数据包捕获和分析工具。它提供了发送接收网络数据包的功能,包括发送层数据包。 发送层数据包意味着通过网络适配器发送构建的数据包,这些数据包在以太网帧中传输。WinPcap提供了一个名为SendPacket的函数,可以用于发送构建好的层数据包。 在使用WinPcap发送层数据包之前,我们需要做以下准备工作: 1. 获取网络适配器:通过调用WinPcap的函数,我们可以列出系统上所有可用的网络适配器,并选择要使用的适配器。 2. 构建数据包:我们需要创建一个符合层协议的数据包。这意味着我们需要构建一个以太网帧,并在帧的头部设置目标MAC地址和源MAC地址。 3. 打开适配器:在发送数据包之前,我们需要打开选择的网络适配器。 4. 发送数据包:调用SendPacket函数将构建好的层数据包发送到网络。 5. 关闭适配器和释放资源:在发送完数据包后,我们应该关闭适配器并释放相关的资源。 需要注意的是,使用WinPcap发送层数据包需要具备一定的网络知识和编程经验,了解以太网协议以及数据包的结构和组成。此外,发送层数据包可能需要管理员权限,因为它涉及到操作网络适配器。 总结起来,WinPcap可以帮助我们在Windows环境下发送层数据包,通过构建以太网帧和设置目标MAC地址和源MAC地址来发送数据包。但是操作时需要小心谨慎,避免对网络造成不必要的干扰或安全风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值