parse.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include "parse.h"
  5. /*
  6. * Assumes full string is given in "start" argument, which is then modified.
  7. * Does not do any validation, inet_network can do that later.
  8. * Need "end" to be a double-pointer so we can modify the original pointer.
  9. */
  10. int split_range(char *start, char **end) {
  11. bool range_delim_found = false;
  12. char *p = start;
  13. while(*p != '\0') {
  14. if(range_delim_found) {
  15. *end = p;
  16. return 0;
  17. }
  18. if(*p == '-') {
  19. *p = '\0';
  20. range_delim_found = true;
  21. }
  22. ++p;
  23. }
  24. // Never found delimiter ...
  25. return 1;
  26. }
  27. void deaggregate(in_addr_t start, in_addr_t end) {
  28. in_addr_t base = start;
  29. while(base <= end) {
  30. int step = 0;
  31. while((base | (1 << step)) != base) { // Look for the first zero from the right
  32. if((base | UINT32_MAX >> (31 - step)) > end) {
  33. break;
  34. }
  35. ++step;
  36. }
  37. struct in_addr prefix;
  38. prefix.s_addr = htonl(base);
  39. printf("%s/%d\n", inet_ntoa(prefix), 32 - step);
  40. base += 1 << step;
  41. }
  42. }