Skip to content

Commit

Permalink
config: better error values for ndpi_set_config()
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanNardi committed Dec 30, 2023
1 parent 92ec8c9 commit fd3ab11
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 63 deletions.
14 changes: 8 additions & 6 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ static void ndpiCheckIPMatch(char *testChar) {
struct in_addr addr;
char appBufStr[64];
ndpi_protocol detected_protocol;
int i, rc;
int i;
ndpi_cfg_error rc;

if(!testChar)
return;
Expand All @@ -369,7 +370,7 @@ static void ndpiCheckIPMatch(char *testChar) {
for(i = 0; i < num_cfgs; i++) {
rc = ndpi_set_config(ndpi_str,
cfgs[i].proto, cfgs[i].param, cfgs[i].value);
if (rc != 0)
if (rc < NDPI_CFG_OK)
fprintf(stderr, "Error setting config [%s][%s][%s]: %d\n",
cfgs[i].proto, cfgs[i].param, cfgs[i].value, rc);
}
Expand Down Expand Up @@ -554,7 +555,7 @@ static void help(u_int long_help) {
" | <d> = max packet payload dissection\n"
" | <d> = max num reported payloads\n"
" | Default: %u:%u:%u:%u:%u\n"
" -c <path> | Load custom categories from the specified file\n"
" -c <path> | Load custom categories from the specified file. It is a shortcut to --cfg=,NULL,filename.categories,<path>\n"
" -C <path> | Write output in CSV format on the specified file\n"
" -r <path> | Load risky domain file. It is a shortcut to --cfg=,NULL,filename.risky_domains,<path>\n"
" -j <path> | Load malicious JA3 fingeprints. It is a shortcut to --cfg=,NULL,filename.malicious_ja3,<path>\n"
Expand All @@ -568,7 +569,7 @@ static void help(u_int long_help) {
" | 1 = verbose\n"
" | 2 = very verbose\n"
" | 3 = port stats\n"
" | 4 = hash stats\n"
" | 4 = hash stats\n"
" -V <1-4> | nDPI logging level\n"
" | 1 - trace, 2 - debug, 3 - full debug\n"
" | >3 - full debug + log enabled for all protocols (i.e. '-u all')\n"
Expand Down Expand Up @@ -2674,7 +2675,8 @@ static void debug_printf(u_int32_t protocol, void *id_struct,
*/
static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
struct ndpi_workflow_prefs prefs;
int i, rc;
int i;
ndpi_cfg_error rc;

memset(&prefs, 0, sizeof(prefs));
prefs.decode_tunnels = decode_tunnels;
Expand Down Expand Up @@ -2702,7 +2704,7 @@ static void setupDetection(u_int16_t thread_id, pcap_t * pcap_handle) {
for(i = 0; i < num_cfgs; i++) {
rc = ndpi_set_config(ndpi_thread_info[thread_id].workflow->ndpi_struct,
cfgs[i].proto, cfgs[i].param, cfgs[i].value);
if (rc != 0)
if (rc < NDPI_CFG_OK)
fprintf(stderr, "Error setting config [%s][%s][%s]: %d\n",
cfgs[i].proto, cfgs[i].param, cfgs[i].value, rc);
}
Expand Down
4 changes: 2 additions & 2 deletions src/include/ndpi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2059,8 +2059,8 @@ extern "C" {

/* ******************************* */

int ndpi_set_config(struct ndpi_detection_module_struct *ndpi_str,
const char *proto, const char *param, const char *value);
ndpi_cfg_error ndpi_set_config(struct ndpi_detection_module_struct *ndpi_str,
const char *proto, const char *param, const char *value);
char *ndpi_get_config(struct ndpi_detection_module_struct *ndpi_str,
const char *proto, const char *param, char *buf, int buf_len);
char *ndpi_dump_config(struct ndpi_detection_module_struct *ndpi_str,
Expand Down
8 changes: 8 additions & 0 deletions src/include/ndpi_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ typedef enum {
ndpi_leaf
} ndpi_VISIT;

typedef enum {
NDPI_CFG_INVALID_PARAM = -100,
NDPI_CFG_NOT_FOUND,
NDPI_CFG_INVALID_VALUE,
NDPI_CFG_MEM_ERROR,

NDPI_CFG_OK = 0,
} ndpi_cfg_error;

/* NDPI_MASK_SIZE */
typedef u_int32_t ndpi_ndpi_mask;
Expand Down
111 changes: 56 additions & 55 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4526,7 +4526,8 @@ static int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_str,
*/
int load_config_file_fd(struct ndpi_detection_module_struct *ndpi_str, FILE *fd) {
char buffer[512], *line, *proto, *param = NULL, *value, *saveptr;
int len, rc;
int len;
ndpi_cfg_error rc;

if(!ndpi_str || !fd)
return -1;
Expand Down Expand Up @@ -4557,7 +4558,7 @@ int load_config_file_fd(struct ndpi_detection_module_struct *ndpi_str, FILE *fd)
value = strtok_r(NULL, ",", &saveptr);
if(value) {
rc = ndpi_set_config(ndpi_str, proto, param, value);
if(rc != 0) {
if(rc < NDPI_CFG_OK) {
NDPI_LOG_ERR(ndpi_str, "Error ndpi_set_config [%s/%s/%s]: %d\n",
proto, param, value, rc);
return rc;
Expand Down Expand Up @@ -10399,30 +10400,30 @@ static u_int16_t __get_proto_id(const char *proto_name)
return proto_id;
}

static int _set_param_enable_disable(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
static ndpi_cfg_error _set_param_enable_disable(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
{
int *variable = (int *)_variable;

if(strcmp(value, "1") == 0 ||
strcmp(value, "enable") == 0) {
*variable = 1;
return 0;
return NDPI_CFG_OK;
}
if(strcmp(value, "0") == 0 ||
strcmp(value, "disable") == 0) {
*variable = 0;
return 0;
return NDPI_CFG_OK;
}
return -1;
return NDPI_CFG_INVALID_VALUE;
}

static int _set_param_int(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
static ndpi_cfg_error _set_param_int(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
{
int *variable = (int *)_variable;
char *endptr;
Expand All @@ -10434,11 +10435,11 @@ static int _set_param_int(struct ndpi_detection_module_struct *ndpi_str,
/* Check for various possible errors */
if((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
(errno != 0 && val == 0)) {
return -1;
return NDPI_CFG_INVALID_VALUE;
}
if (endptr == value) {
/* No digits were found */
return -1;
return NDPI_CFG_INVALID_VALUE;
}
/* If we got here, strtol() successfully parsed a number */
*variable = val;
Expand All @@ -10447,9 +10448,9 @@ static int _set_param_int(struct ndpi_detection_module_struct *ndpi_str,
to integers without too many checks...*/
if(min_value && max_value &&
(val < strtol(min_value, NULL, 0) || val > strtol(max_value, NULL, 0)))
return -1;
return NDPI_CFG_INVALID_VALUE;

return 0;
return NDPI_CFG_OK;
}

static char *_get_param_int(void *_variable, const char *proto, char *buf, int buf_len)
Expand All @@ -10461,17 +10462,17 @@ static char *_get_param_int(void *_variable, const char *proto, char *buf, int b
return buf;
}

static int _set_param_string(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
static ndpi_cfg_error _set_param_string(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
{
char **variable = (char **)_variable;

*variable = ndpi_strdup(value);
if(!*variable)
return -1;
return 0;
return NDPI_CFG_MEM_ERROR;
return NDPI_CFG_OK;
}

static char *_get_param_string(void *_variable, const char *proto, char *buf, int buf_len)
Expand All @@ -10483,30 +10484,30 @@ static char *_get_param_string(void *_variable, const char *proto, char *buf, in
return buf;
}

static int _set_param_filename(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
static ndpi_cfg_error _set_param_filename(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
{
char **variable = (char **)_variable;

if(value == NULL) { /* Valid value */
*variable = NULL;
return 0;
return NDPI_CFG_OK;
}

if(access(value, F_OK) != 0)
return -1;
return NDPI_CFG_INVALID_VALUE;
*variable = ndpi_strdup(value);
if(!*variable)
return -1;
return 0;
return NDPI_CFG_MEM_ERROR;
return NDPI_CFG_OK;
}

static int _set_param_filename_config(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
static ndpi_cfg_error _set_param_filename_config(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
{
int rc;
FILE *fd;
Expand All @@ -10517,13 +10518,13 @@ static int _set_param_filename_config(struct ndpi_detection_module_struct *ndpi_

fd = fopen(value, "r");
if(fd == NULL)
return -1; /* It shoudn't happen because we already checked it */
return NDPI_CFG_INVALID_VALUE; /* It shoudn't happen because we already checked it */
rc = load_config_file_fd(ndpi_str, fd);
fclose(fd);
if(rc < 0)
return rc;

return 0;
return NDPI_CFG_OK;
}


Expand All @@ -10541,10 +10542,10 @@ static char *_get_param_protocol_enable_disable(void *_variable, const char *pro
return buf;
}

static int _set_param_protocol_enable_disable(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
static ndpi_cfg_error _set_param_protocol_enable_disable(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto)
{
NDPI_PROTOCOL_BITMASK *bitmask = (NDPI_PROTOCOL_BITMASK *)_variable;
u_int16_t proto_id;
Expand All @@ -10555,30 +10556,30 @@ static int _set_param_protocol_enable_disable(struct ndpi_detection_module_struc
if(strcmp(value, "1") == 0 ||
strcmp(value, "enable") == 0) {
NDPI_BITMASK_SET_ALL(*bitmask);
return 0;
return NDPI_CFG_OK;
}
if(strcmp(value, "0") == 0 ||
strcmp(value, "disable") == 0) {
NDPI_BITMASK_RESET(*bitmask);
return 0;
return NDPI_CFG_OK;
}
}

proto_id = __get_proto_id(proto);
if(proto_id == NDPI_PROTOCOL_UNKNOWN)
return -1;
return NDPI_CFG_INVALID_VALUE;

if(strcmp(value, "1") == 0 ||
strcmp(value, "enable") == 0) {
NDPI_BITMASK_ADD(*bitmask, proto_id);
return 0;
return NDPI_CFG_OK;
}
if(strcmp(value, "0") == 0 ||
strcmp(value, "disable") == 0) {
NDPI_BITMASK_DEL(*bitmask, proto_id);
return 0;
return NDPI_CFG_OK;
}
return -1;
return NDPI_CFG_INVALID_VALUE;
}


Expand All @@ -10592,10 +10593,10 @@ enum cfg_param_type {
};


typedef int (*cfg_set)(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto);
typedef ndpi_cfg_error (*cfg_set)(struct ndpi_detection_module_struct *ndpi_str,
void *_variable, const char *value,
const char *min_value, const char *max_value,
const char *proto);
typedef char *(*cfg_get)(void *_variable, const char *proto, char *buf, int buf_len);

static const struct cfg_op {
Expand Down Expand Up @@ -10744,13 +10745,13 @@ static void free_config(struct ndpi_detection_module_config_struct *cfg)
}
}

int ndpi_set_config(struct ndpi_detection_module_struct *ndpi_str,
const char *proto, const char *param, const char *value)
ndpi_cfg_error ndpi_set_config(struct ndpi_detection_module_struct *ndpi_str,
const char *proto, const char *param, const char *value)
{
const struct cfg_param *c;

if(!ndpi_str || !param || !value)
return -2;
return NDPI_CFG_INVALID_PARAM;

NDPI_LOG_DBG(ndpi_str, "Set [%s][%s][%s]\n", proto, param, value);

Expand All @@ -10766,7 +10767,7 @@ int ndpi_set_config(struct ndpi_detection_module_struct *ndpi_str,
value, c->min_value, c->max_value, proto);
}
}
return -3;
return NDPI_CFG_NOT_FOUND;
}

char *ndpi_get_config(struct ndpi_detection_module_struct *ndpi_str,
Expand Down

0 comments on commit fd3ab11

Please sign in to comment.