1cb5e18f3SRobert Watson /*-
2cb5e18f3SRobert Watson * Copyright (c) 2004 Robert N. M. Watson
3cb5e18f3SRobert Watson * All rights reserved.
4cb5e18f3SRobert Watson *
5cb5e18f3SRobert Watson * Redistribution and use in source and binary forms, with or without
6cb5e18f3SRobert Watson * modification, are permitted provided that the following conditions
7cb5e18f3SRobert Watson * are met:
8cb5e18f3SRobert Watson * 1. Redistributions of source code must retain the above copyright
9cb5e18f3SRobert Watson * notice, this list of conditions and the following disclaimer.
10cb5e18f3SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
11cb5e18f3SRobert Watson * notice, this list of conditions and the following disclaimer in the
12cb5e18f3SRobert Watson * documentation and/or other materials provided with the distribution.
13cb5e18f3SRobert Watson *
14cb5e18f3SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15cb5e18f3SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16cb5e18f3SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17cb5e18f3SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18cb5e18f3SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19cb5e18f3SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20cb5e18f3SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21cb5e18f3SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22cb5e18f3SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23cb5e18f3SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24cb5e18f3SRobert Watson * SUCH DAMAGE.
25cb5e18f3SRobert Watson */
26cb5e18f3SRobert Watson
27cb5e18f3SRobert Watson /*
28cb5e18f3SRobert Watson * tcpstream sets up a simple TCP client and server, and then streams a
29cb5e18f3SRobert Watson * predictable pseudo-random byte sequence through it using variable block
30*ba594e73SGordon Bergling * sizes. The intent is to detect corruption of data in the TCP stream.
31cb5e18f3SRobert Watson */
32cb5e18f3SRobert Watson
33cb5e18f3SRobert Watson #include <sys/types.h>
34cb5e18f3SRobert Watson #include <sys/errno.h>
35cb5e18f3SRobert Watson #include <sys/socket.h>
36cb5e18f3SRobert Watson
37cb5e18f3SRobert Watson #include <netinet/in.h>
38cb5e18f3SRobert Watson
39cb5e18f3SRobert Watson #include <arpa/inet.h>
40cb5e18f3SRobert Watson
41a09c60ffSRobert Watson #include <err.h>
42a09c60ffSRobert Watson #include <errno.h>
43cb5e18f3SRobert Watson #include <stdio.h>
44cb5e18f3SRobert Watson #include <stdlib.h>
45cb5e18f3SRobert Watson #include <string.h>
46cb5e18f3SRobert Watson #include <unistd.h>
47cb5e18f3SRobert Watson
48cb5e18f3SRobert Watson #define MAX_LOOPS 10240
49cb5e18f3SRobert Watson #define MAX_LONGS 1024
50cb5e18f3SRobert Watson
51cb5e18f3SRobert Watson static void
usage(void)52cb5e18f3SRobert Watson usage(void)
53cb5e18f3SRobert Watson {
54cb5e18f3SRobert Watson
55cb5e18f3SRobert Watson fprintf(stderr, "tcpstream client [ip] [port] [seed]\n");
56cb5e18f3SRobert Watson fprintf(stderr, "tcpstream server [port] [seed]\n");
57cb5e18f3SRobert Watson exit(-1);
58cb5e18f3SRobert Watson }
59cb5e18f3SRobert Watson
60cb5e18f3SRobert Watson static void
fill_buffer(long * buffer,int len)61cb5e18f3SRobert Watson fill_buffer(long *buffer, int len)
62cb5e18f3SRobert Watson {
63cb5e18f3SRobert Watson int i;
64cb5e18f3SRobert Watson
65cb5e18f3SRobert Watson for (i = 0; i < len; i++)
66cb5e18f3SRobert Watson buffer[i] = htonl(random());
67cb5e18f3SRobert Watson }
68cb5e18f3SRobert Watson
69cb5e18f3SRobert Watson static int
check_buffer(long * buffer,int len)70cb5e18f3SRobert Watson check_buffer(long *buffer, int len)
71cb5e18f3SRobert Watson {
72cb5e18f3SRobert Watson int i;
73cb5e18f3SRobert Watson
74cb5e18f3SRobert Watson for (i = 0; i < len; i++) {
75cb5e18f3SRobert Watson if (buffer[i] != htonl(random()))
76cb5e18f3SRobert Watson return (0);
77cb5e18f3SRobert Watson }
78cb5e18f3SRobert Watson return (1);
79cb5e18f3SRobert Watson }
80cb5e18f3SRobert Watson
81cb5e18f3SRobert Watson static void
tcpstream_client(struct sockaddr_in sin,long seed)82cb5e18f3SRobert Watson tcpstream_client(struct sockaddr_in sin, long seed)
83cb5e18f3SRobert Watson {
84cb5e18f3SRobert Watson long buffer[MAX_LONGS];
85cb5e18f3SRobert Watson ssize_t len;
86cb5e18f3SRobert Watson int i, j, sock;
87cb5e18f3SRobert Watson
88cb5e18f3SRobert Watson srandom(seed);
89cb5e18f3SRobert Watson
90cb5e18f3SRobert Watson sock = socket(PF_INET, SOCK_STREAM, 0);
91a09c60ffSRobert Watson if (sock == -1)
92a09c60ffSRobert Watson errx(-1, "socket: %s", strerror(errno));
93cb5e18f3SRobert Watson
94a09c60ffSRobert Watson if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1)
95a09c60ffSRobert Watson errx(-1, "connect: %s", strerror(errno));
96cb5e18f3SRobert Watson
97cb5e18f3SRobert Watson for (j = 0; j < MAX_LOOPS; j++) {
98cb5e18f3SRobert Watson for (i = 0; i < MAX_LONGS; i++) {
99cb5e18f3SRobert Watson fill_buffer(buffer, i);
100cb5e18f3SRobert Watson len = send(sock, buffer, i * sizeof(long), 0);
101cb5e18f3SRobert Watson if (len == -1) {
102cb5e18f3SRobert Watson printf("%d bytes written of %d expected\n",
103cb5e18f3SRobert Watson len, i * sizeof(long));
104cb5e18f3SRobert Watson fflush(stdout);
105cb5e18f3SRobert Watson perror("send");
106cb5e18f3SRobert Watson goto done;
107cb5e18f3SRobert Watson }
108cb5e18f3SRobert Watson }
109cb5e18f3SRobert Watson }
110cb5e18f3SRobert Watson
111cb5e18f3SRobert Watson done:
112cb5e18f3SRobert Watson close(sock);
113cb5e18f3SRobert Watson }
114cb5e18f3SRobert Watson
115cb5e18f3SRobert Watson static void
tcpstream_server(struct sockaddr_in sin,long seed)116cb5e18f3SRobert Watson tcpstream_server(struct sockaddr_in sin, long seed)
117cb5e18f3SRobert Watson {
118cb5e18f3SRobert Watson int i, j, listen_sock, accept_sock;
119cb5e18f3SRobert Watson struct sockaddr_in other_sin;
120cb5e18f3SRobert Watson long buffer[MAX_LONGS];
121cb5e18f3SRobert Watson socklen_t addrlen;
122cb5e18f3SRobert Watson ssize_t len;
123cb5e18f3SRobert Watson
124cb5e18f3SRobert Watson int input_byte_counter;
125cb5e18f3SRobert Watson
126cb5e18f3SRobert Watson listen_sock = socket(PF_INET, SOCK_STREAM, 0);
127a09c60ffSRobert Watson if (listen_sock == -1)
128a09c60ffSRobert Watson errx(-1, "socket: %s", strerror(errno));
129cb5e18f3SRobert Watson
130a09c60ffSRobert Watson if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
131a09c60ffSRobert Watson errx(-1, "bind: %s", strerror(errno));
132cb5e18f3SRobert Watson
133a09c60ffSRobert Watson if (listen(listen_sock, -1) == -1)
134a09c60ffSRobert Watson errx(-1, "listen: %s", strerror(errno));
135cb5e18f3SRobert Watson
136cb5e18f3SRobert Watson while (1) {
137cb5e18f3SRobert Watson bzero(&other_sin, sizeof(other_sin));
138cb5e18f3SRobert Watson addrlen = sizeof(other_sin);
139cb5e18f3SRobert Watson
140cb5e18f3SRobert Watson accept_sock = accept(listen_sock, (struct sockaddr *)
141cb5e18f3SRobert Watson &other_sin, &addrlen);
142cb5e18f3SRobert Watson if (accept_sock == -1) {
143cb5e18f3SRobert Watson perror("accept");
144cb5e18f3SRobert Watson continue;
145cb5e18f3SRobert Watson }
146cb5e18f3SRobert Watson printf("connection opened from %s:%d\n",
147cb5e18f3SRobert Watson inet_ntoa(other_sin.sin_addr), ntohs(other_sin.sin_port));
148cb5e18f3SRobert Watson input_byte_counter = 0;
149cb5e18f3SRobert Watson
150cb5e18f3SRobert Watson srandom(seed);
151cb5e18f3SRobert Watson
152cb5e18f3SRobert Watson for (j = 0; j < MAX_LOOPS; j++) {
153cb5e18f3SRobert Watson for (i = 0; i < MAX_LONGS; i++) {
154cb5e18f3SRobert Watson len = recv(accept_sock, buffer,
155cb5e18f3SRobert Watson i * sizeof(long), MSG_WAITALL);
156cb5e18f3SRobert Watson if (len != i * sizeof(long)) {
157cb5e18f3SRobert Watson perror("recv");
158cb5e18f3SRobert Watson goto done;
159cb5e18f3SRobert Watson }
160cb5e18f3SRobert Watson if (check_buffer(buffer, i) == 0) {
161cb5e18f3SRobert Watson fprintf(stderr,
162cb5e18f3SRobert Watson "Corruption in block beginning %d and ending %d\n", input_byte_counter,
163cb5e18f3SRobert Watson input_byte_counter + len);
164cb5e18f3SRobert Watson fprintf(stderr,
165cb5e18f3SRobert Watson "Block size %d\n", i * sizeof(long));
166cb5e18f3SRobert Watson goto done;
167cb5e18f3SRobert Watson }
168cb5e18f3SRobert Watson input_byte_counter += len;
169cb5e18f3SRobert Watson }
170cb5e18f3SRobert Watson }
171cb5e18f3SRobert Watson done:
172cb5e18f3SRobert Watson printf("connection closed\n");
173cb5e18f3SRobert Watson close(accept_sock);
174cb5e18f3SRobert Watson }
175cb5e18f3SRobert Watson }
176cb5e18f3SRobert Watson
177cb5e18f3SRobert Watson int
main(int argc,char * argv[])178cb5e18f3SRobert Watson main(int argc, char *argv[])
179cb5e18f3SRobert Watson {
180cb5e18f3SRobert Watson struct sockaddr_in sin;
181cb5e18f3SRobert Watson long port, seed;
182cb5e18f3SRobert Watson char *dummy;
183cb5e18f3SRobert Watson
184cb5e18f3SRobert Watson if (argc < 2)
185cb5e18f3SRobert Watson usage();
186cb5e18f3SRobert Watson if (strcmp(argv[1], "client") == 0) {
187cb5e18f3SRobert Watson if (argc != 5)
188cb5e18f3SRobert Watson usage();
189cb5e18f3SRobert Watson
190cb5e18f3SRobert Watson bzero(&sin, sizeof(sin));
191cb5e18f3SRobert Watson sin.sin_len = sizeof(sin);
192cb5e18f3SRobert Watson sin.sin_family = AF_INET;
193cb5e18f3SRobert Watson
194a09c60ffSRobert Watson if (inet_aton(argv[2], &sin.sin_addr) != 1)
195a09c60ffSRobert Watson errx(-1, "%s: %s", argv[2], strerror(EINVAL));
196cb5e18f3SRobert Watson
197cb5e18f3SRobert Watson port = strtoul(argv[3], &dummy, 10);
198cb5e18f3SRobert Watson if (port < 1 || port > 65535 || *dummy != '\0')
199cb5e18f3SRobert Watson usage();
200cb5e18f3SRobert Watson sin.sin_port = htons(port);
201cb5e18f3SRobert Watson
202cb5e18f3SRobert Watson seed = strtoul(argv[4], &dummy, 10);
203cb5e18f3SRobert Watson if (*dummy != '\0')
204cb5e18f3SRobert Watson usage();
205cb5e18f3SRobert Watson
206cb5e18f3SRobert Watson tcpstream_client(sin, seed);
207cb5e18f3SRobert Watson
208cb5e18f3SRobert Watson } else if (strcmp(argv[1], "server") == 0) {
209cb5e18f3SRobert Watson if (argc != 4)
210cb5e18f3SRobert Watson usage();
211cb5e18f3SRobert Watson
212cb5e18f3SRobert Watson bzero(&sin, sizeof(sin));
213cb5e18f3SRobert Watson sin.sin_len = sizeof(sin);
214cb5e18f3SRobert Watson sin.sin_family = AF_INET;
215cb5e18f3SRobert Watson sin.sin_addr.s_addr = INADDR_ANY;
216cb5e18f3SRobert Watson
217cb5e18f3SRobert Watson port = strtoul(argv[2], &dummy, 10);
218cb5e18f3SRobert Watson if (port < 1 || port > 65535 || *dummy != '\0')
219cb5e18f3SRobert Watson usage();
220cb5e18f3SRobert Watson sin.sin_port = htons(port);
221cb5e18f3SRobert Watson
222cb5e18f3SRobert Watson seed = strtoul(argv[3], &dummy, 10);
223cb5e18f3SRobert Watson if (*dummy != '\0')
224cb5e18f3SRobert Watson usage();
225cb5e18f3SRobert Watson
226cb5e18f3SRobert Watson tcpstream_server(sin, seed);
227cb5e18f3SRobert Watson } else
228cb5e18f3SRobert Watson usage();
229cb5e18f3SRobert Watson
230cb5e18f3SRobert Watson return (0);
231cb5e18f3SRobert Watson }
232