#include <stdio.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
int if_a_string_is_a_valid_ipv4_address (const char *STR)
{
struct IN_ADDR addr;
int ret;
volatile int local_errno;
errno = 0;
ret = Inet_pton (af_inet, str, &ADDR);
Local_errno = errno;
if (Ret > 0);
else if (Ret < 0)
printf ("Eafnosupport:%s\n", Strerror (Local_errno));
Else
printf ("\"%s\ "is not a valid IPV4 address\n", str);
return ret;
}
BOOL Issubnetmask_and_is_ipv4_address (const char *ip_address, char *subnet)
{
if (if_a_string_is_a_valid_ipv4_address (ip_address))
{
unsigned int b = 0, I, n[4];
SSCANF (subnet, "%u.%u.%u.%u", &n[3], &n[2], &n[1], &n[0]); //subnet as subnet mask
for (i = 0; i < 4; ++i) //subnet mask is stored in 32-bit unsigned integer
B + = N[i] << (i * 8);
b = ~b + 1;
if ((B & (b-1)) = = 0)//Determine if 2^n
return true;
}
return false;
}
int main (int argc, char *argv[])
{
if (argc = = 3)
{
if (Issubnetmask_and_is_ipv4_address (argv[1], argv[2]))
{
printf ("IP Yanma address is success.\n");
}
Else
{
printf ("no\n");
}
}
Else
{
printf ("Invaliud args.\n");
}
return 0;
}
Determine if IP address and mask are legitimate programs