xref: /freebsd/tools/regression/netinet/tcpconnect/tcpconnect.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /*-
2  * Copyright (c) 2004-2005 Robert N. M. Watson
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  * 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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 
35 #include <arpa/inet.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 
46 static void
47 usage(void)
48 {
49 
50 	fprintf(stderr, "tcpconnect server port\n");
51 	fprintf(stderr, "tcpconnect client ip port count [nonblock] [tcpmd5]\n");
52 	exit(-1);
53 }
54 
55 static void
56 tcpconnect_server(int argc, char *argv[])
57 {
58 	int listen_sock, accept_sock;
59 	struct sockaddr_in sin;
60 	char *dummy;
61 	long port;
62 
63 	if (argc != 1)
64 		usage();
65 
66 	bzero(&sin, sizeof(sin));
67 	sin.sin_len = sizeof(sin);
68 	sin.sin_family = AF_INET;
69 	sin.sin_addr.s_addr = htonl(INADDR_ANY);
70 
71 	port = strtoul(argv[0], &dummy, 10);
72 	if (port < 1 || port > 65535 || *dummy != '\0')
73 		usage();
74 	sin.sin_port = htons(port);
75 
76 	listen_sock = socket(PF_INET, SOCK_STREAM, 0);
77 	if (listen_sock == -1)
78 		err(-1, "socket");
79 
80 	if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
81 		err(-1, "bind");
82 
83 	if (listen(listen_sock, -1) == -1)
84 		err(-1, "listen");
85 
86 	while (1) {
87 		accept_sock = accept(listen_sock, NULL, NULL);
88 		close(accept_sock);
89 	}
90 }
91 
92 static void
93 tcpconnect_client(int argc, char *argv[])
94 {
95 	struct sockaddr_in sin;
96 	long count, i, port;
97 	char *dummy;
98 	int sock;
99 	int nonblock = 0, md5enable = 0;
100 
101 	if (argc < 3 || argc > 5)
102 		usage();
103 	for (i=3; i < argc; i++) {
104 		if (strcmp(argv[i], "nonblock") == 0)
105 			nonblock = 1;
106 		if (strcmp(argv[i], "tcpmd5") == 0)
107 			md5enable = 1;
108 	}
109 
110 	bzero(&sin, sizeof(sin));
111 	sin.sin_len = sizeof(sin);
112 	sin.sin_family = AF_INET;
113 	if (inet_aton(argv[0], &sin.sin_addr) == 0)
114 		err(-1, "listen");
115 
116 	port = strtoul(argv[1], &dummy, 10);
117 	if (port < 1 || port > 65535 || *dummy != '\0')
118 		usage();
119 	sin.sin_port = htons(port);
120 
121 	count = strtoul(argv[2], &dummy, 10);
122 	if (count < 1 || count > 100000 || *dummy != '\0')
123 		usage();
124 
125 	for (i = 0; i < count; i++) {
126 		sock = socket(PF_INET, SOCK_STREAM, 0);
127 		if (sock == -1)
128 			err(-1, "socket");
129 
130 		/* No warning in default case on ENOPROTOOPT. */
131 		if (setsockopt(sock, IPPROTO_TCP, TCP_MD5SIG,
132 		    &md5enable, sizeof(md5enable)) != 0) {
133 			if (errno == ENOPROTOOPT && md5enable > 0)
134 				err(-1, "setsockopt(TCP_MD5SIG)");
135 			else if (errno != ENOPROTOOPT)
136 				warn("setsockopt(TCP_MD5SIG)");
137 		}
138 
139 		if (nonblock) {
140 			if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0)
141 				err(-1, "fcntl(F_SETFL)");
142 
143 			if (connect(sock, (struct sockaddr *)&sin,
144 			    sizeof(sin)) == -1 && errno != EINPROGRESS)
145 				err(-1, "connect");
146 		} else {
147 			if (connect(sock, (struct sockaddr *)&sin,
148 			    sizeof(sin)) == -1)
149 				err(-1, "connect");
150 		}
151 
152 		close(sock);
153 	}
154 }
155 
156 int
157 main(int argc, char *argv[])
158 {
159 
160 	if (argc < 2)
161 		usage();
162 
163 	if (strcmp(argv[1], "server") == 0)
164 		tcpconnect_server(argc - 2, argv + 2);
165 	else if (strcmp(argv[1], "client") == 0)
166 		tcpconnect_client(argc - 2, argv + 2);
167 	else
168 		usage();
169 
170 	exit(0);
171 }
172