123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #include <stdio.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include "parse.h"
- /*
- * Assumes full string is given in "start" argument, which is then modified.
- * Does not do any validation, inet_network can do that later.
- * Need "end" to be a double-pointer so we can modify the original pointer.
- */
- int split_range(char *start, char **end) {
- bool range_delim_found = false;
- char *p = start;
- while(*p != '\0') {
- if(range_delim_found) {
- *end = p;
- return 0;
- }
- if(*p == '-') {
- *p = '\0';
- range_delim_found = true;
- }
- ++p;
- }
- // Never found delimiter ...
- return 1;
- }
- void deaggregate(in_addr_t start, in_addr_t end) {
- in_addr_t base = start;
- while(base <= end) {
- int step = 0;
- while((base | (1 << step)) != base) { // Look for the first zero from the right
- if((base | UINT32_MAX >> (31 - step)) > end) {
- break;
- }
- ++step;
- }
- struct in_addr prefix;
- prefix.s_addr = htonl(base);
- printf("%s/%d\n", inet_ntoa(prefix), 32 - step);
- base += 1 << step;
- }
- }
|