Updated license
[anna.git] / source / diameter / helpers / dcca / functions.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Local
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>
44
45 #include <anna/core/functions.hpp>
46 #include <anna/core/DataBlock.hpp>
47
48 // STL
49 #include <string>
50
51 using namespace anna;
52 using namespace anna::diameter::codec;
53 using namespace anna::diameter::helpers::dcca;
54
55
56
57 //static
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";
63
64
65 // getters
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);
69
70   anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
71   std::string result = "";
72   bool found = false;
73   int pos = 1; // first avp
74   const char * subscriptionIdPtr;
75   const char * subscriptionIdDataPtr;
76   int type;
77   // Decoded avp information:
78   AvpId _id;
79   char _flags;
80   int _length;
81   std::string _dataG /* grouped */, _data;
82
83   while(!found) {
84     subscriptionIdPtr = diameter::codec::functions::findAVP(avpsDB, AVPID__Subscription_Id, pos);
85
86     if(!subscriptionIdPtr) return result;
87
88     // Look up type:
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);
93     // Enumerated:
94     int type = DECODE4BYTES_INDX_VALUETYPE(_data, 0, int);
95     found = (type == subscriptionIdType);
96     pos++;
97   }
98
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);
102   // Result:
103   return result;
104 }
105
106
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);
110
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);
113
114   if(serviceContextIdPtr == NULL)
115     throw anna::RuntimeException("Service-Context-Id AVP not found in DataBlock provided", ANNA_FILE_LOCATION);
116
117   // Decoded avp information:
118   AvpId _id;
119   char _flags;
120   int _length;
121   std::string result;
122   anna::diameter::codec::functions::decodeAVP(serviceContextIdPtr, _id, _flags, _length, result /* data-part */);
123   // Charging context detection:
124   chargingContext = ChargingContext::Unknown;
125
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;
136
137   // ...
138   // future kind of traffic
139   return result;
140 }
141