xref: /freebsd/crypto/openssl/apps/s_time.c (revision b077aed3)
1e71b7053SJung-uk Kim /*
2b6c1fdcdSJung-uk Kim  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
874664626SKris Kennaway  */
974664626SKris Kennaway 
1074664626SKris Kennaway #include <stdio.h>
1174664626SKris Kennaway #include <stdlib.h>
1274664626SKris Kennaway #include <string.h>
1374664626SKris Kennaway 
14e71b7053SJung-uk Kim #include <openssl/opensslconf.h>
15e71b7053SJung-uk Kim 
16e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SOCK
17e71b7053SJung-uk Kim 
185c87c606SMark Murray #include "apps.h"
19e71b7053SJung-uk Kim #include "progs.h"
2074664626SKris Kennaway #include <openssl/x509.h>
2174664626SKris Kennaway #include <openssl/ssl.h>
2274664626SKris Kennaway #include <openssl/pem.h>
2374664626SKris Kennaway #include "s_apps.h"
2474664626SKris Kennaway #include <openssl/err.h>
25b077aed3SPierre Pronchery #include "internal/sockets.h"
265c87c606SMark Murray #if !defined(OPENSSL_SYS_MSDOS)
27b077aed3SPierre Pronchery # include <unistd.h>
285c87c606SMark Murray #endif
2974664626SKris Kennaway 
3074664626SKris Kennaway #define SSL_CONNECT_NAME        "localhost:4433"
3174664626SKris Kennaway 
3274664626SKris Kennaway #define SECONDS 30
33e71b7053SJung-uk Kim #define SECONDSSTR "30"
3474664626SKris Kennaway 
35e71b7053SJung-uk Kim static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx);
3674664626SKris Kennaway 
37e71b7053SJung-uk Kim /*
38e71b7053SJung-uk Kim  * Define a HTTP get command globally.
39e71b7053SJung-uk Kim  * Also define the size of the command, this is two bytes less than
40e71b7053SJung-uk Kim  * the size of the string because the %s is replaced by the URL.
4174664626SKris Kennaway  */
42e71b7053SJung-uk Kim static const char fmt_http_get_cmd[] = "GET %s HTTP/1.0\r\n\r\n";
43e71b7053SJung-uk Kim static const size_t fmt_http_get_cmd_size = sizeof(fmt_http_get_cmd) - 2;
4474664626SKris Kennaway 
45e71b7053SJung-uk Kim typedef enum OPTION_choice {
46b077aed3SPierre Pronchery     OPT_COMMON,
47e71b7053SJung-uk Kim     OPT_CONNECT, OPT_CIPHER, OPT_CIPHERSUITES, OPT_CERT, OPT_NAMEOPT, OPT_KEY,
48b077aed3SPierre Pronchery     OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
49b077aed3SPierre Pronchery     OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
50b077aed3SPierre Pronchery     OPT_NEW, OPT_REUSE, OPT_BUGS, OPT_VERIFY, OPT_TIME, OPT_SSL3,
51b077aed3SPierre Pronchery     OPT_WWW, OPT_TLS1, OPT_TLS1_1, OPT_TLS1_2, OPT_TLS1_3,
52b077aed3SPierre Pronchery     OPT_PROV_ENUM
53e71b7053SJung-uk Kim } OPTION_CHOICE;
5474664626SKris Kennaway 
55e71b7053SJung-uk Kim const OPTIONS s_time_options[] = {
56b077aed3SPierre Pronchery     OPT_SECTION("General"),
57e71b7053SJung-uk Kim     {"help", OPT_HELP, '-', "Display this summary"},
58b077aed3SPierre Pronchery 
59b077aed3SPierre Pronchery     OPT_SECTION("Connection"),
60e71b7053SJung-uk Kim     {"connect", OPT_CONNECT, 's',
61e71b7053SJung-uk Kim      "Where to connect as post:port (default is " SSL_CONNECT_NAME ")"},
62e71b7053SJung-uk Kim     {"new", OPT_NEW, '-', "Just time new connections"},
63e71b7053SJung-uk Kim     {"reuse", OPT_REUSE, '-', "Just time connection reuse"},
64e71b7053SJung-uk Kim     {"bugs", OPT_BUGS, '-', "Turn on SSL bug compatibility"},
65b077aed3SPierre Pronchery     {"cipher", OPT_CIPHER, 's', "TLSv1.2 and below cipher list to be used"},
66b077aed3SPierre Pronchery     {"ciphersuites", OPT_CIPHERSUITES, 's',
67b077aed3SPierre Pronchery      "Specify TLSv1.3 ciphersuites to be used"},
68b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SSL3
69b077aed3SPierre Pronchery     {"ssl3", OPT_SSL3, '-', "Just use SSLv3"},
70b077aed3SPierre Pronchery #endif
71b077aed3SPierre Pronchery #ifndef OPENSSL_NO_TLS1
72b077aed3SPierre Pronchery     {"tls1", OPT_TLS1, '-', "Just use TLSv1.0"},
73b077aed3SPierre Pronchery #endif
74b077aed3SPierre Pronchery #ifndef OPENSSL_NO_TLS1_1
75b077aed3SPierre Pronchery     {"tls1_1", OPT_TLS1_1, '-', "Just use TLSv1.1"},
76b077aed3SPierre Pronchery #endif
77b077aed3SPierre Pronchery #ifndef OPENSSL_NO_TLS1_2
78b077aed3SPierre Pronchery     {"tls1_2", OPT_TLS1_2, '-', "Just use TLSv1.2"},
79b077aed3SPierre Pronchery #endif
80b077aed3SPierre Pronchery #ifndef OPENSSL_NO_TLS1_3
81b077aed3SPierre Pronchery     {"tls1_3", OPT_TLS1_3, '-', "Just use TLSv1.3"},
82b077aed3SPierre Pronchery #endif
83e71b7053SJung-uk Kim     {"verify", OPT_VERIFY, 'p',
84e71b7053SJung-uk Kim      "Turn on peer certificate verification, set depth"},
85e71b7053SJung-uk Kim     {"time", OPT_TIME, 'p', "Seconds to collect data, default " SECONDSSTR},
86e71b7053SJung-uk Kim     {"www", OPT_WWW, 's', "Fetch specified page from the site"},
87b077aed3SPierre Pronchery 
88b077aed3SPierre Pronchery     OPT_SECTION("Certificate"),
89b077aed3SPierre Pronchery     {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
90b077aed3SPierre Pronchery     {"cert", OPT_CERT, '<', "Cert file to use, PEM format assumed"},
91b077aed3SPierre Pronchery     {"key", OPT_KEY, '<', "File with key, PEM; default is -cert file"},
92b077aed3SPierre Pronchery     {"cafile", OPT_CAFILE, '<', "PEM format file of CA's"},
93b077aed3SPierre Pronchery     {"CAfile", OPT_CAFILE, '<', "PEM format file of CA's"},
94b077aed3SPierre Pronchery     {"CApath", OPT_CAPATH, '/', "PEM format directory of CA's"},
95b077aed3SPierre Pronchery     {"CAstore", OPT_CASTORE, ':', "URI to store of CA's"},
96b077aed3SPierre Pronchery     {"no-CAfile", OPT_NOCAFILE, '-',
97b077aed3SPierre Pronchery      "Do not load the default certificates file"},
98b077aed3SPierre Pronchery     {"no-CApath", OPT_NOCAPATH, '-',
99b077aed3SPierre Pronchery      "Do not load certificates from the default certificates directory"},
100b077aed3SPierre Pronchery     {"no-CAstore", OPT_NOCASTORE, '-',
101b077aed3SPierre Pronchery      "Do not load certificates from the default certificates store URI"},
102b077aed3SPierre Pronchery 
103b077aed3SPierre Pronchery     OPT_PROV_OPTIONS,
104e71b7053SJung-uk Kim     {NULL}
105e71b7053SJung-uk Kim };
10674664626SKris Kennaway 
10774664626SKris Kennaway #define START   0
10874664626SKris Kennaway #define STOP    1
10974664626SKris Kennaway 
tm_Time_F(int s)11074664626SKris Kennaway static double tm_Time_F(int s)
11174664626SKris Kennaway {
1121f13597dSJung-uk Kim     return app_tminterval(s, 1);
11374664626SKris Kennaway }
11474664626SKris Kennaway 
s_time_main(int argc,char ** argv)115e71b7053SJung-uk Kim int s_time_main(int argc, char **argv)
11674664626SKris Kennaway {
117e71b7053SJung-uk Kim     char buf[1024 * 8];
11874664626SKris Kennaway     SSL *scon = NULL;
119e71b7053SJung-uk Kim     SSL_CTX *ctx = NULL;
120e71b7053SJung-uk Kim     const SSL_METHOD *meth = NULL;
121b077aed3SPierre Pronchery     char *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
122b077aed3SPierre Pronchery     char *cipher = NULL, *ciphersuites = NULL;
123e71b7053SJung-uk Kim     char *www_path = NULL;
124e71b7053SJung-uk Kim     char *host = SSL_CONNECT_NAME, *certfile = NULL, *keyfile = NULL, *prog;
125e71b7053SJung-uk Kim     double totalTime = 0.0;
126b077aed3SPierre Pronchery     int noCApath = 0, noCAfile = 0, noCAstore = 0;
127e71b7053SJung-uk Kim     int maxtime = SECONDS, nConn = 0, perform = 3, ret = 1, i, st_bugs = 0;
128e71b7053SJung-uk Kim     long bytes_read = 0, finishtime = 0;
129e71b7053SJung-uk Kim     OPTION_CHOICE o;
130b077aed3SPierre Pronchery     int min_version = 0, max_version = 0, ver, buf_len, fd;
131e71b7053SJung-uk Kim     size_t buf_size;
13274664626SKris Kennaway 
133e71b7053SJung-uk Kim     meth = TLS_client_method();
134f579bf8eSKris Kennaway 
135e71b7053SJung-uk Kim     prog = opt_init(argc, argv, s_time_options);
136e71b7053SJung-uk Kim     while ((o = opt_next()) != OPT_EOF) {
137e71b7053SJung-uk Kim         switch (o) {
138e71b7053SJung-uk Kim         case OPT_EOF:
139e71b7053SJung-uk Kim         case OPT_ERR:
140e71b7053SJung-uk Kim  opthelp:
141e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
142e71b7053SJung-uk Kim             goto end;
143e71b7053SJung-uk Kim         case OPT_HELP:
144e71b7053SJung-uk Kim             opt_help(s_time_options);
145e71b7053SJung-uk Kim             ret = 0;
146e71b7053SJung-uk Kim             goto end;
147e71b7053SJung-uk Kim         case OPT_CONNECT:
148e71b7053SJung-uk Kim             host = opt_arg();
149e71b7053SJung-uk Kim             break;
150e71b7053SJung-uk Kim         case OPT_REUSE:
151e71b7053SJung-uk Kim             perform = 2;
152e71b7053SJung-uk Kim             break;
153e71b7053SJung-uk Kim         case OPT_NEW:
154e71b7053SJung-uk Kim             perform = 1;
155e71b7053SJung-uk Kim             break;
156e71b7053SJung-uk Kim         case OPT_VERIFY:
157b077aed3SPierre Pronchery             verify_args.depth = opt_int_arg();
158e71b7053SJung-uk Kim             BIO_printf(bio_err, "%s: verify depth is %d\n",
159e71b7053SJung-uk Kim                        prog, verify_args.depth);
160e71b7053SJung-uk Kim             break;
161e71b7053SJung-uk Kim         case OPT_CERT:
162e71b7053SJung-uk Kim             certfile = opt_arg();
163e71b7053SJung-uk Kim             break;
164e71b7053SJung-uk Kim         case OPT_NAMEOPT:
165e71b7053SJung-uk Kim             if (!set_nameopt(opt_arg()))
166e71b7053SJung-uk Kim                 goto end;
167e71b7053SJung-uk Kim             break;
168e71b7053SJung-uk Kim         case OPT_KEY:
169e71b7053SJung-uk Kim             keyfile = opt_arg();
170e71b7053SJung-uk Kim             break;
171e71b7053SJung-uk Kim         case OPT_CAPATH:
172e71b7053SJung-uk Kim             CApath = opt_arg();
173e71b7053SJung-uk Kim             break;
174e71b7053SJung-uk Kim         case OPT_CAFILE:
175e71b7053SJung-uk Kim             CAfile = opt_arg();
176e71b7053SJung-uk Kim             break;
177e71b7053SJung-uk Kim         case OPT_NOCAPATH:
178e71b7053SJung-uk Kim             noCApath = 1;
179e71b7053SJung-uk Kim             break;
180e71b7053SJung-uk Kim         case OPT_NOCAFILE:
181e71b7053SJung-uk Kim             noCAfile = 1;
182e71b7053SJung-uk Kim             break;
183b077aed3SPierre Pronchery         case OPT_CASTORE:
184b077aed3SPierre Pronchery             CAstore = opt_arg();
185b077aed3SPierre Pronchery             break;
186b077aed3SPierre Pronchery         case OPT_NOCASTORE:
187b077aed3SPierre Pronchery             noCAstore = 1;
188b077aed3SPierre Pronchery             break;
189e71b7053SJung-uk Kim         case OPT_CIPHER:
190e71b7053SJung-uk Kim             cipher = opt_arg();
191e71b7053SJung-uk Kim             break;
192e71b7053SJung-uk Kim         case OPT_CIPHERSUITES:
193e71b7053SJung-uk Kim             ciphersuites = opt_arg();
194e71b7053SJung-uk Kim             break;
195e71b7053SJung-uk Kim         case OPT_BUGS:
196e71b7053SJung-uk Kim             st_bugs = 1;
197e71b7053SJung-uk Kim             break;
198e71b7053SJung-uk Kim         case OPT_TIME:
199b077aed3SPierre Pronchery             maxtime = opt_int_arg();
200e71b7053SJung-uk Kim             break;
201e71b7053SJung-uk Kim         case OPT_WWW:
202e71b7053SJung-uk Kim             www_path = opt_arg();
203e71b7053SJung-uk Kim             buf_size = strlen(www_path) + fmt_http_get_cmd_size;
204e71b7053SJung-uk Kim             if (buf_size > sizeof(buf)) {
205e71b7053SJung-uk Kim                 BIO_printf(bio_err, "%s: -www option is too long\n", prog);
206e71b7053SJung-uk Kim                 goto end;
207e71b7053SJung-uk Kim             }
208e71b7053SJung-uk Kim             break;
209e71b7053SJung-uk Kim         case OPT_SSL3:
210b077aed3SPierre Pronchery             min_version = SSL3_VERSION;
211e71b7053SJung-uk Kim             max_version = SSL3_VERSION;
212e71b7053SJung-uk Kim             break;
213b077aed3SPierre Pronchery         case OPT_TLS1:
214b077aed3SPierre Pronchery             min_version = TLS1_VERSION;
215b077aed3SPierre Pronchery             max_version = TLS1_VERSION;
216b077aed3SPierre Pronchery             break;
217b077aed3SPierre Pronchery         case OPT_TLS1_1:
218b077aed3SPierre Pronchery             min_version = TLS1_1_VERSION;
219b077aed3SPierre Pronchery             max_version = TLS1_1_VERSION;
220b077aed3SPierre Pronchery             break;
221b077aed3SPierre Pronchery         case OPT_TLS1_2:
222b077aed3SPierre Pronchery             min_version = TLS1_2_VERSION;
223b077aed3SPierre Pronchery             max_version = TLS1_2_VERSION;
224b077aed3SPierre Pronchery             break;
225b077aed3SPierre Pronchery         case OPT_TLS1_3:
226b077aed3SPierre Pronchery             min_version = TLS1_3_VERSION;
227b077aed3SPierre Pronchery             max_version = TLS1_3_VERSION;
228b077aed3SPierre Pronchery             break;
229b077aed3SPierre Pronchery         case OPT_PROV_CASES:
230b077aed3SPierre Pronchery             if (!opt_provider(o))
231b077aed3SPierre Pronchery                 goto end;
232b077aed3SPierre Pronchery             break;
233e71b7053SJung-uk Kim         }
234e71b7053SJung-uk Kim     }
235b077aed3SPierre Pronchery 
236b077aed3SPierre Pronchery     /* No extra arguments. */
237e71b7053SJung-uk Kim     argc = opt_num_rest();
238e71b7053SJung-uk Kim     if (argc != 0)
239e71b7053SJung-uk Kim         goto opthelp;
240f579bf8eSKris Kennaway 
241e71b7053SJung-uk Kim     if (cipher == NULL)
242e71b7053SJung-uk Kim         cipher = getenv("SSL_CIPHER");
24374664626SKris Kennaway 
244e71b7053SJung-uk Kim     if ((ctx = SSL_CTX_new(meth)) == NULL)
24574664626SKris Kennaway         goto end;
24674664626SKris Kennaway 
247e71b7053SJung-uk Kim     SSL_CTX_set_quiet_shutdown(ctx, 1);
248b077aed3SPierre Pronchery     if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
249b077aed3SPierre Pronchery         goto end;
250e71b7053SJung-uk Kim     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
251e71b7053SJung-uk Kim         goto end;
25274664626SKris Kennaway 
2536f9291ceSJung-uk Kim     if (st_bugs)
254e71b7053SJung-uk Kim         SSL_CTX_set_options(ctx, SSL_OP_ALL);
255e71b7053SJung-uk Kim     if (cipher != NULL && !SSL_CTX_set_cipher_list(ctx, cipher))
256e71b7053SJung-uk Kim         goto end;
257e71b7053SJung-uk Kim     if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites))
258e71b7053SJung-uk Kim         goto end;
259e71b7053SJung-uk Kim     if (!set_cert_stuff(ctx, certfile, keyfile))
26074664626SKris Kennaway         goto end;
26174664626SKris Kennaway 
262b077aed3SPierre Pronchery     if (!ctx_set_verify_locations(ctx, CAfile, noCAfile, CApath, noCApath,
263b077aed3SPierre Pronchery                                   CAstore, noCAstore)) {
26474664626SKris Kennaway         ERR_print_errors(bio_err);
265e71b7053SJung-uk Kim         goto end;
26674664626SKris Kennaway     }
2676f9291ceSJung-uk Kim     if (!(perform & 1))
2686f9291ceSJung-uk Kim         goto next;
269e71b7053SJung-uk Kim     printf("Collecting connection statistics for %d seconds\n", maxtime);
27074664626SKris Kennaway 
27174664626SKris Kennaway     /* Loop and time how long it takes to make connections */
27274664626SKris Kennaway 
27374664626SKris Kennaway     bytes_read = 0;
274e71b7053SJung-uk Kim     finishtime = (long)time(NULL) + maxtime;
27574664626SKris Kennaway     tm_Time_F(START);
2766f9291ceSJung-uk Kim     for (;;) {
2776f9291ceSJung-uk Kim         if (finishtime < (long)time(NULL))
2786f9291ceSJung-uk Kim             break;
27974664626SKris Kennaway 
280e71b7053SJung-uk Kim         if ((scon = doConnection(NULL, host, ctx)) == NULL)
28174664626SKris Kennaway             goto end;
28274664626SKris Kennaway 
283e71b7053SJung-uk Kim         if (www_path != NULL) {
284e71b7053SJung-uk Kim             buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
285e71b7053SJung-uk Kim                                    www_path);
286e71b7053SJung-uk Kim             if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
28774664626SKris Kennaway                 goto end;
28874664626SKris Kennaway             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
28974664626SKris Kennaway                 bytes_read += i;
29074664626SKris Kennaway         }
29174664626SKris Kennaway         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
292e71b7053SJung-uk Kim         BIO_closesocket(SSL_get_fd(scon));
29374664626SKris Kennaway 
29474664626SKris Kennaway         nConn += 1;
295e71b7053SJung-uk Kim         if (SSL_session_reused(scon)) {
29674664626SKris Kennaway             ver = 'r';
297e71b7053SJung-uk Kim         } else {
29874664626SKris Kennaway             ver = SSL_version(scon);
29974664626SKris Kennaway             if (ver == TLS1_VERSION)
30074664626SKris Kennaway                 ver = 't';
30174664626SKris Kennaway             else if (ver == SSL3_VERSION)
30274664626SKris Kennaway                 ver = '3';
30374664626SKris Kennaway             else
30474664626SKris Kennaway                 ver = '*';
30574664626SKris Kennaway         }
30674664626SKris Kennaway         fputc(ver, stdout);
30774664626SKris Kennaway         fflush(stdout);
30874664626SKris Kennaway 
30974664626SKris Kennaway         SSL_free(scon);
31074664626SKris Kennaway         scon = NULL;
31174664626SKris Kennaway     }
31274664626SKris Kennaway     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
31374664626SKris Kennaway 
314e71b7053SJung-uk Kim     i = (int)((long)time(NULL) - finishtime + maxtime);
3156f9291ceSJung-uk Kim     printf
3166f9291ceSJung-uk Kim         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
3176f9291ceSJung-uk Kim          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
3186f9291ceSJung-uk Kim     printf
3196f9291ceSJung-uk Kim         ("%d connections in %ld real seconds, %ld bytes read per connection\n",
320b6c1fdcdSJung-uk Kim          nConn, (long)time(NULL) - finishtime + maxtime,
321b6c1fdcdSJung-uk Kim          nConn > 0 ? bytes_read / nConn : 0l);
32274664626SKris Kennaway 
3236f9291ceSJung-uk Kim     /*
3246f9291ceSJung-uk Kim      * Now loop and time connections using the same session id over and over
3256f9291ceSJung-uk Kim      */
32674664626SKris Kennaway 
32774664626SKris Kennaway  next:
3286f9291ceSJung-uk Kim     if (!(perform & 2))
3296f9291ceSJung-uk Kim         goto end;
33074664626SKris Kennaway     printf("\n\nNow timing with session id reuse.\n");
33174664626SKris Kennaway 
33274664626SKris Kennaway     /* Get an SSL object so we can reuse the session id */
333e71b7053SJung-uk Kim     if ((scon = doConnection(NULL, host, ctx)) == NULL) {
334e71b7053SJung-uk Kim         BIO_printf(bio_err, "Unable to get connection\n");
33574664626SKris Kennaway         goto end;
33674664626SKris Kennaway     }
33774664626SKris Kennaway 
338e71b7053SJung-uk Kim     if (www_path != NULL) {
339e71b7053SJung-uk Kim         buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd, www_path);
340e71b7053SJung-uk Kim         if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
341e71b7053SJung-uk Kim             goto end;
342e71b7053SJung-uk Kim         while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
343e71b7053SJung-uk Kim             continue;
34474664626SKris Kennaway     }
34574664626SKris Kennaway     SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
346b077aed3SPierre Pronchery     if ((fd = SSL_get_fd(scon)) >= 0)
347b077aed3SPierre Pronchery         BIO_closesocket(fd);
34874664626SKris Kennaway 
34974664626SKris Kennaway     nConn = 0;
35074664626SKris Kennaway     totalTime = 0.0;
35174664626SKris Kennaway 
352e71b7053SJung-uk Kim     finishtime = (long)time(NULL) + maxtime;
35374664626SKris Kennaway 
35474664626SKris Kennaway     printf("starting\n");
35574664626SKris Kennaway     bytes_read = 0;
35674664626SKris Kennaway     tm_Time_F(START);
35774664626SKris Kennaway 
3586f9291ceSJung-uk Kim     for (;;) {
3596f9291ceSJung-uk Kim         if (finishtime < (long)time(NULL))
3606f9291ceSJung-uk Kim             break;
36174664626SKris Kennaway 
362e71b7053SJung-uk Kim         if ((doConnection(scon, host, ctx)) == NULL)
36374664626SKris Kennaway             goto end;
36474664626SKris Kennaway 
365e71b7053SJung-uk Kim         if (www_path != NULL) {
366e71b7053SJung-uk Kim             buf_len = BIO_snprintf(buf, sizeof(buf), fmt_http_get_cmd,
367e71b7053SJung-uk Kim                                    www_path);
368e71b7053SJung-uk Kim             if (buf_len <= 0 || SSL_write(scon, buf, buf_len) <= 0)
36974664626SKris Kennaway                 goto end;
37074664626SKris Kennaway             while ((i = SSL_read(scon, buf, sizeof(buf))) > 0)
37174664626SKris Kennaway                 bytes_read += i;
37274664626SKris Kennaway         }
37374664626SKris Kennaway         SSL_set_shutdown(scon, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
374b077aed3SPierre Pronchery         if ((fd = SSL_get_fd(scon)) >= 0)
375b077aed3SPierre Pronchery             BIO_closesocket(fd);
37674664626SKris Kennaway 
37774664626SKris Kennaway         nConn += 1;
378e71b7053SJung-uk Kim         if (SSL_session_reused(scon)) {
37974664626SKris Kennaway             ver = 'r';
380e71b7053SJung-uk Kim         } else {
38174664626SKris Kennaway             ver = SSL_version(scon);
38274664626SKris Kennaway             if (ver == TLS1_VERSION)
38374664626SKris Kennaway                 ver = 't';
38474664626SKris Kennaway             else if (ver == SSL3_VERSION)
38574664626SKris Kennaway                 ver = '3';
38674664626SKris Kennaway             else
38774664626SKris Kennaway                 ver = '*';
38874664626SKris Kennaway         }
38974664626SKris Kennaway         fputc(ver, stdout);
39074664626SKris Kennaway         fflush(stdout);
39174664626SKris Kennaway     }
39274664626SKris Kennaway     totalTime += tm_Time_F(STOP); /* Add the time for this iteration */
39374664626SKris Kennaway 
3946f9291ceSJung-uk Kim     printf
3956f9291ceSJung-uk Kim         ("\n\n%d connections in %.2fs; %.2f connections/user sec, bytes read %ld\n",
3966f9291ceSJung-uk Kim          nConn, totalTime, ((double)nConn / totalTime), bytes_read);
397b077aed3SPierre Pronchery     if (nConn > 0)
3986f9291ceSJung-uk Kim         printf
3996f9291ceSJung-uk Kim             ("%d connections in %ld real seconds, %ld bytes read per connection\n",
400e71b7053SJung-uk Kim              nConn, (long)time(NULL) - finishtime + maxtime, bytes_read / nConn);
401b077aed3SPierre Pronchery     else
402b077aed3SPierre Pronchery         printf("0 connections in %ld real seconds\n",
403b077aed3SPierre Pronchery                (long)time(NULL) - finishtime + maxtime);
40474664626SKris Kennaway     ret = 0;
40574664626SKris Kennaway 
406e71b7053SJung-uk Kim  end:
407e71b7053SJung-uk Kim     SSL_free(scon);
408e71b7053SJung-uk Kim     SSL_CTX_free(ctx);
409e71b7053SJung-uk Kim     return ret;
41074664626SKris Kennaway }
41174664626SKris Kennaway 
4126f9291ceSJung-uk Kim /*-
41374664626SKris Kennaway  * doConnection - make a connection
41474664626SKris Kennaway  */
doConnection(SSL * scon,const char * host,SSL_CTX * ctx)415e71b7053SJung-uk Kim static SSL *doConnection(SSL *scon, const char *host, SSL_CTX *ctx)
41674664626SKris Kennaway {
41774664626SKris Kennaway     BIO *conn;
41874664626SKris Kennaway     SSL *serverCon;
419e71b7053SJung-uk Kim     int i;
42074664626SKris Kennaway 
42174664626SKris Kennaway     if ((conn = BIO_new(BIO_s_connect())) == NULL)
422e71b7053SJung-uk Kim         return NULL;
42374664626SKris Kennaway 
424b077aed3SPierre Pronchery     if (BIO_set_conn_hostname(conn, host) <= 0
425b077aed3SPierre Pronchery             || BIO_set_conn_mode(conn, BIO_SOCK_NODELAY) <= 0) {
426b077aed3SPierre Pronchery         BIO_free(conn);
427b077aed3SPierre Pronchery         return NULL;
428b077aed3SPierre Pronchery     }
42974664626SKris Kennaway 
430b077aed3SPierre Pronchery     if (scon == NULL) {
431e71b7053SJung-uk Kim         serverCon = SSL_new(ctx);
432b077aed3SPierre Pronchery         if (serverCon == NULL) {
433b077aed3SPierre Pronchery             BIO_free(conn);
434b077aed3SPierre Pronchery             return NULL;
435b077aed3SPierre Pronchery         }
436b077aed3SPierre Pronchery     } else {
43774664626SKris Kennaway         serverCon = scon;
43874664626SKris Kennaway         SSL_set_connect_state(serverCon);
43974664626SKris Kennaway     }
44074664626SKris Kennaway 
44174664626SKris Kennaway     SSL_set_bio(serverCon, conn, conn);
44274664626SKris Kennaway 
44374664626SKris Kennaway     /* ok, lets connect */
44474664626SKris Kennaway     i = SSL_connect(serverCon);
4456f9291ceSJung-uk Kim     if (i <= 0) {
44674664626SKris Kennaway         BIO_printf(bio_err, "ERROR\n");
447e71b7053SJung-uk Kim         if (verify_args.error != X509_V_OK)
44874664626SKris Kennaway             BIO_printf(bio_err, "verify error:%s\n",
449e71b7053SJung-uk Kim                        X509_verify_cert_error_string(verify_args.error));
45074664626SKris Kennaway         else
45174664626SKris Kennaway             ERR_print_errors(bio_err);
45274664626SKris Kennaway         if (scon == NULL)
45374664626SKris Kennaway             SSL_free(serverCon);
45474664626SKris Kennaway         return NULL;
45574664626SKris Kennaway     }
45674664626SKris Kennaway 
457e71b7053SJung-uk Kim #if defined(SOL_SOCKET) && defined(SO_LINGER)
458e71b7053SJung-uk Kim     {
459e71b7053SJung-uk Kim         struct linger no_linger;
460e71b7053SJung-uk Kim         int fd;
461e71b7053SJung-uk Kim 
462e71b7053SJung-uk Kim         no_linger.l_onoff  = 1;
463e71b7053SJung-uk Kim         no_linger.l_linger = 0;
464e71b7053SJung-uk Kim         fd = SSL_get_fd(serverCon);
465e71b7053SJung-uk Kim         if (fd >= 0)
466e71b7053SJung-uk Kim             (void)setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&no_linger,
467e71b7053SJung-uk Kim                              sizeof(no_linger));
468e71b7053SJung-uk Kim     }
469e71b7053SJung-uk Kim #endif
470e71b7053SJung-uk Kim 
47174664626SKris Kennaway     return serverCon;
47274664626SKris Kennaway }
473e71b7053SJung-uk Kim #endif /* OPENSSL_NO_SOCK */
474