tls: dtls initial implementation
[vpp.git] / src / plugins / hs_apps / vcl / vcl_test_client.c
index b9bdd6e..35f1ac1 100644 (file)
@@ -51,6 +51,7 @@ typedef struct
   vcl_test_t post_test;
   uint8_t proto;
   uint32_t n_workers;
+  uint32_t ckpair_index;
   volatile int active_workers;
   struct sockaddr_storage server_addr;
 } vcl_test_client_main_t;
@@ -123,7 +124,7 @@ vtc_quic_connect_test_sessions (vcl_test_client_worker_t * wrk)
 {
   vcl_test_client_main_t *vcm = &vcl_client_main;
   vcl_test_session_t *ts, *tq;
-  uint32_t i;
+  uint32_t i, flags, flen;
   int rv;
 
   if (wrk->cfg.num_test_sessions < 1 || wrk->cfg.num_test_sessions_perq < 1)
@@ -155,7 +156,7 @@ vtc_quic_connect_test_sessions (vcl_test_client_worker_t * wrk)
   for (i = 0; i < wrk->cfg.num_test_qsessions; i++)
     {
       tq = &wrk->qsessions[i];
-      tq->fd = vppcom_session_create (vcm->proto, 1 /* is_nonblocking */ );
+      tq->fd = vppcom_session_create (vcm->proto, 0 /* is_nonblocking */ );
       tq->session_index = i;
       if (tq->fd < 0)
        {
@@ -163,12 +164,16 @@ vtc_quic_connect_test_sessions (vcl_test_client_worker_t * wrk)
          return tq->fd;
        }
 
+      /* Connect is blocking */
       rv = vppcom_session_connect (tq->fd, &vcm->server_endpt);
       if (rv < 0)
        {
          vterr ("vppcom_session_connect()", rv);
          return rv;
        }
+      flags = O_NONBLOCK;
+      flen = sizeof (flags);
+      vppcom_session_attr (tq->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
       vtinf ("Test Qsession %d (fd %d) connected.", i, tq->fd);
     }
   wrk->n_qsessions = wrk->cfg.num_test_qsessions;
@@ -223,6 +228,7 @@ vtc_connect_test_sessions (vcl_test_client_worker_t * wrk)
   vcl_test_client_main_t *vcm = &vcl_client_main;
   vcl_test_session_t *ts;
   uint32_t n_test_sessions;
+  uint32_t flags, flen;
   int i, rv;
 
   if (vcm->proto == VPPCOM_PROTO_QUIC)
@@ -253,19 +259,30 @@ vtc_connect_test_sessions (vcl_test_client_worker_t * wrk)
   for (i = 0; i < n_test_sessions; i++)
     {
       ts = &wrk->sessions[i];
-      ts->fd = vppcom_session_create (vcm->proto, 1 /* is_nonblocking */ );
+      ts->fd = vppcom_session_create (vcm->proto, 0 /* is_nonblocking */ );
       if (ts->fd < 0)
        {
          vterr ("vppcom_session_create()", ts->fd);
          return ts->fd;
        }
 
+      if (vcm->proto == VPPCOM_PROTO_TLS)
+       {
+         uint32_t ckp_len = sizeof (vcm->ckpair_index);
+         vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_CKPAIR,
+                              &vcm->ckpair_index, &ckp_len);
+       }
+
+      /* Connect is blocking */
       rv = vppcom_session_connect (ts->fd, &vcm->server_endpt);
       if (rv < 0)
        {
          vterr ("vppcom_session_connect()", rv);
          return rv;
        }
+      flags = O_NONBLOCK;
+      flen = sizeof (flags);
+      vppcom_session_attr (ts->fd, VPPCOM_ATTR_SET_FLAGS, &flags, &flen);
       vtinf ("Test session %d (fd %d) connected.", i, ts->fd);
     }
   wrk->n_sessions = n_test_sessions;
@@ -385,7 +402,7 @@ vtc_accumulate_stats (vcl_test_client_worker_t * wrk,
 
       if (ctrl->cfg.verbose > 1)
        {
-         sprintf (buf, "CLIENT (fd %d) RESULTS", ts->fd);
+         snprintf (buf, sizeof (buf), "CLIENT (fd %d) RESULTS", ts->fd);
          vcl_test_stats_dump (buf, &ts->stats, show_rx, 1 /* show tx */ ,
                               ctrl->cfg.verbose);
        }
@@ -544,10 +561,10 @@ vtc_print_stats (vcl_test_session_t * ctrl)
     }
 
   if (is_echo)
-    sprintf (buf, "Echo");
+    snprintf (buf, sizeof (buf), "Echo");
   else
-    sprintf (buf, "%s-directional Stream",
-            ctrl->cfg.test == VCL_TEST_TYPE_BI ? "Bi" : "Uni");
+    snprintf (buf, sizeof (buf), "%s-directional Stream",
+             ctrl->cfg.test == VCL_TEST_TYPE_BI ? "Bi" : "Uni");
 }
 
 static void
@@ -632,32 +649,6 @@ vtc_stream_client (vcl_test_client_main_t * vcm)
     vtwrn ("post-test cfg sync failed!");
 }
 
-static void
-dump_help (void)
-{
-#define INDENT "\n  "
-
-  printf ("CLIENT: Test configuration commands:"
-         INDENT VCL_TEST_TOKEN_HELP
-         "\t\t\tDisplay help."
-         INDENT VCL_TEST_TOKEN_EXIT
-         "\t\t\tExit test client & server."
-         INDENT VCL_TEST_TOKEN_SHOW_CFG
-         "\t\t\tShow the current test cfg."
-         INDENT VCL_TEST_TOKEN_RUN_UNI
-         "\t\t\tRun the Uni-directional test."
-         INDENT VCL_TEST_TOKEN_RUN_BI
-         "\t\t\tRun the Bi-directional test."
-         INDENT VCL_TEST_TOKEN_VERBOSE
-         "\t\t\tToggle verbose setting."
-         INDENT VCL_TEST_TOKEN_RXBUF_SIZE
-         "<rxbuf size>\tRx buffer size (bytes)."
-         INDENT VCL_TEST_TOKEN_TXBUF_SIZE
-         "<txbuf size>\tTx buffer size (bytes)."
-         INDENT VCL_TEST_TOKEN_NUM_WRITES
-         "<# of writes>\tNumber of txbuf writes to server." "\n");
-}
-
 static void
 cfg_txbuf_size_set (void)
 {
@@ -903,7 +894,7 @@ vtc_process_opts (vcl_test_client_main_t * vcm, int argc, char **argv)
                   optopt, ctrl->txbuf_size);
            print_usage_and_exit ();
          }
-       strcpy (ctrl->txbuf, optarg);
+       strncpy (ctrl->txbuf, optarg, ctrl->txbuf_size);
        ctrl->cfg.test = VCL_TEST_TYPE_ECHO;
        break;
 
@@ -1114,13 +1105,26 @@ main (int argc, char **argv)
   if (ctrl->fd < 0)
     vtfail ("vppcom_session_create()", ctrl->fd);
 
-  if (vcm->proto == VPPCOM_PROTO_TLS || vcm->proto == VPPCOM_PROTO_QUIC)
+  if (vcm->proto == VPPCOM_PROTO_TLS || vcm->proto == VPPCOM_PROTO_QUIC ||
+      vcm->proto == VPPCOM_PROTO_DTLS)
     {
+      vppcom_cert_key_pair_t ckpair;
+      uint32_t ckp_len;
+      int ckp_index;
+
       vtinf ("Adding tls certs ...");
-      vppcom_session_tls_add_cert (ctrl->fd, vcl_test_crt_rsa,
-                                  vcl_test_crt_rsa_len);
-      vppcom_session_tls_add_key (ctrl->fd, vcl_test_key_rsa,
-                                 vcl_test_key_rsa_len);
+      ckpair.cert = vcl_test_crt_rsa;
+      ckpair.key = vcl_test_key_rsa;
+      ckpair.cert_len = vcl_test_crt_rsa_len;
+      ckpair.key_len = vcl_test_key_rsa_len;
+      ckp_index = vppcom_add_cert_key_pair (&ckpair);
+      if (ckp_index < 0)
+       vtfail ("vppcom_add_cert_key_pair()", ckp_index);
+
+      vcm->ckpair_index = ckp_index;
+      ckp_len = sizeof (ckp_index);
+      vppcom_session_attr (ctrl->fd, VPPCOM_ATTR_SET_CKPAIR, &ckp_index,
+                          &ckp_len);
     }
 
   vtinf ("Connecting to server...");