You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // snfNETmgr.hpp
  2. //
  3. // (C) Copyright 2006 - 2020 ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // SNF network node manager.
  7. // 20080312 _M Refactored exceptions to std::runtime_exception
  8. #pragma once
  9. #include <stdexcept>
  10. #include <vector>
  11. #include "../CodeDweller/networking.hpp"
  12. #include "../CodeDweller/timing.hpp"
  13. #include "../CodeDweller/threading.hpp"
  14. #include "../CodeDweller/mangler.hpp"
  15. #include "snfCFGmgr.hpp"
  16. #include "snfLOGmgr.hpp"
  17. #include "snfGBUdbmgr.hpp"
  18. namespace cd = codedweller;
  19. class snfScanData; // Declare snfScanData;
  20. class snfLOGmgr; // Declare snfLOGmgr;
  21. class snfGBUdbmgr; // Declare snfGBUdbmgr;
  22. typedef std::vector<unsigned char> PadBuffer; // Holds one time pads etc.
  23. const unsigned int SNFHandshakeSize = 8; // Size of an SNF Handshake.
  24. const unsigned int SNFChallengeSize = 32; // Size of an SNF Challenge.
  25. const unsigned int SNFPadSize = 16; // Size of an SNF One Time Pad.
  26. const unsigned int SNFSignatureSize = SNFHandshakeSize; // Size of an SNF Signature.
  27. class snfNETmgr : public cd::Thread { // The network process manager.
  28. private:
  29. cd::Mutex myMutex; // Object is busy mutex.
  30. cd::Mutex ResolverMutex; // Mutex to protect lookups.
  31. cd::Mutex ConfigMutex; // Configuration change/use mutex.
  32. cd::Mutex PadMutex; // Pad use/evoloution mutex.
  33. snfLOGmgr* myLOGmgr; // Log manager to use.
  34. snfGBUdbmgr* myGBUdbmgr; // GBUdb manager to use.
  35. volatile bool isTimeToStop; // Time to shutdown flag.
  36. volatile bool isConfigured; // True once ready to run.
  37. cd::Timeout SYNCTimer; // SYNC timer.
  38. void evolvePad(std::string Entropy = ""); // Add entropy to and evolve.
  39. cd::Mangler PadGenerator; // Random pad source.
  40. PadBuffer OneTimePad(int Len = SNFPadSize); // Provides Len bytes of one time pad.
  41. // Configuration data
  42. std::string License; // Node (license) Id?
  43. std::string SecurityKey; // Security key for this rulebase?
  44. std::string RulebaseFilePath; // Where we can find our rulebase?
  45. std::string HandshakeFilePath; // Where do we keep our handshake?
  46. std::string UpdateReadyFilePath; // Where do I put update trigger files?
  47. std::string SyncHostName; // Where do we connect to sync?
  48. int SyncHostPort; // What port do we use to sync?
  49. int SyncSecsOverride; // How may secs between sync (override)?
  50. int SyncSecsConfigured; // How many secs to sync (nominally)?
  51. PadBuffer Handshake(); // What is the current handshake?
  52. PadBuffer& Handshake(PadBuffer& NewHandshake); // Store a new handshake.
  53. PadBuffer CurrentHandshake; // Where we keep our current handshake.
  54. void postUpdateTrigger(std::string& updateUTC); // Post an update trigger file.
  55. std::string SamplesBuffer; // Message Samples Appended Together.
  56. std::string getSamples(); // Syncrhonized way to get Samples.
  57. std::string ReportsBuffer; // Status Reports Appended Together.
  58. std::string getReports(); // Synchronized way to get Reports.
  59. public:
  60. snfNETmgr(); // Construct and start.
  61. ~snfNETmgr(); // Shutdown and destruct.
  62. void stop(); // How to stop the thread.
  63. void myTask(); // Define the thread task.
  64. void linkLOGmgr(snfLOGmgr& L); // Set the LOGmgr.
  65. void linkGBUdbmgr(snfGBUdbmgr& G); // Set the GBUdbmgr.
  66. void configure(snfCFGData& CFGData); // Update the configuration.
  67. class SyncFailed : public std::runtime_error { // Thrown if sync doesn't work.
  68. public: SyncFailed(const std::string& w):runtime_error(w) {}
  69. };
  70. // Operations
  71. // Why have configure AND pass CFGData in action calls?
  72. // The configure() method updates background task configuration itmes.
  73. // The CFGData passed on action calls informs the configuration in use with
  74. // that particular operation -- it might be different than the current CFG
  75. // if the CFG has been updated recently (reload).
  76. void sendSample( // Send a sampled message...
  77. snfCFGData& CFGData, // Use this configuration,
  78. snfScanData& ScanData, // Include this scan data,
  79. const unsigned char* MessageBuffer, // This is the message itself
  80. int MessageLength // and it is this size.
  81. );
  82. void sendReport(const std::string& StatusReportText); // Send a status report...
  83. void sync(); // Do the whole "sync" thing.
  84. // Utility Functions
  85. unsigned long ResolveHostIPFromName(const std::string& N); // Find the IP.
  86. std::string& RulebaseUTC(std::string& t); // Gets local rulebase file UTC.
  87. const static cd::ThreadType Type; // The thread's type.
  88. const static cd::ThreadState Sleeping; // Taking a break.
  89. const static cd::ThreadState SYNC_Connect; // Connecting to SYNC server.
  90. const static cd::ThreadState SYNC_Read_Challenge; // Reading challenge.
  91. const static cd::ThreadState SYNC_Compute_Response; // Computing crypto response.
  92. const static cd::ThreadState SYNC_Send_Response; // Sending crypto response.
  93. const static cd::ThreadState SYNC_Read_Availabilty; // Reading rulebase status.
  94. const static cd::ThreadState SYNC_Send_GBUdb_Alerts; // Sending GBUdb alerts.
  95. const static cd::ThreadState SYNC_Send_Status_Reports; // Sending status reports.
  96. const static cd::ThreadState SYNC_Send_Samples; // Sending message samples.
  97. const static cd::ThreadState SYNC_Send_End_Of_Report; // Sending end of client data.
  98. const static cd::ThreadState SYNC_Read_Server_Response; // Reading server data.
  99. const static cd::ThreadState SYNC_Close_Connection; // Closing connection.
  100. const static cd::ThreadState SYNC_Parse_GBUdb_Reflections; // Parsing GBUdb reflections.
  101. const static cd::ThreadState SYNC_Log_Event; // Logging SYNC event.
  102. };