From ad502c0acbbd8469f170a0f7315e5b4f01f64d54 Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Sat, 24 Jun 2017 19:11:45 +0900
Subject: [PATCH] Randomize connection ID and packet number

---
 examples/client.cc | 13 +++++++++++--
 examples/server.cc | 13 +++++++++++--
 examples/util.cc   |  5 +++++
 examples/util.h    |  3 +++
 4 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/examples/client.cc b/examples/client.cc
index c8fa1a70..2c79b567 100644
--- a/examples/client.cc
+++ b/examples/client.cc
@@ -39,9 +39,14 @@
 #include "template.h"
 #include "network.h"
 #include "debug.h"
+#include "util.h"
 
 using namespace ngtcp2;
 
+namespace {
+auto randgen = util::make_mt19937();
+} // namespace
+
 namespace {
 void *BIO_get_data(BIO *bio) { return bio->ptr; }
 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
@@ -185,7 +190,8 @@ ssize_t send_client_initial(ngtcp2_conn *conn, uint32_t flags,
     return NGTCP2_ERR_CALLBACK_FAILURE;
   }
 
-  *ppkt_num = 1;
+  *ppkt_num = std::uniform_int_distribution<uint64_t>(
+      0, std::numeric_limits<int32_t>::max())(randgen);
 
   auto len = c->read_client_handshake(pdest, maxdestlen);
 
@@ -241,7 +247,10 @@ int Client::init(int fd) {
       debug::recv_pkt,     debug::recv_frame,     debug::handshake_completed,
   };
 
-  rv = ngtcp2_conn_client_new(&conn_, 1, 1, &callbacks, this);
+  auto conn_id = std::uniform_int_distribution<uint64_t>(
+      0, std::numeric_limits<uint64_t>::max())(randgen);
+
+  rv = ngtcp2_conn_client_new(&conn_, conn_id, 1, &callbacks, this);
   if (rv != 0) {
     std::cerr << "ngtcp2_conn_client_new: " << rv << std::endl;
     return -1;
diff --git a/examples/server.cc b/examples/server.cc
index 70af4ad3..e92987b1 100644
--- a/examples/server.cc
+++ b/examples/server.cc
@@ -39,9 +39,14 @@
 #include "template.h"
 #include "network.h"
 #include "debug.h"
+#include "util.h"
 
 using namespace ngtcp2;
 
+namespace {
+auto randgen = util::make_mt19937();
+} // namespace
+
 namespace {
 void *BIO_get_data(BIO *bio) { return bio->ptr; }
 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
@@ -186,7 +191,8 @@ ssize_t send_server_cleartext(ngtcp2_conn *conn, uint32_t flags,
   }
 
   if (ppkt_num) {
-    *ppkt_num = 1;
+    *ppkt_num = std::uniform_int_distribution<uint64_t>(
+        0, std::numeric_limits<int32_t>::max())(randgen);
   }
 
   auto len = h->read_server_handshake(pdest, maxdestlen);
@@ -239,7 +245,10 @@ int Handler::init(int fd) {
       debug::handshake_completed,
   };
 
-  rv = ngtcp2_conn_server_new(&conn_, 2, 1, &callbacks, this);
+  auto conn_id = std::uniform_int_distribution<uint64_t>(
+      0, std::numeric_limits<uint64_t>::max())(randgen);
+
+  rv = ngtcp2_conn_server_new(&conn_, conn_id, 1, &callbacks, this);
   if (rv != 0) {
     std::cerr << "ngtcp2_conn_server_new: " << rv << std::endl;
     return -1;
diff --git a/examples/util.cc b/examples/util.cc
index 9000a342..41747072 100644
--- a/examples/util.cc
+++ b/examples/util.cc
@@ -55,6 +55,11 @@ std::string format_hex(const uint8_t *s, size_t len) {
   return res;
 }
 
+std::mt19937 make_mt19937() {
+  std::random_device rd;
+  return std::mt19937(rd());
+}
+
 } // namespace util
 
 } // namespace ngtcp2
diff --git a/examples/util.h b/examples/util.h
index b43e09fa..945bc943 100644
--- a/examples/util.h
+++ b/examples/util.h
@@ -30,6 +30,7 @@
 #endif // HAVE_CONFIG_H
 
 #include <string>
+#include <random>
 
 #include <ngtcp2/ngtcp2.h>
 
@@ -41,6 +42,8 @@ std::string format_hex(uint8_t c);
 
 std::string format_hex(const uint8_t *s, size_t len);
 
+std::mt19937 make_mt19937();
+
 } // namespace util
 
 } // namespace ngtcp2;
-- 
GitLab