X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2FpcapDecoder%2Fmain.cpp;h=8029b51ab1a5db2489f8bcf03704c7512fc9e6a5;hb=3190f5702ccfcd2c42c51f4aa3217b249fead77f;hp=8e3e86614ecfc3824137250e55c0ef88ffd37629;hpb=0ce74f51388826845727411a7d37d841111ede84;p=anna.git diff --git a/example/diameter/pcapDecoder/main.cpp b/example/diameter/pcapDecoder/main.cpp index 8e3e866..8029b51 100644 --- a/example/diameter/pcapDecoder/main.cpp +++ b/example/diameter/pcapDecoder/main.cpp @@ -289,31 +289,70 @@ void my_callback(u_char *useless, const struct pcap_pkthdr* pkthdr, 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 filetrace = exec.substr(exec.find_last_of("/") + 1) + ".trace"; std::cout << std::endl; //check command line arguments if(argc < 3) { - std::cout << "Usage: " << exec - << " [--ignore-flags: non-strict validation]" - << std::endl << std::endl; - return 1; + std::string msg = "Usage: "; msg += exec; + msg += " [--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("pcapDecoder", new TraceWriter(filetrace.c_str(), 2048000)); anna::diameter::codec::Engine *codecEngine = new anna::diameter::codec::Engine(); anna::diameter::stack::Engine &stackEngine = @@ -348,18 +387,42 @@ int main(int argc, char **argv) { 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 @@ -368,12 +431,9 @@ int main(int argc, char **argv) { //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; @@ -382,20 +442,14 @@ int main(int argc, char **argv) { 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( @@ -420,8 +474,7 @@ int main(int argc, char **argv) { 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(); @@ -429,10 +482,8 @@ int main(int argc, char **argv) { // 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); }