49ab3797a3f2e83bac0d50a83730b8fd4bb654b0
[anna.git] / include / anna / diameter / codec / functions.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
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 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.
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 #ifndef anna_diameter_codec_functions_hpp
38 #define anna_diameter_codec_functions_hpp
39
40
41 // Local
42 #include <anna/diameter/defines.hpp>
43
44 #include <anna/core/RuntimeException.hpp>
45
46 // STL
47 #include <string>
48 #include <vector>
49
50
51 //------------------------------------------------------------------------------
52 //---------------------------------------------------------------------- #define
53 //------------------------------------------------------------------------------
54
55 // Diameter words are four-byte size. Store n bytes requires 1 word more than former multiple of 4:
56 #define REQUIRED_WORDS(bytes)  ((bytes)/4+((((bytes)%4)!=0)?1:0 ))
57
58
59
60 namespace anna {
61 class DataBlock;
62 }
63
64
65 namespace anna {
66
67 namespace diameter {
68
69 namespace codec {
70
71
72 // Used for alarms, tracing and Failed-AVP construction:
73 typedef struct parent {
74
75   // Used on decoding:
76   anna::diameter::CommandId MessageId;
77   std::string MessageName;
78
79   std::vector<anna::diameter::AvpId> AvpsId;
80   std::vector<std::string> AvpsName;
81
82   void setMessage(const anna::diameter::CommandId & mid, const char *mname = NULL /* well known in validation */) throw();
83   void addAvp(const anna::diameter::AvpId & aid, const char *aname = NULL /* well known in validation */) throw();
84   std::string asString() const throw();
85
86 } parent_t;
87
88
89
90
91 struct functions {
92
93   // getters & helpers
94   static CommandId getCommandId(const anna::DataBlock &) throw(anna::RuntimeException);
95   static ApplicationId getApplicationId(const anna::DataBlock &) throw(anna::RuntimeException);
96   static HopByHop getHopByHop(const anna::DataBlock &) throw(anna::RuntimeException);
97   static EndToEnd getEndToEnd(const anna::DataBlock &) throw(anna::RuntimeException);
98
99   static bool isRequest(const CommandId & cid) throw() { return (cid.second); }
100   static bool isRequest(const anna::DataBlock &) throw(anna::RuntimeException);
101
102   static bool isAnswer(const CommandId & cid) throw() { return (!isRequest(cid)); }
103   static bool isAnswer(const anna::DataBlock & db) throw(anna::RuntimeException) { return (!isRequest(db)); }
104
105
106   /**
107   * Decodes a Command Header. This helper cannot check boundaries. start pointer must be a valid command context.
108   *
109   * @param start Must be a valid command start (point to the command version byte).
110   * @param version Diameter version.
111   * @param length Message length.
112   * @param flags Command flags.
113   * @param id Command identification (code, request<true,false>).
114   * @param appId Application-ID.
115   * @param hbh Hop-by-Hop Identifier.
116   * @param ete End-to-End Identifier.
117   */
118   static void decodeCommandHeader(const char *start, char & version, U24 & length, char & flags, CommandId & id, int & appId, int & hbh, int & ete) throw(anna::RuntimeException);
119
120   /**
121   * Decodes an AVP. This helper cannot check boundaries. start pointer must be a valid avp context.
122   *
123   * @param start Must be a valid avp start (point to the 32-bits avp code word).
124   * @param id Avp identification (code, vendorId).
125   * @param flags Avp flags byte.
126   * @param length Avp length (includes code, flags, length itself, vendorId if exists and data length).
127   * @param data Avp data part.
128   */
129   static void decodeAVP(const char *start, AvpId & id, char & flags, int & length, std::string & data) throw(anna::RuntimeException);
130
131   /**
132   * Gets the next AVP pointer reference starting from a first-avp datablock. It could be the first avp within
133   * a command, or within an grouped avp.
134   *
135   * @param avpsDB AVPs set as datablock
136   * @param start Point to start the search. Must be a valid avp start (point to the 32-bits avp code word).
137   *
138   * @return Pointer to the next AVP found. NULL if no more.
139   */
140   static const char * nextAVP(const anna::DataBlock & avpsDB, const char *start) throw(anna::RuntimeException);
141
142   /**
143   * Gets the next AVP pointer reference within an AVPs set datablock with a certain AVP identification.
144   *
145   * @param avpsDB AVPs set as datablock
146   * @param id Avp identification (code, vendorId).
147   * @param n Ocurrence number (first avp, second avp, etc.)
148   *
149   * @return Pointer to first AVP found with identification provided. NULL if not found.
150   */
151   static const char * findAVP(const anna::DataBlock & avpsDB, const AvpId & id, int n = 1) throw(anna::RuntimeException);
152
153
154
155   // modifiers
156   static void setHopByHop(anna::DataBlock &, HopByHop) throw(anna::RuntimeException);
157   static void setEndToEnd(anna::DataBlock &, EndToEnd) throw(anna::RuntimeException);
158 };
159
160
161 }
162 }
163 }
164
165
166 #endif
167