From: Colin Patrick McCabe Date: Wed, 11 Aug 2010 19:00:57 +0000 (-0700) Subject: Add show_default_sockopts program to do just that. X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5b653d2b0cc30f2bbdeb73ef3e0ffb9b39bd7b5;p=cmccabe-bin Add show_default_sockopts program to do just that. Also update Makefile. --- diff --git a/Makefile b/Makefile index 22ef97c..20927e9 100644 --- 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 index 0000000..cc5f0d4 --- /dev/null +++ b/show_default_sockopts.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include + +/* + * 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; +}