xref: /freebsd/tests/sys/netinet/tcp_user_cookie.c (revision 350b7c3570aa6c87c537e54f706f1866f93a4142)
1 /*
2  *  Copyright (c) 2016 Limelight Networks
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *  1. Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions, and the following disclaimer,
10  *     without modification.
11  *  2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *     substantially similar to the "NO WARRANTY" disclaimer below
13  *     ("Disclaimer") and any redistribution must be conditioned upon
14  *     including a substantially similar Disclaimer requirement for further
15  *     binary redistribution.
16  *
17  *  NO WARRANTY
18  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  *  HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  *  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  *  POSSIBILITY OF SUCH DAMAGES.
29  *
30  *  Authors: George Neville-Neil
31  *
32  * $FreeBSD$
33  */
34 
35 #include <arpa/inet.h>
36 #include <netinet/in.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 
40 #include <err.h>
41 #include <sysexits.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #define buflen 80
50 
51 /*
52  * Setup a TCP server listening on a port for connections, all of
53  * which subseuqently have their user cookie set.
54  */
55 int
56 main(int argc, char **argv)
57 {
58 	struct sockaddr_in srv;
59 	int sock, accepted, port, cookie;
60 	int ret;
61 	char recvbuf[buflen];
62 
63 	if (argc != 3) {
64 		fprintf(stderr, "Usage: %s port cookie\n", argv[0]);
65 		exit(2);
66 	}
67 
68 	port = atoi(argv[1]);
69 	cookie = atoi(argv[2]);
70 
71 	sock = socket(PF_INET, SOCK_STREAM, 0);
72 	if (sock < 0)
73 		err(EXIT_FAILURE, "socket");
74 
75 	memset(&srv, 0, sizeof(srv));
76 	srv.sin_len = sizeof(srv);
77 	srv.sin_family = AF_INET;
78 	srv.sin_port = htons(port);
79 	srv.sin_addr.s_addr = INADDR_ANY;
80 
81 	if (bind(sock, (struct sockaddr *)&srv, srv.sin_len) < 0)
82 		err(EX_OSERR, "failed to bind to port %d", port);
83 
84 	if (listen(sock, 5) < 0)
85 		err(EX_OSERR, "failed to listen on socket");
86 
87 	ret = setsockopt(sock, SOL_SOCKET, SO_USER_COOKIE, &cookie, sizeof(cookie));
88 	if (ret < 0)
89 		err(EX_OSERR, "setsockopt(SO_USER_COOKIE)");
90 
91 	while (1) {
92 
93 		accepted = accept(sock, NULL, 0);
94 
95 		if (accepted < 0)
96 			err(EX_OSERR, "accept failed");
97 
98 		ret = setsockopt(accepted, SOL_SOCKET, SO_USER_COOKIE,
99 				 &cookie, sizeof(cookie));
100 		if (ret < 0)
101 			err(EX_OSERR, "setsockopt(SO_USER_COOKIE)");
102 
103 		ret = read(accepted, &recvbuf, buflen);
104 
105 		if (ret < 0)
106 			warn("failed read");
107 
108 		close(accepted);
109 	}
110 
111 	return (0);
112 }
113