c语言做网络抓包器

先在liunx中安装GTK图形库
然后直接代码


#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <gtk/gtk.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#define ETH_P_REVARP 0x8035
struct arp_packet
{
	unsigned char mac_target[ETH_ALEN];
	unsigned char mac_source[ETH_ALEN];
	unsigned short ethertype;
	unsigned short hw_type; 
	unsigned short proto_type;
	unsigned char mac_addr_len;
	unsigned char ip_addr_len;
	unsigned short operation_code;
	unsigned char mac_sender[ETH_ALEN];
	unsigned char ip_sender[4];
	unsigned char mac_receiver[ETH_ALEN];
	unsigned char ip_receiver[4];
	unsigned char padding[18];
};
/* 定义tpc/ip数据包的数据结构 */
struct ip_tcp {
struct  iphdr *iph;
struct  tcphdr *tcph;
unsigned char buffer[500];
};
/* 定义udp/ip数据包的数据结构 */
struct ip_udp {
struct  iphdr *iph;
struct  udphdr *udph;
unsigned char buffer[500];
};
/* 定义tpc/ip数据包的数据结构 */
struct ip_icmp1 {
struct  iphdr *iph;
struct  icmp *icmph;
unsigned char buffer[500];
};
static GtkWidget* createwindow;
void die(const char*pre)
{
	perror(pre);
	exit(1);
}
/* 创建一个"信息"列表 */
GtkWidget *create_list_arp( void )
{
	
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkListStore *model;
	GtkTreeIter iter;
	GtkCellRenderer *cell;
	GtkTreeViewColumn *column;
	int i;
	/* 创建一个新的滚动窗口(scrolled window),只有需要时,滚动条才出现 */
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	model = gtk_list_store_new (1, G_TYPE_STRING);
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),tree_view);
	gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
	gtk_widget_show (tree_view);
	/* 在窗口中添加一些消息 */
	int sfd;
	struct sockaddr_ll my_etheraddr; 
	struct arp_packet rcvBuffer;
	sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP)); //发送接收以太网数据帧  htons将主机字节顺序转换为网络字节顺序
	if(-1 == sfd)
	{
		die("socket");
	}
	memset(&my_etheraddr, 0, sizeof(my_etheraddr));//内存空间初始化
	//目的物理地址结构
	my_etheraddr.sll_family = AF_PACKET;
	my_etheraddr.sll_protocol = htons(ETH_P_ARP);
	my_etheraddr.sll_ifindex = IFF_BROADCAST;
 	//使用bind将套接字绑定到配置好的本地物理地址结构
	if(-1 == bind(sfd, (struct sockaddr *)&my_etheraddr, sizeof(my_etheraddr))){
		die("bind");
	}
	recv(sfd, &rcvBuffer, sizeof(rcvBuffer), 0);
	for (i = 0; i <= 23; i++) 
	{
		if(i==0){	
		gchar *msg = g_strdup_printf ("MAC target:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==1){int i1=0;
		gchar *msg = g_strdup_printf ("0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x", rcvBuffer.mac_target[i1],rcvBuffer.mac_target[i1+1],rcvBuffer.mac_target[i1+2],rcvBuffer.mac_target[i1+3],rcvBuffer.mac_target[i1+4],rcvBuffer.mac_target[i1+5]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==2){
		gchar *msg = g_strdup_printf ("MAC source:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==3){int i2=0;for(i2=0;i2<ETH_ALEN;i2++){
		gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_source[i2]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}}
		if(i==4){
		char *msg = g_strdup_printf ("ethertype:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==5){
		char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.ethertype));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==6){
		char *msg = g_strdup_printf ("hw_type:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==7){
		char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.hw_type));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==8){
		char *msg = g_strdup_printf ("proto_type:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==9){char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.proto_type));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==10){
		char *msg = g_strdup_printf ("mac_addrlen:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==11){char *msg = g_strdup_printf ("0x%x", rcvBuffer.mac_addr_len);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==12){
		char *msg = g_strdup_printf ("ip_add_len:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==13){char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.ip_addr_len));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==14){
		char *msg = g_strdup_printf ("operation_code:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==15){char *msg = g_strdup_printf ("0x%x",  ntohs(rcvBuffer.operation_code));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==16){char *msg = g_strdup_printf ("mac_sender:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==17){int i3=0;
		for( i3 = 0; i3 < ETH_ALEN; i3++)
		{
		gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_sender[i3]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		}
		if(i==18){char *msg = g_strdup_printf ("ip_sender:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==19){gchar *msg = g_strdup_printf ("%s", inet_ntoa(*(struct in_addr*)(rcvBuffer.ip_sender)));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==20){char *msg = g_strdup_printf ("mac_receiver:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==21){int i4=0;
		for( i4 = 0; i4 < ETH_ALEN; i4++)
		{
		gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_receiver[i4]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		}
		if(i==22){char *msg = g_strdup_printf ("ip_receiver:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==23){gchar *msg = g_strdup_printf ("%s", inet_ntoa(*(struct in_addr*)(rcvBuffer.ip_receiver)));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
	}
	cell = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("catch arp messages as follows",cell,"text", 0,NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
	GTK_TREE_VIEW_COLUMN (column));
	return scrolled_window;
}
GtkWidget* zhanshi_arp()
{
	GtkWidget *window;
	GtkWidget *vpaned;
	GtkWidget *list;
	GtkWidget *text;
	GtkWidget *buttonarp;
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "arp window");
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
	vpaned = gtk_vpaned_new ();
	gtk_container_add (GTK_CONTAINER (window), vpaned);
	gtk_widget_show (vpaned);
	/* 在分栏窗口的两部分各添加一些构件 */
	list = create_list_arp ();
	gtk_paned_add1 (GTK_PANED (vpaned), list);
	gtk_widget_show (list);
	gtk_widget_show_all (window);
	return window;
}
void show_arp()
{
	printf("9999");
	createwindow=zhanshi_arp();
	gtk_widget_show(createwindow);
	
}
GtkWidget *create_list_rarp( void )
{
	
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkListStore *model;
	GtkTreeIter iter;
	GtkCellRenderer *cell;
	GtkTreeViewColumn *column;
	int i;
	/* 创建一个新的滚动窗口(scrolled window),只有需要时,滚动条才出现 */
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	model = gtk_list_store_new (1, G_TYPE_STRING);
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),tree_view);
	gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
	gtk_widget_show (tree_view);
	/* 在窗口中添加一些消息 */
	int sfd;
	struct sockaddr_ll my_etheraddr; 
	struct arp_packet rcvBuffer;
	sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_RARP)); //发送接收以太网数据帧  htons将主机字节顺序转换为网络字节顺序
	if(-1 == sfd)
	{
		die("socket");
	}
	memset(&my_etheraddr, 0, sizeof(my_etheraddr));//内存空间初始化
	//目的物理地址结构
	my_etheraddr.sll_family = AF_PACKET;
	my_etheraddr.sll_protocol = htons(ETH_P_RARP);
	my_etheraddr.sll_ifindex = IFF_BROADCAST;
 	//使用bind将套接字绑定到配置好的本地物理地址结构
	if(-1 == bind(sfd, (struct sockaddr *)&my_etheraddr, sizeof(my_etheraddr))){
		die("bind");
	}
	recv(sfd, &rcvBuffer, sizeof(rcvBuffer), 0);
	for (i = 0; i < 23; i++) 
	{
		if(i==0){	
		gchar *msg = g_strdup_printf ("MAC target:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==1){int i1=0;
		gchar *msg = g_strdup_printf ("0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x", rcvBuffer.mac_target[i1],rcvBuffer.mac_target[i1+1],rcvBuffer.mac_target[i1+2],rcvBuffer.mac_target[i1+3],rcvBuffer.mac_target[i1+4],rcvBuffer.mac_target[i1+5]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==2){
		gchar *msg = g_strdup_printf ("MAC source:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==3){int i2=0;for(i2=0;i2<ETH_ALEN;i2++){
		gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_source[i2]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}}
		if(i==4){
		char *msg = g_strdup_printf ("ethertype:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==5){
		char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.ethertype));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==6){
		char *msg = g_strdup_printf ("hw_type:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==7){
		char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.hw_type));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==8){
		char *msg = g_strdup_printf ("proto_type:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==9){char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.proto_type));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==10){
		char *msg = g_strdup_printf ("mac_addrlen:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==11){char *msg = g_strdup_printf ("0x%x", rcvBuffer.mac_addr_len);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==12){
		char *msg = g_strdup_printf ("ip_add_len:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==13){char *msg = g_strdup_printf ("0x%x", ntohs(rcvBuffer.ip_addr_len));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==14){
		char *msg = g_strdup_printf ("operation:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==15){char *msg = g_strdup_printf ("0x%x",  ntohs(rcvBuffer.operation_code));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==16){char *msg = g_strdup_printf ("mac_sender:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==17){int i3=0;
		for( i3 = 0; i3 < ETH_ALEN; i3++)
		{
		gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_sender[i3]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		}
		if(i==18){char *msg = g_strdup_printf ("ip_sender:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==19){gchar *msg = g_strdup_printf ("%s", inet_ntoa(*(struct in_addr*)(rcvBuffer.ip_sender)));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==20){char *msg = g_strdup_printf ("mac_receiver:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==21){int i4=0;
		for( i4 = 0; i4 < ETH_ALEN; i4++)
		{
		gchar *msg = g_strdup_printf ("0x%.2x", rcvBuffer.mac_receiver[i4]);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		}
		if(i==22){char *msg = g_strdup_printf ("ip_receiver:");
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==23){gchar *msg = g_strdup_printf ("%s", inet_ntoa(*(struct in_addr*)(rcvBuffer.ip_receiver)));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
	}
	cell = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("catch rarp messages as follows",cell,"text", 0,NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
	GTK_TREE_VIEW_COLUMN (column));
	return scrolled_window;
}
GtkWidget* zhanshi_rarp()
{
	GtkWidget *window;
	GtkWidget *vpaned;
	GtkWidget *list;
	GtkWidget *text;
	GtkWidget *buttonarp;
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "rarp window");
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
	vpaned = gtk_vpaned_new ();
	gtk_container_add (GTK_CONTAINER (window), vpaned);
	gtk_widget_show (vpaned);
	/* 在分栏窗口的两部分各添加一些构件 */
	list = create_list_rarp ();
	gtk_paned_add1 (GTK_PANED (vpaned), list);
	gtk_widget_show (list);
	gtk_widget_show_all (window);
	return window;
}
void show_rarp()
{
	createwindow=zhanshi_rarp();
	gtk_widget_show(createwindow);
}
GtkWidget *create_list_tcp( void )
{
	
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkListStore *model;
	GtkTreeIter iter;
	GtkCellRenderer *cell;
	GtkTreeViewColumn *column;
	int i;
	/* 创建一个新的滚动窗口(scrolled window),只有需要时,滚动条才出现 */
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	model = gtk_list_store_new (1, G_TYPE_STRING);
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),tree_view);
	gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
	gtk_widget_show (tree_view);
	/* 在窗口中添加一些消息 */
	int sock, bytes_recieved, fromlen,id;
	unsigned char buffer[65535];
	struct sockaddr_in from,ff;
	struct ip  *ip;
	struct tcphdr *tcp;
	struct ip_tcp *iptcp;
	sock = socket(AF_INET, SOCK_RAW,IPPROTO_TCP);
	fromlen = sizeof(from);
		/* 接收包 */for(id=0;id<5;id++){
	bytes_recieved = recvfrom(sock, buffer, sizeof(buffer),0,(struct sockaddr *)&from, &fromlen);
	if (bytes_recieved>0)
	{
		ip = (struct ip *)buffer;
		/* tcp信息包 从整个IP/TCP包 buffer + (4*ip->ip_length) 地址处开始 */
		tcp = (struct tcphdr *)(buffer + (4*ip->ip_hl));
		for (i = 0; i <= 12; i++) 
		{
		if(i==0){	
		gchar *msg = g_strdup_printf ("Bytes received ::: %5d",bytes_recieved);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==1){	
		gchar *msg = g_strdup_printf ("IP header length ::: %d",ip->ip_hl);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==2){	
		gchar *msg = g_strdup_printf ("IP sum      size ::: %d",ntohs(ip->ip_len));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==3){	
		gchar *msg = g_strdup_printf ("Protocol ::: %d",ip->ip_p);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==4){	
		gchar *msg = g_strdup_printf ("IP_source address ::: %s",inet_ntoa(ip->ip_src));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==5){	
		gchar *msg = g_strdup_printf ("IP_dest address ::: %s",inet_ntoa(ip->ip_dst));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		unsigned short int ntons(unsigned short int netshort);
		if(i==6){	
		gchar *msg = g_strdup_printf ("Source port ::: %d",ntohs(tcp->source));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==7){	
		gchar *msg = g_strdup_printf ("Dest port   ::: %d",ntohs(tcp->dest));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==8){	
		gchar *msg = g_strdup_printf ("seq         ::: %d",ntohs(tcp->seq));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==9){	
		gchar *msg = g_strdup_printf ("ack_seq     ::: %d",ntohs(tcp->ack_seq));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==10){	
		gchar *msg = g_strdup_printf ("th_win      ::: %d",ntohs(tcp->window));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==11){	
		gchar *msg = g_strdup_printf ("check      ::: %d",ntohs(tcp->check));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==12){	
		gchar *msg = g_strdup_printf ("urg_ptr      ::: %d\n",ntohs(tcp->urg_ptr));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
	}}}
	cell = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("catch tcp messages as follows",cell,"text", 0,NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
	GTK_TREE_VIEW_COLUMN (column));
	return scrolled_window;
}
GtkWidget* zhanshi_tcp()
{
	GtkWidget *window;
	GtkWidget *vpaned;
	GtkWidget *list;
	GtkWidget *text;
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "tcp window");
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
	vpaned = gtk_vpaned_new ();
	gtk_container_add (GTK_CONTAINER (window), vpaned);
	gtk_widget_show (vpaned);
	/* 在分栏窗口的两部分各添加一些构件 */
	list = create_list_tcp ();
	gtk_paned_add1 (GTK_PANED (vpaned), list);
	gtk_widget_show (list);
	gtk_widget_show_all (window);
	return window;
}
void show_tcp()
{
	createwindow=zhanshi_tcp();
	gtk_widget_show(createwindow);
}
GtkWidget *create_list_udp( void )
{
	
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkListStore *model;
	GtkTreeIter iter;
	GtkCellRenderer *cell;
	GtkTreeViewColumn *column;
	int i;
	/* 创建一个新的滚动窗口(scrolled window),只有需要时,滚动条才出现 */
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	model = gtk_list_store_new (1, G_TYPE_STRING);
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),tree_view);
	gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
	gtk_widget_show (tree_view);
	/* 在窗口中添加一些消息 */
	int sock, bytes_recieved, fromlen,id;
	unsigned char buffer[65535];
	struct sockaddr_in from,ff;
	struct ip  *ip;
	struct udphdr *udp;
	struct ip_udp *ipudp;
	/* 建立原始TCP包方式 收到IP+TCP信息包 */
	sock = socket(AF_INET, SOCK_RAW,IPPROTO_UDP);
	fromlen = sizeof(from);
		/* 接收包 */for(id=0;id<5;id++){
	bytes_recieved = recvfrom(sock, buffer, sizeof(buffer),0,(struct sockaddr *)&from, &fromlen);
	if (bytes_recieved>0)
	{
		ip = (struct ip *)buffer;
		/* tcp信息包 从整个IP/TCP包 buffer + (4*ip->ip_length) 地址处开始 */
		udp = (struct udphdr *)(buffer + (4*ip->ip_hl));
		for (i = 0; i <= 9; i++) 
		{
		if(i==0){	
		gchar *msg = g_strdup_printf ("Bytes received ::: %5d",bytes_recieved);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==1){	
		gchar *msg = g_strdup_printf ("IP header length ::: %d",ip->ip_hl);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==2){	
		gchar *msg = g_strdup_printf ("IP sum      size ::: %d",ntohs(ip->ip_len));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==3){	
		gchar *msg = g_strdup_printf ("Protocol ::: %d",ip->ip_p);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==4){	
		gchar *msg = g_strdup_printf ("IP_source address ::: %s",inet_ntoa(ip->ip_src));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==5){	
		gchar *msg = g_strdup_printf ("IP_dest address ::: %s",inet_ntoa(ip->ip_dst));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		unsigned short int ntons(unsigned short int netshort);
		if(i==6){	
		gchar *msg = g_strdup_printf ("Source port ::: %d",ntohs(udp->source));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==7){	
		gchar *msg = g_strdup_printf ("Dest port   ::: %d",ntohs(udp->dest));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==8){	
		gchar *msg = g_strdup_printf ("seq         ::: %d",ntohs(udp->len));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==9){	
		gchar *msg = g_strdup_printf ("check      ::: %d\n",ntohs(udp->check));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
	}}}
	cell = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("catch udp messages as follows",cell,"text", 0,NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
	GTK_TREE_VIEW_COLUMN (column));
	return scrolled_window;
}
GtkWidget* zhanshi_udp()
{
	GtkWidget *window;
	GtkWidget *vpaned;
	GtkWidget *list;
	GtkWidget *text;
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "udp window");
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
	vpaned = gtk_vpaned_new ();
	gtk_container_add (GTK_CONTAINER (window), vpaned);
	gtk_widget_show (vpaned);
	/* 在分栏窗口的两部分各添加一些构件 */
	list = create_list_udp ();
	gtk_paned_add1 (GTK_PANED (vpaned), list);
	gtk_widget_show (list);
	gtk_widget_show_all (window);
	return window;
}
void show_udp()
{
	createwindow=zhanshi_udp();
	gtk_widget_show(createwindow);
}
GtkWidget *create_list_icmp( void )
{
	GtkWidget *scrolled_window;
	GtkWidget *tree_view;
	GtkListStore *model;
	GtkTreeIter iter;
	GtkCellRenderer *cell;
	GtkTreeViewColumn *column;
	int i;
	/* 创建一个新的滚动窗口(scrolled window),只有需要时,滚动条才出现 */
	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
	model = gtk_list_store_new (1, G_TYPE_STRING);
	tree_view = gtk_tree_view_new();
	gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),tree_view);
	gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
	gtk_widget_show (tree_view);
	int sock, bytes_recieved, fromlen,n,id=1,on=1,s;
	unsigned char buffer[65535];
	struct sockaddr_in from,ff;
	struct ip  *ip;
	struct icmp *icmp;
	struct ip_icmp1 *ipicmp;
	/* 建立原始TCP包方式 收到IP+TCP信息包 */
	sock = socket(AF_INET, SOCK_RAW,IPPROTO_ICMP);
	fromlen = sizeof(from);
		/* 接收包 */
	bytes_recieved = recvfrom(sock, buffer, sizeof(buffer),0,(struct sockaddr *)&from, &fromlen);
	if (bytes_recieved>0)
	{
/* IP头为最小160位 8位一个字节 共20个字节 (0x14) 后 */

		ip = (struct ip *)buffer;
		/* tcp信息包 从整个IP/TCP包 buffer + (4*ip->ip_length) 地址处开始 */
		icmp = (struct icmp *)(buffer + (4*ip->ip_hl));
		for (i = 0; i < 9; i++) 
		{
		if(i==0){	
		gchar *msg = g_strdup_printf ("Bytes received ::: %5d",bytes_recieved);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==1){	
		gchar *msg = g_strdup_printf ("IP header length ::: %d",ip->ip_hl);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==2){	
		gchar *msg = g_strdup_printf ("IP   len  size ::: %d",ntohs(ip->ip_len));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==3){	
		gchar *msg = g_strdup_printf ("Protocol ::: %d",ip->ip_p);
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==4){	
		gchar *msg = g_strdup_printf ("IP_source address ::: %s",inet_ntoa(ip->ip_src));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==5){	
		gchar *msg = g_strdup_printf ("IP_dest address ::: %s",inet_ntoa(ip->ip_dst));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		unsigned short int ntons(unsigned short int netshort);
		if(i==6){	
		gchar *msg = g_strdup_printf ("icmp type ::: %d",ntohs(icmp->icmp_type));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==7){	
		gchar *msg = g_strdup_printf ("icmp code ::: %d",ntohs(icmp->icmp_code));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}
		if(i==8){	
		gchar *msg = g_strdup_printf ("icmp cksum ::: %d\n",ntohs(icmp->icmp_cksum));
		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
		gtk_list_store_set (GTK_LIST_STORE (model),&iter,0, msg,-1);
		g_free (msg);}

	}}//}
	cell = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes ("catch udp messages as follows",cell,"text", 0,NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
	GTK_TREE_VIEW_COLUMN (column));
	return scrolled_window;
}
GtkWidget* zhanshi_icmp()
{
	GtkWidget *window;
	GtkWidget *vpaned;
	GtkWidget *list;
	GtkWidget *text;
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW (window), "icmp window");
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
	vpaned = gtk_vpaned_new ();
	gtk_container_add (GTK_CONTAINER (window), vpaned);
	gtk_widget_show (vpaned);
	/* 在分栏窗口的两部分各添加一些构件 */
	list = create_list_icmp();
	gtk_paned_add1 (GTK_PANED (vpaned), list);
	gtk_widget_show (list);
	gtk_widget_show_all (window);
	return window;
}
void show_icmp()
{
	createwindow=zhanshi_icmp();
	gtk_widget_show(createwindow);
}
	
/* 用指定的参数创建一个按钮盒 */
GtkWidget *create_bbox1(char *title,gint spacing,gint child_w,gint child_h,gint layout )
{
	GtkWidget *frame;
	GtkWidget *bbox;
	GtkWidget *buttonarp;
	GtkWidget *buttonrarp;
	GtkWidget *buttontcp;
	GtkWidget *button;
	frame = gtk_frame_new (title);
	bbox = gtk_hbutton_box_new ();
	gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
	gtk_container_add (GTK_CONTAINER (frame), bbox);
	/* 设置按钮盒的外观 */
	gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), layout);
	gtk_box_set_spacing (GTK_BOX (bbox), spacing);
	buttonarp= gtk_button_new_with_label("ARP");
	gtk_container_add (GTK_CONTAINER (bbox), buttonarp);
	g_signal_connect(G_OBJECT(buttonarp),"clicked",G_CALLBACK(show_arp),NULL);
	buttonrarp = gtk_button_new_with_label("RARP");
	gtk_container_add (GTK_CONTAINER (bbox), buttonrarp);
	g_signal_connect(G_OBJECT(buttonrarp),"clicked",G_CALLBACK(show_rarp),NULL);
	buttontcp = gtk_button_new_with_label("TCP");
	gtk_container_add (GTK_CONTAINER (bbox), buttontcp);
	g_signal_connect(G_OBJECT(buttontcp),"clicked",G_CALLBACK(show_tcp),NULL);
	return frame;
}
GtkWidget *create_bbox2(char *title,gint spacing,gint child_w,gint child_h,gint layout )
{
	GtkWidget *frame;
	GtkWidget *bbox;
	GtkWidget *buttonclose;GtkWidget *buttonudp;GtkWidget *buttonicmp;
	frame = gtk_frame_new (title);
	bbox = gtk_hbutton_box_new ();
	gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
	gtk_container_add (GTK_CONTAINER (frame), bbox);
	/* 设置按钮盒的外观 */
	gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), layout);
	gtk_box_set_spacing (GTK_BOX (bbox), spacing);
	buttonudp = gtk_button_new_with_label("UDP");
	gtk_container_add (GTK_CONTAINER (bbox), buttonudp);
	g_signal_connect(G_OBJECT(buttonudp),"clicked",G_CALLBACK(show_udp),NULL);
	buttonicmp = gtk_button_new_with_label("ICMP");
	gtk_container_add (GTK_CONTAINER (bbox), buttonicmp);
	g_signal_connect(G_OBJECT(buttonicmp),"clicked",G_CALLBACK(show_icmp),NULL);
	buttonclose = gtk_button_new_with_label("CLOSE");
	gtk_container_add (GTK_CONTAINER (bbox), buttonclose);
	g_signal_connect(G_OBJECT(buttonclose),"clicked",G_CALLBACK(gtk_main_quit),NULL);
	return frame;
}

int main( int argc,char* argv[] )
{
	GtkWidget  *window = NULL;
	GtkWidget *main_vbox;
	GtkWidget *vbox;
	GtkWidget *frame_horz;
	/* 初始化 */
	gtk_init (&argc, &argv);
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
	gtk_window_set_title (GTK_WINDOW (window), "functions are as follows");
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
	g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (gtk_main_quit),NULL);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	main_vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (window), main_vbox);
	frame_horz = gtk_frame_new ("Family protrols");
	gtk_box_pack_start (GTK_BOX (main_vbox), frame_horz, TRUE, TRUE, 10);
	vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
	gtk_container_add (GTK_CONTAINER (frame_horz), vbox);
	gtk_box_pack_start (GTK_BOX (vbox),create_bbox1 ("as arp rarp tcp", 30, 85, 20, GTK_BUTTONBOX_START),TRUE, TRUE, 5);
	gtk_box_pack_start (GTK_BOX (vbox),create_bbox2 ("as udp icmp", 30, 85, 20, GTK_BUTTONBOX_START),TRUE, TRUE, 5);
	/* 进入事件循环 */
	gtk_widget_show_all (window);
	gtk_main ();
	return FALSE;
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言中,可以使用WinPcap库来进行抓包。下面提供一个简单的使用WinPcap库来进行数据包捕获的C语言程序示例: ```c #include <stdio.h> #include <pcap.h> void packet_handle(u_char *user, const struct pcap_pkthdr *header, const u_char *pkt_data) { printf("Packet captured, length = %d\n", header->len); } int main() { char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *handle; struct bpf_program filter; char filter_exp[] = "tcp"; bpf_u_int32 net; bpf_u_int32 mask; // Open the capture device handle = pcap_open_live("eth0", BUFSIZ, 1, 1000, errbuf); if (handle == NULL) { fprintf(stderr, "Could not open device: %s\n", errbuf); return 1; } // Compile the filter expression if (pcap_lookupnet("eth0", &net, &mask, errbuf) == -1) { fprintf(stderr, "Could not get netmask for device: %s\n", errbuf); net = 0; mask = 0; } if (pcap_compile(handle, &filter, filter_exp, 0, net) == -1) { fprintf(stderr, "Could not compile filter: %s\n", pcap_geterr(handle)); return 1; } // Set the filter if (pcap_setfilter(handle, &filter) == -1) { fprintf(stderr, "Could not set filter: %s\n", pcap_geterr(handle)); return 1; } // Capture packets if (pcap_loop(handle, -1, packet_handle, NULL) == -1) { fprintf(stderr, "Error during packet capture: %s\n", pcap_geterr(handle)); return 1; } // Close the capture device pcap_close(handle); return 0; } ``` 该程序首先打开一个网络设备,然后编译并设置过滤,最后循环捕获数据包并调用回调函数进行处理。在回调函数中,可以对捕获到的数据包进行解析和分析。 需要注意的是,为了使用WinPcap库,需要将其头文件和库文件添加到编译环境中。另外,在Windows平台上,还需要安装WinPcap库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小 鱼 儿 呀

您的鼓励就是我创造的动力,懂的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值