/************************************************************************ * * cidrtest.c - Example program to test if an IPv4 address exists * inside of a network, based on a CIDR value. * * Copyright 2016 Todd Shadburn * * Licensed under the GNU GPL version 2 * ************************************************************************/ #include #include #include #include #include #include #include #include int main (int argc, char**argv) { struct in_addr ipaddr; struct in_addr network; uint32_t netmask; uint32_t cidrbits; uint8_t bit; if (argc < 4) { fprintf(stderr, "Usage: %s network cidrmask ipaddr\n", argv[0]); exit (2); } if (inet_aton(argv[1], &network) == 0) { fprintf(stderr, "Invalid network address.\n"); exit (2); } cidrbits = atoi(argv[2]); if (cidrbits < 1 || cidrbits > 32) { fprintf(stderr, "Invalid cidrmask.\n"); exit (2); } if (inet_aton(argv[3], &ipaddr) == 0) { fprintf(stderr, "Invalid IP address.\n"); exit (2); } netmask = 0L; for (bit=31 ; bit>(31-cidrbits) ; bit--) netmask |= (1<