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];
//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:
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 '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
//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 'file.trace' in order to see process traces.\n";
+ msg += "Open '"; msg += outputFile; msg += "' to see decoding results.";
+ _exit(msg, 0);
}
*/
std::string getXMLdata(bool & isHex, const anna::diameter::stack::Format *stackFormat) const throw();
- /**
- Interpret xml data in order to dump over the class content.
-
- \param avpNode Avp root node
- */
- void fromXML(const anna::xml::Node* avpNode) throw(anna::RuntimeException);
-
-
- /**
- Encodes buffer with the class content.
-
- * @param buffer Raw data to be encoded
- * @param size Size of raw data to be encoded
- */
- void code(char* buffer, int &size) const throw(anna::RuntimeException);
-
/**
Decodes Avp data part.
const Unknown * getUnknown() const throw(anna::RuntimeException) { assertFormat("Unknown"); return a_Unknown; }
+ /**
+ Decodes buffer provided over class content. If an error ocurred, decoding will stop launching exception (fatal error) or a warning trace (perhaps the achieved
+ avp is valid against all odds then validation will go on). In case that validation is enabled (codec::Engine::ValidationMode) an exception will be launched
+ depending on validation depth (codec::Engine::ValidationDepth).
+
+ Useful as serialization procedure with #code
+
+ @param db Buffer data block processed
+ */
+ void decode(const anna::DataBlock &db) throw(anna::RuntimeException);
+
+
+ /**
+ Interpret xml data in order to dump over the class content.
+
+ \param avpNode Avp root node
+ */
+ void fromXML(const anna::xml::Node* avpNode) throw(anna::RuntimeException);
+
+
+ /**
+ Encodes buffer with the class content. This method is internally used to encode diameter messages, but is declared as public, to allow
+ its use as serialization procedure. Then, it's assumed that this Avp is valid (validation shall be applied as part of a whole diameter
+ message but nothing will be verified now).
+
+ * @param buffer Raw data to be encoded (shall be externally allocated)
+ * @param size Size of raw data to be encoded
+ */
+ void code(char* buffer, int &size) const throw(anna::RuntimeException);
+
+
// Helpers
/**
*/
std::string asXMLString() const throw();
+ /**
+ Comparison operator by mean serialization
+
+ @param a1 Instance 1 for Avp class
+ @param a2 Instance 2 for Avp class
+
+ @return Comparison result
+ */
+ friend bool operator == (const Avp & a1, const Avp & a2) throw() { return (a1.asXMLString() == a2.asXMLString()); }
+
+ /**
+ Match a regular expression (string pattern) regarding xml string serialization for this avp.
+ This works same as #Message::isLike
+
+ @param pattern Pattern to match
+
+ \return Returns the match result
+ */
+ bool isLike(const std::string &pattern) const throw();
+
/**
Counts the number of ocurrences of Avps (first level) with the identifier provided
#include <anna/diameter/stack/Engine.hpp>
#include <anna/diameter/codec/Engine.hpp>
#include <anna/core/functions.hpp>
+#include <anna/core/util/RegularExpression.hpp>
#include <anna/core/tracing/Logger.hpp>
#include <anna/core/functions.hpp>
}
+//------------------------------------------------------------------------------
+//---------------------------------------------------------------- Avp::decode()
+//------------------------------------------------------------------------------
+void Avp::decode(const anna::DataBlock &db) throw(anna::RuntimeException) {
+
+ parent_t parent;
+ parent.setMessage(CommandId(0,false), "No-Parent");
+ decode(db, parent, NULL);
+}
+
+
//------------------------------------------------------------------------------
//--------------------------------------------------------------- Avp::fromXML()
//------------------------------------------------------------------------------
return result;
}
+
//------------------------------------------------------------------------------
//----------------------------------------------------------- Avp::asXMLString()
//------------------------------------------------------------------------------
return anna::xml::Compiler().apply(asXML(&root));
}
+
+//------------------------------------------------------------------------------
+//---------------------------------------------------------------- Avp::isLike()
+//------------------------------------------------------------------------------
+bool Avp::isLike(const std::string &pattern) const throw() {
+ anna::RegularExpression re(pattern);
+ return re.isLike(asXMLString());
+}