1 // Copyright (C) 2014-2020 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6 #ifndef VSOMEIP_V3_POLICY_HPP_
7 #define VSOMEIP_V3_POLICY_HPP_
8
9 #include <cstring>
10 #include <map>
11 #include <mutex>
12 #include <utility>
13 #include <vector>
14
15 #include <boost/icl/interval_map.hpp>
16 #include <boost/icl/interval_set.hpp>
17
18 #include <vsomeip/constants.hpp>
19 #include <vsomeip/primitive_types.hpp>
20 #include <vsomeip/internal/logger.hpp>
21
22 namespace vsomeip_v3 {
23
24 template<typename T_>
get_bounds(const boost::icl::discrete_interval<T_> & _interval,T_ & _lower,T_ & _upper)25 void get_bounds(const boost::icl::discrete_interval<T_> &_interval,
26 T_ &_lower, T_ &_upper) {
27
28 T_ its_lower, its_upper;
29
30 its_lower = _interval.lower();
31 its_upper = _interval.upper();
32
33 switch (_interval.bounds().bits()) {
34 case boost::icl::interval_bounds::static_open:
35 its_lower++;
36 its_upper--;
37 break;
38 case boost::icl::interval_bounds::static_left_open:
39 its_lower++;
40 break;
41 case boost::icl::interval_bounds::static_right_open:
42 its_upper--;
43 break;
44 default:
45 ;
46 }
47
48 _lower = its_lower;
49 _upper = its_upper;
50 }
51
52 struct policy {
policyvsomeip_v3::policy53 policy() : allow_who_(false), allow_what_(false) {};
54
55 // Returns true if the policy is defined for single uid/gid pair.
56 // uid & gid are copied to the arguments. Otherwise, returns false.
57 bool get_uid_gid(uid_t &_uid, gid_t &_gid) const;
58
59 bool deserialize_uid_gid(const byte_t * &_data, uint32_t &_size,
60 uid_t &_uid, gid_t &_gid) const;
61 bool deserialize(const byte_t * &_data, uint32_t &_size);
62 bool serialize(std::vector<byte_t> &_data) const;
63
64 void print() const;
65
66 // Members
67 boost::icl::interval_map<uid_t,
68 boost::icl::interval_set<gid_t> > credentials_;
69 bool allow_who_;
70
71 boost::icl::interval_map<service_t,
72 boost::icl::interval_map<instance_t,
73 boost::icl::interval_set<method_t> > > requests_;
74 boost::icl::interval_map<service_t,
75 boost::icl::interval_set<instance_t> > offers_;
76 bool allow_what_;
77
78 mutable std::mutex mutex_;
79
80 private:
81 bool deserialize_ids(const byte_t * &_data, uint32_t &_size,
82 boost::icl::interval_map<uint16_t,
83 boost::icl::interval_set<uint16_t> > &_ids) const;
84 bool deserialize_id_item_list(const byte_t * &_data, uint32_t &_size,
85 boost::icl::interval_set<uint16_t> &_intervals) const;
86 bool deserialize_id_item(const byte_t * &_data, uint32_t &_size,
87 uint16_t &_low, uint16_t &_high) const;
88
89 bool deserialize_u32(const byte_t * &_data, uint32_t &_size,
90 uint32_t &_value) const;
91 bool deserialize_u16(const byte_t * &_data, uint32_t &_size,
92 uint16_t &_value) const;
93
94 bool serialize_uid_gid(std::vector<byte_t> &_data) const;
95 void serialize_interval_set(
96 const boost::icl::interval_set<uint16_t> &_intervals,
97 std::vector<byte_t> &_data) const;
98 void serialize_interval(
99 const boost::icl::discrete_interval<uint16_t> &_interval,
100 std::vector<byte_t> &_data) const;
101
102 void serialize_u32(uint32_t _value, std::vector<byte_t> &_data) const;
103 void serialize_u32_at(uint32_t _value, std::vector<byte_t> &_data,
104 size_t _pos) const;
105 void serialize_u16(uint16_t _value, std::vector<byte_t> &_data) const;
106 };
107
108 } // namespace vsomeip_v3
109
110 #endif // VSOMEIP_V3_POLICY_HPP_
111