winPcap获取网卡网络地址和子网掩码

下面是获取网络地址(不是IP地址)和子网掩码的示例,没时间接着往下做例子了,因为接下来需要在LINUX下面使用libPcap,当然我会贴出代码,会linux编程的大牛一般都会,所以准确的说是贴给自己的,喜欢玩数据包的朋友自己看官方例子就行

#include<pcap.h>
/**
数据包主执行函数
 */
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"Packet.lib")
#pragma comment(lib,"ws2_32.lib")

void getAddr();
int main(int argc,char *argv[])
{
	
	getAddr();
	return 0;
}

//获取网卡网络地址和子网掩码
void getAddr()
{
	pcap_if_t *alldevs;
	pcap_if_t *d;
	struct in_addr net_ip_address;//网卡IP信息,在pcap.h里面有定义
	u_int32_t net_ip;
	char *net_ip_string;

	struct in_addr net_mask_address;
	u_int32_t net_mask;
	char *net_mask_string;

	int i=0;
	char errbuf[PCAP_ERRBUF_SIZE];
	if(pcap_findalldevs(&alldevs,errbuf)==-1)//无法找到网卡列表
	{
		fprintf(stderr,"error in pcap_findalldevs: %s\n",errbuf);
		exit(1);
	}
	/* 扫描列表 */
	for(d=alldevs;d;d=d->next)
	{
		printf("%s\n",d->name);
		printf("Description: %s\n",d->description);
		pcap_lookupnet(d->name,&net_ip,&net_mask,errbuf);

		net_ip_address.s_addr = net_ip;
		net_ip_string = inet_ntoa(net_ip_address);//format
		printf("网络地址: %s \n",net_ip_string);
	
		net_mask_address.s_addr = net_mask;
		net_mask_string = inet_ntoa(net_mask_address);//format
		printf("子网掩码: %s \n",net_mask_string);
		printf("\n");
	}

	/* 释放链表 */
	pcap_freealldevs(alldevs);
	printf("\n");
}

VC6.0下测试通过
winPcap获取网卡网络地址和子网掩码_第1张图片

你可能感兴趣的:(编程,linux,网络,String,测试)