Produce less confusing statistics when "-out_totals" is used.
[openssl.git] / demos / tunala / tunala.c
index 708cb92532a5b6d1cf1778224bb02caa338d9493..e802a6209fff582de210704b827bbbf696ed471d 100644 (file)
@@ -108,6 +108,7 @@ static unsigned int def_verify_depth = 10;
 static int def_out_state = 0;
 static unsigned int def_out_verify = 0;
 static int def_out_totals = 0;
+static int def_out_conns = 0;
 
 static const char *helpstring =
 "\n'Tunala' (A tunneler with a New Zealand accent)\n"
@@ -133,6 +134,7 @@ static const char *helpstring =
 " -v_strict              (do not continue if peer doesn't authenticate)\n"
 " -v_once                (no verification in renegotiates)\n"
 " -v_depth <num>         (limit certificate chain depth, default = 10)\n"
+" -out_conns             (prints client connections and disconnections)\n"
 " -out_state             (prints SSL handshake states)\n"
 " -out_verify <0|1|2|3>  (prints certificate verification states: def=1)\n"
 " -out_totals            (prints out byte-totals when a tunnel closes)\n"
@@ -276,14 +278,10 @@ static int parse_verify_depth(const char *s, unsigned int *verify_depth)
 }
 
 /* Some fprintf format strings used when tunnels close */
-static const char *io_stats_client_dirty =
-"    SSL (network) traffic to/from server; %8lu bytes in, %8lu bytes out\n";
-static const char *io_stats_client_clean =
-"    tunnelled data to/from server;        %8lu bytes in, %8lu bytes out\n";
-static const char *io_stats_server_dirty =
-"    SSL (network) traffic to/from client; %8lu bytes in, %8lu bytes out\n";
-static const char *io_stats_server_clean =
-"    tunnelled data to/from client;        %8lu bytes in, %8lu bytes out\n";
+static const char *io_stats_dirty =
+"    SSL traffic;   %8lu bytes in, %8lu bytes out\n";
+static const char *io_stats_clean =
+"    clear traffic; %8lu bytes in, %8lu bytes out\n";
 
 int main(int argc, char *argv[])
 {
@@ -314,6 +312,7 @@ int main(int argc, char *argv[])
        int out_state = def_out_state;
        unsigned int out_verify = def_out_verify;
        int out_totals = def_out_totals;
+       int out_conns = def_out_conns;
 
 /* Parse command-line arguments */
 next_arg:
@@ -466,6 +465,9 @@ next_arg:
                } else if(strcmp(*argv, "-out_totals") == 0) {
                        out_totals = 1;
                        goto next_arg;
+               } else if(strcmp(*argv, "-out_conns") == 0) {
+                       out_conns = 1;
+                       goto next_arg;
                } else if((strcmp(*argv, "-h") == 0) ||
                                (strcmp(*argv, "-help") == 0) ||
                                (strcmp(*argv, "-?") == 0)) {
@@ -474,11 +476,14 @@ next_arg:
                } else
                        return usage(*argv, 1);
        }
+       /* Run any sanity checks we want here */
+       if(!cert && !dcert && server_mode)
+               fprintf(stderr, "WARNING: you are running an SSL server without "
+                               "a certificate - this may not work!\n");
 
        /* Initialise network stuff */
        if(!ip_initialise())
                return err_str0("ip_initialise failed");
-       err_str0("ip_initialise succeeded");
        /* Create the SSL_CTX */
        if((world.ssl_ctx = initialise_ssl_ctx(server_mode, engine_id,
                        cacert, cert, key, dcert, dkey, cipher_list, dh_file,
@@ -486,20 +491,19 @@ next_arg:
                        verify_mode, verify_depth)) == NULL)
                return err_str1("initialise_ssl_ctx(engine_id=%s) failed",
                        (engine_id == NULL) ? "NULL" : engine_id);
-       err_str1("initialise_ssl_ctx(engine_id=%s) succeeded",
-                       (engine_id == NULL) ? "NULL" : engine_id);
+       if(engine_id)
+               fprintf(stderr, "Info, engine '%s' initialised\n", engine_id);
        /* Create the listener */
        if((world.listen_fd = ip_create_listener(listenhost)) == -1)
                return err_str1("ip_create_listener(%s) failed", listenhost);
-       err_str1("ip_create_listener(%s) succeeded", listenhost);
+       fprintf(stderr, "Info, listening on '%s'\n", listenhost);
        if(!ip_parse_address(proxyhost, &proxy_ip, &proxy_port, 0))
                return err_str1("ip_parse_address(%s) failed", proxyhost);
-       err_str1("ip_parse_address(%s) succeeded", proxyhost);
-       fprintf(stderr, "Info - proxying to %d.%d.%d.%d:%d\n",
+       fprintf(stderr, "Info, proxying to '%s' (%d.%d.%d.%d:%d)\n", proxyhost,
                        (int)proxy_ip[0], (int)proxy_ip[1],
                        (int)proxy_ip[2], (int)proxy_ip[3], (int)proxy_port);
-       fprintf(stderr, "Info - set maxtunnels to %d\n", (int)max_tunnels);
-       fprintf(stderr, "Info - set to operate as an SSL %s\n",
+       fprintf(stderr, "Info, set maxtunnels to %d\n", (int)max_tunnels);
+       fprintf(stderr, "Info, set to operate as an SSL %s\n",
                        (server_mode ? "server" : "client"));
        /* Initialise the rest of the stuff */
        world.tunnels_used = world.tunnels_size = 0;
@@ -521,7 +525,7 @@ main_loop:
                fprintf(stderr, "selector_select returned a badness error.\n");
                goto shouldnt_happen;
        case 0:
-               fprintf(stderr, "Warn, selector_select returned 0 - signal??\n");
+               fprintf(stderr, "Warn, selector_select returned 0 - signal?""?\n");
                goto main_loop;
        default:
                break;
@@ -534,7 +538,7 @@ main_loop:
                if(!tunala_world_new_item(&world, newfd, proxy_ip,
                                                proxy_port, flipped))
                        fprintf(stderr, "tunala_world_new_item failed\n");
-               else
+               else if(out_conns)
                        fprintf(stderr, "Info, new tunnel opened, now up to "
                                        "%d\n", world.tunnels_used);
        }
@@ -552,8 +556,7 @@ main_loop:
                                goto skip_totals;
                        fprintf(stderr, "Tunnel closing, traffic stats follow\n");
                        /* Display the encrypted (over the network) stats */
-                       fprintf(stderr, (server_mode ? io_stats_server_dirty :
-                                               io_stats_client_dirty),
+                       fprintf(stderr, io_stats_dirty,
                                buffer_total_in(state_machine_get_buffer(
                                                &t_item->sm,SM_DIRTY_IN)),
                                buffer_total_out(state_machine_get_buffer(
@@ -562,15 +565,15 @@ main_loop:
                         * *receive* is data sent *out* of the state_machine on
                         * its 'clean' side. Hence the apparent back-to-front
                         * OUT/IN mixup here :-) */
-                       fprintf(stderr, (server_mode ? io_stats_server_clean :
-                                               io_stats_client_clean),
+                       fprintf(stderr, io_stats_clean,
                                buffer_total_out(state_machine_get_buffer(
                                                &t_item->sm,SM_CLEAN_OUT)),
                                buffer_total_in(state_machine_get_buffer(
                                                &t_item->sm,SM_CLEAN_IN)));
 skip_totals:
                        tunala_world_del_item(&world, loop);
-                       fprintf(stderr, "Info, tunnel closed, down to %d\n",
+                       if(out_conns)
+                               fprintf(stderr, "Info, tunnel closed, down to %d\n",
                                        world.tunnels_used);
                }
                else {
@@ -718,7 +721,7 @@ static SSL_CTX *initialise_ssl_ctx(int server_mode, const char *engine_id,
                int out_state, int out_verify, int verify_mode,
                unsigned int verify_depth)
 {
-       SSL_CTX *ctx, *ret = NULL;
+       SSL_CTX *ctx = NULL, *ret = NULL;
        SSL_METHOD *meth;
        ENGINE *e = NULL;