count++;
}
+bool getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) throw() {
+ // Get hex string
+ static char buffer[8192];
+ std::ifstream infile(pathfile.c_str(), std::ifstream::in);
+
+ if(infile.is_open()) {
+ infile >> buffer;
+ std::string hexString(buffer, strlen(buffer));
+ // Allow colon separator in hex string: we have to remove them before processing with 'fromHexString':
+ hexString.erase(std::remove(hexString.begin(), hexString.end(), ':'), hexString.end());
+ LOGDEBUG(
+ std::string msg = "Hex string (remove colons if exists): ";
+ msg += hexString;
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+ anna::functions::fromHexString(hexString, db);
+ // Close file
+ infile.close();
+ return true;
+ }
+
+ return false;
+}
+
+
+void _exit(const std::string &message, int resultCode = 1) {
+ if(resultCode)
+ std::cerr << message << std::endl << std::endl;
+ else
+ std::cout << message << std::endl << std::endl;
+
+ exit(resultCode);
+}
+
+
//-------------------------------------------------------------------
int main(int argc, char **argv) {
std::string exec = argv[0];
+ std::string execBN = exec.substr(exec.find_last_of("/") + 1);
+ std::string filetrace = execBN + ".trace";
std::cout << std::endl;
//check command line arguments
if(argc < 3) {
- std::cout << "Usage: " << exec
- << " <list of comma-separated xml dictionaries> <pcap file> [--ignore-flags: non-strict validation]"
- << std::endl << std::endl;
- return 1;
+ std::string msg = "Usage: "; msg += exec;
+ msg += " <dictionaries> <input file> [--ignore-flags: non-strict validation]\n\n";
+ msg += " dictionaries: list of comma-separated xml dictionaries (one or more can be provided).\n";
+ msg += " Input file: normally a pcap file, but hexadecimal content (colons allowed) can also be decoded (use '.hex' extension).";
+ _exit(msg);
}
// Command-line parameters:
std::string dictionaries = argv[1];
- std::string pcapFile = argv[2];
+ std::string inputFile = argv[2];
+ bool isHex = (inputFile.substr(inputFile.find_last_of(".") + 1) == "hex");
+ std::string outputFile = inputFile; // extension will be added later
std::string optional = argv[3] ? argv[3] : "";
bool ignoreFlags = ((argc == 4) && (optional == "--ignore-flags"));
std::cout << "Dictionary(ies) provided: " << dictionaries << std::endl;
- std::cout << "Pcap file provided: " << pcapFile << std::endl;
+ std::cout << "Input file provided: " << inputFile << std::endl;
std::cout << "Validation kindness: "
<< (ignoreFlags ? "non strict" : "strict") << std::endl;
// Logger and engines:
Logger::setLevel(Logger::Debug);
- Logger::initialize("pcapDecoder", new TraceWriter("file.trace", 2048000));
+ Logger::initialize(execBN.c_str(), new TraceWriter(filetrace.c_str(), 2048000));
anna::diameter::codec::Engine *codecEngine =
new anna::diameter::codec::Engine();
anna::diameter::stack::Engine &stackEngine =
std::string buffer = d->asXMLString();
out.write(buffer.c_str(), buffer.size());
out.close();
- std::cout << "Written '" << all_in_one
+ std::cout << "Written accumulated '" << all_in_one
<< "' (provide it next time to be more comfortable)." << std::endl;
}
} catch(anna::RuntimeException &ex) {
- std::cerr << ex.asString() << std::endl << std::endl;
- return 1;
+ _exit(ex.asString());
}
codecEngine->ignoreFlagsOnValidation(ignoreFlags);
// Tracing:
//if (cl.exists("trace"))
// anna::Logger::setLevel(anna::Logger::asLevel(cl.getValue("trace")));
+ // Check hex content input file (look extension):
+ anna::DataBlock db_aux(true);
+
+ if(isHex) {
+ if(!getDataBlockFromHexFile(inputFile, db_aux))
+ _exit("Error reading hex file provided");
+
+ try {
+ G_codecMsg.decode(db_aux);
+ } catch(RuntimeException &ex) {
+ _exit(ex.asString());
+ }
+
+ // Open output file:
+ outputFile += ".as.xml";
+ std::ofstream out(outputFile, std::ifstream::out);
+ out << G_codecMsg.asXMLString();
+ // Close output file:
+ out.close();
+ std::string msg = "Open '"; msg += filetrace; msg += "' in order to see process traces.\n";
+ msg += "Open '"; msg += outputFile; msg += "' to see decoding results.";
+ _exit(msg, 0);
+ }
+
+ // Normal input: pcap file:
// SNIFFING //////////////////////////////////////////////////////////////////////////////////////////////7
//temporary packet buffers
struct pcap_pkthdr header; // The header that pcap gives us
//open the pcap file
pcap_t *handle;
char errbuf[PCAP_ERRBUF_SIZE]; //not sure what to do with this, oh well
- handle = pcap_open_offline(pcapFile.c_str(), errbuf); //call pcap library function
+ handle = pcap_open_offline(inputFile.c_str(), errbuf); //call pcap library function
- if(handle == NULL) {
- std::cerr << errbuf << std::endl << std::endl;
- return 2;
- }
+ if(handle == NULL) _exit(errbuf, 2);
//begin processing the packets in this particular file
int packets = -1;
while(packets != 0)
packets = pcap_dispatch(handle, -1, (pcap_handler) my_callback, NULL);
} catch(RuntimeException &ex) {
- std::cerr << ex.asString() << std::endl << std::endl;
- return 1;
+ _exit(ex.asString());
}
pcap_close(handle); //close the pcap file
// Print payloads //////////////////////////////////////////////////////////////////////////////////////////////
// Open output file:
- std::string output = pcapFile;
- output += ".report";
- std::ofstream out(output, std::ifstream::out);
- std::string xmlStr;
- anna::DataBlock db_aux(true);
-
- //out.write(str.c_str(), str.size());
+ outputFile += ".report";
+ std::ofstream out(outputFile, std::ifstream::out);
for(payloads_it it = G_payloads.begin(); it != G_payloads.end(); it++) {
LOGDEBUG(
try {
G_codecMsg.decode(db_aux);
} catch(RuntimeException &ex) {
- std::cerr << ex.asString() << std::endl << std::endl;
- return 1;
+ _exit(ex.asString());
}
out << G_codecMsg.asXMLString();
// Close output file:
out.close();
- std::cout << "Open 'file.trace' in order to see process traces." << std::endl;
- std::cout << "Open '" << output << "' to see conversion results."
- << std::endl;
- std::cout << std::endl;
- return 0;
+ std::string msg = "Open '"; msg += filetrace; msg += "' in order to see process traces.\n";
+ msg += "Open '"; msg += outputFile; msg += "' to see decoding results.";
+ _exit(msg, 0);
}