PERFORCE change 166187 for review

Gabor Pali pgj at FreeBSD.org
Fri Jul 17 00:51:59 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=166187

Change 166187 by pgj at petymeg-current on 2009/07/17 00:51:21

	Improve nettop:
	- Create a screen() framework, so user can switch between different
	  monitoring modes.
	- Add a screen for local and inet connections.
	- Add a header and footer section.

Affected files ...

.. //depot/projects/soc2009/pgj_libstat/src/usr.bin/nettop/main.c#9 edit

Differences ...

==== //depot/projects/soc2009/pgj_libstat/src/usr.bin/nettop/main.c#9 (text+ko) ====

@@ -33,16 +33,18 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-static void screen(struct socket_type_list *local,
-    struct socket_type_list *inet);
+static void header(void);
+static void footer(void);
+static void screen_local(void);
+static void screen_inet(void);
 
 int
 main(int argc, char *argv[])
 {
 	char ch;
 	char running = 1;
-	struct socket_type_list *local, *inet;
-	int st_flags, refresh = 0;
+	int refresh = 0;
+	void (*screen)(void) = screen_local;
 
 	initscr();
 	clear();
@@ -52,25 +54,26 @@
 		/* Handle input. */
 		ch = getch();
 		switch (ch) {
+		case 'L':
+		case 'l':
+			screen = screen_local;
+			break;
+		case 'I':
+		case 'i':
+			screen = screen_inet;
+			break;
 		case 'Q':
 		case 'q':
 			running = 0;
 			break;
 		}
 		if (refresh == 0) {
-			/* Get data. */
-			local = netstat_stl_alloc();
-			inet = netstat_stl_alloc();
-			st_flags = NETSTAT_SOCKET_ALL;
-			netstat_socket(PF_LOCAL, 0, 0, local, st_flags, NULL);
-			netstat_socket(PF_INET, 0, 0, inet, st_flags, NULL);
-			netstat_socket(PF_INET6, 0, 0, inet, st_flags, NULL);
 			/* Render screen. */
 			clear();
-			screen(local, inet);
+			header();
+			screen();
+			footer();
 			refresh();
-			netstat_stl_free(local);
-			netstat_stl_free(inet);
 			refresh = 3; /* Refresh it only in every 4th cycle */
 		}
 		refresh--;
@@ -82,16 +85,34 @@
 }
 
 void
-screen(struct socket_type_list *local, struct socket_type_list *inet)
+header(void)
+{
+	attron(A_STANDOUT);
+	mvprintw(0, 0, " Very Simple Network Monitor ");
+	attroff(A_STANDOUT);
+}
+
+void
+footer(void)
+{
+	attron(A_STANDOUT);
+	mvprintw(24, 0, " Q - Quit, L - PF_LOCAL, I - PF_INET ");
+	attroff(A_STANDOUT);
+}
+
+void
+screen_local(void)
 {
+	struct socket_type_list *list;
 	struct socket_type_iterator *stip;
 	const struct socket_type *stp;
-	struct addr_type *atp, *laddr, *faddr;
-	int row;
+	struct addr_type *atp;
+	int row, st_flags;
+
+	st_flags = NETSTAT_SOCKET_ALL;
+	list = netstat_stl_alloc();
+	netstat_socket(PF_LOCAL, 0, 0, list, st_flags, NULL);
 
-	attron(A_STANDOUT);
-	mvprintw(0, 0, "Very Simple Network Monitor  (Press Q to quit)");
-	attroff(A_STANDOUT);
 	attron(A_BOLD);
 	mvprintw(2, 0, "Active UNIX domain sockets");
 	mvprintw(3, 0,
@@ -99,8 +120,8 @@
 	    "Address", "Type", "Recv-Q", "Send-Q", "Inode", "Conn",
 	    "Refs", "Nextref");
 	attroff(A_BOLD);
-	netstat_sti_alloc(local, &stip);
-	for (stp = netstat_sti_first(stip), row = 4; stp != NULL && row < 14;
+	netstat_sti_alloc(list, &stip);
+	for (stp = netstat_sti_first(stip), row = 4; stp != NULL && row < 24;
 	    stp = netstat_sti_next(stip), row++) {
 		mvprintw(row, 0,
 		    "%8lx %-6.6s %6u %6u %8lx %8lx %8lx %8lx",
@@ -117,15 +138,32 @@
 		}
 	}
 	netstat_sti_free(stip);
+	netstat_stl_free(list);
+}
+
+void
+screen_inet(void)
+{
+	struct socket_type_list *list;
+	struct socket_type_iterator *stip;
+	const struct socket_type *stp;
+	struct addr_type *laddr, *faddr;
+	int row, st_flags;
+
+	st_flags = NETSTAT_SOCKET_ALL;
+	list = netstat_stl_alloc();
+	netstat_socket(PF_INET, 0, 0, list, st_flags, NULL);
+	netstat_socket(PF_INET6, 0, 0, list, st_flags, NULL);
+
 	attron(A_BOLD);
-	mvprintw(15, 0, "Active Internet connections (including servers)");
-	mvprintw(16, 0,
+	mvprintw(2, 0, "Active Internet connections (including servers)");
+	mvprintw(3, 0,
 	    "%-5.5s %-6.6s %-6.6s %-17.17s %-6.6s %-17.17s %-6.6s (state)",
 	    "Proto", "Recv-Q", "Send-Q",
 	    "Local Address", "Port", "Foreign Address", "Port");
 	attroff(A_BOLD);
-	netstat_sti_alloc(inet, &stip);
-	for (stp = netstat_sti_first(stip), row = 17; stp != NULL && row < 25;
+	netstat_sti_alloc(list, &stip);
+	for (stp = netstat_sti_first(stip), row = 4; stp != NULL && row < 24;
 	    stp = netstat_sti_next(stip), row++) {
 		laddr = netstat_st_get_address(stp, 0);
 		faddr = netstat_st_get_address(stp, 1);
@@ -141,4 +179,5 @@
 		    netstat_st_get_tcpstate(stp));
 	}
 	netstat_sti_free(stip);
+	netstat_stl_free(list);
 }


More information about the p4-projects mailing list