[gnome-network][PATCH] netstat



Hi all, 

Two months ago I sent a patch that added IPv6 support for netstat tool.
Germán approved it but I forgot to apply it :-P so I send now again the
patch, but updated for the current CVS. 
The patch also fixes a minor bug: the tree_model doesn't change when you
toggle between protocol and route twice


-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Carlos Garcia Campos a.k.a. KaL
   elkalmail yahoo es
   carlosgc gnome org
 Grupo Linups
   Usuarios de SL/Linux de la UPSAM
 http://www.linups.org
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=		  
PGP key: http://pgp.rediris.es:11371/pks/lookup?op=get&search=0x523E6462
Index: netstat.c
===================================================================
RCS file: /cvs/gnome/gnome-network/gnome-netinfo/netstat.c,v
retrieving revision 1.11
diff -u -u -r1.11 netstat.c
--- netstat.c	20 Nov 2003 21:12:43 -0000	1.11
+++ netstat.c	9 Dec 2003 17:54:20 -0000
@@ -109,11 +109,11 @@
 	
 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (netinfo->routing))) {
 		/* Works for Solaris and Linux */
-		option = g_strdup ("-r");
+		option = g_strdup ("-rn -A inet -A inet6");
 	}
 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (netinfo->protocol))) {
 		/* Only works for Solaris */
-		option = g_strdup ("-A inet -ln");
+		option = g_strdup ("-A inet -A inet6 -ln");
 	}
 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (netinfo->multicast))) {
 		/* It works for Solaris and Linux */
@@ -266,10 +266,10 @@
 	g_return_if_fail (line != NULL);
 
 	count = strip_protocol_line (line, &data);
-	if (count == 7 || count == 8) {
+	if (count == 5 || count == 6) {
 #ifdef DEBUG
 		g_print ("%s\t%s:%s\t%s\n", data.protocol,
-			data.ip_src, data.port_src, data.state);
+			 data.ip_src, data.port_src, data.state);
 #endif /* DEBUG */
 		
 		/* Creation of GtkTreeView */
@@ -284,41 +284,43 @@
 		
 		model = gtk_tree_view_get_model (widget);
 		
-		if (protocol_model == NULL || gtk_tree_model_get_n_columns (model) != 4) {
+		/*if (protocol_model == NULL || gtk_tree_model_get_n_columns (model) != 4) {*/
+		if (protocol_model == NULL || protocol_model != model) {
 			clean_gtk_tree_view (widget);
 
 			protocol_model = GTK_TREE_MODEL (gtk_list_store_new
-						(4,
-						 G_TYPE_STRING,
-						 G_TYPE_STRING,
-						 G_TYPE_STRING,
-						 G_TYPE_STRING));
+							 (4,
+							  G_TYPE_STRING,
+							  G_TYPE_STRING,
+							  G_TYPE_STRING,
+							  G_TYPE_STRING));
 			netstat_create_protocol_model (widget);
 			
-			gtk_tree_view_set_model (widget, protocol_model);
+			/*gtk_tree_view_set_model (widget, protocol_model);*/
 		}
-
+		gtk_tree_view_set_model (widget, protocol_model);
+		
 		model = gtk_tree_view_get_model (widget);
 		
 		gtk_tree_view_get_cursor (widget, &path, NULL);
-
+		
 		if (path != NULL) {
 			gtk_tree_model_get_iter (model, &sibling, path);
 			gtk_list_store_insert_after (GTK_LIST_STORE
-							 (model),
-							 &iter,
-							 &sibling);
+						     (model),
+						     &iter,
+						     &sibling);
 		} else {
 			gtk_list_store_append (GTK_LIST_STORE
-						   (model), &iter);
+					       (model), &iter);
 		}
 
 		gtk_list_store_set (GTK_LIST_STORE (model), &iter,
-					0, data.protocol,
-					1, data.ip_src,
-					2, data.port_src,
-					3, data.state, -1);
-
+				    0, data.protocol,
+				    1, data.ip_src,
+				    2, data.port_src,
+				    3, data.state, -1);
+		
 		gtk_tree_view_set_model (widget, model);
 		path = gtk_tree_model_get_path (model, &iter);
 		gtk_tree_view_set_cursor (widget, path, NULL, FALSE);
@@ -331,17 +333,24 @@
 strip_protocol_line (gchar * line, netstat_protocol_data *data)
 {
 	gint count = 0;
-	gchar s6[30], s7[30];
+	gchar s6[30], laddr[50];
 	gint n2, n3;
-
-	line = g_strdelimit (line, ":", ' ');
+	gchar *port;
 
 	count = sscanf (line, NETSTAT_PROTOCOL_FORMAT,
 			data->protocol, &n2, &n3,
-			data->ip_src, data->port_src, 
-			s6, s7, data->state);
+			laddr, s6, data->state);
+	
+	port = g_strrstr (laddr, ":");
 	
-	if (count == 7) {
+	if (port != NULL) {
+		g_strlcpy (data->ip_src, laddr, 50 * sizeof (gchar));
+		data->ip_src[strlen (laddr) - strlen (port)] = '\0';
+		port ++;
+		g_strlcpy (data->port_src, port, 30 * sizeof (gchar));
+	}
+
+	if (count == 5) {
 		bzero (&(data)->state, 30);
 	}
 	
@@ -359,31 +368,31 @@
 	renderer = gtk_cell_renderer_text_new ();
 	/* Transport Protocol that runs over */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Protocol"), renderer, "text", 0, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Protocol"), renderer, "text", 0, NULL);
 	gtk_tree_view_append_column (widget, column);
 
 	renderer = gtk_cell_renderer_text_new ();
 	/* IP address where to accept connections */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("IP Source"), renderer, "text", 1, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("IP Source"), renderer, "text", 1, NULL);
 	gtk_tree_view_column_set_alignment (column, 0.5);
 	gtk_tree_view_append_column (widget, column);
 
 	renderer = gtk_cell_renderer_text_new ();
 	/* Number of port where the service is listening */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Port/Service"), renderer, "text", 2, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Port/Service"), renderer, "text", 2, NULL);
 	g_object_set (G_OBJECT (renderer), "xalign", 1.0, NULL);
 	gtk_tree_view_append_column (widget, column);
 
 	renderer = gtk_cell_renderer_text_new ();
 	/* State of the service (commonly LISTEN) */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("State"), renderer, "text", 3, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("State"), renderer, "text", 3, NULL);
 	gtk_tree_view_append_column (widget, column);
 
 	/* return model;*/
@@ -403,13 +412,13 @@
 
 	g_return_if_fail (GTK_IS_TREE_VIEW (widget));
 	g_return_if_fail (line != NULL);
-
+	
 	count = strip_route_line (line, &data);
-	if (count == 8) {
+	if ((count == 8) || (count == 7)) {
 #ifdef DEBUG
 		g_print ("%s\t%s:%s\t%d\t%s\n", data.destination,
-			data.gateway, data.netmask, data.metric,
-			data.iface);
+			 data.gateway, data.netmask, data.metric,
+			 data.iface);
 #endif /* DEBUG */
 
 		/* Creation of GtkTreeView */
@@ -424,19 +433,21 @@
 		
 		model = gtk_tree_view_get_model (widget);
 		
-		if (route_model == NULL || gtk_tree_model_get_n_columns (model) != 4) {
+		/*if (route_model == NULL || gtk_tree_model_get_n_columns (model) != 4) {*/
+		if (route_model == NULL || route_model != model) {
 			clean_gtk_tree_view (widget);
 			
 			route_model = GTK_TREE_MODEL (gtk_list_store_new
-				(4,
-				 G_TYPE_STRING,
-				 G_TYPE_STRING,
-				 G_TYPE_STRING,
-				 G_TYPE_STRING));
+						      (4,
+						       G_TYPE_STRING,
+						       G_TYPE_STRING,
+						       G_TYPE_STRING,
+						       G_TYPE_STRING));
 			netstat_create_route_model (widget);
 			
-			gtk_tree_view_set_model (widget, route_model);
+			/*gtk_tree_view_set_model (widget, route_model);*/
 		}
+		gtk_tree_view_set_model (widget, route_model);
 
 		model = gtk_tree_view_get_model (widget);
 		
@@ -473,13 +484,30 @@
 	gint count = 0;
 	gchar flags[30];
 	gint ref, use;
-
+	gchar dest[50];
+	gchar **items;
+	
 	count = sscanf (line, NETSTAT_ROUTE_FORMAT,
 			data->destination,
-			data->gateway, data->netmask, 
-			flags, &(data)->metric, &ref, &use, 
+			data->gateway, data->netmask,
+			flags, &(data)->metric, &ref, &use,
 			data->iface);
 	
+	if (count == 6) {
+		count = sscanf (line, NETSTAT_ROUTE6_FORMAT,
+				dest, data->netmask,
+				flags, &(data)->metric,
+				&ref, &use, data->iface);
+		
+		items = g_strsplit (dest, "/", 2);
+		if (items != NULL) {
+			g_strlcpy (data->destination, items[0], 50 * sizeof (gchar));
+			g_strlcpy (data->netmask, items[1], 50 * sizeof (gchar));
+			
+			g_strfreev (items);
+		}
+	}
+
 	return count;
 }
 
@@ -492,28 +520,28 @@
 
 	renderer = gtk_cell_renderer_text_new ();
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Destination"), renderer, "text", 0, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Destination"), renderer, "text", 0, NULL);
 	gtk_tree_view_append_column (widget, column);
-
+	
 	renderer = gtk_cell_renderer_text_new ();
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Gateway"), renderer, "text", 1, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Gateway"), renderer, "text", 1, NULL);
 	gtk_tree_view_column_set_alignment (column, 0.5);
 	gtk_tree_view_append_column (widget, column);
-
+	
 	renderer = gtk_cell_renderer_text_new ();
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Netmask"), renderer, "text", 2, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Netmask"), renderer, "text", 2, NULL);
 		
 	gtk_tree_view_append_column (widget, column);
-
+	
 	renderer = gtk_cell_renderer_text_new ();
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Interface"), renderer, "text", 3, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Interface"), renderer, "text", 3, NULL);
 	gtk_tree_view_append_column (widget, column);
 }
 /* END ROUTE */
@@ -551,7 +579,8 @@
 
 		model = gtk_tree_view_get_model (widget);
 		
-		if (multicast_model == NULL || gtk_tree_model_get_n_columns (model) != 3) {
+		/*if (multicast_model == NULL || gtk_tree_model_get_n_columns (model) != 3) {*/
+		if (multicast_model == NULL || multicast_model != model) {
 			clean_gtk_tree_view (widget);
 			
 			multicast_model = GTK_TREE_MODEL (gtk_list_store_new
@@ -561,8 +590,9 @@
 						 G_TYPE_STRING));
 			netstat_create_multicast_model (widget);
 			
-			gtk_tree_view_set_model (widget, multicast_model);
+			/*gtk_tree_view_set_model (widget, multicast_model);*/
 		}
+		gtk_tree_view_set_model (widget, multicast_model);
 
 		model = gtk_tree_view_get_model (widget);
 		
@@ -618,23 +648,23 @@
 	renderer = gtk_cell_renderer_text_new ();
 	/* Interface of multicast group associated */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Interface"), renderer, "text", 0, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Interface"), renderer, "text", 0, NULL);
 	gtk_tree_view_append_column (widget, column);
 
 	renderer = gtk_cell_renderer_text_new ();
 	/* Members of multicast group */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Member"), renderer, "text", 1, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Member"), renderer, "text", 1, NULL);
 	g_object_set (G_OBJECT (renderer), "xalign", 0.5, NULL);
 	gtk_tree_view_append_column (widget, column);
-
+	
 	renderer = gtk_cell_renderer_text_new ();
 	/* Multicast group */
 	column =
-	    gtk_tree_view_column_new_with_attributes
-	    (_("Group"), renderer, "text", 2, NULL);
+		gtk_tree_view_column_new_with_attributes
+		(_("Group"), renderer, "text", 2, NULL);
 	gtk_tree_view_append_column (widget, column);
 }
 /* END MULTICAST */
@@ -657,7 +687,7 @@
 		column = gtk_tree_view_get_column (widget, i-1);
 		gtk_tree_view_remove_column (widget, column);
 	}
-
+	
 	g_list_free (columns);
 }
 
Index: netstat.h
===================================================================
RCS file: /cvs/gnome/gnome-network/gnome-netinfo/netstat.h,v
retrieving revision 1.4
diff -u -u -r1.4 netstat.h
--- netstat.h	21 Jul 2003 02:31:57 -0000	1.4
+++ netstat.h	9 Dec 2003 17:54:20 -0000
@@ -25,9 +25,13 @@
 
 #if defined(__linux__)
 	/* proto 0 0 ip port ip port state */
-#   define NETSTAT_PROTOCOL_FORMAT "%s %d %d %s %s %s %s %s" 
+/*#   define NETSTAT_PROTOCOL_FORMAT "%s %d %d %s %s %s %s %s" 
 #   define NETSTAT_ROUTE_FORMAT "%s %s %s %s %d %d %d %s" 
-#   define NETSTAT_MULTICAST_FORMAT "%s %d %s" 
+#   define NETSTAT_MULTICAST_FORMAT "%s %d %s" */
+#   define NETSTAT_PROTOCOL_FORMAT "%s %d %d %s %s %s"
+#   define NETSTAT_ROUTE_FORMAT "%s %s %s %s %d %d %d %s"
+#   define NETSTAT_ROUTE6_FORMAT "%s %s %s %d %d %d %s"
+#   define NETSTAT_MULTICAST_FORMAT "%s %d %s"
 
 #endif
 
@@ -42,7 +46,7 @@
 	
 struct _netstat_protocol_data {
 	gchar protocol[30];
-	gchar ip_src[30];
+	gchar ip_src[50];
 	gchar port_src[30];
 //	gint  port_src;
 	gchar state[30];
@@ -51,8 +55,8 @@
 typedef struct _netstat_route_data netstat_route_data;
 	
 struct _netstat_route_data {
-	gchar destination[30];
-	gchar gateway[30];
+	gchar destination[50];
+	gchar gateway[50];
 	gchar netmask[30];
 	gint metric;
 	gchar iface[30];
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-network/gnome-netinfo/ChangeLog,v
retrieving revision 1.102
diff -u -u -r1.102 ChangeLog
--- ChangeLog	5 Dec 2003 02:27:22 -0000	1.102
+++ ChangeLog	9 Dec 2003 17:54:25 -0000
@@ -1,3 +1,17 @@
+2003-12-09  Carlos García Campos <carlosgc gnome org>
+
+	* netstat.c (get_active_option, strip_protocol_line,
+          strip_route_line, *_tree_insert): IPv6 support
+
+	* netstat.c (*_tree_insert): the tree_model doesn't change when 
+	you toggle between protocol and route twice
+
 2003-12-01  Bastien Nocera  <hadess hadess net>
                                                                                 
 	* main.c: (load_whois_widgets_from_xml): we don't have a "user"

Attachment: signature.asc
Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada digitalmente



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]