Add show_default_sockopts program to do just that.
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 11 Aug 2010 19:00:57 +0000 (12:00 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 11 Aug 2010 19:00:57 +0000 (12:00 -0700)
Also update Makefile.

Makefile
show_default_sockopts.c [new file with mode: 0644]

index 22ef97c..20927e9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,9 +4,11 @@ all:           errno_speak simple_time
 
 errno_speak:   errno_speak.o
 
+show_default_sockopts: show_default_sockopts.o
+
 simple_time:   simple_time.o
 
 vimstart:      vimstart.o
 
 clean:
-       rm -rf errno_speak simple_time *.o
+       rm -rf errno_speak show_default_sockopts simple_time vimstart *.o
diff --git a/show_default_sockopts.c b/show_default_sockopts.c
new file mode 100644 (file)
index 0000000..cc5f0d4
--- /dev/null
@@ -0,0 +1,108 @@
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+/*
+ * show_default_sockopts
+ *
+ * Show the default socket options 
+ *
+ * Colin McCabe
+ */
+
+struct option_t {
+       int opt;
+       const char *descr;
+};
+
+void option_query(int s, int type, struct option_t *p)
+{
+       int value, vlen = 4;
+
+       for (; p->descr != NULL; ++p) {
+               if (getsockopt(s, type, p->opt, (void *)&value, &vlen) == -1) {
+                       fprintf(stderr,"getsocketopts: could not test %s\n",
+                               p->descr);
+               }
+               else {
+                       printf("Default %s = %d\n", p->descr, value);
+               }
+       }
+}
+
+struct option_t so_option[] = {
+#ifdef SO_ACCEPTCONN
+       { SO_ACCEPTCONN, "SO_ACCEPTCON: accepting connections" },
+#endif
+#ifdef SO_BROADCAST
+       { SO_BROADCAST, "SO_BROADCAST, broadcast allowed" },
+#endif
+#ifdef SO_REUSEADDR
+       { SO_REUSEADDR, "SO_REUSEADDR, address recycling" },
+#endif
+#ifdef SO_KEEPALIVE
+       { SO_KEEPALIVE, "SO_KEEPALIVE, send keepalive packets" },
+#endif
+       /* { SO_LINGER, "SO_LINGER, lingers on close"}, */
+#ifdef SO_OOBINLINE
+       { SO_OOBINLINE, "SO_OOBINLINE, oob data folded inline"},
+#endif
+#ifdef SO_SNDBUF
+       { SO_SNDBUF, "SO_SNDBUF, send buffer size" },
+#endif
+#ifdef SO_RCVLOWAT
+       { SO_RCVLOWAT, "SO_RCVLOWAT, receive low-water mark"},
+#endif
+#ifdef SO_SNDLOWAT
+       { SO_SNDLOWAT, "SO_SNDLOWAT, send low-water mark"},
+#endif
+#ifdef SO_RCVTIMEO
+       { SO_RCVTIMEO, "SO_RCVTIMEO, receive timeout"},
+#endif
+#ifdef SO_SNDTIMEO
+       { SO_SNDTIMEO, "SO_SNDTIMEO, send timeout"},
+#endif
+#ifdef SO_RCVBUF
+       { SO_RCVBUF, "SO_RCVBUF, receive buffer size"},
+#endif
+#ifdef SO_ERROR
+       { SO_ERROR, "SO_ERROR, error status"},
+#endif
+#ifdef SO_TYPE
+       { SO_TYPE, "SO_TYPE, socket type"},
+#endif
+       { 0, NULL }
+};
+
+struct option_t tcp_option[] = {
+#ifdef TCP_MAXSEG
+         { TCP_MAXSEG, "TCP_MAXSEG, maximum segment size (mss)"},
+#endif
+#ifdef TCP_NODELAY
+         { TCP_NODELAY, "TCP_NODELAY, send even tiny packets"},
+#endif
+         { 0, NULL}
+};
+
+int main(int argc, char **argv)
+{
+       int s;
+       if ((getuid()) != 0) {
+               fprintf(stderr,"%s: you must be root to run this program.\n", argv[0]);
+               return 1;
+       }
+
+       if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+               perror("failed to create a socket to test.");
+               return 1;
+       }
+       option_query(s, SOL_SOCKET, so_option);
+       option_query(s, IPPROTO_TCP, tcp_option);
+
+       (void) close(s);
+       return 0;
+}