1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 #include <anna/diameter/stack/Engine.hpp>
40 #include <anna/xml/xml.hpp>
41 #include <anna/core/functions.hpp>
42 #include <anna/core/tracing/Logger.hpp>
44 #include <anna/core/functions.hpp>
51 // libxml2 Parser doesn't support default attribute value retrieving:
52 // <!ATTLIST avp name CDATA #REQUIRED code CDATA #REQUIRED vendor-name CDATA #IMPLIED may-encrypt (yes | no) \"no\" v-bit (must | may | shouldnot | mustnot) \"mustnot\" m-bit (must | may | shouldnot | mustnot) \"may\" p-bit (must | may | shouldnot | mustnot) \"may\">\n\
53 // This dtd sintax will be replaced by #IMPLIED attributes.
59 const char *StackDTD = "\
60 <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
61 <!-- Diameter dictionary DTD -->\n\
63 <!-- <!ELEMENT dictionary (format*, vendor+, avp+, command+)> allowed empty dictionary load: diameter base protocol would be harcoded -->\n\
64 <!ELEMENT dictionary (format*, vendor*, avp*, command*)>\n\
65 <!ATTLIST dictionary name CDATA #REQUIRED>\n\
67 Dictionary definitions\n\
69 name: Dictionary name\n\
72 <!ELEMENT format EMPTY>\n\
73 <!ATTLIST format name CDATA #REQUIRED parent-type ( OctetString | Integer32 | Integer64 | Unsigned32 | Unsigned64 | Float32 | Float64 ) #REQUIRED>\n\
77 name: Format type unique name\n\
78 parent-type: Format parent type within diameter basics: OctetString, Integer32, Integer64, Unsigned32, Unsigned64, Float32, Float64\n\
81 <!ELEMENT vendor EMPTY>\n\
82 <!ATTLIST vendor name CDATA #REQUIRED code CDATA #REQUIRED>\n\
86 name: Vendor unique name\n\
87 code: Vendor numeric code\n\
90 <!ELEMENT avp (single | grouped)>\n\
91 <!ATTLIST avp name CDATA #REQUIRED code CDATA #REQUIRED vendor-name CDATA #IMPLIED may-encrypt (yes | no) #IMPLIED v-bit (must | may | shouldnot | mustnot) #IMPLIED m-bit (must | may | shouldnot | mustnot) #IMPLIED p-bit (must | may | shouldnot | mustnot) #IMPLIED>\n\
95 name: Avp unique name\n\
96 code: Avp numeric code\n\
97 vendor-name: Avp vendor name with vendor-id value. Gets IETF (zero value) when missing\n\
98 may-encrypt: Avp data may be encrypted (yes, no). By default is 'no'\n\
99 v-bit: Avp vendor-specific bit rule (must, may, shouldnot, mustnot). By default is 'mustnot'\n\
100 m-bit: Avp mandatory bit rule (must, may, shouldnot, mustnot). By default is 'may'\n\
101 p-bit: Avp protected (encryption) bit rule (must, may, shouldnot, mustnot). By default is 'may'\n\
104 <!ELEMENT single (label*)>\n\
105 <!ATTLIST single format-name CDATA #REQUIRED enum CDATA #IMPLIED>\n\
107 Non-grouped avp struct\n\
109 format-name: Avp format type name (RFC3588: 'OctetString, Integer32, Integer64, Unsigned32, Unsigned64, Float32, Float64, Address, Time, UTF8String, DiameterIdentity, DiameterURI, Enumerated, IPFilterRule, QoSFilterRule' or application format data type names)\n\
110 enum: Enum values restriction for enumerated-format avp: '[i][,[j]][,[m-n]...]'\n\
113 <!ELEMENT label EMPTY>\n\
114 <!ATTLIST label data CDATA #REQUIRED alias CDATA #REQUIRED>\n\
116 Mostly used with enumerated-type avps\n\
118 data: Specific avp data\n\
119 alias: Data-linked name\n\
122 <!ELEMENT grouped (avprule+)>\n\
124 Grouped avp struct\n\
127 <!ELEMENT avprule EMPTY>\n\
128 <!ATTLIST avprule id CDATA #REQUIRED type (Fixed | Mandatory | Optional) #REQUIRED qual CDATA #IMPLIED>\n\
130 Avp rule definition\n\
132 id: Avp name reference. Generic AVP is harcoded with 'AVP' name\n\
133 type: Avp type (Fixed, Mandatory, Optional)\n\
134 qual: Avp qualifier: *:0-inf; x*:x-inf; *x:0-x; x*y:x-y; missing:1(Fixed, Mandatory), 0-1(Optional)\n\
137 <!ELEMENT command (avprule*)>\n\
138 <!ATTLIST command name CDATA #REQUIRED code CDATA #REQUIRED type (Request | Answer) #REQUIRED>\n\
142 name: Command unique name\n\
143 code: Command numeric code\n\
144 type: Command type (Request, Answer)\n\
154 //------------------------------------------------------------------------------
155 //------------------------------------------------------------- Engine::Engine()
156 //------------------------------------------------------------------------------
157 anna::diameter::stack::Engine::Engine(void) {
158 anna::xml::functions::initialize();
159 a_dtd.initialize(StackDTD);
163 //------------------------------------------------------------------------------
164 //------------------------------------------------------ Engine::getDictionary()
165 //------------------------------------------------------------------------------
166 const anna::diameter::stack::Dictionary * anna::diameter::stack::Engine::getDictionary(int stackId) const throw() {
167 const Dictionary * result = NULL;
168 const_stack_iterator it = a_stacks.find(stackId);
170 if(it != stack_end()) result = (*it).second;
176 //------------------------------------------------------------------------------
177 //----------------------------------------------------------- Engine::asString()
178 //------------------------------------------------------------------------------
179 std::string anna::diameter::stack::Engine::asString(void) const throw() {
184 trace = "No diameter dictionaries found";
186 int numberOfStacks = stack_size();
187 trace = ((numberOfStacks > 1) ? "Multi-stack " : "Mono-stack ");
188 trace += "configuration, ";
189 trace += anna::functions::entriesAsString(numberOfStacks);
193 for(const_stack_iterator it = stack_begin(); it != stack_end(); it++) {
194 std::string title = "Diameter stack id = ";
195 title += anna::functions::asString((*it).first);
196 trace += anna::functions::highlightJustify(title);
197 trace += (*it).second->asString(); trace += "\n";
206 //------------------------------------------------------------------------------
207 //------------------------------------------------- Engine::registerDictionary()
208 //------------------------------------------------------------------------------
209 anna::diameter::stack::Dictionary * anna::diameter::stack::Engine::registerDictionary(int stackId, Dictionary *dictionary) throw(anna::RuntimeException) {
210 Dictionary * result = const_cast<Dictionary *>(getDictionary(stackId));
213 throw anna::RuntimeException("Cannot provide a NULL dictionary. It must be previously allocated", ANNA_FILE_LOCATION);
215 if(result) { // if exists, launch exception
216 throw anna::RuntimeException("Such provided stack id has already been created. Removes it before call this method", ANNA_FILE_LOCATION);
217 } else { // new stack
218 a_stacks[stackId] = dictionary; // no need for singleton destructor
219 const_stack_iterator it = a_stacks.find(stackId);
220 //result = (Dictionary *)(*it).second;
227 //------------------------------------------------------------------------------
228 //--------------------------------------------------- Engine::createDictionary()
229 //------------------------------------------------------------------------------
230 anna::diameter::stack::Dictionary * anna::diameter::stack::Engine::createDictionary(int stackId, const std::string & xmlPathFile) throw(anna::RuntimeException) {
231 Dictionary * result = const_cast<Dictionary *>(getDictionary(stackId));
233 if(result) // if exists, launch exception
234 throw anna::RuntimeException("Such provided stack id has already been created. Removes it before call this method", ANNA_FILE_LOCATION);
236 // Register a new dictionary:
237 result = registerDictionary(stackId, new Dictionary());
239 if(xmlPathFile != "") {
241 result->load(xmlPathFile);
242 } catch(anna::RuntimeException& ex) {
252 void anna::diameter::stack::Engine::loadDictionary(const std::vector<int> & stacks, const std::string & xmlPathFile) throw(anna::RuntimeException) {
253 std::vector<int>::const_iterator it;
256 if(xmlPathFile == "")
257 throw anna::RuntimeException("Empty xml path file provided", ANNA_FILE_LOCATION);
259 for(it = stacks.begin(); it != stacks.end(); it++) {
260 d = const_cast<Dictionary*>(getDictionary(*it));
262 if(d) d->load(xmlPathFile);
265 std::string trace = "Cannot load dictionary '";
266 trace += xmlPathFile;
267 trace += "' over stack id '";
268 trace += anna::functions::asString(*it);
269 trace += "' because it doesn't exists";
270 anna::Logger::warning(trace, ANNA_FILE_LOCATION);
276 void anna::diameter::stack::Engine::loadDictionary(const std::string & xmlPathFile) throw(anna::RuntimeException) {
279 if(xmlPathFile == "")
280 throw anna::RuntimeException("Empty xml path file provided", ANNA_FILE_LOCATION);
282 for(const_stack_iterator it = stack_begin(); it != stack_end(); it++) {
283 d = const_cast<Dictionary*>(getDictionary((*it).first));
284 d->load(xmlPathFile);
289 //------------------------------------------------------------------------------
290 //-------------------------------------------------------- Engine::removeStack()
291 //------------------------------------------------------------------------------
292 void anna::diameter::stack::Engine::removeStack(int stackId) throw() {
293 stack_iterator it = a_stacks.find(stackId);
295 if(it != stack_end()) { // if exists, clear
296 //(*it).second->clear();
298 } else { // new stack
300 std::string trace = "Cannot remove stack id '";
301 trace += anna::functions::asString(stackId);
302 trace += "' because it doesn't exists";
303 anna::Logger::warning(trace, ANNA_FILE_LOCATION);