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 : // Project
41 : #include <ert/diametercodec/stack/Avp.hpp>
42 : #include <ert/diametercodec/stack/AvpRule.hpp>
43 : #include <ert/diametercodec/stack/Dictionary.hpp>
44 :
45 : namespace ert {
46 : namespace diametercodec {
47 : namespace stack {
48 :
49 : assign_enum(AvpRule::Presence) = {"Fixed", "Mandatory", "Optional", nullptr /* list end indicator */};
50 :
51 33446 : void AvpRule::setQual(const std::string &q) {
52 33446 : const char *asterisk = strstr(q.c_str(), "*");
53 :
54 33446 : if ((q != "") && (asterisk == nullptr)) throw std::runtime_error("Non-empty qualifier must contain '*'");
55 :
56 33446 : qual_ = q;
57 33446 : }
58 :
59 3 : std::string AvpRule::getAvpName(void) const {
60 3 : const Avp *avp = dictionary_->getAvp(avp_id_);
61 3 : return avp->getName();
62 : }
63 :
64 1 : bool AvpRule::isAny(void) const {
65 1 : const Avp *avp = dictionary_->getAvp(avp_id_);
66 1 : const Format *format = dictionary_->getFormat(avp->getFormatName());
67 1 : return format->isAny();
68 : }
69 :
70 1 : int AvpRule::getQualMin(void) const {
71 1 : if (qual_ == "") {
72 0 : if (isFixed() || isMandatory()) return 1;
73 :
74 0 : if (isOptional()) return 0;
75 : }
76 :
77 : // Asterisk location
78 1 : const char *c_qual = qual_.c_str();
79 1 : int asterisk_pos = strstr(c_qual, "*") - c_qual;
80 :
81 : // '*', '*y'
82 1 : if (asterisk_pos == 0) return 0;
83 :
84 : // 'x*', 'x*y'
85 1 : std::string min = qual_.substr(0, asterisk_pos); // 'x'
86 1 : return (atoi(min.c_str()));
87 1 : }
88 :
89 1 : int AvpRule::getQualMax(void) const {
90 1 : if (qual_ == "") return 1;
91 :
92 : // Asterisk location
93 1 : const char *c_qual = qual_.c_str();
94 1 : int asterisk_pos = strstr(c_qual, "*") - c_qual;
95 :
96 : // '*', 'x*'
97 1 : if (asterisk_pos == (qual_.size() - 1)) return -1; // inf
98 :
99 : // '*y', 'x*y'
100 0 : std::string max = qual_.substr(asterisk_pos + 1, qual_.size() - asterisk_pos - 1); // 'y'
101 0 : return (atoi(max.c_str()));
102 0 : }
103 :
104 1 : nlohmann::json AvpRule::asJson(void) const {
105 1 : nlohmann::json result;
106 :
107 1 : if (dictionary_->getAvp(avp_id_)) {
108 1 : result["type"] = Presence::asText(presence_);
109 1 : if (!qual_.empty()) result["qual"] = qual_;
110 1 : result["name"] = getAvpName();
111 : }
112 :
113 1 : return result;
114 0 : }
115 :
116 : } // namespace stack
117 : } // namespace diametercodec
118 : } // namespace ert
|