Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / diameter / codec / functions.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef anna_diameter_codec_functions_hpp
10 #define anna_diameter_codec_functions_hpp
11
12
13 // Local
14 #include <anna/diameter/defines.hpp>
15
16 #include <anna/core/RuntimeException.hpp>
17
18 // STL
19 #include <string>
20 #include <vector>
21
22
23 //------------------------------------------------------------------------------
24 //---------------------------------------------------------------------- #define
25 //------------------------------------------------------------------------------
26
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 ))
29
30
31
32 namespace anna {
33 class DataBlock;
34 }
35
36
37 namespace anna {
38
39 namespace diameter {
40
41 namespace codec {
42
43
44 // Used for alarms, tracing and Failed-AVP construction:
45 typedef struct parent {
46
47   // Used on decoding:
48   anna::diameter::CommandId MessageId;
49   std::string MessageName;
50
51   std::vector<anna::diameter::AvpId> AvpsId;
52   std::vector<std::string> AvpsName;
53
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();
57
58 } parent_t;
59
60
61
62
63 struct functions {
64
65   // getters & helpers
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);
70
71   static bool isRequest(const CommandId & cid) throw() { return (cid.second); }
72   static bool isRequest(const anna::DataBlock &) throw(anna::RuntimeException);
73
74   static bool isAnswer(const CommandId & cid) throw() { return (!isRequest(cid)); }
75   static bool isAnswer(const anna::DataBlock & db) throw(anna::RuntimeException) { return (!isRequest(db)); }
76
77
78   /**
79   * Decodes a Command Header. This helper cannot check boundaries. start pointer must be a valid command context.
80   *
81   * @param start Must be a valid command start (point to the command version byte).
82   * @param version Diameter version.
83   * @param length Message length.
84   * @param flags Command flags.
85   * @param id Command identification (code, request<true,false>).
86   * @param appId Application-ID.
87   * @param hbh Hop-by-Hop Identifier.
88   * @param ete End-to-End Identifier.
89   */
90   static void decodeCommandHeader(const char *start, char & version, U24 & length, char & flags, CommandId & id, int & appId, int & hbh, int & ete) throw(anna::RuntimeException);
91
92   /**
93   * Decodes an AVP. This helper cannot check boundaries. start pointer must be a valid avp context.
94   *
95   * @param start Must be a valid avp start (point to the 32-bits avp code word).
96   * @param id Avp identification (code, vendorId).
97   * @param flags Avp flags byte.
98   * @param length Avp length (includes code, flags, length itself, vendorId if exists and data length).
99   * @param data Avp data part.
100   */
101   static void decodeAVP(const char *start, AvpId & id, char & flags, int & length, std::string & data) throw(anna::RuntimeException);
102
103   /**
104   * Gets the next AVP pointer reference starting from a first-avp datablock. It could be the first avp within
105   * a command, or within an grouped avp.
106   *
107   * @param avpsDB AVPs set as datablock
108   * @param start Point to start the search. Must be a valid avp start (point to the 32-bits avp code word).
109   *
110   * @return Pointer to the next AVP found. NULL if no more.
111   */
112   static const char * nextAVP(const anna::DataBlock & avpsDB, const char *start) throw(anna::RuntimeException);
113
114   /**
115   * Gets the next AVP pointer reference within an AVPs set datablock with a certain AVP identification.
116   *
117   * @param avpsDB AVPs set as datablock
118   * @param id Avp identification (code, vendorId).
119   * @param n Ocurrence number (first avp, second avp, etc.)
120   *
121   * @return Pointer to first AVP found with identification provided. NULL if not found.
122   */
123   static const char * findAVP(const anna::DataBlock & avpsDB, const AvpId & id, int n = 1) throw(anna::RuntimeException);
124
125
126
127   // modifiers
128   static void setHopByHop(anna::DataBlock &, HopByHop) throw(anna::RuntimeException);
129   static void setEndToEnd(anna::DataBlock &, EndToEnd) throw(anna::RuntimeException);
130 };
131
132
133 }
134 }
135 }
136
137
138 #endif
139