Project

General

Profile

Bug #1426 ยป acceptor.c

Test program - Dan McDonald, 2011-08-30 03:12 PM

 
1
/* XXX KEBE SAYS --> link with -lnsl -lsocket. */
2

    
3
#include <sys/socket.h>
4
#include <netinet/in.h>
5
#include <netinet/tcp.h>
6
#include <err.h>
7
#include <stdio.h>
8
#include <stdlib.h>
9

    
10
void
11
print_sockopts(int s)
12
{
13
	int val;
14
	socklen_t vallen = sizeof (int);
15

    
16
	if (getsockopt(s, IPPROTO_TCP, TCP_KEEPCNT, &val, &vallen) == -1)
17
		err(-1, "getsockopt(TCP_KEEPCNT)");
18
	printf("CNT = %d ", val);
19

    
20
	vallen = sizeof (int);
21
	if (getsockopt(s, IPPROTO_TCP, TCP_KEEPIDLE, &val, &vallen) == -1)
22
		err(-1, "getsockopt(TCP_KEEPIDLE)");
23
	printf("IDLE = %d ", val);
24

    
25
	vallen = sizeof (int);
26
	if (getsockopt(s, IPPROTO_TCP, TCP_KEEPINTVL, &val, &vallen) == -1)
27
		err(-1, "getsockopt(TCP_KEEPINTVL)");
28
	printf("INTVL = %d ", val);
29

    
30
	vallen = sizeof (int);
31
	if (getsockopt(s, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, &val, &vallen)
32
	    == -1)
33
		err(-1, "getsockopt(TCP_KEEPALIVE_THRESHOLD)");
34
	printf("ALIVE_THRESHOLD = %d ", val);
35

    
36
	vallen = sizeof (int);
37
	if (getsockopt(s, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD,
38
	    &val, &vallen) == -1)
39
		err(-1, "getsockopt(TCP_KEEPALIVE_ABORT_THRESHOLD)");
40
	printf("ALIVE_ABORT_THRESHOLD = %d\n", val);
41
}
42

    
43
int
44
main(int argc, char *argv[])
45
{
46
	int s, child;
47
	struct sockaddr_in sin, peer;
48
	int val = 10;
49
	socklen_t peerlen;
50

    
51
	sin.sin_family = AF_INET;
52
	if (argc >= 2)
53
		sin.sin_port = htons(atoi(argv[1]));
54
	else
55
		sin.sin_port = htons(2112);
56
	sin.sin_addr.s_addr = 0;
57

    
58
	s = socket(AF_INET, SOCK_STREAM, 0);
59
	if (s == -1)
60
		err(-1, "socket");
61

    
62
	if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) == -1)
63
		err(-1, "bind");
64

    
65
	/* Print the defaults now. */
66
	print_sockopts(s);
67

    
68
	/* Set the warped socket options now. */
69
	if (setsockopt(s, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (int)) == -1)
70
		err(-1, "setsockopt(TCP_KEEPCNT)");
71

    
72
	/* Let's create a new one. */
73
	if (listen(s, 1) == -1)
74
		err(-1, "listen");
75

    
76
	peerlen = sizeof (peer);
77
	child = accept(s, (struct sockaddr *)&peer, &peerlen);
78
	if (child == -1)
79
		err(-1, "accept");
80

    
81
	printf("Parent options:\t");
82
	print_sockopts(s);
83
	printf("Child options:\t");
84
	print_sockopts(child);
85

    
86
	return (0);
87
}
    (1-1/1)