example: str_ip_mask = "192.168.1.1/24";
int ip_mask_parse(char * str_ip_mask)
{
char *pos = str_ip_mask;
char str_ip[44] = {0};
unsigned int str_ip_len = 0;
char str_mask[8] = {0};
unsigned int str_mask_len = 0;
struct in_addr ipv4_addr;
struct in6_addr v6_addr;
int mask = 0;
int is_ipv6 = 0;
int mask_start= 0;
while('\0' != *pos)
{
switch(*pos)
{
case '/'
mask_start = 1;
break;
case ':'
is_ipv6 = 1;
break;
default:
if(!mask_start)
{
str_ip[str_ip_len++] = *pos;
}
else
{
str_mask[str_mask_len++] = *pos;
}
break;
}
pos++;
}
if(0 == str_ip_len || 0 == str_mask_len)
{
return -1;
}
if(is_ipv6)
{
if(1 != inet_pton(AF_INET6, str_ip, &v6_addr))
{
return -1;
}
}
else
{
if(1 != inet_pton(AF_INET, str_ip, &ipv4_addr))
{
return -1;
}
}
mask = strtoul(str_mask, 0, 0);
return 0;
}
11-15
1365
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)