From c2aef839594edd4cef580b105d510c2ad09bf6a5 Mon Sep 17 00:00:00 2001 From: Amin Ben Ramdhane Date: Tue, 21 Jan 2020 22:53:06 +0100 Subject: [PATCH] link icwmpd to libbbfdm library (data model) after separating libbbfdm into two libraries: libbbf_api (API) and libbbfdm (data model) --- bulkdata/common.c | 27 ++++++++++++- bulkdata/common.h | 4 +- bulkdata/report.c | 2 +- config.c | 17 +++++++++ config/cwmp | 2 +- config/cwmp_xmpp | 2 +- cwmp.c | 71 ++++++++++++++++++++++++++-------- diagnostic.c | 97 +++++++++++++++++++++++++++++++++++++++++++---- event.c | 46 ++++++++++++++++------ external.c | 11 ++---- http.c | 4 ++ inc/cwmp.h | 6 ++- ubus.c | 21 +++++++--- xml.c | 35 ++++++++++++++++- 14 files changed, 290 insertions(+), 55 deletions(-) diff --git a/bulkdata/common.c b/bulkdata/common.c index 8b40b7a..b8983c5 100644 --- a/bulkdata/common.c +++ b/bulkdata/common.c @@ -38,6 +38,31 @@ int bulkdata_dm_ctx_clean(struct dmctx *ctx) return 0; } +static char **str_split(const char* str, const char* delim, size_t* numtokens) +{ + char *s = strdup(str); + size_t tokens_alloc = 1; + size_t tokens_used = 0; + char **tokens = calloc(tokens_alloc, sizeof(char*)); + char *token, *strtok_ctx; + for (token = strtok_r(s, delim, &strtok_ctx); token != NULL; token = strtok_r(NULL, delim, &strtok_ctx)) { + if (tokens_used == tokens_alloc) { + tokens_alloc *= 2; + tokens = realloc(tokens, tokens_alloc * sizeof(char*)); + } + tokens[tokens_used++] = strdup(token); + } + // cleanup + if (tokens_used == 0) { + FREE(tokens); + } else { + tokens = realloc(tokens, tokens_used * sizeof(char*)); + } + *numtokens = tokens_used; + FREE(s); + return tokens; +} + static bool match(const char *string, const char *pattern) { regex_t re; @@ -333,7 +358,7 @@ char *get_bulkdata_profile_parameter_name(char *paramref, char *paramname, char if(paramname == NULL || strlen(paramname) <= 0) return strdup(param); - paramarr = strsplit(paramref, "*", &length); + paramarr = str_split(paramref, "*", &length); res = strdup(paramname); for(i = 0; i < length; i++) { if(i == length - 1) diff --git a/bulkdata/common.h b/bulkdata/common.h index 7865b73..fc0979c 100644 --- a/bulkdata/common.h +++ b/bulkdata/common.h @@ -23,10 +23,8 @@ #include #include -#include -#include #include -#include +#include #include "config.h" #include "log.h" diff --git a/bulkdata/report.c b/bulkdata/report.c index c26b8e4..f133c75 100644 --- a/bulkdata/report.c +++ b/bulkdata/report.c @@ -57,7 +57,7 @@ static void create_json_bulkdata_report_object_hierarchy(struct profile *profile strcpy(buf, param_name); for (pch = strtok_r(buf, ".", &pchr); pch != NULL; pch = strtok_r(NULL, ".", &pchr)) { argv[j] = pch; - json_obj1 = dmjson_select_obj(json_obj, argv); + json_obj1 = (json_object *)bbfdmjson_select_obj(json_obj, argv); if (json_obj1) json_obj2 = json_obj1; else { diff --git a/config.c b/config.c index b9086db..59e45ba 100644 --- a/config.c +++ b/config.c @@ -29,6 +29,7 @@ #include #else #include +#include #include #endif #include "config.h" @@ -1061,16 +1062,32 @@ int global_env_init (int argc, char** argv, struct env *env) case 'u': upnpuser = optarg; if (strcmp(upnpuser, "public") == 0) { +#ifdef TR098 upnp_in_user_mask = DM_PUBLIC_MASK; +#else + set_upnp_in_user_mask(DM_PUBLIC_MASK); +#endif } else if (strcmp(upnpuser, "basic") == 0) { +#ifdef TR098 upnp_in_user_mask = DM_BASIC_MASK; +#else + set_upnp_in_user_mask(DM_BASIC_MASK); +#endif } else if (strcmp(upnpuser, "xxxadmin") == 0) { +#ifdef TR098 upnp_in_user_mask = DM_XXXADMIN_MASK; +#else + set_upnp_in_user_mask(DM_XXXADMIN_MASK); +#endif } else if (strcmp(upnpuser, "superadmin") == 0) { +#ifdef TR098 upnp_in_user_mask = DM_SUPERADMIN_MASK; +#else + set_upnp_in_user_mask(DM_SUPERADMIN_MASK); +#endif } break; diff --git a/config/cwmp b/config/cwmp index 0020a64..da7923a 100644 --- a/config/cwmp +++ b/config/cwmp @@ -45,4 +45,4 @@ config cwmp 'cpe' config cwmp 'lwn' option enable '1' option hostname '' - option port '' \ No newline at end of file + option port '' diff --git a/config/cwmp_xmpp b/config/cwmp_xmpp index 96642b0..b39ca91 100644 --- a/config/cwmp_xmpp +++ b/config/cwmp_xmpp @@ -1,4 +1,4 @@ - config cwmp 'xmpp' +config cwmp 'xmpp' option enable '0' option id '0' option allowed_jid '' diff --git a/cwmp.c b/cwmp.c index 73dab37..315ddf0 100644 --- a/cwmp.c +++ b/cwmp.c @@ -25,15 +25,6 @@ #include "ubus.h" #include "diagnostic.h" #include "config.h" -#include -#ifdef TR098 -#include -#include -#else -#include -#include -#include -#endif struct cwmp cwmp_main = {0}; @@ -509,9 +500,17 @@ struct session *cwmp_add_queue_session (struct cwmp *cwmp) int run_session_end_func (struct session *session) { - +#ifndef TR098 + bbf_apply_end_session(); +#else apply_end_session(); +#endif + +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_EXTERNAL_ACTION)) +#else if (end_session_flag & END_SESSION_EXTERNAL_ACTION) +#endif { CWMP_LOG (INFO,"Executing external commands: end session request"); external_init(); @@ -519,7 +518,11 @@ int run_session_end_func (struct session *session) external_exit(); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_FACTORY_RESET)) +#else if (end_session_flag & END_SESSION_FACTORY_RESET) +#endif { CWMP_LOG (INFO,"Executing factory reset: end session request"); external_init(); @@ -528,25 +531,34 @@ int run_session_end_func (struct session *session) exit(EXIT_SUCCESS); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_IPPING_DIAGNOSTIC)) +#else if (end_session_flag & END_SESSION_IPPING_DIAGNOSTIC) +#endif { CWMP_LOG (INFO,"Executing ippingdiagnostic: end session request"); cwmp_ip_ping_diagnostic(); } #ifndef TR098 - if (end_session_flag & END_SESSION_DOWNLOAD_DIAGNOSTIC) + if (set_bbf_end_session_flag(END_SESSION_DOWNLOAD_DIAGNOSTIC)) { CWMP_LOG (INFO,"Executing download diagnostic: end session request"); cwmp_start_diagnostic(DOWNLOAD_DIAGNOSTIC); } - if (end_session_flag & END_SESSION_UPLOAD_DIAGNOSTIC) + if (set_bbf_end_session_flag(END_SESSION_UPLOAD_DIAGNOSTIC)) { CWMP_LOG (INFO,"Executing upload diagnostic: end session request"); cwmp_start_diagnostic(UPLOAD_DIAGNOSTIC); } #endif + +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_REBOOT)) +#else if (end_session_flag & END_SESSION_REBOOT) +#endif { CWMP_LOG (INFO,"Executing Reboot: end session request"); external_init(); @@ -555,14 +567,21 @@ int run_session_end_func (struct session *session) exit(EXIT_SUCCESS); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_RELOAD)) +#else if (end_session_flag & END_SESSION_RELOAD) +#endif { CWMP_LOG (INFO,"Config reload: end session request"); cwmp_apply_acs_changes(); } - +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_X_FACTORY_RESET_SOFT)) +#else if (end_session_flag & END_SESSION_X_FACTORY_RESET_SOFT) +#endif { CWMP_LOG (INFO,"Executing factory reset soft: end session request"); external_init(); @@ -571,25 +590,41 @@ int run_session_end_func (struct session *session) exit(EXIT_SUCCESS); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_NSLOOKUP_DIAGNOSTIC)) +#else if (end_session_flag & END_SESSION_NSLOOKUP_DIAGNOSTIC) +#endif { CWMP_LOG (INFO,"Executing nslookupdiagnostic: end session request"); cwmp_nslookup_diagnostic(); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_TRACEROUTE_DIAGNOSTIC)) +#else if (end_session_flag & END_SESSION_TRACEROUTE_DIAGNOSTIC) +#endif { CWMP_LOG (INFO,"Executing traceroutediagnostic: end session request"); cwmp_traceroute_diagnostic(); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_UDPECHO_DIAGNOSTIC)) +#else if (end_session_flag & END_SESSION_UDPECHO_DIAGNOSTIC) +#endif { CWMP_LOG (INFO,"Executing udpechodiagnostic: end session request"); cwmp_udp_echo_diagnostic(); } +#ifndef TR098 + if (set_bbf_end_session_flag(END_SESSION_SERVERSELECTION_DIAGNOSTIC)) +#else if (end_session_flag & END_SESSION_SERVERSELECTION_DIAGNOSTIC) +#endif { CWMP_LOG (INFO,"Executing serverselectiondiagnostic: end session request"); cwmp_serverselection_diagnostic(); @@ -597,7 +632,11 @@ int run_session_end_func (struct session *session) dm_entry_restart_services(); +#ifndef TR098 + reset_bbf_end_session_flag(); +#else end_session_flag = 0; +#endif return CWMP_OK; } @@ -684,7 +723,7 @@ int main(int argc, char **argv) struct sigaction act = {0}; #ifndef TR098 - bbfdatamodel_type = BBFDM_CWMP; // To show only CWMP parameters + set_bbfdatamodel_type(BBFDM_CWMP); // To show only CWMP parameters #endif if (error = cwmp_init(argc, argv, cwmp)) @@ -775,6 +814,8 @@ int main(int argc, char **argv) CWMP_LOG(INFO,"EXIT ICWMP"); cwmp_exit(); - free_json_dynamic_arrays(); +#ifndef TR098 + free_dynamic_arrays(); +#endif return CWMP_OK; } diff --git a/diagnostic.c b/diagnostic.c index 5679bbd..3dd65e2 100644 --- a/diagnostic.c +++ b/diagnostic.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "cwmp.h" #include "backupSession.h" #include "xml.h" @@ -29,9 +30,89 @@ #include #else #include -#include #include +#endif +static int icwmpd_cmd(char *cmd, int n, ...) +{ + va_list arg; + int i, pid; + static int dmcmd_pfds[2]; + char *argv[n+2]; + + argv[0] = cmd; + + va_start(arg,n); + for (i=0; i #include #else -#include -#include #include #include -#include #endif + LIST_HEAD(list_value_change); LIST_HEAD(list_lw_value_change); pthread_mutex_t mutex_value_change = PTHREAD_MUTEX_INITIALIZER; @@ -339,6 +337,7 @@ void cwmp_add_notification(void) int len = strlen(buf); if (len) buf[len-1] = '\0'; +#ifdef TR098 dmjson_parse_init(buf); dmjson_get_var("parameter", &jval); parameter = strdup(jval); @@ -347,12 +346,28 @@ void cwmp_add_notification(void) dmjson_get_var("notification", &jval); notification = strdup(jval); dmjson_parse_fini(); +#else + bbfdmjson_parse_init(buf); + bbfdmjson_get_var("parameter", &jval); + parameter = strdup(jval); + bbfdmjson_get_var("value", &jval); + value = strdup(jval); + bbfdmjson_get_var("notification", &jval); + notification = strdup(jval); + bbfdmjson_parse_fini(); +#endif fault = dm_entry_param_method(&dmctx, CMD_GET_VALUE, parameter, NULL, NULL); if (!fault && dmctx.list_parameter.next != &dmctx.list_parameter) { dm_parameter = list_entry(dmctx.list_parameter.next, struct dm_parameter, list); if (strcmp(dm_parameter->data, value) != 0) { +#ifdef TR098 dm_update_file_enabled_notify(parameter, dm_parameter->data); iscopy = copy_temporary_file_to_original_file(DM_ENABLED_NOTIFY, DM_ENABLED_NOTIFY_TEMPORARY); + +#else + bbfdm_update_file_enabled_notify(parameter, dm_parameter->data); + iscopy = dm_copy_temporary_file_to_original_file(DM_ENABLED_NOTIFY, DM_ENABLED_NOTIFY_TEMPORARY); +#endif if(iscopy) remove(DM_ENABLED_NOTIFY_TEMPORARY); if (notification[0] == '1' || notification[0] == '2' || notification[0] == '4' || notification[0] == '6' ) @@ -367,7 +382,12 @@ void cwmp_add_notification(void) } fclose(fp); +#ifdef TR098 list_for_each_entry(p, &list_enabled_lw_notify, list) { +#else + struct list_head bbf_list_enabled_lw_notify = get_bbf_list_enabled_lw_notify(); + list_for_each_entry(p, &bbf_list_enabled_lw_notify, list) { +#endif if (!initiate || i != 0) dm_ctx_init_sub(&dmctx, DM_CWMP, cwmp_main.conf.amd_version, cwmp_main.conf.instance_mode); i++; @@ -377,11 +397,13 @@ void cwmp_add_notification(void) if (!fault && dmctx.list_parameter.next != &dmctx.list_parameter) { dm_parameter = list_entry(dmctx.list_parameter.next, struct dm_parameter, list); if (strcmp(dm_parameter->data, p->value) != 0) { +#ifdef TR098 dm_update_enabled_notify(p, dm_parameter->data); - if (p->notification[0] >= '3' ) - add_lw_list_value_change(p->name, dm_parameter->data, dm_parameter->type); - if (p->notification[0] == '5' || p->notification[0] == '6') - lw_isactive = true; +#else + bbfdm_update_enabled_notify(p, dm_parameter->data); +#endif + if (p->notification[0] >= '3' ) add_lw_list_value_change(p->name, dm_parameter->data, dm_parameter->type); + if (p->notification[0] == '5' || p->notification[0] == '6') lw_isactive = true; } } dm_ctx_clean_sub(&dmctx); @@ -481,7 +503,6 @@ int cwmp_root_cause_event_bootstrap (struct cwmp *cwmp) int error,cmp=0; struct event_container *event_container; struct session *session; - char buf[64] = ""; error = cwmp_load_saved_session(cwmp, &acsurl, ACS); @@ -525,9 +546,12 @@ int cwmp_root_cause_event_bootstrap (struct cwmp *cwmp) pthread_mutex_unlock (&(cwmp->mutex_session_queue)); return CWMP_MEM_ERR; } - sprintf(buf, "%sManagementServer.URL", dmroot); - add_dm_parameter_tolist(&(event_container->head_dm_parameter), - buf, NULL, NULL); +#ifdef TR098 + char buf[64] = "InternetGatewayDevice.ManagementServer.URL"; +#else + char buf[64] = "Device.ManagementServer.URL"; +#endif + add_dm_parameter_tolist(&(event_container->head_dm_parameter), buf, NULL, NULL); cwmp_save_event_container (cwmp,event_container); save_acs_bkp_config(cwmp); cwmp_scheduleInform_remove_all(); diff --git a/external.c b/external.c index c1bc60a..800f2fc 100644 --- a/external.c +++ b/external.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -29,12 +30,6 @@ #include "cwmp.h" #include "xml.h" #include "log.h" -#ifdef TR098 -#include -#else -#include -#endif -#include static int pid; static json_object *json_obj_in; @@ -46,6 +41,8 @@ char *external_MethodVersion = NULL; char *external_MethodUUID = NULL; char *external_MethodENV = NULL; +#define ICWMP_PROMPT "icwmp>" + void external_downloadFaultResp (char *fault_code) { FREE(external_MethodFault); @@ -131,7 +128,7 @@ static void external_read_pipe_input(int (*external_handler)(char *msg)) value = c; } else { if (!value) continue; - if (strcmp(value, DM_PROMPT)==0) { + if (strcmp(value, ICWMP_PROMPT)==0) { FREE(value); break; } diff --git a/http.c b/http.c index d54b289..712161c 100644 --- a/http.c +++ b/http.c @@ -132,7 +132,11 @@ if (cwmp->conf.ipv6_enable) { curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ip); curl_easy_perform(curl); int tmp = inet_pton(AF_INET, ip, buf); +#ifdef TR098 ip_version = (tmp == 1) ? 4 : 6; +#else + bbf_set_ip_version((tmp == 1) ? 4 : 6); +#endif } return 0; } diff --git a/inc/cwmp.h b/inc/cwmp.h index 30c0329..48e9ba0 100644 --- a/inc/cwmp.h +++ b/inc/cwmp.h @@ -15,14 +15,18 @@ #include #include +#include #include #include #include #include #ifdef TR098 +#include #include #else -#include +#include +#include +#include #endif #define MAX_EVENTS 64 diff --git a/ubus.c b/ubus.c index 351b8fd..19dd3f7 100644 --- a/ubus.c +++ b/ubus.c @@ -93,14 +93,22 @@ cwmp_handle_command(struct ubus_context *ctx, struct ubus_object *obj, if (!strcmp("reload_end_session", cmd)) { CWMP_LOG(INFO, "triggered ubus reload_end_session"); +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_RELOAD); +#else cwmp_set_end_session(END_SESSION_RELOAD); +#endif blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "icwmpd config will reload at the end of the session") == -1) return -1; } else if (!strcmp("reload", cmd)) { CWMP_LOG(INFO, "triggered ubus reload"); if (cwmp_main.session_status.last_status == SESSION_RUNNING) { +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_RELOAD); +#else cwmp_set_end_session(END_SESSION_RELOAD); +#endif blobmsg_add_u32(&b, "status", 0); blobmsg_add_string(&b, "info", "Session running, reload at the end of the session"); } @@ -114,13 +122,21 @@ cwmp_handle_command(struct ubus_context *ctx, struct ubus_object *obj, } } else if (!strcmp("reboot_end_session", cmd)) { CWMP_LOG(INFO, "triggered ubus reboot_end_session"); +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_REBOOT); +#else cwmp_set_end_session(END_SESSION_REBOOT); +#endif blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "icwmpd will reboot at the end of the session") == -1) return -1; } else if (!strcmp("action_end_session", cmd)) { CWMP_LOG(INFO, "triggered ubus action_end_session"); +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_EXTERNAL_ACTION); +#else cwmp_set_end_session(END_SESSION_EXTERNAL_ACTION); +#endif blobmsg_add_u32(&b, "status", 0); if (asprintf(&info, "icwmpd will execute the scheduled action commands at the end of the session") == -1) return -1; @@ -283,11 +299,6 @@ cwmp_handle_inform(struct ubus_context *ctx, struct ubus_object *obj, } else { int event_code = cwmp_get_int_event_code(event); - if (event_code == 6) - { - CWMP_LOG(INFO,"Receive Connection Request: success authentication"); - CWMP_LOG(INFO,"Connection Request thread: add connection request event in the queue"); - } pthread_mutex_lock (&(cwmp_main.mutex_session_queue)); cwmp_add_event_container (&cwmp_main, event_code, ""); pthread_mutex_unlock (&(cwmp_main.mutex_session_queue)); diff --git a/xml.c b/xml.c index da2e316..31237d4 100644 --- a/xml.c +++ b/xml.c @@ -30,8 +30,8 @@ #include #else #include +#include #include -#include #include #endif LIST_HEAD(list_download); @@ -793,7 +793,12 @@ int cwmp_rpc_acs_prepare_message_inform (struct cwmp *cwmp, struct session *sess dm_parameter = list_entry(dmctx.list_parameter.next, struct dm_parameter, list); if (xml_prepare_parameters_inform(&dmctx, dm_parameter, parameter_list, &size)) goto error; + +#ifdef TR098 del_list_parameter(dm_parameter); +#else + bbf_del_list_parameter(dm_parameter); +#endif } if (asprintf(&c, "cwmp:ParameterValueStruct[%d]", size) == -1) @@ -1215,7 +1220,11 @@ int cwmp_handle_rpc_cpe_get_parameter_values(struct session *session, struct rpc counter++; +#ifdef TR098 del_list_parameter(dm_parameter); +#else + bbf_del_list_parameter(dm_parameter); +#endif } #ifdef ACS_MULTI b = mxmlFindElement(session->tree_out, session->tree_out, "ParameterList", @@ -1322,7 +1331,11 @@ int cwmp_handle_rpc_cpe_get_parameter_names(struct session *session, struct rpc counter++; +#ifdef TR098 del_list_parameter(dm_parameter); +#else + bbf_del_list_parameter(dm_parameter); +#endif } #ifdef ACS_MULTI @@ -1432,7 +1445,11 @@ int cwmp_handle_rpc_cpe_get_parameter_attributes(struct session *session, struct counter++; +#ifdef TR098 del_list_parameter(dm_parameter); +#else + bbf_del_list_parameter(dm_parameter); +#endif } #ifdef ACS_MULTI b = mxmlFindElement(session->tree_out, session->tree_out, "ParameterList", @@ -1925,7 +1942,11 @@ int cwmp_handle_rpc_cpe_factory_reset(struct session *session, struct rpc *rpc) b = mxmlNewElement(b, "cwmp:FactoryResetResponse"); if (!b) goto fault; +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_FACTORY_RESET); +#else cwmp_set_end_session(END_SESSION_FACTORY_RESET); +#endif return 0; @@ -1951,7 +1972,11 @@ int cwmp_handle_rpc_cpe_x_factory_reset_soft(struct session *session, struct rpc b = mxmlNewElement(b, "cwmp:X_FactoryResetSoftResponse"); if (!b) goto fault; +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_X_FACTORY_RESET_SOFT); +#else cwmp_set_end_session(END_SESSION_X_FACTORY_RESET_SOFT); +#endif return 0; @@ -2083,7 +2108,11 @@ int cwmp_handle_rpc_cpe_reboot(struct session *session, struct rpc *rpc) b = mxmlNewElement(b, "cwmp:RebootResponse"); if (!b) goto fault; +#ifndef TR098 + bbf_cwmp_set_end_session(END_SESSION_REBOOT); +#else cwmp_set_end_session(END_SESSION_REBOOT); +#endif return 0; @@ -4568,7 +4597,11 @@ int cwmp_handle_rpc_cpe_fault(struct session *session, struct rpc *rpc) u = mxmlNewText(u, 0, FAULT_CPE_ARRAY[idx].DESCRIPTION); if (!u) return -1; } +#ifdef TR098 del_list_fault_param(param_fault); +#else + bbf_del_list_fault_param(param_fault); +#endif } }