sockstat tcp/udp switches

Josh Carroll josh.carroll at gmail.com
Thu Nov 2 23:07:17 UTC 2006


>         I haven't tested yet, but I think in the options structure you
> should use :
>
> +                             {"ipv6", 0, NULL, '6'},
>
> instead of:
> +                             {"ipv6", 0, NULL, 0},

Oops, thanks for catching that. Fixed that in the new patch below.

> also for portability you should use:
> no_argument or required_argument as a second field....

Thank you for the feedback, I've modified that and the new patch is below.

Thanks,
Josh



--- sockstat.c.orig	Thu Nov  2 15:01:16 2006
+++ sockstat.c	Thu Nov  2 15:02:32 2006
@@ -58,6 +58,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <getopt.h>

 static int	 opt_4;		/* Show IPv4 sockets */
 static int	 opt_6;		/* Show IPv6 sockets */
@@ -65,6 +66,8 @@
 static int	 opt_l;		/* Show listening sockets */
 static int	 opt_u;		/* Show Unix domain sockets */
 static int	 opt_v;		/* Verbose mode */
+static int	 opt_tcp;		/* show tcp */
+static int	 opt_udp;		/* show udp */

 static int	*ports;

@@ -584,8 +587,20 @@
 main(int argc, char *argv[])
 {
 	int o;
+	static struct option options[] = {
+				{"ipv4", no_argument, NULL, '4'},
+				{"ipv6", no_argument, NULL, '6'},
+				{"connected", no_argument, NULL, 'c'},
+				{"listening", no_argument, NULL, 'l'},
+				{"unix", no_argument, NULL, 'u'},
+				{"verbose", no_argument, NULL, 'v'},
+				{"port", required_argument, NULL, 'p'},
+				{"tcp", no_argument, NULL, 't'},
+				{"udp", no_argument, NULL, 'd'},
+				{NULL, 0, NULL, 0}
+	};

-	while ((o = getopt(argc, argv, "46clp:uv")) != -1)
+	while ((o = getopt_long_only(argc, argv, "46clp:uvtd", options, NULL)) != -1)
 		switch (o) {
 		case '4':
 			opt_4 = 1;
@@ -608,6 +623,12 @@
 		case 'v':
 			++opt_v;
 			break;
+		case 't':
+			opt_tcp = 1;
+			break;
+		case 'd':
+			opt_udp = 1;
+			break;
 		default:
 			usage();
 		}
@@ -618,20 +639,35 @@
 	if (argc > 0)
 		usage();

-	if (!opt_4 && !opt_6 && !opt_u)
-		opt_4 = opt_6 = opt_u = 1;
+	if (!opt_4 && !opt_6) {
+		opt_4 = opt_6 = 1;
+		
+		if(!opt_u) {
+			if(opt_tcp || opt_udp)
+				opt_u = 0;
+		} else {
+			opt_4 = opt_6 = opt_u = 1;
+		}
+	}
+
 	if (!opt_c && !opt_l)
 		opt_c = opt_l = 1;

+	if(!opt_tcp && !opt_udp)
+		opt_tcp = opt_udp = 1;
+
 	if (opt_4 || opt_6) {
-		gather_inet(IPPROTO_TCP);
-		gather_inet(IPPROTO_UDP);
+		if(opt_tcp)
+			gather_inet(IPPROTO_TCP);
+		if(opt_udp)
+			gather_inet(IPPROTO_UDP);
 		gather_inet(IPPROTO_DIVERT);
 	}
 	if (opt_u) {
 		gather_unix(SOCK_STREAM);
 		gather_unix(SOCK_DGRAM);
 	}
+
 	getfiles();
 	display();


More information about the freebsd-hackers mailing list