e8750db559fb16b2c81ea03737645c6cadc8c296
[ric-app/admin.git] / src / protector-plugin / NetworkProtector.h
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20
21 #ifndef NETWORKPROTECTOR_H
22 #define NETWORKPROTECTOR_H
23
24 #include "sliding_window.hpp"
25 #include <mutex>
26 #include <sgnb_addition_request.hpp>  // to decode the X2AP payload
27 #include <sgnb_addition_response.hpp> // to respond
28
29 #ifdef __GNUC__
30 #define likely(x)  __builtin_expect((x), 1)
31 #define unlikely(x) __builtin_expect((x), 0)
32 #else
33 #define likely(x) (x)
34 #define unlikely(x) (x)
35 #endif
36
37 extern bool report_mode_only;
38
39 class protector
40 {
41 public: 
42   
43   protector( bool , int , int , double , bool rep=true);
44
45   bool operator()(unsigned char *, size_t , unsigned char *, size_t *);
46   
47   bool configure(bool enforce, int windowSize_, int threshold_, double blockRate_);
48   void clear();
49   bool selectiveBlock();
50
51   unsigned long int get_requests(void) const;
52   unsigned long int get_rejects(void) const;
53   std::string get_error(void) { return error_string;};
54   
55 private:
56   bool m_enforce; // whether to execute logic or not 
57   int m_counter;                        // count the # of attaching access
58   int m_windowSize;                     // time in seconds window for the # of counts
59   int m_threshold;                      // count above which we start enforcing if enforce set
60   double m_blockRate;           // % of rejecting rate for counter > threshold
61   time_t m_timeWindow;      // time active window started
62   unsigned long int m_req; // number of requests
63   unsigned long int m_rej; // number of rejects
64
65   std::unique_ptr<sliding_window> m_window_ref;
66   std::unique_ptr<std::mutex> m_access;
67   sgnb_addition_helper sgnb_data;
68   sgnb_addition_request sgnb_req;
69   sgnb_addition_response sgnb_resp;
70   
71   std::string error_string;
72   bool report_mode_only;
73 };
74
75 #endif