clients.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. typedef uint8_t token[TOKEN_SIZE];
  2. typedef uint8_t aes_key[SGX_AESGCM_KEY_SIZE];
  3. // #define VERBOSE_CLIENT
  4. #define RANDOMIZE_CLIENT_RETRY_SLEEP_TIME
  5. // #define CLIENT_UNIQUE_IP
  6. // #define SHOW_RECEIVED_MESSAGES
  7. // VERBOSE_CLIENT implies SHOW_RECEIVED_MESSAGES
  8. #ifdef VERBOSE_CLIENT
  9. #define SHOW_RECEIVED_MESSAGES
  10. #endif
  11. #define PORT_START 32769
  12. #define PORT_END 60000
  13. /*
  14. Client -> Ingestion Server (C->I) communication protocols:
  15. 1) Authentication
  16. Format: Client sim_id, Epoch number, Authentication token
  17. 2) Messages
  18. Format: IV, AESGCM([CM_1], [CM_2], ..., [CM_k]), TAG
  19. - each CM = Client Message for token channel has the format :
  20. 4-byte Sender ID, 4-byte Recipient ID, 16-byte Token,
  21. <Upto msg_size - 24> - bytes of message data
  22. where the Sender ID and Recipient ID are the TEEMS client id
  23. (and not sim_id)
  24. */
  25. struct ip_addr
  26. {
  27. uint8_t ip1;
  28. uint8_t ip2;
  29. uint8_t ip3;
  30. uint8_t ip4;
  31. void increment(int nthreads) {
  32. ip4++;
  33. if(ip4==0) {
  34. ip3++;
  35. if(ip3==0) {
  36. ip2+=nthreads;
  37. }
  38. }
  39. }
  40. std::string ip_str() {
  41. return(std::to_string(ip1) + "." + std::to_string(ip2) + "."
  42. + std::to_string(ip3) + "." + std::to_string(ip4));
  43. }
  44. };
  45. /*
  46. Structure for capture each individual simulated client's state
  47. */
  48. class Client
  49. {
  50. private:
  51. // Clients' have a simulator ID sim_id used for:
  52. // (i) the simulator to divvy up clients across threads
  53. // (ii) the simulator and ingestion servers to align simulated clients
  54. // and their pre-established shared-secrets
  55. clientid_t sim_id;
  56. // The actual client id used by TEEMS is id.
  57. // Format: the first DEST_STORAGE_NODE_BITS bits store the storage node
  58. // number and the userid at that storage node in the last DEST_UID_BITS
  59. clientid_t id;
  60. aes_key ing_key;
  61. aes_key stg_key;
  62. // Clients send encrypted messages to ingestion
  63. // so they set and increment the IV
  64. unsigned char ing_iv[SGX_AESGCM_IV_SIZE] = {0};
  65. token *token_list;
  66. boost::asio::ip::tcp::socket *ingestion_sock = NULL;
  67. boost::asio::ip::tcp::socket *storage_sock = NULL;
  68. void generateAuthenticationMessage();
  69. int sendIngAuthMessage(unsigned long epoch_no);
  70. int sendStgAuthMessage(unsigned long epoch_no);
  71. void generateMessageBundle(uint8_t token_out, uint32_t msg_size,
  72. unsigned char *pt_msgbundle);
  73. bool encryptMessageBundle(uint32_t bundle_size, unsigned char *pt_msgbundle,
  74. unsigned char* enc_msgbundle);
  75. void sendMessageBundle();
  76. void initializeIngSocket(boost::asio::io_context &ioc, NodeConfig &ing_server,
  77. ip_addr *curr_ip, uint16_t &port_no);
  78. void initializeStgSocket(boost::asio::io_context &ioc, NodeConfig &ing_server,
  79. ip_addr *curr_ip, uint16_t &port_no);
  80. void initClient(clientid_t cid, uint16_t stg_id, aes_key ikey, aes_key skey);
  81. public:
  82. Client() {}
  83. ~Client() {
  84. free(token_list);
  85. delete(ingestion_sock);
  86. delete(storage_sock);
  87. }
  88. void setup_client(boost::asio::io_context &ioc, uint32_t sim_id,
  89. uint16_t ing_node_id, uint16_t stg_node_id,
  90. ip_addr *curr_ip, uint16_t &pno);
  91. void epoch_process();
  92. };