From bb29521ab0f778db37a9297577df533c73a556bf Mon Sep 17 00:00:00 2001 From: Jehan Monnier Date: Tue, 9 May 2017 16:09:51 +0200 Subject: [PATCH] add default values for nat policy in link with proxy config default values --- coreapi/lpconfig.c | 28 ++++++++++++++--- coreapi/nat_policy.c | 53 +++++++++++++++++++------------- coreapi/private.h | 4 +++ coreapi/proxy.c | 10 ++++++ coreapi/remote_provisioning.c | 13 +------- include/linphone/lpconfig.h | 9 ++---- tester/proxy_config_tester.c | 58 ++++++++++++++++++++++++++++++----- 7 files changed, 124 insertions(+), 51 deletions(-) diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index d83df8740..ba2283f35 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -500,30 +500,50 @@ static char* _linphone_config_xml_convert(LpConfig *lpc, xml2lpc_context *contex return error_msg; } -char* linphone_config_load_from_xml_file(LinphoneConfig *lpc, const char *filename, void* lc, void* ctx) { +char* linphone_config_load_from_xml_file(LinphoneConfig *lpc, const char *filename) { xml2lpc_context *context = NULL; char* path = lp_realpath(filename, NULL); char* error_msg = NULL; if (path) { - context = xml2lpc_context_new(ctx, lc); + context = xml2lpc_context_new(NULL, NULL); error_msg = _linphone_config_xml_convert(lpc, context, xml2lpc_set_xml_file(context, path)); } if (context) xml2lpc_context_destroy(context); return error_msg; } -char* linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer, void* lc, void* ctx) { +static void xml2lpc_callback(void *ctx, xml2lpc_log_level level, const char *fmt, va_list list) { + BctbxLogLevel bctbx_level; + switch(level) { + case XML2LPC_DEBUG: bctbx_level = BCTBX_LOG_DEBUG; break; + case XML2LPC_MESSAGE: bctbx_level = BCTBX_LOG_MESSAGE;break; + case XML2LPC_WARNING: bctbx_level = BCTBX_LOG_WARNING;break; + case XML2LPC_ERROR: bctbx_level = BCTBX_LOG_ERROR;break; + } + bctbx_logv(BCTBX_LOG_DOMAIN, bctbx_level,fmt,list); +} + +char* _linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer) { xml2lpc_context *context = NULL; char* error_msg = NULL; if (buffer != NULL) { - context = xml2lpc_context_new(ctx, lc); + context = xml2lpc_context_new(xml2lpc_callback, NULL); error_msg = _linphone_config_xml_convert(lpc, context, xml2lpc_set_xml_string(context, buffer)); } if (context) xml2lpc_context_destroy(context); return error_msg; } +LinphoneStatus linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer) { + char *status; + if ((status =_linphone_config_load_from_xml_string(lpc,buffer))) { + ms_error("%s",status); + //ms_free(status) + return -1; + } else + return 0; +} void lp_item_set_value(LpItem *item, const char *value){ if (item->value != value) { diff --git a/coreapi/nat_policy.c b/coreapi/nat_policy.c index e5c234275..64199cce0 100644 --- a/coreapi/nat_policy.c +++ b/coreapi/nat_policy.c @@ -267,40 +267,51 @@ LinphoneNatPolicy * linphone_core_create_nat_policy(LinphoneCore *lc) { return linphone_nat_policy_new(lc); } +LinphoneNatPolicy * linphone_config_create_nat_policy_from_section(const LinphoneConfig *config, const char* section) { + const char *config_ref = lp_config_get_string(config, section, "ref", NULL); + const char *server = lp_config_get_string(config, section, "stun_server", NULL); + const char *username = lp_config_get_string(config, section, "stun_server_username", NULL); + bctbx_list_t *l = lp_config_get_string_list(config, section, "protocols", NULL); + LinphoneNatPolicy *policy; + if (config_ref) + policy = _linphone_nat_policy_new_with_ref(NULL, config_ref); + else + policy = linphone_nat_policy_new(NULL); + + if (server != NULL) linphone_nat_policy_set_stun_server(policy, server); + if (username != NULL) linphone_nat_policy_set_stun_server_username(policy, username); + if (l != NULL) { + bool_t upnp_enabled = FALSE; + bctbx_list_t *elem; + for (elem = l; elem != NULL; elem = elem->next) { + const char *value = (const char *)elem->data; + if (strcmp(value, "stun") == 0) linphone_nat_policy_enable_stun(policy, TRUE); + else if (strcmp(value, "turn") == 0) linphone_nat_policy_enable_turn(policy, TRUE); + else if (strcmp(value, "ice") == 0) linphone_nat_policy_enable_ice(policy, TRUE); + else if (strcmp(value, "upnp") == 0) upnp_enabled = TRUE; + } + if (upnp_enabled) linphone_nat_policy_enable_upnp(policy, TRUE); + } + return policy; +} LinphoneNatPolicy * linphone_core_create_nat_policy_from_config(LinphoneCore *lc, const char *ref) { LpConfig *config = lc->config; LinphoneNatPolicy *policy = NULL; char *section; int index; bool_t finished = FALSE; - + for (index = 0; finished != TRUE; index++) { section = belle_sip_strdup_printf("nat_policy_%i", index); if (lp_config_has_section(config, section)) { const char *config_ref = lp_config_get_string(config, section, "ref", NULL); if ((config_ref != NULL) && (strcmp(config_ref, ref) == 0)) { - const char *server = lp_config_get_string(config, section, "stun_server", NULL); - const char *username = lp_config_get_string(config, section, "stun_server_username", NULL); - bctbx_list_t *l = lp_config_get_string_list(config, section, "protocols", NULL); - policy = _linphone_nat_policy_new_with_ref(lc, ref); - if (server != NULL) linphone_nat_policy_set_stun_server(policy, server); - if (username != NULL) linphone_nat_policy_set_stun_server_username(policy, username); - if (l != NULL) { - bool_t upnp_enabled = FALSE; - bctbx_list_t *elem; - for (elem = l; elem != NULL; elem = elem->next) { - const char *value = (const char *)elem->data; - if (strcmp(value, "stun") == 0) linphone_nat_policy_enable_stun(policy, TRUE); - else if (strcmp(value, "turn") == 0) linphone_nat_policy_enable_turn(policy, TRUE); - else if (strcmp(value, "ice") == 0) linphone_nat_policy_enable_ice(policy, TRUE); - else if (strcmp(value, "upnp") == 0) upnp_enabled = TRUE; - } - if (upnp_enabled) linphone_nat_policy_enable_upnp(policy, TRUE); - } + policy = linphone_config_create_nat_policy_from_section(config, section); + policy->lc = lc; finished = TRUE; } } else finished = TRUE; - belle_sip_free(section); + belle_sip_free(section); } - return policy; +return policy; } diff --git a/coreapi/private.h b/coreapi/private.h index cdc11ef43..a0fbbf550 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -2029,6 +2029,10 @@ LinphoneVideoDefinition * linphone_video_definition_new(unsigned int width, unsi LinphoneVideoDefinition * linphone_factory_find_supported_video_definition(const LinphoneFactory *factory, unsigned int width, unsigned int height); LinphoneVideoDefinition * linphone_factory_find_supported_video_definition_by_name(const LinphoneFactory *factory, const char *name); +char* _linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer); +LinphoneNatPolicy * linphone_config_create_nat_policy_from_section(const LinphoneConfig *config, const char* section); + + #ifdef __cplusplus } #endif diff --git a/coreapi/proxy.c b/coreapi/proxy.c index b4e1a545f..03d07e395 100644 --- a/coreapi/proxy.c +++ b/coreapi/proxy.c @@ -107,6 +107,7 @@ static void linphone_proxy_config_init(LinphoneCore* lc, LinphoneProxyConfig *cf const char *contact_params = lc ? lp_config_get_default_string(lc->config, "proxy", "contact_parameters", NULL) : NULL; const char *contact_uri_params = lc ? lp_config_get_default_string(lc->config, "proxy", "contact_uri_parameters", NULL) : NULL; const char *refkey = lc ? lp_config_get_default_string(lc->config, "proxy", "refkey", NULL) : NULL; + const char *nat_policy_ref = lc ? lp_config_get_default_string(lc->config, "proxy", "nat_policy_ref", NULL):NULL; cfg->lc = lc; cfg->expires = lc ? lp_config_get_default_int(lc->config, "proxy", "reg_expires", 3600) : 3600; cfg->reg_sendregister = lc ? lp_config_get_default_int(lc->config, "proxy", "reg_sendregister", 1) : 1; @@ -127,6 +128,15 @@ static void linphone_proxy_config_init(LinphoneCore* lc, LinphoneProxyConfig *cf cfg->avpf_rr_interval = lc ? lp_config_get_default_int(lc->config, "proxy", "avpf_rr_interval", 5) : 5; cfg->publish_expires= lc ? lp_config_get_default_int(lc->config, "proxy", "publish_expires", -1) : -1; cfg->refkey = refkey ? ms_strdup(refkey) : NULL; + if (nat_policy_ref) { + LinphoneNatPolicy *policy = linphone_config_create_nat_policy_from_section(lc->config,nat_policy_ref); + linphone_proxy_config_set_nat_policy(cfg, policy); + if (policy) { + linphone_nat_policy_unref(policy); + } else { + ms_error("Cannot create default nat policy with ref [%s] for proxy config [%p]",nat_policy_ref,cfg); + } + } } LinphoneProxyConfig *linphone_proxy_config_new() { diff --git a/coreapi/remote_provisioning.c b/coreapi/remote_provisioning.c index 213613803..44b8dd43d 100644 --- a/coreapi/remote_provisioning.c +++ b/coreapi/remote_provisioning.c @@ -22,20 +22,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define XML2LPC_CALLBACK_BUFFER_SIZE 1024 -static void xml2lpc_callback(void *ctx, xml2lpc_log_level level, const char *fmt, va_list list) { - char buffer[XML2LPC_CALLBACK_BUFFER_SIZE]; - vsnprintf(buffer, XML2LPC_CALLBACK_BUFFER_SIZE, fmt, list); - - if (level == XML2LPC_ERROR) - ms_error("%s", buffer); - else if (level == XML2LPC_WARNING) - ms_warning("%s", buffer); - /*else - ms_message("%s", buffer); // Don't log debug messages */ -} static void linphone_remote_provisioning_apply(LinphoneCore *lc, const char *xml) { - char* error_msg = linphone_config_load_from_xml_string(linphone_core_get_config(lc), xml, lc, xml2lpc_callback); + char* error_msg = _linphone_config_load_from_xml_string(linphone_core_get_config(lc), xml); linphone_configuring_terminated(lc ,error_msg ? LinphoneConfiguringFailed : LinphoneConfiguringSuccessful diff --git a/include/linphone/lpconfig.h b/include/linphone/lpconfig.h index 518045bfb..ae676e539 100644 --- a/include/linphone/lpconfig.h +++ b/include/linphone/lpconfig.h @@ -87,20 +87,17 @@ LINPHONE_PUBLIC LinphoneStatus linphone_config_read_file(LinphoneConfig *lpconfi * @ingroup misc * @param lpconfig The LinphoneConfig object to fill with the content of the file * @param filename The filename of the config file to read to fill the LinphoneConfig - * @param lc LinphoneCore to share with xml2lpc - * @param ctx The context given to xml2lpc callback */ -LINPHONE_PUBLIC char* linphone_config_load_from_xml_file(LinphoneConfig *lpc, const char *filename, void* lc, void* ctx); +LINPHONE_PUBLIC char* linphone_config_load_from_xml_file(LinphoneConfig *lpc, const char *filename); /** * Reads a xml config string and fill the LinphoneConfig with the read config dynamic values. * @ingroup misc * @param lpconfig The LinphoneConfig object to fill with the content of the file * @param buffer The string of the config file to fill the LinphoneConfig - * @param lc LinphoneCore to share with xml2lpc - * @param ctx The context given to xml2lpc callback + * @return 0 in case of success */ -LINPHONE_PUBLIC char* linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer, void* lc, void* ctx); +LINPHONE_PUBLIC LinphoneStatus linphone_config_load_from_xml_string(LpConfig *lpc, const char *buffer); /** * Retrieves a configuration item as a string, given its section, key, and default value. diff --git a/tester/proxy_config_tester.c b/tester/proxy_config_tester.c index fe71778c6..b137df936 100644 --- a/tester/proxy_config_tester.c +++ b/tester/proxy_config_tester.c @@ -166,7 +166,7 @@ static void phone_normalization_with_dial_escape_plus(void){ BC_ASSERT_STRING_EQUAL(actual_str, expected); \ ms_free(actual_str); \ linphone_address_unref(res); \ - linphone_proxy_config_destroy(proxy); \ + linphone_proxy_config_unref(proxy); \ } @@ -179,20 +179,62 @@ static void sip_uri_normalization(void) { SIP_URI_CHECK("١", expected); //test that no more invalid memory writes are made (valgrind only) } -/*static void load_dynamic_proxy_config(void) { +static void load_dynamic_proxy_config(void) { + LinphoneCoreManager *lauriane = linphone_core_manager_new(NULL); LinphoneProxyConfig *proxy; - - //Load file - - proxy = linphone_proxy_config_new(); -}*/ + LinphoneAddress *read, *expected; + LinphoneNatPolicy *nat_policy; + const char* config = "\r\n" + "\r\n" + "
\r\n" + "1\r\n" + "0\r\n" + "0\r\n" + "sip:voip-metrics@sip.linphone.org;transport=tls\r\n" + "1\r\n" + "180\r\n" + "31536000\r\n" + "sip:?@sip.linphone.org\r\n" + "<sip:sip.linphone.org;transport=tls>\r\n" + "1\r\n" + "nat_policy_default_values\r\n" + "sip.linphone.org\r\n" + "
\r\n" + "
\r\n" + "stun.linphone.org\r\n" + "stun,ice\r\n" + "
\r\n" + "
"; + BC_ASSERT_FALSE(linphone_config_load_from_xml_string(linphone_core_get_config(lauriane->lc),config)); + proxy = linphone_core_create_proxy_config(lauriane->lc); + + read = linphone_address_new(linphone_proxy_config_get_server_addr(proxy)); + expected = linphone_address_new("sip:sip.linphone.org;transport=tls"); + + BC_ASSERT_TRUE(linphone_address_equal(read,expected)); + linphone_address_unref(read); + linphone_address_unref(expected); + + nat_policy = linphone_proxy_config_get_nat_policy(proxy); + + if (BC_ASSERT_PTR_NOT_NULL(nat_policy)) { + BC_ASSERT_TRUE(linphone_nat_policy_ice_enabled(nat_policy)); + BC_ASSERT_TRUE(linphone_nat_policy_stun_enabled(nat_policy)); + BC_ASSERT_FALSE(linphone_nat_policy_turn_enabled(nat_policy)); + } + linphone_proxy_config_unref(proxy); + linphone_core_manager_destroy(lauriane); + + //BC_ASSERT_STRING_EQUAL(linphone_proxy_config_get(proxy), "sip:sip.linphone.org;transport=tls"); + +} test_t proxy_config_tests[] = { TEST_NO_TAG("Phone normalization without proxy", phone_normalization_without_proxy), TEST_NO_TAG("Phone normalization with proxy", phone_normalization_with_proxy), TEST_NO_TAG("Phone normalization with dial escape plus", phone_normalization_with_dial_escape_plus), TEST_NO_TAG("SIP URI normalization", sip_uri_normalization), - //TEST_NO_TAG("Load new default value for proxy config", load_dynamic_proxy_config) + TEST_NO_TAG("Load new default value for proxy config", load_dynamic_proxy_config) }; test_suite_t proxy_config_test_suite = {"Proxy config", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,