Updated license
[anna.git] / source / diameter / helpers / base / 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/defines.hpp>
39 #include <anna/diameter/helpers/base/defines.hpp>
40 #include <anna/diameter/helpers/base/functions.hpp>
41 #include <anna/diameter/codec/Message.hpp>
42 #include <anna/diameter/codec/functions.hpp>
43 #include <anna/config/defines.hpp> // general types, decoding helpers (DECODE[2/3/4]BYTES_INDX_VALUETYPE), etc.
44
45 #include <anna/core/functions.hpp>
46 #include <anna/core/DataBlock.hpp>
47 #include <anna/core/util/Tokenizer.hpp>
48
49 // STL
50 #include <string>
51
52 using namespace anna;
53 using namespace anna::diameter::codec;
54 using namespace anna::diameter::helpers::base;
55
56 // getters
57 U32 anna::diameter::helpers::base::functions::getResultCode(const anna::DataBlock & db) throw(anna::RuntimeException) {
58   if(db.getSize() < Message::HeaderLength)
59     throw anna::RuntimeException("Not enough bytes to cover command header length", ANNA_FILE_LOCATION);
60
61   anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
62   const char * resultCodePtr = anna::diameter::codec::functions::findAVP(avpsDB, AVPID__Result_Code);
63
64   if(resultCodePtr == NULL)
65     throw anna::RuntimeException("Result-Code AVP not found in DataBlock provided", ANNA_FILE_LOCATION);
66
67   // Decoded avp information:
68   AvpId _id;
69   char _flags;
70   int _length;
71   std::string _data;
72   anna::diameter::codec::functions::decodeAVP(resultCodePtr, _id, _flags, _length, _data);
73   // Result:
74   U32 result = DECODE4BYTES_INDX_VALUETYPE(_data, 0, U32);
75   return result;
76 }
77
78
79
80 void anna::diameter::helpers::base::functions::decodeSessionId(const std::string &sessionId,
81     std::string &diameterIdentity, U32 &high, U32 &low, std::string &optional) throw(anna::RuntimeException) {
82 // RFC 3588:
83 //
84 //      8.8.  Session-Id AVP
85 //
86 //         The Session-Id AVP (AVP Code 263) is of type UTF8String and is used
87 //         to identify a specific session (see Section 8).  All messages
88 //         pertaining to a specific session MUST include only one Session-Id AVP
89 //         and the same value MUST be used throughout the life of a session.
90 //         When present, the Session-Id SHOULD appear immediately following the
91 //         Diameter Header (see Section 3).
92 //
93 //         The Session-Id MUST be globally and eternally unique, as it is meant
94 //         to uniquely identify a user session without reference to any other
95 //         information, and may be needed to correlate historical authentication
96 //         information with accounting information.  The Session-Id includes a
97 //         mandatory portion and an implementation-defined portion; a
98 //         recommended format for the implementation-defined portion is outlined
99 //         below.
100 //
101 //         The Session-Id MUST begin with the sender's identity encoded in the
102 //         DiameterIdentity type (see Section 4.4).  The remainder of the
103 //         Session-Id is delimited by a ";" character, and MAY be any sequence
104 //         that the client can guarantee to be eternally unique; however, the
105 //         following format is recommended, (square brackets [] indicate an
106 //         optional element):
107 //
108 //         <DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]
109 //
110 //         <high 32 bits> and <low 32 bits> are decimal representations of the
111 //         high and low 32 bits of a monotonically increasing 64-bit value.  The
112 //         64-bit value is rendered in two part to simplify formatting by 32-bit
113 //         processors.  At startup, the high 32 bits of the 64-bit value MAY be
114 //         initialized to the time, and the low 32 bits MAY be initialized to
115 //         zero.  This will for practical purposes eliminate the possibility of
116 //         overlapping Session-Ids after a reboot, assuming the reboot process
117 //         takes longer than a second.  Alternatively, an implementation MAY
118 //         keep track of the increasing value in non-volatile memory.
119 //
120 //         <optional value> is implementation specific but may include a modem's
121 //         device Id, a layer 2 address, timestamp, etc.
122 //
123 //         Example, in which there is no optional value:
124 //            accesspoint7.acme.com;1876543210;523
125 //
126 //         Example, in which there is an optional value:
127 //            accesspoint7.acme.com;1876543210;523;mobile@200.1.1.88
128 //
129 //         The Session-Id is created by the Diameter application initiating the
130 //         session, which in most cases is done by the client.  Note that a
131 //         Session-Id MAY be used for both the authorization and accounting
132 //         commands of a given application.
133   anna::Tokenizer sid;
134   sid.apply(sessionId, ";");
135
136   if(sid.size() < 3)
137     throw anna::RuntimeException("Session-Id AVP has wrong format: at least three semicolons ';' must be included", ANNA_FILE_LOCATION);
138
139   anna::Tokenizer::const_iterator tok_min(sid.begin());
140   anna::Tokenizer::const_iterator tok_max(sid.end());
141   anna::Tokenizer::const_iterator tok_iter = tok_min;
142   diameterIdentity = anna::Tokenizer::data(tok_iter); tok_iter++;
143   std::string s_high = anna::Tokenizer::data(tok_iter); tok_iter++;
144   std::string s_low = anna::Tokenizer::data(tok_iter); tok_iter++;
145   optional = ((tok_iter != tok_max) ? (anna::Tokenizer::data(tok_iter)) : "");
146   high = (U32)atoi(s_high.c_str());
147   low = (U32)atoi(s_low.c_str());
148 }
149
150
151 std::string anna::diameter::helpers::base::functions::getSessionId(const anna::DataBlock & db,
152     std::string &diameterIdentity, U32 &high, U32 &low, std::string &optional) throw(anna::RuntimeException) {
153   if(db.getSize() < Message::HeaderLength)
154     throw anna::RuntimeException("Not enough bytes to cover command header length", ANNA_FILE_LOCATION);
155
156   anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
157   const char * sessionIdPtr = anna::diameter::codec::functions::findAVP(avpsDB, AVPID__Session_Id);
158
159   if(sessionIdPtr == NULL)
160     throw anna::RuntimeException("Session-Id AVP not found in DataBlock provided", ANNA_FILE_LOCATION);
161
162   // Decoded avp information:
163   AvpId _id;
164   char _flags;
165   int _length;
166   std::string result;
167   anna::diameter::codec::functions::decodeAVP(sessionIdPtr, _id, _flags, _length, result);
168   // Aditional helper data:
169   decodeSessionId(result.c_str(), diameterIdentity, high, low, optional);
170   return result;
171 }
172