#include #include #include #include #include #include #include int main(int argc, char **argv) { in_addr_t ip; /* in network byte order */ in_addr_t i; /* in local byte order */ char class; /* IPv4 address class: A, B, C, D, or E */ if (argc != 2) { fprintf(stderr, "%s: argument must be a dotted IPv4 address\n", argv[0]); return 1; } if (inet_pton(AF_INET, argv[1], &ip) != 1) { fprintf(stderr, "%s: argument %s must be a dotted IPv4 address\n", argv[0], argv[1]); return 2; } i = ntohl(ip); /* network to host long */ if ((i & 1 << 31) == 0) { class = 'A'; /* Bit 31 is 0 (includes loopback address). */ } else if ((i & 1 << 30) == 0) { class = 'B'; /* Bit 31 is 1, bit 30 is 0. */ } else if ((i & 1 << 29) == 0) { class = 'C'; /* Bit 31 is 1, bit 30 is 1, bit 29 is 0. */ } else if ((i & 1 << 28) == 0) { class = 'D'; /* Bit 31 is 1, bit 30 is 1, 29 is 1, 28 is 0. */ } else { class = 'E'; /* Bit 31 is 1, bit 30 is 1, 29 is 1, 28 is 1. */ } printf("%s is a class %c IPv4 address.\n", argv[1], class); return EXIT_SUCCESS; }