1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 #include <anna/diameter/helpers/dcca/defines.hpp>
39 #include <anna/diameter/helpers/dcca/functions.hpp>
40 #include <anna/diameter/codec/Message.hpp>
41 #include <anna/diameter/codec/functions.hpp>
42 #include <anna/config/defines.hpp> // general types, decoding helpers (DECODE[2/3/4]BYTES_INDX_VALUETYPE), etc.
43 #include <anna/core/functions.hpp>
45 #include <anna/core/functions.hpp>
46 #include <anna/core/DataBlock.hpp>
52 using namespace anna::diameter::codec;
53 using namespace anna::diameter::helpers::dcca;
58 const char anna::diameter::helpers::dcca::ChargingContextAndDomainSuffix::Data[] = "32251@3gpp.org";
59 const char anna::diameter::helpers::dcca::ChargingContextAndDomainSuffix::Voice[] = "OCS-CS@telefonica.com";
60 const char anna::diameter::helpers::dcca::ChargingContextAndDomainSuffix::Content[] = "OCS-Generic-Services@telefonica.com";
61 const char anna::diameter::helpers::dcca::ChargingContextAndDomainSuffix::SMS[] = "32274@3gpp.org";
62 const char anna::diameter::helpers::dcca::ChargingContextAndDomainSuffix::MMS[] = "32270@3gpp.org";
66 std::string anna::diameter::helpers::dcca::functions::getSubscriptionIdData(const anna::DataBlock & db, int subscriptionIdType) throw(anna::RuntimeException) {
67 if(db.getSize() < Message::HeaderLength)
68 throw anna::RuntimeException("Not enough bytes to cover command header length", ANNA_FILE_LOCATION);
70 anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
71 std::string result = "";
73 int pos = 1; // first avp
74 const char * subscriptionIdPtr;
75 const char * subscriptionIdDataPtr;
77 // Decoded avp information:
81 std::string _dataG /* grouped */, _data;
84 subscriptionIdPtr = diameter::codec::functions::findAVP(avpsDB, AVPID__Subscription_Id, pos);
86 if(!subscriptionIdPtr) return result;
89 diameter::codec::functions::decodeAVP(subscriptionIdPtr, _id, _flags, _length, _dataG);
90 // Data is Fixed Subscription-Id-Type (Enumerated derived from Integer32) and then Fixed Subscription-Id-Data (UTF8String):
91 // No need to find Subscription-Id-Type, it's always the first:
92 diameter::codec::functions::decodeAVP(_dataG.c_str(), _id, _flags, _length, _data);
94 int type = DECODE4BYTES_INDX_VALUETYPE(_data, 0, int);
95 found = (type == subscriptionIdType);
99 // No need to find Subscription-Id-Data within _dataG, it's always the second, and the first takes always 3 words (no vendorID):
100 subscriptionIdDataPtr = _dataG.c_str() + 12;
101 diameter::codec::functions::decodeAVP(subscriptionIdDataPtr, _id, _flags, _length, result);
107 std::string anna::diameter::helpers::dcca::functions::getServiceContextId(const anna::DataBlock & db, ChargingContext::_v &chargingContext) throw(anna::RuntimeException) {
108 if(db.getSize() < Message::HeaderLength)
109 throw anna::RuntimeException("Not enough bytes to cover command header length", ANNA_FILE_LOCATION);
111 anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
112 const char * serviceContextIdPtr = anna::diameter::codec::functions::findAVP(avpsDB, AVPID__Service_Context_Id);
114 if(serviceContextIdPtr == NULL)
115 throw anna::RuntimeException("Service-Context-Id AVP not found in DataBlock provided", ANNA_FILE_LOCATION);
117 // Decoded avp information:
122 anna::diameter::codec::functions::decodeAVP(serviceContextIdPtr, _id, _flags, _length, result /* data-part */);
123 // Charging context detection:
124 chargingContext = ChargingContext::Unknown;
126 if(anna::functions::endsWith(result, ChargingContextAndDomainSuffix::Data))
127 chargingContext = ChargingContext::Data;
128 else if(anna::functions::endsWith(result, ChargingContextAndDomainSuffix::Voice))
129 chargingContext = ChargingContext::Voice;
130 else if(anna::functions::endsWith(result, ChargingContextAndDomainSuffix::Content))
131 chargingContext = ChargingContext::Content;
132 else if(anna::functions::endsWith(result, ChargingContextAndDomainSuffix::SMS))
133 chargingContext = ChargingContext::SMS;
134 else if(anna::functions::endsWith(result, ChargingContextAndDomainSuffix::MMS))
135 chargingContext = ChargingContext::MMS;
138 // future kind of traffic