From: Seonah Moon Date: Tue, 21 Mar 2017 05:01:34 +0000 (+0900) Subject: Add converting function for netmask X-Git-Tag: accepted/tizen/common/20170322.153719^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F89%2F119989%2F1;p=platform%2Fcore%2Fapi%2Fconnection.git Add converting function for netmask Change-Id: I4e04daa8e5b2efce8c1fdde40d35857cecabe3e1 Signed-off-by: Seonah Moon --- diff --git a/src/connection_profile.c b/src/connection_profile.c index 39fbc8c..f088a27 100755 --- a/src/connection_profile.c +++ b/src/connection_profile.c @@ -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; }