Makefile: add pickrand
[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;
25         socklen_t vlen = 4;
26
27         for (; p->descr != NULL; ++p) {
28                 if (getsockopt(s, type, p->opt, (void *)&value, &vlen) == -1) {
29                         fprintf(stderr,"getsocketopts: could not test %s\n",
30                                 p->descr);
31                 }
32                 else {
33                         printf("Default %s = %d\n", p->descr, value);
34                 }
35         }
36 }
37
38 struct option_t so_option[] = {
39 #ifdef SO_ACCEPTCONN
40         { SO_ACCEPTCONN, "SO_ACCEPTCON: accepting connections" },
41 #endif
42 #ifdef SO_BROADCAST
43         { SO_BROADCAST, "SO_BROADCAST, broadcast allowed" },
44 #endif
45 #ifdef SO_REUSEADDR
46         { SO_REUSEADDR, "SO_REUSEADDR, address recycling" },
47 #endif
48 #ifdef SO_KEEPALIVE
49         { SO_KEEPALIVE, "SO_KEEPALIVE, send keepalive packets" },
50 #endif
51         /* { SO_LINGER, "SO_LINGER, lingers on close"}, */
52 #ifdef SO_OOBINLINE
53         { SO_OOBINLINE, "SO_OOBINLINE, oob data folded inline"},
54 #endif
55 #ifdef SO_SNDBUF
56         { SO_SNDBUF, "SO_SNDBUF, send buffer size" },
57 #endif
58 #ifdef SO_RCVLOWAT
59         { SO_RCVLOWAT, "SO_RCVLOWAT, receive low-water mark"},
60 #endif
61 #ifdef SO_SNDLOWAT
62         { SO_SNDLOWAT, "SO_SNDLOWAT, send low-water mark"},
63 #endif
64 #ifdef SO_RCVTIMEO
65         { SO_RCVTIMEO, "SO_RCVTIMEO, receive timeout"},
66 #endif
67 #ifdef SO_SNDTIMEO
68         { SO_SNDTIMEO, "SO_SNDTIMEO, send timeout"},
69 #endif
70 #ifdef SO_RCVBUF
71         { SO_RCVBUF, "SO_RCVBUF, receive buffer size"},
72 #endif
73 #ifdef SO_ERROR
74         { SO_ERROR, "SO_ERROR, error status"},
75 #endif
76 #ifdef SO_TYPE
77         { SO_TYPE, "SO_TYPE, socket type"},
78 #endif
79         { 0, NULL }
80 };
81
82 struct option_t tcp_option[] = {
83 #ifdef TCP_MAXSEG
84           { TCP_MAXSEG, "TCP_MAXSEG, maximum segment size (mss)"},
85 #endif
86 #ifdef TCP_NODELAY
87           { TCP_NODELAY, "TCP_NODELAY, send even tiny packets"},
88 #endif
89           { 0, NULL}
90 };
91
92 int main(int argc, char **argv)
93 {
94         int s;
95         if ((getuid()) != 0) {
96                 fprintf(stderr,"%s: you must be root to run this program.\n", argv[0]);
97                 return 1;
98         }
99
100         if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
101                 perror("failed to create a socket to test.");
102                 return 1;
103         }
104         option_query(s, SOL_SOCKET, so_option);
105         option_query(s, IPPROTO_TCP, tcp_option);
106
107         (void) close(s);
108         return 0;
109 }