From 92f3ec647146f6413052af3ce49bb35dccf40710 Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Wed, 6 Oct 2021 16:10:01 -0800 Subject: netcalc: adding verbose mode --- src/netcalc.c | 478 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 344 insertions(+), 134 deletions(-) (limited to 'src') diff --git a/src/netcalc.c b/src/netcalc.c index e4fa60a..5b5ebda 100644 --- a/src/netcalc.c +++ b/src/netcalc.c @@ -100,6 +100,9 @@ #define NETCALC_IPV6_EXPAND 0x0002 #define NETCALC_IPV6_FULL 0x0004 #define NETCALC_NO_MAP 0x0008 +#define NETCALC_VERBOSE 0x0010 +#define NETCALC_LIST 0x0020 +#define NETCALC_SUPERBLOCK 0x0040 #define NETCALC_INET 4 #define NETCALC_INET6 6 @@ -116,6 +119,7 @@ typedef struct _netcalc netcalc; struct _netcalc_ip { + int32_t cidr; uint32_t addr[8]; }; @@ -132,6 +136,11 @@ struct _netcalc size_t count; netcalc_ip * lower; netcalc_ip * upper; + netcalc_ip * superblock; + netcalc_ip ** list; + size_t list_len; + int padint; + int len_address; int len_netmask; int len_broadcast; int len_network; @@ -155,6 +164,11 @@ netcalc_free( netcalc * cnfi ); +int +netcalc_ip_append( + netcalc * cnf, + netcalc_ip * ip ); + int netcalc_ip_cmp( netcalc_ip * ip1, @@ -249,6 +263,12 @@ netcalc_net_network_r( int32_t cidr ); +int +netcalc_net_sort_cmp( + const void * ap, + const void * bp ); + + void netcalc_net_wildmask_r( netcalc_ip * wildmask, @@ -260,7 +280,24 @@ netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, - int header ); + uint64_t opts ); + + +void +netcalc_print_space( + netcalc * cnf, + netcalc_ip * ip, + int32_t cidr ); + + +void +netcalc_results_default( + netcalc * cnf ); + + +void +netcalc_results_verbose( + netcalc * cnf ); // displays usage @@ -292,9 +329,23 @@ char buff[1024]; void netcalc_free( netcalc * cnf ) { + size_t pos; + if (!(cnf)) return; + for(pos = 0; (pos < cnf->list_len); pos++) + { + netcalc_ip_free(cnf->list[pos]); + cnf->list[pos] = NULL; + }; + + if (cnf->superblock != NULL) + { + netcalc_ip_free(cnf->superblock); + cnf->superblock = NULL; + }; + bzero(cnf, sizeof(netcalc)); free(cnf); @@ -313,12 +364,29 @@ int netcalc_init( netcalc ** cnfp ) }; bzero(*cnfp, sizeof(netcalc)); + if (((*cnfp)->list = malloc(sizeof(netcalc_ip *))) == NULL) + { + netcalc_free(*cnfp); + fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); + return(1); + }; + (*cnfp)->list[0] = NULL; + + if (((*cnfp)->superblock = malloc(sizeof(netcalc_ip))) == NULL) + { + netcalc_free(*cnfp); + fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); + return(1); + }; + bzero((*cnfp)->superblock, sizeof(netcalc_ip)); + (*cnfp)->cidr = 128; (*cnfp)->cidr_limit = -1; (*cnfp)->cidr_incr = -1; (*cnfp)->family = NETCALC_INET; (*cnfp)->display = NETCALC_INET; + (*cnfp)->len_address = strlen("SUPERBLOCK");; (*cnfp)->len_netmask = 7; (*cnfp)->len_network = 7; (*cnfp)->len_wildmask = 8; @@ -328,6 +396,30 @@ int netcalc_init( netcalc ** cnfp ) } +int netcalc_ip_append( netcalc * cnf, netcalc_ip * ip ) +{ + size_t size; + void * ptr; + + assert(cnf != NULL); + assert(ip != NULL); + + size = sizeof(netcalc_ip *) * (cnf->list_len+1); + + if ((ptr = realloc(cnf->list, size)) == NULL) + { + fprintf(stderr, "%s:%i: out of virtual memory\n", PROGRAM_NAME, __LINE__); + return(1); + }; + cnf->list = ptr; + + cnf->list[cnf->list_len] = ip; + cnf->list_len++; + + return(0); +} + + int netcalc_ip_cmp( netcalc_ip * ip1, netcalc_ip * ip2 ) { int pos; @@ -358,7 +450,6 @@ int netcalc_ip_parse( netcalc * cnf, netcalc_ip ** ipp, const char * ipstr ) netcalc_ip * ip; char * ptr; char * endptr; - int32_t cidr; char * str; assert(cnf != NULL); @@ -369,6 +460,7 @@ int netcalc_ip_parse( netcalc * cnf, netcalc_ip ** ipp, const char * ipstr ) fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); return(1); }; + ip->cidr = -1; if ((str = strdup(ipstr)) == NULL) { netcalc_ip_free(ip); @@ -377,11 +469,10 @@ int netcalc_ip_parse( netcalc * cnf, netcalc_ip ** ipp, const char * ipstr ) }; // parses CIDR - cidr = -1; if ((ptr = strrchr(str, '/')) != NULL) { - ptr[0] = '\0'; - cidr = (int32_t)strtol(&ptr[1], &endptr, 0); + ptr[0] = '\0'; + ip->cidr = (int32_t)strtol(&ptr[1], &endptr, 0); if ((endptr[0] != '\0') || (&ptr[1] == endptr)) { fprintf(stderr, "%s: invalid CIDR\n", PROGRAM_NAME); @@ -394,20 +485,6 @@ int netcalc_ip_parse( netcalc * cnf, netcalc_ip ** ipp, const char * ipstr ) // parses string if ((ptr = strchr(str, ':')) == NULL) { - // verifies cidr - if ((cidr < -1) || (cidr > 32)) - { - fprintf(stderr, "%s: invalid CIDR\n", PROGRAM_NAME); - netcalc_ip_free(ip); - free(str); - return(1); - }; - - // normalizes CIDR for IPv6 - if (cidr >= 0) - cidr += 128 - 32; - - // parse string if ((rc = netcalc_ip_parse_ipv4(cnf, ip, str)) != 0) { netcalc_ip_free(ip); @@ -415,16 +492,6 @@ int netcalc_ip_parse( netcalc * cnf, netcalc_ip ** ipp, const char * ipstr ) return(1); }; } else { - // verifies cidr - if ((cidr < -1) || (cidr > 128)) - { - fprintf(stderr, "%s: invalid CIDR\n", PROGRAM_NAME); - netcalc_ip_free(ip); - free(str); - return(1); - }; - - // parses string if ((rc = netcalc_ip_parse_ipv6(cnf, ip, str)) != 0) { netcalc_ip_free(ip); @@ -434,8 +501,10 @@ int netcalc_ip_parse( netcalc * cnf, netcalc_ip ** ipp, const char * ipstr ) }; // normalizes CIDR - if ((cnf->cidr > cidr) && (cidr != -1)) - cnf->cidr = cidr; + if (ip->cidr == -1) + ip->cidr = 128; + if ((cnf->cidr > ip->cidr) && (ip->cidr != -1)) + cnf->cidr = ip->cidr; // frees resources free(str); @@ -451,6 +520,18 @@ int netcalc_ip_parse_ipv4( netcalc * cnf, netcalc_ip * ip, char * str ) assert(cnf != NULL); assert(ip != NULL); + // verifies cidr + if ((ip->cidr < -1) || (ip->cidr > 32)) + { + fprintf(stderr, "%s: invalid CIDR\n", PROGRAM_NAME); + return(1); + }; + + // normalizes CIDR for IPv6 + if (ip->cidr >= 0) + ip->cidr += 128 - 32; + + // sets prefix for IPv6 mapped IPv4 addresses ip->addr[5] = 0xffff; // parses IPv4 string @@ -522,6 +603,13 @@ int netcalc_ip_parse_ipv6( netcalc * cnf, netcalc_ip * ip, char * str) cnf->family = NETCALC_INET6; + // verifies cidr + if ((ip->cidr < -1) || (ip->cidr > 128)) + { + fprintf(stderr, "%s: invalid CIDR\n", PROGRAM_NAME); + return(1); + }; + // verifies zero compression compress = 0; for(pos = 1; (pos < (int)strlen(str)); pos++) @@ -793,10 +881,40 @@ void netcalc_net_network_r( netcalc_ip * network, netcalc_ip * ip, int32_t cidr netcalc_net_netmask_r(&netmask, cidr); for(pos = 0; (pos < 8); pos++) network->addr[pos] = ip->addr[pos] & netmask.addr[pos]; + network->cidr = cidr; return; } +int netcalc_net_sort_cmp( const void * ap, const void * bp ) +{ + int i; + const netcalc_ip * a; + const netcalc_ip * b; + + assert( ap != NULL ); + assert( bp != NULL ); + a = *(const netcalc_ip * const *) ap; + b = *(const netcalc_ip * const *) bp; + + for(i = 0; ( (i < 7) && (a->addr[i] == b->addr[i]) ); i++); + + if ( a->addr[i] < b->addr[i] ) + return(-1); + + if ( a->addr[i] > b->addr[i] ) + return(1); + + if ( a->cidr < b->cidr ) + return(-1); + + if ( a->cidr > b->cidr ) + return(1); + + return(0); +} + + void netcalc_net_wildmask_r( netcalc_ip * wildmask, int32_t cidr ) { int32_t pos; @@ -807,37 +925,51 @@ void netcalc_net_wildmask_r( netcalc_ip * wildmask, int32_t cidr ) } -void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, int header ) +void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, uint64_t opts ) { int pos; netcalc_ip netmask; netcalc_ip broadcast; netcalc_ip network; netcalc_ip wildmask; + char str_address[56]; char str_netmask[56]; char str_broadcast[56]; char str_network[56]; char str_wildmask[56]; char str_subnets[128]; + char str_ip[56]; uint64_t count; + opts |= cnf->opts; + if ((ip)) { netcalc_net_netmask_r( &netmask, cidr ); netcalc_net_wildmask_r( &wildmask, cidr ); netcalc_net_network_r( &network, ip, cidr ); netcalc_net_broadcast_r( &broadcast, ip, cidr ); + netcalc_ip_string( cnf, ip, str_ip, sizeof(str_ip) ); netcalc_ip_string( cnf, &netmask, str_netmask, sizeof(str_netmask) ); netcalc_ip_string( cnf, &wildmask, str_wildmask, sizeof(str_wildmask) ); netcalc_ip_string( cnf, &network, str_network, sizeof(str_network) ); netcalc_ip_string( cnf, &broadcast, str_broadcast, sizeof(str_broadcast) ); + if ((opts & NETCALC_SUPERBLOCK)) + { + snprintf(str_address, sizeof(str_address), "SUPERBLOCK"); + } else { + if (cnf->display == NETCALC_INET6) + snprintf(str_address, sizeof(str_address), "%s/%i", str_ip, ip->cidr); + else + snprintf(str_address, sizeof(str_address), "%s/%i", str_ip, ip->cidr+32-128); + }; }; // network count - if (cnf->cidr_incr >= cnf->cidr) + if (cnf->cidr_incr >= cidr) { count = 1; - for(pos = 0; (pos < (cnf->cidr_incr - cnf->cidr)); pos++) + for(pos = 0; (pos < (cnf->cidr_incr - cidr)); pos++) count *= 2; } else { count = 0; @@ -846,75 +978,166 @@ void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, int header // print IPv4 information if (cnf->display == NETCALC_INET) { - if ((header)) + if (!(ip)) { snprintf(str_subnets, sizeof(str_subnets), "/%" PRIu32 "s", (cnf->cidr_incr + 32 - 128)); // prints field names - printf("%-15s", "Network"); - printf(" %-15s", "Broadcast"); - printf(" %-15s", "Netmask"); - printf(" %-15s", "Wildcard"); - printf(" %-4s", "CIDR"); - printf(" %s", str_subnets); - printf("\n"); + if ((cnf->opts & NETCALC_VERBOSE)) + printf( "%-15s ", "Address"); + printf( "%-15s ", "Network"); + printf( "%-15s ", "Broadcast"); + printf( "%-15s ", "Netmask"); + printf( "%-15s ", "Wildcard"); + printf( "%-4s ", "CIDR"); + printf( "%s", str_subnets); + printf( "\n"); return; }; - printf("%-15s", str_network); - printf(" %-15s", str_broadcast); - printf(" %-15s", str_netmask); - printf(" %-15s", str_wildmask); - printf(" %-4i", cidr + 32 - 128); - printf(" %" PRIu64, count); - printf("\n"); + if ((cnf->opts & NETCALC_VERBOSE)) + printf( "%-15s ", str_address ); + printf( "%-15s ", str_network ); + printf( "%-15s ", str_broadcast ); + printf( "%-15s ", str_netmask ); + printf( "%-15s ", str_wildmask ); + printf( "%-4i ", cidr + 32 - 128 ); + if ((cnf->cidr_incr >= cidr) && ((cnf->cidr_incr - cidr) < 64)) + printf("%" PRIu64 " ", count); + else + printf("n/a "); + printf( "\n" ); return; }; - cnf->len_netmask = (cnf->len_netmask < (int)strlen(str_netmask)) ? (int)strlen(str_netmask) : cnf->len_netmask; - cnf->len_network = (cnf->len_network < (int)strlen(str_network)) ? (int)strlen(str_network) : cnf->len_network; - cnf->len_wildmask = (cnf->len_wildmask < (int)strlen(str_wildmask)) ? (int)strlen(str_wildmask) : cnf->len_wildmask; - cnf->len_broadcast = (cnf->len_broadcast < (int)strlen(str_broadcast)) ? (int)strlen(str_broadcast) : cnf->len_broadcast; - // print IPv6 information - if ((header)) + if (!(ip)) { snprintf(str_subnets, sizeof(str_subnets), "/%" PRIu32 "s", cnf->cidr_incr); // prints field names - printf("%-*s", cnf->len_network, "Network"); - printf(" %-*s", cnf->len_broadcast, "Broadcast"); - printf(" %-*s", cnf->len_netmask, "Netmask"); - printf(" %-*s", cnf->len_wildmask, "Wildcard"); - printf(" %-4s", "CIDR"); - printf(" %s", str_subnets); - printf("\n"); + if ((cnf->opts & NETCALC_VERBOSE)) + printf( "%-*s ", cnf->len_address, "Address" ); + printf( "%-*s ", cnf->len_network, "Network" ); + printf( "%-*s ", cnf->len_broadcast, "Broadcast" ); + printf( "%-*s ", cnf->len_netmask, "Netmask" ); + printf( "%-*s ", cnf->len_wildmask, "Wildcard" ); + printf( "%-4s ", "CIDR" ); + printf( "%s", str_subnets ); + printf( "\n"); return; }; - printf("%-*s", cnf->len_network, str_network); - printf(" %-*s", cnf->len_broadcast, str_broadcast); - printf(" %-*s", cnf->len_netmask, str_netmask); - printf(" %-*s", cnf->len_wildmask, str_wildmask); - printf(" %-4i", cidr); - if ((cnf->cidr_incr >= cnf->cidr) && ((cnf->cidr_incr - cnf->cidr) < 64)) - printf(" %" PRIu64, count); + if ((cnf->opts & NETCALC_VERBOSE)) + printf( "%-*s ", cnf->len_address, str_address); + printf( "%-*s ", cnf->len_network, str_network); + printf( "%-*s ", cnf->len_broadcast, str_broadcast); + printf( "%-*s ", cnf->len_netmask, str_netmask); + printf( "%-*s ", cnf->len_wildmask, str_wildmask); + printf( "%-4i ", cidr); + if ((cnf->cidr_incr >= cidr) && ((cnf->cidr_incr - cidr) < 64)) + printf("%" PRIu64 " ", count); else - printf(" n/a"); + printf("n/a "); printf("\n"); return; } +void netcalc_print_space( netcalc * cnf, netcalc_ip * ip, int32_t cidr ) +{ + netcalc_ip netmask; + netcalc_ip broadcast; + netcalc_ip network; + netcalc_ip wildmask; + char str_address[56]; + char str_netmask[56]; + char str_broadcast[56]; + char str_network[56]; + char str_wildmask[56]; + char str_cidr[5]; + int len_address; + + netcalc_net_netmask_r( &netmask, cidr ); + netcalc_net_wildmask_r( &wildmask, cidr ); + netcalc_net_network_r( &network, ip, cidr ); + netcalc_net_broadcast_r( &broadcast, ip, cidr ); + + netcalc_ip_string( cnf, ip, str_address, sizeof(str_address) ); + netcalc_ip_string( cnf, &netmask, str_netmask, sizeof(str_netmask) ); + netcalc_ip_string( cnf, &wildmask, str_wildmask, sizeof(str_wildmask) ); + netcalc_ip_string( cnf, &network, str_network, sizeof(str_network) ); + netcalc_ip_string( cnf, &broadcast, str_broadcast, sizeof(str_broadcast) ); + + len_address = (int)strlen(str_address); + if (cnf->display == NETCALC_INET6) + len_address += snprintf(str_cidr, sizeof(str_cidr), "/%i", ip->cidr); + else + len_address += snprintf(str_cidr, sizeof(str_cidr), "/%i", ip->cidr + 32 - 128); + + cnf->len_address = (cnf->len_address < len_address) ? len_address : cnf->len_address; + cnf->len_netmask = (cnf->len_netmask < (int)strlen(str_netmask)) ? (int)strlen(str_netmask) : cnf->len_netmask; + cnf->len_network = (cnf->len_network < (int)strlen(str_network)) ? (int)strlen(str_network) : cnf->len_network; + cnf->len_wildmask = (cnf->len_wildmask < (int)strlen(str_wildmask)) ? (int)strlen(str_wildmask) : cnf->len_wildmask; + cnf->len_broadcast = (cnf->len_broadcast < (int)strlen(str_broadcast)) ? (int)strlen(str_broadcast) : cnf->len_broadcast; + + return; +} + + +void netcalc_results_default( netcalc * cnf ) +{ + int32_t cidr; + int32_t cidr_floor; + + cidr_floor = (cnf->family == NETCALC_INET6) ? 0 : 128-32; + + // calculate spacing + netcalc_print_space(cnf, cnf->superblock, cnf->cidr); + if ((cnf->opts & NETCALC_ALL_NETWORKS)) + for(cidr = cnf->cidr; (cidr > cidr_floor); cidr--) + netcalc_print_space(cnf, cnf->lower, cidr); + + // print data + netcalc_print_ip(cnf, NULL, cnf->cidr, 0); + netcalc_print_ip(cnf, cnf->superblock, cnf->cidr, 0); + if ((cnf->opts & NETCALC_ALL_NETWORKS)) + for(cidr = cnf->cidr; (cidr > cidr_floor); cidr--) + netcalc_print_ip(cnf, cnf->lower, cidr-1, 0); + + return; +} + + +void netcalc_results_verbose( netcalc * cnf ) +{ + size_t pos; + + // calculate spacing + netcalc_print_space(cnf, cnf->superblock, cnf->cidr); + for(pos = 0; (pos < cnf->list_len); pos++) + netcalc_print_space(cnf, cnf->list[pos], cnf->list[pos]->cidr); + + // print data + netcalc_print_ip(cnf, NULL, cnf->cidr, 0); + netcalc_print_ip(cnf, cnf->superblock, cnf->cidr, NETCALC_SUPERBLOCK); + for(pos = 0; (pos < cnf->list_len); pos++) + netcalc_print_ip(cnf, cnf->list[pos], cnf->list[pos]->cidr, 0); + + return; +} + + /// displays usage void netcalc_usage(void) { printf("Usage: %s [OPTIONS] address1 address2 ... addressN\n", PROGRAM_NAME); - printf(" -a display all matching networks\n"); - printf(" -c cidr requested network size in CIDR notation\n"); - printf(" -f print full IPv6 notation (do not compress zeros)\n"); - printf(" -h, --help print this help and exit\n"); - printf(" -i cidr increment size of network list\n"); - printf(" -m do not display IPv4 mapped addresses\n"); - printf(" -V, --version print version number and exit\n"); - printf(" -x print IPv6 expanded notation (print leading zeros)\n"); + printf(" -a display all inclusive networks (incompatible with -v)\n"); + printf(" -c cidr requested network size in CIDR notation\n"); + printf(" -f print full IPv6 notation (do not compress zeros)\n"); + printf(" -h, --help print this help and exit\n"); + printf(" -i cidr increment size of network list\n"); + printf(" -m do not display IPv4 mapped addresses\n"); + printf(" -V, --version print version number and exit\n"); + printf(" -v, --verbose display all input networks (incompatible with -a)\n"); + printf(" -x print IPv6 expanded notation (print leading zeros)\n"); printf("\n"); printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT); return; @@ -944,8 +1167,9 @@ int main(int argc, char * argv[]) int opt_index; netcalc * cnf; netcalc_ip * ip; + size_t pos; - static char short_opt[] = "6ac:fhi:mVx"; + static char short_opt[] = "6ac:fhi:mVvx"; static struct option long_opt[] = { {"help", no_argument, 0, 'h'}, @@ -969,9 +1193,9 @@ int main(int argc, char * argv[]) break; case 'a': - cnf->opts |= NETCALC_ALL_NETWORKS; - cnf->len_wildmask = 39; - cnf->len_broadcast = 39; + cnf->opts |= NETCALC_ALL_NETWORKS; + cnf->opts &= ~NETCALC_LIST; + cnf->opts &= ~NETCALC_VERBOSE; break; case 'c': @@ -1002,6 +1226,12 @@ int main(int argc, char * argv[]) }; break; + case 'l': + cnf->opts &= ~NETCALC_ALL_NETWORKS; + cnf->opts |= NETCALC_LIST; + cnf->opts &= ~NETCALC_VERBOSE; + break; + case 'm': cnf->opts |= NETCALC_NO_MAP; break; @@ -1010,6 +1240,12 @@ int main(int argc, char * argv[]) netcalc_version(); return(0); + case 'v': + cnf->opts &= ~NETCALC_ALL_NETWORKS; + cnf->opts &= ~NETCALC_LIST; + cnf->opts |= NETCALC_VERBOSE; + break; + case 'x': cnf->opts |= NETCALC_IPV6_EXPAND; cnf->opts |= NETCALC_IPV6_FULL; @@ -1042,40 +1278,25 @@ int main(int argc, char * argv[]) return(1); }; - // initialize bounds - if (!(cnf->lower)) + if ((rc = netcalc_ip_append(cnf, ip)) != 0) { - if ((cnf->lower = malloc(sizeof(netcalc_ip))) == NULL) - { - fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); - netcalc_ip_free(ip); - netcalc_free(cnf); - return(1); - }; - memcpy(cnf->lower, ip, sizeof(netcalc_ip)); - }; - if (!(cnf->upper)) - { - if ((cnf->upper = malloc(sizeof(netcalc_ip))) == NULL) - { - fprintf(stderr, "%s: out of virtual memory\n", PROGRAM_NAME); - netcalc_ip_free(ip); - netcalc_free(cnf); - return(1); - }; - memcpy(cnf->upper, ip, sizeof(netcalc_ip)); + netcalc_ip_free(ip); + netcalc_free(cnf); + return(1); }; + }; + qsort(cnf->list, cnf->list_len, sizeof(netcalc_ip *), netcalc_net_sort_cmp); - // updates bounds - if (netcalc_ip_cmp(ip, cnf->lower) < 0) + // initialize bounds + cnf->lower = cnf->list[0]; + cnf->upper = cnf->list[0]; + for(pos = 0; (pos < cnf->list_len); pos++) + { + if (netcalc_ip_cmp(cnf->list[pos], cnf->lower) < 0) { - netcalc_ip_free(cnf->lower); - cnf->lower = ip; - } else if (netcalc_ip_cmp(ip, cnf->upper) > 0) { - netcalc_ip_free(cnf->upper); - cnf->upper = ip; - } else { - netcalc_ip_free(ip); + cnf->lower = cnf->list[pos]; + } else if (netcalc_ip_cmp(cnf->list[pos], cnf->upper) > 0) { + cnf->upper = cnf->list[pos]; }; }; @@ -1091,33 +1312,22 @@ int main(int argc, char * argv[]) cnf->cidr_incr = (cnf->cidr_incr == -1) ? 64 : cnf->cidr_incr; cnf->cidr = (cnf->cidr < cnf->cidr_limit) ? cnf->cidr : cnf->cidr_limit; }; + if ((cnf->opts & NETCALC_ALL_NETWORKS)) + { + cnf->len_wildmask = 39; + cnf->len_broadcast = 39; + }; // calculate inclusive CIDR while ((rc = netcalc_net_cmp(cnf->lower, cnf->upper, cnf->cidr)) != 0) cnf->cidr--; + netcalc_net_network_r(cnf->superblock, cnf->lower, cnf->cidr); - // print header - netcalc_print_ip(cnf, cnf->lower, cnf->cidr, 1); - netcalc_print_ip(cnf, cnf->lower, cnf->cidr, 0); - - // print all matching subnets - if ((cnf->opts & NETCALC_ALL_NETWORKS)) - { - if (cnf->family == NETCALC_INET6) - { - while(cnf->cidr > 0) - { - cnf->cidr--; - netcalc_print_ip(cnf, cnf->lower, cnf->cidr, 0); - }; - } else { - while(cnf->cidr > (128-32)) - { - cnf->cidr--; - netcalc_print_ip(cnf, cnf->lower, cnf->cidr, 0); - }; - }; - }; + // display results + if ((cnf->opts & NETCALC_VERBOSE)) + netcalc_results_verbose(cnf); + else + netcalc_results_default(cnf); return(0); } -- cgit v1.2.3