Line data Source code
1 : /*
2 : ________________________________________________________________________
3 : | |
4 : | _ _ _ _ |
5 : | | (_) | | | | |
6 : | __| |_ __ _ _ __ ___ ___| |_ ___ _ __ ___ ___ __| | ___ ___ |
7 : | / _` | |/ _` | '_ ` _ \ / _ \ __/ _ \ '__/ __/ _ \ / _` |/ _ \/ __| |
8 : | | (_| | | (_| | | | | | | __/ || __/ | | (_| (_) | (_| | __/ (__ |
9 : | \__,_|_|\__,_|_| |_| |_|\___|\__\___|_| \___\___/ \__,_|\___|\___| |
10 : | |
11 : |________________________________________________________________________|
12 :
13 : C++ CODEC FOR DIAMETER PROTOCOL (RFC 6733)
14 : Version 0.0.z
15 : https://github.com/testillano/diametercodec
16 :
17 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
18 : SPDX-License-Identifier: MIT
19 : Copyright (c) 2021 Eduardo Ramos
20 :
21 : Permission is hereby granted, free of charge, to any person obtaining a copy
22 : of this software and associated documentation files (the "Software"), to deal
23 : in the Software without restriction, including without limitation the rights
24 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 : copies of the Software, and to permit persons to whom the Software is
26 : furnished to do so, subject to the following conditions:
27 :
28 : The above copyright notice and this permission notice shall be included in all
29 : copies or substantial portions of the Software.
30 :
31 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 : SOFTWARE.
38 : */
39 :
40 :
41 : // Project
42 : #include <ert/diametercodec/stack/Dictionary.hpp>
43 : #include <ert/diametercodec/stack/Avp.hpp>
44 : #include <ert/diametercodec/stack/AvpRule.hpp>
45 :
46 :
47 : namespace ert
48 : {
49 : namespace diametercodec
50 : {
51 : namespace stack
52 : {
53 :
54 : assign_enum(AvpRule::Presence) = { "Fixed", "Mandatory", "Optional", nullptr /* list end indicator */};
55 :
56 5737 : void AvpRule::setQual(const std::string & q) {
57 5737 : const char *asterisk = strstr(q.c_str(), "*");
58 :
59 5737 : if((q != "") && (asterisk == nullptr))
60 0 : throw std::runtime_error("Non-empty qualifier must contain '*'");
61 :
62 5737 : qual_ = q;
63 5737 : }
64 :
65 3 : std::string AvpRule::getAvpName(void) const {
66 3 : const Avp * avp = dictionary_->getAvp(avp_id_);
67 3 : return avp->getName();
68 : }
69 :
70 1 : bool AvpRule::isAny(void) const {
71 1 : const Avp * avp = dictionary_->getAvp(avp_id_);
72 1 : const Format * format = dictionary_->getFormat(avp->getFormatName());
73 1 : return format->isAny();
74 : }
75 :
76 1 : int AvpRule::getQualMin(void) const {
77 1 : if(qual_ == "") {
78 0 : if(isFixed() || isMandatory()) return 1;
79 :
80 0 : if(isOptional()) return 0;
81 : }
82 :
83 : // Asterisk location
84 1 : const char * c_qual = qual_.c_str();
85 1 : int asterisk_pos = strstr(c_qual, "*") - c_qual;
86 :
87 : // '*', '*y'
88 1 : if(asterisk_pos == 0) return 0;
89 :
90 : // 'x*', 'x*y'
91 1 : std::string min = qual_.substr(0, asterisk_pos); // 'x'
92 1 : return (atoi(min.c_str()));
93 1 : }
94 :
95 1 : int AvpRule::getQualMax(void) const {
96 1 : if(qual_ == "") return 1;
97 :
98 : // Asterisk location
99 1 : const char * c_qual = qual_.c_str();
100 1 : int asterisk_pos = strstr(c_qual, "*") - c_qual;
101 :
102 : // '*', 'x*'
103 1 : if(asterisk_pos == (qual_.size() - 1)) return -1; // inf
104 :
105 : // '*y', 'x*y'
106 0 : std::string max = qual_.substr(asterisk_pos + 1, qual_.size() - asterisk_pos - 1); // 'y'
107 0 : return (atoi(max.c_str()));
108 0 : }
109 :
110 1 : nlohmann::json AvpRule::asJson(void) const {
111 1 : nlohmann::json result;
112 :
113 1 : if (dictionary_->getAvp(avp_id_)) {
114 1 : result["type"] = Presence::asText(presence_);
115 1 : if (!qual_.empty()) result["qual"] = qual_;
116 1 : result["name"] = getAvpName();
117 : }
118 :
119 1 : return result;
120 0 : }
121 :
122 : }
123 : }
124 : }
125 :
|