From 0db66fa60ee85eceb5a605319ba5aa4dac56e5af Mon Sep 17 00:00:00 2001 From: Sachin Singla <sachin.singla@iopsys.eu> Date: Mon, 21 Oct 2019 14:41:12 +0200 Subject: [PATCH] qos: fix ipv4 and ipvr ipaddress and netmask issue --- cli_classcfg.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/cli_classcfg.c b/cli_classcfg.c index df8be8e..073db9a 100755 --- a/cli_classcfg.c +++ b/cli_classcfg.c @@ -441,22 +441,35 @@ void classcfg_parse_ipaddr_ipmask( const char *optstring, const char* optarg, cl ipmask_conv(subStr[1], &mask); inet_ntop(AF_INET, &(mask), pxipmatch->mask, INET_ADDRSTRLEN); } else { - strncpy_s(pxipmatch->mask, sizeof(pxipmatch->mask), - subStr[1], strnlen_s(subStr[1], QCLI_MAX_STR)); + uint32_t m = -1; + uint32_t prefix = atoi(subStr[1]); + struct in_addr in_mask; + m = m << (32 -prefix); + in_mask.s_addr = htonl(m); + inet_ntop(AF_INET, &(in_mask), pxipmatch->mask, INET_ADDRSTRLEN); } } break; case AF_INET6: - ip6addr_conv(subStr[0], &ipaddr6); - inet_ntop(AF_INET6, &(ipaddr6), pxipmatch->ipaddr, INET_ADDRSTRLEN); + ip6addr_conv(subStr[0], &ipaddr6); + inet_ntop(AF_INET6, &(ipaddr6), pxipmatch->ipaddr, INET6_ADDRSTRLEN); if(nSubCnt == 2) { if (strchr(subStr[1], ':') != NULL) { ip6mask_conv(subStr[1], &mask6); - inet_ntop(AF_INET6, &(mask6), pxipmatch->mask, INET_ADDRSTRLEN); + inet_ntop(AF_INET6, &(mask6), pxipmatch->mask, INET6_ADDRSTRLEN); } else { - strncpy_s(pxipmatch->mask, sizeof(pxipmatch->mask), - subStr[1], strnlen_s(subStr[1], QCLI_MAX_STR)); + struct in6_addr in6 = { 0 }; + uint32_t prefix = atoi(subStr[1]); + int i , j ; + for (i = prefix, j = 0; i > 0; i -= 8, j++) { + if (i >= 8) { + in6.s6_addr[j] = 0xff; + } else { + in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i)); + } + } + inet_ntop(AF_INET6, &(in6), pxipmatch->mask, INET6_ADDRSTRLEN); } } break; @@ -466,6 +479,7 @@ void classcfg_parse_ipaddr_ipmask( const char *optstring, const char* optarg, cl printf("***** Invalid ipaddres/Mask %s passed to %s\n", option_string, optstring); exit(0); } + LOGF_LOG_DEBUG("pxipmatch->ipaddr %s pxipmatch->mask %s\n", pxipmatch->ipaddr, pxipmatch->mask); //freeSubStr(subStr, nSubCnt); } -- GitLab