From: Niraj Kumar Goit Date: Tue, 5 Sep 2017 10:32:14 +0000 (+0530) Subject: [connection] Added support to validate netmask address. X-Git-Tag: submit/tizen/20170911.125730^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0ee4867072c1669158582c767e6f556c5c06e79f;p=platform%2Fcore%2Fapi%2Fconnection.git [connection] Added support to validate netmask address. Change-Id: I901cba2c331e19eb11ada4f4ca5d709b3ce3d53d Signed-off-by: Niraj Kumar Goit --- diff --git a/src/connection_profile.c b/src/connection_profile.c index 7f54484..de4c037 100755 --- a/src/connection_profile.c +++ b/src/connection_profile.c @@ -121,13 +121,18 @@ static char* __profile_get_ethernet_proxy(void) } //LCOV_EXCL_STOP -static int __profile_convert_netmask_to_prefix_len(const char *netmask) +static unsigned char __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; + in_addr_t host = ~mask; + unsigned char prefix_len = 0; + + /* a valid netmask must be 2^n - 1 */ + if ((host & (host + 1)) != 0) + return -1; for (; mask; mask <<= 1) ++prefix_len; @@ -970,6 +975,10 @@ EXPORT_API int connection_profile_set_subnet_mask(connection_profile_h profile, return CONNECTION_ERROR_INVALID_PARAMETER; net_info->PrefixLen = __profile_convert_netmask_to_prefix_len(subnet_mask); + if (net_info->PrefixLen <= 0 || net_info->PrefixLen > 31) { + CONNECTION_LOG(CONNECTION_ERROR, "Invalid Prefix length: %d", net_info->PrefixLen); + return CONNECTION_ERROR_INVALID_PARAMETER; + } return CONNECTION_ERROR_NONE; }