1/*-
2 * Copyright (c) 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
27#include <sys/types.h>
28#include <sys/fcntl.h>
29#include <sys/ioctl.h>
30#include <sys/select.h>
31#include <sys/socket.h>
32#include <sys/stdint.h>
33
34#include <netinet/in.h>
35
36#include <arpa/inet.h>
37
38#include <err.h>
39#include <errno.h>
40#include <signal.h>
41#include <stdio.h>
42#include <string.h>
43#include <unistd.h>
44
45/*
46 * Simple micro-benchmark to see how many connections/second can be created
47 * in a serialized fashion against a given server.  A timer signal is used
48 * to interrupt the loop and assess the cost.
49 */
50#define	SECONDS	60
51#define	PORT	6060
52
53static int	timer_fired;
54
55/*
56 * Signal timer, which both interrupts the in-progress socket operation, and
57 * flags the timer as having fired so the main loop can exit.
58 */
59static void
60alarm_handler(__unused int signum)
61{
62
63	timer_fired = 1;
64}
65
66/*
67 * Build a connection.  In order to make sure the signal interrupts us as
68 * we wait, use non-blocking sockets and select().  Return 0 on success, or
69 * -1 if we were interrupted.  Exit with a failure if we get an unspected
70 * error.
71 */
72static int
73try_connect(struct sockaddr_in *sin)
74{
75	struct
76	fd_set read_set;
77	int i, s;
78
79	s = socket(PF_INET, SOCK_STREAM, 0);
80	if (s < 0)
81		err(-1, "socket(PF_INET, SOCK_STREAM)");
82
83	i = 1;
84	if (fcntl(s, F_SETFL, FIONBIO, &i) < 0)
85		err(-1, "fcntl(s, FIOBIO, 1)");
86
87	FD_ZERO(&read_set);
88	FD_SET(s, &read_set);
89
90	if (connect(s, (struct sockaddr *)sin, sizeof(*sin)) < 0 &&
91	    errno != EINPROGRESS)
92		err(-1, "connect(%s)", inet_ntoa(sin->sin_addr));
93
94	if (select(s + 1, &read_set, &read_set, &read_set, NULL) < 0) {
95		if ((errno == EINTR && !timer_fired) || (errno != EINTR))
96			err(-1, "select");
97		return (-1);
98	}
99
100	close(s);
101	return (0);
102}
103
104
105int
106main(int argc, char *argv[])
107{
108	struct sockaddr_in sin;
109	u_int64_t counter;
110
111	if (argc != 2)
112		errx(-1, "usage: tcpconnect [ip]");
113
114	bzero(&sin, sizeof(sin));
115	sin.sin_family = AF_INET;
116	sin.sin_len = sizeof(sin);
117	sin.sin_addr.s_addr = inet_addr(argv[1]);
118	sin.sin_port = htons(PORT);
119
120	if (signal(SIGALRM, alarm_handler) == SIG_ERR)
121		err(-1, "signal(SIGALRM)");
122
123	alarm(SECONDS);
124
125	counter = 0;
126	while (!timer_fired) {
127		if (try_connect(&sin) == 0)
128			counter++;
129	}
130	printf("%ju count\n", (uintmax_t)counter);
131	printf("%ju connections/second\n", (uintmax_t)(counter / SECONDS));
132
133	return (0);
134}
135