/************************************************************************ * * netmasktest.c - Example program to test if an IPv4 address exists * inside of a network, based on a netmask 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; struct in_addr netmask; if (argc < 4) { fprintf(stderr, "Usage: %s network netmask ipaddr\n", argv[0]); exit (2); } if (inet_aton(argv[1], &network) == 0) { fprintf(stderr, "Invalid network address.\n"); exit (2); } if (inet_aton(argv[2], &netmask) == 0) { fprintf(stderr, "Invalid netmask.\n"); exit (2); } if (inet_aton(argv[3], &ipaddr) == 0) { fprintf(stderr, "Invalid IP address.\n"); exit (2); } fprintf(stderr, "netmask = %x\n", netmask.s_addr); if ((network.s_addr & netmask.s_addr) == (ipaddr.s_addr & netmask.s_addr)) fprintf(stdout, "IP address is in network.\n"); else fprintf(stdout, "IP address is not in network.\n"); return 0; }