|
|
|
|
|
|
|
|
// #include "tcp_watchdog.hpp" No longer using TCPWatchdog -- see below _M |
|
|
// #include "tcp_watchdog.hpp" No longer using TCPWatchdog -- see below _M |
|
|
|
|
|
|
|
|
using namespace std; |
|
|
using namespace std; |
|
|
|
|
|
|
|
|
|
|
|
///// utilities ////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
const int MSecsInSecs = 1000; // Multiplier - seconds to milliseconds.
|
|
|
|
|
|
unsigned long long int SecsAsMSecs(unsigned int Secs) {
|
|
|
|
|
|
return (MSecsInSecs * Secs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//// snfNETmgr ///////////////////////////////////////////////////////////////// |
|
|
//// snfNETmgr ///////////////////////////////////////////////////////////////// |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
snfNETmgr::snfNETmgr() : // Starting up the NETmgr |
|
|
snfNETmgr::snfNETmgr() : // Starting up the NETmgr |
|
|
Thread(snfNETmgr::Type, "NET Manager"), // Network manager and Name. |
|
|
Thread(snfNETmgr::Type, "NET Manager"), // Network manager and Name. |
|
|
SYNCTimer(30000), // Sync every 30 secs by default. |
|
|
|
|
|
SyncSecsOverride(-1), // Override is -1 by default. |
|
|
|
|
|
myLOGmgr(NULL), |
|
|
|
|
|
isTimeToStop(false), |
|
|
|
|
|
isConfigured(false) { // On construction, NETmgr |
|
|
|
|
|
run(); // runs it's thread. |
|
|
|
|
|
|
|
|
myLOGmgr(NULL), // No LOGmgr yet.
|
|
|
|
|
|
isTimeToStop(false), // Not time to stop yet. |
|
|
|
|
|
isConfigured(false), // Not configured yet. |
|
|
|
|
|
SYNCTimer(30000), // Sync every 30 secs by default.
|
|
|
|
|
|
SyncSecsOverride(-1) { // Override is -1 (off) by default.
|
|
|
|
|
|
run(); // Run the thread. |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
snfNETmgr::~snfNETmgr() { // On descruction, NETmgr must |
|
|
snfNETmgr::~snfNETmgr() { // On descruction, NETmgr must |
|
|
|
|
|
|
|
|
HandshakeFilePath = CFGData.paths_workspace_path + ".handshake"; // Where we store our handshake. |
|
|
HandshakeFilePath = CFGData.paths_workspace_path + ".handshake"; // Where we store our handshake. |
|
|
UpdateReadyFilePath = CFGData.paths_workspace_path + "UpdateReady.txt"; // Where we put update trigger files. |
|
|
UpdateReadyFilePath = CFGData.paths_workspace_path + "UpdateReady.txt"; // Where we put update trigger files. |
|
|
|
|
|
|
|
|
const int SecsAsms = 1000; // Multiplier - seconds to milliseconds. |
|
|
|
|
|
|
|
|
|
|
|
SyncSecsConfigured = CFGData.network_sync_secs; // Capture the configured sync time. |
|
|
SyncSecsConfigured = CFGData.network_sync_secs; // Capture the configured sync time. |
|
|
|
|
|
|
|
|
if(0 > SyncSecsOverride) { // If the sync timer isn't in override, |
|
|
if(0 > SyncSecsOverride) { // If the sync timer isn't in override, |
|
|
if(SYNCTimer.getDuration() != (SyncSecsConfigured * SecsAsms)) { // And the config time is different than |
|
|
|
|
|
SYNCTimer.setDuration(SyncSecsConfigured * SecsAsms); // the timer's current setting then set |
|
|
|
|
|
|
|
|
if(SYNCTimer.getDuration() != SecsAsMSecs(SyncSecsConfigured)) { // And the config time is different than |
|
|
|
|
|
SYNCTimer.setDuration(SecsAsMSecs(SyncSecsConfigured)); // the timer's current setting then set |
|
|
} // the timer to the new value. |
|
|
} // the timer to the new value. |
|
|
} // If we are in override, timer is set. |
|
|
} // If we are in override, timer is set. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XML << "</sample>" << endl; |
|
|
XML << "</sample>" << endl; |
|
|
|
|
|
|
|
|
// Last thing we do is post the formatted string to the buffer. |
|
|
// Last thing we do is post the formatted string to the buffer. |
|
|
const int SampleSafetyLimit = 100000; // 100 Kbyte limit on samples. |
|
|
|
|
|
|
|
|
const unsigned int SampleSafetyLimit = 100000; // 100 Kbyte limit on samples. |
|
|
ScopeMutex DoNotDisturb(myMutex); // Don't bug me man I'm busy. |
|
|
ScopeMutex DoNotDisturb(myMutex); // Don't bug me man I'm busy. |
|
|
if(SampleSafetyLimit < SamplesBuffer.length()) // If the samples buffer is full |
|
|
if(SampleSafetyLimit < SamplesBuffer.length()) // If the samples buffer is full |
|
|
SamplesBuffer.clear(); // clear it before adding more. |
|
|
SamplesBuffer.clear(); // clear it before adding more. |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void snfNETmgr::sendReport(const string& S) { // How to send a status report. |
|
|
void snfNETmgr::sendReport(const string& S) { // How to send a status report. |
|
|
const int ReportSafetyLimit = 100000; // 100 Kbytes limit on reports. |
|
|
|
|
|
|
|
|
const unsigned int ReportSafetyLimit = 100000; // 100 Kbytes limit on reports. |
|
|
ScopeMutex DoNotDisturb(myMutex); // Lock the mutex for a moment. |
|
|
ScopeMutex DoNotDisturb(myMutex); // Lock the mutex for a moment. |
|
|
if(ReportSafetyLimit < ReportsBuffer.length()) // If the reports buffer is full |
|
|
if(ReportSafetyLimit < ReportsBuffer.length()) // If the reports buffer is full |
|
|
ReportsBuffer.clear(); // clear it before adding more. |
|
|
ReportsBuffer.clear(); // clear it before adding more. |
|
|
|
|
|
|
|
|
struct tm RulebaseTime; // Allocate a time structure. |
|
|
struct tm RulebaseTime; // Allocate a time structure. |
|
|
RulebaseTime = *(gmtime(&RulebaseStat.st_mtime)); // Copy the file time to it as UTC. |
|
|
RulebaseTime = *(gmtime(&RulebaseStat.st_mtime)); // Copy the file time to it as UTC. |
|
|
|
|
|
|
|
|
char TimestampBfr[20]; // Timestamp buffer. |
|
|
|
|
|
|
|
|
char TimestampBfr[20]; // Timestamp buffer. |
|
|
|
|
|
|
|
|
sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d\0", // Format yyyymmddhhmmss |
|
|
|
|
|
|
|
|
sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d", // Format yyyymmddhhmmss |
|
|
RulebaseTime.tm_year+1900, |
|
|
RulebaseTime.tm_year+1900, |
|
|
RulebaseTime.tm_mon+1, |
|
|
RulebaseTime.tm_mon+1, |
|
|
RulebaseTime.tm_mday, |
|
|
RulebaseTime.tm_mday, |
|
|
|
|
|
|
|
|
void snfNETmgr::evolvePad(string Entropy) { // Add entropy and evolve. |
|
|
void snfNETmgr::evolvePad(string Entropy) { // Add entropy and evolve. |
|
|
ScopeMutex OneAtATimePlease(PadMutex); // Protect the one time pad. |
|
|
ScopeMutex OneAtATimePlease(PadMutex); // Protect the one time pad. |
|
|
myLOGmgr->Timestamp(Entropy); // Time matters ;-) |
|
|
myLOGmgr->Timestamp(Entropy); // Time matters ;-) |
|
|
int x; // We want to capture this. |
|
|
|
|
|
for(int a = 0; a < Entropy.length(); a++) { // Add the entropy to our generator. |
|
|
|
|
|
x = PadGenerator.Encrypt(Entropy.at(a)); |
|
|
|
|
|
|
|
|
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. |
|
|
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. |
|
|
unsigned char* rtb = reinterpret_cast<unsigned char*>(&rt); // Convert that long long into bytes. |
|
|
for(int a = 0; a < sizeof(msclock); a++) { // Encrypt those bytes one by one |
|
|
|
|
|
|
|
|
for(unsigned int a = 0; a < sizeof(msclock); a++) { // Encrypt those bytes one by one |
|
|
PadGenerator.Encrypt(rtb[a]); // to add more entropy. |
|
|
PadGenerator.Encrypt(rtb[a]); // to add more entropy. |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Utility to read a line from a non-blocking TCPHost & check the timeout. |
|
|
// Utility to read a line from a non-blocking TCPHost & check the timeout. |
|
|
|
|
|
|
|
|
const int MaxReadLineLength = 1024; // How long a line can be. |
|
|
|
|
|
|
|
|
const unsigned int MaxReadLineLength = 1024; // How long a line can be. |
|
|
string readLineTimeout(TCPHost& S, Timeout& T) { // Read a line from S until T. |
|
|
string readLineTimeout(TCPHost& S, Timeout& T) { // Read a line from S until T. |
|
|
Sleeper WaitForMoreData(50); // How long to wait when no data. |
|
|
Sleeper WaitForMoreData(50); // How long to wait when no data. |
|
|
string LineBuffer = ""; // Buffer for the line. |
|
|
string LineBuffer = ""; // Buffer for the line. |
|
|
|
|
|
|
|
|
//--- Prepare the secret. |
|
|
//--- Prepare the secret. |
|
|
|
|
|
|
|
|
MANGLER ResponseGenerator; // Grab a mangler. |
|
|
MANGLER ResponseGenerator; // Grab a mangler. |
|
|
for(int i = 0; i < Secret.length(); i++) // Fill it with the |
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < Secret.length(); i++) // Fill it with the |
|
|
ResponseGenerator.Encrypt(Secret.at(i)); // security key. |
|
|
ResponseGenerator.Encrypt(Secret.at(i)); // security key. |
|
|
|
|
|
|
|
|
const int ManglerKeyExpansionCount = 1024; // Loop this many to randomize. |
|
|
const int ManglerKeyExpansionCount = 1024; // Loop this many to randomize. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//--- Absorb the challenge. |
|
|
//--- Absorb the challenge. |
|
|
|
|
|
|
|
|
for(int i = 0; i < DecodedChallenge.size(); i++) // Evolve through the challenge. |
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < DecodedChallenge.size(); i++) // Evolve through the challenge. |
|
|
ResponseGenerator.Encrypt(DecodedChallenge.at(i)); |
|
|
ResponseGenerator.Encrypt(DecodedChallenge.at(i)); |
|
|
|
|
|
|
|
|
/*** We now have half of the key for this session ***/ |
|
|
/*** We now have half of the key for this session ***/ |
|
|
|
|
|
|
|
|
PadBuffer NewPad = OneTimePad(); // Grab a new Pad (default size). |
|
|
PadBuffer NewPad = OneTimePad(); // Grab a new Pad (default size). |
|
|
|
|
|
|
|
|
base64buffer ResponseBin; // With the key now established, |
|
|
base64buffer ResponseBin; // With the key now established, |
|
|
for(int i = 0; i < NewPad.size(); i++) // encrypt the one time pad for |
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < NewPad.size(); i++) // encrypt the one time pad for |
|
|
ResponseBin.push_back( // transfer. |
|
|
ResponseBin.push_back( // transfer. |
|
|
ResponseGenerator.Encrypt(NewPad[i])); |
|
|
ResponseGenerator.Encrypt(NewPad[i])); |
|
|
|
|
|
|
|
|
//--- Encrypt our Handshake. |
|
|
//--- Encrypt our Handshake. |
|
|
|
|
|
|
|
|
PadBuffer CurrentHandshake = Handshake(); // Recall the secret handshake. |
|
|
PadBuffer CurrentHandshake = Handshake(); // Recall the secret handshake. |
|
|
for(int i = 0; i < CurrentHandshake.size(); i++) // Encrypt that into the stream. |
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < CurrentHandshake.size(); i++) // Encrypt that into the stream. |
|
|
ResponseBin.push_back( |
|
|
ResponseBin.push_back( |
|
|
ResponseGenerator.Encrypt(CurrentHandshake[i])); |
|
|
ResponseGenerator.Encrypt(CurrentHandshake[i])); |
|
|
|
|
|
|
|
|
//--- Encrypt our Signature. |
|
|
//--- Encrypt our Signature. |
|
|
|
|
|
|
|
|
for(int x = 0, i = 0; i < SNFSignatureSize; i++) // Generate a hash by having Mangler |
|
|
|
|
|
|
|
|
for(unsigned int x = 0, i = 0; i < SNFSignatureSize; i++) // Generate a hash by having Mangler |
|
|
ResponseBin.push_back( // chase it's tail for the appropriate |
|
|
ResponseBin.push_back( // chase it's tail for the appropriate |
|
|
x = ResponseGenerator.Encrypt(x)); // number of bytes. |
|
|
x = ResponseGenerator.Encrypt(x)); // number of bytes. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = ResponseGenerator.Encrypt(x); // have Mangler chase it's tail. |
|
|
x = ResponseGenerator.Encrypt(x); // have Mangler chase it's tail. |
|
|
|
|
|
|
|
|
PadBuffer NewHandshake; // Grab a new handshake buffer. |
|
|
PadBuffer NewHandshake; // Grab a new handshake buffer. |
|
|
for(int x = 0, i = 0; i < SNFHandshakeSize; i++) // Create the new handshake as a |
|
|
|
|
|
|
|
|
for(unsigned int x = 0, i = 0; i < SNFHandshakeSize; i++) // Create the new handshake as a |
|
|
NewHandshake.push_back( // mangler hash of the current |
|
|
NewHandshake.push_back( // mangler hash of the current |
|
|
x = ResponseGenerator.Encrypt(x)); // key state (proper length of course). |
|
|
x = ResponseGenerator.Encrypt(x)); // key state (proper length of course). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int SecsAsms = 1000; // Multiplier - seconds to milliseconds. |
|
|
const int SecsAsms = 1000; // Multiplier - seconds to milliseconds. |
|
|
|
|
|
|
|
|
if(0 > SyncSecsOverride) { // If the sync timer IS NOT in override, |
|
|
if(0 > SyncSecsOverride) { // If the sync timer IS NOT in override, |
|
|
if(SYNCTimer.getDuration() != (SyncSecsConfigured * SecsAsms)) { // And the config time is different than |
|
|
|
|
|
|
|
|
if(SYNCTimer.getDuration() != SecsAsMSecs(SyncSecsConfigured)) { // And the config time is different than |
|
|
SYNCTimer.setDuration(SyncSecsConfigured * SecsAsms); // the timer's current setting then set |
|
|
SYNCTimer.setDuration(SyncSecsConfigured * SecsAsms); // the timer's current setting then set |
|
|
} // the timer to the new value. |
|
|
} // the timer to the new value. |
|
|
} else { // If the sync timer IS in override now, |
|
|
} else { // If the sync timer IS in override now, |
|
|
if(SYNCTimer.getDuration() != (SyncSecsOverride * SecsAsms)) { // and the override is different than the |
|
|
|
|
|
SYNCTimer.setDuration(SyncSecsOverride * SecsAsms); // current setting then override the setting |
|
|
|
|
|
|
|
|
if(SYNCTimer.getDuration() != SecsAsMSecs(SyncSecsOverride)) { // and the override is different than the |
|
|
|
|
|
SYNCTimer.setDuration(SecsAsMSecs(SyncSecsOverride)); // current setting then override the setting |
|
|
} // with the new value. |
|
|
} // with the new value. |
|
|
} |
|
|
} |
|
|
|
|
|
|