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/diameter/helpers/defines.hpp>
11 #include <anna/diameter/helpers/base/defines.hpp>
12 #include <anna/diameter/helpers/base/functions.hpp>
13 #include <anna/diameter/codec/Message.hpp>
14 #include <anna/diameter/codec/functions.hpp>
15 #include <anna/config/defines.hpp> // general types, decoding helpers (DECODE[2/3/4]BYTES_INDX_VALUETYPE), etc.
17 #include <anna/core/functions.hpp>
18 #include <anna/core/DataBlock.hpp>
19 #include <anna/core/util/Tokenizer.hpp>
25 using namespace anna::diameter::codec;
26 using namespace anna::diameter::helpers::base;
29 U32 anna::diameter::helpers::base::functions::getResultCode(const anna::DataBlock & db) throw(anna::RuntimeException) {
30 if(db.getSize() < Message::HeaderLength)
31 throw anna::RuntimeException("Not enough bytes to cover command header length", ANNA_FILE_LOCATION);
33 anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
34 const char * resultCodePtr = anna::diameter::codec::functions::findAVP(avpsDB, AVPID__Result_Code);
36 if(resultCodePtr == NULL)
37 throw anna::RuntimeException("Result-Code AVP not found in DataBlock provided", ANNA_FILE_LOCATION);
39 // Decoded avp information:
44 anna::diameter::codec::functions::decodeAVP(resultCodePtr, _id, _flags, _length, _data);
46 U32 result = DECODE4BYTES_INDX_VALUETYPE(_data, 0, U32);
52 void anna::diameter::helpers::base::functions::decodeSessionId(const std::string &sessionId,
53 std::string &diameterIdentity, U32 &high, U32 &low, std::string &optional) throw(anna::RuntimeException) {
56 // 8.8. Session-Id AVP
58 // The Session-Id AVP (AVP Code 263) is of type UTF8String and is used
59 // to identify a specific session (see Section 8). All messages
60 // pertaining to a specific session MUST include only one Session-Id AVP
61 // and the same value MUST be used throughout the life of a session.
62 // When present, the Session-Id SHOULD appear immediately following the
63 // Diameter Header (see Section 3).
65 // The Session-Id MUST be globally and eternally unique, as it is meant
66 // to uniquely identify a user session without reference to any other
67 // information, and may be needed to correlate historical authentication
68 // information with accounting information. The Session-Id includes a
69 // mandatory portion and an implementation-defined portion; a
70 // recommended format for the implementation-defined portion is outlined
73 // The Session-Id MUST begin with the sender's identity encoded in the
74 // DiameterIdentity type (see Section 4.4). The remainder of the
75 // Session-Id is delimited by a ";" character, and MAY be any sequence
76 // that the client can guarantee to be eternally unique; however, the
77 // following format is recommended, (square brackets [] indicate an
80 // <DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>]
82 // <high 32 bits> and <low 32 bits> are decimal representations of the
83 // high and low 32 bits of a monotonically increasing 64-bit value. The
84 // 64-bit value is rendered in two part to simplify formatting by 32-bit
85 // processors. At startup, the high 32 bits of the 64-bit value MAY be
86 // initialized to the time, and the low 32 bits MAY be initialized to
87 // zero. This will for practical purposes eliminate the possibility of
88 // overlapping Session-Ids after a reboot, assuming the reboot process
89 // takes longer than a second. Alternatively, an implementation MAY
90 // keep track of the increasing value in non-volatile memory.
92 // <optional value> is implementation specific but may include a modem's
93 // device Id, a layer 2 address, timestamp, etc.
95 // Example, in which there is no optional value:
96 // accesspoint7.acme.com;1876543210;523
98 // Example, in which there is an optional value:
99 // accesspoint7.acme.com;1876543210;523;mobile@200.1.1.88
101 // The Session-Id is created by the Diameter application initiating the
102 // session, which in most cases is done by the client. Note that a
103 // Session-Id MAY be used for both the authorization and accounting
104 // commands of a given application.
106 sid.apply(sessionId, ";");
109 throw anna::RuntimeException("Session-Id AVP has wrong format: at least three semicolons ';' must be included", ANNA_FILE_LOCATION);
111 anna::Tokenizer::const_iterator tok_min(sid.begin());
112 anna::Tokenizer::const_iterator tok_max(sid.end());
113 anna::Tokenizer::const_iterator tok_iter = tok_min;
114 diameterIdentity = anna::Tokenizer::data(tok_iter); tok_iter++;
115 std::string s_high = anna::Tokenizer::data(tok_iter); tok_iter++;
116 std::string s_low = anna::Tokenizer::data(tok_iter); tok_iter++;
117 optional = ((tok_iter != tok_max) ? (anna::Tokenizer::data(tok_iter)) : "");
118 high = (U32)atoi(s_high.c_str());
119 low = (U32)atoi(s_low.c_str());
123 std::string anna::diameter::helpers::base::functions::getSessionId(const anna::DataBlock & db,
124 std::string &diameterIdentity, U32 &high, U32 &low, std::string &optional) throw(anna::RuntimeException) {
125 if(db.getSize() < Message::HeaderLength)
126 throw anna::RuntimeException("Not enough bytes to cover command header length", ANNA_FILE_LOCATION);
128 anna::DataBlock avpsDB(db.getData() + Message::HeaderLength, db.getSize() - Message::HeaderLength);
129 const char * sessionIdPtr = anna::diameter::codec::functions::findAVP(avpsDB, AVPID__Session_Id);
131 if(sessionIdPtr == NULL)
132 throw anna::RuntimeException("Session-Id AVP not found in DataBlock provided", ANNA_FILE_LOCATION);
134 // Decoded avp information:
139 anna::diameter::codec::functions::decodeAVP(sessionIdPtr, _id, _flags, _length, result);
140 // Aditional helper data:
141 decodeSessionId(result.c_str(), diameterIdentity, high, low, optional);