2 * Copyright (c) 2004-2005 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
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
35 #include <arpa/inet.h>
49 fprintf(stderr, "tcpconnect server port\n");
50 fprintf(stderr, "tcpconnect client ip port count [nonblock] [tcpmd5]\n");
55 tcpconnect_server(int argc, char *argv[])
57 int listen_sock, accept_sock;
58 struct sockaddr_in sin;
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);
70 port = strtoul(argv[0], &dummy, 10);
71 if (port < 1 || port > 65535 || *dummy != '\0')
73 sin.sin_port = htons(port);
75 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
76 if (listen_sock == -1)
79 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
82 if (listen(listen_sock, -1) == -1)
86 accept_sock = accept(listen_sock, NULL, NULL);
92 tcpconnect_client(int argc, char *argv[])
94 struct sockaddr_in sin;
98 int nonblock = 0, md5enable = 0;
100 if (argc < 3 || argc > 5)
102 for (i=3; i < argc; i++) {
103 if (strcmp(argv[i], "nonblock") == 0)
105 if (strcmp(argv[i], "tcpmd5") == 0)
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)
115 port = strtoul(argv[1], &dummy, 10);
116 if (port < 1 || port > 65535 || *dummy != '\0')
118 sin.sin_port = htons(port);
120 count = strtoul(argv[2], &dummy, 10);
121 if (count < 1 || count > 100000 || *dummy != '\0')
124 for (i = 0; i < count; i++) {
125 sock = socket(PF_INET, SOCK_STREAM, 0);
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)");
139 if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0)
140 err(-1, "fcntl(F_SETFL)");
142 if (connect(sock, (struct sockaddr *)&sin,
143 sizeof(sin)) == -1 && errno != EINPROGRESS)
146 if (connect(sock, (struct sockaddr *)&sin,
156 main(int argc, char *argv[])
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);