aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2021-10-06 22:08:26 -0800
committerDavid M. Syzdek <david@syzdek.net>2021-10-06 22:08:26 -0800
commit7b42878b7ccb07c66d5093e9a861036fbf7d76de (patch)
tree5a7833c07f10821e48ee80965589470dd2b49dcf /src
parent92f3ec647146f6413052af3ce49bb35dccf40710 (diff)
downloaddmstools-7b42878b7ccb07c66d5093e9a861036fbf7d76de.tar.gz
dmstools-7b42878b7ccb07c66d5093e9a861036fbf7d76de.tar.bz2
dmstools-7b42878b7ccb07c66d5093e9a861036fbf7d76de.tar.xz
dmstools-7b42878b7ccb07c66d5093e9a861036fbf7d76de.zip
netcalc: adding ability to list subnets within a superblock
Diffstat (limited to 'src')
-rw-r--r--src/netcalc.c104
1 files changed, 95 insertions, 9 deletions
diff --git a/src/netcalc.c b/src/netcalc.c
index 5b5ebda..eb2ac6d 100644
--- a/src/netcalc.c
+++ b/src/netcalc.c
@@ -296,6 +296,18 @@ netcalc_results_default(
296 296
297 297
298void 298void
299netcalc_results_list(
300 netcalc * cnf );
301
302
303void
304netcalc_results_list_add(
305 netcalc * cnf,
306 netcalc_ip * network,
307 netcalc_ip * incr );
308
309
310void
299netcalc_results_verbose( 311netcalc_results_verbose(
300 netcalc * cnf ); 312 netcalc * cnf );
301 313
@@ -982,8 +994,8 @@ void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, uint64_t op
982 { 994 {
983 snprintf(str_subnets, sizeof(str_subnets), "/%" PRIu32 "s", (cnf->cidr_incr + 32 - 128)); 995 snprintf(str_subnets, sizeof(str_subnets), "/%" PRIu32 "s", (cnf->cidr_incr + 32 - 128));
984 // prints field names 996 // prints field names
985 if ((cnf->opts & NETCALC_VERBOSE)) 997 if ((cnf->opts & (NETCALC_VERBOSE|NETCALC_LIST)))
986 printf( "%-15s ", "Address"); 998 printf( "%-18s ", "Address");
987 printf( "%-15s ", "Network"); 999 printf( "%-15s ", "Network");
988 printf( "%-15s ", "Broadcast"); 1000 printf( "%-15s ", "Broadcast");
989 printf( "%-15s ", "Netmask"); 1001 printf( "%-15s ", "Netmask");
@@ -993,8 +1005,8 @@ void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, uint64_t op
993 printf( "\n"); 1005 printf( "\n");
994 return; 1006 return;
995 }; 1007 };
996 if ((cnf->opts & NETCALC_VERBOSE)) 1008 if ((cnf->opts & (NETCALC_VERBOSE|NETCALC_LIST)))
997 printf( "%-15s ", str_address ); 1009 printf( "%-18s ", str_address );
998 printf( "%-15s ", str_network ); 1010 printf( "%-15s ", str_network );
999 printf( "%-15s ", str_broadcast ); 1011 printf( "%-15s ", str_broadcast );
1000 printf( "%-15s ", str_netmask ); 1012 printf( "%-15s ", str_netmask );
@@ -1013,7 +1025,7 @@ void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, uint64_t op
1013 { 1025 {
1014 snprintf(str_subnets, sizeof(str_subnets), "/%" PRIu32 "s", cnf->cidr_incr); 1026 snprintf(str_subnets, sizeof(str_subnets), "/%" PRIu32 "s", cnf->cidr_incr);
1015 // prints field names 1027 // prints field names
1016 if ((cnf->opts & NETCALC_VERBOSE)) 1028 if ((cnf->opts & (NETCALC_VERBOSE|NETCALC_LIST)))
1017 printf( "%-*s ", cnf->len_address, "Address" ); 1029 printf( "%-*s ", cnf->len_address, "Address" );
1018 printf( "%-*s ", cnf->len_network, "Network" ); 1030 printf( "%-*s ", cnf->len_network, "Network" );
1019 printf( "%-*s ", cnf->len_broadcast, "Broadcast" ); 1031 printf( "%-*s ", cnf->len_broadcast, "Broadcast" );
@@ -1024,7 +1036,7 @@ void netcalc_print_ip( netcalc * cnf, netcalc_ip * ip, int32_t cidr, uint64_t op
1024 printf( "\n"); 1036 printf( "\n");
1025 return; 1037 return;
1026 }; 1038 };
1027 if ((cnf->opts & NETCALC_VERBOSE)) 1039 if ((cnf->opts & (NETCALC_VERBOSE|NETCALC_LIST)))
1028 printf( "%-*s ", cnf->len_address, str_address); 1040 printf( "%-*s ", cnf->len_address, str_address);
1029 printf( "%-*s ", cnf->len_network, str_network); 1041 printf( "%-*s ", cnf->len_network, str_network);
1030 printf( "%-*s ", cnf->len_broadcast, str_broadcast); 1042 printf( "%-*s ", cnf->len_broadcast, str_broadcast);
@@ -1106,6 +1118,77 @@ void netcalc_results_default( netcalc * cnf )
1106} 1118}
1107 1119
1108 1120
1121void netcalc_results_list( netcalc * cnf )
1122{
1123 int rc;
1124 int32_t cidr;
1125 int32_t pos;
1126 netcalc_ip network;
1127 netcalc_ip incr;
1128
1129 memcpy(&network, cnf->superblock, sizeof(netcalc_ip));
1130 network.cidr = cnf->cidr_incr;
1131
1132 bzero(&incr, sizeof(netcalc_ip));
1133 if (cnf->cidr_incr > 0)
1134 {
1135 cidr = cnf->cidr_incr-1;
1136 incr.cidr = cnf->cidr_incr;
1137 incr.addr[cidr/16] = 0x01 << (15-(cidr%16));
1138 };
1139
1140 netcalc_print_space(cnf, cnf->superblock, cnf->cidr);
1141 netcalc_print_space(cnf, &network, network.cidr);
1142 rc = netcalc_net_cmp(&network, cnf->superblock, cnf->cidr);
1143 while((rc == 0) && (network.addr[0] < 0x10000))
1144 {
1145 netcalc_print_space(cnf, &network, cnf->cidr_incr);
1146 for(pos = 7; (pos > 0); pos--)
1147 {
1148 network.addr[pos] += incr.addr[pos];
1149 if (network.addr[pos] > 0xffff)
1150 {
1151 network.addr[pos-1]++;
1152 network.addr[pos-0] &= 0xffff;
1153 };
1154 };
1155 rc = netcalc_net_cmp(&network, cnf->superblock, cnf->cidr);
1156 };
1157
1158 memcpy(&network, cnf->superblock, sizeof(netcalc_ip));
1159 network.cidr = cnf->cidr_incr;
1160
1161 netcalc_print_ip(cnf, NULL, cnf->cidr, 0);
1162 netcalc_print_ip(cnf, cnf->superblock, cnf->superblock->cidr, NETCALC_SUPERBLOCK);
1163 rc = netcalc_net_cmp(&network, cnf->superblock, cnf->cidr);
1164 while((rc == 0) && (network.addr[0] < 0x10000))
1165 {
1166 netcalc_print_ip(cnf, &network, cnf->cidr_incr, 0);
1167 for(pos = 7; (pos > 0); pos--)
1168 {
1169 network.addr[pos] += incr.addr[pos];
1170 if (network.addr[pos] > 0xffff)
1171 {
1172 network.addr[pos-1]++;
1173 network.addr[pos-0] &= 0xffff;
1174 };
1175 };
1176 rc = netcalc_net_cmp(&network, cnf->superblock, cnf->cidr);
1177 };
1178
1179 return;
1180}
1181
1182
1183void netcalc_results_list_add( netcalc * cnf, netcalc_ip * network, netcalc_ip * incr )
1184{
1185 assert(cnf != NULL);
1186 assert(network != NULL);
1187 assert(incr != NULL);
1188 return;
1189}
1190
1191
1109void netcalc_results_verbose( netcalc * cnf ) 1192void netcalc_results_verbose( netcalc * cnf )
1110{ 1193{
1111 size_t pos; 1194 size_t pos;
@@ -1129,14 +1212,15 @@ void netcalc_results_verbose( netcalc * cnf )
1129void netcalc_usage(void) 1212void netcalc_usage(void)
1130{ 1213{
1131 printf("Usage: %s [OPTIONS] address1 address2 ... addressN\n", PROGRAM_NAME); 1214 printf("Usage: %s [OPTIONS] address1 address2 ... addressN\n", PROGRAM_NAME);
1132 printf(" -a display all inclusive networks (incompatible with -v)\n"); 1215 printf(" -a display all inclusive networks (incompatible with -v and -l)\n");
1133 printf(" -c cidr requested network size in CIDR notation\n"); 1216 printf(" -c cidr requested network size in CIDR notation\n");
1134 printf(" -f print full IPv6 notation (do not compress zeros)\n"); 1217 printf(" -f print full IPv6 notation (do not compress zeros)\n");
1135 printf(" -h, --help print this help and exit\n"); 1218 printf(" -h, --help print this help and exit\n");
1136 printf(" -i cidr increment size of network list\n"); 1219 printf(" -i cidr increment size of network list\n");
1220 printf(" -l display incremental networks (incompatible with -a and -v)\n");
1137 printf(" -m do not display IPv4 mapped addresses\n"); 1221 printf(" -m do not display IPv4 mapped addresses\n");
1138 printf(" -V, --version print version number and exit\n"); 1222 printf(" -V, --version print version number and exit\n");
1139 printf(" -v, --verbose display all input networks (incompatible with -a)\n"); 1223 printf(" -v, --verbose display all input networks (incompatible with -a and -l)\n");
1140 printf(" -x print IPv6 expanded notation (print leading zeros)\n"); 1224 printf(" -x print IPv6 expanded notation (print leading zeros)\n");
1141 printf("\n"); 1225 printf("\n");
1142 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT); 1226 printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
@@ -1169,7 +1253,7 @@ int main(int argc, char * argv[])
1169 netcalc_ip * ip; 1253 netcalc_ip * ip;
1170 size_t pos; 1254 size_t pos;
1171 1255
1172 static char short_opt[] = "6ac:fhi:mVvx"; 1256 static char short_opt[] = "6ac:fhi:lmVvx";
1173 static struct option long_opt[] = 1257 static struct option long_opt[] =
1174 { 1258 {
1175 {"help", no_argument, 0, 'h'}, 1259 {"help", no_argument, 0, 'h'},
@@ -1326,6 +1410,8 @@ int main(int argc, char * argv[])
1326 // display results 1410 // display results
1327 if ((cnf->opts & NETCALC_VERBOSE)) 1411 if ((cnf->opts & NETCALC_VERBOSE))
1328 netcalc_results_verbose(cnf); 1412 netcalc_results_verbose(cnf);
1413 else if ((cnf->opts & NETCALC_LIST))
1414 netcalc_results_list(cnf);
1329 else 1415 else
1330 netcalc_results_default(cnf); 1416 netcalc_results_default(cnf);
1331 1417