diff options
| author | David M. Syzdek <david@syzdek.net> | 2020-10-01 12:40:07 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2020-10-01 12:40:07 -0800 |
| commit | 3af5b307b97b972ea8db6bac3859a0252761a9a9 (patch) | |
| tree | 906c6a978477a9f360b1f127b5221a5a3d224c37 /src/netcalc.c | |
| parent | b3994156ef29ed3d4aee3fdd1f4bbaa0a56649c1 (diff) | |
| download | dmstools-3af5b307b97b972ea8db6bac3859a0252761a9a9.tar.gz dmstools-3af5b307b97b972ea8db6bac3859a0252761a9a9.tar.bz2 dmstools-3af5b307b97b972ea8db6bac3859a0252761a9a9.tar.xz dmstools-3af5b307b97b972ea8db6bac3859a0252761a9a9.zip | |
netcalc: adding ability to read CIDR from network address
Diffstat (limited to '')
| -rw-r--r-- | src/netcalc.c | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/netcalc.c b/src/netcalc.c index 43599ff..35629e4 100644 --- a/src/netcalc.c +++ b/src/netcalc.c | |||
| @@ -130,7 +130,7 @@ | |||
| 130 | const char * netcalc_ipv4tostr(uint64_t ipv4); | 130 | const char * netcalc_ipv4tostr(uint64_t ipv4); |
| 131 | 131 | ||
| 132 | // tests if string represents an IP address | 132 | // tests if string represents an IP address |
| 133 | int netcalc_is_ipv4 PARAMS((const char * str, uint64_t * ipp)); | 133 | int netcalc_is_ipv4 PARAMS((const char * str, uint64_t * ipp, uint64_t * cidrp)); |
| 134 | 134 | ||
| 135 | // displays usage | 135 | // displays usage |
| 136 | void netcalc_usage PARAMS((void)); | 136 | void netcalc_usage PARAMS((void)); |
| @@ -164,7 +164,7 @@ const char * netcalc_ipv4tostr(uint64_t ipv4) | |||
| 164 | 164 | ||
| 165 | 165 | ||
| 166 | /// tests if string represents an IP address | 166 | /// tests if string represents an IP address |
| 167 | int netcalc_is_ipv4(const char * str, uint64_t * ipv4_ptr) | 167 | int netcalc_is_ipv4(const char * str, uint64_t * ipv4_ptr, uint64_t * cidrp) |
| 168 | { | 168 | { |
| 169 | int c; | 169 | int c; |
| 170 | int value; | 170 | int value; |
| @@ -177,7 +177,7 @@ int netcalc_is_ipv4(const char * str, uint64_t * ipv4_ptr) | |||
| 177 | 177 | ||
| 178 | // reset initial IP address | 178 | // reset initial IP address |
| 179 | ipv4 = 0; | 179 | ipv4 = 0; |
| 180 | ptr = 0; | 180 | ptr = NULL; |
| 181 | 181 | ||
| 182 | // loops through octets | 182 | // loops through octets |
| 183 | for(c = 0; c < 4; c++) | 183 | for(c = 0; c < 4; c++) |
| @@ -189,7 +189,7 @@ int netcalc_is_ipv4(const char * str, uint64_t * ipv4_ptr) | |||
| 189 | // exits if string is not a number or IP address | 189 | // exits if string is not a number or IP address |
| 190 | if (!(ptr)) | 190 | if (!(ptr)) |
| 191 | return(1); | 191 | return(1); |
| 192 | if ((ptr[0] != '.') && (ptr[0] != '\0')) | 192 | if ((ptr[0] != '.') && (ptr[0] != '\0') && (ptr[0] != '/')) |
| 193 | return(1); | 193 | return(1); |
| 194 | 194 | ||
| 195 | // exits if number is out of bounds for IPv4 octet | 195 | // exits if number is out of bounds for IPv4 octet |
| @@ -208,6 +208,21 @@ int netcalc_is_ipv4(const char * str, uint64_t * ipv4_ptr) | |||
| 208 | *ipv4_ptr = ipv4; | 208 | *ipv4_ptr = ipv4; |
| 209 | return(0); | 209 | return(0); |
| 210 | }; | 210 | }; |
| 211 | |||
| 212 | // process CIDR | ||
| 213 | if (ptr[0] == '/') | ||
| 214 | { | ||
| 215 | value = (int)strtol(&ptr[1], &ptr, 10); | ||
| 216 | if (!(ptr)) | ||
| 217 | return(1); | ||
| 218 | if (ptr[0] != '\0') | ||
| 219 | return(1); | ||
| 220 | if ((value < 0) || (value > 32)) | ||
| 221 | return(1); | ||
| 222 | if ((cidrp)) | ||
| 223 | *cidrp = value; | ||
| 224 | return(0); | ||
| 225 | }; | ||
| 211 | }; | 226 | }; |
| 212 | 227 | ||
| 213 | // if we made it here, then the string is not an IPv4 address | 228 | // if we made it here, then the string is not an IPv4 address |
| @@ -263,9 +278,9 @@ int main(int argc, char * argv[]) | |||
| 263 | int c; | 278 | int c; |
| 264 | int opt; | 279 | int opt; |
| 265 | int opt_index; | 280 | int opt_index; |
| 266 | int64_t cidr; | 281 | uint64_t cidr; |
| 267 | int64_t opt_cidr_high; | 282 | uint64_t opt_cidr_high; |
| 268 | int64_t opt_ip_count; | 283 | uint64_t opt_ip_count; |
| 269 | uint64_t ipv4; | 284 | uint64_t ipv4; |
| 270 | uint64_t ipv4_low; | 285 | uint64_t ipv4_low; |
| 271 | uint64_t ipv4_high; | 286 | uint64_t ipv4_high; |
| @@ -334,7 +349,7 @@ int main(int argc, char * argv[]) | |||
| 334 | ipv4_low = 0; | 349 | ipv4_low = 0; |
| 335 | for(c = optind; c < argc; c++) | 350 | for(c = optind; c < argc; c++) |
| 336 | { | 351 | { |
| 337 | if (netcalc_is_ipv4(argv[c], &ipv4)) | 352 | if (netcalc_is_ipv4(argv[c], &ipv4, &opt_cidr_high)) |
| 338 | { | 353 | { |
| 339 | printf("%s: invalid IPv4 address: %s\n", PROGRAM_NAME, argv[c]); | 354 | printf("%s: invalid IPv4 address: %s\n", PROGRAM_NAME, argv[c]); |
| 340 | return(1); | 355 | return(1); |
