X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2FpcapDecoder%2Fmain.cpp;h=c8bbea76e49c03b0006a8e350079704475601cca;hb=36bb910c8e4ad26ab0b2c9618abc02c255b24e58;hp=6214825cc3f8f5b59632d9235b60eb12d2567518;hpb=4456556fc4cd3d24eab8d555acceae339d476c5b;p=anna.git diff --git a/example/diameter/pcapDecoder/main.cpp b/example/diameter/pcapDecoder/main.cpp index 6214825..c8bbea7 100644 --- a/example/diameter/pcapDecoder/main.cpp +++ b/example/diameter/pcapDecoder/main.cpp @@ -51,13 +51,9 @@ #include #include #include -#include -#include -#include -#include + using namespace anna; -using namespace anna::diameter; // Payload and frame metadata ///////////////////////////////////////////////////////////////////////////// class Payload { @@ -146,7 +142,6 @@ public: typedef std::map < int /* frame */, Payload > payloads_t; typedef std::map < int /* frame */, Payload >::const_iterator payloads_it; payloads_t G_payloads; -anna::diameter::codec::Message G_codecMsg; // Sniffing structures //////////////////////////////////////////////////////////////////////////////////// @@ -327,101 +322,32 @@ void _exit(const std::string &message, int resultCode = 1) { //------------------------------------------------------------------- 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) { + if(argc < 2) { 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)."; + msg += " [--write-hex: to write hex files] [--debug: activates debug level traces (warning by default)]\n\n"; _exit(msg); } // Command-line parameters: - std::string dictionaries = argv[1]; - std::string inputFile = argv[2]; + std::string inputFile = argv[1]; 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 << "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)); - anna::diameter::codec::Engine *codecEngine = - new anna::diameter::codec::Engine(); - anna::diameter::stack::Engine &stackEngine = - anna::diameter::stack::Engine::instantiate(); + std::string optionals; + int indx = 2; + while(indx < argc) { optionals += " "; optionals += argv[indx]; indx++; } - try { - anna::diameter::stack::Dictionary * d = stackEngine.createDictionary( - 0 /* stack id */); - // Analyze comma-separated list: - anna::Tokenizer lst; - lst.apply(dictionaries, ","); - - if(lst.size() >= 1) { // always true (at least one, because -dictionary is mandatory) - anna::Tokenizer::const_iterator tok_min(lst.begin()); - anna::Tokenizer::const_iterator tok_max(lst.end()); - anna::Tokenizer::const_iterator tok_iter; - std::string pathFile; - d->allowUpdates(); - - for(tok_iter = tok_min; tok_iter != tok_max; tok_iter++) { - pathFile = anna::Tokenizer::data(tok_iter); - d->load(pathFile); - } - } + bool debug = (optionals.find("--debug") != std::string::npos); + bool writeHex = (optionals.find("--write-hex") != std::string::npos); + Logger::setLevel(debug ? Logger::Debug:Logger::Warning); + Logger::initialize(execBN.c_str(), new TraceWriter(filetrace.c_str(), 2048000)); - codecEngine->setDictionary(d); - //LOGDEBUG(anna::Logger::debug(codecEngine->asString(), ANNA_FILE_LOCATION)); - - if(lst.size() > 1) { - std::string all_in_one = "./dictionary-all-in-one.xml"; - std::ofstream out(all_in_one, std::ifstream::out); - std::string buffer = d->asXMLString(); - out.write(buffer.c_str(), buffer.size()); - out.close(); - std::cout << "Written accumulated '" << all_in_one - << "' (provide it next time to be more comfortable)." << std::endl; - } - } catch(anna::RuntimeException &ex) { - _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 'file.trace' 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 @@ -434,6 +360,25 @@ int main(int argc, char **argv) { if(handle == NULL) _exit(errbuf, 2); + // TODO: add filtering. At the moment, pcap must be previously filtered for diameter protocol ('tcp port 3868' or any other filter allowed) + /* + // Filtering: + std::string filter = ?????; + struct bpf_program _fp; + struct bpf_program *fp = &_fp; + bpf_u_int32 netmask = 4294967295; // FFFFFFFF + + if (pcap_compile(handle, fp, (char*)(filter.c_str()), 1, netmask) == -1) { + std::cerr << "Couldn't compile the filter " << filter << std::endl; + return(2); + } + + if (pcap_setfilter(handle, fp) == -1) { + std::cerr << "Couldn't set the filter " << filter << std::endl; + return(2); + } + */ + //begin processing the packets in this particular file int packets = -1; @@ -445,10 +390,11 @@ int main(int argc, char **argv) { } pcap_close(handle); //close the pcap file + // Print payloads ////////////////////////////////////////////////////////////////////////////////////////////// // Open output file: outputFile += ".report"; - std::ofstream out(outputFile, std::ifstream::out); + std::ofstream out(outputFile.c_str(), std::ifstream::out); for(payloads_it it = G_payloads.begin(); it != G_payloads.end(); it++) { LOGDEBUG( @@ -457,32 +403,31 @@ int main(int argc, char **argv) { int tsu = (it->second).getTimestampU(); std::string ts_str = ctime(&ts); ts_str.erase(ts_str.find("\n")); - out << std::endl; - out - << "===================================================================================================" - << std::endl; - out << "Date: " << ts_str << std::endl; - out << "Timestamp: " << std::to_string(ts) << "." - << std::to_string(tsu) << std::endl; - out << "Origin IP: " << (it->second).getSourceIP() << std::endl; - out << "Destination IP: " << (it->second).getDestinationIP() << std::endl; - out << std::endl; - // decode hex string: - anna::functions::fromHexString((it->second).getDataAsHex(), db_aux); - - try { - G_codecMsg.decode(db_aux); - } catch(RuntimeException &ex) { - _exit(ex.asString()); + out << "Frame: " << anna::functions::asString(it->first) << std::endl; + out << "Date: " << ts_str << std::endl; + out << "Timestamp: " << anna::functions::asString((int)ts) << "." + << anna::functions::asString((int)tsu) << std::endl; + out << "Origin IP: " << (it->second).getSourceIP() << std::endl; + out << "Destination IP: " << (it->second).getDestinationIP() << std::endl; + out << "Destination IP: " << (it->second).getDestinationIP() << std::endl; + out << "Hex String: " << (it->second).getDataAsHex() << std::endl; + + // Create hex file: + if (writeHex) { + std::string hexFile = anna::functions::asString(it->first) + ".hex"; + std::ofstream hex(hexFile.c_str(), std::ifstream::out); + hex << (it->second).getDataAsHex(); + hex.close(); } - out << G_codecMsg.asXMLString(); + out << std::endl; } // Close output file: out.close(); - std::string msg = "Open 'file.trace' in order to see process traces.\n"; - msg += "Open '"; msg += outputFile; msg += "' to see decoding results."; + std::string msg = "Open '"; msg += filetrace; msg += "' in order to see process traces.\n"; + msg += "Open '"; msg += outputFile; msg += "' to see decoding results.\n"; + if (writeHex) msg += "Open '.hex' to see specific frame data."; _exit(msg, 0); }