Re: [gnome-network][PATCH] netstat



El mar, 16-12-2003 a las 20:59, Carlos Garcia Campos escribió:
> El mar, 16-12-2003 a las 00:33, William Jon McCann escribió:
> > Carlos Garcia Campos wrote:
> > > I have added a function that check if IPv6 is enabled in the system and
> > > only uses -A inet6 when it is possible. I have attached an updated
> > > patch.
> > > 
> > > 
> > 
> > Hmm, that function always returns true for me.  Does the following work 
> > for you?  We won't only need to add cases here for systems that don't 
> > handle IPv6 gracefully (like Solaris 9 does).
> 
> I can't test it without IPv6 enabled, but the solution you propose below
> is valid for me. I thought doing a gethostbyname with the :: address in
> a system without IPv6 would return NULL, but it doesn't seem so.
> 
> > gboolean
> > netinfo_is_ipv6_enabled (void)
> > {
> > 
> > #if defined(__linux__)
> > 	return g_file_test ("/proc/net/if_inet6", G_FILE_TEST_EXISTS);
> > #endif
> > 
> > 	return TRUE;
> > }

Hi all!!! Merry Christmas!!! 

I have updated the function and I have tested it in a system without
IPv6 and it seems to work correctly. 

Ok to commit? :-)

-- 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 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: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-network/gnome-netinfo/ChangeLog,v
retrieving revision 1.104
diff -u -u -r1.104 ChangeLog
--- ChangeLog	17 Dec 2003 18:06:37 -0000	1.104
+++ ChangeLog	25 Dec 2003 16:12:58 -0000
@@ -1,3 +1,20 @@
+2003-12-25  Carlos García Campos <carlosgc gnome org>
+
+	* netinfo.[ch] (is_ipv6_enable): added is_ipv6_enable function to know
+	if IPv6 is enabled in the system
+
+	* netinfo.c (toggle_state): change the cursor to GDK_WATCH when
+        a process is running
+
+	* netstat.[ch] (get_active_option, strip_protocol_line,
+        strip_route_line, *_tree_insert): IPv6 support
+
+	* netstat.[ch] (get_active_option): checks if IPv6 is enabled before
+	use inet6 protocol
+
+	* netstat.c (*_tree_insert): the tree_model doesn't change when
+        you toggle between protocol and route twice
+
 2003-12-16  William Jon McCann  <mccann jhu edu>
 
 	* main.c (load_finger_widgets_from_xml) 
Index: netinfo.c
===================================================================
RCS file: /cvs/gnome/gnome-network/gnome-netinfo/netinfo.c,v
retrieving revision 1.18
diff -u -u -r1.18 netinfo.c
--- netinfo.c	17 Dec 2003 18:06:37 -0000	1.18
+++ netinfo.c	25 Dec 2003 16:12:58 -0000
@@ -74,6 +74,29 @@
 			    host);
 }
 
+gboolean
+netinfo_is_ipv6_enable ()
+{
+	gint sock;
+	struct sockaddr_in6 sin6;
+	guint len;
+
+	if ((sock = socket (PF_INET6, SOCK_STREAM, 0)) == -1) {
+		return FALSE;
+	}
+	} else {
+		len = sizeof (struct sockaddr_in6);
+		if (getsockname (sock, (struct sockaddr *)&sin6, (void *)&len) < 0) {
+			close (sock);
+			return FALSE;
+		} else {
+			close (sock);
+			return TRUE;
+		}
+	}
+}
+
+
 const gchar *
 netinfo_get_user (Netinfo * netinfo)
 {
@@ -202,7 +225,7 @@
 netinfo_text_buffer_insert (Netinfo * netinfo)
 {
 	gchar *dir = g_get_current_dir ();
-	gint child_pid, pout, perr;
+	gint child_pid, pout/*, perr*/;
 	GIOChannel *channel;
 	GError *err = NULL;
 
Index: netinfo.h
===================================================================
RCS file: /cvs/gnome/gnome-network/gnome-netinfo/netinfo.h,v
retrieving revision 1.17
diff -u -u -r1.17 netinfo.h
--- netinfo.h	17 Dec 2003 18:06:37 -0000	1.17
+++ netinfo.h	25 Dec 2003 16:12:58 -0000
@@ -117,6 +117,7 @@
 gushort netinfo_get_count (Netinfo * netinfo);
 const gchar * netinfo_get_host (Netinfo * netinfo);
 const gchar * netinfo_get_user (Netinfo * netinfo);
+gboolean netinfo_is_ipv6_enable ();
 void netinfo_set_host (Netinfo * netinfo, const gchar *host);
 void netinfo_set_user (Netinfo * netinfo, const gchar *user);
 gint netinfo_get_ip_version (Netinfo * netinfo);
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	25 Dec 2003 16:13:06 -0000
@@ -109,11 +109,19 @@
 	
 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (netinfo->routing))) {
 		/* Works for Solaris and Linux */
-		option = g_strdup ("-r");
+		if (netinfo_is_ipv6_enable ()) {
+			option = g_strdup ("-rn -A inet -A inet6");
+		} else {
+			option = g_strdup ("-rn -A inet");
+		}
 	}
 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (netinfo->protocol))) {
 		/* Only works for Solaris */
-		option = g_strdup ("-A inet -ln");
+		if (netinfo_is_ipv6_enable ()) {
+			option = g_strdup ("-A inet -A inet6 -ln");
+		} else {
+			option = g_strdup ("-A inet -ln");
+		}
 	}
 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (netinfo->multicast))) {
 		/* It works for Solaris and Linux */
@@ -266,10 +274,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 +292,40 @@
 		
 		model = gtk_tree_view_get_model (widget);
 		
-		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);
+		
 		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 +338,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);
 	
-	if (count == 7) {
+	port = g_strrstr (laddr, ":");
+	
+	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 +373,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 +417,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 +438,18 @@
 		
 		model = gtk_tree_view_get_model (widget);
 		
-		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);
 
 		model = gtk_tree_view_get_model (widget);
 		
@@ -473,13 +486,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 +522,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 +581,7 @@
 
 		model = gtk_tree_view_get_model (widget);
 		
-		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
@@ -560,9 +590,8 @@
 						 G_TYPE_STRING,
 						 G_TYPE_STRING));
 			netstat_create_multicast_model (widget);
-			
-			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 +647,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 +686,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	25 Dec 2003 16:13:07 -0000
@@ -25,9 +25,10 @@
 
 #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_ROUTE_FORMAT "%s %s %s %s %d %d %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 +43,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 +52,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];

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]