-//-------------------------------------------------------------------
-int main(int argc, char **argv)
-{
- std::string exec = argv[0];
-
- 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;
- }
-
- // Command-line parameters:
- std::string dictionaries = argv[1];
- std::string pcapFile = argv[2];
- std::string optional = 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 << "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();
-
- 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);
- }
- }
-
- codecEngine->setDictionary(d);
- //LOGDEBUG(anna::Logger::debug(codecEngine->asString(), ANNA_FILE_LOCATION));
-
- if (lst.size() > 1) {
- std::ofstream out("./dictionary.xml", std::ifstream::out);
- std::string buffer = d->asXMLString();
- out.write(buffer.c_str(), buffer.size());
- out.close();
- std::cout << "Written 'dictionary.xml' (provide it next time to be more comfortable)." << std::endl;
- }
-
- } 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;