1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
16 #include <anna/core/DataBlock.hpp>
17 #include <anna/core/util/Tokenizer.hpp>
18 #include <anna/core/functions.hpp>
19 #include <anna/core/tracing/Logger.hpp>
20 #include <anna/core/tracing/TraceWriter.hpp>
21 #include <anna/core/RuntimeException.hpp>
22 #include <anna/io/Directory.hpp>
23 #include <anna/xml/xml.hpp>
24 #include <anna/diameter/stack/Engine.hpp>
25 #include <anna/diameter/codec/Engine.hpp>
26 #include <anna/diameter/codec/EngineManager.hpp>
27 #include <anna/diameter/codec/Message.hpp>
28 //#include <anna/diameter/codec/functions.hpp> // ApplicationId anna::diameter::codec::functions::getApplicationId(const anna::DataBlock &) throw(anna::RuntimeException);
32 using namespace anna::diameter;
34 anna::diameter::codec::Message G_codecMsg;
36 bool getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) throw() {
38 static char buffer[8192];
39 std::ifstream infile(pathfile.c_str(), std::ifstream::in);
41 if(infile.is_open()) {
43 std::string hexString(buffer, strlen(buffer));
44 // Allow colon separator in hex string: we have to remove them before processing with 'fromHexString':
45 hexString.erase(std::remove(hexString.begin(), hexString.end(), ':'), hexString.end());
47 std::string msg = "Hex string (remove colons if exists): ";
49 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
51 anna::functions::fromHexString(hexString, db);
61 void _exit(const std::string &message, int resultCode = 1) {
63 std::cerr << message << std::endl << std::endl;
65 std::cout << message << std::endl << std::endl;
70 // Decodes a diameter message coming from a datablock
71 void decodeDataBlock(const anna::DataBlock &db/*, unsigned int & detectedApplicationId*/) throw() {
73 G_codecMsg.decode(db);
74 } catch(RuntimeException &ex) {
79 //-------------------------------------------------------------------
80 int main(int argc, char **argv) {
81 std::string exec = argv[0];
82 std::string execBN = exec.substr(exec.find_last_of("/") + 1);
83 std::string filetrace = execBN + ".trace";
84 std::cout << std::endl;
86 //check command line arguments
88 std::string msg = "Usage: "; msg += exec;
89 msg += " <stacks> <working directory> [--hex-only] [--xml-only] [--no-validation] [--ignore-flags] [--debug]\n\n";
90 msg += " stacks: <appid1,dictionary1#appid2,dictionary2#...#appidN,dictionaryN>\n";
91 msg += " This is a list of #-separated stacks defined by a comma-separated pair <application-id,xml dictionary pathfile>\n";
92 msg += " If only one stack is provided, application-id could be omitted and then, all the messages will be decoded with the\n";
93 msg += " dictionary regardless the value of the application-id (the stack will be registered with id=0).\n";
94 msg += " Working directory: Contains 'xml files' (ANNA-Diameter message format) and/or 'hex files' (hexadecimal content, colons allowed).\n";
95 msg += " --hex-only: Only hex files are converted.\n";
96 msg += " --xml-only: Only xml files are converted.\n";
97 msg += " --no-validation: no validation is performed.\n";
98 msg += " --ignore-flags: wrong flags regarding dictionary are ignored in xml representation.\n";
99 msg += " --debug: activates debug level traces (warning by default).\n";
101 msg += " The batch process will translate '.xml' files into '.xml.as.hex', and '.hex' files into '.hex.as.xml'\n";
102 msg += " Take care not to include the dictionary files (which have '.xml' extension) in the working directory, or there will be an exception.\n";
106 // Command-line parameters:
107 std::string stacks = argv[1];
108 std::string wkDir = argv[2];
109 std::string optionals;
111 while(indx < argc) { optionals += " "; optionals += argv[indx]; indx++; }
113 bool no_validation = (optionals.find("--no-validation") != std::string::npos);
114 bool ignore_flags = (optionals.find("--ignore-flags") != std::string::npos);
115 bool debug = (optionals.find("--debug") != std::string::npos);
116 bool hexOnly = (optionals.find("--hex-only") != std::string::npos);
117 bool xmlOnly = (optionals.find("--xml-only") != std::string::npos);
118 if (hexOnly && xmlOnly) _exit("Cannot provide both '--hex-only' and '--xml-only' !!");
119 bool processXml = hexOnly ? false:true;
120 bool processHex = xmlOnly ? false:true;
121 Logger::setLevel(debug ? Logger::Debug:Logger::Warning);
122 Logger::initialize(execBN.c_str(), new TraceWriter(filetrace.c_str(), 2048000));
123 anna::diameter::stack::Engine &stackEngine = anna::diameter::stack::Engine::instantiate();
124 anna::diameter::codec::EngineManager &em = anna::diameter::codec::EngineManager::instantiate();
125 anna::diameter::codec::Engine *ce;
126 unsigned int appid = 0;
131 anna::Tokenizer stacksTok;
132 stacksTok.apply(stacks, "#");
133 anna::Tokenizer::const_iterator stacks_it, stack_it;
135 for(stacks_it = stacksTok.begin(); stacks_it != stacksTok.end(); stacks_it++) {
136 std::string stack = anna::Tokenizer::data(stacks_it);
137 anna::Tokenizer stackTok;
138 stackTok.apply(stack, ",");
140 if(stackTok.size() == 1) {
141 if(stacksTok.size() != 1)
142 throw anna::RuntimeException("Application Id value is mandatory when more than one stack is going to be configured", ANNA_FILE_LOCATION);
143 anna::diameter::stack::Dictionary *d = stackEngine.createDictionary(appid, stack); // the stack is the dictionary
144 ce = new anna::diameter::codec::Engine("CodecEngineForUniqueStackId_0", d);
145 em.registerCodecEngine(0, ce);
149 if(stackTok.size() != 2)
150 throw anna::RuntimeException("Each stack must be in the form '<application-id>,<xml dictionary pathfile>'", ANNA_FILE_LOCATION);
152 stack_it = stackTok.begin();
153 unsigned int stackId = atoll(anna::Tokenizer::data(stack_it));
155 std::string file = anna::Tokenizer::data(stack_it);
156 anna::diameter::stack::Dictionary *d = stackEngine.createDictionary(stackId, file);
157 std::string codecEngineName = anna::functions::asString("CodecEngineForStackId_%llu", stackId);
158 ce = new anna::diameter::codec::Engine(codecEngineName.c_str(), d);
159 em.registerCodecEngine(stackId, ce);
162 std::cout << "Stacks provided: " << std::endl;
163 std::cout << anna::functions::tab(stackEngine.asString(false /* light */));
164 std::cout << std::endl;
165 std::cout << "Working directory: " << wkDir << std::endl;
166 std::cout << "Validation: " << (!no_validation ? "yes" : "no") << std::endl;
167 std::cout << "Ignore Flags: " << (ignore_flags ? "yes" : "no") << std::endl;
168 std::cout << std::endl;
169 } catch(anna::RuntimeException &ex) {
170 _exit(ex.asString());
173 // Validation kindness
174 for (anna::diameter::codec::appid_codec_engines_it it = em.begin(); it != em.end(); it++) {
176 ce->setFixMode(anna::diameter::codec::EngineImpl::FixMode::Never); // we will encode "as is" (because --no-validation is assumed as user desire)
177 ce->setValidationDepth(anna::diameter::codec::EngineImpl::ValidationDepth::Complete); // complete validation for better reports
178 if(no_validation) ce->setValidationMode(anna::diameter::codec::EngineImpl::ValidationMode::Never);
179 if(ignore_flags) ce->ignoreFlagsOnValidation(true);
182 // Auxiliary variables:
183 anna::DataBlock db_aux(true);
184 anna::io::Directory directoryHex, directoryXml; // we separate (although it wouldn't be neccessary using setPattern each time), because we don't
185 // want to process the resulting xml files from hex conversion, only the xml files present at the
186 // beginning in the directory
188 directoryHex.setPattern(".hex$");
189 directoryHex.read(wkDir.c_str(), anna::io::Directory::Mode::FullPath);
190 directoryXml.setPattern(".xml$");
191 directoryXml.read(wkDir.c_str(), anna::io::Directory::Mode::FullPath);
193 // Processing .hex files:
194 bool anyHexConverted = false;
196 for (anna::io::Directory::const_iterator it = directoryHex.begin(); it != directoryHex.end(); it++) {
197 const std::string& entry = anna::io::Directory::data (it);
198 LOGDEBUG(anna::Logger::debug(entry + " is being converted to xml", ANNA_FILE_LOCATION));
200 if(!getDataBlockFromHexFile(entry, db_aux))
201 _exit("Error reading hex file provided");
204 decodeDataBlock(db_aux);
207 std::string outputFile = entry + ".as.xml";
208 std::ofstream out(outputFile.c_str(), std::ifstream::out);
209 out << G_codecMsg.asXMLString();
212 anyHexConverted = true;
216 // Processing .xml files:
217 bool anyXmlConverted = false;
219 for (anna::io::Directory::const_iterator it = directoryXml.begin(); it != directoryXml.end(); it++) {
220 const std::string& entry = anna::io::Directory::data (it);
221 LOGDEBUG(anna::Logger::debug(entry + " is being converted to hex", ANNA_FILE_LOCATION));
224 G_codecMsg.loadXML(entry);
227 std::string hexString = anna::functions::asHexString(G_codecMsg.code());
228 std::string outputFile = entry + ".as.hex";
229 std::ofstream out(outputFile.c_str(), std::ifstream::out);
230 out.write(hexString.c_str(), hexString.size());
233 anyXmlConverted = true;
238 std::string msg = "Open '"; msg += filetrace; msg += "' in order to see process traces.\n";
239 if (anyHexConverted) msg += "Open '*.hex.as.xml' files to see decoding results.\n";
240 if (anyXmlConverted) msg += "Open '*.xml.as.hex' files to see encoding results.\n";