diff options
Diffstat (limited to 'src/nSocket/nstack/nstack_info_parse.c')
-rw-r--r-- | src/nSocket/nstack/nstack_info_parse.c | 736 |
1 files changed, 425 insertions, 311 deletions
diff --git a/src/nSocket/nstack/nstack_info_parse.c b/src/nSocket/nstack/nstack_info_parse.c index 8b2d62f..2f74cf6 100644 --- a/src/nSocket/nstack/nstack_info_parse.c +++ b/src/nSocket/nstack/nstack_info_parse.c @@ -28,6 +28,8 @@ #include "json.h" #include "nsfw_base_linux_api.h" #include "nstack_info_parse.h" +#include "nstack_rd_api.h" +#include "nstack_rd_priv.h" /*get string value*/ #define NSTACK_JSON_PARSE_STRING(obj, name, lent, result, index) do { \ @@ -41,7 +43,7 @@ NSSOC_LOGERR("can't get value from %s index:%d", name, (index)); \ goto RETURN_ERROR; \ } \ - (void)STRNCPY_S((result), (lent), temp_value1, (lent)); \ + (void)strncpy_s((result), (lent), temp_value1, (lent)-1); \ } \ else \ { \ @@ -66,7 +68,7 @@ } \ else \ { \ - if (strcmp(name, "deploytype") == 0 || strcmp(name, "stackid") == 0) \ + if (strcmp(name, "deploytype") == 0 || strcmp(name, "stackid") == 0) \ { \ NSSOC_LOGERR("can't get obj from %s index:%d", name, (index)); \ } \ @@ -77,449 +79,561 @@ } \ } while ( 0 ); -/*parse module cfg*/ -int -nstack_parse_module_cfg_json (char *param) +/* load default config */ +static int load_default_module() { - struct json_object *obj = json_tokener_parse (param); - struct json_object *module_list_obj = NULL; - struct json_object *module_obj = NULL; - struct json_object *temp_obj = NULL; - const char *default_name = NULL; - const char *temp_value = NULL; - int module_num = 0; - int ret = NSTACK_RETURN_FAIL; - int index = 0; - int icnt = 0; - int ret_val; + if (EOK != + strcpy_s(g_nstack_module_desc[0].modName, NSTACK_MODULE_NAME_MAX, + RD_KERNEL_NAME)) + { + NSSOC_LOGERR("strcpy_s failed!"); + return NSTACK_RETURN_FAIL; + } + if (EOK != + strcpy_s(g_nstack_module_desc[0].register_fn_name, + NSTACK_MODULE_NAME_MAX, "kernel_stack_register")) + { + NSSOC_LOGERR("strcpy_s failed!"); + return NSTACK_RETURN_FAIL; + } + if (EOK != + strcpy_s(g_nstack_module_desc[0].libPath, NSTACK_MODULE_NAME_MAX, + "./")) + { + NSSOC_LOGERR("strcpy_s failed!"); + return NSTACK_RETURN_FAIL; + } + g_nstack_module_desc[0].deploytype = NSTACK_MODEL_TYPE1; + g_nstack_module_desc[0].libtype = NSTACK_LIB_LOAD_STATIC; + g_nstack_module_desc[0].default_stack = 1; + g_nstack_module_desc[0].priority = 0; + g_nstack_module_desc[0].maxfdid = 8191; + g_nstack_module_desc[0].minfdid = 0; + g_nstack_module_desc[0].modInx = 0; + + if (EOK != + strcpy_s(g_nstack_module_desc[1].modName, NSTACK_MODULE_NAME_MAX, + "stackpool")) + { + NSSOC_LOGERR("strcpy_s failed!"); + return NSTACK_RETURN_FAIL; + } + if (EOK != + strcpy_s(g_nstack_module_desc[1].register_fn_name, + NSTACK_MODULE_NAME_MAX, "nstack_stack_register")) + { + NSSOC_LOGERR("strcpy_s failed!"); + return NSTACK_RETURN_FAIL; + } + if (EOK != + strcpy_s(g_nstack_module_desc[1].libPath, NSTACK_MODULE_NAME_MAX, + "libnstack.so")) + { + NSSOC_LOGERR("strcpy_s failed!"); + return NSTACK_RETURN_FAIL; + } + g_nstack_module_desc[1].deploytype = NSTACK_MODEL_TYPE3; + g_nstack_module_desc[1].libtype = NSTACK_LIB_LOAD_DYN; + g_nstack_module_desc[1].default_stack = 0; + g_nstack_module_desc[1].priority = 0; + g_nstack_module_desc[1].maxfdid = 8192; + g_nstack_module_desc[1].minfdid = 0; + g_nstack_module_desc[1].modInx = 1; + + g_module_num = 2; + return NSTACK_RETURN_OK; +} - if (!obj) +/*parse module cfg*/ +static int parse_module_cfg(char *param) +{ + struct json_object *obj = json_tokener_parse(param); + struct json_object *module_list_obj = NULL; + struct json_object *module_obj = NULL; + struct json_object *temp_obj = NULL; + const char *default_name = NULL; + const char *temp_value = NULL; + int module_num = 0; + int ret = NSTACK_RETURN_FAIL; + int index = 0; /* local variable:index */ + int icnt = 0; + + if (!obj) { - NSSOC_LOGERR ("json parse fail"); - return NSTACK_RETURN_FAIL; + NSSOC_LOGERR("json parse fail"); + return NSTACK_RETURN_FAIL; } - (void) MEMSET_S (&g_nstack_module_desc[0], sizeof (g_nstack_module_desc), 0, - sizeof (g_nstack_module_desc)); + (void) memset_s(&g_nstack_module_desc[0], sizeof(g_nstack_module_desc), + 0, sizeof(g_nstack_module_desc)); - (void) json_object_object_get_ex (obj, "default_stack_name", &temp_obj); - if (!temp_obj) + (void) json_object_object_get_ex(obj, "default_stack_name", &temp_obj); + if (!temp_obj) { - NSSOC_LOGERR ("can't get module_list"); - goto RETURN_ERROR; + NSSOC_LOGERR("can't get module_list"); + goto RETURN_ERROR; } - default_name = json_object_get_string (temp_obj); + default_name = json_object_get_string(temp_obj); - (void) json_object_object_get_ex (obj, "module_list", &module_list_obj); - if (!module_list_obj) + (void) json_object_object_get_ex(obj, "module_list", &module_list_obj); + if (!module_list_obj) { - NSSOC_LOGERR ("can't get module_list"); - goto RETURN_ERROR; + NSSOC_LOGERR("can't get module_list"); + goto RETURN_ERROR; } - module_num = json_object_array_length (module_list_obj); - if ((module_num <= 0) || (module_num >= NSTACK_MAX_MODULE_NUM)) + module_num = json_object_array_length(module_list_obj); + if ((module_num <= 0) || (module_num >= NSTACK_MAX_MODULE_NUM)) { - NSSOC_LOGERR ("get module number:%d fail", module_num); - goto RETURN_ERROR; + NSSOC_LOGERR("get module number:%d fail", module_num); + goto RETURN_ERROR; } - for (index = 0; index < module_num; index++) + for (index = 0; index < module_num; index++) { - module_obj = json_object_array_get_idx (module_list_obj, index); - if (module_obj) + module_obj = json_object_array_get_idx(module_list_obj, index); + if (module_obj) { - NSTACK_JSON_PARSE_STRING (module_obj, "stack_name", MODULE_NAME_MAX, - &(g_nstack_module_desc[icnt].modName[0]), - index); - STRCPY_S (&(g_nstack_module_desc[icnt].register_fn_name[0]), - sizeof (& - (g_nstack_module_desc[icnt].register_fn_name[0])), - &(g_nstack_module_desc[icnt].modName[0])); - ret_val = - STRCAT_S (&(g_nstack_module_desc[icnt].register_fn_name[0]), - sizeof (& - (g_nstack_module_desc[icnt].register_fn_name - [0])), "_stack_register"); - if (EOK != ret_val) + NSTACK_JSON_PARSE_STRING(module_obj, "stack_name", + NSTACK_MODULE_NAME_MAX, + &(g_nstack_module_desc[icnt].modName + [0]), index); + NSTACK_JSON_PARSE_STRING(module_obj, "function_name", + NSTACK_MODULE_NAME_MAX, + &(g_nstack_module_desc + [icnt].register_fn_name[0]), index); + NSTACK_JSON_PARSE_STRING(module_obj, "libname", + NSTACK_MODULE_NAME_MAX, + &(g_nstack_module_desc[icnt].libPath + [0]), index); + + (void) json_object_object_get_ex(module_obj, "loadtype", + &temp_obj); + if (temp_obj) { - NSFW_LOGERR ("register_fn_name STRCAT_S failed]ret_val=%d", - ret_val); - return -1; - } - NSTACK_JSON_PARSE_STRING (module_obj, "libname", MODULE_NAME_MAX, - &(g_nstack_module_desc[icnt].libPath[0]), - index); - - (void) json_object_object_get_ex (module_obj, "loadtype", - &temp_obj); - if (temp_obj) - { - temp_value = json_object_get_string (temp_obj); - if (strcmp (temp_value, "static") == 0) + temp_value = json_object_get_string(temp_obj); + if (temp_value && (strcmp(temp_value, "static") == 0)) { - g_nstack_module_desc[icnt].libtype = NSTACK_LIB_LOAD_STATIC; + g_nstack_module_desc[icnt].libtype = + NSTACK_LIB_LOAD_STATIC; } - else + else { - g_nstack_module_desc[icnt].libtype = NSTACK_LIB_LOAD_DYN; + g_nstack_module_desc[icnt].libtype = NSTACK_LIB_LOAD_DYN; } } - else + else { - if (strcmp (g_nstack_module_desc[icnt].modName, "kernel") == 0) + if (strcmp + (g_nstack_module_desc[icnt].modName, RD_KERNEL_NAME) == 0) { - g_nstack_module_desc[icnt].libtype = NSTACK_LIB_LOAD_STATIC; + g_nstack_module_desc[icnt].libtype = + NSTACK_LIB_LOAD_STATIC; } - else + else { - g_nstack_module_desc[icnt].libtype = NSTACK_LIB_LOAD_DYN; + g_nstack_module_desc[icnt].libtype = NSTACK_LIB_LOAD_DYN; } - NSSOC_LOGWAR ("can't get the value of loadtype for module:%s", - g_nstack_module_desc[icnt].modName); + NSSOC_LOGWAR("can't get the value of loadtype for module:%s", + g_nstack_module_desc[icnt].modName); } - NSSBR_LOGINF ("load type of %d has been chosen", - g_nstack_module_desc[icnt].libtype); - NSTACK_JSON_PARSE_INT (module_obj, "deploytype", MODULE_NAME_MAX, - g_nstack_module_desc[icnt].deploytype, - index); - NSTACK_JSON_PARSE_INT (module_obj, "maxfd", MODULE_NAME_MAX, - g_nstack_module_desc[icnt].maxfdid, index); - if (g_nstack_module_desc[icnt].maxfdid == 0) + NSTACK_JSON_PARSE_INT(module_obj, "deploytype", + NSTACK_MODULE_NAME_MAX, + g_nstack_module_desc[icnt].deploytype, + index); + g_nstack_module_desc[icnt].maxfdid = 8191; + NSTACK_JSON_PARSE_INT(module_obj, "maxfd", + NSTACK_MODULE_NAME_MAX, + g_nstack_module_desc[icnt].maxfdid, index); + g_nstack_module_desc[icnt].minfdid = 0; + NSTACK_JSON_PARSE_INT(module_obj, "minfd", + NSTACK_MODULE_NAME_MAX, + g_nstack_module_desc[icnt].minfdid, index); + NSTACK_JSON_PARSE_INT(module_obj, "priorty", + NSTACK_MODULE_NAME_MAX, + g_nstack_module_desc[icnt].priority, index); + NSTACK_JSON_PARSE_INT(module_obj, "stackid", + NSTACK_MODULE_NAME_MAX, + g_nstack_module_desc[icnt].modInx, index); + if (icnt != g_nstack_module_desc[icnt].modInx) { - g_nstack_module_desc[icnt].maxfdid = 1024; + NSSOC_LOGERR + ("stackid mismatch, expected:%d, actually given:%d", icnt, + g_nstack_module_desc[icnt].modInx); + goto RETURN_ERROR; } - NSTACK_JSON_PARSE_INT (module_obj, "minfd", MODULE_NAME_MAX, - g_nstack_module_desc[icnt].minfdid, index); - NSTACK_JSON_PARSE_INT (module_obj, "priorty", MODULE_NAME_MAX, - g_nstack_module_desc[icnt].priority, index); - NSTACK_JSON_PARSE_INT (module_obj, "stackid", MODULE_NAME_MAX, - g_nstack_module_desc[icnt].modInx, index); - if (0 == strcmp (g_nstack_module_desc[icnt].modName, default_name)) + if (0 == strcmp(g_nstack_module_desc[icnt].modName, default_name)) { - g_nstack_module_desc[icnt].default_stack = 1; + g_nstack_module_desc[icnt].default_stack = 1; } - icnt++; - g_module_num = icnt; + icnt++; + g_module_num = icnt; } } - ret = NSTACK_RETURN_OK; + ret = NSTACK_RETURN_OK; -RETURN_ERROR: - json_object_put (obj); - return ret; + RETURN_ERROR: + json_object_put(obj); + return ret; } -/*parse module cfg*/ -int -nstack_parse_rd_cfg_json (char *param, rd_route_data ** data, int *num) +static int rd_do_parse(struct json_object *obj, const char *name, void *table) { - struct json_object *obj = json_tokener_parse (param); - struct json_object *ip_list_obj = NULL; - struct json_object *proto_list_obj = NULL; - struct json_object *module_obj = NULL; - struct json_object *temp_obj = NULL; - rd_route_data *rdtemp = NULL; - rd_route_data *rddata = NULL; - const char *sub = NULL; - const char *temp_value = NULL; - int ip_list_num = 0; - int proto_list_num = 0; - int totalnum = 0; - int tlen = 0; - int index = 0; - int icnt = 0; - char ipadd[32] = { 0 }; - - if (!obj) + struct json_object *ip = NULL; + struct json_object *proto = NULL; + struct json_object *type = NULL; + struct json_object *temp = NULL; + struct json_object *o = NULL; + int index = 0; /* local variable:index */ + int ip_list_num = 0, type_list_num = 0, proto_list_num = 0; + const char *ip_addr; + char *sub = NULL; + char addr[32]; + + rd_ip_data ip_data; + rd_type_data type_data; + rd_proto_data proto_data; + + (void) json_object_object_get_ex(obj, "ip_route", &ip); + (void) json_object_object_get_ex(obj, "protocol_route", &proto); + (void) json_object_object_get_ex(obj, "type_route", &type); + if (!ip && !proto && !type) { - NSSOC_LOGERR ("json parse fail"); - return NSTACK_RETURN_FAIL; + NSSOC_LOGERR("Error: no rd policies found!"); + return -1; } - (void) json_object_object_get_ex (obj, "ip_route", &ip_list_obj); - if (!ip_list_obj) + if (ip) + { + ip_list_num = json_object_array_length(ip); + } + if (type) { - NSSOC_LOGERR ("can't get module_list"); - goto RETURN_ERROR; + type_list_num = json_object_array_length(type); } - (void) json_object_object_get_ex (obj, "prot_route", &proto_list_obj); - if (!proto_list_obj) + if (proto) { - NSSOC_LOGERR ("can't get module_list"); - goto RETURN_ERROR; + proto_list_num = json_object_array_length(proto); } - ip_list_num = json_object_array_length (ip_list_obj); - proto_list_num = json_object_array_length (proto_list_obj); - totalnum = ip_list_num + proto_list_num; - if (totalnum > NSTACK_RD_MAX) + for (index = 0; index < ip_list_num; index++) { - NSSOC_LOGERR ("rd num is too more, and return fail"); - goto RETURN_ERROR; + temp = json_object_array_get_idx(ip, index); + if (temp) + { + ip_addr = json_object_get_string(temp); + if (!ip_addr) + { + NSSOC_LOGERR("cannot get ip address at index:%d", index); + return -1; + } + sub = strstr(ip_addr, "/"); + if (!sub) + { + NSSOC_LOGERR("cannot get masklen from %s", ip_addr); + return -1; + } + if (EOK != memset_s(addr, sizeof(addr), 0, sizeof(addr))) + { + NSSOC_LOGERR("memset_s failed!"); + return -1; + } + if (EOK != + strncpy_s(addr, sizeof(addr), ip_addr, + (size_t) (sub - ip_addr))) + { + NSSOC_LOGERR("strncpy_s failed!"); + return -1; + } + ip_data.addr = inet_addr(addr); + ip_data.masklen = atoi(sub + 1); /* not deprecated */ + ip_data.resev[0] = 0; + ip_data.resev[1] = 0; + nstack_rd_ip_node_insert(name, &ip_data, table); + } } - tlen = sizeof (rd_route_data) * totalnum; - rdtemp = (rd_route_data *) malloc (tlen); - if (!rdtemp) + + for (index = 0; index < type_list_num; index++) { - NSSOC_LOGERR ("malloc mem fail"); - goto RETURN_ERROR; + temp = json_object_array_get_idx(type, index); + if (temp) + { + (void) json_object_object_get_ex(temp, "value", &o); + if (!o) + { + NSSOC_LOGERR("no value specified of type_route index:%d", + index); + return -1; + } + type_data.value = json_object_get_int(o); + o = NULL; + (void) json_object_object_get_ex(temp, "attr", &o); + if (!o) + { + NSSOC_LOGERR("no attr specified of type_route index:%d", + index); + return -1; + } + type_data.attr = json_object_get_int(o); + type_data.reserved[0] = 0; + type_data.reserved[1] = 0; + type_data.reserved[2] = 0; + type_data.reserved[3] = 0; + nstack_rd_type_node_insert(name, &type_data, table); + o = NULL; + } } - MEMSET_S (rdtemp, tlen, 0, tlen); - for (index = 0; index < ip_list_num; index++) + + for (index = 0; index < proto_list_num; index++) { - module_obj = json_object_array_get_idx (ip_list_obj, index); - if (module_obj) + temp = json_object_array_get_idx(proto, index); + if (temp) { - rddata = rdtemp + icnt; - rddata->type = RD_DATA_TYPE_IP; - (void) json_object_object_get_ex (module_obj, "stack_name", - &temp_obj); - if (temp_obj) + (void) json_object_object_get_ex(temp, "value", &o); + if (!o) { - temp_value = json_object_get_string (temp_obj); - if (!temp_value) - { - NSSOC_LOGERR ("can't get value from subnet index:%d", - index); - goto RETURN_ERROR; - } - (void) STRNCPY_S (rddata->stack_name, RD_PLANE_NAMELEN, - temp_value, RD_PLANE_NAMELEN); + NSSOC_LOGERR("no value specified of protocol_route index:%d", + index); + return -1; } - (void) json_object_object_get_ex (module_obj, "subnet", &temp_obj); - if (temp_obj) + proto_data.value = json_object_get_int(o); + o = NULL; + (void) json_object_object_get_ex(temp, "attr", &o); + if (!o) { - temp_value = json_object_get_string (temp_obj); - if (!temp_value) - { - NSSOC_LOGERR ("can't get value from subnet index:%d", - index); - goto RETURN_ERROR; - } - sub = strstr (temp_value, "/"); - if (!sub) - { - NSSOC_LOGERR ("can't get maskklen from %s", temp_value); - goto RETURN_ERROR; - } - (void) MEMSET_S (ipadd, sizeof (ipadd), 0, sizeof (ipadd)); - (void) STRNCPY_S (ipadd, sizeof (ipadd), temp_value, - (size_t) (sub - temp_value)); - rddata->ipdata.masklen = atoi (sub + 1); - rddata->ipdata.addr = ntohl (inet_addr (ipadd)); - icnt++; + NSSOC_LOGERR("no attr specified of protocol_route index:%d", + index); + return -1; } + proto_data.attr = json_object_get_int(o); + nstack_rd_proto_node_insert(name, &proto_data, table); + o = NULL; } } - for (index = 0; index < proto_list_num; index++) + return 0; +} + +/*parse rd cfg*/ +static int parse_rd_cfg(char *param, const char *name, void *table) +{ + struct json_object *obj = json_tokener_parse(param); + struct json_object *module_obj = NULL; + struct json_object *temp_obj = NULL; + struct json_object *modules = NULL; + const char *module_name; + int total = 0; + int i = 0; + + if ((!name) || (!obj)) + { + NSSOC_LOGERR("json parse fail"); + return NSTACK_RETURN_FAIL; + } + + (void) json_object_object_get_ex(obj, "modules", &modules); + if (!modules) { - module_obj = json_object_array_get_idx (proto_list_obj, index); - if (module_obj) + NSSOC_LOGERR("can't get modules"); + goto RETURN_ERROR; + } + + total = json_object_array_length(modules); + if (total > NSTACK_MAX_MODULE_NUM) + { + NSSOC_LOGERR("too many modules specified!"); + goto RETURN_ERROR; + } + for (i = 0; i < total; i++) + { + module_obj = json_object_array_get_idx(modules, i); + if (module_obj) { - rddata = rdtemp + icnt; - rddata->type = RD_DATA_TYPE_PROTO; - (void) json_object_object_get_ex (module_obj, "stack_name", - &temp_obj); - if (temp_obj) + (void) json_object_object_get_ex(module_obj, "name", &temp_obj); + if (temp_obj) { - temp_value = json_object_get_string (temp_obj); - if (!temp_value) + module_name = json_object_get_string(temp_obj); + if (!module_name) { - NSSOC_LOGERR ("can't get value from proto index:%d", index); - goto RETURN_ERROR; + NSSOC_LOGERR("cannot get module name at index:%d", i); + goto RETURN_ERROR; } - (void) STRNCPY_S (rddata->stack_name, RD_PLANE_NAMELEN, - temp_value, RD_PLANE_NAMELEN); - } - (void) json_object_object_get_ex (module_obj, "proto_type", - &temp_obj); - if (temp_obj) - { - temp_value = json_object_get_string (temp_obj); - if (!temp_value) + if (strcmp(module_name, name) == 0) // this is what we are looking for { - NSSOC_LOGERR ("can't get value from proto index:%d", index); - goto RETURN_ERROR; + if (rd_do_parse(module_obj, module_name, table)) + { + NSSOC_LOGERR("parse failed at index:%d", i); + goto RETURN_ERROR; + } + break; } - rddata->proto_type = atoi (temp_value); - icnt++; } } } - *data = rdtemp; - *num = icnt; - json_object_put (obj); - return NSTACK_RETURN_OK; -RETURN_ERROR: - json_object_put (obj); - if (rdtemp) - { - free (rdtemp); - } - return -1; + json_object_put(obj); + return NSTACK_RETURN_OK; + RETURN_ERROR: + json_object_put(obj); + return -1; } /*read json file, and return a buf, if return success, the caller need to free **buf*/ -int -nstack_json_file_read (char *filename, char **buf) +static int read_json_file(char *filename, char **buf) { - char *cfg_buf = NULL; - int fp = 0; - off_t file_len = 0; - off_t buff_len = 0; - int ret = NSTACK_RETURN_FAIL; + char *cfg_buf = NULL; + int fp = 0; + off_t file_len = 0; + off_t buff_len = 0; + int ret = NSTACK_RETURN_FAIL; - if ((!filename) || (!buf)) + if ((!filename) || (!buf)) { - return NSTACK_RETURN_FAIL; + return NSTACK_RETURN_FAIL; } - fp = open (filename, O_RDONLY); - if (fp < 0) + fp = open(filename, O_RDONLY); + if (fp < 0) { - NSSOC_LOGERR ("open %s fail, error:%d!", filename, errno); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR("open %s fail, error:%d!", filename, errno); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - file_len = lseek (fp, 0, SEEK_END); - if (file_len <= 0) + file_len = lseek(fp, 0, SEEK_END); + if (file_len <= 0) { - NSSOC_LOGERR ("failed to get file len]file name=%s", filename); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR("failed to get file len]file name=%s", filename); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - if (file_len > NSTACK_CFG_FILELEN_MAX) + if (file_len > NSTACK_CFG_FILELEN_MAX) { - NSSOC_LOGERR - ("file len is too big]file len=%d, max len=%d, file name=%s", - file_len, NSTACK_CFG_FILELEN_MAX, filename); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR + ("file len is too big]file len=%ld, max len=%d, file name=%s", + file_len, NSTACK_CFG_FILELEN_MAX, filename); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - ret = lseek (fp, 0, SEEK_SET); - if (ret < 0) + ret = lseek(fp, 0, SEEK_SET); + if (ret < 0) { - NSSOC_LOGERR ("seek to start failed]file name=%s", filename); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR("seek to start failed]file name=%s", filename); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - buff_len = file_len + 1; - cfg_buf = (char *) malloc (buff_len); - if (!cfg_buf) + buff_len = file_len + 1; + cfg_buf = (char *) malloc(buff_len); + if (!cfg_buf) { - NSSOC_LOGERR ("malloc buff failed]buff_len=%d", buff_len); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR("malloc buff failed]buff_len=%ld", buff_len); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - ret = MEMSET_S (cfg_buf, buff_len, 0, buff_len); - if (NSTACK_RETURN_OK != ret) + ret = memset_s(cfg_buf, buff_len, 0, buff_len); + if (NSTACK_RETURN_OK != ret) { - NSSOC_LOGERR ("MEMSET_S failed]ret=%d.", ret); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR("memset_s failed]ret=%d.", ret); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - ret = nsfw_base_read (fp, cfg_buf, buff_len - 1); - if (ret <= 0) + ret = nsfw_base_read(fp, cfg_buf, buff_len - 1); + if (ret <= 0) { - NSSOC_LOGERR ("read failed]ret=%d, errno:%d", ret, errno); - ret = NSTACK_RETURN_FAIL; - goto RETURN_RELEASE; + NSSOC_LOGERR("read failed]ret=%d, errno:%d", ret, errno); + ret = NSTACK_RETURN_FAIL; + goto RETURN_RELEASE; } - *buf = cfg_buf; - nsfw_base_close (fp); - return NSTACK_RETURN_OK; -RETURN_RELEASE: - if (fp >= 0) + *buf = cfg_buf; + nsfw_base_close(fp); + return NSTACK_RETURN_OK; + RETURN_RELEASE: + if (fp >= 0) { - nsfw_base_close (fp); + nsfw_base_close(fp); } - if (cfg_buf) + if (cfg_buf) { - free (cfg_buf); + free(cfg_buf); } - return ret; + return ret; } /*parse module cfg file*/ -int -nstack_module_parse () +int nstack_module_parse() { - char *modulecfg = NULL; - char *tmp_config_path = NULL; - char *cfg_buf = NULL; - int ret = NSTACK_RETURN_FAIL; + char *modulecfg = NULL; + char *tmp_config_path = NULL; + char *cfg_buf = NULL; + int ret = NSTACK_RETURN_FAIL; - modulecfg = getenv (NSTACK_MOD_CFG_FILE); + modulecfg = getenv(NSTACK_MOD_CFG_FILE); - if (modulecfg) + if (modulecfg) { - tmp_config_path = realpath (modulecfg, NULL); + tmp_config_path = realpath(modulecfg, NULL); } - else + else { - tmp_config_path = realpath (DEFALT_MODULE_CFG_FILE, NULL); + tmp_config_path = realpath(DEFALT_MODULE_CFG_FILE, NULL); } - if (!tmp_config_path) + if (!tmp_config_path) { - NSSOC_LOGERR ("nstack module file:%s get real path fail!", - modulecfg ? modulecfg : DEFALT_MODULE_CFG_FILE); - return NSTACK_RETURN_FAIL; + NSSOC_LOGWAR + ("nstack module file:%s get real path failed! Load default instead.", + modulecfg ? modulecfg : DEFALT_MODULE_CFG_FILE); + return load_default_module(); } - ret = nstack_json_file_read (tmp_config_path, &cfg_buf); - if (NSTACK_RETURN_OK == ret) + ret = read_json_file(tmp_config_path, &cfg_buf); + if (NSTACK_RETURN_OK == ret) { - ret = nstack_parse_module_cfg_json (cfg_buf); - free (cfg_buf); + ret = parse_module_cfg(cfg_buf); + free(cfg_buf); } - free (tmp_config_path); - return ret; + free(tmp_config_path); + return ret; } -int -nstack_stack_rd_parse (rd_route_data ** data, int *num) +int nstack_rd_parse(const char *name, void *table) { - char *modulecfg = NULL; - char *tmp_config_path = NULL; - char *cfg_buf = NULL; - int ret = NSTACK_RETURN_FAIL; + char *modulecfg = NULL; + char *tmp_config_path = NULL; + char *cfg_buf = NULL; + int ret = NSTACK_RETURN_FAIL; - modulecfg = getenv (NSTACK_MOD_CFG_RD); + modulecfg = getenv(NSTACK_MOD_CFG_RD); - if (modulecfg) + if (modulecfg) { - tmp_config_path = realpath (modulecfg, NULL); + tmp_config_path = realpath(modulecfg, NULL); } - else + else { - tmp_config_path = realpath (DEFALT_RD_CFG_FILE, NULL); + tmp_config_path = realpath(DEFALT_RD_CFG_FILE, NULL); } - if (!tmp_config_path) + if (!tmp_config_path) { - NSSOC_LOGERR ("nstack rd file:%s get real path fail!", - modulecfg ? modulecfg : DEFALT_MODULE_CFG_FILE); - return NSTACK_RETURN_FAIL; + NSSOC_LOGWAR("nstack rd file:%s get real path failed!", + modulecfg ? modulecfg : DEFALT_MODULE_CFG_FILE); + return NSTACK_RETURN_FAIL; } - ret = nstack_json_file_read (tmp_config_path, &cfg_buf); - if (NSTACK_RETURN_OK == ret) + ret = read_json_file(tmp_config_path, &cfg_buf); + if (NSTACK_RETURN_OK == ret) { - ret = nstack_parse_rd_cfg_json (cfg_buf, data, num); - free (cfg_buf); + ret = parse_rd_cfg(cfg_buf, name, table); + free(cfg_buf); } - free (tmp_config_path); - return ret; + free(tmp_config_path); + return ret; } |