2022-11-17 05:05:35 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: maat_utils.h
|
|
|
|
|
* Description: maat utils entry
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
2023-05-04 17:10:19 +08:00
|
|
|
* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved.
|
2022-11-17 05:05:35 +08:00
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _MAAT_UTILS_H_
|
|
|
|
|
#define _MAAT_UTILS_H_
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
#ifdef __cplusplus
|
2022-11-17 05:05:35 +08:00
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stddef.h>
|
2022-12-09 17:12:18 +08:00
|
|
|
#include <arpa/inet.h>
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
#define TRUE 1
|
|
|
|
|
#define FALSE 0
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
#ifndef MAX
|
|
|
|
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-02-03 17:28:14 +08:00
|
|
|
#define ALLOC(type, number) ((type *)calloc(sizeof(type), number))
|
|
|
|
|
|
|
|
|
|
#define FREE(ptr) \
|
|
|
|
|
{ \
|
|
|
|
|
if (ptr) \
|
|
|
|
|
{ \
|
|
|
|
|
free(ptr); \
|
|
|
|
|
ptr = NULL; \
|
|
|
|
|
} \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef offsetof
|
|
|
|
|
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef container_of
|
|
|
|
|
#define container_of(ptr, type, member) ({ \
|
|
|
|
|
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
|
|
|
|
(type *)( (char *)__mptr - offsetof(type,member) );})
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
#define MAX_SCANNER_HIT_COMPILE_NUM 4096
|
|
|
|
|
#define MAX_SCANNER_HIT_GROUP_NUM 4096
|
|
|
|
|
#define MAX_SCANNER_HIT_ITEM_NUM 4096
|
2023-03-23 11:57:17 +08:00
|
|
|
#define MAX_SCANNER_HIT_PATTERN_NUM 4096 * 8
|
2022-12-14 15:28:21 +08:00
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
enum ip_format {
|
|
|
|
|
IP_FORMAT_SINGLE = 1,
|
2022-12-09 17:12:18 +08:00
|
|
|
IP_FORMAT_RANGE,
|
|
|
|
|
IP_FORMAT_CIDR,
|
2023-03-27 15:52:47 +08:00
|
|
|
IP_FORMAT_MASK,
|
2022-12-09 17:12:18 +08:00
|
|
|
IP_FORMAT_UNKNOWN
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
enum port_format {
|
|
|
|
|
PORT_FORMAT_SINGLE = 1,
|
|
|
|
|
PORT_FORMAT_RANGE,
|
|
|
|
|
PORT_FORMAT_UNKNOWN
|
|
|
|
|
};
|
2022-12-09 17:12:18 +08:00
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
enum ip_format ip_format_str2int(const char *format);
|
|
|
|
|
int ip_format2range(int ip_type, enum ip_format format, const char *ip1, const char *ip2,
|
|
|
|
|
uint32_t range_begin[], uint32_t range_end[]);
|
2023-02-15 11:53:46 +08:00
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
enum port_format port_format_str2int(const char *format);
|
2022-12-09 17:12:18 +08:00
|
|
|
|
2022-12-03 22:23:41 +08:00
|
|
|
#define UNUSED __attribute__((unused))
|
|
|
|
|
|
2022-12-09 17:12:18 +08:00
|
|
|
const char *module_name_str(const char *name);
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
char *maat_strdup(const char *s);
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
void ipv6_ntoh(unsigned int *v6_addr);
|
|
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
int get_column_pos(const char *line, int column_seq, size_t *offset, size_t *len);
|
|
|
|
|
|
2023-01-30 21:59:35 +08:00
|
|
|
/* the column value must be integer */
|
2023-02-22 15:08:52 +08:00
|
|
|
long long get_column_value(const char *line, int column_seq);
|
2023-01-30 21:59:35 +08:00
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
int load_file_to_memory(const char *file_name, unsigned char **pp_out, size_t *out_sz);
|
|
|
|
|
|
|
|
|
|
char *strtok_r_esc(char *s, const char delim, char **save_ptr);
|
|
|
|
|
|
|
|
|
|
char *str_unescape_and(char *s);
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-11-25 16:32:29 +08:00
|
|
|
char *str_unescape(char *s);
|
2022-11-17 05:05:35 +08:00
|
|
|
|
2022-12-03 22:23:41 +08:00
|
|
|
char *md5_file(const char *filename, char *md5string);
|
|
|
|
|
|
|
|
|
|
int decrypt_open(const char* file_name, const char* key, const char* algorithm,
|
|
|
|
|
unsigned char**pp_out, size_t *out_sz, char* err_str, size_t err_str_sz);
|
|
|
|
|
|
|
|
|
|
int crypt_memory(const unsigned char *inbuf, size_t inlen, unsigned char **pp_out, size_t *out_sz,
|
|
|
|
|
const char *key, const char *algorithm, int do_encrypt, char *err_str, size_t err_str_sz);
|
|
|
|
|
|
|
|
|
|
int gzip_uncompress(const unsigned char *in_compressed_data, size_t in_compressed_sz,
|
|
|
|
|
unsigned char **out_uncompressed_data, size_t *out_uncompressed_sz);
|
|
|
|
|
|
|
|
|
|
size_t memcat(void **dest, size_t offset, size_t *n_dest, const void *src, size_t n_src);
|
|
|
|
|
|
2022-11-29 14:12:40 +08:00
|
|
|
/* system cmd wrapper */
|
|
|
|
|
int system_cmd_mkdir(const char* path);
|
|
|
|
|
|
2022-12-05 23:21:18 +08:00
|
|
|
int system_cmd_rmdir(const char *dir);
|
|
|
|
|
|
2023-05-04 17:10:19 +08:00
|
|
|
int system_cmd_gzip(const char *src_file, const char *dst_file);
|
|
|
|
|
|
|
|
|
|
int system_cmd_encrypt(const char *src_file, const char *dst_file, const char *password);
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
#ifdef __cplusplus
|
2022-11-17 05:05:35 +08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|