First release of open-appsec source code

This commit is contained in:
roybarda
2022-10-26 19:33:19 +03:00
parent 3883109caf
commit a883352f79
1353 changed files with 276290 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ATTACHMENT_TYPES_H__
#define __ATTACHMENT_TYPES_H__
#ifdef __cplusplus
enum class AttachmentType
#else // __cplusplus
enum AttachmentType
#endif
{
NGINX_ATT_ID,
PRELOAD_ATT_ID,
#ifdef __cplusplus
COUNT
#endif
};
#endif // __ATTACHMENT_TYPES_H__

View File

@@ -0,0 +1,86 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __COMPRESSION_UTILS_H__
#define __COMPRESSION_UTILS_H__
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
typedef enum CompressionUtilsDebugLevel
{
COMPRESSION_DBG_LEVEL_TRACE,
COMPRESSION_DBG_LEVEL_DEBUG,
COMPRESSION_DBG_LEVEL_INFO,
COMPRESSION_DBG_LEVEL_WARNING,
COMPRESSION_DBG_LEVEL_ERROR,
COMPRESSION_DBG_LEVEL_ASSERTION
} CompressionUtilsDebugLevel;
void resetCompressionDebugFunctionsToStandardError();
void setCompressionDebugFunction(const CompressionUtilsDebugLevel debug_level, void (*debug_function)(const char *));
typedef struct CompressionStream CompressionStream;
CompressionStream * initCompressionStream();
void finiCompressionStream(CompressionStream *compression_stream);
typedef enum CompressionType
{
NO_COMPRESSION,
GZIP,
ZLIB
} CompressionType;
typedef struct CompressionResult
{
int ok;
uint32_t num_output_bytes;
unsigned char *output;
} CompressionResult;
CompressionResult
compressData(
CompressionStream *compression_stream,
const CompressionType compression_type,
const uint32_t uncompressed_data_size,
const unsigned char *uncompressed_data,
const int is_last_chunk
);
typedef struct DecompressionResult
{
int ok;
uint32_t num_output_bytes;
unsigned char *output;
int is_last_chunk;
} DecompressionResult;
DecompressionResult
decompressData(
CompressionStream *compression_stream,
const uint32_t compressed_data_size,
const unsigned char *compressed_data
);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __COMPRESSION_UTILS_H__

View File

@@ -0,0 +1,68 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HTTP_CONFIGURATION_H__
#define __HTTP_CONFIGURATION_H__
#include <string>
#include <map>
#include <vector>
#include "cereal/archives/json.hpp"
struct DebugConfig
{
void save(cereal::JSONOutputArchive &archive) const;
void load(cereal::JSONInputArchive &archive);
bool operator==(const DebugConfig &another) const;
std::string client;
std::string server;
unsigned int port = 0;
std::string method;
std::string host;
std::string uri;
};
class HttpAttachmentConfiguration
{
public:
int init(const std::string &conf_file);
void save(cereal::JSONOutputArchive &archive) const;
void load(cereal::JSONInputArchive &archive);
bool operator==(const HttpAttachmentConfiguration &other) const;
unsigned int getNumericalValue(const std::string &key) const;
const std::string & getStringValue(const std::string &key) const;
const std::vector<std::string> & getExcludeSources() const { return exclude_sources; }
const DebugConfig & getDebugContext() const { return dbg; }
void setNumericalValue(const std::string &key, unsigned int value) { numerical_values[key] = value; }
void setStringValue(const std::string &key, const std::string &value) { string_values[key] = value; }
void setExcludeSources(const std::vector<std::string> &new_sources) { exclude_sources = new_sources; }
void setDebugContext(const DebugConfig &_dbg) { dbg = _dbg; }
private:
void loadNumericalValue(cereal::JSONInputArchive &archive, const std::string &name, unsigned int default_value);
DebugConfig dbg;
std::map<std::string, unsigned int> numerical_values;
std::map<std::string, std::string> string_values;
std::vector<std::string> exclude_sources;
std::string empty;
};
#endif // __HTTP_CONFIGURATION_H__

View File

@@ -0,0 +1,279 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __NGINX_ATTACHMENT_COMMON_H__
#define __NGINX_ATTACHMENT_COMMON_H__
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#define MAX_NGINX_UID_LEN 32
#define NUM_OF_NGINX_IPC_ELEMENTS 200
#define DEFAULT_KEEP_ALIVE_INTERVAL_MSEC 300000
#define SHARED_MEM_PATH "/dev/shm/"
#define SHARED_REGISTRATION_SIGNAL_PATH SHARED_MEM_PATH "check-point/cp-nano-attachment-registration"
#define SHARED_KEEP_ALIVE_PATH SHARED_MEM_PATH "check-point/cp-nano-attachment-registration-expiration-socket"
#define SHARED_VERDICT_SIGNAL_PATH SHARED_MEM_PATH "check-point/cp-nano-http-transaction-handler"
#define SHARED_ATTACHMENT_CONF_PATH SHARED_MEM_PATH "cp_nano_http_attachment_conf"
#define DEFAULT_STATIC_RESOURCES_PATH SHARED_MEM_PATH "static_resources"
#define INJECT_POS_IRRELEVANT -1
#define CORRUPTED_SESSION_ID 0
#define METRIC_PERIODIC_TIMEOUT 600
extern char shared_verdict_signal_path[];
extern int workers_amount_to_send;
typedef int64_t ngx_http_cp_inject_pos_t;
#ifdef __cplusplus
typedef enum class ngx_http_modification_type
#else
typedef enum ngx_http_modification_type
#endif
{
APPEND,
INJECT,
REPLACE
} ngx_http_modification_type_e;
#ifdef __cplusplus
typedef enum class ngx_http_chunk_type
#else
typedef enum ngx_http_chunk_type
#endif
{
REQUEST_START,
REQUEST_HEADER,
REQUEST_BODY,
REQUEST_END,
RESPONSE_CODE,
RESPONSE_HEADER,
RESPONSE_BODY,
RESPONSE_END,
CONTENT_LENGTH,
METRIC_DATA_FROM_PLUGIN,
HOLD_DATA,
COUNT
} ngx_http_chunk_type_e;
#ifdef __cplusplus
typedef enum class ngx_http_plugin_metric_type
#else
typedef enum ngx_http_plugin_metric_type
#endif
{
TRANSPARENTS_COUNT,
TOTAL_TRANSPARENTS_TIME,
INSPECTION_OPEN_FAILURES_COUNT,
INSPECTION_CLOSE_FAILURES_COUNT,
INSPECTION_SUCCESSES_COUNT,
INJECT_VERDICTS_COUNT,
DROP_VERDICTS_COUNT,
ACCEPT_VERDICTS_COUNT,
IRRELEVANT_VERDICTS_COUNT,
RECONF_VERDICTS_COUNT,
INSPECT_VERDICTS_COUNT,
HOLD_VERDICTS_COUNT,
AVERAGE_OVERALL_PPROCESSING_TIME_UNTIL_VERDICT,
MAX_OVERALL_PPROCESSING_TIME_UNTIL_VERDICT,
MIN_OVERALL_PPROCESSING_TIME_UNTIL_VERDICT,
AVERAGE_REQ_PPROCESSING_TIME_UNTIL_VERDICT,
MAX_REQ_PPROCESSING_TIME_UNTIL_VERDICT,
MIN_REQ_PPROCESSING_TIME_UNTIL_VERDICT,
AVERAGE_RES_PPROCESSING_TIME_UNTIL_VERDICT,
MAX_RES_PPROCESSING_TIME_UNTIL_VERDICT,
MIN_RES_PPROCESSING_TIME_UNTIL_VERDICT,
THREAD_TIMEOUT,
REG_THREAD_TIMEOUT,
REQ_HEADER_THREAD_TIMEOUT,
REQ_BODY_THREAD_TIMEOUT,
AVERAGE_REQ_BODY_SIZE_UPON_TIMEOUT,
MAX_REQ_BODY_SIZE_UPON_TIMEOUT,
MIN_REQ_BODY_SIZE_UPON_TIMEOUT,
RES_HEADER_THREAD_TIMEOUT,
RES_BODY_THREAD_TIMEOUT,
HOLD_THREAD_TIMEOUT,
AVERAGE_RES_BODY_SIZE_UPON_TIMEOUT,
MAX_RES_BODY_SIZE_UPON_TIMEOUT,
MIN_RES_BODY_SIZE_UPON_TIMEOUT,
THREAD_FAILURE,
REQ_PROCCESSING_TIMEOUT,
RES_PROCCESSING_TIMEOUT,
REQ_FAILED_TO_REACH_UPSTREAM,
REQ_FAILED_COMPRESSION_COUNT,
RES_FAILED_COMPRESSION_COUNT,
REQ_FAILED_DECOMPRESSION_COUNT,
RES_FAILED_DECOMPRESSION_COUNT,
REQ_SUCCESSFUL_COMPRESSION_COUNT,
RES_SUCCESSFUL_COMPRESSION_COUNT,
REQ_SUCCESSFUL_DECOMPRESSION_COUNT,
RES_SUCCESSFUL_DECOMPRESSION_COUNT,
CORRUPTED_ZIP_SKIPPED_SESSION_COUNT,
CPU_USAGE,
AVERAGE_VM_MEMORY_USAGE,
AVERAGE_RSS_MEMORY_USAGE,
MAX_VM_MEMORY_USAGE,
MAX_RSS_MEMORY_USAGE,
METRIC_TYPES_COUNT
} ngx_http_plugin_metric_type_e;
#ifdef __cplusplus
typedef enum class ngx_http_cp_verdict
#else
typedef enum ngx_http_cp_verdict
#endif
{
TRAFFIC_VERDICT_INSPECT,
TRAFFIC_VERDICT_ACCEPT,
TRAFFIC_VERDICT_DROP,
TRAFFIC_VERDICT_INJECT,
TRAFFIC_VERDICT_IRRELEVANT,
TRAFFIC_VERDICT_RECONF,
TRAFFIC_VERDICT_WAIT
} ngx_http_cp_verdict_e;
#ifdef __cplusplus
typedef enum class ngx_http_cp_debug_level
#else
typedef enum ngx_http_cp_debug_level
#endif
{
DBG_LEVEL_TRACE,
DBG_LEVEL_DEBUG,
DBG_LEVEL_INFO,
DBG_LEVEL_WARNING,
DBG_LEVEL_ERROR,
#ifndef __cplusplus
DBG_LEVEL_ASSERT,
#endif
DBG_LEVEL_COUNT
} ngx_http_cp_debug_level_e;
#ifdef __cplusplus
typedef enum class ngx_http_meta_data
#else
typedef enum ngx_http_meta_data
#endif
{
HTTP_PROTOCOL_SIZE,
HTTP_PROTOCOL_DATA,
HTTP_METHOD_SIZE,
HTTP_METHOD_DATA,
HOST_NAME_SIZE,
HOST_NAME_DATA,
LISTENING_ADDR_SIZE,
LISTENING_ADDR_DATA,
LISTENING_PORT,
URI_SIZE,
URI_DATA,
CLIENT_ADDR_SIZE,
CLIENT_ADDR_DATA,
CLIENT_PORT,
META_DATA_COUNT
} ngx_http_meta_data_e;
#ifdef __cplusplus
typedef enum class ngx_http_header_data
#else
typedef enum ngx_http_header_data
#endif
{
HEADER_KEY_SIZE,
HEADER_KEY_DATA,
HEADER_VAL_SIZE,
HEADER_VAL_DATA,
HEADER_DATA_COUNT
} ngx_http_header_data_e;
typedef enum ngx_http_inspection_mode
{
NON_BLOCKING_THREAD,
BLOCKING_THREAD,
NO_THREAD,
INSPECTION_MODE_COUNT
} ngx_http_inspection_mode_e;
#ifdef __cplusplus
typedef enum class ngx_web_response_type
#else
typedef enum ngx_web_response_type
#endif
{
CUSTOM_WEB_RESPONSE,
REDIRECT_WEB_RESPONSE
} ngx_web_response_type_e;
typedef struct __attribute__((__packed__)) ngx_http_cp_inject_data {
ngx_http_cp_inject_pos_t injection_pos;
ngx_http_modification_type_e mod_type;
uint16_t injection_size;
uint8_t is_header;
uint8_t orig_buff_index;
char data[0];
} ngx_http_cp_inject_data_t;
typedef struct __attribute__((__packed__)) ngx_http_cp_web_response_data {
uint8_t web_repsonse_type;
uint8_t uuid_size;
union {
struct __attribute__((__packed__)) ngx_http_cp_custom_web_response_data {
uint16_t response_code;
uint8_t title_size;
uint8_t body_size;
char data[0];
} custom_response_data;
struct __attribute__((__packed__)) ngx_http_cp_redirect_data {
uint8_t add_event_id;
uint16_t redirect_location_size;
char redirect_location[0];
} redirect_data;
} response_data;
} ngx_http_cp_web_response_data_t;
typedef union __attribute__((__packed__)) ngx_http_cp_modify_data {
ngx_http_cp_inject_data_t inject_data[0];
ngx_http_cp_web_response_data_t web_response_data[0];
} ngx_http_cp_modify_data_t;
typedef struct __attribute__((__packed__)) ngx_http_cp_reply_from_service {
uint16_t verdict;
uint32_t session_id;
uint8_t modification_count;
ngx_http_cp_modify_data_t modify_data[0];
} ngx_http_cp_reply_from_service_t;
typedef struct __attribute__((__packed__)) ngx_http_cp_request_data {
uint16_t data_type;
uint32_t session_id;
unsigned char data[0];
} ngx_http_cp_request_data_t;
typedef struct __attribute__((__packed__)) ngx_http_cp_metric_data {
uint16_t data_type;
#ifdef __cplusplus
uint64_t data[static_cast<int>(ngx_http_plugin_metric_type::METRIC_TYPES_COUNT)];
#else
uint64_t data[METRIC_TYPES_COUNT];
#endif
} ngx_http_cp_metric_data_t;
#endif // __NGINX_ATTACHMENT_COMMON_H__

View File

@@ -0,0 +1,67 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __NGINX_ATTACHMENT_UTIL__
#define __NGINX_ATTACHMENT_UTIL__
#include <stdio.h>
#include "nginx_attachment_common.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#define IP_STR_MAX_LEN 40
typedef const char * c_str;
int initAttachmentConfig(c_str conf_file);
ngx_http_inspection_mode_e getInspectionMode();
unsigned int getNumOfNginxIpcElements();
unsigned int getKeepAliveIntervalMsec();
unsigned int getDbgLevel();
int isDebugContext(c_str client, c_str server, unsigned int port, c_str method, c_str host, c_str uri);
c_str getStaticResourcesPath();
int isFailOpenMode();
unsigned int getFailOpenTimeout();
int isFailOpenHoldMode();
unsigned int getFailOpenHoldTimeout();
unsigned int getMaxSessionsPerMinute();
int isFailOpenOnSessionLimit();
unsigned int getRegistrationThreadTimeout();
unsigned int getReqProccessingTimeout();
unsigned int getReqHeaderThreadTimeout();
unsigned int getReqBodyThreadTimeout();
unsigned int getResProccessingTimeout();
unsigned int getResHeaderThreadTimeout();
unsigned int getResBodyThreadTimeout();
unsigned int getWaitingForVerdictThreadTimeout();
int isIPAddress(c_str ip_str);
int isSkipSource(c_str ip_str);
#ifdef __cplusplus
}
#endif
#endif // __NGINX_ATTACHMENT_UTIL__

View File

@@ -0,0 +1,66 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __SHMEM_IPC_H__
#define __SHMEM_IPC_H__
#include <stdint.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
typedef struct SharedMemoryIPC SharedMemoryIPC;
extern const int corrupted_shmem_error;
SharedMemoryIPC * initIpc(
const char queue_name[32],
const uint32_t user_id,
const uint32_t group_id,
int is_owner,
uint16_t num_of_queue_elem,
void (*debug_func)(int is_error, const char *func, const char *file, int line_num, const char *fmt, ...)
);
void destroyIpc(SharedMemoryIPC *ipc, int is_owner);
int sendData(SharedMemoryIPC *ipc, const uint16_t data_to_send_size, const char *data_to_send);
int
sendChunkedData(
SharedMemoryIPC *ipc,
const uint16_t *data_to_send_sizes,
const char **data_elem_to_send,
const uint8_t num_of_data_elem
);
int receiveData(SharedMemoryIPC *ipc, uint16_t *received_data_size, const char **received_data);
int popData(SharedMemoryIPC *ipc);
int isDataAvailable(SharedMemoryIPC *ipc);
void resetIpc(SharedMemoryIPC *ipc, uint16_t num_of_data_segments);
void dumpIpcMemory(SharedMemoryIPC *ipc);
int isCorruptedShmem(SharedMemoryIPC *ipc, int is_owner);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __SHMEM_IPC_H__

200
core/include/general/buffer.h Executable file
View File

@@ -0,0 +1,200 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUFFER_H__
#define __BUFFER_H__
#include <vector>
#include <string>
#include <memory>
#include "cereal/types/vector.hpp"
#include "cereal/types/memory.hpp"
#include "maybe_res.h"
#include "debug.h"
class Buffer final
{
public:
// Indication of the type of memory that is held by the system:
// OWNED - The system allocated the memory and is resposible for releasing it.
// STATIC - The memory is such that is always availabe and doesn't require releasing.
// VOLATILE - The memory was allocated outside of the system and is only availabe for the duration of the
// instance. This memory may require the system to later duplicate (and does change it to OWNED
// memeory).
enum class MemoryType
{
OWNED, STATIC, VOLATILE
};
private:
// Indication of the volatility of the memory.
// OWNED and STATIC type of memory are not volatile, and are marked NONE.
// The initial VOLATILE instance is guaranteed to have the volatile memory available while that instance exists,
// and is marked PRIMARY.
// Instances that are created based on an existing VOLATILE instance are marked as SECONDARY. They are guaranteed
// to have the memory available to thme only as long as the PRIMARY instance exists. If such SECONDARY instance
// continues to exists at the time when the PRIMARY instance is destoryed, then a copy of the memory (of which the
// I/S will be the owner) needs to be made turning the instance from VOLATILE to OWNED.
enum class Volatility
{
NONE, PRIMARY, SECONDARY
};
// The "DataContainer" class represent a shared piece of memory - so if two idifferent buffers buffers
// can both reference the same memory segement without copying it.
class DataContainer;
public:
// The "Segment" class represent a countinuous part of the buffer. Unlike the "DataContainer" class, it is not
// shared between diffrent buffers. It can be thought of as shared pointer to the "DataContainer" class - but it
// also has additional capabilities of scoping, compairson, and handling copying-in of the memory.
class Segment;
// The "SegIterator" class allow iterating over the different segments of the buffer (for specifc part of the code
// that require very high performance). The "SegRange" class is used for the `for ( : )` syntax.
using SegIterator = std::vector<Segment>::const_iterator;
class SegRange final
{
public:
SegIterator begin() { return b; }
SegIterator end() { return e; }
private:
friend class Buffer;
SegRange(const SegIterator &_b, const SegIterator &_e) : b(_b), e(_e) {}
SegIterator b, e;
};
// The "CharIterator" class is used to access the buffer, and may become invalid if the buffer changes.
// The "InternalPtr" class is used to read from the buffer through a structure, and is garantied to hold its
// original value even after the buffer changes or deleted.
class CharIterator;
template <typename T> class InternalPtr;
public:
using value_type = u_char;
using const_iterator = CharIterator;
Buffer() {}
Buffer(std::vector<u_char> &&vec);
Buffer(const std::vector<u_char> &vec);
Buffer(const std::vector<char> &vec);
Buffer(const std::string &str);
Buffer(const u_char *_ptr, uint _len, MemoryType type);
Buffer(const char *_ptr, uint _len, MemoryType type);
Buffer(const Buffer &);
Buffer(Buffer &&);
Buffer & operator=(const Buffer &);
Buffer & operator=(Buffer &&);
static void preload(); // Adds buffer evaluators
static void init() {}
static void fini() {}
static std::string getName() { return "Buffer"; }
uint size() const { return len; }
bool isEmpty() const { return len==0; }
bool contains(char ch) const;
uint segmentsNumber() const;
void operator+=(const Buffer &);
Buffer operator+(const Buffer &) const;
Buffer getSubBuffer(uint start, uint end) const;
Maybe<uint> findFirstOf(char ch, uint start = 0) const;
Maybe<uint> findFirstOf(const Buffer &buf, uint start = 0) const;
Maybe<uint> findFirstNotOf(char ch, uint start = 0) const;
Maybe<uint> findLastOf(char ch) const { return findLastOf(ch, len); }
Maybe<uint> findLastOf(char ch, uint start) const;
Maybe<uint> findLastNotOf(char ch) const { return findLastNotOf(ch, len); }
Maybe<uint> findLastNotOf(char ch, uint start) const;
void truncateHead(uint size);
void truncateTail(uint size);
void keepHead(uint size);
void keepTail(uint size);
void clear();
bool operator==(const Buffer &buf) const;
bool operator!=(const Buffer &buf) const { return !((*this)==buf); }
bool isEqual(const u_char *ptr, uint size) const;
bool isEqual(const char *ptr, uint size) const { return isEqual(reinterpret_cast<const u_char *>(ptr), size); }
bool isEqualLowerCase(const Buffer &buf) const;
bool operator<(const Buffer &buf) const;
bool operator<=(const Buffer &buf) const { return !(buf < *this); }
bool operator>(const Buffer& buf) const { return (buf < *this); }
bool operator>=(const Buffer &buf) const { return !(*this < buf); }
const u_char * data() const { serialize(); return fast_path_ptr; }
Maybe<InternalPtr<u_char>> getPtr(uint start, uint len) const;
template <typename T> Maybe<InternalPtr<T>> getTypePtr(uint start) const;
const u_char & operator[](uint offset) const;
operator std::string() const;
CharIterator begin() const;
CharIterator end() const;
SegRange segRange() const;
void serialize() const;
template<class Archive> void save(Archive &ar, uint32_t) const;
template<class Archive> void load(Archive &ar, uint32_t);
private:
void evalFastPath() const;
std::vector<Segment> segs;
uint len = 0;
// The "fast_path_ptr" and "fast_path_len" are used to allow a direct fast access to the beginning of the buffer
// (the first segment), which is the typical case.
uint fast_path_len = 0;
const u_char *fast_path_ptr = nullptr;
// The "type" and "is_owned" are used to make sure that the "fast_path_ptr" is up-to-date (regarding copying-in).
Volatility type = Volatility::NONE;
bool *is_owned = nullptr;
};
#include "buffer/data_container.h"
#include "buffer/segment.h"
#include "buffer/char_iterator.h"
#include "buffer/internal_ptr.h"
template<typename T>
Maybe<Buffer::InternalPtr<T>>
Buffer::getTypePtr(uint start) const
{
auto ptr = getPtr(start, sizeof(T));
if (!ptr.ok()) return ptr.passErr();
return InternalPtr<T>(ptr.unpackMove());
}
template<class Archive>
void
Buffer::save(Archive &ar, uint32_t) const
{
ar(segs, len);
}
template<class Archive>
void
Buffer::load(Archive &ar, uint32_t)
{
ar(segs, len);
evalFastPath();
}
#include "buffer/helper_functions.h"
#endif // __BUFFER_H__

View File

@@ -0,0 +1,53 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUFFER_CHAR_ITERATOR_H__
#define __BUFFER_CHAR_ITERATOR_H__
#include <iterator>
class Buffer::CharIterator
{
public:
using value_type = u_char;
CharIterator() {}
void operator++();
void operator++(int) { ++(*this); }
void operator+=(uint);
CharIterator operator+(uint) const;
bool operator==(const CharIterator &other) const;
bool operator!=(const CharIterator &other) const { return !((*this)==other); }
const u_char & operator*() const;
private:
friend class Buffer;
CharIterator(const SegIterator &_cur, const SegIterator &_end, uint _offset);
CharIterator(const SegIterator &_end);
SegIterator cur_seg, end_seg;
const u_char *ptr = nullptr;
uint offset = 0, size = 0;
};
namespace std
{
template <>
struct iterator_traits<Buffer::CharIterator>
{
using value_type = u_char;
};
}; // namespace std
#endif // __BUFFER_CHAR_ITERATOR_H__

View File

@@ -0,0 +1,71 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUFFER_DATA_CONTAINER_H__
#define __BUFFER_DATA_CONTAINER_H__
class Buffer::DataContainer
{
public:
DataContainer() : ptr(nullptr), len(0) {}
DataContainer(std::vector<u_char> &&_vec);
DataContainer(const u_char *_ptr, uint _len, MemoryType _type);
DataContainer(const DataContainer &) = delete;
DataContainer(DataContainer &&) = delete;
const u_char * data() const { return ptr; }
uint size() const { return len; }
// The "checkOnwership" method returns the place where the current memory type is owned by the I/S.
// This makes it possible to check if the memory has been moved (from VOLATILE to ONWED).
bool * checkOnwership() { return &is_owned; }
void
takeOwnership()
{
vec = std::vector<u_char>(ptr, ptr + len);
ptr = vec.data();
is_owned = true;
}
template<class Archive>
void
save(Archive &ar, uint32_t) const
{
if (is_owned) {
ar(vec);
} else {
std::vector<u_char> data(ptr, ptr + len);
ar(data);
}
}
template<class Archive>
void
load(Archive &ar, uint32_t)
{
ar(vec);
is_owned = true;
ptr = vec.data();
len = vec.size();
}
private:
// If the memory is OWNED (not STATIC or VOLATILE), the "vec" member is holding it - otherwise it is empty.
std::vector<u_char> vec;
// The "ptr" member points to the the beginning of the data, regardless of the type of memory.
const u_char *ptr = nullptr;
uint len = 0;
bool is_owned = true;
};
#endif // __BUFFER_DATA_CONTAINER_H__

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUFFER_HELPER_FUNCTIONS_H__
#define __BUFFER_HELPER_FUNCTIONS_H__
// Function to allow comparison with types that have data (of types char or u_char) and size
template <typename T>
bool
operator==(const Buffer &buf, const T &t)
{
return buf.isEqual(t.data(), t.size());
}
template <typename T>
bool
operator==(const T &t, const Buffer &buf)
{
return buf.isEqual(t.data(), t.size());
}
template <typename T>
bool
operator!=(const Buffer &buf, const T &t)
{
return !buf.isEqual(t.data(), t.size());
}
template <typename T>
bool
operator!=(const T &t, const Buffer &buf)
{
return !buf.isEqual(t.data(), t.size());
}
#endif // __BUFFER_HELPER_FUNCTIONS_H__

View File

@@ -0,0 +1,53 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUFFER_INTERNAL_PTR_H__
#define __BUFFER_INTERNAL_PTR_H__
template <typename T>
class Buffer::InternalPtr
{
public:
InternalPtr(const InternalPtr &) = default;
InternalPtr(InternalPtr &&o) : ptr(o.ptr), ref(std::move(o.ref)) { o.ptr = nullptr; }
InternalPtr & operator=(const InternalPtr &) = default;
InternalPtr &
operator=(InternalPtr &&o)
{
ptr = o.ptr;
ref = std::move(o.ref);
o.ptr = nullptr;
return *this;
}
operator const T *() const { return ptr; }
const T & operator*() const { dbgAssert(ptr != nullptr) << "Accessing a moved pointer"; return *ptr; }
const T * operator->() const { return ptr; }
private:
friend class Buffer;
InternalPtr(const T *_ptr, const std::shared_ptr<DataContainer> &data) : ptr(_ptr), ref(data) {}
template<typename O>
InternalPtr(InternalPtr<O> &&other)
:
ptr(reinterpret_cast<const T *>(other.ptr)),
ref(std::move(other.ref))
{}
const T *ptr;
std::shared_ptr<DataContainer> ref;
};
#endif // __BUFFER_INTERNAL_PTR_H__

View File

@@ -0,0 +1,75 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUFFER_SEGMENT_H__
#define __BUFFER_SEGMENT_H__
class Buffer::Segment
{
public:
Segment() {}
Segment(std::vector<u_char> &&_vec);
Segment(const u_char *_ptr, uint _len, MemoryType _type);
~Segment();
Segment(const Segment &);
Segment(Segment &&);
Segment & operator=(const Segment &);
Segment & operator=(Segment &&);
const u_char * data() const;
uint size() const { return len; }
template<class Archive>
void
save(Archive &ar, uint32_t) const
{
ar(data_container, offset, len);
}
template<class Archive>
void
load(Archive &ar, uint32_t)
{
// In the usual case, the `load` method is called on a newly default constructed object.
// However, since there is no guarantee that will always be the case, we need to make sure to handle the case
// where the object the data is loaded to is currently used as a PRIMARY.
if (type==Volatility::PRIMARY && !data_container.unique()) {
data_container->takeOwnership();
}
ar(data_container, offset, len);
type = Volatility::NONE;
is_owned = nullptr;
ptr = data_container->data() + offset;
}
private:
friend class Buffer;
// The "data_container" is the smart pointer to the actual memory.
std::shared_ptr<DataContainer> data_container;
// The "offset" and "len" members are used to indicate what part of the shared memory the segment refers to.
uint offset = 0, len = 0;
// The "type" member holds the volatility status of the memory.
Volatility type = Volatility::NONE;
// The "is_owned" member is used in case of SECONDARY volatility to check if ownership of the memory was taken.
// It a pointer to `data_container->is_owned` if the segement is SECONDARY, and nullptr if it isn't.
bool *is_owned = nullptr;
// The "ptr" member is used to gain access to the memory directly without going to through the shared memory
// pointer (fast path).
const u_char *ptr = nullptr;
};
#endif // __BUFFER_SEGMENT_H__

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __IP_COMMON_C__
#define __IP_COMMON_C__
enum IpVersion {
IP_VERSION_ANY = 0,
IP_VERSION_4 = 4,
IP_VERSION_6 = 6,
};
typedef enum IpVersion IpVersion;
typedef struct IpAddress {
union {
struct in_addr ipv4;
struct in6_addr ipv6;
} ip;
#define addr4_t ip.ipv4
#define addr6_t ip.ipv6
IpVersion ip_type;
} IpAddress;
typedef struct IPRange {
IpAddress start;
IpAddress end;
} IPRange;
typedef struct PortsRange {
uint16_t start;
uint16_t end;
} PortsRange;
typedef struct IpProtoRange {
uint8_t start;
uint8_t end;
} IpProtoRange;
typedef struct GDFilter {
IPRange *source;
unsigned int size;
} GDFilter;
#endif // __IP_COMMON_C__

View File

@@ -0,0 +1,265 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __NETWORK_DEFS__
#define __NETWORK_DEFS__
// Various network layer definitions
// Note: we get Linux's annyoing TCP headers, not BSD's nicer ones.
// A significant difference is that TCP flags are bit fields, so masking is hard.
// Maybe we should just copy&paste nice headers and be more portable?
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#ifndef alpine
#include <bits/endian.h>
#else
#include <endian.h>
#endif
// Sometimes BSD's flag definitions are just so much more convenient.
// So let people use them.
// XXX: Very ugly. Better really switch to BSD.
#ifdef __cplusplus
using TCPFlags = u_char;
#else
typedef u_char TCPFlags;
#endif
#ifndef TH_FIN
static const u_char TH_FIN=0x01;
#endif
#ifndef TH_SYN
static const u_char TH_SYN=0x02;
#endif
#ifndef TH_RST
static const u_char TH_RST=0x04;
#endif
#ifndef TH_PSH
static const u_char TH_PSH=0x08;
#endif
#ifndef TH_ACK
static const u_char TH_ACK=0x10;
#endif
#ifndef TH_URG
static const u_char TH_URG=0x20;
#endif
// Linux TCP headers are not the same for all distros, so we bring tcp/udp structs here
// probably switch to BSD headers is a better options
struct UdpHdr
{
uint16_t source;
uint16_t dest;
uint16_t len;
uint16_t check;
};
struct TcpHdr
{
uint16_t source;
uint16_t dest;
uint32_t seq;
uint32_t ack_seq;
# if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t res1:4;
uint8_t doff:4;
union {
struct {
uint8_t fin:1;
uint8_t syn:1;
uint8_t rst:1;
uint8_t psh:1;
uint8_t ack:1;
uint8_t urg:1;
uint8_t res2:2;
};
uint8_t flags;
};
# elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t doff:4;
uint8_t res1:4;
union {
struct {
uint8_t res2:2;
uint8_t urg:1;
uint8_t ack:1;
uint8_t psh:1;
uint8_t rst:1;
uint8_t syn:1;
uint8_t fin:1;
};
uint8_t flags;
};
#else
# error "Adjust your <bits/endian.h> defines"
#endif
uint16_t window;
uint16_t check;
uint16_t urg_ptr;
};
#ifndef IPPROTO_MH
#define IPPROTO_MH 135
#endif
// GRE START
#ifndef GREPROTO_PPP
#define GREPROTO_PPP 0x880B
#endif
struct GreHdr
{
uint16_t flags;
uint16_t proto_type;
};
struct EnhancedGreHdr
{
uint16_t flags;
uint16_t proto_type;
uint16_t data_length;
uint16_t call_id;
};
// GRE END
// SCTP START
struct SctpHdr
{
uint16_t sport;
uint16_t dport;
uint vtag;
uint sum;
};
struct SctpChunkHdr
{
u_char chunk_type;
u_char chunk_flags;
uint16_t chunk_length;
};
// SCTP END
// DCCP START
#ifndef DCCPO_CHANGE_L
#define DCCPO_CHANGE_L 32
#endif
#ifndef DCCPO_CHANGE_R
#define DCCPO_CHANGE_R 34
#endif
struct DccpHdr
{
uint16_t dccph_sport;
uint16_t dccph_dport;
u_char dccph_doff;
#if __BYTE_ORDER == __LITTLE_ENDIAN
u_char dccph_cscov : 4,
dccph_ccval : 4;
#elif __BYTE_ORDER == __BIG_ENDIAN
u_char dccph_ccval : 4,
dccph_cscov : 4;
#else
#error unknown byte order
#endif // __BYTE_ORDER
uint16_t dccph_checksum;
#if __BYTE_ORDER == __LITTLE_ENDIAN
u_char dccph_x : 1,
dccph_type : 4,
dccph_reserved : 3;
#elif __BYTE_ORDER == __BIG_ENDIAN
u_char dccph_reserved : 3,
dccph_type : 4,
dccph_x : 1;
#else
#error unknown byte order
#endif // __BYTE_ORDER
u_char dccph_seq2;
uint16_t dccph_seq;
};
struct DccpHdrExt
{
uint dccph_seq_low;
};
struct DccpOptHdr
{
u_char type;
u_char length;
};
struct DccpHdrAckBits
{
uint16_t dccph_reserved1;
uint16_t dccph_ack_nr_high;
uint dccph_ack_nr_low;
};
struct DccpHdrRequest
{
uint dccph_req_service;
};
struct DccpHdrResponse
{
struct DccpHdrAckBits dccph_resp_ack;
uint dccph_resp_service;
};
struct DccpHdrReset
{
struct DccpHdrAckBits dccph_reset_ack;
uint16_t dccph_reset_code;
uint16_t dccph_reset_data[3];
};
enum DccpPacketType {
DCCP_PKT_REQUEST = 0,
DCCP_PKT_RESPONSE,
DCCP_PKT_DATA,
DCCP_PKT_ACK,
DCCP_PKT_DATAACK,
DCCP_PKT_CLOSEREQ,
DCCP_PKT_CLOSE,
DCCP_PKT_RESET,
DCCP_PKT_SYNC,
DCCP_PKT_SYNCACK,
DCCP_PKT_INVALID,
};
// DCCP END
static inline TCPFlags
getTCPFlags(const struct TcpHdr *tcp)
{
TCPFlags res = (TCPFlags)0;
if (tcp->fin) res |= TH_FIN;
if (tcp->syn) res |= TH_SYN;
if (tcp->rst) res |= TH_RST;
if (tcp->psh) res |= TH_PSH;
if (tcp->ack) res |= TH_ACK;
if (tcp->urg) res |= TH_URG;
return res;
}
#endif // __NETWORK_DEFS__

View File

@@ -0,0 +1,211 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __NETWORKING_HEADERS_H__
#define __NETWORKING_HEADERS_H__
#define ETH_P_IPV6 0x86DD
#define ETH_P_IP 0x0800
#define NF_DROP 0u
#define NF_ACCEPT 1u
#ifndef DCCPO_CHANGE_L
#define DCCPO_CHANGE_L 32
#endif
#ifndef DCCPO_CHANGE_R
#define DCCPO_CHANGE_R 34
#endif
typedef unsigned int sk_buff_data_t;
enum PROTOCOL {
ICMP = 1, //0x1
TCP = 6, //0x6
UDP = 17, //0x11
DCCP = 33, //0x21
IPV6_FRAG = 44, //0x2c
ICMPV6 = 58, //0x3A
SCTP = 132 //0x84
};
enum DccpPacketType {
DCCP_PKT_REQUEST = 0,
DCCP_PKT_RESPONSE,
DCCP_PKT_DATA,
DCCP_PKT_ACK,
DCCP_PKT_DATAACK,
DCCP_PKT_CLOSEREQ,
DCCP_PKT_CLOSE,
DCCP_PKT_RESET,
DCCP_PKT_SYNC,
DCCP_PKT_SYNCACK,
DCCP_PKT_INVALID,
};
struct sk_buff {
uint16_t protocol;
union {
struct iphdr *ip_header;
struct ipv6hdr *ipv6_header;
} network_header;
union {
struct udphdr *udp_header;
struct tcphdr *tcp_header;
struct icmphdr *icmp_header;
struct icmp6hdr *icmp6_header;
struct sctphdr *sctp_header;
struct dccphdr *dccp_header;
} transport_header;
unsigned char *tail;
unsigned char *data;
unsigned char *head;
unsigned int len;
};
struct geneve_opt {
__u16 opt_class;
u_int8_t type;
#ifdef __LITTLE_ENDIAN_BITFIELD
u_int8_t length:5;
u_int8_t r3:1;
u_int8_t r2:1;
u_int8_t r1:1;
#else
u_int8_t r1:1;
u_int8_t r2:1;
u_int8_t r3:1;
u_int8_t length:5;
#endif
u_int8_t opt_data[];
};
struct genevehdr {
#ifdef __LITTLE_ENDIAN_BITFIELD
u_int8_t opt_len:6;
u_int8_t ver:2;
u_int8_t rsvd1:6;
u_int8_t critical:1;
u_int8_t oam:1;
#else
u_int8_t ver:2;
u_int8_t opt_len:6;
u_int8_t oam:1;
u_int8_t critical:1;
u_int8_t rsvd1:6;
#endif
__u16 proto_type;
u_int8_t vni[3];
u_int8_t rsvd2;
struct geneve_opt options[];
};
struct sctphdr {
u_int16_t source;
u_int16_t dest;
u_int32_t vtag;
u_int32_t checksum;
};
struct ipv6hdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
u_int8_t priority : 4,
version : 4;
#elif __BYTE_ORDER == __BIG_ENDIAN
u_int8_t version : 4,
priority : 4;
#else
#error unknown byte order
#endif // __BYTE_ORDER
u_int8_t flow_lbl[3];
u_int16_t payload_len;
u_int8_t nexthdr;
u_int8_t hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
};
struct dccp_hdr
{
uint16_t dccph_sport;
uint16_t dccph_dport;
u_char dccph_doff;
#if __BYTE_ORDER == __LITTLE_ENDIAN
u_char dccph_cscov : 4,
dccph_ccval : 4;
#elif __BYTE_ORDER == __BIG_ENDIAN
u_char dccph_ccval : 4,
dccph_cscov : 4;
#else
#error unknown byte order
#endif // __BYTE_ORDER
uint16_t dccph_checksum;
#if __BYTE_ORDER == __LITTLE_ENDIAN
u_char dccph_x : 1,
dccph_type : 4,
dccph_reserved : 3;
#elif __BYTE_ORDER == __BIG_ENDIAN
u_char dccph_reserved : 3,
dccph_type : 4,
dccph_x : 1;
#else
#error unknown byte order
#endif // __BYTE_ORDER
u_char dccph_seq2;
uint16_t dccph_seq;
};
struct sctp_chunkhdr
{
u_char chunk_type;
u_char chunk_flags;
uint16_t chunk_length;
};
struct dccp_hdr_ext
{
uint dccph_seq_low;
};
struct dccp_hdr_request
{
uint dccph_req_service;
};
struct dccp_opt_hdr
{
u_char type;
u_char length;
};
struct dccp_hdr_ack_bits
{
uint16_t dccph_reserved1;
uint16_t dccph_ack_nr_high;
uint dccph_ack_nr_low;
};
struct dccp_hdr_response
{
struct dccp_hdr_ack_bits dccph_resp_ack;
uint dccph_resp_service;
};
struct dccp_hdr_reset
{
struct dccp_hdr_ack_bits dccph_reset_ack;
uint16_t dccph_reset_code;
uint16_t dccph_reset_data[3];
};
#endif // __NETWORKING_HEADERS_H__

View File

@@ -0,0 +1,138 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __COMMON_H__
#define __COMMON_H__
#define CP_LIKELY(cond) __builtin_expect((bool)(cond), 1)
#define CP_UNLIKELY(cond) __builtin_expect((bool)(cond), 0)
#define CP_UNUSED __attribute__((unused))
#define CP_NO_RETURN __attribute__((noreturn))
#if defined(__GNUC__) && __GNUC__ >= 7
#define CP_FALL_THROUGH __attribute__ ((fallthrough))
#else
#define CP_FALL_THROUGH ((void)0)
#endif // __GNUC__ >= 7
#include <memory>
#include <string>
#include <sstream>
#include <iomanip>
#include <sys/types.h>
namespace std
{
#if __cplusplus < 201402L
// make_unique isn't part of C++11 - but pretty useful, so we'll define it in such cases
template<typename ConstructedType, typename... Args>
unique_ptr<ConstructedType>
make_unique(Args&&... args)
{
return unique_ptr<ConstructedType>(new ConstructedType(forward<Args>(args)...));
}
#endif // __cplusplus < 201402L
// Not part of the C++11 standard, but is useful imitation of Python's `join`
template <typename Iterable>
string
makeSeparatedStr(const Iterable &data, const string &separator)
{
ostringstream os;
bool not_first = false;
for (const auto &element : data) {
if (not_first) os << separator;
os << element;
not_first = true;
}
return os.str();
}
template <typename Char>
string
dumpHexChar(const Char &ch)
{
ostringstream stream;
if (isprint(ch) && (!isspace(ch) || ch==' ')) {
stream << "'" << ch << "'";
} else {
stream << "\\x" << setw(2) << setfill('0') << hex << static_cast<int>(ch);
}
return stream.str();
}
// Produce a hex string from some container of charecters.
// The container must be iterable.
template <typename CharIterable>
string
dumpHex(const CharIterable &arg)
{
ostringstream stream;
stream << hex;
for (uint8_t ch : arg) {
if (isprint(ch) && (!isspace(ch) || ch==' ')) {
// Printable characters, except for whitespaces which aren't space.
if (ch == '\\') stream << '\\';
stream << ch;
} else {
stream << "\\x" << setw(2) << setfill('0') << static_cast<int>(ch);
}
}
return stream.str();
}
template <typename CharIterable>
string
dumpRealHex(const CharIterable &arg)
{
ostringstream stream;
stream << hex;
for (uint8_t ch : arg) {
stream << " " << setw(2) << setfill('0') << static_cast<int>(ch);
}
return stream.str();
}
template <typename T, typename Helper = void>
struct IsPrintable : false_type
{
};
template <typename T>
struct IsPrintable<T, decltype(static_cast<void>(declval<ostream &>() << declval<T>()))> : true_type
{
};
template <typename CanPrint>
ostream &
operator<<(ostream &os, const decltype(declval<CanPrint>().print(declval<ostream &>()), declval<CanPrint>()) &obj)
{
obj.print(os);
return os;
}
template <typename First, typename Second>
ostream&
operator<<(ostream &os, const pair<const First, Second> &printable_pair)
{
os << "{" << printable_pair.first << "," << printable_pair.second << "}";
return os;
}
} // namespace std
#endif // __COMMON_H__

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __CONFIG_COMPONENT_H__
#define __CONFIG_COMPONENT_H__
#include <string>
#include <vector>
#include "config.h"
#include "singleton.h"
#include "i_rest_api.h"
#include "i_time_get.h"
#include "i_mainloop.h"
#include "i_environment.h"
#include "i_messaging.h"
#include "i_instance_awareness.h"
#include "i_tenant_manager.h"
#include "component.h"
class ConfigComponent
:
public Component,
Singleton::Provide<Config::I_Config>,
Singleton::Consume<I_RestApi>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_InstanceAwareness>,
Singleton::Consume<I_TenantManager>
{
public:
ConfigComponent();
~ConfigComponent();
void preload();
void init();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __CONFIG_COMPONENT_H__

43
core/include/general/cptest.h Executable file
View File

@@ -0,0 +1,43 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#if !defined(__CP_TEST_H__)
#define __CP_TEST_H__
//
// CP definitions which are useful in many unit tests
//
#include <string>
#include <vector>
#include <ostream>
#include <functional>
#include "cptest/cptest_basic.h"
#include "cptest/cptest_file.h"
#include "cptest/cptest_singleton.h"
#include "cptest/cptest_maybe.h"
#include "buffer.h"
#include "scope_exit.h"
#include "tostring.h"
std::ostream& operator<<(std::ostream &os, const Buffer &buf);
// Parse a hex string, e.g. the output of tcpdump -xx, into a vector.
std::vector<u_char> cptestParseHex(const std::string &hex_text);
// The inverse of cptest_parse_hex
// Take a vector of data, and generate hex from it output, like tcpdump.
std::string cptestGenerateHex(const std::vector<u_char> &vec, bool print_offsets);
#endif // __CP_TEST_H__

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __CP_TEST_BASIC_H__
#define __CP_TEST_BASIC_H__
#include "gtest/gtest.h"
#include "gmock/gmock.h"
// Before EXPECT_DEATH, call this to do all necessary preparations.
void cptestPrepareToDie();
// Path to a file in the UT directory
std::string cptestFnameInExeDir(const std::string &name);
std::string cptestFnameInSrcDir(const std::string &name);
ACTION_TEMPLATE(
SaveVoidArgPointee,
HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
AND_1_VALUE_PARAMS(output)
)
{
*output = *static_cast<T *>(::testing::get<k>(args));
}
#endif // __CP_TEST_BASIC_H__

View File

@@ -0,0 +1,37 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __CPTEST_FILE_H__
#define __CPTEST_FILE_H__
// Create a temporary file with some content. Delete on destruction.
class CPTestTempfile
{
public:
explicit CPTestTempfile(const std::vector<std::string> &lines);
explicit CPTestTempfile();
~CPTestTempfile();
std::string readFile() const;
std::string fname;
// Not copiable (would delete the file twice), but movable
CPTestTempfile(const CPTestTempfile &other) = delete;
CPTestTempfile(CPTestTempfile &&other) = default;
CPTestTempfile & operator=(const CPTestTempfile &other) = delete;
CPTestTempfile & operator=(CPTestTempfile &&other) = default;
};
#endif // __CPTEST_FILE_H__

View File

@@ -0,0 +1,164 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __CPTEST_MAYBE_H__
#define __CPTEST_MAYBE_H__
#include "maybe_res.h"
#include "gmock/gmock.h"
namespace testing {
namespace maybe_matcher {
// Matchers for Maybe<T> objects.
// Usage examples:
// Maybe<int> m = ...;
// EXPECT_THAT(m, IsValue(3)); // Must hold 3
// EXPECT_THAT(m, IsValue(_)); // Must be a value
// EXPECT_THAT(m, IsError("HA")); // Must be an error with specific text
// EXPECT_THAT(m, IsError(_)); // Any error (but not a value)
//
// Generic classes that handle either the value or the error
//
// Matcher for Maybe values.
// Verify that Maybe<T>::ok() is as expected, and runs a matcher on the internal value or error.
//
// Abstract base class, inherited to match either the internal value or the error value.
// Template parameters:
// MaybeType - the Maybe type, possibly with "const &".
// GetInternal - gets the internal value or error from Maybe.
template <typename MaybeType, typename GetInternal>
class BaseMatcher : public MatcherInterface<MaybeType>
{
private:
using InternalValue = decltype(GetInternal::get(std::declval<MaybeType>()));
public:
BaseMatcher(const Matcher<InternalValue> &_matcher)
:
label(GetInternal::expected_ok ? "Value" : "Error"),
matcher(_matcher)
{
}
bool
MatchAndExplain(MaybeType m, MatchResultListener *listener) const override {
if (m.ok() != GetInternal::expected_ok) return false;
return matcher.MatchAndExplain(GetInternal::get(m), listener);
}
// LCOV_EXCL_START - Only called when a test fails, to explain why.
void
DescribeTo(::std::ostream *os) const override {
*os << label << "(";
matcher.DescribeTo(os);
*os << ")";
}
void
DescribeNegationTo(::std::ostream *os) const override {
*os << label << "(";
matcher.DescribeNegationTo(os);
*os << ")";
}
// LCOV_EXCL_STOP
private:
std::string label;
Matcher<InternalValue> matcher;
};
// Temporary matcher for Maybe values - converted to BaseMatcher when needed.
// Converts any matcher to a Maybe matcher, which invokes the internal matcher on the value or error.
// Template parameters:
// InternalMatcherType - Any matcher type that matches the value/error.
// InternalValueGetter - gets the internal value or error from Maybe.
template <typename InternalMatcherType, typename InternalValueGetter>
class TempMatcher
{
public:
TempMatcher(InternalMatcherType _matcher) : matcher(_matcher) {}
// The internal type becomes known when this object is cast to a specific
// matcher type. Create a Maybe matcher, while casting the internal matcher to the
// type that we now know.
template <typename MaybeType>
operator Matcher<MaybeType>() const
{
using MaybeMatcherType = BaseMatcher<MaybeType, InternalValueGetter>;
return MakeMatcher(new MaybeMatcherType(matcher));
}
private:
InternalMatcherType matcher;
};
//
// Classes to get the internal value from a Maybe object.
// The internal value can be either the value or the error.
//
class GetValue
{
public:
static const bool expected_ok = true;
template<typename T, typename TErr>
static T
get(const Maybe<T, TErr> &m)
{
return m.unpack();
}
};
class GetError
{
public:
static const bool expected_ok = false;
template<typename T, typename TErr>
static TErr
get(const Maybe<T, TErr> &m)
{
return m.getErr();
}
};
} // namespace maybe_matcher
//
// Functions to return matchers - to be used by test code.
//
// Convert Matcher<T> to Matcher<Maybe<T>>, which verifies that it's ok and matches the value.
template<typename MatcherType>
static inline ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetValue>
IsValue(MatcherType matcher)
{
return ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetValue>(matcher);
}
// Convert Matcher<TErr> to Matcher<Maybe<T, TErr>>, which verifies that it's not ok and matches the error.
template<typename MatcherType>
static inline ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetError>
IsError(MatcherType matcher)
{
return ::testing::maybe_matcher::TempMatcher<MatcherType, ::testing::maybe_matcher::GetError>(matcher);
}
} // namespace testing
#endif // __CPTEST_MAYBE_H__

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __CP_TEST_SINGLETON_H__
#define __CP_TEST_SINGLETON_H__
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "singleton.h"
// Mock objects should use Singleton::Provide<I_Face>::From<MockProvider> with the interface they mock.
template<typename I_Face>
class MockProvider : Singleton::Provide<I_Face>
{};
#endif // __CP_TEST_SINGLETON_H__

View File

@@ -0,0 +1,88 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __CPTEST_TCPPACKET_H__
#define __CPTEST_TCPPACKET_H__
#include <vector>
#include <string>
#include <memory>
#include "packet.h"
// Generate TCP options
class TCPOption
{
public:
explicit TCPOption(const std::string &_name, const std::vector<u_char> _data);
TCPOption(const TCPOption &from);
~TCPOption();
// Accessors
size_t size() const;
std::vector<u_char> build() const;
// Well-known options - simple ones are constants, complex are are static functions
static const TCPOption NOP;
static const TCPOption SACK_PERMITTED;
static TCPOption windowScaling(u_char shift_count);
static TCPOption timeStamp(uint value, uint echo_reply);
static TCPOption selectiveACK(const std::vector<std::pair<uint, uint>> &edges);
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
class TCPPacket
{
public:
// Build an empty packet
explicit TCPPacket(CDir _cdir);
~TCPPacket();
// Movable, not copiable
TCPPacket(TCPPacket &&from);
TCPPacket(const TCPPacket &from) = delete;
// Methods to set TCP properties. Return reference to this, to allow chaining
TCPPacket & setTCPPayload(const std::vector<u_char> &payload);
TCPPacket & setTCPPayload(const std::string &payload);
TCPPacket & setTCPSeq(uint _tcp_seq);
TCPPacket & setTCPAck(uint _tcp_ack);
TCPPacket & setTCPWindow(uint16_t _tcp_window);
TCPPacket & setTCPFlags(std::string _tcp_flags);
TCPPacket & setTCPUrgentPtr(uint16_t _tcp_urgent_ptr);
TCPPacket & setTCPCksum(uint _tcp_cksum_override);
TCPPacket & setL2Header(const std::vector<u_char> &_l2_header);
TCPPacket & addTCPOption(const TCPOption &tcp_option);
TCPPacket & setL4HeaderSize(uint header_size);
TCPPacket & setL4DataOffset(uint data_offset);
TCPPacket && move() { return std::move(*this); }
// Build a Packet
std::unique_ptr<Packet> build(const ConnKey &ck) const;
// Get the TCP sequence
uint getTCPSeq() const;
static uint16_t calcTCPv4Checksum(const std::vector<u_char> &pkt);
static uint16_t calcTCPv6Checksum(const std::vector<u_char> &pkt);
static uint16_t calcIPv4Checksum(const std::vector<u_char> &pkt);
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __CPTEST_TCPPACKET_H__

269
core/include/general/debug.h Executable file
View File

@@ -0,0 +1,269 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __DEBUG_H__
#define __DEBUG_H__
#include <set>
#include <functional>
#include <chrono>
#include <vector>
#include "common.h"
#include "singleton.h"
#include "scope_exit.h"
class I_TimeGet;
class I_Messaging;
class I_MainLoop;
class I_Environment;
class I_InstanceAwareness;
class I_Encryptor;
class I_AgentDetails;
class I_SignalHandler;
class Debug
:
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_InstanceAwareness>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_Encryptor>,
Singleton::Consume<I_AgentDetails>,
Singleton::Consume<I_SignalHandler>
{
public:
class DebugStream;
enum class DebugLevel { NOISE, TRACE, DEBUG, INFO, WARNING, ERROR, ASSERTION, NONE };
enum class DebugFlags;
class DebugStreamAggr
{
template <typename T, typename Helper = void>
struct Print
{
Print(std::ostream *str, const T &obj) { (*str) << obj; }
};
template <typename T>
struct Print<T, decltype(std::declval<T>().print(std::declval<std::ostream &>()))>
{
Print(std::ostream *str, const T &obj) { obj.print(*str); }
};
public:
template <typename T>
DebugStreamAggr &
operator<<(const T &obj)
{
for (auto &stream : streams) {
Print<T>(stream, obj);
}
return *this;
}
DebugStreamAggr &
operator<<(std::ostream & (*func)(std::ostream &))
{
for (auto &stream : streams) {
func(*stream);
}
return *this;
}
void addStream(std::ostream *stream) { streams.insert(stream); }
private:
std::set<std::ostream *> streams;
};
class DebugLockState
{
private:
friend class Environment;
static bool getState() { return is_debug_running; }
static void setState(const bool _is_debug_running) { is_debug_running = _is_debug_running; }
};
public:
Debug(
const std::string &file_name,
const std::string &func_name,
const uint &line
);
Debug(
const std::string &file_name,
const std::string &func_name,
const uint &line,
const DebugLevel &level,
const DebugFlags &flag1
);
Debug(
const std::string &file_name,
const std::string &func_name,
const uint &line,
const DebugLevel &level,
const DebugFlags &flag1,
const DebugFlags &flag2
);
Debug(
const std::string &file_name,
const std::string &func_name,
const uint &line,
const DebugLevel &level,
const DebugFlags &flag1,
const DebugFlags &flag2,
const DebugFlags &flag3
);
Debug(
const std::string &file_name,
const std::string &func_name,
const uint &line,
const DebugLevel &level,
const DebugFlags &flag1,
const DebugFlags &flag2,
const DebugFlags &flag3,
const DebugFlags &flag4
);
~Debug();
DebugStreamAggr &
getStreamAggr() __attribute__((warn_unused_result))
{
return stream;
}
static void preload();
static void init();
static void fini();
static std::string getName() { return "DebugIS"; }
static void prepareConfig();
static void commitConfig();
static void abortConfig();
static void failOpenDebugMode(std::chrono::seconds debug_period);
template <typename... Args>
static bool
evalFlags(DebugLevel level, DebugFlags flag, Args... args)
{
return !is_debug_running && level>=lowest_global_level && evalFlagByFlag(level, flag, args...);
}
static bool isFlagAtleastLevel(DebugFlags flag, DebugLevel level);
static void setNewDefaultStdout(std::ostream *new_stream);
static void setUnitTestFlag(DebugFlags flag, DebugLevel level);
static std::string findDebugFilePrefix(const std::string &file_name);
private:
template <typename T, typename... Args>
static bool
evalFlagByFlag(DebugLevel _level, T flag, Args... args)
{
return evalFlagByFlag(_level, flag) || evalFlagByFlag(_level, args...);
}
static bool evalFlagByFlag(DebugLevel _level, DebugFlags flag);
static bool evalFlagByFlag(DebugLevel) { return true; }
static void applyOverrides();
bool shouldApplyFailOpenOnStream(const std::string &name) const;
void addActiveStream(const std::string &name);
void printBacktraceBeforeAbort();
void startStreams(
const DebugLevel &level,
const std::string &file_name,
const std::string &func_name,
const uint &line
);
static DebugLevel lowest_global_level;
static I_TimeGet *time;
static I_MainLoop *mainloop;
static I_Environment *env;
static bool is_debug_running;
static bool is_fail_open_mode;
static bool debug_override_exist;
static std::string default_debug_file_stream_path;
static std::vector<std::string> streams_from_mgmt;
bool do_assert;
DebugStreamAggr stream;
std::set<std::shared_ptr<DebugStream>> current_active_streams;
};
#define USE_DEBUG_FLAG(x) extern const Debug::DebugFlags x
// This function extract the base name from a full path.
// The `iter` variable holds the current place of the iteration over the full path.
// The `base` variable holds where we currently think the base name starts
static inline constexpr const char *
getBaseName(const char *iter, const char *base)
{
// If `iter` doesn't point to the next charecter in the string, then return where we think the base name starts.
return (iter==nullptr || *iter=='\0') ? base :
// If `iter` points to '/' char, then now we thik the next char is the start of the base name, otherwise we
// stil think that `base` points to the start of the base name.
// In any case, we recursively progress `iter` to check the next char.
(*iter=='/' ? getBaseName(iter+1, iter+1) : getBaseName(iter+1, base));
}
#define __FILENAME__ getBaseName(__FILE__, __FILE__)
#define dbgAssert(cond) \
if (CP_LIKELY(cond)) { \
} else Debug(__FILENAME__, __FUNCTION__, __LINE__).getStreamAggr()
// Macros to allow simple debug messaging
#define DBG_GENERIC(level, ...) \
if (!Debug::evalFlags(Debug::DebugLevel::level, __VA_ARGS__)) { \
} else Debug(__FILENAME__, __FUNCTION__, __LINE__, Debug::DebugLevel::level, __VA_ARGS__).getStreamAggr()
#define isDebugRequired(level, flag) (Debug::evalFlags(Debug::DebugLevel::level, flag))
#define dbgTrace(...) DBG_GENERIC(TRACE, __VA_ARGS__)
#define dbgDebug(...) DBG_GENERIC(DEBUG, __VA_ARGS__)
#define dbgInfo(...) DBG_GENERIC(INFO, __VA_ARGS__)
#define dbgWarning(...) DBG_GENERIC(WARNING, __VA_ARGS__)
#define dbgError(...) DBG_GENERIC(ERROR, __VA_ARGS__)
// Macro for automatic printouts on entering and leaving scope
// Should be in the first line of a function
// Output is in Trace level
#define dbgFlow(...) \
auto __function_name = __FUNCTION__; \
auto __scope_exit = std::make_scope_exit( \
[__function_name] () { \
if (Debug::evalFlags(Debug::DebugLevel::TRACE, __VA_ARGS__)) { \
Debug(__FILENAME__, __function_name, __LINE__, Debug::DebugLevel::TRACE, __VA_ARGS__) \
.getStreamAggr() << "Exit"; \
} \
} \
); \
dbgTrace(__VA_ARGS__) << "Enter "
#endif // __DEBUG_H__

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ENCRYPTOR_H__
#define __ENCRYPTOR_H__
#include <memory>
#include "i_encryptor.h"
#include "singleton.h"
#include "component.h"
class Encryptor : public Component, Singleton::Provide<I_Encryptor>
{
public:
Encryptor();
~Encryptor();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __ENCRYPTOR_H__

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ENVIRONMENT_H__
#define __ENVIRONMENT_H__
#include <memory>
#include "i_environment.h"
#include "i_tenant_manager.h"
#include "singleton.h"
#include "component.h"
class I_RestApi;
class Environment
:
public Component,
Singleton::Provide<I_Environment>,
Singleton::Consume<I_RestApi>,
Singleton::Consume<I_TenantManager>
{
public:
Environment();
~Environment();
void init();
void fini();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __ENVIRONMENT_H__

View File

@@ -0,0 +1,25 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HASH_COMBINE_H__
#define __HASH_COMBINE_H__
// Copied from Boost (hash_combine)
template <class T>
inline void hashCombine(size_t &seed, const T &v)
{
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
#endif // __HASH_COMBINE_H__

View File

@@ -0,0 +1,162 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __IMPL_SINGLETON_H__
#define __IMPL_SINGLETON_H__
#ifndef __SINGLETON_H__
#error impl/singleton.h should not be included directly.
#endif // SINGLETON_H__
template <class I_Face>
class Singleton::Provide
{
public:
template <class Comp>
class From : public I_Face
{
static_assert(
std::is_base_of<Provide<I_Face>, Comp>::value,
"Comp class must inherit from Singleton::Provide<I_Face>"
);
public:
From() { Singleton::registerSingleton(typeid(I_Face), this); }
~From() { Singleton::unregisterSingleton(typeid(I_Face), this); }
};
class Self;
class SelfInterface : Self, public I_Face
{
};
};
template <class I_Face>
class Singleton::Provide<I_Face>::Self : Provide<I_Face>
{
public:
Self() { Singleton::registerSingleton(typeid(I_Face), this); }
~Self() { Singleton::unregisterSingleton(typeid(I_Face), this); }
};
template <class I_Face>
class Singleton::Consume
{
public:
template <class ConsumingComp>
static inline I_Face *
by()
{
static_assert(
std::is_base_of<Consume<I_Face>, ConsumingComp>::value,
"ConsumingComp class must inherit from Singleton::Consume<I_Face>"
);
return get();
}
template <class ProvidingComp>
static inline I_Face *
from()
{
static_assert(
std::is_base_of<Provide<I_Face>, ProvidingComp>::value,
"ProvidingComp class must inherit from Singleton::Provide<I_Face>"
);
return get();
}
template <class ProvidingComp>
static inline I_Face *
from(const ProvidingComp &)
{
return from<ProvidingComp>();
}
template <class Comp>
static inline I_Face *
to()
{
static_assert(
std::is_base_of<Consume<I_Face>, Comp>::value || std::is_base_of<Provide<I_Face>, Comp>::value,
"Component class must declare it relationship to the interface"
);
return get();
}
private:
static inline I_Face * get() { return Singleton::get<I_Face>(); }
};
template <typename T>
bool
Singleton::exists()
{
return exists(typeid(T));
}
class Singleton::OwnedSingleton
{
public:
virtual ~OwnedSingleton() {}
};
template <typename T>
T *
Singleton::getOwned()
{
return static_cast<T *>(owned_singles[typeid(T)].get());
}
template <typename T>
bool
Singleton::existsOwned()
{
return owned_singles.count(typeid(T)) != 0;
}
template <typename T, typename ...Args>
void
Singleton::newOwned(Args ...args)
{
owned_singles[typeid(T)] = std::make_unique<T>(std::forward<Args>(args)...);
}
template <typename T>
void
Singleton::deleteOwned()
{
owned_singles.erase(typeid(T));
}
template <typename T>
void
Singleton::setOwned(std::unique_ptr<T> &&ptr)
{
owned_singles[typeid(T)] = std::move(ptr);
}
template <typename T>
T *
Singleton::get()
{
return static_cast<T *>(get(typeid(T)));
}
template <typename Component, typename Interface>
Interface *
getInterface()
{
return Singleton::Consume<Interface>::template to<Component>();
}
#endif // __IMPL_SINGLETON_H__

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __INTELLIGENCE_COMP_V2_H__
#define __INTELLIGENCE_COMP_V2_H__
#include "i_intelligence_is_v2.h"
#include "singleton.h"
#include "i_messaging.h"
#include "i_mainloop.h"
#include "i_time_get.h"
#include "component.h"
class IntelligenceComponentV2
:
public Component,
Singleton::Provide<I_Intelligence_IS_V2>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TimeGet>
{
public:
IntelligenceComponentV2();
~IntelligenceComponentV2();
void init();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __INTELLIGENCE_COMP_V2_H__

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __LOGGING_COMP_H__
#define __LOGGING_COMP_H__
#include <memory>
#include "i_logging.h"
#include "i_messaging.h"
#include "singleton.h"
#include "i_mainloop.h"
#include "i_instance_awareness.h"
#include "i_socket_is.h"
#include "component.h"
#include "i_agent_details.h"
class LoggingComp
:
public Component,
Singleton::Provide<I_Logging>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_InstanceAwareness>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_Logging>,
Singleton::Consume<I_Socket>,
Singleton::Consume<I_AgentDetails>
{
public:
LoggingComp();
~LoggingComp();
void init();
void fini();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __LOGGING_COMP_H__

View File

@@ -0,0 +1,54 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __MAINLOOP_H__
#define __MAINLOOP_H__
#include <memory>
#include "i_mainloop.h"
#include "i_environment.h"
#include "i_time_get.h"
#include "i_messaging.h"
#include "i_agent_details.h"
#include "i_signal_handler.h"
#include "singleton.h"
#include "component.h"
extern bool fini_signal_flag;
class MainloopComponent
:
public Component,
Singleton::Provide<I_MainLoop>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_AgentDetails>,
Singleton::Consume<I_SignalHandler>
{
public:
MainloopComponent();
~MainloopComponent();
void preload();
void init();
void fini();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __MAINLOOP_H__

View File

@@ -0,0 +1,467 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __MAYBE_RES_H__
#define __MAYBE_RES_H__
#include <string>
#include <ostream>
#include "common.h"
#include "debug.h"
#include "tostring.h"
template <typename Err>
class Error
{
template <typename T, typename TErr>
friend class Maybe;
public:
template<typename... Args>
Error(Args&&... args) : err(std::forward<Args>(args)...) {}
bool
operator==(const Error &other) const
{
return err == other.err;
}
template<class Archive>
void
serialize(Archive &ar, uint32_t)
{
ar(err);
}
static constexpr uint32_t getSerializationVersion() { return 0; }
private:
Err err;
};
template <>
class Error<void>
{
template <typename T, typename TErr>
friend class Maybe;
public:
template<typename... Args> Error(Args&&...) {}
bool operator==(const Error &) const { return true; }
template<class Archive> void serialize(Archive &ar, uint32_t) {}
};
// Wrapper templated functions, useful for creating Error class since the templating matching for them is better
template <typename Err, typename... Args>
Error<Err>
genError(Args&&... args)
{
return Error<Err>(std::forward<Args>(args)...);
}
template <typename Err>
Error<Err>
genError(Err err)
{
return Error<Err>(std::forward<Err>(err));
}
template <typename T, typename TErr = std::string>
class Maybe
{
public:
// Constructors from error or from value
Maybe(const Error<TErr> &_err) : set(false), err(_err) {}
Maybe(Error<TErr> &&_err) : set(false), err(std::move(_err)) {}
Maybe(const T &_val) : set(true), val(_val) {}
Maybe(T &&_val) : set(true), val(std::move(_val)) {}
// Constructors from another error class (which is convertible to TErr)
template<typename OTErr>
Maybe(const Error<OTErr> &_err) : set(false), err(_err.err) {}
template<typename OTErr>
Maybe(Error<OTErr> &&_err) : set(false), err(std::move(_err.err)) {}
// Constructors from another maybe class (if types are convertible)
template<typename OT, typename OTErr>
Maybe(const Maybe<OT, OTErr> &m) : set(m.ok())
{
if (set) {
new (&val) T(m.unpack());
} else {
new (&err) Error<TErr>(m.getErr());
}
}
template<typename OT, typename OTErr>
Maybe(Maybe<OT, OTErr> &&m) : set(m.ok())
{
if (set) {
new (&val) T(m.unpackMove());
} else {
new (&err) Error<TErr>(m.getErr());
}
}
Maybe(const Maybe &m);
Maybe(Maybe &&m);
~Maybe();
// Comparison operators
bool operator==(const Maybe &other) const;
bool operator!=(const Maybe &other) const { return !(*this==other); }
// Assignment - you can assing a new value, a new error, or a Maybe containing either
Maybe & operator=(const T &val);
Maybe & operator=(T &&val);
template<typename OTErr>
Maybe & operator=(const Error<OTErr> &err);
template<typename OTErr>
Maybe & operator=(Error<OTErr> &&err);
Maybe & operator=(const Maybe &other);
Maybe & operator=(Maybe &&other);
// Looking inside. Will assert if trying to get value/error when you have the other.
// ok - do we have a value (true) or error (false)?
// unpack/dereference - get the inner value.
// getErr - get the error.
// passErr - get the wrapper for the error, used for forwarding errors between different kinds of Maybes
// unpackMove - get an R-value reference to the inner value.
bool ok() const { return set; }
const T & unpack() const;
// LCOV_EXCL_START Reason: This function is tested in maybe_res_ut but marked as untested
const T & operator*() const { return unpack(); }
// LCOV_EXCL_STOP
const T * operator->() const { return &unpack(); }
T && unpackMove();
TErr getErr() const;
const Error<TErr> & passErr() const;
// Customized unpack & verify - throw the requested exception type on failure
template <class Exp, typename Aggregator = ToString, typename... Args>
void verify(Args... args) const;
template <class Exp, typename Aggregator = ToString, typename... Args>
const T & unpack(Args... args) const;
std::ostream & print(std::ostream &os) const;
template<class Archive>
void
save(Archive &ar, uint32_t) const
{
ar(set);
if (set) {
ar(val);
} else {
ar(err);
}
}
template<class Archive>
void
load(Archive &ar, uint32_t)
{
bool temp_set;
ar(temp_set);
if (temp_set) {
T temp_val;
ar(temp_val);
*this = temp_val;
} else {
Error<TErr> temp_err;
ar(temp_err);
*this = temp_err;
}
}
static constexpr uint32_t getSerializationVersion() { return 0; }
private:
bool set;
union {
T val;
Error<TErr> err;
};
};
template <typename TErr>
class Maybe<void, TErr>
{
class Nothing
{
};
public:
// Since void isn't a value that we can construct from, we use default constructor instead.
Maybe() : maybe(Nothing()) {}
// Constructors from error
Maybe(const Error<TErr> &_err) : maybe(_err) {}
Maybe(Error<TErr> &&_err) : maybe(std::move(_err)) {}
// Constructors from another error class (which is convertible to TErr)
template<typename OTErr>
Maybe(const Error<OTErr> &_err) : maybe(_err) {}
template<typename OTErr>
Maybe(Error<OTErr> &&_err) : maybe(std::move(_err)) {}
Maybe(const Maybe &) = default;
Maybe(Maybe &&) = default;
~Maybe() {}
// Comparison operators
bool operator==(const Maybe &other) const { return maybe == other.maybe; }
bool operator!=(const Maybe &other) const { return !(*this==other); }
// Assignment - you can assing a new error, or a Maybe containing either error or `void`
template<typename OTErr>
Maybe & operator=(const Error<OTErr> &err) { maybe = err; return *this; }
// LCOV_EXCL_START Reason: coverage upgrade
template<typename OTErr>
Maybe & operator=(Error<OTErr> &&err) { maybe = std::move(err); return *this; }
// LCOV_EXCL_STOP
Maybe & operator=(const Maybe &other) { maybe = other.maybe; return *this; }
Maybe & operator=(Maybe &&other) { maybe = std::move(other.maybe); return *this; }
// Looking inside. Will assert if trying to get error when you have a `void`.
// ok - do we have a `void` (true) or error (false)?
// getErr - get the error.
// passErr - get the wrapper for the error, used for forwarding errors between different kinds of Maybes
bool ok() const { return maybe.ok(); }
TErr getErr() const { return maybe.getErr(); }
const Error<TErr> & passErr() const { return maybe.passErr(); }
// Customized verify - throw the requested exception type on failure
template <typename... Args>
void verify(Args... args) const { maybe.verify(std::forward(args)...); }
std::ostream &
print(std::ostream &os) const
{
if (ok()) return os << "Value()";
return os << "Error(" << getErr() << ")";
}
template<class Archive>
void
serialize(Archive &ar)
{
ar(maybe);
}
private:
Maybe<Nothing, TErr> maybe;
};
//
// Method Implementations
//
template <typename T, typename TErr>
Maybe<T, TErr>::Maybe(const Maybe<T, TErr> &m)
:
set(m.set)
{
if (set) {
new (&val) T(m.val);
} else {
new (&err) Error<TErr>(m.err);
}
}
template <typename T, typename TErr>
Maybe<T, TErr>::Maybe(Maybe<T, TErr> &&m)
:
set(m.set)
{
if (set) {
new (&val) T(std::move(m.val));
} else {
new (&err) Error<TErr>(std::move(m.err));
}
}
template <typename T, typename TErr>
Maybe<T, TErr>::~Maybe()
{
if (set) {
val.~T();
} else {
err.~Error<TErr>();
}
}
template <typename T, typename TErr>
bool
Maybe<T, TErr>::operator==(const Maybe<T, TErr> &other) const
{
if (set != other.set) return false;
if (set) {
return val == other.val;
} else {
return err == other.err;
}
}
template <typename T, typename TErr>
const T &
Maybe<T, TErr>::unpack() const
{
dbgAssert(set) << "Maybe value is not set";
return val;
}
template <typename T, typename TErr>
T &&
Maybe<T, TErr>::unpackMove()
{
dbgAssert(set) << "No value to be moved";
return std::move(val);
}
template <typename T, typename TErr>
TErr
Maybe<T, TErr>::getErr() const
{
dbgAssert(!set) << "Maybe value is set";
return err.err;
}
template <typename T, typename TErr>
const Error<TErr> &
Maybe<T, TErr>::passErr() const
{
dbgAssert(!set) << "Maybe value is set";
return err;
}
template <typename T, typename TErr>
template <class Exp, typename Aggregator, typename... Args>
void
Maybe<T, TErr>::verify(Args... args) const
{
if (!set) throw Exp(Aggregator(std::forward<Args>(args)..., err.err));
}
template <typename T, typename TErr>
template <class Exp, typename Aggregator, typename... Args>
const T &
Maybe<T, TErr>::unpack(Args... args) const
{
if (!set) throw Exp(Aggregator(std::forward<Args>(args)..., err.err));
return val;
}
template <typename T, typename TErr>
Maybe<T, TErr> &
Maybe<T, TErr>::operator=(const T &_val)
{
if (set) {
val = _val;
} else {
err.~Error<TErr>();
set = true;
new (&val) T(_val);
}
return *this;
}
template <typename T, typename TErr>
Maybe<T, TErr> &
Maybe<T, TErr>::operator=(T &&_val)
{
if (set) {
val = std::move(_val);
} else {
err.~Error<TErr>();
set = true;
new (&val) T(std::move(_val));
}
return *this;
}
template <typename T, typename TErr>
template<typename OTErr>
Maybe<T, TErr> &
Maybe<T, TErr>::operator=(const Error<OTErr> &_err)
{
if (!set) {
err = _err.err;
} else {
val.~T();
set = false;
new (&err) Error<TErr>(_err.err);
}
return *this;
}
template <typename T, typename TErr>
template<typename OTErr>
Maybe<T, TErr> &
Maybe<T, TErr>::operator=(Error<OTErr> &&_err)
{
if (!set) {
err = std::move(_err.err);
} else {
val.~T();
set = false;
new (&err) Error<TErr>(std::move(_err.err));
}
return *this;
}
template <typename T, typename TErr>
Maybe<T, TErr> &
Maybe<T, TErr>::operator=(const Maybe<T, TErr> &other)
{
if (other.set) {
*this = other.val;
} else {
*this = other.err;
}
return *this;
}
template <typename T, typename TErr>
Maybe<T, TErr> &
Maybe<T, TErr>::operator=(Maybe<T, TErr> &&other)
{
if (other.set) {
*this = std::move(other.val);
} else {
*this = std::move(other.err);
}
return *this;
}
template <typename T, typename TErr>
std::ostream &
Maybe<T, TErr>::print(std::ostream &os) const
{
if (ok()) return os << "Value(" << unpack() << ")";
return os << "Error(" << getErr() << ")";
}
// Formatting operator. Prints either the value or the error.
template <typename T, typename TErr>
std::ostream &
operator<<(std::ostream &os, const Maybe<T, TErr> &maybe)
{
return maybe.print(os);
}
#endif // __MAYBE_RES_H__

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __REST_SERVER__
#define __REST_SERVER__
#include <memory>
#include "i_rest_api.h"
#include "i_mainloop.h"
#include "singleton.h"
#include "i_environment.h"
#include "component.h"
class RestServer
:
public Component,
Singleton::Provide<I_RestApi>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_Environment>
{
public:
RestServer();
~RestServer();
void preload();
void init();
void fini();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __REST_SERVER__

View File

@@ -0,0 +1,91 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __SCOPE_EXIT_H__
#define __SCOPE_EXIT_H__
// Scope Exit - executes some code when an instance of this class falls off scope.
// This is very useful when a clean-up operation is needed due to pre-mature / exception.
// Example usage:
//
// work_t work;
// auto guard = scope_guard([&]{ work.rollback(); }); // access to local/class vars allowed in code
// work.do_work(); // may throw
// if (!work.success()) return false; // premature exit
// guard.release();
// return true;
//
//
// Code taken from N4189 pending standard - www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189.pdf
// (with some minor adaptations for C++11)
#include <type_traits> // For ::std::remove_reference
namespace std
{
template <typename EF>
class scope_exit
{
// private must come first due to use of noexcept in dtor
private:
EF exit_function;
bool execute_on_destruction;
public:
// ctor
explicit scope_exit(EF &&f) noexcept
:
exit_function(::std::move(f)),
execute_on_destruction{true}
{}
// dtor
~scope_exit()
{
if (execute_on_destruction) this->exit_function();
}
// LCOV_EXCL_START Reason: coverage upgrade
// move
scope_exit(scope_exit &&rhs) noexcept
:
exit_function(::std::move(rhs.exit_function)),
execute_on_destruction{rhs.execute_on_destruction}
{
rhs.release();
}
// LCOV_EXCL_STOP
void
release() noexcept
{
this->execute_on_destruction = false;
}
private:
scope_exit(const scope_exit &) = delete;
void operator=(const scope_exit &) = delete;
scope_exit & operator=(scope_exit &&) = delete;
};
template <typename EF>
scope_exit<typename ::std::remove_reference<EF>::type>
make_scope_exit(EF &&exit_function) noexcept
{
return scope_exit<typename ::std::remove_reference<EF>::type>(::std::forward<EF>(exit_function));
}
} // namespace std
#endif // __SCOPE_EXIT_H__

View File

@@ -0,0 +1,71 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __SHMPKTQUEUE_H__
#define __SHMPKTQUEUE_H__
#include <net/ethernet.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct shm_pkt_queue_stub shm_pkt_queue_stub;
typedef enum {
shmq_msg_mode_l2 = 0x01, // Layer 2 packet
shmq_msg_mode_l3 = 0x02, // Layer 3 packet
shmq_msg_mode_bb = 0x04, // Packet need to be bounced back to incoming interface
} shmq_msg_mode;
typedef enum {
shmq_msg_no_proto = 0,
shmq_msg_proto_ipv4 = ETHERTYPE_IP, // Internet Protocol version 4
shmq_msg_proto_ipv6 = ETHERTYPE_IPV6, // Internet Protocol version 6
} shm_pkt_msg_proto;
typedef struct {
uint16_t mode; // Of type: shmq_msg_mode. Message mode flags.
uint16_t l3_proto; // Of type: shm_pkt_msg_proto. Message protocol: IPv4/IPv6, etc.
uint16_t len; // Data length
uint16_t maclen; // MAC header length. TODO: Remove it. Data content should be is enough
uint16_t if_index; // VPP Interface index
unsigned char data[0];
} shm_pkt_queue_msg_hdr;
shm_pkt_queue_stub *get_shm_pkt_queue_id();
int init_shm_pkt_queue(shm_pkt_queue_stub *id, const char *shm_name, const char *queue_name);
int push_to_shm_pkt_queue(
shm_pkt_queue_stub *id,
const unsigned char *msg,
uint16_t length,
shmq_msg_mode mode,
shm_pkt_msg_proto l3_proto,
uint16_t l2_length,
uint16_t if_index
);
unsigned char *pop_from_shm_pkt_queue(shm_pkt_queue_stub *id);
int is_shm_pkt_queue_empty(shm_pkt_queue_stub *id);
void delete_shm_pkt_queue(shm_pkt_queue_stub *id);
#ifdef __cplusplus
}
#endif
#endif // __SHMPKTQUEUE_H__

View File

@@ -0,0 +1,75 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
#include <typeinfo>
#include <typeindex>
#include <map>
#include <set>
#include <memory>
#include "common.h"
class Singleton
{
public:
template <class I_Face>
class Provide;
template <class I_Face>
class Consume;
template <typename T>
static bool exists();
// Dealing with owned singletons - see advanced topics.
class OwnedSingleton;
template <typename DerivedFromOwnedSingleton>
static DerivedFromOwnedSingleton * getOwned();
template <typename DerivedFromOwnedSingleton>
static bool existsOwned();
template <typename DerivedFromOwnedSingleton, typename ... BuildArgs>
static void newOwned(BuildArgs ... args);
template <typename DerivedFromOwnedSingleton>
static void deleteOwned();
template <typename DerivedFromOwnedSingleton>
static void setOwned(std::unique_ptr<DerivedFromOwnedSingleton> &&ptr);
private:
Singleton() {}
template <typename I_Face>
static I_Face * get();
static void registerSingleton(std::type_index type, void *ptr);
static void unregisterSingleton(std::type_index type, void *ptr);
static void * get(const std::type_index &index);
static bool exists(const std::type_index &index);
static std::map<std::type_index, std::set<void *>> singles;
static std::map<std::type_index, std::unique_ptr<OwnedSingleton>> owned_singles;
};
template <typename Component, typename Interface>
Interface * getInterface();
#include "impl/singleton.h"
#endif // __SINGLETON_H__

View File

@@ -0,0 +1,51 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TABLE_H__
#define __TABLE_H__
#include <memory>
#include "i_table.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "i_time_get.h"
#include "singleton.h"
#include "component.h"
template <typename Key>
class Table
:
public Component,
Singleton::Provide<I_Table>,
Singleton::Provide<I_TableSpecific<Key>>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_MainLoop>
{
public:
Table();
~Table();
void init();
void fini();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#include "table/table_impl.h"
#endif // __TABLE_H__

View File

@@ -0,0 +1,220 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ENTRY_IMPL_H__
#define __ENTRY_IMPL_H__
#ifndef __TABLE_IMPL_H__
#error "entry_impl.h should not be included directly"
#endif // __TABLE_IMPL_H__
USE_DEBUG_FLAG(D_TABLE);
template <typename Key>
class Table<Key>::Impl::Entry
{
using KeyNodePtr = TableHelper::KeyNodePtr<Key>;
using ListInterface = TableHelper::I_InternalTableList<KeyNodePtr> *;
using ExpirationInterface = TableHelper::I_InternalTableExpiration<Key, ExpIter> *;
using ms = std::chrono::microseconds;
public:
Entry(ListInterface _table, ExpirationInterface _expiration, const Key &key, const KeyNodePtr &ptr, ms expire);
bool hasState(std::type_index index) const;
bool createState(const std::type_index &index, std::unique_ptr<TableOpaqueBase> &&ptr);
bool delState(const std::type_index &index);
TableOpaqueBase * getState(const std::type_index &index);
void addKey(const Key &key, const KeyNodePtr &ptr);
void removeSelf();
void setExpiration(ms expire);
std::chrono::microseconds getExpiration();
std::vector<Key> getKeys();
void uponEnteringContext();
void uponLeavingContext();
template <class Archive>
void save(Archive &ar) const;
template <class Archive>
void load(Archive &ar);
private:
ListInterface table;
ExpirationInterface expiration;
std::unordered_map<Key, KeyNodePtr> keys;
std::unordered_map<std::type_index, std::unique_ptr<TableOpaqueBase>> opaques;
ExpIter expr_iter;
};
template <typename Key>
Table<Key>::Impl::Entry::Entry(
ListInterface _table,
ExpirationInterface _expiration,
const Key &key,
const KeyNodePtr &ptr,
ms expire)
:
table(_table),
expiration(_expiration)
{
addKey(key, ptr);
expr_iter = expiration->addExpiration(expire, key);
}
template <typename Key>
bool
Table<Key>::Impl::Entry::hasState(std::type_index index) const
{
return opaques.count(index) != 0;
}
template <typename Key>
bool
Table<Key>::Impl::Entry::createState(const std::type_index &index, std::unique_ptr<TableOpaqueBase> &&ptr)
{
if (hasState(index)) {
dbgError(D_TABLE) << "Failed to recreate a state of type " << index.name();
return false;
}
dbgTrace(D_TABLE) << "Creating a state of type " << index.name();
return opaques.emplace(index, std::move(ptr)).second;
}
template <typename Key>
bool
Table<Key>::Impl::Entry::delState(const std::type_index &index)
{
dbgTrace(D_TABLE) << "Deleting state of type " << index.name();
auto iter = opaques.find(index);
if (iter == opaques.end()) return false;
opaques.erase(iter);
return true;
}
template <typename Key>
TableOpaqueBase *
Table<Key>::Impl::Entry::getState(const std::type_index &index)
{
auto iter = opaques.find(index);
if (iter == opaques.end()) return nullptr;
return iter->second.get();
}
template <typename Key>
void
Table<Key>::Impl::Entry::addKey(const Key &key, const KeyNodePtr &ptr)
{
keys[key] = ptr;
}
template <typename Key>
void
Table<Key>::Impl::Entry::removeSelf()
{
expiration->removeExpiration(expr_iter);
for (auto &iter : keys) {
table->removeKey(iter.second);
}
keys.clear();
opaques.clear();
}
template <typename Key>
void
Table<Key>::Impl::Entry::setExpiration(ms expire)
{
expiration->removeExpiration(expr_iter);
expr_iter = expiration->addExpiration(expire, keys.begin()->first);
}
template <typename Key>
std::chrono::microseconds
Table<Key>::Impl::Entry::getExpiration()
{
return expr_iter->getExpiration();
}
template <typename Key>
std::vector<Key>
Table<Key>::Impl::Entry::getKeys()
{
std::vector<Key> keys_vec;
keys_vec.reserve(keys.size());
for (auto &iter : keys) {
keys_vec.emplace_back(iter.first);
}
return keys_vec;
}
template <typename Key>
void
Table<Key>::Impl::Entry::uponEnteringContext()
{
for (auto &opauqe : opaques) {
opauqe.second->uponEnteringContext();
}
}
template <typename Key>
void
Table<Key>::Impl::Entry::uponLeavingContext()
{
for (auto &opauqe : opaques) {
opauqe.second->uponLeavingContext();
}
}
template <typename Key>
template <class Archive>
void
Table<Key>::Impl::Entry::save(Archive &ar) const
{
std::vector<std::string> opaque_names;
opaque_names.reserve(opaques.size());
for (auto &iter : opaques) {
opaque_names.emplace_back(iter.second->nameOpaque());
}
ar(cereal::make_nvp("opaque_names", opaque_names));
for (auto &iter : opaques) {
// 0 is used currently until supporting versions
iter.second->saveOpaque(ar, 0);
}
}
template <typename Key>
template <class Archive>
void
Table<Key>::Impl::Entry::load(Archive &ar)
{
std::vector<std::string> opaque_names;
ar(cereal::make_nvp("opaque_names", opaque_names));
auto &rep = ::cereal::detail::StaticObject<TableOpaqueRep>::getInstance();
for (auto &iter : opaque_names) {
auto opaque = rep.getOpaqueByName(iter);
if (!opaque) {
dbgTrace(D_TABLE) << "Failed to load synced opaque " << iter;
return;
}
// 0 is used currently until supporting versions
opaque->loadOpaque(ar, 0);
if (!createState(typeid(*opaque), move(opaque))) {
dbgError(D_TABLE) << "Failed to create the state for opaque " << iter;
}
}
}
#endif // __ENTRY_IMPL_H__

View File

@@ -0,0 +1,117 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __EXPIRATION_IMPL_H__
#define __EXPIRATION_IMPL_H__
#ifndef __TABLE_IMPL_H__
#error "expiration_impl.h should not be included directly"
#endif // __TABLE_IMPL_H__
template <typename Key>
class Table<Key>::Impl::ExpirationEntry
{
public:
ExpirationEntry(std::chrono::microseconds _expire, const Key &_key);
bool isBeforeTime(std::chrono::microseconds other_expire) const;
const Key & getKey() const;
std::chrono::microseconds getExpiration() const;
private:
std::chrono::microseconds expire;
Key key;
};
template <typename Key>
Table<Key>::Impl::ExpirationEntry::ExpirationEntry(std::chrono::microseconds _expire, const Key &_key)
:
expire(_expire),
key(_key)
{
}
template <typename Key>
bool
Table<Key>::Impl::ExpirationEntry::isBeforeTime(std::chrono::microseconds other_expire) const
{
return expire <= other_expire;
}
template <typename Key>
const Key &
Table<Key>::Impl::ExpirationEntry::getKey() const
{
return key;
}
template <typename Key>
std::chrono::microseconds
Table<Key>::Impl::ExpirationEntry::getExpiration() const
{
return expire;
}
template <typename Key>
class Table<Key>::Impl::ExpList
:
public TableHelper::I_InternalTableExpiration<Key, ExpIter>
{
public:
ExpIter addExpiration(std::chrono::microseconds expire, const Key &key) override;
void removeExpiration(const ExpIter &iter) override;
bool shouldExpire(std::chrono::microseconds expire) const;
const Key & getEarliest() const;
private:
std::list<ExpirationEntry> list;
};
template <typename Key>
typename Table<Key>::Impl::ExpIter
Table<Key>::Impl::ExpList::addExpiration(std::chrono::microseconds expire, const Key &key)
{
// The list is ordered from the highest value (in the far future) to the lowest (in the near future).
// So we scan the list to enter before the first vlaue that is smaller than us (typically, the first one).
for (auto iter = list.begin(); iter != list.end(); iter++) {
if (iter->isBeforeTime(expire)) {
return list.emplace(iter, expire, key);
}
}
// There was no value that is closer to the current time, so it should be placed in at the end of the list.
return list.emplace(list.end(), expire, key);
}
template <typename Key>
void
Table<Key>::Impl::ExpList::removeExpiration(const ExpIter &iter)
{
list.erase(iter);
}
template <typename Key>
bool
Table<Key>::Impl::ExpList::shouldExpire(std::chrono::microseconds expire) const
{
if (list.empty()) return false;
return list.back().isBeforeTime(expire);
}
template <typename Key>
const Key &
Table<Key>::Impl::ExpList::getEarliest() const
{
dbgAssert(!list.empty()) << "Cannot access the earliest member of an empty list";
return list.back().getKey();
}
#endif // __EXPIRATION_IMPL_H__

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TABLE_HELPERS_H__
#define __TABLE_HELPERS_H__
#ifndef __TABLE_IMPL_H__
#error "table_helpers.h should not be included directly"
#endif // __TABLE_IMPL_H__
#include "context.h"
namespace TableHelper
{
class Constant
{
protected:
static const std::string primary_key;
};
template <typename KeyPtr>
class I_InternalTableList
{
public:
virtual void removeKey(const KeyPtr &key) = 0;
protected:
~I_InternalTableList() {}
};
template <typename Key, typename ExpIter>
class I_InternalTableExpiration
{
public:
virtual ExpIter addExpiration(std::chrono::microseconds expire, const Key &key) = 0;
virtual void removeExpiration(const ExpIter &iter) = 0;
protected:
~I_InternalTableExpiration() {}
};
} // TableHelper
#endif // __TABLE_HELPERS_H__

View File

@@ -0,0 +1,442 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TABLE_IMPL_H__
#define __TABLE_IMPL_H__
#ifndef __TABLE_H__
#error "table_impl.h should not be included directly"
#endif // __TABLE_H__
#include <sstream>
#include <memory>
#include <iostream>
#include "debug.h"
#include "time_print.h"
#include "singleton.h"
#include "context.h"
#include "table/table_helpers.h"
#include "table/table_list.h"
#include "table/opaque_repo.h"
#include "config.h"
USE_DEBUG_FLAG(D_TABLE);
template <typename Key>
class Table<Key>::Impl
:
public TableHelper::Constant,
public TableHelper::I_InternalTableList<TableHelper::KeyNodePtr<Key>>,
public Singleton::Provide<I_Table>::From<Table<Key>>,
public Singleton::Provide<I_TableSpecific<Key>>::template From<Table<Key>>
{
class ExpirationEntry;
using ExpIter = typename std::list<ExpirationEntry>::iterator;
class ExpList;
class Entry;
using EntryRef = std::shared_ptr<Entry>;
using EntryMap = std::unordered_map<Key, EntryRef>;
public:
void init();
void fini();
// I_InternalTableControl methods
void removeKey(const TableHelper::KeyNodePtr<Key> &key) override;
// I_Table protected methods
bool hasState (const std::type_index &index) const override;
bool createState(const std::type_index &index, std::unique_ptr<TableOpaqueBase> &&ptr) override;
bool deleteState(const std::type_index &index) override;
TableOpaqueBase * getState (const std::type_index &index) override;
// I_Table public methods
void setExpiration(std::chrono::milliseconds expire) override;
bool doesKeyExists() const override;
std::string keyToString () const override;
TableIter begin () const override;
TableIter end () const override;
// I_TableSpecific methods
bool hasEntry (const Key &key) override;
bool createEntry (const Key &key, std::chrono::microseconds expire) override;
bool deleteEntry (const Key &key) override;
bool addLinkToEntry(const Key &key, const Key &link) override;
uint count () override;
void expireEntries () override;
bool setActiveKey (const Key &key) override;
void unsetActiveKey() override;
Maybe<Key, void> getCurrentKey () const override;
void saveEntry (TableIter iter, SyncMode mode, cereal::BinaryOutputArchive &ar) const override;
void loadEntry (cereal::BinaryInputArchive &ar) override;
private:
EntryRef getCurrEntry() const;
// Members
EntryMap entries;
ExpList expiration;
TableHelper::KeyList<Key> list;
Context ctx;
// Interfaces
I_TimeGet *timer = nullptr;
I_Environment *env = nullptr;
};
#include "table/entry_impl.h"
#include "table/expiration_impl.h"
template <typename Key>
void
Table<Key>::Impl::init()
{
env = Singleton::Consume<I_Environment>::by<Table<Key>>();
timer = Singleton::Consume<I_TimeGet>::by<Table<Key>>();
auto mainloop = Singleton::Consume<I_MainLoop>::by<Table<Key>>();
mainloop->addRecurringRoutine(
I_MainLoop::RoutineType::Timer,
std::chrono::milliseconds(100),
[&] () { expireEntries(); },
"Delete expired table entries"
);
}
template <typename Key>
void
Table<Key>::Impl::fini()
{
while (count() > 0) {
deleteEntry(expiration.getEarliest());
}
env = nullptr;
timer = nullptr;
}
template <typename Key>
void
Table<Key>::Impl::removeKey(const TableHelper::KeyNodePtr<Key> &key)
{
if (!key) {
dbgError(D_TABLE) << "Function called without a key";
return;
}
auto iter = entries.find(key->getKey());
if (iter == entries.end()) {
dbgError(D_TABLE) << "Trying to remove a non-existing key " << key;
return;
}
dbgTrace(D_TABLE) << "Removing the key " << key;
entries.erase(iter);
list.removeKey(key);
}
template <typename Key>
bool
Table<Key>::Impl::hasState(const std::type_index &index) const
{
dbgTrace(D_TABLE) << "Checking if there is a state of type " << index.name();
auto entry = getCurrEntry();
if (!entry) return false;
return entry->hasState(index);
}
template <typename Key>
bool
Table<Key>::Impl::createState(const std::type_index &index, std::unique_ptr<TableOpaqueBase> &&ptr)
{
auto ent = getCurrEntry();
if (ent == nullptr) {
dbgError(D_TABLE) << "Trying to create a state without an entry";
return false;
}
return ent->createState(index, std::move(ptr));
}
template <typename Key>
bool
Table<Key>::Impl::deleteState(const std::type_index &index)
{
auto ent = getCurrEntry();
if (ent) return ent->delState(index);
return false;
}
template <typename Key>
TableOpaqueBase *
Table<Key>::Impl::getState(const std::type_index &index)
{
auto ent = getCurrEntry();
dbgTrace(D_TABLE) << "Getting a state of type " << index.name();
if (!ent) return nullptr;
return ent->getState(index);
}
template <typename Key>
void
Table<Key>::Impl::setExpiration(std::chrono::milliseconds expire)
{
auto ent = getCurrEntry();
if (!ent) return;
auto curr_time = timer->getMonotonicTime();
ent->setExpiration(curr_time + expire);
}
template <typename Key>
bool
Table<Key>::Impl::doesKeyExists() const
{
auto key = env->get<Key>(primary_key);
if (!key.ok()) return false;
return entries.find(key.unpack()) != entries.end();
}
template <typename Key>
std::string
Table<Key>::Impl::keyToString() const
{
auto key = env->get<Key>(primary_key);
if (!key.ok()) return "";
std::ostringstream os;
os << key.unpack();
return os.str();
}
template <typename Key>
TableIter
Table<Key>::Impl::begin() const
{
return TableIter(list.begin());
}
template <typename Key>
TableIter
Table<Key>::Impl::end() const
{
return TableIter(list.end());
}
template <typename Key>
bool
Table<Key>::Impl::hasEntry(const Key &key)
{
return entries.find(key) != entries.end();
}
template <typename Key>
bool
Table<Key>::Impl::createEntry(const Key &key, std::chrono::microseconds expire)
{
if (entries.find(key) != entries.end()) {
dbgWarning(D_TABLE) << "Trying to recreate an entry with the key " << key;
return false;
}
auto curr_time = timer->getMonotonicTime();
auto expire_time = curr_time + expire;
dbgTrace(D_TABLE) << "Creating an entry with the key " << key << " for " << expire;
entries.emplace(key, std::make_shared<Entry>(this, &expiration, key, list.addKey(key), expire_time));
return true;
}
template <typename Key>
bool
Table<Key>::Impl::deleteEntry(const Key &key)
{
auto iter = entries.find(key);
if (iter == entries.end()) {
dbgWarning(D_TABLE) << "Trying to delete a non-existing entry of the key " << key;
return false;
}
auto ent = iter->second; // Important, since we don't want the entry to disappear before we leave the function.
dbgTrace(D_TABLE) << "Deleting an entry of the key " << key;
ent->removeSelf();
return true;
}
template <typename Key>
bool
Table<Key>::Impl::addLinkToEntry(const Key &key, const Key &link)
{
auto iter = entries.find(key);
if (iter == entries.end()) {
dbgWarning(D_TABLE) << "No entry, to which to add a key";
return false;
}
bool success = entries.emplace(link, iter->second).second;
if (!success) {
dbgWarning(D_TABLE) << "Attempting to re-enter a key " <<link;
return false;
}
dbgTrace(D_TABLE) << "Linking the key " << link << " with the key " << key;
iter->second->addKey(link, list.addKey(link));
return true;
}
template <typename Key>
uint
Table<Key>::Impl::count()
{
return entries.size();
}
template <typename Key>
void
Table<Key>::Impl::expireEntries()
{
auto curr_time = timer->getMonotonicTime();
while (expiration.shouldExpire(curr_time)) {
auto key = expiration.getEarliest();
ScopedContext ctx;
ctx.registerValue(primary_key, key);
deleteEntry(key);
}
}
template <typename Key>
bool
Table<Key>::Impl::setActiveKey(const Key &key)
{
auto entry = entries.find(key);
if (entry == entries.end()) return false;
ctx.registerValue(primary_key, key);
ctx.activate();
entry->second->uponEnteringContext();
return true;
}
template <typename Key>
void
Table<Key>::Impl::unsetActiveKey()
{
auto entry = getCurrEntry();
if (entry == nullptr) {
dbgError(D_TABLE) << "Unsetting the active key when there is no active entry";
return;
}
entry->uponLeavingContext();
ctx.deactivate();
}
template <typename Key>
Maybe<Key, void>
Table<Key>::Impl::getCurrentKey() const
{
return env->get<Key>(primary_key);
}
template <typename Key>
typename Table<Key>::Impl::EntryRef
Table<Key>::Impl::getCurrEntry() const
{
auto key = env->get<Key>(primary_key);
if (!key.ok()) {
dbgTrace(D_TABLE) << "Key was not found";
return nullptr;
}
auto iter = entries.find(key.unpack());
if (iter == entries.end()) {
dbgTrace(D_TABLE) << "No entry matches the key " << key.unpack();
return nullptr;
}
return iter->second;
}
template <typename Key>
void
Table<Key>::Impl::saveEntry(TableIter iter, SyncMode mode, cereal::BinaryOutputArchive &ar) const
{
iter.setEntry();
auto ent = getCurrEntry();
ar(
cereal::make_nvp("keys_vec", ent->getKeys()),
cereal::make_nvp("expire", ent->getExpiration())
);
ent->save(ar);
if (mode == SyncMode::TRANSFER_ENTRY) {
std::string key = keyToString();
ent->removeSelf();
dbgTrace(D_TABLE) << "Key '" << key <<"' was removed";
}
iter.unsetEntry();
}
template <typename Key>
void
Table<Key>::Impl::loadEntry(cereal::BinaryInputArchive &ar)
{
std::vector<Key> keys_vec;
std::chrono::microseconds expire;
ar(
cereal::make_nvp("keys_vec", keys_vec),
cereal::make_nvp("expire", expire)
);
if (keys_vec.size() == 0) {
dbgError(D_TABLE) << "No Keys to load";
return;
}
if (!createEntry(keys_vec[0], expire)) {
dbgError(D_TABLE) << "Cannot create a new entry";
return;
}
for (decltype(keys_vec.size()) index=1; index<keys_vec.size(); index++) {
if (!addLinkToEntry(keys_vec[0], keys_vec[index])) {
dbgError(D_TABLE) << "Cannot add link to an entry";
return;
}
}
auto entry_iter = entries.find(keys_vec[0]);
if (entry_iter == entries.end()) return;
auto ent = entry_iter->second;
ent->load(ar);
}
template <typename Key>
Table<Key>::Table() : Component("Table"), pimpl(std::make_unique<Table::Impl>())
{
}
template <typename Key>
Table<Key>::~Table()
{
}
template <typename Key>
void
Table<Key>::init()
{
pimpl->init();
}
template <typename Key>
void
Table<Key>::fini()
{
pimpl->fini();
}
template <typename Key>
void
Table<Key>::preload()
{
}
#endif // __TABLE_IMPL_H__

View File

@@ -0,0 +1,96 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TABLE_LIST_H__
#define __TABLE_LIST_H__
#ifndef __TABLE_IMPL_H__
#error "table_list.h should not be included directly"
#endif // __TABLE_IMPL_H__
#include "table/table_list_node.h"
#include "table/table_list_iter.h"
#include "debug.h"
USE_DEBUG_FLAG(D_TABLE);
namespace TableHelper
{
template <typename Key>
class KeyList
{
public:
KeyNodePtr<Key> addKey(const Key &key);
void removeKey(const KeyNodePtr<Key> &val);
std::shared_ptr<I_TableIter> begin() const;
std::shared_ptr<I_TableIter> end() const;
private:
KeyNodePtr<Key> first;
KeyNodePtr<Key> last;
};
template <typename Key>
KeyNodePtr<Key>
KeyList<Key>::addKey(const Key &key)
{
KeyNodePtr<Key> new_entry = std::make_shared<KeyNode<Key>>(key);
if (!first) {
first = new_entry;
} else {
last->setNext(new_entry);
}
last = new_entry;
return new_entry;
};
template <typename Key>
void
KeyList<Key>::removeKey(const KeyNodePtr<Key> &val)
{
val->deactivate();
if (val == first) {
first = first->getNext();
if (last == val) last = first; // `val` was the only member of the list
return;
}
for (auto iter = first; iter != last; iter = iter->getNext()) {
if (iter->getNext() == val) {
if (iter->getNext() == last) last = iter;
iter->setNext(iter->getNext()->getNext());
return;
}
}
dbgError(D_TABLE) << "Iterator was not found in the table key list";
}
template <typename Key>
std::shared_ptr<I_TableIter>
KeyList<Key>::begin() const
{
return std::make_shared<KeyNodeIter<Key>>(first);
}
template <typename Key>
std::shared_ptr<I_TableIter>
KeyList<Key>::end() const
{
return std::make_shared<KeyNodeIter<Key>>(nullptr);
}
} // TableHelper
#endif // __TABLE_LIST_H__

View File

@@ -0,0 +1,98 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TABLE_LIST_ITER_H__
#define __TABLE_LIST_ITER_H__
#ifndef __TABLE_IMPL_H__
#error "table_list_iter.h should not be included directly"
#endif // __TABLE_IMPL_H__
#include "context.h"
namespace TableHelper
{
template <typename Key>
class KeyNodeIter : public I_TableIter, public Constant
{
public:
KeyNodeIter(const KeyNodePtr<Key> &iter);
void operator++() override;
void operator++(int) override;
void setEntry() override;
void unsetEntry() override;
void * getUniqueId() const override;
private:
void moveNext();
KeyNodePtr<Key> curr;
Context ctx;
};
template <typename Key>
KeyNodeIter<Key>::KeyNodeIter(const KeyNodePtr<Key> &iter) : curr(iter)
{
}
template <typename Key>
void
KeyNodeIter<Key>::operator++()
{
moveNext();
}
template <typename Key>
void
KeyNodeIter<Key>::operator++(int)
{
moveNext();
}
template <typename Key>
void
KeyNodeIter<Key>::setEntry()
{
if (!curr || !curr->isActive()) return;
ctx.registerValue(primary_key, curr->getKey());
ctx.activate();
}
template <typename Key>
void
KeyNodeIter<Key>::unsetEntry()
{
ctx.deactivate();
}
template <typename Key>
void *
KeyNodeIter<Key>::getUniqueId() const
{
return curr.get();
}
template <typename Key>
void
KeyNodeIter<Key>::moveNext()
{
while (curr != nullptr) {
curr = curr->getNext();
if (curr != nullptr && curr->isActive()) return;
}
}
} // TableHelper
#endif // __TABLE_LIST_ITER_H__

View File

@@ -0,0 +1,87 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TABLE_LIST_NODE_H__
#define __TABLE_LIST_NODE_H__
#ifndef __TABLE_IMPL_H__
#error "table_list_node.h should not be included directly"
#endif // __TABLE_IMPL_H__
namespace TableHelper
{
template <typename Key>
class KeyNode
{
public:
KeyNode(const Key &_key);
void setNext(const std::shared_ptr<KeyNode> &_next);
const Key & getKey() const;
const std::shared_ptr<KeyNode> & getNext() const;
bool isActive() const;
void deactivate();
private:
Key key;
std::shared_ptr<KeyNode> next;
bool is_active = true;
};
template <typename Key>
KeyNode<Key>::KeyNode(const Key &_key)
:
key(_key)
{
}
template <typename Key>
void
KeyNode<Key>::setNext(const std::shared_ptr<KeyNode> &_next)
{
next = _next;
}
template <typename Key>
const Key &
KeyNode<Key>::getKey() const
{
return key;
}
template <typename Key>
const std::shared_ptr<KeyNode<Key>> &
KeyNode<Key>::getNext() const
{
return next;
}
template <typename Key>
bool
KeyNode<Key>::isActive() const
{
return is_active;
}
template <typename Key>
void
KeyNode<Key>::deactivate()
{
is_active = false;
}
template <typename Key> using KeyNodePtr = std::shared_ptr<KeyNode<Key>>;
} // TableHelper
#endif // __TABLE_LIST_NODE_H__

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TENANT_MANAGER_H__
#define __TENANT_MANAGER_H__
#include "i_tenant_manager.h"
#include "i_messaging.h"
#include "i_rest_api.h"
#include "i_mainloop.h"
#include "i_time_get.h"
#include "i_instance_awareness.h"
#include "component.h"
enum class TenantManagerType { CLIENT, SERVER };
class TenantManager
:
public Component,
Singleton::Provide<I_TenantManager>,
Singleton::Consume<I_InstanceAwareness>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_RestApi>
{
public:
TenantManager();
~TenantManager();
void preload() override;
void init() override;
void fini() override;
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __TENANT_MANAGER_H__

View File

@@ -0,0 +1,34 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TIME_PROXY_H__
#define __TIME_PROXY_H__
#include <memory>
#include "i_time_get.h"
#include "i_time_set.h"
#include "singleton.h"
#include "component.h"
class TimeProxyComponent : public Component, Singleton::Provide<I_TimeGet>, Singleton::Provide<I_TimeSet>
{
public:
TimeProxyComponent();
~TimeProxyComponent();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __TIME_PROXY_H__

View File

@@ -0,0 +1,72 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TOSTRING_H__
#define __TOSTRING_H__
#include <sstream>
class ToString
{
public:
ToString() {}
template<typename T, typename... Args>
ToString(const T &obj, Args... args)
{
str << obj << static_cast<std::string>(ToString(std::forward<Args>(args)...));
}
template<typename T>
ToString &
operator<<(const T &obj)
{
str << obj;
return *this;
}
template<typename T>
bool
operator==(const T &obj) const
{
return *this == ToString(obj);
}
template<typename T>
bool
operator!=(const T &obj) const
{
return *this != ToString(obj);
}
bool
operator==(const ToString &other) const
{
return str.str() == other.str.str();
}
bool
operator!=(const ToString &other) const
{
return str.str() != other.str.str();
}
void reset() { str.str(""); }
operator std::string() { return str.str(); }
private:
std::ostringstream str;
};
#endif // __TOSTRING_H__

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __AGENT_DETAILS_REPORTER_H__
#define __AGENT_DETAILS_REPORTER_H__
#include "singleton.h"
#include "component.h"
#include "i_agent_details_reporter.h"
#include "i_messaging.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "i_rest_api.h"
#include "component.h"
class AgentDetailsReporter
:
public Component,
Singleton::Provide<I_AgentDetailsReporter>,
Singleton::Consume<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_RestApi>
{
public:
AgentDetailsReporter();
~AgentDetailsReporter();
void preload() override;
void init() override;
void fini() override;
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __AGENT_DETAILS_REPORTER_H__

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HTTP_ENCODER_H__
#define __HTTP_ENCODER_H__
#include <string>
#include "messaging/http_core.h"
class HTTPRequest
{
public:
HTTPRequest() = default;
HTTPRequest(const std::string &_method_statement);
HTTPRequest(const std::string &_method_statement, const std::string &_host, const bool to_proxy);
HTTPRequest & insertHeader(const std::string &header_key, const std::string &header_val);
HTTPRequest & insertHeader(const std::string &header);
HTTPRequest & insertHeaders(const std::string &rec_headers);
HTTPRequest & insertBody(const std::string &body);
std::string toString() const;
private:
std::string method_statement;
HTTPHeaders headers;
std::string body;
};
class ConnectRequest: public HTTPRequest
{
public:
ConnectRequest(const std::string &_host, const std::string &_port);
};
class PostRequest: public HTTPRequest
{
public:
PostRequest(const std::string &_post_path, const std::string &_host, bool to_proxy);
};
class PutRequest : public HTTPRequest
{
public:
PutRequest(const std::string &_put_path, const std::string &_host, bool to_proxy);
};
class GetRequest: public HTTPRequest
{
public:
GetRequest(const std::string &_get_path, const std::string &_host, bool to_proxy);
};
class PatchRequest: public HTTPRequest
{
public:
PatchRequest(const std::string &_patch_path, const std::string &_host, bool to_proxy);
};
class HTTPEncoder
{
public:
HTTPEncoder(const std::string &_host, const std::string &_port);
HTTPRequest & Connect();
HTTPRequest & Post(const std::string &post_path);
HTTPRequest & Put(const std::string &put_path);
HTTPRequest & Patch(const std::string &patch_path);
HTTPRequest & Get(const std::string &get_path);
HTTPEncoder & isOverProxy();
HTTPEncoder & isOverSSL();
std::string build() const { return request.toString(); }
private:
HTTPRequest request;
std::string host;
std::string port;
bool over_ssl = false;
bool over_proxy = false;
};
#endif // __HTTP_ENCODER_H__

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __INSTANCE_AWARENESS_H__
#define __INSTANCE_AWARENESS_H__
#include <string>
#include <memory>
#include "singleton.h"
#include "i_instance_awareness.h"
#include "component.h"
class InstanceAwareness : public Component, public Singleton::Provide<I_InstanceAwareness>
{
public:
InstanceAwareness();
~InstanceAwareness();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __INSTANCE_AWARENESS_H__

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __IOCTL_IS_H__
#define __IOCTL_IS_H__
#include <memory>
#include "singleton.h"
#include "i_ioctl.h"
#include "i_mainloop.h"
#include "i_shell_cmd.h"
#include "component.h"
class IoctlIS
:
public Component,
Singleton::Provide<I_Ioctl>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_ShellCmd>
{
public:
IoctlIS();
~IoctlIS();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __IOCTL_IS_H__

View File

@@ -0,0 +1,96 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __MAINLOOP_METRIC_H__
#define __MAINLOOP_METRIC_H__
#include "generic_metric.h"
class MainloopEvent : public Event<MainloopEvent>
{
public:
void
setTimeSlice(int value)
{
time_slice_used = value;
}
void
setSleepTime(uint64_t value)
{
sleep_time_used = value;
}
void
setStressValue(uint32_t value)
{
current_stress_used = value;
}
uint32_t
getTimeSlice() const
{
return time_slice_used;
}
uint32_t
getSleepTime() const
{
return sleep_time_used;
}
uint32_t
getStressValue() const
{
return current_stress_used;
}
private:
uint32_t time_slice_used = 0;
uint64_t sleep_time_used = 0;
uint32_t current_stress_used = 0;
};
class MainloopMetric
:
public GenericMetric,
public Listener<MainloopEvent>
{
public:
void
upon(const MainloopEvent &event) override
{
max_time_slice.report(event.getTimeSlice());
avg_time_slice.report(event.getTimeSlice());
last_report_time_slice.report(event.getTimeSlice());
max_sleep_time.report(event.getSleepTime());
avg_sleep_time.report(event.getSleepTime());
last_report_sleep_time.report(event.getSleepTime());
max_stress_value.report(event.getStressValue());
avg_stress_value.report(event.getStressValue());
last_report_stress_value.report(event.getStressValue());
}
private:
MetricCalculations::Max<uint32_t> max_time_slice{this, "mainloopMaxTimeSliceSample", 0};
MetricCalculations::Average<double> avg_time_slice{this, "mainloopAvgTimeSliceSample"};
MetricCalculations::LastReportedValue<uint32_t> last_report_time_slice{this, "mainloopLastTimeSliceSample"};
MetricCalculations::Max<uint64_t> max_sleep_time{this, "mainloopMaxSleepTimeSample", 0};
MetricCalculations::Average<double> avg_sleep_time{this, "mainloopAvgSleepTimeSample"};
MetricCalculations::LastReportedValue<uint32_t> last_report_sleep_time{this, "mainloopLastSleepTimeSample"};
MetricCalculations::Max<uint32_t> max_stress_value{this, "mainloopMaxStressValueSample", 0};
MetricCalculations::Average<double> avg_stress_value{this, "mainloopAvgStressValueSample"};
MetricCalculations::LastReportedValue<uint32_t> last_report_stress_value{this, "mainloopLastStressValueSample"};
};
#endif // __MAINLOOP_METRIC_H__

View File

@@ -0,0 +1,52 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __MESSAGING_BUFFER_H__
#define __MESSAGING_BUFFER_H__
#include <memory>
#include "i_messaging_buffer.h"
#include "i_time_get.h"
#include "i_instance_awareness.h"
#include "singleton.h"
#include "i_encryptor.h"
#include "i_environment.h"
#include "i_mainloop.h"
#include "component.h"
class MessagingBuffer
:
public Component,
Singleton::Provide<I_MessagingBuffer>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_InstanceAwareness>,
Singleton::Consume<I_Encryptor>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_MainLoop>
{
public:
MessagingBuffer();
~MessagingBuffer();
void preload();
void init();
void fini();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __MESSAGING_BUFFER_H__

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __BUCKET_MANAGER_H__
#define __BUCKET_MANAGER_H__
#include <unordered_map>
#include <iterator>
#include <string>
#include "event_queue.h"
#include "instance_awareness.h"
#include "i_time_get.h"
#include "i_encryptor.h"
using bucketName = std::string;
class BucketManager
{
public:
void init(const std::string &service_name);
void fini();
bool doesExist(const bucketName &);
void push(const bucketName &, std::string &&);
bool handleNextBucket();
bool hasValue();
EventQueue & peek();
void flush();
private:
std::string resolveFilesName(const std::string &file_name);
std::string buffer_directory = "";
std::string next_bucket = "";
std::string service_name = "";
std::string management_file_path = "";
uint buffer_max_size = 0; // in MB
uint max_buffer_files = 0;
EventQueue iterator;
std::unordered_map<bucketName, EventQueue> buckets;
I_InstanceAwareness *instance_awareness = nullptr;
I_Encryptor *encryptor = nullptr;
};
#endif // __BUCKET_MANAGER_H__

View File

@@ -0,0 +1,125 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __EVENT_QUEUE_H__
#define __EVENT_QUEUE_H__
#include <boost/range/iterator_range.hpp>
#include <cstring>
#include <iostream>
#include <fstream>
#include <memory>
#include <list>
#include <chrono>
#include "i_time_get.h"
#include "maybe_res.h"
USE_DEBUG_FLAG(D_EVENT_BUFFER);
class EventQueueFile
{
public:
EventQueueFile(const std::string &_file_path)
:
file_path(_file_path)
{}
EventQueueFile(
const std::string &file_location_path,
const std::string &file_extention,
bool is_file_compressed);
EventQueueFile(const EventQueueFile &other_event, int _num_of_events_in_file, uint64_t _size_of_file)
:
file_path(other_event.file_path),
suffix(other_event.suffix),
num_of_events_in_file(_num_of_events_in_file),
size_of_file(_size_of_file)
{}
static const std::string zip_file_suffix;
const std::string & getFilePath() const { return file_path; }
bool isCompressed() const { return is_compressed; }
int getSuffix() const { return suffix; }
int getNumOfEvents() const { return num_of_events_in_file; }
uint64_t getFileSizeInBytes() const { return size_of_file; }
void restoreNumberOfLines();
void incFileSize(uint64_t size_to_add);
void handleCompression(int size_of_files_list);
void decompress(const std::string &infilename, const std::string &outfilename, bool remove_old = true);
void compress();
private:
std::string file_path;
int suffix = -1;
bool is_compressed = false;
int num_of_events_in_file = 0;
size_t size_of_file = 0;
};
class EventQueue
{
public:
EventQueue() = default;
~EventQueue();
void init(const std::string &path, uint max_buff_size);
void fini();
bool isEmpty() const;
const std::string & peek();
void push(std::string &&event_data);
void reloadEventsIntoList(const std::string &path);
Maybe<void> refreshBufferFile();
void refreshReadBuff();
void trim();
void flush();
private:
void rotate();
void updateReadFile();
void setReaderFileAndOpen(const EventQueueFile &file);
void sortEventFilesBySuffix(std::vector<EventQueueFile> &tmp_vec);
void pushNewEventQueueFile(EventQueueFile &eventFile, std::vector<EventQueueFile> &tmp_vec);
double getSizeMB(double size_in_B) const;
Maybe<void> writeCachesToFile();
void enforceMaxNumberOfFiles();
// File management
std::list<EventQueueFile> files; //front is write, back is read
std::ifstream reader;
std::ofstream writer;
// Read & write management
double max_size; // in MB
uint64_t size_on_disk; // in B
uint64_t write_cache_size; // in B
uint64_t read_cache_size; // in B
std::list<std::string> write_cache_buff;
std::list<std::string> read_cache_buff;
unsigned int num_of_events_on_disk;
unsigned int read_events_on_disk;
// Timing management
std::chrono::microseconds next_sync_freq_in_sec;
I_TimeGet *timer = nullptr;
bool is_pending_rotate = false;
bool is_pending_write = false;
};
#endif // __EVENT_QUEUE_H__

View File

@@ -0,0 +1,120 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HTTP_REQUEST_EVENT_H__
#define __HTTP_REQUEST_EVENT_H__
#include <string>
#include "cereal/archives/json.hpp"
class HTTPRequestSignature
{
public:
HTTPRequestSignature() = default;
HTTPRequestSignature(const std::string &_method, const std::string &_url, const std::string &_tag)
:
method(_method),
url(_url),
tag(_tag)
{
}
bool
operator<(const HTTPRequestSignature &other) const
{
return getSignature() < other.getSignature();
}
std::string getSignature() const { return method + url + tag; }
const std::string & getMethod() const { return method; }
const std::string & getURL() const { return url; }
const std::string & getTag() const { return tag; }
template<class Archive>
void load(Archive &ar)
{
try {
ar(cereal::make_nvp("tag", tag));
} catch(...) {
tag = "buffered messages";
ar.setNextName(nullptr);
}
ar(method, url);
}
template<class Archive>
void save(Archive &ar) const
{
ar(cereal::make_nvp("tag", tag));
ar(method, url);
}
private:
std::string method;
std::string url;
std::string tag;
};
class HTTPRequestEvent : public HTTPRequestSignature
{
public:
HTTPRequestEvent() = default;
HTTPRequestEvent(
const std::string &_method,
const std::string &_url,
const std::string &_headers,
const std::string &&_body,
const std::string &_tag = "buffered messages")
:
HTTPRequestSignature(_method, _url, _tag),
headers(_headers),
body(std::move(_body))
{
}
HTTPRequestEvent(
const HTTPRequestSignature &&sig,
const std::string &_headers,
const std::string &&_body)
:
HTTPRequestSignature(std::move(sig)),
headers(_headers),
body(std::move(_body))
{
}
template<class Archive>
void load(Archive &ar)
{
HTTPRequestSignature::load(ar);
ar(headers, body);
}
template<class Archive>
void save(Archive &ar) const
{
HTTPRequestSignature::save(ar);
ar(headers, body);
}
const std::string & getHeaders() const { return headers; }
const std::string & getBody() const { return body; }
private:
std::string headers;
std::string body;
};
#endif // __HTTP_REQUEST_EVENT_H__

View File

@@ -0,0 +1,57 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __PROTO_MESSAGE_COMP_H__
#define __PROTO_MESSAGE_COMP_H__
#include <memory>
#include <string>
#include "singleton.h"
#include "i_mainloop.h"
#include "i_encryptor.h"
#include "i_messaging.h"
#include "i_agent_details.h"
#include "i_environment.h"
#include "i_rest_api.h"
#include "i_messaging_buffer.h"
#include "i_shell_cmd.h"
#include "component.h"
class ProtoMessageComp
:
public Component,
Singleton::Provide<I_Messaging>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TimeGet>,
Singleton::Consume<I_AgentDetails>,
Singleton::Consume<I_Encryptor>,
Singleton::Consume<I_Environment>,
Singleton::Consume<I_MessagingBuffer>,
Singleton::Consume<I_ShellCmd>
{
public:
ProtoMessageComp();
~ProtoMessageComp();
void init();
void fini();
void preload();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __PROTO_MESSAGE_COMP_H__

View File

@@ -0,0 +1,43 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __SHELL_CMD_H__
#define __SHELL_CMD_H__
#include "i_shell_cmd.h"
#include "i_mainloop.h"
#include "i_time_get.h"
#include "singleton.h"
#include "component.h"
class ShellCmd
:
public Component,
Singleton::Provide<I_ShellCmd>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TimeGet>
{
public:
ShellCmd();
~ShellCmd();
void preload() override;
void init() override;
void fini() override;
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __SHELL_CMD_H__

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __TRAP_HANDLER_H__
#define __TRAP_HANDLER_H__
#include <memory>
#include "singleton.h"
#include "i_trap_handler.h"
#include "i_ioctl.h"
#include "i_mainloop.h"
#include "component.h"
class TrapHandler
:
public Component,
Singleton::Provide<I_TrapHandler>,
Singleton::Consume<I_Ioctl>,
Singleton::Consume<I_MainLoop>,
Singleton::Consume<I_TrapHandler>
{
public:
TrapHandler();
~TrapHandler();
void init();
void fini();
private:
class Impl;
std::unique_ptr<Impl> pimpl;
};
#endif // __TRAP_HANDLER_H__

View File

@@ -0,0 +1,62 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_AGENT_DETAILS_H__
#define __I_AGENT_DETAILS_H__
#include <string>
#include <map>
#include "maybe_res.h"
enum class OrchestrationMode { ONLINE, OFFLINE, HYBRID, COUNT };
class I_AgentDetails
{
public:
// Fog Details
virtual void setFogPort(const uint16_t _fog_port) = 0;
virtual void setSSLFlag(const bool _is_over_ssl) = 0;
virtual void setFogDomain(const std::string &_fog_domain) = 0;
virtual void setProfileId(const std::string &_profile_id) = 0;
virtual void setTenantId(const std::string &_tenant_id) = 0;
virtual Maybe<uint16_t> getFogPort() const = 0;
virtual bool getSSLFlag() const = 0;
virtual Maybe<std::string> getFogDomain() const = 0;
virtual std::string getTenantId() const = 0;
virtual std::string getProfileId() const = 0;
// Agent Details
virtual Maybe<std::string> getProxy() const = 0;
virtual void setProxy(const std::string &_proxy) = 0;
virtual void setAgentId(const std::string &_agent_id) = 0;
virtual std::string getAgentId() const = 0;
virtual void setOrchestrationMode(OrchestrationMode _orchstration_mode) = 0;
virtual OrchestrationMode getOrchestrationMode() const = 0;
// OpenSSL
virtual void setOpenSSLDir(const std::string &openssl_dir) = 0;
virtual Maybe<std::string> getOpenSSLDir() const = 0;
// Serialization
virtual bool readAgentDetails() = 0;
virtual bool writeAgentDetails() = 0;
// Environment
virtual void setClusterId(const std::string &_cluster_id) = 0;
enum class MachineType { AZURE, AWS, ON_PREM, UNRECOGNIZED };
};
#endif // __I_AGENT_DETAILS_H__

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_AGENT_DETAILS_REPORTER_H__
#define __I_AGENT_DETAILS_REPORTER_H__
#include "cereal/archives/json.hpp"
#include <string>
#include <map>
#include "maybe_res.h"
class metaDataReport
{
public:
metaDataReport() = default;
metaDataReport(const metaDataReport &) = default;
metaDataReport & operator<<(const std::pair<std::string, std::string> &data);
void serialize(cereal::JSONOutputArchive &out_ar) const;
private:
std::map<std::string, std::string> agent_details;
};
class I_AgentDetailsReporter
{
public:
virtual void sendReport(
const metaDataReport &agent_details,
const Maybe<std::string> &policy_version,
const Maybe<std::string> &platform,
const Maybe<std::string> &architecture,
const Maybe<std::string> &agent_version) = 0;
virtual bool addAttr(const std::string &key, const std::string &val, bool allow_override = false) = 0;
virtual bool addAttr(const std::map<std::string, std::string> &attr, bool allow_override = false) = 0;
virtual void deleteAttr(const std::string &key) = 0;
virtual bool sendAttributes() = 0;
protected:
~I_AgentDetailsReporter() = default;
};
#endif // __I_AGENT_DETAILS_REPORTER_H__

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_CPU_H__
#define __I_CPU_H__
#include "enum_array.h"
class I_CPU
{
public:
enum CPUGeneralDataEntryType {
USER,
NICE,
SYS,
IDLE,
IOWAIT,
IRQ,
SOFTIRQ,
STEAL,
GUEST,
GUEST_NICE,
COUNT
};
virtual double getCurrentProcessCPUUsage() = 0;
virtual Maybe<double> getCurrentGeneralCPUUsage() = 0;
};
#endif // __I_CPU_H__

View File

@@ -0,0 +1,44 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_ENCRYPTOR_H__
#define __I_ENCRYPTOR_H__
#include "maybe_res.h"
#include <string>
static const std::string data1_file_name = "data1.a";
static const std::string data4_file_name = "data4.a";
static const std::string data6_file_name = "data6.a";
static const std::string user_cred_file_name = "data5.a";
static const std::string proxy_auth_file_name = "data2.a";
static const std::string session_token_file_name = "data3.a";
class I_Encryptor
{
public:
// Base64
virtual std::string base64Encode(const std::string &input) = 0;
virtual std::string base64Decode(const std::string &input) = 0;
// Obfuscating
virtual std::string obfuscateXor(const std::string &input) = 0;
virtual std::string obfuscateXorBase64(const std::string &input) = 0;
protected:
virtual ~I_Encryptor() {}
};
#endif // __I_ENCRYPTOR_H__

View File

@@ -0,0 +1,116 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_ENVIRONMENT_H__
#define __I_ENVIRONMENT_H__
#include <vector>
#include <functional>
#include "context.h"
#include "environment/span.h"
#include "scope_exit.h"
class I_Environment
{
using Param = EnvKeyAttr::ParamAttr;
public:
enum class TracingStatus { ON, OFF, DISABLED };
using ActiveContexts = std::pair<std::vector<Context *>, bool>;
template <typename T>
Maybe<T, Context::Error>
get(const std::string &name) const
{
auto active_contexts_vec = getActiveContexts().first;
for (auto iter = active_contexts_vec.crbegin(); iter != active_contexts_vec.crend(); iter++) {
auto value = (*iter)->get<T>(name);
if (value.ok() || (value.getErr() != Context::Error::NO_VALUE)) return value;
}
return genError(Context::Error::NO_VALUE);
}
template <typename T>
Maybe<T, Context::Error>
get(Context::MetaDataType name) const
{
return get<T>(Context::convertToString(name));
}
virtual Context & getConfigurationContext() = 0;
template <typename T>
void
registerValue(const std::string &name, const T &value)
{
getConfigurationContext().registerValue(name, value);
}
template <typename T>
void
unregisterKey(const std::string &name)
{
getConfigurationContext().unregisterKey<T>(name);
}
template <typename ... Attr>
std::map<std::string, std::string> getAllStrings(Attr ... attr) const { return getAllStrings(Param(attr ...)); }
template <typename ... Attr>
std::map<std::string, uint64_t> getAllUints(Attr ... attr) const { return getAllUints(Param(attr ...)); }
template <typename ... Attr>
std::map<std::string, bool> getAllBools(Attr ... attr) const { return getAllBools(Param(attr ...)); }
virtual void setActiveTenant(const std::string &tenant_id) = 0;
virtual void unsetActiveTenant() = 0;
virtual std::string getCurrentTrace() const = 0;
virtual std::string getCurrentSpan() const = 0;
virtual std::string getCurrentHeaders() = 0;
virtual void startNewTrace(bool new_span = true, const std::string &_trace_id = std::string()) = 0;
virtual void startNewSpan(
Span::ContextType _type,
const std::string &prev_span = std::string(),
const std::string &trace = std::string()
) = 0;
virtual std::scope_exit<std::function<void(void)>> startNewSpanScope(
Span::ContextType _type,
const std::string &prev_span = std::string(),
const std::string &trace = std::string()
) = 0;
virtual void finishTrace(const std::string &trace = std::string()) = 0;
virtual void finishSpan(const std::string &span = std::string()) = 0;
protected:
~I_Environment() {}
virtual const ActiveContexts & getActiveContexts() const = 0;
virtual std::map<std::string, std::string> getAllStrings(const Param &param) const = 0;
virtual std::map<std::string, uint64_t> getAllUints(const Param &param) const = 0;
virtual std::map<std::string, bool> getAllBools(const Param &param) const = 0;
// Registration of Contexts should be done from the Context object, therefore only those object
// should have access to the environment registration methods.
friend class Context;
virtual void registerContext(Context *ptr) = 0;
virtual void unregisterContext(Context *ptr) = 0;
friend class MainloopComponent;
virtual ActiveContexts createEnvironment() = 0;
virtual ActiveContexts saveEnvironment() = 0;
virtual void loadEnvironment(ActiveContexts &&env) = 0;
};
#endif // __I_ENVIRONMENT_H__

View File

@@ -0,0 +1,22 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_FAILOPEN_H__
#define __I_FAILOPEN_H__
class I_Failopen
{
virtual bool isFailOpenMode() const = 0;
};
#endif // __I_FAILOPEN_H__

View File

@@ -0,0 +1,31 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_HEALTH_CHECK_MANAGER__
#define __I_HEALTH_CHECK_MANAGER__
#include <fstream>
#include "health_check_status/health_check_status.h"
class I_Health_Check_Manager
{
public:
virtual HealthCheckStatus getAggregatedStatus() = 0;
virtual void printRepliesHealthStatus(std::ofstream &output_file) = 0;
protected:
~I_Health_Check_Manager() {}
};
#endif //__I_HEALTH_CHECK_MANAGER__

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_INSTANCE_AWARENESS_H__
#define __I_INSTANCE_AWARENESS_H__
#include <string>
#include "maybe_res.h"
class I_InstanceAwareness
{
public:
virtual Maybe<std::string> getUniqueID() = 0;
virtual Maybe<std::string> getFamilyID() = 0;
virtual Maybe<std::string> getInstanceID() = 0;
virtual std::string getUniqueID(const std::string &defaul_value) = 0;
virtual std::string getFamilyID(const std::string &defaul_value) = 0;
virtual std::string getInstanceID(const std::string &defaul_value) = 0;
};
#endif // __I_INSTANCE_AWARENESS_H__

View File

@@ -0,0 +1,285 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_INTELLIGENCE_IS_V2_H__
#define __I_INTELLIGENCE_IS_V2_H__
#include <chrono>
#include "maybe_res.h"
#include "i_messaging.h"
#include "i_time_get.h"
#include "i_mainloop.h"
#include "intelligence_is_v2/intelligence_types_v2.h"
#include "intelligence_is_v2/intelligence_query_v2.h"
#include "config.h"
class I_Intelligence_IS_V2
{
public:
template<typename Data>
Maybe<std::vector<AssetReply<Data>>>
queryIntelligence(QueryRequest &query_request)
{
uint assets_limit = query_request.getAssetsLimit();
static const uint upper_assets_limit = 50;
if (assets_limit == 0 || assets_limit > upper_assets_limit) {
return genError("Assets limit must be in the range of [1, " + std::to_string(upper_assets_limit) + "]");
}
static const uint upper_confidence_limit = 1000;
bool min_conf_res = query_request.checkMinConfidence(upper_confidence_limit);
if (!min_conf_res) {
return genError(
"Minimum confidence value must be in the range of [1, " + std::to_string(upper_confidence_limit) + "]"
);
}
if (query_request.isPagingActivated() && query_request.isPagingFinished()) {
return genError("Paging is activated and already finished. No need for more queries.");
}
IntelligenceQuery<Data> intelligence_query(query_request);
static const std::string query_uri = "/api/v2/intelligence/assets/query";
bool res = getIsOfflineOnly() ? false : sendQueryObject(intelligence_query, query_uri, assets_limit);
if (!res) {
dbgTrace(D_INTELLIGENCE) << "Could not message fog, trying to get offline intelligence.";
Maybe<std::string> offline_res = getOfflineInfoString(query_request.getQuery());
if (!offline_res.ok()) {
dbgDebug(D_INTELLIGENCE) << "Offline intelligence error: " << offline_res.getErr();
return genError("Could not query intelligence");
}
if (!intelligence_query.loadJson(offline_res.unpack())) {
dbgWarning(D_INTELLIGENCE) << "Offline intelligence error: invalid JSON for requested asset";
return genError("Could not query intelligence");
}
}
return intelligence_query.getData();
}
private:
template<typename Data>
bool
sendMessage(
IntelligenceQuery<Data> &intelligence_query,
const std::string &query_uri,
I_Messaging *i_message,
Flags<MessageConnConfig> conn_flags,
const std::string &ip,
uint server_port
) {
if (ip == "" && server_port == 0) {
return i_message->sendObject(
intelligence_query,
I_Messaging::Method::POST,
query_uri,
"",
nullptr,
true,
MessageTypeTag::INTELLIGENCE
);
}
return i_message->sendObject(
intelligence_query,
I_Messaging::Method::POST,
ip,
server_port,
conn_flags,
query_uri,
"",
nullptr,
MessageTypeTag::INTELLIGENCE
);
}
template<typename Data>
bool
sendQueryMessage(
IntelligenceQuery<Data> &intelligence_query,
const std::string &query_uri,
I_Messaging *i_message,
Flags<MessageConnConfig> conn_flags,
const std::string &ip = "",
uint server_port = 0
) {
auto i_timer = getTimer();
auto i_mainloop = getMainloop();
uint request_overall_timeout_conf = getConfigurationWithDefault<uint>(
20,
"intelligence",
"request overall timeout"
);
uint request_lap_timeout_conf = getConfigurationWithDefault<uint>(
5,
"intelligence",
"request lap timeout"
);
std::chrono::seconds request_overall_timeout = std::chrono::seconds(request_overall_timeout_conf);
std::chrono::seconds request_lap_timeout = std::chrono::seconds(request_lap_timeout_conf);
std::chrono::microseconds send_request_start_time = i_timer->getMonotonicTime();
std::chrono::microseconds last_lap_time = i_timer->getMonotonicTime();
std::chrono::seconds seconds_since_start = std::chrono::seconds(0);
std::chrono::seconds seconds_since_last_lap = std::chrono::seconds(0);
bool res= true;
while (res &&
intelligence_query.getResponseStatus() == ResponseStatus::IN_PROGRESS &&
seconds_since_start < request_overall_timeout &&
seconds_since_last_lap < request_lap_timeout
) {
res = sendMessage(intelligence_query, query_uri, i_message, conn_flags, ip, server_port);
if (res && intelligence_query.getResponseStatus() == ResponseStatus::IN_PROGRESS) {
i_mainloop->yield(true);
}
seconds_since_start = std::chrono::duration_cast<std::chrono::seconds>(
i_timer->getMonotonicTime() - send_request_start_time
);
seconds_since_last_lap = std::chrono::duration_cast<std::chrono::seconds>(
i_timer->getMonotonicTime() - last_lap_time
);
last_lap_time = i_timer->getMonotonicTime();
}
return res;
}
template<typename Data>
bool
sendPagingQueryMessage(
IntelligenceQuery<Data> &intelligence_query,
const std::string &query_uri,
int assets_limit,
I_Messaging *i_message,
Flags<MessageConnConfig> conn_flags,
const std::string &ip = "",
uint server_port = 0
) {
bool res= true;
res = sendMessage(intelligence_query, query_uri, i_message, conn_flags, ip, server_port);
if (intelligence_query.getResponseStatus() == ResponseStatus::DONE &&
intelligence_query.getResponseAssetCollectionsSize() < assets_limit
) {
intelligence_query.setRequestCursor(Intelligence_IS_V2::CursorState::DONE, "");
} else {
intelligence_query.setRequestCursor(
Intelligence_IS_V2::CursorState::IN_PROGRESS,
intelligence_query.getResponseCursorVal()
);
}
return res;
}
// LCOV_EXCL_START Reason: one templated instance is tested in intelligence ut. the rest are tested in system tests
template<typename Data>
bool
sendQueryObjectToLocalServer(
IntelligenceQuery<Data> &intelligence_query,
const std::string &query_uri,
const std::string &ip,
bool is_primary_port,
int assets_limit,
I_Messaging *i_message,
Flags<MessageConnConfig> conn_flags
) {
static const std::string primary_port_setting = "local intelligence server primary port";
static const std::string secondary_port_setting = "local intelligence server secondary port";
auto server_port = getSetting<uint>(
"intelligence",
is_primary_port ? primary_port_setting : secondary_port_setting
);
if (!server_port.ok()) return false;
conn_flags.reset();
if (intelligence_query.getPagingStatus().ok()) {
return sendPagingQueryMessage(
intelligence_query,
query_uri,
assets_limit,
i_message,
conn_flags,
ip,
*server_port
);
}
return sendQueryMessage(intelligence_query, query_uri, i_message, conn_flags, ip, *server_port);
}
// LCOV_EXCL_STOP
template<typename Data>
bool
sendQueryObject(IntelligenceQuery<Data> &intelligence_query, const std::string &query_uri, int assets_limit)
{
auto i_message = getMessaging();
Flags<MessageConnConfig> conn_flags;
bool use_local_intelligence = getProfileAgentSettingWithDefault<bool>(
false,
"agent.config.useLocalIntelligence"
);
auto server_ip = getSetting<std::string>("intelligence", "local intelligence server ip");
if (server_ip.ok() && use_local_intelligence) {
if (sendQueryObjectToLocalServer(
intelligence_query,
query_uri,
*server_ip,
true,
assets_limit,
i_message,
conn_flags
)
) {
return true;
}
if (sendQueryObjectToLocalServer(
intelligence_query,
query_uri,
*server_ip,
false,
assets_limit,
i_message,
conn_flags
)
) {
return true;
};
}
if (intelligence_query.getPagingStatus().ok()) {
return sendPagingQueryMessage(intelligence_query, query_uri, assets_limit, i_message, conn_flags);
}
return sendQueryMessage(intelligence_query, query_uri, i_message, conn_flags);
}
virtual I_Messaging * getMessaging() const = 0;
virtual I_TimeGet * getTimer() const = 0;
virtual I_MainLoop * getMainloop() const = 0;
virtual Maybe<std::string> getOfflineInfoString(const SerializableQueryFilter &query) const = 0;
virtual bool getIsOfflineOnly() const = 0;
};
#endif // __I_INTELLIGENCE_IS_V2_H__

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_IOCTL_H__
#define __I_IOCTL_H__
#include "maybe_res.h"
#include "common_is/ioctl_common.h"
class I_Ioctl
{
public:
virtual Maybe<int> sendIoctl(AgentIoctlCmdNumber request, void *ioctl_data, uint32_t size_of_data) = 0;
protected:
virtual ~I_Ioctl() {}
};
#endif // __I_IOCTL_H__

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_LOGGING_H__
#define __I_LOGGING_H__
#include <functional>
#include "report/report.h"
#include "report/log_rest.h"
class I_Logging
{
public:
using GeneralModifier = std::function<void(LogBulkRest &)>;
virtual bool addStream(ReportIS::StreamType type) = 0;
virtual bool addStream(ReportIS::StreamType type, const std::string &log_server_url) = 0;
virtual bool delStream(ReportIS::StreamType type) = 0;
virtual void sendLog(const Report &msg) = 0;
virtual uint64_t getCurrentLogId() = 0;
virtual void addGeneralModifier(const GeneralModifier &modifier) = 0;
protected:
virtual ~I_Logging() {}
};
#endif // __I_LOGGING_H__

View File

@@ -0,0 +1,87 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_MAINLOOP_H__
#define __I_MAINLOOP_H__
#include <functional>
#include <string>
#include <chrono>
#include "maybe_res.h"
class I_MainLoop
{
public:
using Routine = std::function<void(void)>;
using RoutineID = uint;
enum class RoutineType { RealTime, Timer, System, Offline };
// There are two types of routines:
// 1. The primary routines that perform the main functionality of the product.
// 2. The secondary routines that perform auxiliary functionality (upgrade, REST, etc.)
// The mainloop needs to run only as long as there are any primary routines in effect.
virtual RoutineID
addOneTimeRoutine(
RoutineType priority,
Routine func,
const std::string &routine_name,
bool is_primary = false
) = 0;
virtual RoutineID
addRecurringRoutine(
RoutineType priority,
std::chrono::microseconds time,
Routine func,
const std::string &routine_name,
bool is_primary = false
) = 0;
virtual RoutineID
addFileRoutine(
RoutineType priority,
int fd,
Routine func,
const std::string &routine_name,
bool is_primary = false
) = 0;
virtual bool doesRoutineExist(RoutineID id) = 0;
virtual Maybe<I_MainLoop::RoutineID> getCurrentRoutineId() const = 0;
virtual void run() = 0;
// When a routine yields the scheduler may choose to let it continue to run (in the case the routine didn't use
// all of the time that was allocated to it). However, if the routine doesn't have more work to do at the moment
// and wants not to be called directly again by the scheduler, it can force the scheduler not to call it back
// immediately by setting `force` to true.
virtual void yield(bool force = false) = 0;
virtual void yield(std::chrono::microseconds time) = 0;
void yield(int) = delete; // This prevents the syntax `yield(0)` which is otherwise ambiguous
virtual void stopAll() = 0;
virtual void stop() = 0;
virtual void stop(RoutineID id) = 0;
virtual void halt() = 0;
virtual void halt(RoutineID id) = 0;
virtual void resume(RoutineID id) = 0;
protected:
virtual ~I_MainLoop() {}
};
#endif // __I_MAINLOOP_H__

View File

@@ -0,0 +1,194 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_FOG_MESSAGING_H__
#define __I_FOG_MESSAGING_H__
#include <string>
#include <sstream>
#include <fstream>
#include <functional>
#include "cereal/archives/json.hpp"
#include "cereal/types/common.hpp"
#include "cereal/types/string.hpp"
#include "cereal/types/vector.hpp"
#include "maybe_res.h"
#include "debug.h"
#include "messaging/http_core.h"
#include "flags.h"
USE_DEBUG_FLAG(D_COMMUNICATION);
enum class ProxyProtocol
{
HTTP,
HTTPS
};
enum class MessageTypeTag
{
GENERIC,
LOG,
DEBUG,
METRIC,
REPORT,
WAAP_LEARNING,
INTELLIGENCE,
BUFFERED_MESSAGES,
COUNT
};
enum class MessageConnConfig
{
SECURE_CONN,
ONE_TIME_CONN,
EXPECT_REPLY,
EXTERNAL,
IGNORE_SSL_VALIDATION,
COUNT
};
class I_Messaging
{
public:
using string = std::string;
using ErrorCB = std::function<void(HTTPStatusCode)>;
enum class Method { GET, POST, PATCH, CONNECT, PUT };
template <typename T, typename ...Args>
bool
sendObject(T &obj, Args ...args)
{
auto req = obj.genJson();
if (!req.ok()) {
dbgWarning(D_COMMUNICATION) << "Failed to create a request. Error: " << req.getErr();
return false;
}
dbgTrace(D_COMMUNICATION) << "Request generated from json. Request: " << req.unpack();
auto res = sendMessage(true, *req, args...);
if (!res.ok()) {
dbgWarning(D_COMMUNICATION) << "Failed to send request. Error: " << res.getErr();
return false;
}
dbgTrace(D_COMMUNICATION) << "Successfully got response: " << res.unpack();
auto res_json = obj.loadJson(res.unpack());
if (!res_json) {
dbgWarning(D_COMMUNICATION) << "Failed to parse response body. Content: " << res.unpack();
}
dbgTrace(D_COMMUNICATION) << "Successfully parsed response body";
return res_json;
}
template <typename T, typename ...Args>
bool
sendNoReplyObject(T &obj, Args ...args)
{
auto req = obj.genJson();
if (!req.ok()) {
dbgWarning(D_COMMUNICATION) << "Failed to create a request. Error: " << req.getErr();;
return false;
}
auto res = sendMessage(false, *req, args...);
if (!res.ok()) {
dbgWarning(D_COMMUNICATION) << "Failed to send request. Error: " << res.getErr();
}
return res.ok();
}
template <typename T, typename ...Args>
void
sendObjectWithPersistence(T &obj, Args ...args)
{
auto req = obj.genJson();
if (!req.ok()) {
dbgWarning(D_COMMUNICATION) << "Failed to create a request. Error: " << req.getErr();;
return;
}
sendPersistentMessage(false, req.unpackMove(), args...);
}
template <typename T, typename ...Args>
Maybe<string>
downloadFile(T &obj, Args ...args)
{
auto req = obj.genJson();
if (!req.ok()) return genError("Invalid request");
auto response = sendMessage(true, *req, args...);
if (response.ok()) {
return response.unpack();
}
return genError("Failed to download file. Error: " + response.getErr());
}
virtual Maybe<std::string> getProxyDomain(ProxyProtocol protocol) const = 0;
virtual Maybe<std::string> getProxyCredentials(ProxyProtocol protocol) const = 0;
virtual Maybe<uint16_t> getProxyPort(ProxyProtocol protocol) const = 0;
virtual bool getProxyExists(ProxyProtocol protocol) const = 0;
virtual Maybe<std::string> getProxyAddress(ProxyProtocol protocol) const = 0;
virtual Maybe<void> loadProxy() = 0;
virtual bool setActiveFog(MessageTypeTag tag) = 0;
virtual void loadAccessToken() = 0;
virtual bool setActiveFog(const string &host, const uint16_t port, bool is_secure, MessageTypeTag tag) = 0;
virtual std::string getAccessToken() = 0;
protected:
~I_Messaging() {}
private:
virtual Maybe<string>
sendPersistentMessage(
bool get_reply,
const string &&body,
Method method,
const string &uri,
const string &headers = "",
bool should_yield = true,
MessageTypeTag tag = MessageTypeTag::GENERIC,
bool skip_sending = false) = 0;
virtual Maybe<string>
sendMessage(
bool get_reply,
const string &body,
Method method,
const string &uri,
const string &headers = "",
ErrorCB err_call_back = nullptr,
bool should_yield = true,
MessageTypeTag tag = MessageTypeTag::GENERIC) = 0;
virtual Maybe<string>
sendMessage(
bool get_reply,
const string &body,
Method method,
const std::string &host,
uint16_t port,
Flags<MessageConnConfig> &conn_flags,
const string &uri,
const string &headers = "",
ErrorCB err_call_back = nullptr,
MessageTypeTag tag = MessageTypeTag::GENERIC) = 0;
};
#endif // __I_FOG_MESSAGING_H__

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_MESSAGING_BUFFER_H__
#define __I_MESSAGING_BUFFER_H__
#include "messaging_buffer/http_request_event.h"
#include "maybe_res.h"
class I_MessagingBuffer
{
public:
virtual Maybe<HTTPRequestEvent> peekRequest() = 0;
virtual void popRequest() = 0;
virtual void bufferNewRequest(const HTTPRequestEvent &request, bool is_rejected = false) = 0;
virtual bool isPending(const HTTPRequestSignature &request) = 0;
virtual void cleanBuffer() = 0;
protected:
~I_MessagingBuffer() {}
};
#endif // __I_MESSAGING_BUFFER_H__

View File

@@ -0,0 +1,38 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_MESSAGING_DOWNLOADER_H__
#define __I_MESSAGING_DOWNLOADER_H__
#include <string>
#include <functional>
#include "maybe_res.h"
class I_MessagingDownloader
{
public:
using OnCompleteCB = std::function<void(const Maybe<std::string> &)>;
virtual bool downloadFile(
const std::string &file_name,
const std::string &url,
OnCompleteCB cb = nullptr,
const unsigned int port = 0
) = 0;
protected:
virtual ~I_MessagingDownloader() {}
};
#endif // __I_MESSAGING_DOWNLOADER_H__

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_REST_API__
#define __I_REST_API__
#include "rest.h"
#include <string>
#include <memory>
#include "common.h"
enum class RestAction { ADD, SET, SHOW, DELETE };
// The RestInit class provides an interface through which new JsonRest object can be created.
class RestInit
{
public:
~RestInit() {}
virtual std::unique_ptr<ServerRest> getRest() = 0;
};
template <typename T>
class SpecificRestInit : public RestInit
{
public:
std::unique_ptr<ServerRest> getRest() override { return std::make_unique<T>(); }
};
class I_RestApi
{
public:
template <typename T>
bool
addRestCall(RestAction oper, const std::string &uri)
{
return addRestCall(oper, uri, std::make_unique<SpecificRestInit<T>>());
}
protected:
~I_RestApi() {}
virtual bool addRestCall(RestAction oper, const std::string &uri, std::unique_ptr<RestInit> &&init) = 0;
};
#endif // __I_REST_API__

View File

@@ -0,0 +1,34 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_SHELL_CMD_H__
#define __I_SHELL_CMD_H__
#include <string>
#include <utility>
#include "maybe_res.h"
class I_ShellCmd
{
using FullOutput = Maybe<std::pair<std::string, int>>;
public:
virtual Maybe<std::string> getExecOutput(const std::string &cmd, uint ms_tmout = 200, bool do_yield = false) = 0;
virtual Maybe<int> getExecReturnCode(const std::string &cmd, uint ms_tmout = 200, bool do_yield = false) = 0;
virtual FullOutput getExecOutputAndCode(const std::string &cmd, uint ms_tmout = 200, bool do_yield = false) = 0;
protected:
virtual ~I_ShellCmd() {}
};
#endif // __I_SHELL_CMD_H__

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_SIGNAL_HANDLER_H__
#define __I_SIGNAL_HANDLER_H__
class I_SignalHandler
{
public:
virtual void dumpErrorReport(const std::string &error) = 0;
virtual Maybe<std::vector<std::string>> getBacktrace() = 0;
protected:
~I_SignalHandler() {}
};
#endif // __I_SIGNAL_HANDLER_H__

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_SOCKET_IS_H__
#define __I_SOCKET_IS_H__
#include <string.h>
#include <vector>
#include "maybe_res.h"
class I_Socket
{
public:
enum class SocketType { UNIX, TCP, UDP };
using socketFd = int;
virtual Maybe<socketFd>
genSocket(SocketType type, bool is_blocking, bool is_server, const std::string &address) = 0;
virtual Maybe<socketFd> acceptSocket(
socketFd server_socket_fd,
bool is_blocking,
const std::string &authorized_ip = ""
) = 0;
virtual void closeSocket(socketFd &socket) = 0;
virtual bool writeData(socketFd socket, const std::vector<char> &data) = 0;
virtual Maybe<std::vector<char>> receiveData(socketFd socket, uint data_size, bool is_blocking = true) = 0;
virtual bool isDataAvailable(socketFd socket) = 0;
protected:
virtual ~I_Socket() {}
};
#endif // __I_SOCKET_IS_H__

View File

@@ -0,0 +1,86 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_TABLE_H__
#define __I_TABLE_H__
#include <chrono>
#include <string>
#include <typeindex>
#include "table/opaque_basic.h"
#include "table_iter.h"
#include "maybe_res.h"
#include "cereal/archives/binary.hpp"
enum class SyncMode
{
DUPLICATE_ENTRY,
TRANSFER_ENTRY
};
class I_Table
{
public:
template <typename Opaque>
bool hasState() const;
template <typename Opaque, typename ...Args>
bool createState(Args ...args);
template <typename Opaque>
void deleteState();
template <typename Opaque>
Opaque & getState();
virtual void setExpiration(std::chrono::milliseconds expire) = 0;
virtual bool doesKeyExists() const = 0;
virtual std::string keyToString() const = 0;
virtual TableIter begin() const = 0;
virtual TableIter end() const = 0;
protected:
~I_Table() {}
virtual bool hasState (const std::type_index &index) const = 0;
virtual bool createState(const std::type_index &index, std::unique_ptr<TableOpaqueBase> &&ptr) = 0;
virtual bool deleteState(const std::type_index &index) = 0;
virtual TableOpaqueBase * getState (const std::type_index &index) = 0;
};
template <typename Key>
class I_TableSpecific : public I_Table
{
public:
virtual bool hasEntry (const Key &key) = 0;
virtual bool createEntry (const Key &key, std::chrono::microseconds expire) = 0;
virtual bool deleteEntry (const Key &key) = 0;
virtual bool addLinkToEntry(const Key &key, const Key &link) = 0;
virtual uint count () = 0;
virtual void expireEntries () = 0;
virtual void saveEntry(TableIter iter, SyncMode mode, cereal::BinaryOutputArchive &ar) const = 0;
virtual void loadEntry(cereal::BinaryInputArchive &ar) = 0;
virtual bool setActiveKey (const Key &key) = 0;
virtual void unsetActiveKey () = 0;
virtual Maybe<Key, void> getCurrentKey() const = 0;
protected:
~I_TableSpecific() {}
};
#include "table/i_table_impl.h"
#endif // __I_TABLE_H__

View File

@@ -0,0 +1,28 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_TABLE_ITER_H__
#define __I_TABLE_ITER_H__
class I_TableIter
{
public:
virtual ~I_TableIter() {}
virtual void operator++() = 0;
virtual void operator++(int) = 0;
virtual void setEntry() = 0;
virtual void unsetEntry() = 0;
virtual void * getUniqueId() const = 0;
};
#endif // __I_TABLE_ITER_H__

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_TENANT_MANAGER_H__
#define __I_TENANT_MANAGER_H__
#include <string>
#include <vector>
#include <chrono>
#include <functional>
class I_TenantManager
{
public:
using newTenantCB = std::function<void(const std::vector<std::string> &)>;
virtual void uponNewTenants(const newTenantCB &cb) = 0;
virtual bool isTenantActive(const std::string &tenant_id) const = 0;
virtual std::vector<std::string> fetchActiveTenants() const = 0;
virtual std::vector<std::string> getInstances(const std::string &tenant_id) const = 0;
virtual void addActiveTenant(const std::string &tenant_id) = 0;
virtual void addActiveTenants(const std::vector<std::string> &tenants_id) = 0;
virtual void deactivateTenant(const std::string &tenant_id) = 0;
virtual void deactivateTenants(const std::vector<std::string> &tenants_id) = 0;
virtual std::chrono::microseconds getTimeoutVal() const = 0;
private:
friend class LoadNewTenants;
virtual void addInstance(const std::string &tenant_id, const std::string &instace_id) = 0;
protected:
virtual ~I_TenantManager() {}
};
#endif // __I_TENANT_MANAGER_H__

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_TIME_GET_H__
#define __I_TIME_GET_H__
#include <string>
#include <chrono>
class I_TimeGet
{
public:
virtual std::chrono::microseconds getMonotonicTime() = 0;
virtual std::chrono::microseconds getWalltime() = 0;
virtual std::string getWalltimeStr() = 0;
virtual std::string getWalltimeStr(const std::chrono::microseconds &ms) = 0;
virtual std::string getLocalTimeStr() = 0;
protected:
virtual ~I_TimeGet() {}
};
#endif // __I_TIME_GET_H__

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_TIME_SET_H__
#define __I_TIME_SET_H__
#include <chrono>
class I_TimeSet
{
public:
virtual void setMonotonicTime(std::chrono::microseconds new_time) = 0;
virtual void setWalltime(std::chrono::microseconds new_time) = 0;
protected:
virtual ~I_TimeSet() {}
};
#endif // __I_TIME_SET_H__

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __I_TRAP_HANDLER_H__
#define __I_TRAP_HANDLER_H__
#include <signal.h>
#include "maybe_res.h"
#include "common_is/trap_common.h"
class I_TrapHandler
{
public:
using SignalHandler = std::function<Maybe<bool>(AgentTrapCmd)>;
virtual Maybe<bool> registerTrap(AgentTrapCmd cmd, SignalHandler signal_handler) = 0;
virtual void signalFunctionHandler(siginfo_t *info) = 0;
protected:
virtual ~I_TrapHandler() {}
};
#endif // __I_TRAP_HANDLER_H__

View File

@@ -0,0 +1,71 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ASSET_SOURCE_V2_H__
#define __ASSET_SOURCE_V2_H__
#include "debug.h"
#include "intelligence_types_v2.h"
#include "cereal/archives/json.hpp"
#include "cereal/types/vector.hpp"
#include "customized_cereal_map.h"
template <typename UserSerializableReplyAttr>
class SerializableAssetSource
{
public:
SerializableAssetSource() {}
void load(cereal::JSONInputArchive &ar);
const std::string & getTenantId() const { return tenant_id; }
const std::string & getSourceId() const { return source_id; }
const std::string & getAssetId() const { return asset_id; }
const std::chrono::seconds getTTL() const { return ttl; }
const std::string & getExpirationTime() const { return expiration_time; }
uint getConfidence() const { return confidence; }
const std::vector<UserSerializableReplyAttr> & getAttributes() const { return attributes; }
UserSerializableReplyAttr
mergeReplyData() const
{
UserSerializableReplyAttr reply_data;
for (const UserSerializableReplyAttr &reply_attr : attributes) {
reply_data.insert(reply_attr);
}
return reply_data;
}
template <typename Values>
bool
matchValues(const Values &requested_vals) const
{
for (const UserSerializableReplyAttr &recieved_attr : attributes) {
if (recieved_attr.matchValues(requested_vals)) return true;
}
return false;
}
private:
std::string tenant_id = "";
std::string source_id = "";
std::string asset_id = "";
std::chrono::seconds ttl = std::chrono::seconds::zero();
std::string expiration_time = "";
uint confidence = 0;
std::vector<UserSerializableReplyAttr> attributes;
};
#include "asset_source_v2_impl.h"
#endif //__ASSET_SOURCE_V2_H__

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ASSET_SOURCE_V2_IMPL_H__
#define __ASSET_SOURCE_V2_IMPL_H__
#ifndef __ASSET_SOURCE_V2_H__
#error intelligence_query_impl_8_0.h should not be included directly!
#endif //__ASSET_V2_SOURCE_H__
USE_DEBUG_FLAG(D_INTELLIGENCE);
template <typename UserSerializableReplyAttr>
void
SerializableAssetSource<UserSerializableReplyAttr>::load(cereal::JSONInputArchive &ar)
{
uint raw_seconds;
ar(
cereal::make_nvp("tenantId", tenant_id),
cereal::make_nvp("sourceId", source_id),
cereal::make_nvp("assetId", asset_id),
cereal::make_nvp("ttl", raw_seconds),
cereal::make_nvp("expirationTime", expiration_time),
cereal::make_nvp("confidence", confidence)
);
ttl = std::chrono::seconds(raw_seconds);
UserSerializableReplyAttr raw_attribute;
try {
ar(cereal::make_nvp("attributes", raw_attribute));
} catch(const std::exception &e) {}
attributes.push_back(raw_attribute);
}
#endif //__ASSET_SOURCE_V2_IMPL_H__

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __INTELLIGENCE_QUERY_V2_H__
#define __INTELLIGENCE_QUERY_V2_H__
#include "cereal/archives/json.hpp"
#include "intelligence_types_v2.h"
#include "query_request_v2.h"
#include "query_response_v2.h"
#include "rest.h"
template <typename UserSerializableReplyAttr>
class IntelligenceQuery
{
public:
IntelligenceQuery(QueryRequest &filter)
:
request(filter),
response()
{}
Maybe<std::string> genJson() const;
bool loadJson(const std::string &json);
void load(cereal::JSONInputArchive &ar);
void save(cereal::JSONOutputArchive &ar) const;
std::vector<AssetReply<UserSerializableReplyAttr>> getData();
ResponseStatus getResponseStatus() { return response.getResponseStatus(); }
int getResponseAssetCollectionsSize() const { return response.getAssetCollectionsSize(); }
const std::string & getResponseCursorVal() const { return response.getCursor(); }
void activatePaging();
Maybe<Intelligence_IS_V2::CursorState> getPagingStatus();
void setRequestCursor(CursorState state, const std::string &value);
private:
QueryRequest &request;
IntelligenceQueryResponse<UserSerializableReplyAttr> response;
};
#include "intelligence_query_v2_impl.h"
#endif // __INTELLIGENCE_QUERY_V2_H__

View File

@@ -0,0 +1,100 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __INTELLIGENCE_QUERY_V2_IMPL_H_
#define __INTELLIGENCE_QUERY_V2_IMPL_H_
#ifndef __INTELLIGENCE_QUERY_V2_H__
#error intelligence_query_impl_v2.h should not be included directly!
#endif // __INTELLIGENCE_QUERY_V2_H__
USE_DEBUG_FLAG(D_INTELLIGENCE);
template <typename UserSerializableReplyAttr>
Maybe<std::string>
IntelligenceQuery<UserSerializableReplyAttr>::genJson() const
{
{
std::stringstream out;
{
cereal::JSONOutputArchive out_ar(out);
request.saveToJson(out_ar);
}
return out.str();
}
}
template <typename UserSerializableReplyAttr>
bool
IntelligenceQuery<UserSerializableReplyAttr>::loadJson(const std::string &json)
{
try {
std::stringstream in;
in.str(json);
try {
cereal::JSONInputArchive in_ar(in);
load(in_ar);
} catch (const Intelligence_IS_V2::IntelligenceException &e) {
dbgWarning(D_INTELLIGENCE) << "Failed to load query response. Error: " << e.what();
return false;
}
return true;
} catch (const std::exception &e) {
return false;
}
}
template <typename UserSerializableReplyAttr>
void
IntelligenceQuery<UserSerializableReplyAttr>::load(cereal::JSONInputArchive &ar)
{
response.loadFromJson(ar);
}
template <typename UserSerializableReplyAttr>
void
IntelligenceQuery<UserSerializableReplyAttr>::save(cereal::JSONOutputArchive &ar) const
{
request.saveToJson(ar);
}
template <typename UserSerializableReplyAttr>
std::vector<AssetReply<UserSerializableReplyAttr>>
IntelligenceQuery<UserSerializableReplyAttr>::getData()
{
return response.getData();
}
template <typename UserSerializableReplyAttr>
void
IntelligenceQuery<UserSerializableReplyAttr>::activatePaging()
{
request.setCursor(Intelligence_IS_V2::CursorState::START, "start");
}
template <typename UserSerializableReplyAttr>
Maybe<Intelligence_IS_V2::CursorState>
IntelligenceQuery<UserSerializableReplyAttr>::getPagingStatus()
{
if (!request.isPagingActivated()) return genError("Paging not activated");
return request.getCursorState();
}
template <typename UserSerializableReplyAttr>
void
IntelligenceQuery<UserSerializableReplyAttr>::setRequestCursor(CursorState state, const std::string &value)
{
request.setCursor(state, value);
}
#endif //__INTELLIGENCE_QUERY_V2_IMPL_H_

View File

@@ -0,0 +1,80 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __INTELLIGENCE_TYPES_V2_H__
#define __INTELLIGENCE_TYPES_V2_H__
#include <string>
#include <chrono>
#include <unordered_map>
#include "debug.h"
namespace Intelligence_IS_V2
{
enum class AttributeKeyType {
MAIN,
REGULAR,
NONE
};
enum class Operator
{
AND,
OR,
NONE
};
enum class Condition
{
EQUALS,
NOT_EQUALS,
MATCH,
STARTS_WITH,
CONTAINS,
IN,
NOT_IN
};
enum class CursorState {
START,
IN_PROGRESS,
DONE
};
enum class ResponseStatus
{
DONE,
IN_PROGRESS
};
const std::string & convertConditionTypeToString(const Condition &condition_type);
const std::string & convertOperationTypeToString(const Operator &operation_type);
std::string createAttributeString(const std::string &key, AttributeKeyType type);
ResponseStatus convertStringToResponseStatus(const std::string &status);
class IntelligenceException : public std::exception
{
public:
IntelligenceException() : message() {}
IntelligenceException(const std::string &msg) : message(msg) {}
const char * what() const throw() { return message.c_str(); }
private:
std::string message;
};
} // namespace Intelligence_IS_V2
#endif // __INTELLIGENCE_TYPES_V2_H__

View File

@@ -0,0 +1,90 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __QUERY_FILTER_V2_H__
#define __QUERY_FILTER_V2_H__
#include <string>
#include <chrono>
#include <boost/functional/hash.hpp>
#include "cereal/archives/json.hpp"
#include "cereal/types/vector.hpp"
#include "debug.h"
#include "intelligence_types_v2.h"
#include "maybe_res.h"
using namespace Intelligence_IS_V2;
class SerializableQueryCondition
{
public:
SerializableQueryCondition() {}
SerializableQueryCondition(Condition _condition_type, std::string _key, std::string _value)
:
condition_type(_condition_type),
key(_key),
value(_value)
{};
void save(cereal::JSONOutputArchive &ar) const;
Condition getConditionType() const { return condition_type; }
const std::string & getKey() const { return key; }
const std::string & getValue() const { return value; }
private:
Condition condition_type = Condition::EQUALS;
std::string key = "";
std::string value = "";
};
class SerializableQueryFilter
{
public:
SerializableQueryFilter() {}
SerializableQueryFilter(Condition condition_type, const std::string &key, const std::string &value);
SerializableQueryFilter(
Operator operator_type,
Condition condition_type,
const std::string &key,
const std::string &value
);
void save(cereal::JSONOutputArchive &ar) const;
void addCondition(Condition condition_type, const std::string &key, const std::string &value);
Operator getOperator() const { return operator_type; }
const std::vector<SerializableQueryCondition> & getConditionOperands() const { return condition_operands; }
const std::vector<SerializableQueryFilter> & getQueriesOperands() const { return queries_operands; }
const std::string & getConditionValueByKey(const std::string &key) const;
bool empty() const { return condition_operands.empty() && queries_operands.empty(); }
SerializableQueryFilter operator &&(const SerializableQueryFilter &other_query);
SerializableQueryFilter operator ||(const SerializableQueryFilter &other_query);
private:
void saveCondition(cereal::JSONOutputArchive &ar) const;
void saveOperation(cereal::JSONOutputArchive &ar) const;
SerializableQueryFilter calcOperator(const SerializableQueryFilter &other_query, const Operator &operator_type);
Operator operator_type = Operator::NONE;
std::vector<SerializableQueryFilter> queries_operands = {};
std::vector<SerializableQueryCondition> condition_operands = {};
};
#endif // __QUERY_FILTER_V2_H__

View File

@@ -0,0 +1,92 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __QUERY_REQUEST_V2_H__
#define __QUERY_REQUEST_V2_H__
#include "rest.h"
#include "common.h"
#include "intelligence_is_v2/intelligence_types_v2.h"
#include "intelligence_is_v2/query_filter_v2.h"
#include "intelligence_is_v2/requested_attributes_v2.h"
#include "intelligence_is_v2/query_types_v2.h"
#include "maybe_res.h"
class QueryRequest
{
public:
using RequestCursor = std::pair<CursorState, std::string>;
QueryRequest() {}
QueryRequest(
Condition condition_type,
const std::string &key,
const std::string &value,
bool full_response,
AttributeKeyType type = AttributeKeyType::MAIN
);
void saveToJson(cereal::JSONOutputArchive &ar) const;
uint getAssetsLimit() const;
const SerializableQueryFilter & getQuery() const;
const SerializableAttributesMap & getRequestedAttributes() const;
void addCondition(
Condition condition_type,
const std::string &key,
const std::string &value,
AttributeKeyType attribute_type = AttributeKeyType::MAIN
);
void setRequestedAttr(
const std::string &attr,
AttributeKeyType attribute_type = AttributeKeyType::REGULAR
);
void setRequestedAttr(
const std::string &attr,
uint min_conf,
AttributeKeyType = AttributeKeyType::REGULAR
);
void setTenantsList(const std::vector<std::string> tenants);
void setAssetsLimit(uint _assets_limit);
bool checkMinConfidence(uint upper_confidence_limit);
void activatePaging();
bool isPagingActivated();
Maybe<CursorState> getCursorState();
bool isPagingFinished();
void setCursor(CursorState state, const std::string &value);
bool empty() const { return query.empty(); }
QueryRequest operator &&(const QueryRequest &other_query);
QueryRequest operator ||(const QueryRequest &other_query);
static const uint default_min_confidence;
static const uint default_assets_limit;
private:
uint assets_limit = default_assets_limit;
bool full_response = false;
Maybe<RequestCursor> cursor = genError("Cursor not initialized");
SerializableQueryFilter query;
SerializableAttributesMap requested_attributes;
SerializableQueryTypes query_types;
QueryRequest calcQueryRequestOperator(const QueryRequest &other_query, const Operator &operator_type);
};
#endif // __QUERY_REQUEST_V2_H__

View File

@@ -0,0 +1,120 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __QUERY_RESPONSE_V2_H__
#define __QUERY_RESPONSE_V2_H__
#include <sstream>
#include <vector>
#include <map>
#include "cereal/archives/json.hpp"
#include "cereal/types/vector.hpp"
#include "cereal/types/map.hpp"
#include "debug.h"
#include "maybe_res.h"
#include "customized_cereal_map.h"
#include "customized_cereal_multimap.h"
#include "intelligence_types_v2.h"
#include "asset_source_v2.h"
USE_DEBUG_FLAG(D_INTELLIGENCE);
template <typename UserSerializableReplyAttr>
class AssetReply
{
public:
AssetReply() {}
void load(cereal::JSONInputArchive &ar);
std::vector<UserSerializableReplyAttr> getData() const;
const std::map<std::string, std::vector<std::string>> & getMainAttributes() const { return main_attributes; }
const std::vector<SerializableAssetSource<UserSerializableReplyAttr>> & getSources() const { return sources; }
uint getAssetSchemaVersion() const { return asset_schema_version; }
const std::string & getAssetType() const { return asset_type; }
uint getAssetTypeSchemaVersion() const { return asset_type_schema_version; }
const std::string & getAssetPermissionGroupId() const { return asset_permission_group_id; }
const std::string & getAssetName() const { return asset_name; }
const std::string & getAssetID() const { return asset_id; }
const std::string & getAssetClass() const { return asset_class; }
const std::string & getAssetCategory() const { return asset_category; }
const std::string & getAssetFamily() const { return asset_family; }
const std::string & getAssetGroup() const { return asset_group; }
const std::string & getAssetOrder() const { return asset_order; }
const std::string & getAssetKind() const { return asset_kind; }
UserSerializableReplyAttr
mergeReplyData() const
{
UserSerializableReplyAttr reply_data;
for (const SerializableAssetSource<UserSerializableReplyAttr> &source : sources) {
UserSerializableReplyAttr data_by_source = source.mergeReplyData();
reply_data.insert(data_by_source);
}
return reply_data;
}
template <typename Values>
bool
matchValues(const Values &values) const
{
for (const SerializableAssetSource<UserSerializableReplyAttr> &source : sources) {
if (source.template matchValues<Values>(values)) return true;
}
return false;
}
private:
uint asset_schema_version = 0;
std::string asset_type = "";
uint asset_type_schema_version = 0;
std::string asset_permission_group_id = "";
std::string asset_name = "";
std::string asset_id = "";
std::string asset_class = "";
std::string asset_category = "";
std::string asset_family = "";
std::string asset_group = "";
std::string asset_order = "";
std::string asset_kind = "";
std::map<std::string, std::vector<std::string>> main_attributes;
std::vector<SerializableAssetSource<UserSerializableReplyAttr>> sources;
};
template <typename UserSerializableReplyAttr>
class IntelligenceQueryResponse
{
public:
IntelligenceQueryResponse() {}
void loadFromJson(cereal::JSONInputArchive &ar);
Intelligence_IS_V2::ResponseStatus getResponseStatus() const;
uint getAmountOfAssets() const;
const std::string & getCursor() const;
int getAssetCollectionsSize() const;
const std::vector<AssetReply<UserSerializableReplyAttr>> & getData() const;
private:
Intelligence_IS_V2::ResponseStatus status = Intelligence_IS_V2::ResponseStatus::IN_PROGRESS;
uint total_num_assets = 0;
std::string cursor = "";
std::vector<AssetReply<UserSerializableReplyAttr>> asset_collections;
};
#include "query_response_v2_impl.h"
#endif // __QUERY_RESPONSE_V2_H__

View File

@@ -0,0 +1,138 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __QUERY_RESPONSE_V2_IMPL_H_
#define __QUERY_RESPONSE_V2_IMPL_H_
#ifndef __QUERY_RESPONSE_V2_H__
#error intelligence_query_response_v2_impl.h should not be included directly!
#endif // __QUERY_RESPONSE_V2_H__
#include "debug.h"
#include "intelligence_types_v2.h"
USE_DEBUG_FLAG(D_INTELLIGENCE);
template <typename UserSerializableReplyAttr>
void
AssetReply<UserSerializableReplyAttr>::load(cereal::JSONInputArchive &ar)
{
SerializableMultiMap<std::string, std::vector<std::string>> tmp_multimap;
ar(
cereal::make_nvp("schemaVersion", asset_schema_version),
cereal::make_nvp("assetTypeSchemaVersion", asset_type_schema_version),
cereal::make_nvp("class", asset_class),
cereal::make_nvp("category", asset_category),
cereal::make_nvp("family", asset_family),
cereal::make_nvp("mainAttributes", tmp_multimap)
);
for (auto const &attr : tmp_multimap.getMap<std::string>()) {
std::vector<std::string> attr_vec = { attr.second };
main_attributes[attr.first] = attr_vec;
}
for (auto const &attr : tmp_multimap.getMap<std::vector<std::string>>()) {
main_attributes[attr.first] = attr.second;
}
try {
ar(cereal::make_nvp("permissionGroupId", asset_permission_group_id));
} catch(...) {}
try {
ar(cereal::make_nvp("name", asset_name));
} catch(...) {}
try {
ar(cereal::make_nvp("group", asset_group));
} catch(...) {}
try {
ar(cereal::make_nvp("order", asset_order));
} catch(...) {}
try {
ar(cereal::make_nvp("kind", asset_kind));
} catch(...) {}
ar(cereal::make_nvp("sources", sources));
ar(cereal::make_nvp("assetType", asset_type));
}
template <typename UserSerializableReplyAttr>
std::vector<UserSerializableReplyAttr>
AssetReply<UserSerializableReplyAttr>::getData() const
{
std::vector<UserSerializableReplyAttr> all_attributes;
for (SerializableAssetSource<UserSerializableReplyAttr> const &asset_source : sources) {
for (UserSerializableReplyAttr const &attribute : asset_source.getAttributes()) {
all_attributes.push_back(attribute);
}
}
return all_attributes;
}
template <typename UserSerializableReplyAttr>
void
IntelligenceQueryResponse<UserSerializableReplyAttr>::loadFromJson(cereal::JSONInputArchive &ar)
{
std::string raw_data;
ar(
cereal::make_nvp("status", raw_data),
cereal::make_nvp("totalNumAssets", total_num_assets),
cereal::make_nvp("assetCollections", asset_collections)
);
status = Intelligence_IS_V2::convertStringToResponseStatus(raw_data);
try {
ar(cereal::make_nvp("cursor", cursor));
} catch(...) {}
}
template <typename UserSerializableReplyAttr>
Intelligence_IS_V2::ResponseStatus
IntelligenceQueryResponse<UserSerializableReplyAttr>::getResponseStatus() const
{
return status;
}
template <typename UserSerializableReplyAttr>
uint
IntelligenceQueryResponse<UserSerializableReplyAttr>::getAmountOfAssets() const
{
return total_num_assets;
}
template <typename UserSerializableReplyAttr>
const std::string &
IntelligenceQueryResponse<UserSerializableReplyAttr>::getCursor() const
{
return cursor;
}
template <typename UserSerializableReplyAttr>
int
IntelligenceQueryResponse<UserSerializableReplyAttr>::getAssetCollectionsSize() const
{
return asset_collections.size();
}
template <typename UserSerializableReplyAttr>
const std::vector<AssetReply<UserSerializableReplyAttr>> &
IntelligenceQueryResponse<UserSerializableReplyAttr>::getData() const
{
return asset_collections;
}
#endif // __QUERY_RESPONSE_V2_IMPL_H_

View File

@@ -0,0 +1,53 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __QUERY_TYPES_V2_H__
#define __QUERY_TYPES_V2_H__
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"
#include "cereal/types/tuple.hpp"
#include "cereal/types/vector.hpp"
#include "intelligence_types_v2.h"
#include <vector>
#include <unordered_map>
class serializableTenantList
{
public:
serializableTenantList(const std::vector<std::string> &_tenants)
:
tenants(_tenants)
{}
void serialize(cereal::JSONOutputArchive &ar) const;
private:
std::vector<std::string> tenants;
};
class SerializableQueryTypes
{
public:
SerializableQueryTypes() {};
void save(cereal::JSONOutputArchive &ar) const;
void setSerializableTenantList(const std::vector<std::string> tenants);
private:
std::vector<std::string> tenants;
bool is_nsaas = false;
};
#endif // __QUERY_TYPES_V2_H__

View File

@@ -0,0 +1,60 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __REQUESTED_ATTRIBUTES_V2_H__
#define __REQUESTED_ATTRIBUTES_V2_H__
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"
#include "cereal/types/tuple.hpp"
#include "cereal/types/vector.hpp"
#include "intelligence_types_v2.h"
#include <vector>
#include <unordered_map>
class serializableAttribute
{
public:
serializableAttribute(std::string _key, uint _min_confidence)
:
key(_key),
min_confidence(_min_confidence)
{}
void serialize(cereal::JSONOutputArchive &ar) const;
private:
std::string key;
uint min_confidence;
};
class SerializableAttributesMap
{
public:
SerializableAttributesMap() {};
void save(cereal::JSONOutputArchive &ar) const;
void setSerializableAttribute(const std::string &attribute, uint confidence = 500);
uint getAttributeByKey(const std::string &key) const;
uint getSize() const { return requested_attributes.size(); }
bool isRequestedAttributesMapEmpty() const { return requested_attributes.empty(); }
bool checkMinConfidence(uint upper_confidence_limit);
private:
std::unordered_map<std::string, uint> requested_attributes;
};
#endif // __REQUESTED_ATTRIBUTES_V2_H__

View File

@@ -0,0 +1,103 @@
// Copyright (C) 2022 Check Point Software Technologies Ltd. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __HTTP_CORE_H__
#define __HTTP_CORE_H__
#include <string>
#include <unordered_map>
#include "maybe_res.h"
#include "cereal/archives/json.hpp"
#include "cereal/types/string.hpp"
enum class HTTPStatusCode
{
// 10X - Information responses. Not supported yet.
// 20x - Successful responses.
HTTP_OK = 200,
HTTP_NO_CONTENT = 204,
HTTP_MULTI_STATUS = 207,
// 30x - Redirection messages. Not supported yet.
// 4xx - Client error responses.
HTTP_BAD_REQUEST = 400,
HTTP_UNAUTHORIZED = 401,
HTTP_FORBIDDEN = 403,
HTTP_NOT_FOUND = 404,
HTTP_METHOD_NOT_ALLOWED = 405,
HTTP_PROXY_AUTHENTICATION_REQUIRED = 407,
HTTP_REQUEST_TIME_OUT = 408,
HTTP_PAYLOAD_TOO_LARGE = 413,
// 5xx - Server error responses.
HTTP_INTERNAL_SERVER_ERROR = 500,
HTTP_NOT_IMPLEMENTED = 501,
HTTP_BAD_GATEWAY = 502,
HTTP_SERVICE_UNABAILABLE = 503,
HTTP_GATEWAY_TIMEOUT = 504,
HTTP_VERSION_NOT_SUPPORTED = 505,
HTTP_VARIANT_ALSO_NEGOTIATES = 506,
HTTP_INSUFFICIENT_STORAGE = 507,
HTTP_LOOP_DETECTED = 508,
HTTP_NOT_EXTENDED = 510,
HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511,
// Not supported status code.
HTTP_UNKNOWN
};
class HTTPResponse
{
public:
HTTPResponse(const HTTPStatusCode status_code, const std::string &&body);
Maybe<std::string> getResponse() const;
HTTPStatusCode getStatusCode() const { return status_code; }
std::string getBody() const { return body; }
class BadRequestResponse
{
public:
void serialize(cereal::JSONInputArchive &ar);
std::string getMsg() const { return message; }
std::string getID() const { return message_id; }
private:
std::string message;
std::string message_id;
};
private:
HTTPStatusCode status_code;
std::string body;
};
class HTTPHeaders
{
public:
HTTPHeaders() = default;
static Maybe<HTTPHeaders> createHTTPHeader(const std::string &http_data);
void insertHeader(const std::string &header_key, const std::string &header_val);
void insertHeader(const std::string &header);
void insertHeaders(const std::string &headers);
Maybe<std::string> getHeaderVal(const std::string &header_key);
std::string toString() const;
private:
HTTPHeaders(const std::string &http_data);
std::unordered_map<std::string, std::string> headers;
};
#endif // __HTTP_CORE_H__

View File

@@ -0,0 +1,42 @@
#ifndef __MOCK_AGENT_DTEAILS_H__
#define __MOCK_AGENT_DTEAILS_H__
#include "i_agent_details.h"
#include "cptest.h"
class MockAgentDetails : public Singleton::Provide<I_AgentDetails>::From<MockProvider<I_AgentDetails>>
{
public:
MOCK_METHOD1(setFogPort, void(const uint16_t));
MOCK_METHOD1(setSSLFlag, void(const bool));
MOCK_METHOD1(setOrchestrationMode, void(const OrchestrationMode));
MOCK_METHOD1(setFogDomain, void(const std::string&));
MOCK_METHOD1(setProfileId, void(const std::string&));
MOCK_METHOD1(setTenantId, void(const std::string&));
MOCK_CONST_METHOD0(getFogPort, Maybe<uint16_t>());
MOCK_CONST_METHOD0(getSSLFlag, bool());
MOCK_CONST_METHOD0(getOrchestrationMode, OrchestrationMode());
MOCK_CONST_METHOD0(getFogDomain, Maybe<std::string>());
MOCK_CONST_METHOD0(getTenantId, std::string());
MOCK_CONST_METHOD0(getProfileId, std::string());
// Agent Details
MOCK_CONST_METHOD0(getProxy, Maybe<std::string>());
MOCK_METHOD1(setProxy, void(const std::string&));
MOCK_METHOD1(setAgentId, void(const std::string&));
MOCK_CONST_METHOD0(getAgentId, std::string());
// OpenSSL
MOCK_METHOD1(setOpenSSLDir, void(const std::string&));
MOCK_CONST_METHOD0(getOpenSSLDir, Maybe<std::string>());
// Serialization
MOCK_METHOD0(readAgentDetails, bool());
MOCK_METHOD0(writeAgentDetails, bool());
// Environment
MOCK_METHOD1(setClusterId, void(const std::string&));
};
#endif

View File

@@ -0,0 +1,32 @@
#ifndef __MOCK_AGENT_DETAILS_REPORTER_H__
#define __MOCK_AGENT_DETAILS_REPORTER_H__
#include <string>
#include "i_agent_details_reporter.h"
#include "cptest.h"
class MockAgenetDetailsReporter
:
public Singleton::Provide<I_AgentDetailsReporter>::From<MockProvider<I_AgentDetailsReporter>>
{
public:
MOCK_METHOD5(
sendReport,
void(
const metaDataReport &,
const Maybe<std::string> &,
const Maybe<std::string> &,
const Maybe<std::string> &,
const Maybe<std::string> &
)
);
MOCK_METHOD3(addAttr, bool(const std::string &key, const std::string &val, bool allow_override));
MOCK_METHOD2(addAttr, bool(const std::map<std::string, std::string> &attr, bool allow_override));
MOCK_METHOD1(deleteAttr, void(const std::string &key));
MOCK_METHOD0(sendAttributes, bool());
};
#endif // __MOCK_AGENT_DETAILS_REPORTER_H__

View File

@@ -0,0 +1,15 @@
#ifndef __MOCK_CPU_H__
#define __MOCK_CPU_H__
#include "i_cpu.h"
#include "singleton.h"
#include "cptest.h"
class MockCPU : public Singleton::Provide<I_CPU>::From<MockProvider<I_CPU>>
{
public:
MOCK_METHOD0(getCurrentProcessCPUUsage, double());
MOCK_METHOD0(getCurrentGeneralCPUUsage, Maybe<double>());
};
#endif // __MOCK_CPU_H__

View File

@@ -0,0 +1,26 @@
#ifndef __MOCK_ENCRYPTOR_H__
#define __MOCK_ENCRYPTOR_H__
#include "i_encryptor.h"
#include "cptest.h"
class MockEncryptor : public Singleton::Provide<I_Encryptor>::From<MockProvider<I_Encryptor>>
{
public:
// Base64
MOCK_METHOD1(base64Encode, std::string(const std::string &));
MOCK_METHOD1(base64Decode, std::string(const std::string &));
// Obfuscating
MOCK_METHOD1(obfuscateXor, std::string(const std::string &));
MOCK_METHOD1(obfuscateXorBase64, std::string(const std::string &));
// AES256
MOCK_METHOD1(decryptAES256obfuscateXorBase64, Maybe<std::string>(const std::string &));
MOCK_METHOD1(encryptAES256obfuscateXorBase64, std::string(const std::string &));
MOCK_METHOD1(aes256EncryptWithSizePad, std::string(const std::string &));
MOCK_METHOD1(aes256DecryptWithSizePad, Maybe<std::string>(const std::string &));
};
#endif //__MOCK_ENCRYPTOR_H__

Some files were not shown because too many files have changed in this diff Show More