⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hitview.c

📁 Linux系统下著名的个人防火墙
💻 C
📖 第 1 页 / 共 2 页
字号:
	gtk_tree_view_column_set_sort_column_id (column, HITCOL_PROTOCOL);	gtk_tree_view_append_column (treeview, column);		visible = preferences_get_bool (PREFS_HITVIEW_PROTOCOL_COL);	gtk_tree_view_column_set_visible (column, visible);	/* column for service */	column = create_text_column (HITCOL_SERVICE, _("Service"));	gtk_tree_view_column_set_sort_column_id (column, HITCOL_SERVICE);	gtk_tree_view_append_column (treeview, column);		visible = preferences_get_bool (PREFS_HITVIEW_SERVICE_COL);	gtk_tree_view_column_set_visible (column, visible);}/* [ get_hit ] * Retrieve the specific hit iter points to */static Hit *get_hit (GtkTreeModel *model,         GtkTreeIter iter){	Hit *h = g_new (Hit, 1);	gtk_tree_model_get (model, &iter,	                    HITCOL_TIME,        &h->time,			    HITCOL_DIRECTION,   &h->direction,	                    HITCOL_IN,          &h->in,	                    HITCOL_OUT,         &h->out,	                    HITCOL_PORT,        &h->port,	                    HITCOL_SOURCE,      &h->source,	                    HITCOL_DESTINATION, &h->destination,	                    HITCOL_LENGTH,      &h->length,	                    HITCOL_TOS,         &h->tos,	                    HITCOL_PROTOCOL,    &h->protocol,	                    HITCOL_SERVICE,     &h->service,	                    -1);	return h; }/* [ hit_activated_cb ] * Callback for selecting a row in the hit view * TODO: Default action for hits? Hit->Rule helper maybe. */static void hit_activated_cb (GtkTreeView *treeview,                 GtkTreePath *path,                 GtkTreeViewColumn *arg2,                 gpointer data){	GtkTreeModel *model;	GtkTreeIter iter;	Hit *h;	model = gtk_tree_view_get_model (treeview);	gtk_tree_model_get_iter (model, &iter, path);	h = get_hit (model, iter);	print_hit (h);	free_hit (h);	unselect_all ();}/* [ hitview_get_selected_hit ] * Get the hit that is currently selected in the hitview */Hit *hitview_get_selected_hit (void){	GtkTreeSelection *selection;	GtkTreeModel *model;	GtkTreeIter iter;	Hit *h = NULL;	gboolean has_selected;	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (hitview));	has_selected = gtk_tree_selection_get_selected (selection,	                                                NULL,	                                                &iter);	if (has_selected) {		model = gtk_tree_view_get_model (GTK_TREE_VIEW (hitview));		h = get_hit (model, iter);	}	return h;}GList *hitview_get_all_hits (void){	GList *hits = NULL;	Hit *h;	GtkTreeModel *model;	GtkTreeIter iter;	model = gtk_tree_view_get_model (GTK_TREE_VIEW (hitview));	if (gtk_tree_model_get_iter_first (model, &iter)) {		do {			h = get_hit (model, iter);			hits = g_list_append (hits, h);		} while (gtk_tree_model_iter_next (model, &iter));	} else		printf ("Error compiling list of hits\n");	return hits;}/* [ hitview_button_press_cb ] * Pop up an menu when right clicking the hitview */static gbooleanhitview_button_press_cb (GtkWidget* widget, GdkEventButton* event){	gboolean retval = FALSE;	Hit *h;	GtkWidget *menu;	/* Clear hit state */	if (status_get_state () == STATUS_HIT)		status_set_state (STATUS_RUNNING);	h = hitview_get_selected_hit ();	if (h == NULL)		return retval;	else if (hit_is_outbound (h))		menu = menus_get_events_outbound_context_menu ();	else		menu = menus_get_events_inbound_context_menu ();	switch (event->button) {		case 1: break;				case 3: gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 		                        event->button, event->time);			retval = TRUE;			break;	}	g_free (h);	return retval;}/* [ append_filter_file ] * Append data to a events filter file */static gbooleanappend_filter_file (gchar *path, gchar *data){	GIOChannel* out;	GError *error = NULL;	out = g_io_channel_new_file (path, "a", &error);	if (out == NULL) {		g_printerr ("Error reading file %s: %s\n", path, error->message);		return FALSE;	}	if (g_io_channel_write_chars (out, data, -1, NULL, &error) == G_IO_STATUS_NORMAL) {		g_io_channel_shutdown (out, TRUE, &error);		return TRUE;	} else {		g_io_channel_shutdown (out, FALSE, &error);		return FALSE;	}}/* [ disable_events_selected_source ] * Disable events from the selected source */voidhitview_disable_events_selected_source (void){	Hit *h;	gchar *data;	h = hitview_get_selected_hit ();	if (h == NULL)		return;	data = g_strconcat (h->source, "\n", NULL);	append_filter_file (FIRESTARTER_FILTER_HOSTS_SCRIPT, data);	g_free (h);	g_free (data);	restart_firewall_if_active ();}voidhitview_disable_events_selected_port (void){	Hit *h;	gchar *data;	h = hitview_get_selected_hit ();	if (h == NULL)		return;	data = g_strconcat (h->port, "\n", NULL);	append_filter_file (FIRESTARTER_FILTER_PORTS_SCRIPT, data);	g_free (h);	g_free (data);	restart_firewall_if_active ();}voidhitview_allow_host (void){	Hit *h;	h = hitview_get_selected_hit ();	if (h == NULL)		return;	if (hit_is_outbound (h))		policyview_create_rule (RULETYPE_OUTBOUND_ALLOW_TO, h);	else		policyview_create_rule (RULETYPE_INBOUND_ALLOW_FROM, h);	g_free (h);}voidhitview_allow_service (void){	Hit *h;	h = hitview_get_selected_hit ();	if (h == NULL)		return;	if (hit_is_outbound (h))		policyview_create_rule (RULETYPE_OUTBOUND_ALLOW_SERVICE, h);	else		policyview_create_rule (RULETYPE_INBOUND_ALLOW_SERVICE, h);	g_free (h);}voidhitview_allow_service_from (void){	Hit *h;	h = hitview_get_selected_hit ();	if (h == NULL)		return;	if (hit_is_outbound (h))		policyview_create_rule (RULETYPE_OUTBOUND_ALLOW_SERVICE_FROM, h);	else		policyview_create_rule (RULETYPE_INBOUND_ALLOW_SERVICE_FROM, h);	g_free (h);}/* [ lookup_selected_hit ] * Resolve the IP address/hostname from the selected line in hitview */voidhitview_lookup_selected_hit (void){	GtkTreeSelection *selection;	GtkTreeIter iter;	static GtkTreeModel *model = NULL;	gchar *source, *destination;	gchar *hostname = NULL;	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (hitview));	if (!gtk_tree_selection_get_selected (selection, NULL, &iter))		return;	if (!model)		model = gtk_tree_view_get_model (GTK_TREE_VIEW (hitview));	gtk_tree_model_get (model, &iter,	                    HITCOL_SOURCE, &source,			    HITCOL_DESTINATION, &destination,	                    -1);	hostname = lookup_ip (source);	if (hostname != NULL)		gtk_list_store_set (hitstore, &iter,	        	            HITCOL_SOURCE, hostname,	         	           -1);	hostname = lookup_ip (destination);	if (hostname != NULL)		gtk_list_store_set (hitstore, &iter,	        	            HITCOL_DESTINATION, hostname,	         	           -1);	gtk_tree_view_columns_autosize (GTK_TREE_VIEW (hitview));	g_free (source);	g_free (destination);}/* [ month_number ] * Convert a three letter month identifier to a number */static intmonth_number (gchar *month){	int num = 0;	if (strcmp (month, "Jan") == 0)		num = 1;	else if (strcmp (month, "Feb") == 0)		num = 2;	else if (strcmp (month, "Mar") == 0)		num = 3;	else if (strcmp (month, "Apr") == 0)		num = 4;	else if (strcmp (month, "May") == 0)		num = 5;	else if (strcmp (month, "Jun") == 0)		num = 6;	else if (strcmp (month, "Jul") == 0)		num = 7;	else if (strcmp (month, "Aug") == 0)		num = 8;	else if (strcmp (month, "Sep") == 0)		num = 9;	else if (strcmp (month, "Oct") == 0)		num = 10;	else if (strcmp (month, "Nov") == 0)		num = 11;	else if (strcmp (month, "Dec") == 0)		num = 12;	return num;}/* [ time_sort_func ] * Function for sorting the time column */static inttime_sort_func (GtkTreeModel *model, 	        GtkTreeIter  *a, 	        GtkTreeIter  *b, 	        gpointer      user_data){	enum { MONTH, DATE, CLOCK };	gchar *data1, *data2;	gchar **time1, **time2;	gint month1, month2;	gint day1, day2;	gint sort = 0;	gtk_tree_model_get (model, a, HITCOL_TIME, &data1, -1);	gtk_tree_model_get (model, b, HITCOL_TIME, &data2, -1);	time1 = g_strsplit (data1, " ", 3);	time2 = g_strsplit (data2, " ", 3);	month1 = month_number (time1[MONTH]);	month2 = month_number (time2[MONTH]); 	/* Compare first month, then the day, and last the clock */	if (month1 != month2)		sort = ((month1 < month2) ? -1:1);	else {		day1 = atoi (time1[DATE]);		day2 = atoi (time2[DATE]);		if (day1 != day2)			sort = ((day1 < day2) ? -1:1);		else			sort = strcasecmp (time1[CLOCK], time2[CLOCK]);	}	g_free (data1);	g_free (data2);	g_strfreev (time1);	g_strfreev (time2);	return sort;}/* [ num_sort_func ] * Function for sorting a (text) column with only numbers in it */static intnum_sort_func (GtkTreeModel *model, 	       GtkTreeIter  *a, 	       GtkTreeIter  *b, 	       gpointer      column){	gchar *data1, *data2;	gint n1, n2;	gtk_tree_model_get (model, a, (gint)column, &data1, -1);	gtk_tree_model_get (model, b, (gint)column, &data2, -1);	n1 = atoi (data1);	n2 = atoi (data2);	g_free (data1);	g_free (data2);	if (n1 == n2)		return 0;	else		return ((n1 < n2) ? -1:1);}/* [ copy_selected_hit ] * Copy the selected hit to the clipboard */voidcopy_selected_hit (void){	Hit *h;	gchar *text;	GtkClipboard *cb;	h = hitview_get_selected_hit ();	if (h == NULL)		return;	cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);	text = g_strconcat (		"Time: ", h->time,		" Source: ", h->source,		" Destination: ", h->destination,		" In IF: ", h->in,		" Out IF: ", h->out,		" Port: ", h->port,		" Length: ", h->length,		" ToS: ", h->tos,		" Protocol: ", h->protocol,		" Service: ", h->service,		NULL);	gtk_clipboard_set_text (cb, text, strlen (text));	g_free (text);	free_hit (h);}/* [ create_hitview_page ] * Create the hitview */GtkWidget *create_hitview_page (void){	GtkWidget *hitpagebox;	GtkTreeModel *hitmodel;	GtkWidget *scrolledwin;	GtkWidget *frame;	GtkWidget *label;	hitpagebox = gtk_vbox_new (FALSE, 0);	hitmodel = create_hitlist_model ();	hitview = gtk_tree_view_new_with_model (hitmodel);	frame = gtk_frame_new (NULL);	label = gtk_label_new (NULL);	gtk_label_set_markup (GTK_LABEL (label), g_strconcat (		"<b>", _("Blocked Connections"), "</b>", NULL));	gtk_frame_set_label_widget (GTK_FRAME (frame), label);	gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);	gtk_box_pack_start (GTK_BOX (hitpagebox), frame, FALSE, FALSE, GNOME_PAD_SMALL);	scrolledwin = gtk_scrolled_window_new (NULL, NULL);	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),	                                GTK_POLICY_NEVER,	                                GTK_POLICY_ALWAYS);	gtk_box_pack_start (GTK_BOX (hitpagebox), scrolledwin, TRUE, TRUE, 0);	/* Pack the treeview into the scrolled window  */	gtk_container_add (GTK_CONTAINER (scrolledwin), hitview);	hitview_add_columns (GTK_TREE_VIEW (hitview));	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (hitview), TRUE);	gtk_tree_view_set_search_column (GTK_TREE_VIEW (hitview),		HITCOL_TIME);	g_signal_connect (G_OBJECT (hitview), "button_press_event",	                  G_CALLBACK (hitview_button_press_cb), NULL);	g_signal_connect (G_OBJECT (hitview), "row-activated",	                  G_CALLBACK (hit_activated_cb), NULL);	/* The list is by default sorted by time */	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (hitmodel), HITCOL_TIME, GTK_SORT_ASCENDING);	/* Some of the columns need special functions for sorting */	gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (hitmodel), HITCOL_TIME,	                                 time_sort_func, NULL, NULL);	gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (hitmodel), HITCOL_PORT,	                                 num_sort_func, (gpointer)HITCOL_PORT, NULL);	gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (hitmodel), HITCOL_LENGTH,	                                 num_sort_func, (gpointer)HITCOL_LENGTH, NULL);	g_object_unref (G_OBJECT (hitmodel));	/* Default icon states */	menus_events_clear_enabled (FALSE);	menus_events_save_enabled (FALSE);	gtk_widget_show_all (hitpagebox);	return hitpagebox;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -