X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2Flauncher%2Fmain.cpp;h=5a8422d8503ca8c04a05fa7656fd5ba8db1dfeac;hb=d05828d1ebfc81da3041e8ac474057fc0e453f0c;hp=6f5084e0d48571f01b80f5421c3e78305e4e284f;hpb=39033fd99e58e994a5e98c1060dcc79e0d81f9c9;p=anna.git diff --git a/example/diameter/launcher/main.cpp b/example/diameter/launcher/main.cpp index 6f5084e..5a8422d 100644 --- a/example/diameter/launcher/main.cpp +++ b/example/diameter/launcher/main.cpp @@ -337,6 +337,9 @@ public: std::string programmedAnswers2e() const throw(); std::string programmedAnswers2c() const throw(); + // helpers + bool getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) const throw(); + // Burst feature int clearBurst() throw(); // returns removed int loadBurstMessage(const anna::DataBlock & db) throw(anna::RuntimeException); @@ -352,6 +355,31 @@ public: std::string gotoBurst(int order) throw(); }; +bool Launcher::getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) const 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; +} + int Launcher::clearBurst() throw() { int size = a_burstMessages.size(); @@ -432,7 +460,7 @@ int Launcher::pushBurst(int loadAmount) throw() { } a_burstActive = true; - register int count; + int count; for (count = 0; count < loadAmount; count++) if (!sendBurstMessage()) break; @@ -456,7 +484,7 @@ int Launcher::sendBurst(int loadAmount) throw() { return -2; } - register int count; + int count; for (count = 0; count < loadAmount; count++) if (!sendBurstMessage(true /* anyway */)) break; @@ -497,7 +525,7 @@ bool Launcher::sendBurstMessage(bool anyway) throw() { if (a_burstRepeat) { a_burstCycle++; - if (burstLogEnabled()) writeBurstLogFile(anna::functions::asString(("\nCompleted burst cycle. Starting again (repeat mode) on cycle %d.\n", a_burstCycle))); + if (burstLogEnabled()) writeBurstLogFile(anna::functions::asString("\nCompleted burst cycle. Starting again (repeat mode) on cycle %d.\n", a_burstCycle)); } else { if (burstLogEnabled()) writeBurstLogFile("\nCompleted burst cycle. Burst finished (repeat mode disabled).\n"); @@ -737,6 +765,17 @@ std::string Launcher::help() const throw() { result += "\nanswerxml|[source_file] Same as 'answerxml2c'."; result += "\n List programmed answers if no parameter provided."; result += "\n"; + result += "\nSend operations are available using hexadecimal content (hex formatted files) which also allow to test"; + result += "\nspecial scenarios (protocol errors):"; + result += "\n"; + result += "\nsendhex2e| Sends hex source file (pathfile) through configured entity."; + result += "\nsendhex2c| Sends hex source file (pathfile) to client."; + result += "\nsendhex| Same as 'sendhex2e'."; + result += "\n"; + result += "\nAnswer programming in hexadecimal is not really neccessary (you could use send primitives) and also"; + result += "\nis intended to be used with decoded messages in order to replace things like hop by hop, end to end,"; + result += "\nsubscriber id, session id, etc."; + result += "\n"; result += "\nIf a request is received, answer map (built with 'answerxml<[2c] or 2e>' operations) will be"; result += "\n checked to find a corresponding programmed answer to be replied(*). If no ocurrence is found,"; result += "\n or answer message was received, the message is forwarded to the other side (entity or client),"; @@ -745,7 +784,7 @@ std::string Launcher::help() const throw() { result += "\n"; result += "\n(*) sequence values (hop-by-hop and end-to-end), Session-Id and Subscription-Id avps, are mirrored"; result += "\n to the peer which sent the request. If user wants to test a specific answer without changing it,"; - result += "\n use sendxml operations better than programming."; + result += "\n use sendxml/sendhex operations better than programming."; result += "\n"; result += "\nBalance ('-balance' command line parameter) could be used to forward server socket receptions through"; result += "\n entity servers by mean a round-robin algorithm. Both diameter server socket and entity targets should"; @@ -1435,8 +1474,8 @@ throw(anna::RuntimeException) { std::string address; int port; anna::functions::getAddressAndPortFromSocketLiteral(cl.getValue("httpServer"), address, port); - //const anna::comm::Device* device = network.find(Device::asAddress(address)); // aqui hay que proporcionar una IP ! - const anna::comm::Device* device = *((network.resolve(address)->device_begin())); // ArtimaƱa para resolver (IP o hostname) + //const anna::comm::Device* device = network.find(Device::asAddress(address)); // here provide IP + const anna::comm::Device* device = *((network.resolve(address)->device_begin())); // trick to solve a_httpServerSocket = new anna::comm::ServerSocket(anna::comm::INetAddress(device, port), cl.exists("httpServerShared") /* shared bind */, &anna::http::Transport::getFactory()); } @@ -1645,6 +1684,10 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons LOGDEBUG(anna::Logger::debug(operation, ANNA_FILE_LOCATION)); response_content = "Operation processed with exception. See traces\n"; // supposed std::string result = ""; + anna::DataBlock db_aux(true); + + /////////////////////////////////////////////////////////////////// + // Simple operations without arguments: // Help: if (operation == "help") { @@ -1662,61 +1705,69 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons response_content = "All process counters & statistic information have been reset\n"; return; } + /////////////////////////////////////////////////////////////////// // Tokenize operation Tokenizer params; params.apply(operation, "|"); int numParams = params.size() - 1; - //LOGDEBUG(anna::Logger::debug(anna::functions::asString("Number of operation parameters: %d", numParams), ANNA_FILE_LOCATION)); + // No operation has more than 2 arguments ... if (numParams > 2) { LOGWARNING(anna::Logger::warning(help(), ANNA_FILE_LOCATION)); throw anna::RuntimeException("Wrong body content format on HTTP Request", ANNA_FILE_LOCATION); } + // Get the operation type: Tokenizer::const_iterator tok_iter = params.begin(); std::string opType = Tokenizer::data(tok_iter); - std::string param1, param2; - if (numParams >= 1) { tok_iter++; param1 = Tokenizer::data(tok_iter); } + // Check the number of parameters: + bool wrongBody = false; + if ( ((opType == "code")||(opType == "decode")) && (numParams != 2) ) wrongBody = true; + if ( ((opType == "sendxml") || (opType == "sendxml2e") || (opType == "sendhex") || (opType == "sendhex2e")) && (numParams != 1) ) wrongBody = true; + if ( (opType == "burst") && (numParams < 1) ) wrongBody = true; + if ( ((opType == "sendxml2c")||(opType == "sendhex2c")||(opType == "loadxml")||(opType == "diameterServerSessions")) && (numParams != 1) ) wrongBody = true; + if (wrongBody) { + // Launch exception + std::string msg = "Wrong body content format on HTTP Request for '"; + msg += opType; + msg += "' operation (missing parameter/s)"; + throw anna::RuntimeException(msg, ANNA_FILE_LOCATION); + } + // All seems ok: + std::string param1, param2; + if (numParams >= 1) { tok_iter++; param1 = Tokenizer::data(tok_iter); } if (numParams == 2) { tok_iter++; param2 = Tokenizer::data(tok_iter); } - if (opType == "code") { - if (numParams != 2) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'code' operation (missing parameter/s)", ANNA_FILE_LOCATION); + // Operations: + if (opType == "code") { G_codecMsg.loadXML(param1); std::string hexString = anna::functions::asHexString(G_codecMsg.code()); // write to outfile ofstream outfile(param2.c_str(), ifstream::out); outfile.write(hexString.c_str(), hexString.size()); outfile.close(); - } else if (opType == "decode") { - if (numParams != 2) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'decode' operation (missing parameter/s)", ANNA_FILE_LOCATION); - char buffer[2048]; - ifstream infile(param1.c_str(), ifstream::in); - infile >> buffer; - std::string hexString(buffer, strlen(buffer)); - anna::DataBlock db(true); - anna::functions::fromHexString(hexString, db); + } else if (opType == "decode") { + // Get DataBlock from file with hex content: + if (!getDataBlockFromHexFile(param1, db_aux)) + throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION); // Decode - try { G_codecMsg.decode(db); } catch (anna::RuntimeException &ex) { ex.trace(); } + try { G_codecMsg.decode(db_aux); } catch (anna::RuntimeException &ex) { ex.trace(); } std::string xmlString = G_codecMsg.asXMLString(); // write to outfile ofstream outfile(param2.c_str(), ifstream::out); outfile.write(xmlString.c_str(), xmlString.size()); outfile.close(); - infile.close(); + } else if ((opType == "hide") || (opType == "show") || (opType == "hidden") || (opType == "shown")) { anna::diameter::comm::Entity *entity = getEntity(); - if (!entity) throw anna::RuntimeException("No entity configured to send messages", ANNA_FILE_LOCATION); - if (param1 != "") { if (param2 != "") { std::string key = param1; @@ -1724,11 +1775,8 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons key += param2; if (opType == "hide") getMyDiameterEngine()->findClientSession(key)->hide(); - if (opType == "show") getMyDiameterEngine()->findClientSession(key)->show(); - if (opType == "hidden") result = getMyDiameterEngine()->findClientSession(key)->hidden() ? "true" : "false"; - if (opType == "shown") result = getMyDiameterEngine()->findClientSession(key)->shown() ? "true" : "false"; } else { std::string address; @@ -1736,35 +1784,34 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons anna::functions::getAddressAndPortFromSocketLiteral(param1, address, port); if (opType == "hide") getMyDiameterEngine()->findServer(address, port)->hide(); - if (opType == "show") getMyDiameterEngine()->findServer(address, port)->show(); - if (opType == "hidden") result = getMyDiameterEngine()->findServer(address, port)->hidden() ? "true" : "false"; - if (opType == "shown") result = getMyDiameterEngine()->findServer(address, port)->shown() ? "true" : "false"; } } else { if (opType == "hide") entity->hide(); - if (opType == "show") entity->show(); - if (opType == "hidden") result = entity->hidden() ? "true" : "false"; - if (opType == "shown") result = entity->shown() ? "true" : "false"; } - } else if ((opType == "sendxml") || (opType == "sendxml2e")) { - if (numParams != 1) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'sendxml/sendxml2e' operation (missing parameter)", ANNA_FILE_LOCATION); + } else if ((opType == "sendxml") || (opType == "sendxml2e") || (opType == "sendhex") || (opType == "sendhex2e")) { anna::diameter::comm::Entity *entity = getEntity(); - if (!entity) throw anna::RuntimeException("No entity configured to send the message", ANNA_FILE_LOCATION); - G_codecMsg.loadXML(param1); - G_commMsgSent2e.clearBody(); - try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher) + if ((opType == "sendxml") || (opType == "sendxml2e")) { + G_codecMsg.loadXML(param1); + G_commMsgSent2e.clearBody(); + try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher) + G_commMsgSent2e.setBody(G_codecMsg.code()); + } + else { + // Get DataBlock from file with hex content: + if (!getDataBlockFromHexFile(param1, db_aux)) + throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION); + G_commMsgSent2e.setBody(db_aux); + } - G_commMsgSent2e.setBody(G_codecMsg.code()); bool success = entity->send(G_commMsgSent2e, cl.exists("balance")); // Detailed log: @@ -1775,11 +1822,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons writeLogFile(G_codecMsg, (success ? "sent2e" : "send2eError"), detail); } } else if ((opType == "burst")) { - if (numParams < 1) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'burst' operation (missing action parameter)", ANNA_FILE_LOCATION); - anna::diameter::comm::Entity *entity = getEntity(); - if (!entity) throw anna::RuntimeException("No entity configured to use burst feature", ANNA_FILE_LOCATION); // burst|clear clears all loaded burst messages. @@ -1881,19 +1924,24 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons } else { throw anna::RuntimeException("Wrong body content format on HTTP Request for 'burst' operation (unexpected action parameter). See help", ANNA_FILE_LOCATION); } - } else if (opType == "sendxml2c") { - if (numParams != 1) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'sendxml2c' operation (missing parameter)", ANNA_FILE_LOCATION); + } else if ((opType == "sendxml2c") || (opType == "sendhex2c")) { anna::diameter::comm::LocalServer *localServer = getDiameterLocalServer(); - if (!localServer) throw anna::RuntimeException("No local server configured to send the message", ANNA_FILE_LOCATION); - G_codecMsg.loadXML(param1); - G_commMsgSent2c.clearBody(); - try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher) + if (opType == "sendxml2c") { + G_codecMsg.loadXML(param1); + G_commMsgSent2c.clearBody(); + try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher) + G_commMsgSent2c.setBody(G_codecMsg.code()); + } + else { + // Get DataBlock from file with hex content: + if (!getDataBlockFromHexFile(param1, db_aux)) + throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION); + G_commMsgSent2c.setBody(db_aux); + } - G_commMsgSent2c.setBody(G_codecMsg.code()); bool success = localServer->send(G_commMsgSent2c); // Detailed log: @@ -1902,16 +1950,13 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons std::string detail = usedServerSession ? usedServerSession->asString() : ""; // esto no deberia ocurrir writeLogFile(G_codecMsg, (success ? "sent2c" : "send2cError"), detail); } - } else if (opType == "loadxml") { - if (numParams != 1) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'loadxml' operation (missing parameter)", ANNA_FILE_LOCATION); + } else if (opType == "loadxml") { G_codecMsg.loadXML(param1); std::string xmlString = G_codecMsg.asXMLString(); std::cout << xmlString << std::endl; + } else if (opType == "diameterServerSessions") { - if (numParams != 1) - throw anna::RuntimeException("Wrong body content format on HTTP Request for 'diameterServerSessions' operation (missing parameter)", ANNA_FILE_LOCATION); int diameterServerSessions = atoi(param1.c_str()); @@ -1950,7 +1995,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons response_content = "Programmed answers dumped on stdout\n"; return; } - } else if ((opType == "answerxml2e")) { + } else if (opType == "answerxml2e") { anna::diameter::comm::Entity *entity = getEntity(); if (!entity) @@ -2014,7 +2059,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons response_content += "Result: "; response_content += result; response_content += "\n"; - } else if ((opType == "sendxml") || (opType == "sendxml2e")) { + } else if ((opType == "sendxml") || (opType == "sendxml2e") || (opType == "sendhex") || (opType == "sendhex2e")) { response_content += "Message '"; response_content += param1; response_content += "' sent to entity."; @@ -2025,7 +2070,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons response_content += "' executed. "; response_content += result; response_content += "\n"; - } else if (opType == "sendxml2c") { + } else if ((opType == "sendxml2c")||(opType == "sendhex2c")) { response_content += "Message '"; response_content += param1; response_content += "' sent to client."; @@ -2040,7 +2085,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons response_content += param1; response_content += "' programmed."; response_content += "\n"; - } else if ((opType == "answerxml2e")) { + } else if (opType == "answerxml2e") { response_content += "Answer to entity '"; response_content += param1; response_content += "' programmed.";