|
@@ -172,59 +172,128 @@ void NodeIO::recv_commands(
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
- Handler for received client messages.
|
|
|
-
|
|
|
+ Receive clients dropped off messages, i.e. a CLIENT_MESSAGE_BUNDLE
|
|
|
*/
|
|
|
-void NetIO::handle_async_clients(std::shared_ptr<tcp::socket> csocket,
|
|
|
- const boost::system::error_code& error, size_t auth_size,
|
|
|
- size_t msgbundle_size)
|
|
|
+
|
|
|
+void NetIO::receive_msgbundle(tcp::socket* csocket)
|
|
|
{
|
|
|
- if(!error) {
|
|
|
-#ifdef VERBOSE_NET
|
|
|
- printf("Accept handler success\n");
|
|
|
-#endif
|
|
|
- // Read header (1 uint64_t) from the socket and extract the client ID
|
|
|
- size_t header;
|
|
|
- clientid_t cid;
|
|
|
- boost::asio::read(*csocket,
|
|
|
- boost::asio::buffer(&header, sizeof(uint64_t)));
|
|
|
+ // Read header (1 uint64_t) from the socket and extract the client ID
|
|
|
+ size_t *header = new size_t;
|
|
|
+ boost::asio::async_read(*csocket, boost::asio::buffer(header, sizeof(size_t)),
|
|
|
+ [this, csocket, header]
|
|
|
+ (boost::system::error_code ec, std::size_t) {
|
|
|
+ if (ec) {
|
|
|
+ if(ec == boost::asio::error::eof) {
|
|
|
+ // Client connection terminated so we delete this socket
|
|
|
+ delete(csocket);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ printf("Error %s\n", ec.message().c_str());
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if((header & 0xff) == CLIENT_AUTHENTICATE) {
|
|
|
+ if((*header & 0xff) == CLIENT_MESSAGE_BUNDLE) {
|
|
|
+ clientid_t cid = (clientid_t)(*header >> 8);
|
|
|
// Read the authentication token
|
|
|
- boost::asio::read(*csocket,
|
|
|
- boost::asio::buffer(&header, auth_size));
|
|
|
-
|
|
|
- } else if ((header & 0xff) == CLIENT_MESSAGE_BUNDLE) {
|
|
|
+ delete(header);
|
|
|
unsigned char *msgbundle = (unsigned char*) malloc(msgbundle_size);
|
|
|
- cid = (clientid_t)(header >> 8);
|
|
|
|
|
|
- // Read the message_bundle
|
|
|
- boost::asio::read(*csocket,
|
|
|
- boost::asio::buffer(msgbundle, msgbundle_size));
|
|
|
+ boost::asio::async_read(*csocket, boost::asio::buffer(msgbundle, msgbundle_size),
|
|
|
+ [this, csocket, msgbundle, cid]
|
|
|
+ (boost::system::error_code ecc, std::size_t) {
|
|
|
+ if (ecc) {
|
|
|
+ if(ecc == boost::asio::error::eof) {
|
|
|
+ // Client connection terminated so we delete this socket
|
|
|
+ delete(csocket);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ printf("Error %s\n", ecc.message().c_str());
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //Ingest the message_bundle
|
|
|
+ bool ret = ecall_ingest_msgbundle(cid, msgbundle, apiparams.m_priv_out);
|
|
|
+ free(msgbundle);
|
|
|
+ });
|
|
|
|
|
|
- //Ingest the message_bundle
|
|
|
- bool ret = ecall_ingest_msgbundle(cid, msgbundle, apiparams.m_priv_out);
|
|
|
- free(msgbundle);
|
|
|
+ // Continue to async receive client message bundles
|
|
|
+ receive_msgbundle(csocket);
|
|
|
}
|
|
|
+ });
|
|
|
+}
|
|
|
|
|
|
- start_accept(auth_size, msgbundle_size);
|
|
|
- } else {
|
|
|
+/*
|
|
|
+ Handle new client connections.
|
|
|
+ New clients always send a CLIENT_AUTHENTICATE message, and then followed
|
|
|
+ by their CLIENT_MESSAGE_BUNDLE every epoch.
|
|
|
+*/
|
|
|
+void NetIO::authenticate_new_client(tcp::socket* csocket,
|
|
|
+ const boost::system::error_code& error)
|
|
|
+{
|
|
|
+ if(error) {
|
|
|
printf("Accept handler failed\n");
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+#ifdef VERBOSE_NET
|
|
|
+ printf("Accept handler success\n");
|
|
|
+#endif
|
|
|
+
|
|
|
+ // Read header (1 uint64_t) from the socket and extract the client ID
|
|
|
+ size_t *header = new size_t;
|
|
|
+ boost::asio::async_read(*csocket, boost::asio::buffer(header, sizeof(size_t)),
|
|
|
+ [this, csocket, header]
|
|
|
+ (boost::system::error_code ec, std::size_t) {
|
|
|
+ if (ec) {
|
|
|
+ if(ec == boost::asio::error::eof) {
|
|
|
+ // Client connection terminated so we delete this socket
|
|
|
+ delete(csocket);
|
|
|
+ } else {
|
|
|
+ printf("Error %s\n", ec.message().c_str());
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if((*header & 0xff) == CLIENT_AUTHENTICATE) {
|
|
|
+ clientid_t cid = (clientid_t)(*header >> 8);
|
|
|
+ // Read the authentication token
|
|
|
+ unsigned char* auth_string = (unsigned char*) malloc(auth_size);
|
|
|
+ delete(header);
|
|
|
+
|
|
|
+ boost::asio::async_read(*csocket, boost::asio::buffer(auth_string, auth_size),
|
|
|
+ [this, csocket, auth_string, cid]
|
|
|
+ (boost::system::error_code ecn, std::size_t) {
|
|
|
+ if (ecn) {
|
|
|
+ printf("Error %s\n", ecn.message().c_str());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool ret = ecall_authenticate(cid, auth_string);
|
|
|
+ free(auth_string);
|
|
|
+ });
|
|
|
+
|
|
|
+ // Receive client message bundles on this socket
|
|
|
+ receive_msgbundle(csocket);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ start_accept();
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
- Asynchronously accept client connections
|
|
|
+ Asynchronously accept new client connections
|
|
|
*/
|
|
|
-void NetIO::start_accept(size_t auth_size, size_t msgbundle_size)
|
|
|
+void NetIO::start_accept()
|
|
|
{
|
|
|
- std::shared_ptr<tcp::socket> csocket(new tcp::socket(io_context()));
|
|
|
+ tcp::socket *csocket = new tcp::socket(io_context());
|
|
|
#ifdef VERBOSE_NET
|
|
|
std::cout << "Accepting on " << myconf.clistenhost << ":" << myconf.clistenport << "\n";
|
|
|
#endif
|
|
|
client_acceptor->async_accept(*csocket,
|
|
|
- boost::bind(&NetIO::handle_async_clients, this, csocket,
|
|
|
- boost::asio::placeholders::error, auth_size, msgbundle_size));
|
|
|
+ boost::bind(&NetIO::authenticate_new_client, this, csocket,
|
|
|
+ boost::asio::placeholders::error));
|
|
|
}
|
|
|
|
|
|
|
|
@@ -304,14 +373,11 @@ NetIO::NetIO(boost::asio::io_context &io_context, const Config &config)
|
|
|
resolver.resolve(this->myconf.clistenhost,
|
|
|
this->myconf.clistenport)->endpoint()));
|
|
|
|
|
|
- size_t auth_size, msgbundle_size;
|
|
|
auth_size = SGX_AESGCM_MAC_SIZE;
|
|
|
msgbundle_size = SGX_AESGCM_IV_SIZE +
|
|
|
(apiparams.m_priv_out * apiparams.msg_size) + SGX_AESGCM_MAC_SIZE;
|
|
|
- start_accept(auth_size, msgbundle_size);
|
|
|
+ start_accept();
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|