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 //
9 #ifndef anna_diameter_codec_functions_hpp
10 #define anna_diameter_codec_functions_hpp
14 #include <anna/diameter/defines.hpp>
16 #include <anna/core/RuntimeException.hpp>
23 //------------------------------------------------------------------------------
24 //---------------------------------------------------------------------- #define
25 //------------------------------------------------------------------------------
27 // Diameter words are four-byte size. Store n bytes requires 1 word more than former multiple of 4:
28 #define REQUIRED_WORDS(bytes) ((bytes)/4+((((bytes)%4)!=0)?1:0 ))
44 // Used for alarms, tracing and Failed-AVP construction:
45 typedef struct parent {
48 anna::diameter::CommandId MessageId;
49 std::string MessageName;
51 std::vector<anna::diameter::AvpId> AvpsId;
52 std::vector<std::string> AvpsName;
54 void setMessage(const anna::diameter::CommandId & mid, const char *mname = NULL /* well known in validation */) throw();
55 void addAvp(const anna::diameter::AvpId & aid, const char *aname = NULL /* well known in validation */) throw();
56 std::string asString() const throw();
66 static CommandId getCommandId(const anna::DataBlock &) throw(anna::RuntimeException);
67 static ApplicationId getApplicationId(const anna::DataBlock &) throw(anna::RuntimeException);
68 static HopByHop getHopByHop(const anna::DataBlock &) throw(anna::RuntimeException);
69 static EndToEnd getEndToEnd(const anna::DataBlock &) throw(anna::RuntimeException);
71 static bool requestBit(const anna::DataBlock &) throw(anna::RuntimeException);
72 static bool proxiableBit(const anna::DataBlock &) throw(anna::RuntimeException);
73 static bool errorBit(const anna::DataBlock &) throw(anna::RuntimeException);
74 static bool potentiallyReTransmittedMessageBit(const anna::DataBlock &) throw(anna::RuntimeException);
76 static bool isRequest(const CommandId & cid) throw() { return (cid.second); }
77 static bool isRequest(const anna::DataBlock & db) throw(anna::RuntimeException) { return requestBit(db); }
79 static bool isAnswer(const CommandId & cid) throw() { return (!isRequest(cid)); }
80 static bool isAnswer(const anna::DataBlock & db) throw(anna::RuntimeException) { return (!isRequest(db)); }
85 * Decodes a Command Header. This helper cannot check boundaries. start pointer must be a valid command context.
87 * @param start Must be a valid command start (point to the command version byte).
88 * @param version Diameter version.
89 * @param length Message length.
90 * @param flags Command flags.
91 * @param id Command identification (code, request<true,false>).
92 * @param appId Application-ID.
93 * @param hbh Hop-by-Hop Identifier.
94 * @param ete End-to-End Identifier.
96 static void decodeCommandHeader(const char *start, char & version, U24 & length, char & flags, CommandId & id, int & appId, int & hbh, int & ete) throw(anna::RuntimeException);
99 * Decodes an AVP. This helper cannot check boundaries. start pointer must be a valid avp context.
101 * @param start Must be a valid avp start (point to the 32-bits avp code word).
102 * @param id Avp identification (code, vendorId).
103 * @param flags Avp flags byte.
104 * @param length Avp length (includes code, flags, length itself, vendorId if exists and data length).
105 * @param data Avp data part.
107 static void decodeAVP(const char *start, AvpId & id, char & flags, int & length, std::string & data) throw(anna::RuntimeException);
110 * Gets the next AVP pointer reference starting from a first-avp data block. It could be the first avp within
111 * a command, or within an grouped avp.
113 * @param avpsDB AVP data block buffer pointer
114 * @param avpsLen AVP data block buffer length
115 * @param start Point to start the search. Must be a valid avp start (point to the 32-bits avp code word).
117 * @return Pointer to the next AVP found. NULL if no more.
119 static const char * nextAVP(const char *avpsDB, int avpsLen, const char *start) throw(anna::RuntimeException);
122 // * Gets the next AVP pointer reference starting from a first-avp datablock. It could be the first avp within
123 // * a command, or within an grouped avp.
125 // * @param avpsDB AVPs set as datablock
126 // * @param start Point to start the search. Must be a valid avp start (point to the 32-bits avp code word).
128 // * @return Pointer to the next AVP found. NULL if no more.
130 // static const char * nextAVP(const anna::DataBlock & avpsDB, const char *start) throw(anna::RuntimeException);
133 * Gets the next AVP pointer reference within an AVPs set data block with a certain AVP identification.
135 * @param avpsDB AVP data block buffer pointer
136 * @param avpsLen AVP data block buffer length
137 * @param id Avp identification (code, vendorId).
138 * @param n Ocurrence number (first avp, second avp, etc.). 1 by default.
140 * @return Pointer to first AVP found with identification provided. NULL if not found.
142 static const char *findAVP(const char *avpsDB, int avpsLen, const diameter::AvpId & id, int n = 1) throw(anna::RuntimeException);
145 // * Gets the next AVP pointer reference within an AVPs set datablock with a certain AVP identification.
147 // * @param avpsDB AVPs set as datablock
148 // * @param id Avp identification (code, vendorId).
149 // * @param n Ocurrence number (first avp, second avp, etc.). 1 by default.
151 // * @return Pointer to first AVP found with identification provided. NULL if not found.
153 // static const char * findAVP(const anna::DataBlock & avpsDB, const AvpId & id, int n = 1) throw(anna::RuntimeException);
158 static void setHopByHop(anna::DataBlock &, HopByHop) throw(anna::RuntimeException);
159 static void setEndToEnd(anna::DataBlock &, EndToEnd) throw(anna::RuntimeException);
160 static void setPotentiallyReTransmittedMessageBit(const anna::DataBlock & db, bool activate = true) throw(anna::RuntimeException);