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