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 //
10 #include <anna/core/core.hpp>
11 #include <anna/diameter/functions.hpp>
12 #include <anna/time/functions.hpp>
13 #include <anna/diameter.comm/Response.hpp>
14 #include <anna/diameter.comm/ClientSession.hpp>
15 #include <anna/diameter.comm/OriginHost.hpp>
16 //#include <anna/diameter/helpers/base/functions.hpp>
17 #include <anna/diameter/helpers/nas/defines.hpp>
20 #include <MyDiameterEngine.hpp>
21 #include <MyDiameterEntity.hpp>
22 #include <MyLocalServer.hpp>
23 #include <rxSimpleTest.hpp>
24 #include <anna/testing/TestManager.hpp>
27 void MyDiameterEntity::eventRequestRetransmission(const anna::diameter::comm::ClientSession* clientSession, anna::diameter::comm::Message *request) throw() {
29 LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventRequestRetransmission", ANNA_FILE_LOCATION));
32 Entity::eventRequestRetransmission(clientSession, request); // warning trace
35 RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
36 anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
38 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(request->getBody());
41 std::string msg = "Request retransmitted: ";
42 msg += anna::diameter::functions::commandIdAsPairString(cid);
43 msg += " | DiameterServer: ";
44 msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
45 msg += " | EventTime: ";
46 msg += anna::time::functions::currentTimeAsString();
47 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
52 void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
53 throw(anna::RuntimeException) {
54 LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventRequest", ANNA_FILE_LOCATION));
56 RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
57 anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
60 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
63 std::string msg = "Request received: ";
64 msg += anna::diameter::functions::commandIdAsPairString(cid);
65 msg += " | DiameterServer: ";
66 msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
67 msg += " | EventTime: ";
68 msg += anna::time::functions::currentTimeAsString();
69 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
73 void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
74 throw(anna::RuntimeException) {
75 LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventResponse", ANNA_FILE_LOCATION));
76 RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
77 anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
78 anna::diameter::comm::ClassCode::_v code = response.getClassCode();
79 anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
80 anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
81 const anna::DataBlock* message = response.getMessage();
82 const anna::diameter::comm::ClientSession *clientSession_c = static_cast<const anna::diameter::comm::ClientSession *>(response.getSession());
83 anna::diameter::comm::ClientSession *clientSession = const_cast<anna::diameter::comm::ClientSession *>(clientSession_c);
84 bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
85 bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
86 bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
89 anna::diameter::CommandId request_cid = request->getCommandId();
92 std::string msg = "Response received for original diameter request: ";
93 msg += anna::diameter::functions::commandIdAsPairString(request_cid);
94 msg += " | Response: ";
95 msg += response.asString();
96 msg += " | DiameterServer: ";
97 msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
98 msg += " | EventTime: ";
99 msg += anna::time::functions::currentTimeAsString();
100 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
104 //if (isApplicationMessage)
105 LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
109 //if (isApplicationMessage)
110 LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
115 std::string msg = "Received response for diameter message: ";
116 msg += anna::diameter::functions::commandIdAsPairString(request_cid);
117 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
120 if(request_cid == anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) {
121 // Build AAR and send it:
122 anna::diameter::codec::Message aar;
124 // From file would be:
125 //aar.loadXML("./aar.xml");
126 aar.setApplicationId(anna::diameter::helpers::APPID__3GPP_Rx); // 16777236
127 aar.setId("AA-Request");
130 Avp *sessionId = aar.addAvp("Session-Id");
131 sessionId->getUTF8String()->setValue("ocs3;1332774430;1;1332774430");
133 // Auth-Application-Id
134 Avp *authApplicationId = aar.addAvp("Auth-Application-Id");
135 authApplicationId->getUnsigned32()->setValue(16777236);
138 Avp *originHost = aar.addAvp("Origin-Host");
139 originHost->getDiameterIdentity()->setValue("thehost.theOriginRealm.com");
142 Avp *originRealm = aar.addAvp("Origin-Realm");
143 originRealm->getDiameterIdentity()->setValue("theOriginRealm.com");
146 Avp *destinationRealm = aar.addAvp("Destination-Realm");
147 destinationRealm->getDiameterIdentity()->setValue("theDestinationRealm.com");
149 // Suppported features:
150 Avp *supportedFeatures = aar.addAvp("Supported-Features");
151 supportedFeatures->addAvp("Vendor-Id")->getUnsigned32()->setValue(10415);
152 supportedFeatures->addAvp("Feature-List-ID")->getUnsigned32()->setValue(1);
153 supportedFeatures->addAvp("Feature-List")->getUnsigned32()->setValue(3);
155 anna::diameter::comm::Message *msg;
158 msg = my_node->createCommMessage();
159 msg->setBody(aar.code());
160 /* response = NULL =*/clientSession->send(msg);
161 } catch(anna::RuntimeException &ex) {
166 my_node->releaseCommMessage(msg);
168 else if(request_cid == anna::diameter::helpers::nas::COMMANDID__AA_Request) {
170 anna::diameter::codec::Message aaa;
171 try { aaa.decode(*message); } catch(anna::RuntimeException &ex) { ex.trace(); }
173 Avp *resultCode = aaa.getAvp("Result-Code");
174 int rc = resultCode->getUnsigned32()->getValue();
176 if (rc != 2001) exit(1);
182 void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
183 throw(anna::RuntimeException) {
184 LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
185 // Performance stats:
186 RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
187 anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
189 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
192 std::string msg = "Out-of-context response received from entity: ";
193 msg += anna::diameter::functions::commandIdAsPairString(cid);
194 msg += " | DiameterServer: ";
195 msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
196 msg += " | EventTime: ";
197 msg += anna::time::functions::currentTimeAsString();
198 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
202 void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
203 throw(anna::RuntimeException) {
204 LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
205 // Performance stats:
206 RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
207 anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
209 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
212 std::string msg = "Disconnect-Peer-Answer received from entity: ";
213 msg += anna::diameter::functions::commandIdAsPairString(cid);
214 msg += " | DiameterServer: ";
215 msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
216 msg += " | EventTime: ";
217 msg += anna::time::functions::currentTimeAsString();
218 anna::Logger::debug(msg, ANNA_FILE_LOCATION);