Browse Source

Code cleanup and up to latest libs

master
Pete McNeil 4 years ago
parent
commit
b9096f26eb
5 changed files with 125 additions and 113 deletions
  1. 1
    1
      CodeDweller
  2. 115
    107
      SNFClient/main.cpp
  3. 1
    1
      SNFMulti
  4. 1
    2
      makefile
  5. 7
    2
      readme.md

+ 1
- 1
CodeDweller

@@ -1 +1 @@
Subproject commit 57459a5d9aea2a42de064a05998d5f48f7614220
Subproject commit 2c3aafcc16cbc2d9ea121b0b9c8851b0337c9168

+ 115
- 107
SNFClient/main.cpp View File

@@ -29,9 +29,9 @@
#include "../CodeDweller/threading.hpp"
#include "../SNFMulti/snf_xci.hpp"
using namespace std; // Introduce standard namespace.
namespace cd = codedweller;
const char* VERSION_INFO = "SNF Client Version 3.1 Build: " __DATE__ " " __TIME__;
const char* VERSION_INFO = "SNF Client Version 3.2 Build: " __DATE__ " " __TIME__;
time_t Timestamp() { // Get an ordinary timestamp.
time_t rawtime; // Grab raw time from
@@ -39,11 +39,14 @@ time_t Timestamp() {
return rawtime;
}
string Timestamp(time_t t) { // Convert time_t to a timestamp.
char TimestampBfr[20]; // Create a small buffer.
std::string Timestamp(time_t t) { // Convert time_t to a timestamp.
char TimestampBfr[16]; // Create a small buffer.
tm* gmt; // Get a ptr to a tm structure.
gmt = gmtime(&t); // Fill it with UTC.
sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d", // Format yyyymmddhhmmss
size_t l = snprintf(
TimestampBfr,
sizeof(TimestampBfr),
"%04d%02d%02d%02d%02d%02d", // Format yyyymmddhhmmss
gmt->tm_year+1900,
gmt->tm_mon+1,
gmt->tm_mday,
@@ -51,23 +54,25 @@ string Timestamp(time_t t) {
gmt->tm_min,
gmt->tm_sec
);
return string(TimestampBfr); // Return a string.
const size_t TimestampLength = 4+2+2+2+2+2;
if(TimestampLength == l) return std::string(TimestampBfr); // Return a string.
else return std::string("00000000000000");
}
string ErrorMessage(int argc, char** argv, string ErrorText) { // Create an error message.
std::string ErrorMessage(int argc, char** argv, std::string ErrorText) { // Create an error message.
ostringstream FailurePreamble; // Setup a stringstream for formatting.
std::ostringstream FailurePreamble; // Setup a stringstream for formatting.
string TimestampString = Timestamp(Timestamp()); // Get the UTC timestamp.
std::string TimestampString = Timestamp(Timestamp()); // Get the UTC timestamp.
FailurePreamble << TimestampString << ", "; // Start with the timestamp and
for(int a = 1; a < argc; a++) { // then roll in all of the command
FailurePreamble << "arg" << a << "=" << argv[a]; // line parameters. Follow that with
FailurePreamble << "arg" << a << "=" << argv[a]; // line parameters. Follow that with
if(a < argc-1) { FailurePreamble << ", "; } // a colon and the error itself.
else { FailurePreamble << " : "; }
}
FailurePreamble << ErrorText << endl; // Tack on the error text.
FailurePreamble << ErrorText << std::endl; // Tack on the error text.
return FailurePreamble.str(); // Return the completed string.
}
@@ -77,17 +82,17 @@ int main(int argc, char* argv[]) {
// Figure out what we're going to do and create a suitable RequestString.
bool DebugMode = false; // This will be our debug mode.
string argv0(argv[0]); // Capture how we were called.
std::string argv0(argv[0]); // Capture how we were called.
if(
string::npos != argv0.find("Debug") || // If we find "Debug" or
string::npos != argv0.find("debug") // "debug" in our command path
std::string::npos != argv0.find("Debug") || // If we find "Debug" or
std::string::npos != argv0.find("debug") // "debug" in our command path
) { // then we are in DebugMode.
DebugMode = true; // Set the flag and tell the
cout << "Debug Mode" << endl; // watchers.
std::cout << "Debug Mode" << std::endl; // watchers.
}
string FileToScan; // What will we be scanning?
string RequestString; // What will we send to the server?
std::string FileToScan; // What will we be scanning?
std::string RequestString; // What will we send to the server?
bool ItIsACommand = false; // Is it a command?
bool ItIsAScan = false; // Is it a scan?
bool GetXHDRs = false; // GetXHDRs with scan?
@@ -121,9 +126,9 @@ int main(int argc, char* argv[]) {
} else
if(0 == FileToScan.find("-shutdown")) { // If argv1 is -shutdown:
ItIsACommand = true; // Set the command flag.
if(DebugMode) { cout << "Command: shutdown" << endl; } // In debug - tell what we are doing.
if(0 == FileToScan.find("-shutdown")) { // If argv1 is -shutdown:
ItIsACommand = true; // Set the command flag.
if(DebugMode) { std::cout << "Command: shutdown" << std::endl; } // In debug - tell what we are doing.
// Format the request.
@@ -134,9 +139,9 @@ int main(int argc, char* argv[]) {
if('-' == FileToScan.at(0)) { // If argv1 still looks like -something
goto BadCommandLine; // Complain and show the help text.
} else { // If it does not then it is a file.
ItIsAScan = true; // Set the scan flag.
if(DebugMode) { cout << "Scan: " << FileToScan << endl; } // In debug - tell what we are doing.
} else { // If it does not then it is a file.
ItIsAScan = true; // Set the scan flag.
if(DebugMode) { std::cout << "Scan: " << FileToScan << std::endl; } // In debug - tell what we are doing.
// Format the request.
@@ -149,7 +154,7 @@ int main(int argc, char* argv[]) {
}
case 3: { // Special Scan Mode
string CommandString = argv[1]; // -command
std::string CommandString = argv[1]; // -command
FileToScan = argv[2]; // What file to scan?
// XHeader Scan? ---------------------------------------------------
@@ -158,7 +163,7 @@ int main(int argc, char* argv[]) {
ItIsAScan = true; // Set the scan flag.
GetXHDRs = true; // Turn on XHDRs for the scan.
if(DebugMode) {
cout << "(xhdr)Scan: " << FileToScan << endl; // In debug - tell what we are doing.
std::cout << "(xhdr)Scan: " << FileToScan << std::endl; // In debug - tell what we are doing.
}
// Format the request.
@@ -175,11 +180,11 @@ int main(int argc, char* argv[]) {
ItIsAScan = true; // Set the scan flag.
const int SourceIPIndex = CommandString.find("=") + 1; // Find source after "-source="
IP4Address SourceIP = CommandString.substr(SourceIPIndex); // Extract the source IP.
cd::IP4Address SourceIP = CommandString.substr(SourceIPIndex); // Extract the source IP.
if(DebugMode) {
cout << "([" << (string) SourceIP // In debug - tell what we are doing.
<< "])Scan: " << FileToScan << endl;
std::cout << "([" << (std::string) SourceIP // In debug - tell what we are doing.
<< "])Scan: " << FileToScan << std::endl;
}
// Format the request.
@@ -187,7 +192,7 @@ int main(int argc, char* argv[]) {
RequestString = "<snf><xci><scanner><scan file=\'";
RequestString.append(FileToScan);
RequestString.append("\' ip=\'");
RequestString.append((string) SourceIP);
RequestString.append((std::string) SourceIP);
RequestString.append("\'/></scanner></xci></snf>\n");
} else
@@ -250,7 +255,7 @@ int main(int argc, char* argv[]) {
ItIsAScan = true; // Set the scan flag.
FileToScan = argv[2]; // Grab the message_file_name for
if(DebugMode) { // a compatability scan and if
cout << "(compat)Scan: " << FileToScan << endl; // debugging then announce it.
std::cout << "(compat)Scan: " << FileToScan << std::endl; // debugging then announce it.
}
// Format the request.
@@ -268,14 +273,14 @@ int main(int argc, char* argv[]) {
case 4: {
string Command1String = argv[1]; // -command1
string Command2String = argv[2]; // -command2
std::string Command1String = argv[1]; // -command1
std::string Command2String = argv[2]; // -command2
FileToScan = argv[3]; // What file to scan?
// XHeader Scan W/ Source IP? --------------------------------------
if(0 == Command1String.find("-source=")) { // If things are reversed
string tmp = Command2String; // swap them to the order we
std::string tmp = Command2String; // swap them to the order we
Command2String = Command1String; // are expecting. If we're wrong
Command1String = tmp; // then that case will be handled
} // next step.
@@ -288,11 +293,11 @@ int main(int argc, char* argv[]) {
GetXHDRs = true; // Turn on XHDRs for the scan.
const int SourceIPIndex = Command2String.find("=") + 1; // Find source after "-source="
IP4Address SourceIP = Command2String.substr(SourceIPIndex); // Extract the source IP.
cd::IP4Address SourceIP = Command2String.substr(SourceIPIndex); // Extract the source IP.
if(DebugMode) {
cout << "(xhdr [" << (string) SourceIP // In debug - tell what we are doing.
<< "])Scan: " << FileToScan << endl;
std::cout << "(xhdr [" << (std::string) SourceIP // In debug - tell what we are doing.
<< "])Scan: " << FileToScan << std::endl;
}
// Format the request.
@@ -300,7 +305,7 @@ int main(int argc, char* argv[]) {
RequestString = "<snf><xci><scanner><scan file=\'";
RequestString.append(FileToScan);
RequestString.append("\' xhdr=\'yes\' ip=\'");
RequestString.append((string) SourceIP);
RequestString.append((std::string) SourceIP);
RequestString.append("\'/></scanner></xci></snf>\n");
} else
@@ -311,7 +316,7 @@ int main(int argc, char* argv[]) {
}
case 6: { // GBUdb set mode.
string CommandString = argv[1]; // -command
std::string CommandString = argv[1]; // -command
// GBUdb set? ------------------------------------------------------
@@ -366,35 +371,35 @@ int main(int argc, char* argv[]) {
default: { // If we don't know: show help.
BadCommandLine: // Shortcut for others.
cout
<< endl
<< VERSION_INFO << endl
<< endl
<< "Help:" << endl
<< endl
<< " To scan a message file use: " << endl
<< " SNFClient.exe [-xhdr] [-source=<IP4Address>] <FileNameToScan>" << endl
<< " or: SNFClient.exe <Authenticationxx> <FileNameToScan>" << endl
<< endl
<< " To test an IP with GBUdb use: " << endl
<< " SNFClient.exe -test <IP4Address>" << endl
<< endl
<< " To update GBUdb records use: " << endl
<< " SNFClient.exe -set <IP4Address> <flag> <bad> <good>" << endl
<< " or: SNFClient.exe -drop <IP4Address>" << endl
<< " or: SNFClient.exe -good <IP4Address>" << endl
<< " or: SNFClient.exe -bad <IP4Address>" << endl
<< endl
<< " To check SNFServer status use: " << endl
<< " SNFClient.exe -status.second" << endl
<< " or: SNFClient.exe -status.minute" << endl
<< " or: SNFClient.exe -status.hour" << endl
<< endl
<< " To shut down the SNFServer use: " << endl
<< " SNFClient.exe -shutdown" << endl
<< endl
<< " For more information see www.armresearch.com" << endl
<< " (C) 2007-2019 Arm Research Labs, LLC." << endl;
std::cout
<< std::endl
<< VERSION_INFO << std::endl
<< std::endl
<< "Help:" << std::endl
<< std::endl
<< " To scan a message file use: " << std::endl
<< " SNFClient.exe [-xhdr] [-source=<IP4Address>] <FileNameToScan>" << std::endl
<< " or: SNFClient.exe <Authenticationxx> <FileNameToScan>" << std::endl
<< std::endl
<< " To test an IP with GBUdb use: " << std::endl
<< " SNFClient.exe -test <IP4Address>" << std::endl
<< std::endl
<< " To update GBUdb records use: " << std::endl
<< " SNFClient.exe -set <IP4Address> <flag> <bad> <good>" << std::endl
<< " or: SNFClient.exe -drop <IP4Address>" << std::endl
<< " or: SNFClient.exe -good <IP4Address>" << std::endl
<< " or: SNFClient.exe -bad <IP4Address>" << std::endl
<< std::endl
<< " To check SNFServer status use: " << std::endl
<< " SNFClient.exe -status.second" << std::endl
<< " or: SNFClient.exe -status.minute" << std::endl
<< " or: SNFClient.exe -status.hour" << std::endl
<< std::endl
<< " To shut down the SNFServer use: " << std::endl
<< " SNFClient.exe -shutdown" << std::endl
<< std::endl
<< " For more information see www.armresearch.com" << std::endl
<< " (C) 2007-2020 Arm Research Labs, LLC." << std::endl;
return 0; // Return 0 on help.
}
@@ -402,11 +407,11 @@ int main(int argc, char* argv[]) {
// If we're in debug mode this is a good time to emit our request string
if(DebugMode) { cout << RequestString << endl; } // If debugging, show our request.
if(DebugMode) { std::cout << RequestString << std::endl; } // If debugging, show our request.
// Since we're gonna do this -- prepare for an error
string ERRFname; // The default error log name will
std::string ERRFname; // The default error log name will
ERRFname = argv[0]; // be the program file name with
ERRFname.append(".err"); // .err tagged on the end.
const int FailSafeResult = 0; // Fail Safe result code.
@@ -414,7 +419,7 @@ int main(int argc, char* argv[]) {
// Connect to the server and get the result...
string ResultString; // We need our result string.
std::string ResultString; // We need our result string.
bool ConnectSuccess = false; // We need our success flag.
// Max time in this loop should be (100*50ms) = 5 seconds per try times
@@ -425,13 +430,13 @@ int main(int argc, char* argv[]) {
memset(ResultBuffer, 0, sizeof(ResultBuffer)); // Set the entire thing to nulls.
const int Tries = 20; // How many times to try this.
Sleeper SleepAfterAttempt(100); // How long to sleep between attempts.
cd::Sleeper SleepAfterAttempt(100); // How long to sleep between attempts.
const int OpenTries = 90; // How many tries at opening.
Sleeper WaitForOpen(10); // How long to wait for an open cycle.
cd::Sleeper WaitForOpen(10); // How long to wait for an open cycle.
const int ReadTries = 900; // How many tries at reading.
Sleeper SleepBeforeReading(10); // How long to pause before reading.
cd::Sleeper SleepBeforeReading(10); // How long to pause before reading.
/*
** 20 * 100ms = 2 seconds for all tries.
@@ -445,7 +450,7 @@ int main(int argc, char* argv[]) {
for(int tryagain = Tries; (0<tryagain) && (!ConnectSuccess); tryagain--) { // Try a few times to get this done.
try {
ResultString = ""; // Clear our result string.
TCPHost SNFServer(9001); // Create connection to server.
cd::TCPHost SNFServer(9001); // Create connection to server.
SNFServer.makeNonBlocking(); // Make it non-blocking.
for(int tries = OpenTries; 0 < tries; tries--) { // Wait & Watch for a good connection.
@@ -464,7 +469,7 @@ int main(int argc, char* argv[]) {
memset(ResultBuffer, 0, sizeof(ResultBuffer)); // Clear the buffer.
SNFServer.receive(ResultBuffer, ResultBufferSize); // Receive the answer.
ResultString.append(ResultBuffer);
if(string::npos ==
if(std::string::npos ==
ResultString.rfind("</snf>",ResultString.length())) { // If we don't have the end yet.
continue; // Try again.
} else { // If we got to end of line
@@ -481,11 +486,11 @@ int main(int argc, char* argv[]) {
if(!ConnectSuccess) { // If no connection success complain!
if(DebugMode) {
cout << FileToScan << ": ";
cout << "Could Not Connect!" << endl;
std::cout << FileToScan << ": ";
std::cout << "Could Not Connect!" << std::endl;
}
ofstream ERRF(ERRFname.c_str(), ios::out | ios::ate | ios::app);
std::ofstream ERRF(ERRFname.c_str(), std::ios::out | std::ios::ate | std::ios::app);
ERRF << ErrorMessage(argc, argv, "Could Not Connect!");
ERRF.close();
@@ -497,18 +502,19 @@ int main(int argc, char* argv[]) {
// At this point we should have a usable result.
if(DebugMode) { cout << ResultString << endl; } // In debug, show the result string.
if(DebugMode) { std::cout << ResultString << std::endl; } // In debug, show the result string.
snf_xci Reader(ResultString); // Interpret the data and check for
if(Reader.bad()) { // a proper read. If it was bad then
if(DebugMode) {
cout << FileToScan << ": ";
cout << "Bad result from server!" << endl;
cout << ResultString << endl;
std::cout << FileToScan << ": ";
std::cout << "Bad result from server!" << std::endl;
std::cout << ResultString << std::endl;
}
ofstream ERRF(ERRFname.c_str(), ios::out | ios::ate | ios::app);
std::ofstream ERRF(ERRFname.c_str(), std::ios::out | std::ios::ate | std::ios::app);
ERRF << ErrorMessage(argc, argv, // complain and spit out what we got
"Bad result from server! " + ResultString); // for debugging purposes.
return FailSafeResult; // Return our failsafe value.
@@ -517,11 +523,11 @@ int main(int argc, char* argv[]) {
if(0 < Reader.xci_error_message.length()) { // If the result was a general error
if(DebugMode) {
cout << FileToScan << ": ";
cout << "XCI Error!: " << Reader.xci_error_message << endl;
std::cout << FileToScan << ": ";
std::cout << "XCI Error!: " << Reader.xci_error_message << std::endl;
}
ofstream ERRF(ERRFname.c_str(), ios::out | ios::ate | ios::app);
std::ofstream ERRF(ERRFname.c_str(), std::ios::out | std::ios::ate | std::ios::app);
ERRF << ErrorMessage(argc, argv,
"XCI Error!: " + Reader.xci_error_message); // then spit that out
return FailSafeResult; // and return the failsafe.
@@ -530,56 +536,58 @@ int main(int argc, char* argv[]) {
// If we got here we've successfully parsed the results.
if(ItIsACommand) { // If it was a command then
cout << " [Server Says: " << Reader.xci_server_response << "]" << endl; // show the response.
std::cout
<< " [Server Says: " << Reader.xci_server_response << "]" // show the response.
<< std::endl;
}
else if(ItIsAScan) { // If it was a scan then
int ResultCode = Reader.scanner_result_code; // grab the result code.
if(0 < Reader.scanner_result_xhdr.length()) { // If we have xheaders show them.
cout << endl << Reader.scanner_result_xhdr << endl;
if(GetXHDRs) { // If we want xheaders show them.
std::cout << std::endl << Reader.scanner_result_xhdr << std::endl;
}
if(DebugMode) { cout << "(" << ResultCode << ")"; }
if(DebugMode) { std::cout << "(" << ResultCode << ")"; }
if( (0 < ResultCode) && (64 > ResultCode) ) { // If the result code means SPAM
if(DebugMode) { cout << "[spam]" << endl; }
if(DebugMode) { std::cout << "[spam]" << std::endl; }
return ResultCode; // Return the result code.
} else
if(0 == ResultCode) {
if(DebugMode) { cout << "[clean]" << endl; }
if(DebugMode) { std::cout << "[clean]" << std::endl; }
return 0;
} else {
if(DebugMode) { cout << "[Fail Safe!]" << endl; }
if(DebugMode) { std::cout << "[Fail Safe!]" << std::endl; }
return FailSafeResult;
}
}
else if(ItIsAGBUdb) { // If it was a GBUdb function then show
cout // the results to whomever is watching.
<< "GBUdb Record for " << Reader.gbudb_result_ip << endl
<< " Type Flag: " << Reader.gbudb_result_type << endl
<< " Bad Count: " << Reader.gbudb_result_bad_count << endl
<< " Good Count: " << Reader.gbudb_result_good_count << endl
<< "Probability: " << Reader.gbudb_result_probability << endl
<< " Confidence: " << Reader.gbudb_result_confidence << endl
<< " Range: " << Reader.gbudb_result_range << endl
<< " Code: " << Reader.gbudb_result_code << endl
<< endl;
std::cout // the results to whomever is watching.
<< "GBUdb Record for " << Reader.gbudb_result_ip << std::endl
<< " Type Flag: " << Reader.gbudb_result_type << std::endl
<< " Bad Count: " << Reader.gbudb_result_bad_count << std::endl
<< " Good Count: " << Reader.gbudb_result_good_count << std::endl
<< "Probability: " << Reader.gbudb_result_probability << std::endl
<< " Confidence: " << Reader.gbudb_result_confidence << std::endl
<< " Range: " << Reader.gbudb_result_range << std::endl
<< " Code: " << Reader.gbudb_result_code << std::endl
<< std::endl;
return Reader.gbudb_result_code;
}
else if(ItIsAReport) { // If it was a report request then
cout // output the report. Add a handy
<< endl << "<!-- Status Report -->" << endl // XML comment to help humans.
<< Reader.report_response << endl;
std::cout // output the report. Add a handy
<< std::endl << "<!-- Status Report -->" << std::endl // XML comment to help humans.
<< Reader.report_response << std::endl;
return 0;
}
// You can't get here from there.
if(DebugMode) { cout << "End Of Logic [Fail Safe!!]" << endl; }
if(DebugMode) { std::cout << "End Of Logic [Fail Safe!!]" << std::endl; }
return FailSafeResult;
}

+ 1
- 1
SNFMulti

@@ -1 +1 @@
Subproject commit a81ce5f3e8ea19d300e169517579d63d7567bed9
Subproject commit 869c039bef12a6cdc191781f0c0a76dd7eb00c85

+ 1
- 2
makefile View File

@@ -7,12 +7,11 @@ target: main.o networking.o timing.o threading.o configuration.o snf_xci.o main.
main.o: SNFClient/main.cpp \
CodeDweller/timing.hpp \
CodeDweller/networking.hpp \
CodeDweller/networking.inline.hpp \
CodeDweller/threading.hpp \
SNFMulti/snf_xci.hpp
$(CXX) $(CXXFLAGS) -c SNFClient/main.cpp

networking.o: CodeDweller/networking.cpp CodeDweller/networking.hpp CodeDweller/networking.inline.hpp
networking.o: CodeDweller/networking.cpp CodeDweller/networking.hpp
$(CXX) $(CXXFLAGS) -c CodeDweller/networking.cpp

timing.o: CodeDweller/timing.cpp CodeDweller/timing.hpp

+ 7
- 2
readme.md View File

@@ -4,12 +4,17 @@ in debug mode (SNFDebugClient) to demonstrate how XCI is called and what to expe

# ChangeLog / Journal

## [3.2] 20200624_M
- Cleaned up namespace and warnings.
- Using latest CodeDweller.
- Using latest SNFMulti.

## 20200617_M
- Moved the changelog.md file to readme.md so that it can serve both purposes.
- Readme is visible immediately in most online git repo's.
- It's better to have one file tell the story than many.

## [3.1] 20191104_M
- Set up the changelog.md file
- fixed a bug where combining -xhdr and -source caused a string overflow
- Set up the changelog.md file.
- fixed a bug where combining -xhdr and -source caused a string overflow.


Loading…
Cancel
Save