2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * tcpstream sets up a simple TCP client and server, and then streams a
31 * predictable pseudo-random byte sequence through it using variable block
32 * sizes. The intent is to detect corruption of data in the TCP stream.
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
41 #include <arpa/inet.h>
50 #define MAX_LOOPS 10240
51 #define MAX_LONGS 1024
57 fprintf(stderr, "tcpstream client [ip] [port] [seed]\n");
58 fprintf(stderr, "tcpstream server [port] [seed]\n");
63 fill_buffer(long *buffer, int len)
67 for (i = 0; i < len; i++)
68 buffer[i] = htonl(random());
72 check_buffer(long *buffer, int len)
76 for (i = 0; i < len; i++) {
77 if (buffer[i] != htonl(random()))
84 tcpstream_client(struct sockaddr_in sin, long seed)
86 long buffer[MAX_LONGS];
92 sock = socket(PF_INET, SOCK_STREAM, 0);
94 errx(-1, "socket: %s", strerror(errno));
96 if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1)
97 errx(-1, "connect: %s", strerror(errno));
99 for (j = 0; j < MAX_LOOPS; j++) {
100 for (i = 0; i < MAX_LONGS; i++) {
101 fill_buffer(buffer, i);
102 len = send(sock, buffer, i * sizeof(long), 0);
104 printf("%d bytes written of %d expected\n",
105 len, i * sizeof(long));
118 tcpstream_server(struct sockaddr_in sin, long seed)
120 int i, j, listen_sock, accept_sock;
121 struct sockaddr_in other_sin;
122 long buffer[MAX_LONGS];
126 int input_byte_counter;
128 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
129 if (listen_sock == -1)
130 errx(-1, "socket: %s", strerror(errno));
132 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
133 errx(-1, "bind: %s", strerror(errno));
135 if (listen(listen_sock, -1) == -1)
136 errx(-1, "listen: %s", strerror(errno));
139 bzero(&other_sin, sizeof(other_sin));
140 addrlen = sizeof(other_sin);
142 accept_sock = accept(listen_sock, (struct sockaddr *)
143 &other_sin, &addrlen);
144 if (accept_sock == -1) {
148 printf("connection opened from %s:%d\n",
149 inet_ntoa(other_sin.sin_addr), ntohs(other_sin.sin_port));
150 input_byte_counter = 0;
154 for (j = 0; j < MAX_LOOPS; j++) {
155 for (i = 0; i < MAX_LONGS; i++) {
156 len = recv(accept_sock, buffer,
157 i * sizeof(long), MSG_WAITALL);
158 if (len != i * sizeof(long)) {
162 if (check_buffer(buffer, i) == 0) {
164 "Corruption in block beginning %d and ending %d\n", input_byte_counter,
165 input_byte_counter + len);
167 "Block size %d\n", i * sizeof(long));
170 input_byte_counter += len;
174 printf("connection closed\n");
180 main(int argc, char *argv[])
182 struct sockaddr_in sin;
188 if (strcmp(argv[1], "client") == 0) {
192 bzero(&sin, sizeof(sin));
193 sin.sin_len = sizeof(sin);
194 sin.sin_family = AF_INET;
196 if (inet_aton(argv[2], &sin.sin_addr) != 1)
197 errx(-1, "%s: %s", argv[2], strerror(EINVAL));
199 port = strtoul(argv[3], &dummy, 10);
200 if (port < 1 || port > 65535 || *dummy != '\0')
202 sin.sin_port = htons(port);
204 seed = strtoul(argv[4], &dummy, 10);
208 tcpstream_client(sin, seed);
210 } else if (strcmp(argv[1], "server") == 0) {
214 bzero(&sin, sizeof(sin));
215 sin.sin_len = sizeof(sin);
216 sin.sin_family = AF_INET;
217 sin.sin_addr.s_addr = INADDR_ANY;
219 port = strtoul(argv[2], &dummy, 10);
220 if (port < 1 || port > 65535 || *dummy != '\0')
222 sin.sin_port = htons(port);
224 seed = strtoul(argv[3], &dummy, 10);
228 tcpstream_server(sin, seed);