Add converting function for netmask 89/119989/1 accepted/tizen/common/20170322.153719 accepted/tizen/ivi/20170322.235805 accepted/tizen/mobile/20170322.235640 accepted/tizen/tv/20170322.235712 accepted/tizen/unified/20170322.235834 accepted/tizen/wearable/20170322.235739 submit/tizen/20170322.011410
authorSeonah Moon <seonah1.moon@samsung.com>
Tue, 21 Mar 2017 05:01:34 +0000 (14:01 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Tue, 21 Mar 2017 05:01:45 +0000 (14:01 +0900)
Change-Id: I4e04daa8e5b2efce8c1fdde40d35857cecabe3e1
Signed-off-by: Seonah Moon <seonah1.moon@samsung.com>
src/connection_profile.c

index 39fbc8c..f088a27 100755 (executable)
@@ -121,6 +121,25 @@ static const char* __profile_get_ethernet_proxy(void)
 }
 //LCOV_EXCL_STOP
 
+static int __profile_convert_netmask_to_prefix_len(const char *netmask)
+{
+       if (netmask == NULL)
+               return 0;
+
+       in_addr_t mask = inet_network(netmask);
+       int prefix_len = 0;
+
+       for (; mask; mask <<= 1)
+               ++prefix_len;
+
+       return prefix_len;
+}
+
+static in_addr_t __profile_convert_prefix_len_to_netmask(int prefix_len)
+{
+       return (prefix_len ? (in_addr_t) 0xFFFFFFFFu >> (32 - prefix_len) : 0);
+}
+
 //LCOV_EXCL_START
 connection_cellular_service_type_e _profile_convert_to_connection_cellular_service_type(net_service_type_t svc_type)
 {
@@ -889,8 +908,10 @@ EXPORT_API int connection_profile_set_subnet_mask(connection_profile_h profile,
        } else {
                if (subnet_mask == NULL)
                        net_info->SubnetMask.Data.Ipv4.s_addr = 0;
-               else if (inet_pton(AF_INET, subnet_mask , &net_info->SubnetMask.Data.Ipv4)  < 1)
+               else if (inet_pton(AF_INET, subnet_mask , &net_info->SubnetMask.Data.Ipv4) < 1)
                        return CONNECTION_ERROR_INVALID_PARAMETER;
+
+               net_info->PrefixLen = __profile_convert_netmask_to_prefix_len(subnet_mask);
        }
 
        return CONNECTION_ERROR_NONE;
@@ -1931,7 +1952,11 @@ EXPORT_API int connection_profile_set_prefix_length(connection_profile_h profile
        if (net_info == NULL)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
-       net_info->PrefixLen6 = prefix_len;
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4) {
+               net_info->PrefixLen = prefix_len;
+               net_info->SubnetMask.Data.Ipv4.s_addr = __profile_convert_prefix_len_to_netmask(prefix_len);
+       } else
+               net_info->PrefixLen6 = prefix_len;
 
        return CONNECTION_ERROR_NONE;
 }
@@ -1955,7 +1980,10 @@ EXPORT_API int connection_profile_get_prefix_length(connection_profile_h profile
        if (net_info == NULL)
                return CONNECTION_ERROR_OPERATION_FAILED;
 
-       *prefix_len =  net_info->PrefixLen6;
+       if (address_family == CONNECTION_ADDRESS_FAMILY_IPV4)
+               *prefix_len =  net_info->PrefixLen;
+       else if (address_family == CONNECTION_ADDRESS_FAMILY_IPV6)
+               *prefix_len =  net_info->PrefixLen6;
 
        return CONNECTION_ERROR_NONE;
 }