- } catch (anna::RuntimeException &ex) {
- std::cerr << ex.asString() << std::endl << std::endl;
- return 1;
- }
-
- codecEngine->ignoreFlagsOnValidation(ignoreFlags);
- // Tracing:
- //if (cl.exists("trace"))
- // anna::Logger::setLevel(anna::Logger::asLevel(cl.getValue("trace")));
-
-
-
- // SNIFFING //////////////////////////////////////////////////////////////////////////////////////////////7
-
- //temporary packet buffers
- struct pcap_pkthdr header; // The header that pcap gives us
- const u_char *packet; // The actual packet
-
- //------------------
- //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
-
- if (handle == NULL) {
- std::cerr << errbuf << std::endl << std::endl;
- return 2;
- }
-
-
- //begin processing the packets in this particular file
- int packets = -1;
- try {
- 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;
- }
- 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());
-
-
- for (payloads_it it = G_payloads.begin(); it != G_payloads.end(); it++) {
- LOGDEBUG (Logger::debug(anna::functions::asString("Dumping frame %d", it->first), ANNA_FILE_LOCATION));
- time_t ts = (it->second).getTimestamp();
- 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) {
- std::cerr << ex.asString() << std::endl << std::endl;
- return 1;
- }
- 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;
+ if(stackTok.size() != 2)
+ throw anna::RuntimeException("Each stack must be in the form '<application-id>#<xml dictionary pathfile>'", ANNA_FILE_LOCATION);
+
+ stack_it = stackTok.begin();
+ unsigned int stackId = atoll(anna::Tokenizer::data(stack_it));
+ stack_it++;
+ std::string file = anna::Tokenizer::data(stack_it);
+ anna::diameter::stack::Dictionary * d = stackEngine.createDictionary(stackId, file);
+ }
+
+ std::cout << "Stacks provided: " << std::endl;
+ std::cout << anna::functions::tab(stackEngine.asString(false /* light */));
+ std::cout << std::endl;
+ std::cout << "Input file provided: " << inputFile << std::endl;
+ std::cout << "Validation: " << (!no_validation ? "yes" : "no") << std::endl;
+ std::cout << "Ignore Flags: " << (ignore_flags ? "yes" : "no") << std::endl;
+ std::cout << std::endl;
+ } catch(anna::RuntimeException &ex) {
+ _exit(ex.asString());
+ }
+
+ // Validation kindness
+ G_codecEngine->setValidationDepth(anna::diameter::codec::EngineImpl::ValidationDepth::Complete); // complete validation for better reports
+ if(no_validation) G_codecEngine->setValidationMode(anna::diameter::codec::EngineImpl::ValidationMode::Never);
+
+ if(ignore_flags) G_codecEngine->ignoreFlagsOnValidation(true);
+
+ // 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);
+ unsigned int detectedApplicationId;
+
+ if(isHex) {
+ if(!getDataBlockFromHexFile(inputFile, db_aux))
+ _exit("Error reading hex file provided");
+
+ // Decode datablock:
+ decodeDataBlock(db_aux, detectedApplicationId);
+ // Open output file:
+ outputFile += ".as.xml";
+ std::ofstream out(outputFile.c_str(), 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
+ const u_char *packet; // The actual packet
+ //------------------
+ //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(inputFile.c_str(), errbuf); //call pcap library function
+
+ if(handle == NULL) _exit(errbuf, 2);
+
+ //begin processing the packets in this particular file
+ int packets = -1;
+
+ try {
+ while(packets != 0)
+ packets = pcap_dispatch(handle, -1, (pcap_handler) my_callback, NULL);
+ } catch(RuntimeException &ex) {
+ _exit(ex.asString());
+ }
+
+ pcap_close(handle); //close the pcap file
+ // Print payloads //////////////////////////////////////////////////////////////////////////////////////////////
+ // Open output file:
+ outputFile += ".report";
+ std::ofstream out(outputFile.c_str(), std::ifstream::out);
+
+ for(payloads_it it = G_payloads.begin(); it != G_payloads.end(); it++) {
+ LOGDEBUG(
+ Logger::debug(anna::functions::asString("Dumping frame %d", it->first), ANNA_FILE_LOCATION));
+ time_t ts = (it->second).getTimestamp();
+ 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: " << 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;
+ // decode hex string:
+ anna::functions::fromHexString((it->second).getDataAsHex(), db_aux);
+ // Decode datablock:
+ decodeDataBlock(db_aux, detectedApplicationId);
+ // Stack identification:
+ //out << "Application Id: " << detectedApplicationId << std::endl;
+ out << "Dictionary used: " << G_codecEngine->getDictionary()->getName() << std::endl;
+ out << std::endl;
+ 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);