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>
50 fprintf(stderr, "tcpconnect server port\n");
51 fprintf(stderr, "tcpconnect client ip port count [nonblock] [tcpmd5]\n");
56 tcpconnect_server(int argc, char *argv[])
58 int listen_sock, accept_sock;
59 struct sockaddr_in sin;
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);
71 port = strtoul(argv[0], &dummy, 10);
72 if (port < 1 || port > 65535 || *dummy != '\0')
74 sin.sin_port = htons(port);
76 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
77 if (listen_sock == -1)
80 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
83 if (listen(listen_sock, -1) == -1)
87 accept_sock = accept(listen_sock, NULL, NULL);
93 tcpconnect_client(int argc, char *argv[])
95 struct sockaddr_in sin;
99 int nonblock = 0, md5enable = 0;
101 if (argc < 3 || argc > 5)
103 for (i=3; i < argc; i++) {
104 if (strcmp(argv[i], "nonblock") == 0)
106 if (strcmp(argv[i], "tcpmd5") == 0)
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)
116 port = strtoul(argv[1], &dummy, 10);
117 if (port < 1 || port > 65535 || *dummy != '\0')
119 sin.sin_port = htons(port);
121 count = strtoul(argv[2], &dummy, 10);
122 if (count < 1 || count > 100000 || *dummy != '\0')
125 for (i = 0; i < count; i++) {
126 sock = socket(PF_INET, SOCK_STREAM, 0);
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)");
140 if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0)
141 err(-1, "fcntl(F_SETFL)");
143 if (connect(sock, (struct sockaddr *)&sin,
144 sizeof(sin)) == -1 && errno != EINPROGRESS)
147 if (connect(sock, (struct sockaddr *)&sin,
157 main(int argc, char *argv[])
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);