Add show_default_sockopts program to do just that.
[cmccabe-bin] / show_default_sockopts.c
1 #include <netinet/in.h>
2 #include <netinet/tcp.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/socket.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 /*
10  * show_default_sockopts
11  *
12  * Show the default socket options 
13  *
14  * Colin McCabe
15  */
16
17 struct option_t {
18         int opt;
19         const char *descr;
20 };
21
22 void option_query(int s, int type, struct option_t *p)
23 {
24         int value, vlen = 4;
25
26         for (; p->descr != NULL; ++p) {
27                 if (getsockopt(s, type, p->opt, (void *)&value, &vlen) == -1) {
28                         fprintf(stderr,"getsocketopts: could not test %s\n",
29                                 p->descr);
30                 }
31                 else {
32                         printf("Default %s = %d\n", p->descr, value);
33                 }
34         }
35 }
36
37 struct option_t so_option[] = {
38 #ifdef SO_ACCEPTCONN
39         { SO_ACCEPTCONN, "SO_ACCEPTCON: accepting connections" },
40 #endif
41 #ifdef SO_BROADCAST
42         { SO_BROADCAST, "SO_BROADCAST, broadcast allowed" },
43 #endif
44 #ifdef SO_REUSEADDR
45         { SO_REUSEADDR, "SO_REUSEADDR, address recycling" },
46 #endif
47 #ifdef SO_KEEPALIVE
48         { SO_KEEPALIVE, "SO_KEEPALIVE, send keepalive packets" },
49 #endif
50         /* { SO_LINGER, "SO_LINGER, lingers on close"}, */
51 #ifdef SO_OOBINLINE
52         { SO_OOBINLINE, "SO_OOBINLINE, oob data folded inline"},
53 #endif
54 #ifdef SO_SNDBUF
55         { SO_SNDBUF, "SO_SNDBUF, send buffer size" },
56 #endif
57 #ifdef SO_RCVLOWAT
58         { SO_RCVLOWAT, "SO_RCVLOWAT, receive low-water mark"},
59 #endif
60 #ifdef SO_SNDLOWAT
61         { SO_SNDLOWAT, "SO_SNDLOWAT, send low-water mark"},
62 #endif
63 #ifdef SO_RCVTIMEO
64         { SO_RCVTIMEO, "SO_RCVTIMEO, receive timeout"},
65 #endif
66 #ifdef SO_SNDTIMEO
67         { SO_SNDTIMEO, "SO_SNDTIMEO, send timeout"},
68 #endif
69 #ifdef SO_RCVBUF
70         { SO_RCVBUF, "SO_RCVBUF, receive buffer size"},
71 #endif
72 #ifdef SO_ERROR
73         { SO_ERROR, "SO_ERROR, error status"},
74 #endif
75 #ifdef SO_TYPE
76         { SO_TYPE, "SO_TYPE, socket type"},
77 #endif
78         { 0, NULL }
79 };
80
81 struct option_t tcp_option[] = {
82 #ifdef TCP_MAXSEG
83           { TCP_MAXSEG, "TCP_MAXSEG, maximum segment size (mss)"},
84 #endif
85 #ifdef TCP_NODELAY
86           { TCP_NODELAY, "TCP_NODELAY, send even tiny packets"},
87 #endif
88           { 0, NULL}
89 };
90
91 int main(int argc, char **argv)
92 {
93         int s;
94         if ((getuid()) != 0) {
95                 fprintf(stderr,"%s: you must be root to run this program.\n", argv[0]);
96                 return 1;
97         }
98
99         if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
100                 perror("failed to create a socket to test.");
101                 return 1;
102         }
103         option_query(s, SOL_SOCKET, so_option);
104         option_query(s, IPPROTO_TCP, tcp_option);
105
106         (void) close(s);
107         return 0;
108 }