|
|
@@ -1,6 +1,6 @@ |
|
|
|
// snfNETmgr.cpp |
|
|
|
// |
|
|
|
// (C) Copyright 2006 - 2009 ARM Research Labs, LLC |
|
|
|
// (C) Copyright 2006 - 2020 ARM Research Labs, LLC |
|
|
|
// See www.armresearch.com for the copyright terms. |
|
|
|
// |
|
|
|
// See snfNETmgr.hpp for details. |
|
|
@@ -31,22 +31,22 @@ unsigned long long int SecsAsMSecs(unsigned int Secs) { |
|
|
|
|
|
|
|
//// snfNETmgr ///////////////////////////////////////////////////////////////// |
|
|
|
|
|
|
|
const ThreadType snfNETmgr::Type("snfNETManager"); // The thread's type. |
|
|
|
|
|
|
|
const ThreadState snfNETmgr::Sleeping("Sleeping"); // Taking a break. |
|
|
|
const ThreadState snfNETmgr::SYNC_Connect("Connecting"); // Connecting to SYNC server. |
|
|
|
const ThreadState snfNETmgr::SYNC_Read_Challenge("Reading challenge"); // Reading challenge. |
|
|
|
const ThreadState snfNETmgr::SYNC_Compute_Response("Computing crypto"); // Computing crypto response. |
|
|
|
const ThreadState snfNETmgr::SYNC_Send_Response("Sending crypto"); // Sending crypto response. |
|
|
|
const ThreadState snfNETmgr::SYNC_Read_Availabilty("Reading Availability"); // Reading rulebase status. |
|
|
|
const ThreadState snfNETmgr::SYNC_Send_GBUdb_Alerts("Sending GBUdb"); // Sending GBUdb alerts. |
|
|
|
const ThreadState snfNETmgr::SYNC_Send_Status_Reports("Sending Status"); // Sending status reports. |
|
|
|
const ThreadState snfNETmgr::SYNC_Send_Samples("Sending Samples"); // Sending message samples. |
|
|
|
const ThreadState snfNETmgr::SYNC_Send_End_Of_Report("Sending End"); // Sending end of client data. |
|
|
|
const ThreadState snfNETmgr::SYNC_Read_Server_Response("Reading Server"); // Reading server data. |
|
|
|
const ThreadState snfNETmgr::SYNC_Close_Connection("Closing Connection"); // Closing connection. |
|
|
|
const ThreadState snfNETmgr::SYNC_Parse_GBUdb_Reflections("Parsing GBUdb"); // Parsing GBUdb reflections. |
|
|
|
const ThreadState snfNETmgr::SYNC_Log_Event("Logging SYNC"); // Logging SYNC event. |
|
|
|
const cd::ThreadType snfNETmgr::Type("snfNETManager"); // The thread's type. |
|
|
|
|
|
|
|
const cd::ThreadState snfNETmgr::Sleeping("Sleeping"); // Taking a break. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Connect("Connecting"); // Connecting to SYNC server. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Read_Challenge("Reading challenge"); // Reading challenge. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Compute_Response("Computing crypto"); // Computing crypto response. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Send_Response("Sending crypto"); // Sending crypto response. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Read_Availabilty("Reading Availability"); // Reading rulebase status. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Send_GBUdb_Alerts("Sending GBUdb"); // Sending GBUdb alerts. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Send_Status_Reports("Sending Status"); // Sending status reports. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Send_Samples("Sending Samples"); // Sending message samples. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Send_End_Of_Report("Sending End"); // Sending end of client data. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Read_Server_Response("Reading Server"); // Reading server data. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Close_Connection("Closing Connection"); // Closing connection. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Parse_GBUdb_Reflections("Parsing GBUdb"); // Parsing GBUdb reflections. |
|
|
|
const cd::ThreadState snfNETmgr::SYNC_Log_Event("Logging SYNC"); // Logging SYNC event. |
|
|
|
|
|
|
|
snfNETmgr::snfNETmgr() : // Starting up the NETmgr |
|
|
|
Thread(snfNETmgr::Type, "NET Manager"), // Network manager and Name. |
|
|
@@ -72,7 +72,7 @@ void snfNETmgr::stop() { |
|
|
|
} |
|
|
|
|
|
|
|
void snfNETmgr::myTask() { // Here's the thread task. |
|
|
|
Sleeper WaitASecond(1000); // Heartbeat timer. |
|
|
|
cd::Sleeper WaitASecond(1000); // Heartbeat timer. |
|
|
|
while(false == isTimeToStop) { // Until it's time to stop, |
|
|
|
CurrentThreadState(Sleeping); // post our status, |
|
|
|
WaitASecond(); // pause for a second, |
|
|
@@ -100,7 +100,7 @@ void snfNETmgr::linkGBUdbmgr(snfGBUdbmgr& G) { |
|
|
|
// CFGData object in order to maintain self-consistency. |
|
|
|
|
|
|
|
void snfNETmgr::configure(snfCFGData& CFGData) { // Update the configuration. |
|
|
|
ScopeMutex CFGDataExchange(ConfigMutex); // Lock the config data during updates. |
|
|
|
cd::ScopeMutex CFGDataExchange(ConfigMutex); // Lock the config data during updates. |
|
|
|
|
|
|
|
// Update the internal config data from CFGData while we are locked. |
|
|
|
// Internal functions which depend on this data will lock the object, |
|
|
@@ -164,10 +164,10 @@ void snfNETmgr::sendSample( |
|
|
|
case Truncate: { IPRange = "Truncate"; break; } // Don't even bother looking. |
|
|
|
} |
|
|
|
|
|
|
|
SocketAddress IP; |
|
|
|
cd::SocketAddress IP; |
|
|
|
IP.setAddress(ScanData.SourceIPRecord().IP); |
|
|
|
|
|
|
|
XML << IPRange << "\' ip=\'" << (string) IP4Address(IP.getAddress()) << "\' t=\'"; |
|
|
|
XML << IPRange << "\' ip=\'" << (std::string) cd::IP4Address(IP.getAddress()) << "\' t=\'"; |
|
|
|
|
|
|
|
string IPType; |
|
|
|
switch(ScanData.SourceIPRecord().GBUdbData.Flag()) { |
|
|
@@ -218,14 +218,14 @@ void snfNETmgr::sendSample( |
|
|
|
|
|
|
|
// Last thing we do is post the formatted string to the buffer. |
|
|
|
const unsigned int SampleSafetyLimit = 100000; // 100 Kbyte limit on samples. |
|
|
|
ScopeMutex DoNotDisturb(myMutex); // Don't bug me man I'm busy. |
|
|
|
cd::ScopeMutex DoNotDisturb(myMutex); // Don't bug me man I'm busy. |
|
|
|
if(SampleSafetyLimit < SamplesBuffer.length()) // If the samples buffer is full |
|
|
|
SamplesBuffer.clear(); // clear it before adding more. |
|
|
|
SamplesBuffer.append(XML.str()); // Append the XML to the buffer. |
|
|
|
} |
|
|
|
|
|
|
|
string snfNETmgr::getSamples() { // Synchronized way to get Samples. |
|
|
|
ScopeMutex DoNotDisturb(myMutex); // Lock the mutex to protect our work. |
|
|
|
cd::ScopeMutex DoNotDisturb(myMutex); // Lock the mutex to protect our work. |
|
|
|
string SamplesBatch = SamplesBuffer; // Copy the samples to a new string. |
|
|
|
SamplesBuffer.clear(); // Clear the samples buffer. |
|
|
|
return SamplesBatch; // Return a batch of Samples. |
|
|
@@ -233,14 +233,14 @@ string snfNETmgr::getSamples() { |
|
|
|
|
|
|
|
void snfNETmgr::sendReport(const string& S) { // How to send a status report. |
|
|
|
const unsigned int ReportSafetyLimit = 100000; // 100 Kbytes limit on reports. |
|
|
|
ScopeMutex DoNotDisturb(myMutex); // Lock the mutex for a moment. |
|
|
|
cd::ScopeMutex DoNotDisturb(myMutex); // Lock the mutex for a moment. |
|
|
|
if(ReportSafetyLimit < ReportsBuffer.length()) // If the reports buffer is full |
|
|
|
ReportsBuffer.clear(); // clear it before adding more. |
|
|
|
ReportsBuffer.append(S); // Append the report. |
|
|
|
} |
|
|
|
|
|
|
|
string snfNETmgr::getReports() { // Synchronized way to get Reports. |
|
|
|
ScopeMutex DoNotDisturb(myMutex); // Lock the mutex to protect our work. |
|
|
|
cd::ScopeMutex DoNotDisturb(myMutex); // Lock the mutex to protect our work. |
|
|
|
string ReportsBatch = ReportsBuffer; // Copy the reports to a new string. |
|
|
|
ReportsBuffer.clear(); // Clear the reports buffer. |
|
|
|
return ReportsBatch; // Return a batch of Reports. |
|
|
@@ -277,7 +277,7 @@ string& snfNETmgr::RulebaseUTC(string& t) { |
|
|
|
} |
|
|
|
|
|
|
|
unsigned long snfNETmgr::ResolveHostIPFromName(const string& N) { // Host name resolution tool. |
|
|
|
ScopeMutex OneAtATimePlease(ResolverMutex); // Resolve only one at a time. |
|
|
|
cd::ScopeMutex OneAtATimePlease(ResolverMutex); // Resolve only one at a time. |
|
|
|
unsigned long IP = inet_addr(N.c_str()); // See if it's an IP. |
|
|
|
if (INADDR_NONE == IP) { // If it's not an IP resolve it. |
|
|
|
hostent* H = gethostbyname(N.c_str()); // Resolve the host. |
|
|
@@ -302,14 +302,14 @@ unsigned long snfNETmgr::ResolveHostIPFromName(const string& N) { |
|
|
|
// platform solution that depends only on our own code ;-) |
|
|
|
|
|
|
|
void snfNETmgr::evolvePad(string Entropy) { // Add entropy and evolve. |
|
|
|
ScopeMutex OneAtATimePlease(PadMutex); // Protect the one time pad. |
|
|
|
cd::ScopeMutex OneAtATimePlease(PadMutex); // Protect the one time pad. |
|
|
|
myLOGmgr->Timestamp(Entropy); // Time matters ;-) |
|
|
|
for(unsigned int a = 0; a < Entropy.length(); a++) { // Add the entropy to our generator. |
|
|
|
PadGenerator.Encrypt(Entropy.at(a)); |
|
|
|
} |
|
|
|
msclock rt = myLOGmgr->RunningTime(); // Get the elapsed running time so far. |
|
|
|
cd::msclock rt = myLOGmgr->RunningTime(); // Get the elapsed running time so far. |
|
|
|
unsigned char* rtb = reinterpret_cast<unsigned char*>(&rt); // Convert that long long into bytes. |
|
|
|
for(unsigned int a = 0; a < sizeof(msclock); a++) { // Encrypt those bytes one by one |
|
|
|
for(unsigned int a = 0; a < sizeof(cd::msclock); a++) { // Encrypt those bytes one by one |
|
|
|
PadGenerator.Encrypt(rtb[a]); // to add more entropy. |
|
|
|
} |
|
|
|
} |
|
|
@@ -368,8 +368,8 @@ void snfNETmgr::postUpdateTrigger(string& updateUTC) { |
|
|
|
// Utility to read a line from a non-blocking TCPHost & check the timeout. |
|
|
|
|
|
|
|
const unsigned int MaxReadLineLength = 1024; // How long a line can be. |
|
|
|
string readLineTimeout(TCPHost& S, Timeout& T) { // Read a line from S until T. |
|
|
|
Sleeper WaitForMoreData(50); // How long to wait when no data. |
|
|
|
string readLineTimeout(cd::TCPHost& S, cd::Timeout& T) { // Read a line from S until T. |
|
|
|
cd::Sleeper WaitForMoreData(50); // How long to wait when no data. |
|
|
|
string LineBuffer = ""; // Buffer for the line. |
|
|
|
while( // Keep going as long as: |
|
|
|
false == T.isExpired() && // our timeout has not expired AND |
|
|
@@ -397,8 +397,8 @@ string readLineTimeout(TCPHost& S, Timeout& T) { |
|
|
|
// delay between chunks to give the channel more time. |
|
|
|
|
|
|
|
const int MaxSendChunkSize = 512; // Size of one chunk in a write. |
|
|
|
void sendDataTimeout(TCPHost& S, Timeout& T, char* Bfr, int Len) { // Send and keep track of time. |
|
|
|
Sleeper WaitForMoreRoom(15); // Wait to send more data. |
|
|
|
void sendDataTimeout(cd::TCPHost& S, cd::Timeout& T, char* Bfr, int Len) { // Send and keep track of time. |
|
|
|
cd::Sleeper WaitForMoreRoom(15); // Wait to send more data. |
|
|
|
int Remaining = Len; // This is how much we have left. |
|
|
|
while( // For as long as: |
|
|
|
false == T.isExpired() && // We still have time left AND |
|
|
@@ -413,7 +413,7 @@ void sendDataTimeout(TCPHost& S, Timeout& T, char* Bfr, int Len) { |
|
|
|
} // the pause before the next chunk. |
|
|
|
} |
|
|
|
|
|
|
|
void sendDataTimeout(TCPHost& S, Timeout& T, string& D) { // Send a string and keep track |
|
|
|
void sendDataTimeout(cd::TCPHost& S, cd::Timeout& T, std::string& D) { // Send a string and keep track |
|
|
|
sendDataTimeout(S, T, const_cast<char*>(D.c_str()), D.length()); // of time. (Polymorphism is fun) |
|
|
|
} |
|
|
|
|
|
|
@@ -430,7 +430,7 @@ void snfNETmgr::sync() { |
|
|
|
|
|
|
|
if(!isConfigured) return; // If we're not configured, don't! |
|
|
|
else { |
|
|
|
ScopeMutex GettingConfig(ConfigMutex); // Temporarily lock our config. |
|
|
|
cd::ScopeMutex GettingConfig(ConfigMutex); // Temporarily lock our config. |
|
|
|
HostName = SyncHostName; // We will connect to this host. |
|
|
|
HostPort = SyncHostPort; // We will connect to this port. |
|
|
|
Secret = SecurityKey; // Get the security key. |
|
|
@@ -452,19 +452,19 @@ void snfNETmgr::sync() { |
|
|
|
// if(SessionDog.isExpired()) throw SyncFailed("Out Of Time"); |
|
|
|
|
|
|
|
const int SyncSessionTimeout = 2 * SYNCTimer.getDuration(); // Timeout is twice poll time. |
|
|
|
Timeout SessionDog(SyncSessionTimeout); // Give this long for a session. |
|
|
|
cd::Timeout SessionDog(SyncSessionTimeout); // Give this long for a session. |
|
|
|
|
|
|
|
// Connect to the sync host. |
|
|
|
|
|
|
|
CurrentThreadState(SYNC_Connect); |
|
|
|
|
|
|
|
SocketAddress SyncHostAddress; // We'll need an address. |
|
|
|
cd::SocketAddress SyncHostAddress; // We'll need an address. |
|
|
|
SyncHostAddress.setPort(HostPort); // Set the port. |
|
|
|
SyncHostAddress.setAddress(ResolveHostIPFromName(HostName)); // Resolve and set the IP. |
|
|
|
TCPHost SyncServer(SyncHostAddress); // Set up a host connection. |
|
|
|
cd::TCPHost SyncServer(SyncHostAddress); // Set up a host connection. |
|
|
|
SyncServer.makeNonBlocking(); // Make the connection non-blocking. |
|
|
|
|
|
|
|
PollTimer WaitForOpen(10, 340); // Expand 10ms to 340ms between tries. |
|
|
|
cd::PollTimer WaitForOpen(10, 340); // Expand 10ms to 340ms between tries. |
|
|
|
while(!SessionDog.isExpired()) { // Wait & Watch for a good connection. |
|
|
|
try { SyncServer.open(); } // Try opening the connection. |
|
|
|
catch(exception& e) { // If we get an exception then |
|
|
@@ -505,7 +505,7 @@ void snfNETmgr::sync() { |
|
|
|
|
|
|
|
//--- Prepare the secret. |
|
|
|
|
|
|
|
MANGLER ResponseGenerator; // Grab a mangler. |
|
|
|
cd::MANGLER ResponseGenerator; // Grab a mangler. |
|
|
|
for(unsigned int i = 0; i < Secret.length(); i++) // Fill it with the |
|
|
|
ResponseGenerator.Encrypt(Secret.at(i)); // security key. |
|
|
|
|