Add converting function for netmask
[platform/core/api/connection.git] / 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;
 }