From f25a86887fbe7a1c28168f69c7659a774be4f78e Mon Sep 17 00:00:00 2001 From: Zhijie Gui Date: Tue, 3 Feb 2015 15:03:15 +0800 Subject: [PATCH] Get/Set the proxy information CAPI set proxy inteface is divided into type and address functions,therefore, save last set proxy type for avoid failure to set manual proxy address. Change-Id: Id9c464f2f06365260635b6f0a4a67e41d4fc75ce Signed-off-by: Zhijie Gui --- include/net_connection_private.h | 3 + src/connection_profile.c | 185 +++++++++++++++++++++---------- src/libnetwork.c | 94 ++++++++-------- test/connection_test.c | 5 + 4 files changed, 181 insertions(+), 106 deletions(-) diff --git a/include/net_connection_private.h b/include/net_connection_private.h index 1b14840..5a067da 100644 --- a/include/net_connection_private.h +++ b/include/net_connection_private.h @@ -117,6 +117,9 @@ void _connection_cb_proxy_change_cb(char *proxy, void *user_data); void _connection_inter_mutex_lock(void); void _connection_inter_mutex_unlock(void); +net_proxy_type_t _connection_libnet_proxy_type_string2type(const char *str); +int _connection_libnet_get_proxy_address(struct connman_service *service, + char **proxy_address); #ifdef __cplusplus } diff --git a/src/connection_profile.c b/src/connection_profile.c index 6c1d687..23531ea 100644 --- a/src/connection_profile.c +++ b/src/connection_profile.c @@ -24,6 +24,9 @@ #define HTTP_PROXY "http_proxy" +/*save last set type, only for manual */ +static net_proxy_type_t last_set_proxy_type; + /* static net_dev_info_t* __profile_get_net_info(net_profile_info_t *profile_info) { @@ -694,24 +697,19 @@ EXPORT_API int connection_profile_get_proxy_type(connection_profile_h profile, c return CONNECTION_ERROR_INVALID_PARAMETER; } - /* - const char *proxy; - net_profile_info_t *profile_info = profile; - net_dev_info_t *net_info = __profile_get_net_info(profile_info); - if (net_info == NULL) - return CONNECTION_ERROR_OPERATION_FAILED; - - if (profile_info->profile_type == NET_DEVICE_ETHERNET) { - proxy = __profile_get_ethernet_proxy(); - if (proxy == NULL) - *type = CONNECTION_PROXY_TYPE_DIRECT; - else - *type = CONNECTION_PROXY_TYPE_MANUAL; + const struct service_proxy *proxy; + net_proxy_type_t proxy_type; + struct connman_service *service = + _connection_libnet_get_service_h(profile); + if (service == NULL) + return CONNECTION_ERROR_INVALID_PARAMETER; - return CONNECTION_ERROR_NONE; - } + proxy = connman_service_get_proxy_info(service); + if (proxy == NULL || proxy->method == NULL) + return CONNECTION_ERROR_OPERATION_FAILED; - switch (net_info->ProxyMethod) { + proxy_type = _connection_libnet_proxy_type_string2type(proxy->method); + switch (proxy_type) { case NET_PROXY_TYPE_DIRECT: *type = CONNECTION_PROXY_TYPE_DIRECT; break; @@ -725,7 +723,6 @@ EXPORT_API int connection_profile_get_proxy_type(connection_profile_h profile, c default: return CONNECTION_ERROR_OPERATION_FAILED; } - */ return CONNECTION_ERROR_NONE; } @@ -741,30 +738,15 @@ EXPORT_API int connection_profile_get_proxy_address(connection_profile_h profile return CONNECTION_ERROR_INVALID_PARAMETER; } - /* - const char *proxy; - net_profile_info_t *profile_info = profile; - net_dev_info_t *net_info = __profile_get_net_info(profile_info); - if (net_info == NULL) - return CONNECTION_ERROR_OPERATION_FAILED; - if (address_family == CONNECTION_ADDRESS_FAMILY_IPV6) return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED; - if (profile_info->profile_type == NET_DEVICE_ETHERNET) { - proxy = __profile_get_ethernet_proxy(); - if (proxy == NULL) - return CONNECTION_ERROR_OPERATION_FAILED; - - *proxy_address = g_strdup(proxy); - } else - *proxy_address = g_strdup(net_info->ProxyAddr); - - if (*proxy_address == NULL) - return CONNECTION_ERROR_OUT_OF_MEMORY; - */ + struct connman_service *service = + _connection_libnet_get_service_h(profile); + if (service == NULL) + return CONNECTION_ERROR_INVALID_PARAMETER; - return CONNECTION_ERROR_NONE; + return _connection_libnet_get_proxy_address(service, proxy_address); } EXPORT_API int connection_profile_set_ip_config_type(connection_profile_h profile, @@ -1108,28 +1090,60 @@ EXPORT_API int connection_profile_set_proxy_type(connection_profile_h profile, c return CONNECTION_ERROR_INVALID_PARAMETER; } - /* - net_profile_info_t *profile_info = profile; - net_dev_info_t *net_info = __profile_get_net_info(profile_info); - if (net_info == NULL) + connection_profile_type_e profile_type; + if (connection_profile_get_type(profile, &profile_type) + != CONNECTION_ERROR_NONE) { + CONNECTION_LOG(CONNECTION_ERROR, "Fail to get profile type"); return CONNECTION_ERROR_OPERATION_FAILED; + } - switch (type) { - case CONNECTION_PROXY_TYPE_DIRECT: - net_info->ProxyMethod = NET_PROXY_TYPE_DIRECT; + switch (profile_type) { + case CONNECTION_PROFILE_TYPE_CELLULAR: + CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n"); + return CONNECTION_ERROR_OPERATION_FAILED; + case CONNECTION_PROFILE_TYPE_WIFI: + break; + case CONNECTION_PROFILE_TYPE_ETHERNET: break; + case CONNECTION_PROFILE_TYPE_BT: + CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n"); + return CONNECTION_ERROR_OPERATION_FAILED; + default: + return CONNECTION_ERROR_OPERATION_FAILED; + } + + enum connman_lib_err err = CONNMAN_LIB_ERR_NONE; + struct service_proxy proxy_config; + struct connman_service *service = + _connection_libnet_get_service_h(profile); + if (service == NULL) + return CONNECTION_ERROR_INVALID_PARAMETER; + + if (type == CONNECTION_PROXY_TYPE_MANUAL) { + last_set_proxy_type = NET_PROXY_TYPE_MANUAL; + return CONNECTION_ERROR_NONE; + } else + last_set_proxy_type = NET_PROXY_TYPE_UNKNOWN; + + + memset(&proxy_config, 0, sizeof(struct service_proxy)); + + switch (type) { case CONNECTION_PROXY_TYPE_AUTO: - net_info->ProxyMethod = NET_PROXY_TYPE_AUTO; + proxy_config.method = "auto"; break; - case CONNECTION_PROXY_TYPE_MANUAL: - net_info->ProxyMethod = NET_PROXY_TYPE_MANUAL; + case CONNECTION_PROXY_TYPE_DIRECT: + proxy_config.method = "direct"; break; default: return CONNECTION_ERROR_INVALID_PARAMETER; } - */ - return CONNECTION_ERROR_NONE; + err = connman_service_set_proxy_config(service, &proxy_config); + if (err != CONNMAN_LIB_ERR_NONE) + return CONNECTION_ERROR_OPERATION_FAILED; + else + return CONNECTION_ERROR_NONE; } EXPORT_API int connection_profile_set_proxy_address(connection_profile_h profile, @@ -1142,20 +1156,71 @@ EXPORT_API int connection_profile_set_proxy_address(connection_profile_h profile return CONNECTION_ERROR_INVALID_PARAMETER; } - /* - net_profile_info_t *profile_info = profile; - net_dev_info_t *net_info = __profile_get_net_info(profile_info); - if (net_info == NULL) - return CONNECTION_ERROR_OPERATION_FAILED; - if (address_family == CONNECTION_ADDRESS_FAMILY_IPV6) return CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED; - if (proxy_address == NULL) - net_info->ProxyAddr[0] = '\0'; - else - g_strlcpy(net_info->ProxyAddr, proxy_address, NET_PROXY_LEN_MAX); - */ + connection_profile_type_e profile_type; + if (connection_profile_get_type(profile, &profile_type) + != CONNECTION_ERROR_NONE) { + CONNECTION_LOG(CONNECTION_ERROR, "Fail to get profile type"); + return CONNECTION_ERROR_OPERATION_FAILED; + } + + switch (profile_type) { + case CONNECTION_PROFILE_TYPE_CELLULAR: + CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n"); + return CONNECTION_ERROR_OPERATION_FAILED; + case CONNECTION_PROFILE_TYPE_WIFI: + break; + case CONNECTION_PROFILE_TYPE_ETHERNET: + break; + case CONNECTION_PROFILE_TYPE_BT: + CONNECTION_LOG(CONNECTION_ERROR, "Not supported yet\n"); + return CONNECTION_ERROR_OPERATION_FAILED; + default: + return CONNECTION_ERROR_OPERATION_FAILED; + } + + enum connman_lib_err err = CONNMAN_LIB_ERR_NONE; + struct service_proxy proxy_config; + const struct service_proxy *proxy; + net_proxy_type_t proxy_type; + struct connman_service *service = + _connection_libnet_get_service_h(profile); + if (service == NULL) + return CONNECTION_ERROR_INVALID_PARAMETER; + + if (last_set_proxy_type != NET_PROXY_TYPE_MANUAL) { + proxy = connman_service_get_proxy_config(service); + if (proxy == NULL || proxy->method == NULL) + return CONNECTION_ERROR_OPERATION_FAILED; + memset(&proxy_config, 0, sizeof(struct service_proxy)); + proxy_type = _connection_libnet_proxy_type_string2type( + proxy->method); + } else + proxy_type = NET_PROXY_TYPE_MANUAL; + + switch (proxy_type) { + case NET_PROXY_TYPE_AUTO: + proxy_config.method = "auto"; + proxy_config.url = g_strdup(proxy_address); + err = connman_service_set_proxy_config(service, &proxy_config); + g_free(proxy_config.url); + break; + case NET_PROXY_TYPE_MANUAL: + proxy_config.method = "manual"; + proxy_config.servers = g_try_malloc0(sizeof(char *)); + *proxy_config.servers = g_strdup(proxy_address); + err = connman_service_set_proxy_config(service, &proxy_config); + g_free(*proxy_config.servers); + g_free(proxy_config.servers); + break; + default: + return CONNECTION_ERROR_OPERATION_FAILED; + } + + if (err != CONNMAN_LIB_ERR_NONE) + return CONNECTION_ERROR_OPERATION_FAILED; return CONNECTION_ERROR_NONE; } diff --git a/src/libnetwork.c b/src/libnetwork.c index c81f969..88924a7 100755 --- a/src/libnetwork.c +++ b/src/libnetwork.c @@ -245,21 +245,6 @@ static void __libnet_clear_profile_list(struct _profile_list_s *profile_list) profile_list->profiles = NULL; } -static net_proxy_type_t __libnet_proxy_type_string2type(const char *str) -{ - if (str == NULL) - return NET_PROXY_TYPE_UNKNOWN; - - if (g_strcmp0(str, "direct") == 0) - return NET_PROXY_TYPE_DIRECT; - if (g_strcmp0(str, "manual") == 0) - return NET_PROXY_TYPE_MANUAL; - if (g_strcmp0(str, "auto") == 0) - return NET_PROXY_TYPE_AUTO; - - return NET_PROXY_TYPE_UNKNOWN; -} - static net_device_t __libnet_service_type_string2type(const char *str) { if (str == NULL) @@ -427,34 +412,6 @@ static int __libnet_get_ipv4_ip_address(struct connman_service *service, return CONNECTION_ERROR_NONE; } -static int __libnet_get_proxy_address(struct connman_service *service, - char **proxy_address) -{ - const struct service_proxy *proxy; - net_proxy_type_t proxy_type; - - if (service == NULL) - return CONNECTION_ERROR_INVALID_PARAMETER; - - proxy = connman_service_get_proxy_info(service); - if (proxy == NULL || proxy->method == NULL) - return CONNECTION_ERROR_OPERATION_FAILED; - - proxy_type = __libnet_proxy_type_string2type(proxy->method); - - if (proxy_type == NET_PROXY_TYPE_AUTO && proxy->url != NULL) - *proxy_address = g_strdup(proxy->url); - else if (proxy_type == NET_PROXY_TYPE_MANUAL && proxy->servers != NULL) - *proxy_address = g_strdup(proxy->servers[0]); - else - return CONNECTION_ERROR_OPERATION_FAILED; - - if (*proxy_address == NULL) - return CONNECTION_ERROR_OUT_OF_MEMORY; - - return CONNECTION_ERROR_NONE; -} - static void __libnet_update_network_type(net_device_t device_type) { if (last_default_service_property.device_type == device_type) @@ -504,8 +461,8 @@ static void __libnet_update_network_proxy(struct connman_service *service, if (service == NULL) /*Disconnected the network*/ proxy_addr = g_strdup(""); else { - if (__libnet_get_proxy_address(service, &proxy_addr) != - CONNECTION_ERROR_NONE) + if (_connection_libnet_get_proxy_address(service, &proxy_addr) + != CONNECTION_ERROR_NONE) return; } @@ -1331,9 +1288,54 @@ int _connection_libnet_get_default_proxy(char **proxy_address) if (rv == CONNECTION_ERROR_NO_CONNECTION) return CONNECTION_ERROR_NO_CONNECTION; - return __libnet_get_proxy_address(default_service, proxy_address); + return _connection_libnet_get_proxy_address( + default_service, proxy_address); } +net_proxy_type_t _connection_libnet_proxy_type_string2type(const char *str) +{ + if (str == NULL) + return NET_PROXY_TYPE_UNKNOWN; + + if (g_strcmp0(str, "direct") == 0) + return NET_PROXY_TYPE_DIRECT; + if (g_strcmp0(str, "manual") == 0) + return NET_PROXY_TYPE_MANUAL; + if (g_strcmp0(str, "auto") == 0) + return NET_PROXY_TYPE_AUTO; + + return NET_PROXY_TYPE_UNKNOWN; +} + +int _connection_libnet_get_proxy_address(struct connman_service *service, + char **proxy_address) +{ + const struct service_proxy *proxy; + net_proxy_type_t proxy_type; + + if (service == NULL) + return CONNECTION_ERROR_INVALID_PARAMETER; + + proxy = connman_service_get_proxy_info(service); + if (proxy == NULL || proxy->method == NULL) + return CONNECTION_ERROR_OPERATION_FAILED; + + proxy_type = _connection_libnet_proxy_type_string2type(proxy->method); + + if (proxy_type == NET_PROXY_TYPE_AUTO && proxy->url != NULL) + *proxy_address = g_strdup(proxy->url); + else if (proxy_type == NET_PROXY_TYPE_MANUAL && proxy->servers != NULL) + *proxy_address = g_strdup(proxy->servers[0]); + else + return CONNECTION_ERROR_OPERATION_FAILED; + + if (*proxy_address == NULL) + return CONNECTION_ERROR_OUT_OF_MEMORY; + + return CONNECTION_ERROR_NONE; +} + + void _connection_libnet_set_type_changed_cb() { last_default_service_property.device_type = NET_DEVICE_UNKNOWN; diff --git a/test/connection_test.c b/test/connection_test.c index 305ee62..dac3e44 100644 --- a/test/connection_test.c +++ b/test/connection_test.c @@ -440,6 +440,11 @@ static int test_update_network_info(connection_profile_h profile) rv = connection_profile_set_ip_config_type(profile, CONNECTION_ADDRESS_FAMILY_IPV4, CONNECTION_IP_CONFIG_TYPE_DYNAMIC); + if (rv != CONNECTION_ERROR_NONE) + return -1; + + if (test_update_proxy_info(profile) == -1) + return -1; break; case 2: rv = connection_profile_set_ip_config_type(profile, -- 2.34.1