Browse Source

A bit of refactoring in preparation for the networking code

Ian Goldberg 1 year ago
parent
commit
a48982890f
8 changed files with 69 additions and 24 deletions
  1. 5 6
      App/config.cpp
  2. 2 3
      App/config.hpp
  3. 2 4
      App/net.cpp
  4. 10 1
      App/net.hpp
  5. 9 0
      App/start.cpp
  6. 10 0
      App/start.hpp
  7. 26 6
      App/teems.cpp
  8. 5 4
      Makefile

+ 5 - 6
App/config.cpp

@@ -6,8 +6,6 @@
 #include "boost/property_tree/ptree.hpp"
 #include "boost/property_tree/json_parser.hpp"
 
-Config g_config;
-
 // Split a hostport string like "127.0.0.1:12000" at the rightmost colon
 // into a host part "127.0.0.1" and a port part "12000".
 static bool split_host_port(std::string &host, std::string &port,
@@ -23,7 +21,8 @@ static bool split_host_port(std::string &host, std::string &port,
     return true;
 }
 
-bool config_parse(const std::string configstr, const std::string &myname)
+bool config_parse(Config &config, const std::string configstr,
+    const std::string &myname)
 {
     bool found_my_node = false;
     bool found_params = false;
@@ -38,7 +37,7 @@ bool config_parse(const std::string configstr, const std::string &myname)
         if (!entry.first.compare("params")) {
             for (auto & pentry : entry.second) {
                 if (!pentry.first.compare("msgsize")) {
-                    g_config.msgsize = pentry.second.get_value<uint16_t>();
+                    config.msgsize = pentry.second.get_value<uint16_t>();
                 } else {
                     std::cerr << "Unknown field in params: " <<
                         pentry.first << "\n";
@@ -54,7 +53,7 @@ bool config_parse(const std::string configstr, const std::string &myname)
                     if (!nentry.first.compare("name")) {
                         nc.name = nentry.second.get_value<std::string>();
                         if (!myname.compare(nc.name)) {
-                            g_config.mynodenum = g_config.nodes.size();
+                            config.mynodenum = config.nodes.size();
                             found_my_node = true;
                         }
                     } else if (!nentry.first.compare("pubkey")) {
@@ -73,7 +72,7 @@ bool config_parse(const std::string configstr, const std::string &myname)
                         ret = false;
                     }
                 }
-                g_config.nodes.push_back(std::move(nc));
+                config.nodes.push_back(std::move(nc));
             }
         } else {
             std::cerr << "Unknown key in config: " <<

+ 2 - 3
App/config.hpp

@@ -24,8 +24,7 @@ struct Config {
     size_t mynodenum;
 };
 
-extern Config g_config;
-
-bool config_parse(const std::string configstr, const std::string &myname);
+bool config_parse(Config &config, const std::string configstr,
+    const std::string &myname);
 
 #endif

+ 2 - 4
App/net.cpp

@@ -1,9 +1,7 @@
 #include "config.hpp"
 #include "net.hpp"
 
-bool net_setup()
+NetIO::NetIO(boost::asio::io_context &io_context, const Config &config)
+    : conf(config)
 {
-    bool ret = true;
-
-    return ret;
 }

+ 10 - 1
App/net.hpp

@@ -1,6 +1,15 @@
 #ifndef __NET_HPP__
 #define __NET_HPP__
 
-bool net_setup();
+#include <boost/asio.hpp>
+
+#include "config.hpp"
+
+class NetIO {
+    const Config &conf;
+
+public:
+    NetIO(boost::asio::io_context &io_context, const Config &config);
+};
 
 #endif

+ 9 - 0
App/start.cpp

@@ -0,0 +1,9 @@
+#include "start.hpp"
+
+// Once all the networking is set up, start doing whatever we were asked
+// to do on the command line
+void start(NetIO &netio, int argc, char **argv)
+{
+    // Nothing yet
+}
+

+ 10 - 0
App/start.hpp

@@ -0,0 +1,10 @@
+#ifndef __START_HPP__
+#define __START_HPP__
+
+#include "net.hpp"
+
+// Once all the networking is set up, start doing whatever we were asked
+// to do on the command line
+void start(NetIO &netio, int argc, char **argv);
+
+#endif

+ 26 - 6
App/teems.cpp

@@ -2,12 +2,16 @@
 #include <cstdio>
 #include <cstring>
 
+#include <boost/asio.hpp>
+#include <boost/thread.hpp>
+
 #include "sgx_urts.h"
 #include "sgx_tcrypto.h"
 #include "sgx_tseal.h"
 #include "Untrusted.hpp"
 #include "config.hpp"
 #include "net.hpp"
+#include "start.hpp"
 
 static bool hexdump(FILE *outf, const char *label, void *p, size_t len)
 {
@@ -183,8 +187,8 @@ int main(int argc, char **argv)
     // Read the config.json from the first line of stdin.  We have to do
     // this before outputting anything to avoid potential deadlock with
     // the launch program.
-    std::string config;
-    std::getline(std::cin, config);
+    std::string configstr;
+    std::getline(std::cin, configstr);
 
     // Load the sealed private key
     FILE *sprivf = fopen(sealedprivkeyfile, "rb");
@@ -198,13 +202,29 @@ int main(int argc, char **argv)
     }
     fclose(sprivf);
 
-    if (!config_parse(config, myname)) {
-        exit(1);
-    }
-    if (!net_setup()) {
+    Config config;
+
+    if (!config_parse(config, configstr, myname)) {
         exit(1);
     }
 
+    boost::asio::io_context io_context;
+
+    // The NetIO will keep a (const) reference to the config
+    NetIO netio(io_context, config);
+
+    // Queue up the actual work
+    boost::asio::post(io_context, [&]{
+        start(netio, argc, argv);
+    });
+
+    // Start another thread; one will perform the work and the other
+    // will execute the async_write handlers
+    boost::thread t([&]{io_context.run();});
+    io_context.run();
+    t.join();
+
+    // All done
     sgx_destroy_enclave(global_eid);
 
     return 0;

+ 5 - 4
Makefile

@@ -101,7 +101,7 @@ else
 endif
 
 App_Cpp_Flags := $(App_C_Flags)
-App_Link_Flags := -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lpthread 
+App_Link_Flags := -lboost_thread -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lpthread
 
 App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
 
@@ -114,7 +114,7 @@ ifeq ($(SGX_MODE), HW)
 ifneq ($(SGX_DEBUG), 1)
 ifneq ($(SGX_PRERELEASE), 1)
 	# Choose to use 'Enclave.lds' for HW release mode
-	Enclave_Version_Script = Enclave/Enclave.lds 
+	Enclave_Version_Script = Enclave/Enclave.lds
 endif
 endif
 endif
@@ -129,7 +129,7 @@ endif
 Crypto_Library_Name := sgx_tcrypto
 
 Enclave_Cpp_Files := $(wildcard Enclave/*.cpp)
-Enclave_Include_Paths := -IEnclave -I$(SGX_SDK)/include -I$(SGX_SDK)/include/libcxx -I$(SGX_SDK)/include/tlibc 
+Enclave_Include_Paths := -IEnclave -I$(SGX_SDK)/include -I$(SGX_SDK)/include/libcxx -I$(SGX_SDK)/include/tlibc
 
 Enclave_C_Flags := -nostdinc -fvisibility=hidden -fpie -fstack-protector -fno-builtin-printf $(Enclave_Include_Paths)
 Enclave_Cpp_Flags := $(Enclave_C_Flags) -nostdinc++
@@ -284,7 +284,8 @@ depend:
 
 App/config.o: App/config.hpp
 App/net.o: App/config.hpp App/net.hpp
-App/teems.o: Untrusted/Untrusted.hpp App/config.hpp App/net.hpp
+App/start.o: App/start.hpp App/net.hpp App/config.hpp
+App/teems.o: Untrusted/Untrusted.hpp App/config.hpp App/net.hpp App/start.hpp
 Untrusted/Untrusted.o: Untrusted/Untrusted.hpp Untrusted/Enclave_u.h
 
 Enclave/comms.o: Enclave/Enclave_t.h Enclave/utils.hpp