// snfNETmgr.hpp // // (C) Copyright 2006 - 2009 ARM Research Labs, LLC. // See www.armresearch.com for the copyright terms. // // SNF network node manager. // 20080312 _M Refactored exceptions to std::runtime_exception #ifndef snfNETmgr_included #define snfNETmgr_included #include #include #include "CodeDweller/networking.hpp" #include "CodeDweller/timing.hpp" #include "CodeDweller/threading.hpp" #include "CodeDweller/mangler.hpp" #include "SNFMulti/snfCFGmgr.hpp" #include "SNFMulti/snfLOGmgr.hpp" #include "SNFMulti/snfGBUdbmgr.hpp" namespace SNFMulti { class snfScanData; // Declare snfScanData; class snfLOGmgr; // Declare snfLOGmgr; class snfGBUdbmgr; // Declare snfGBUdbmgr; typedef std::vector PadBuffer; // Holds one time pads etc. const unsigned int SNFHandshakeSize = 8; // Size of an SNF Handshake. const unsigned int SNFChallengeSize = 32; // Size of an SNF Challenge. const unsigned int SNFPadSize = 16; // Size of an SNF One Time Pad. const unsigned int SNFSignatureSize = SNFHandshakeSize; // Size of an SNF Signature. class snfNETmgr : public CodeDweller::Thread { // The network process manager. private: CodeDweller::Mutex myMutex; // Object is busy mutex. CodeDweller::Mutex ResolverMutex; // Mutex to protect lookups. CodeDweller::Mutex ConfigMutex; // Configuration change/use mutex. CodeDweller::Mutex PadMutex; // Pad use/evoloution mutex. snfLOGmgr* myLOGmgr; // Log manager to use. snfGBUdbmgr* myGBUdbmgr; // GBUdb manager to use. volatile bool isTimeToStop; // Time to shutdown flag. volatile bool isConfigured; // True once ready to run. CodeDweller::Timeout SYNCTimer; // SYNC timer. void evolvePad(std::string Entropy = ""); // Add entropy to and evolve. CodeDweller::MANGLER PadGenerator; // Random pad source. PadBuffer OneTimePad(int Len = SNFPadSize); // Provides Len bytes of one time pad. // Configuration data std::string License; // Node (license) Id? std::string SecurityKey; // Security key for this rulebase? std::string RulebaseFilePath; // Where we can find our rulebase? std::string HandshakeFilePath; // Where do we keep our handshake? std::string UpdateReadyFilePath; // Where do I put update trigger files? std::string SyncHostName; // Where do we connect to sync? int SyncHostPort; // What port do we use to sync? int SyncSecsOverride; // How may secs between sync (override)? int SyncSecsConfigured; // How many secs to sync (nominally)? PadBuffer Handshake(); // What is the current handshake? PadBuffer& Handshake(PadBuffer& NewHandshake); // Store a new handshake. PadBuffer CurrentHandshake; // Where we keep our current handshake. void postUpdateTrigger(std::string& updateUTC); // Post an update trigger file. std::string SamplesBuffer; // Message Samples Appended Together. std::string getSamples(); // Syncrhonized way to get Samples. std::string ReportsBuffer; // Status Reports Appended Together. std::string getReports(); // Synchronized way to get Reports. public: snfNETmgr(); // Construct and start. ~snfNETmgr(); // Shutdown and destruct. void stop(); // How to stop the thread. void myTask(); // Define the thread task. void linkLOGmgr(snfLOGmgr& L); // Set the LOGmgr. void linkGBUdbmgr(snfGBUdbmgr& G); // Set the GBUdbmgr. void configure(snfCFGData& CFGData); // Update the configuration. class SyncFailed : public std::runtime_error { // Thrown if sync doesn't work. public: SyncFailed(const std::string& w):std::runtime_error(w) {} }; // Operations // Why have configure AND pass CFGData in action calls? // The configure() method updates background task configuration itmes. // The CFGData passed on action calls informs the configuration in use with // that particular operation -- it might be different than the current CFG // if the CFG has been updated recently (reload). void sendSample( // Send a sampled message... snfCFGData& CFGData, // Use this configuration, snfScanData& ScanData, // Include this scan data, const unsigned char* MessageBuffer, // This is the message itself int MessageLength // and it is this size. ); void sendReport(const std::string& StatusReportText); // Send a status report... void sync(); // Do the whole "sync" thing. // Utility Functions unsigned long ResolveHostIPFromName(const std::string& N); // Find the IP. std::string& RulebaseUTC(std::string& t); // Gets local rulebase file UTC. const static CodeDweller::ThreadType Type; // The thread's type. const static CodeDweller::ThreadState Sleeping; // Taking a break. const static CodeDweller::ThreadState SYNC_Connect; // Connecting to SYNC server. const static CodeDweller::ThreadState SYNC_Read_Challenge; // Reading challenge. const static CodeDweller::ThreadState SYNC_Compute_Response; // Computing crypto response. const static CodeDweller::ThreadState SYNC_Send_Response; // Sending crypto response. const static CodeDweller::ThreadState SYNC_Read_Availabilty; // Reading rulebase status. const static CodeDweller::ThreadState SYNC_Send_GBUdb_Alerts; // Sending GBUdb alerts. const static CodeDweller::ThreadState SYNC_Send_Status_Reports; // Sending status reports. const static CodeDweller::ThreadState SYNC_Send_Samples; // Sending message samples. const static CodeDweller::ThreadState SYNC_Send_End_Of_Report; // Sending end of client data. const static CodeDweller::ThreadState SYNC_Read_Server_Response; // Reading server data. const static CodeDweller::ThreadState SYNC_Close_Connection; // Closing connection. const static CodeDweller::ThreadState SYNC_Parse_GBUdb_Reflections; // Parsing GBUdb reflections. const static CodeDweller::ThreadState SYNC_Log_Event; // Logging SYNC event. }; } // namespace SNFMulti #endif